summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Allison <jra@samba.org>2006-05-23 15:57:26 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 11:17:10 -0500
commit07c8c98cad2badc33133082a925179160c64bd57 (patch)
treee7b2da1d7d90edcf80f98a2e97736ccb75142163
parentec3021dc3bca5901c70b377c6205c37ce63011df (diff)
downloadsamba-07c8c98cad2badc33133082a925179160c64bd57.tar.gz
samba-07c8c98cad2badc33133082a925179160c64bd57.tar.bz2
samba-07c8c98cad2badc33133082a925179160c64bd57.zip
r15838: Back-port tridge's talloc fixes (r15824, r15828) from Samba4.
Jeremy. (This used to be commit f6c110ddb8cfaa1a57dea52818e7611134c2dcfe)
-rw-r--r--source3/include/talloc.h1
-rw-r--r--source3/lib/talloc.c39
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;
+}