]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
lib: Update backend of print_size to accept 64 bit size
authorJay Vosburgh <jay.vosburgh@canonical.com>
Tue, 16 Sep 2025 21:57:28 +0000 (14:57 -0700)
committerDavid Ahern <dsahern@kernel.org>
Thu, 18 Sep 2025 02:18:40 +0000 (02:18 +0000)
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 <jhs@mojatatu.com>
Signed-off-by: Jay Vosburgh <jay.vosburgh@canonical.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
include/json_print.h
lib/json_print_math.c

index daebcf5d25f5967346806518159bf1c4866863d4..59edd5b2467edabd5cb9c61832bdec9c2a5d86fe 100644 (file)
@@ -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_ */
index f4d50499592433bb1b05a047969e663c3d296387..3e951cd9f5046dad1c755368efea81f230c867ba 100644 (file)
@@ -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);