From: Rusty Russell Date: Mon, 4 Aug 2008 03:44:18 +0000 (+1000) Subject: Fix talloc external alloc parent pointer. X-Git-Url: https://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=0f126c41cd2e9ccaff5fb7633c079db0bb0e14c3 Fix talloc external alloc parent pointer. Fix autofree context not to be done in fork() child. Fix comment above talloc_array_size. --- diff --git a/ccan/talloc/talloc.c b/ccan/talloc/talloc.c index 59a981b4..ba998f54 100644 --- a/ccan/talloc/talloc.c +++ b/ccan/talloc/talloc.c @@ -35,6 +35,8 @@ #include #include #include +#include +#include /* use this to force every realloc to change the pointer, to stress test code that might not cope */ @@ -78,7 +80,7 @@ NULL */ static void *null_context; -static void *autofree_context; +static pid_t *autofree_context; static void *(*tc_external_alloc)(void *parent, size_t size); static void (*tc_external_free)(void *ptr, void *parent); @@ -378,8 +380,10 @@ static inline int _talloc_free(void *ptr) tc->destructor = NULL; } + if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC)) + oldparent = talloc_parent(ptr); + if (tc->parent) { - oldparent = TC_PTR_FROM_CHUNK(tc->parent); _TLIST_REMOVE(tc->parent->child, tc); if (tc->parent->child) { tc->parent->child->parent = tc->parent; @@ -1334,7 +1338,8 @@ static int talloc_autofree_destructor(void *ptr) static void talloc_autofree(void) { - _talloc_free(autofree_context); + if (autofree_context && *autofree_context == getpid()) + _talloc_free(autofree_context); } /* @@ -1343,8 +1348,11 @@ static void talloc_autofree(void) */ void *talloc_autofree_context(void) { - if (autofree_context == NULL) { - autofree_context = _talloc_named_const(NULL, 0, "autofree_context"); + if (autofree_context == NULL || *autofree_context != getpid()) { + autofree_context = talloc(NULL, pid_t); + *autofree_context = getpid(); + talloc_set_name_const(autofree_context, "autofree_context"); + talloc_set_destructor(autofree_context, talloc_autofree_destructor); atexit(talloc_autofree); } diff --git a/ccan/talloc/talloc.h b/ccan/talloc/talloc.h index da416a4b..8b0f8a38 100644 --- a/ccan/talloc/talloc.h +++ b/ccan/talloc/talloc.h @@ -442,12 +442,12 @@ void talloc_free_children(void *ptr); #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type) /** - * talloc_zero_array - allocate an array of zeroed types + * talloc_array_size - allocate an array of elements of the given size * @ctx: context to be parent of this allocation, or NULL. - * @type: the type to be allocated. + * @size: the size of each element * @count: the number of elements to be allocated. * - * Just like talloc_array, but zeroes the memory. + * Typeless form of talloc_array. */ #define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)