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
This commit is contained in:
Daniel Stenberg 2020-09-22 17:28:34 +02:00
parent 2355857702
commit 7e8561e030
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
4 changed files with 45 additions and 18 deletions

View file

@ -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.
*/