From 876e3f9380d373ba76f9b6d4c08ff84327956abf Mon Sep 17 00:00:00 2001 From: Zach Brown Date: Tue, 9 Jul 2013 13:24:43 -0700 Subject: btrfs-progs: per-thread, per-call pretty buffer We don't need callers to manage string storage for each pretty_sizes() call. We can use a macro to have per-thread and per-call static storage so that pretty_sizes() can be used as many times as needed in printf() arguments without requiring a bunch of supporting variables. This lets us have a natural interface at the cost of requiring __thread and TLS from gcc and a small amount of static storage. This seems better than the current code or doing something with illegible format specifier macros. Signed-off-by: Zach Brown Acked-by: Wang Shilong Signed-off-by: David Sterba Signed-off-by: Chris Mason --- utils.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'utils.c') diff --git a/utils.c b/utils.c index 1eeda0f7..ced85aaa 100644 --- a/utils.c +++ b/utils.c @@ -1152,13 +1152,14 @@ out: } static char *size_strs[] = { "", "KB", "MB", "GB", "TB", - "PB", "EB", "ZB", "YB"}; -char *pretty_sizes(u64 size) + "PB", "EB"}; +void pretty_size_snprintf(u64 size, char *str, size_t str_bytes) { int num_divs = 0; - int pretty_len = 16; float fraction; - char *pretty; + + if (str_bytes == 0) + return; if( size < 1024 ){ fraction = size; @@ -1172,13 +1173,13 @@ char *pretty_sizes(u64 size) num_divs ++; } - if (num_divs >= ARRAY_SIZE(size_strs)) - return NULL; + if (num_divs >= ARRAY_SIZE(size_strs)) { + str[0] = '\0'; + return; + } fraction = (float)last_size / 1024; } - pretty = malloc(pretty_len); - snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs]); - return pretty; + snprintf(str, str_bytes, "%.2f%s", fraction, size_strs[num_divs]); } /* -- cgit v1.2.3