From 3c2b7777ed5e1b873b98eb3d9e424308936ca17b Mon Sep 17 00:00:00 2001 From: Martin Pool Date: Thu, 3 Jan 2002 05:25:30 +0000 Subject: 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) --- source3/lib/talloc.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 4 deletions(-) (limited to 'source3/lib/talloc.c') 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 + Copyright (C) 2001, 2002 by Martin Pool 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. + **/ + + + /** @} */ -- cgit