diff options
Diffstat (limited to 'source3')
-rw-r--r-- | source3/include/talloc.h | 1 | ||||
-rw-r--r-- | source3/lib/talloc.c | 39 |
2 files changed, 37 insertions, 3 deletions
diff --git a/source3/include/talloc.h b/source3/include/talloc.h index 1001a95170..17032ac7ac 100644 --- a/source3/include/talloc.h +++ b/source3/include/talloc.h @@ -139,6 +139,7 @@ void *talloc_autofree_context(void); size_t talloc_get_size(const void *ctx); void *talloc_find_parent_byname(const void *ctx, const char *name); void talloc_show_parents(const void *context, FILE *file); +int talloc_is_parent(const void *context, const char *ptr); #endif diff --git a/source3/lib/talloc.c b/source3/lib/talloc.c index c111615506..51087ef979 100644 --- a/source3/lib/talloc.c +++ b/source3/lib/talloc.c @@ -540,7 +540,13 @@ int talloc_free(void *ptr) tc = talloc_chunk_from_ptr(ptr); if (tc->refs) { + int is_child; + struct talloc_reference_handle *handle = tc->refs; + is_child = talloc_is_parent(handle, handle->ptr); talloc_reference_destructor(tc->refs); + if (is_child) { + return talloc_free(ptr); + } return -1; } @@ -690,7 +696,7 @@ void *talloc_steal(const void *new_ctx, const void *ptr) new_tc = talloc_chunk_from_ptr(new_ctx); - if (tc == new_tc) { + if (tc == new_tc || tc->parent == new_tc) { return discard_const_p(void, ptr); } @@ -1278,7 +1284,10 @@ void *talloc_find_parent_byname(const void *context, const char *name) return TC_PTR_FROM_CHUNK(tc); } while (tc && tc->prev) tc = tc->prev; - tc = tc->parent; + if (tc) { + tc = tc->parent; + } + } return NULL; } @@ -1300,6 +1309,30 @@ void talloc_show_parents(const void *context, FILE *file) while (tc) { fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc))); while (tc && tc->prev) tc = tc->prev; - tc = tc->parent; + if (tc) { + tc = tc->parent; + } } } + +/* + return 1 if ptr is a parent of context +*/ +int talloc_is_parent(const void *context, const char *ptr) +{ + struct talloc_chunk *tc; + + if (context == NULL) { + return 0; + } + + tc = talloc_chunk_from_ptr(context); + while (tc) { + if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1; + while (tc && tc->prev) tc = tc->prev; + if (tc) { + tc = tc->parent; + } + } + return 0; +} |