From d51310678e4ce59b94576bd294118f62c4a8a158 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 24 Sep 2024 14:42:30 +0000 Subject: [PATCH] util: Statically allocate size buffer Signed-off-by: Michael Tremer --- src/tui.c | 18 ++++++++++++------ src/util.c | 23 ++++++++++++----------- src/util.h | 6 +++++- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/tui.c b/src/tui.c index 96863f7..ef23556 100644 --- a/src/tui.c +++ b/src/tui.c @@ -114,6 +114,7 @@ static int fireperf_tui_setup_frame(struct fireperf_tui* tui) { static int fireperf_tui_update_status(struct fireperf_tui* tui) { const struct fireperf_stats* stats = NULL; + char buffer[32]; int r; int max_x = 0; @@ -142,11 +143,16 @@ static int fireperf_tui_update_status(struct fireperf_tui* tui) { if (r < 0) goto ERROR; + // Fetch peak bandwidth size_t peak_bps = fireperf_tui_get_peak_bps(tui, max_x); + // Format peak bandwidth + r = format_size(buffer, peak_bps, FIREPERF_FORMAT_BITS); + if (r < 0) + goto ERROR; + // Print Peak Bandwidth - r = asprintf(&status_l, "%s, Peak Bandwidth: %s", status_l, - format_size(peak_bps, FIREPERF_FORMAT_BITS)); + r = asprintf(&status_l, "%s, Peak Bandwidth: %s", status_l, buffer); if (r < 0) goto ERROR; @@ -340,7 +346,8 @@ int fireperf_tui_action(struct fireperf_tui* tui) { */ static int fireperf_tui_draw_graph(struct fireperf_tui* tui) { const struct fireperf_stats* stats = NULL; - char* scale = NULL; + char scale[32]; + int r; int max_x = 0; int max_y = 0; @@ -361,8 +368,8 @@ static int fireperf_tui_draw_graph(struct fireperf_tui* tui) { // Draw the grid for (double line = 0; line <= 1; line += 0.25) { - scale = format_size(line * peak_bps, FIREPERF_FORMAT_BITS); - if (!scale) + r = format_size(scale, line * peak_bps, FIREPERF_FORMAT_BITS); + if (r < 0) continue; size_t l = strlen(scale); @@ -374,7 +381,6 @@ static int fireperf_tui_draw_graph(struct fireperf_tui* tui) { // Write the scale mvwaddnstr(tui->graph, max_y - (max_y * line), max_x - l, scale, l); - free(scale); } wattroff(tui->graph, COLOR_PAIR(5)); diff --git a/src/util.c b/src/util.c index 558a478..de4715e 100644 --- a/src/util.c +++ b/src/util.c @@ -18,6 +18,7 @@ # # #############################################################################*/ +#include #include #include #include @@ -28,9 +29,10 @@ static const char* suffixes_bytes[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL }; static const char* suffixes_bits[] = { "b", "Kb", "Mb", "Gb", "Tb", NULL }; -char* format_size(ssize_t size, int unit) { - const char** suffixes; +int __format_size(char* buffer, const size_t length, ssize_t size, const int unit) { + const char** suffixes = NULL; unsigned int divisor; + int r; switch (unit) { case FIREPERF_FORMAT_BYTES: @@ -45,11 +47,10 @@ char* format_size(ssize_t size, int unit) { // Invalid input default: - return NULL; + return -EINVAL; } - const char** suffix; - char* retval = NULL; + const char** suffix = NULL; // Convert into double double s = size; @@ -62,19 +63,19 @@ char* format_size(ssize_t size, int unit) { } if (!*suffix) - return NULL; + return -EINVAL; // Format the output string - int r = asprintf(&retval, "%.02f %s", s, *suffix); + r = snprintf(buffer, length, "%.02f %s", s, *suffix); if (r < 0) - return NULL; + return r; - return retval; + return 0; } -static char __timespec[20]; - const char* format_timespec(const struct timespec* t) { + static char __thread __timespec[20]; + // Convert to local time struct tm* tm = localtime(&t->tv_sec); diff --git a/src/util.h b/src/util.h index 8315b94..32c3f2a 100644 --- a/src/util.h +++ b/src/util.h @@ -26,6 +26,10 @@ enum { FIREPERF_FORMAT_BITS, }; -char* format_size(ssize_t size, int unit); +#define format_size(buffer, size, unit) \ + __format_size(buffer, sizeof(buffer), size, unit) + +int __format_size(char* buffer, const size_t length, ssize_t size, const int unit); + const char* format_timespec(const struct timespec* t); double timespec_delta(struct timespec* t1, struct timespec* t2); -- 2.47.2