mirror of
https://github.com/curl/curl.git
synced 2026-08-24 20:23:40 +03:00
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:
parent
2355857702
commit
7e8561e030
4 changed files with 45 additions and 18 deletions
14
lib/dynbuf.c
14
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.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue