diff options
author | Martin Pool <mbp@samba.org> | 2002-01-03 05:25:30 +0000 |
---|---|---|
committer | Martin Pool <mbp@samba.org> | 2002-01-03 05:25:30 +0000 |
commit | 3c2b7777ed5e1b873b98eb3d9e424308936ca17b (patch) | |
tree | dab9715807ce96a5013eddb4c17f63882119d5b8 /source3/lib | |
parent | 9c409a9e397fad052606d7416cbb70f077d52cfd (diff) | |
download | samba-3c2b7777ed5e1b873b98eb3d9e424308936ca17b.tar.gz samba-3c2b7777ed5e1b873b98eb3d9e424308936ca17b.tar.bz2 samba-3c2b7777ed5e1b873b98eb3d9e424308936ca17b.zip |
Add talloc_asprintf_append, which grows an existing string buffer to
contain new print-formatted information. (Also
talloc_vasprintf_append.) Idea borrowed from glib.
(This used to be commit 53723e874885936dd67483ebf46601fc73489d17)
Diffstat (limited to 'source3/lib')
-rw-r--r-- | source3/lib/talloc.c | 80 |
1 files changed, 76 insertions, 4 deletions
diff --git a/source3/lib/talloc.c b/source3/lib/talloc.c index 8496170daf..3b4c3e9d15 100644 --- a/source3/lib/talloc.c +++ b/source3/lib/talloc.c @@ -1,9 +1,8 @@ /* - Unix SMB/Netbios implementation. - Version 3.0 + Samba Unix SMB/Netbios implementation. Samba temporary memory allocation functions Copyright (C) Andrew Tridgell 2000 - Copyright (C) 2001 by Martin Pool <mbp@samba.org> + Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -286,7 +285,6 @@ char *talloc_strdup(TALLOC_CTX *t, const char *p) va_list ap; char *ret; - /* work out how long it will be */ va_start(ap, fmt); ret = talloc_vasprintf(t, fmt, ap); va_end(ap); @@ -310,4 +308,78 @@ char *talloc_strdup(TALLOC_CTX *t, const char *p) } +/** + * Realloc @p s to append the formatted result of @p fmt and return @p + * s, which may have moved. Good for gradually accumulating output + * into a string buffer. + **/ + char *talloc_asprintf_append(TALLOC_CTX *t, char *s, + const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + s = talloc_vasprintf_append(t, s, fmt, ap); + va_end(ap); + return s; +} + + + +/** + * Realloc @p s to append the formatted result of @p fmt and @p ap, + * and return @p s, which may have moved. Good for gradually + * accumulating output into a string buffer. + **/ + char *talloc_vasprintf_append(TALLOC_CTX *t, char *s, + const char *fmt, va_list ap) +{ + int len, s_len; + + s_len = strlen(s); + len = vsnprintf(NULL, 0, fmt, ap); + + s = talloc_realloc(t, s, s_len + len+1); + if (!s) return NULL; + + vsnprintf(s+s_len, len+1, fmt, ap); + + return s; +} + + +/** + * Return a human-readable description of all talloc memory usage. + * The result is allocated from @p t. + **/ +char *talloc_describe_all(TALLOC_CTX *t) +{ + int n_pools = 0; + size_t total = 0; + TALLOC_CTX *titer; + char *s; + + s = talloc_asprintf(t, "global talloc allocations in pid%u:\n", + (unsigned) getpid()); + s = talloc_asprintf_append(t, s, "name\n----\n"); + + for (titer = list_head; titer; titer = titer->next_ctx) { + n_pools++; + s = talloc_asprintf_append(t, s, "\"%s\"\n", titer->name); + } + + s = talloc_asprintf_append(t, s, "-----\nTotal %d pools\n", n_pools); + + return s; +} + + + +/** + * Return an estimated memory usage for the specified pool. This does + * not include memory used by the underlying malloc implementation. + **/ + + + /** @} */ |