summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2001-10-14 09:32:14 +0000
committerAndrew Tridgell <tridge@samba.org>2001-10-14 09:32:14 +0000
commit47a0b69029857e92703ca5b00022dcfcb179c9c7 (patch)
tree2fe7b82eed3343121ca7098a34edb4ed90ffae5a
parent0567d2d969af381fc691a5e3b02ed4a0af2383de (diff)
downloadsamba-47a0b69029857e92703ca5b00022dcfcb179c9c7.tar.gz
samba-47a0b69029857e92703ca5b00022dcfcb179c9c7.tar.bz2
samba-47a0b69029857e92703ca5b00022dcfcb179c9c7.zip
bit neater talloc_asprintf() implementation
(This used to be commit 8ca8875cd94dce8827ce8cb857b2d7a959ab7891)
-rw-r--r--source3/lib/talloc.c30
1 files changed, 11 insertions, 19 deletions
diff --git a/source3/lib/talloc.c b/source3/lib/talloc.c
index 05f4351aaf..7bfabdd88d 100644
--- a/source3/lib/talloc.c
+++ b/source3/lib/talloc.c
@@ -169,30 +169,22 @@ char *talloc_strdup(TALLOC_CTX *t, char *p)
/* allocate a bit of memory from the specified pool */
char *talloc_asprintf(TALLOC_CTX *t, const char *fmt, ...)
{
- struct talloc_chunk *tc;
va_list ap;
- char *str;
- size_t ret;
-
- tc = malloc(sizeof(*tc));
- if (!tc)
- return NULL;
+ int len;
+ char *ret;
+ /* work out how long it will be */
va_start(ap, fmt);
- ret = vasprintf(&str, fmt, ap);
+ len = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
- if (ret <= 0)
- {
- SAFE_FREE(tc);
- return NULL;
- }
+
+ ret = talloc(t, len);
+ if (!ret) return NULL;
- tc->ptr = str;
- tc->size = ret + 1;
- tc->next = t->list;
- t->list = tc;
- t->total_alloc_size += tc->size;
+ va_start(ap, fmt);
+ vsnprintf(ret, len, fmt, ap);
+ va_end(ap);
- return str;
+ return ret;
}