diff options
author | Andrew Tridgell <tridge@samba.org> | 2004-10-03 00:04:30 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 12:59:34 -0500 |
commit | 15b9736ed30d8e947dbe2513dd9cf27d5b3761af (patch) | |
tree | 1b5ab1fbdf06e67b67eb8452eac970aac80fe4ad /source4/lib/talloc.c | |
parent | a248164de5f89cef824f5a1f7d8618fbe81ae0c4 (diff) | |
download | samba-15b9736ed30d8e947dbe2513dd9cf27d5b3761af.tar.gz samba-15b9736ed30d8e947dbe2513dd9cf27d5b3761af.tar.bz2 samba-15b9736ed30d8e947dbe2513dd9cf27d5b3761af.zip |
r2791: got rid of talloc_unreference() and instead created talloc_unlink(),
which is much clearer and simpler to use. It removes a specific parent
from a pointer, no matter whether that parent is a "reference" or a
direct parent. This gives complete control over the free process.
(This used to be commit 6c563887f1b9b8c842309a523e88b6f2a32db10f)
Diffstat (limited to 'source4/lib/talloc.c')
-rw-r--r-- | source4/lib/talloc.c | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/source4/lib/talloc.c b/source4/lib/talloc.c index 4032039142..fc65546063 100644 --- a/source4/lib/talloc.c +++ b/source4/lib/talloc.c @@ -241,7 +241,7 @@ void *talloc_reference(const void *context, const void *ptr) talloc_reference() has done. The context and pointer arguments must match those given to a talloc_reference() */ -void *talloc_unreference(const void *context, const void *ptr) +static int talloc_unreference(const void *context, const void *ptr) { struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr); struct talloc_reference_handle *h; @@ -255,13 +255,58 @@ void *talloc_unreference(const void *context, const void *ptr) if ((p==NULL && context==NULL) || p+1 == context) break; } if (h == NULL) { - return NULL; + return -1; } talloc_set_destructor(h, NULL); _TLIST_REMOVE(tc->refs, h); talloc_free(h); - return discard_const_p(void, ptr); + return 0; +} + +/* + remove a specific parent context from a pointer. This is a more + controlled varient of talloc_free() +*/ +int talloc_unlink(const void *context, void *ptr) +{ + struct talloc_chunk *tc_p, *new_p; + void *new_parent; + + if (talloc_unreference(context, ptr) == 0) { + return 0; + } + + if (context == NULL) { + if (talloc_parent_chunk(ptr) != NULL) { + return -1; + } + } else { + if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) { + return -1; + } + } + + tc_p = talloc_chunk_from_ptr(ptr); + + if (tc_p->refs == NULL) { + return talloc_free(ptr); + } + + new_p = talloc_parent_chunk(tc_p->refs); + if (new_p) { + new_parent = new_p+1; + } else { + new_parent = NULL; + } + + if (talloc_unreference(new_parent, ptr) != 0) { + return -1; + } + + talloc_steal(new_parent, ptr); + + return 0; } /* |