diff options
author | Daniel Stenberg <daniel@haxx.se> | 2020-09-22 17:28:34 +0200 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2020-09-23 08:54:42 +0200 |
commit | 7e8561e030fcfaba5cd620d7e9c9ab8356163132 (patch) | |
tree | bede470aac7ebaea20edbd604624050cac54fb7e /lib/dynbuf.c | |
parent | 23558577023538a17c70933acc5de04bf350d9c2 (diff) | |
download | curl-7e8561e030fcfaba5cd620d7e9c9ab8356163132.tar.gz |
dynbuf: make *addf() not require extra mallocs
... by introducing a printf() function that appends directly into a
dynbuf: Curl_dyn_vprintf(). This avoids the mandatory extra malloc so if
the buffer is already big enough it can just printf directly into it.
Since this less-malloc version requires tthe use of a library internal
printf function, we only provide this version when building libcurl and
not for the dynbuf code that is used when building the curl tool.
Closes #5998
Diffstat (limited to 'lib/dynbuf.c')
-rw-r--r-- | lib/dynbuf.c | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/lib/dynbuf.c b/lib/dynbuf.c index 5e15040bb..2dace3428 100644 --- a/lib/dynbuf.c +++ b/lib/dynbuf.c @@ -181,8 +181,17 @@ CURLcode Curl_dyn_add(struct dynbuf *s, const char *str) */ CURLcode Curl_dyn_addf(struct dynbuf *s, const char *fmt, ...) { - char *str; va_list ap; +#ifdef BUILDING_LIBCURL + int rc; + va_start(ap, fmt); + rc = Curl_dyn_vprintf(s, fmt, ap); + va_end(ap); + + if(!rc) + return CURLE_OK; +#else + char *str; va_start(ap, fmt); str = vaprintf(fmt, ap); /* this allocs a new string to append */ va_end(ap); @@ -194,9 +203,12 @@ CURLcode Curl_dyn_addf(struct dynbuf *s, const char *fmt, ...) } /* If we failed, we cleanup the whole buffer and return error */ Curl_dyn_free(s); +#endif return CURLE_OUT_OF_MEMORY; } + + /* * Returns a pointer to the buffer. */ |