From: Jay Vosburgh Date: Tue, 16 Sep 2025 21:57:28 +0000 (-0700) Subject: lib: Update backend of print_size to accept 64 bit size X-Git-Tag: v6.18.0~14^2~3^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ee417ac43ee02fdc4458a647f3cf2e4f8a48d65;p=thirdparty%2Fiproute2.git lib: Update backend of print_size to accept 64 bit size In preparation for accepting 64 bit burst sizes, modify sprint_size, the formatting function behind print_size, to accept __u64 as its size parameter. Also include a "Gb" size category. Acked-by: Jamal Hadi Salim Signed-off-by: Jay Vosburgh Signed-off-by: David Ahern --- diff --git a/include/json_print.h b/include/json_print.h index daebcf5d..59edd5b2 100644 --- a/include/json_print.h +++ b/include/json_print.h @@ -68,7 +68,7 @@ _PRINT_FUNC(on_off, bool) _PRINT_FUNC(null, const char*) _PRINT_FUNC(string, const char*) _PRINT_FUNC(uint, unsigned int) -_PRINT_FUNC(size, __u32) +_PRINT_FUNC(size, __u64) _PRINT_FUNC(u64, uint64_t) _PRINT_FUNC(hhu, unsigned char) _PRINT_FUNC(hu, unsigned short) @@ -109,6 +109,6 @@ static inline int print_bool_opt(enum output_type type, } /* A backdoor to the size formatter. Please use print_size() instead. */ -char *sprint_size(__u32 sz, char *buf); +char *sprint_size(__u64 sz, char *buf); #endif /* _JSON_PRINT_H_ */ diff --git a/lib/json_print_math.c b/lib/json_print_math.c index f4d50499..3e951cd9 100644 --- a/lib/json_print_math.c +++ b/lib/json_print_math.c @@ -7,25 +7,28 @@ #include "utils.h" #include "json_print.h" -char *sprint_size(__u32 sz, char *buf) +char *sprint_size(__u64 sz, char *buf) { long kilo = 1024; long mega = kilo * kilo; + long giga = mega * kilo; size_t len = SPRINT_BSIZE - 1; double tmp = sz; - if (sz >= mega && fabs(mega * rint(tmp / mega) - sz) < 1024) + if (sz >= giga && fabs(giga * rint(tmp / giga) - sz) < 1024) + snprintf(buf, len, "%gGb", rint(tmp / giga)); + else if (sz >= mega && fabs(mega * rint(tmp / mega) - sz) < 1024) snprintf(buf, len, "%gMb", rint(tmp / mega)); else if (sz >= kilo && fabs(kilo * rint(tmp / kilo) - sz) < 16) snprintf(buf, len, "%gKb", rint(tmp / kilo)); else - snprintf(buf, len, "%ub", sz); + snprintf(buf, len, "%llub", sz); return buf; } int print_color_size(enum output_type type, enum color_attr color, - const char *key, const char *fmt, __u32 sz) + const char *key, const char *fmt, __u64 sz) { SPRINT_BUF(buf);