1
0
mirror of https://github.com/samba-team/samba.git synced 2025-02-03 13:47:25 +03:00

talloc: Tune talloc_vasprintf

vsnprintf is significantly more expensive than memcpy. For the
common case where the string we print is less than a kilobyte, avoid
the second vsnprintf.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>

Autobuild-User(master): Jeremy Allison <jra@samba.org>
Autobuild-Date(master): Thu May 15 12:49:14 CEST 2014 on sn-devel-104
This commit is contained in:
Volker Lendecke 2014-01-10 10:45:22 +01:00 committed by Jeremy Allison
parent 5d998358e2
commit 593c8103af

View File

@ -2356,11 +2356,11 @@ _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
int len;
char *ret;
va_list ap2;
char c;
char buf[1024];
/* this call looks strange, but it makes it work on older solaris boxes */
va_copy(ap2, ap);
len = vsnprintf(&c, 1, fmt, ap2);
len = vsnprintf(buf, sizeof(buf), fmt, ap2);
va_end(ap2);
if (unlikely(len < 0)) {
return NULL;
@ -2369,9 +2369,13 @@ _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
ret = (char *)__talloc(t, len+1);
if (unlikely(!ret)) return NULL;
va_copy(ap2, ap);
vsnprintf(ret, len+1, fmt, ap2);
va_end(ap2);
if (len < sizeof(buf)) {
memcpy(ret, buf, len+1);
} else {
va_copy(ap2, ap);
vsnprintf(ret, len+1, fmt, ap2);
va_end(ap2);
}
_talloc_set_name_const(ret, ret);
return ret;