From b7e1ea20dc873a753ff64653987130f03897a4e9 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 21 Aug 2004 07:43:29 +0000 Subject: r1985: take advantage of the new talloc in a few more places (This used to be commit 6ffdfd779936ce8c5ca49c5f444e8da2bbeee0a8) --- source4/lib/talloc.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/talloc.c b/source4/lib/talloc.c index 8a67f83a7f..b01bcb27c8 100644 --- a/source4/lib/talloc.c +++ b/source4/lib/talloc.c @@ -20,6 +20,10 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +/* + inspired by http://swapped.cc/halloc/ +*/ + #include "includes.h" #define MAX_TALLOC_SIZE 0x10000000 @@ -81,24 +85,49 @@ void *talloc(void *context, size_t size) /* - create a named talloc pointer + add a name to an existing pointer - va_list version +*/ +static void talloc_set_name_v(void *ptr, const char *fmt, va_list ap) +{ + struct talloc_chunk *tc; + + tc = ((struct talloc_chunk *)ptr)-1; + if (tc->magic != TALLOC_MAGIC) { + return; + } + + vasprintf(&tc->name, fmt, ap); +} + +/* + add a name to an existing pointer +*/ +void talloc_set_name(void *ptr, const char *fmt, ...) _PRINTF_ATTRIBUTE(2,3) +{ + va_list ap; + va_start(ap, fmt); + talloc_set_name_v(ptr, fmt, ap); + va_end(ap); +} + +/* + create a named talloc pointer. Any talloc pointer can be named, and + talloc_named() operates just like talloc() except that it allows you + to name the pointer. */ void *talloc_named(void *context, size_t size, const char *fmt, ...) _PRINTF_ATTRIBUTE(3,4) { va_list ap; void *ptr; - struct talloc_chunk *tc; ptr = talloc(context, size); if (ptr == NULL) { return NULL; } - tc = ((struct talloc_chunk *)ptr)-1; - va_start(ap, fmt); - vasprintf(&tc->name, fmt, ap); + talloc_set_name_v(ptr, fmt, ap); va_end(ap); return ptr; -- cgit