From 0cbe438ac199abae3a890c72fa25c4e24fbbfd66 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 17 Mar 2020 15:04:07 +0900 Subject: [PATCH] [util] Make va slight robust against chained use va now cycles through a set of four dstrings rather than using just the one. This allows for some simple chaining of va usage. --- libs/util/va.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/libs/util/va.c b/libs/util/va.c index aa99f7955..dd24c05d1 100644 --- a/libs/util/va.c +++ b/libs/util/va.c @@ -51,16 +51,23 @@ VISIBLE char * va (const char *fmt, ...) { va_list args; - static dstring_t *string; + static dstring_t *string[4]; +#define NUM_STRINGS (sizeof (string) / sizeof (string[0])) + static int str_index; + dstring_t *dstr; - if (!string) - string = dstring_new (); + if (!string[0]) { + for (size_t i = 0; i < NUM_STRINGS; i++) { + string[i] = dstring_new (); + } + } + dstr = string[str_index++ % NUM_STRINGS]; va_start (args, fmt); - dvsprintf (string, fmt, args); + dvsprintf (dstr, fmt, args); va_end (args); - return string->str; + return dstr->str; } VISIBLE char *