r7781: finding the parent of a talloc ptr is trickier than it looks due to the two-way
authorAndrew Tridgell <tridge@samba.org>
Mon, 20 Jun 2005 06:15:35 +0000 (06:15 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:18:36 +0000 (13:18 -0500)
tree nature of the data structure. I think I've finally got it right

also added talloc_show_parents() for debugging
(This used to be commit 5760ed20eed509b0b6e09e78c942dd0f70350fa9)

source4/lib/talloc/talloc.c
source4/lib/talloc/talloc.h

index 9dd7ef36c71092d6832739db082e6be37506782c..08d4cb93a6fc0b2471741955e3c9536178f99d58 100644 (file)
@@ -1121,14 +1121,33 @@ void *talloc_find_parent_byname(const void *context, const char *name)
        }
 
        tc = talloc_chunk_from_ptr(context);
-       while (tc->prev) {
-               tc = tc->prev;
-       }
-       while (tc->parent && (!tc->name || strcmp(tc->name, name))) {
+       while (tc) {
+               if (tc->name && strcmp(tc->name, name) == 0) {
+                       return (void*)(tc+1);
+               }
+               while (tc && tc->prev) tc = tc->prev;
                tc = tc->parent;
        }
-       if (tc == NULL || tc->name == NULL || strcmp(tc->name, name)) {
-               return NULL;
+       return NULL;
+}
+
+/*
+  show the parentage of a context
+*/
+void talloc_show_parents(const void *context, FILE *file)
+{
+       struct talloc_chunk *tc;
+
+       if (context == NULL) {
+               fprintf(file, "talloc no parents for NULL\n");
+               return;
+       }
+
+       tc = talloc_chunk_from_ptr(context);
+       fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
+       while (tc) {
+               fprintf(file, "\t'%s'\n", talloc_get_name(tc+1));
+               while (tc && tc->prev) tc = tc->prev;
+               tc = tc->parent;
        }
-       return (void *)(tc+1);
 }
index 5160a3874ad339516224cc67dac8214c1ccc6bb1..04d5cbb9afc9bfdc77e8db04cd033cfe8fdc2a91 100644 (file)
@@ -130,6 +130,7 @@ void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
 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);
 
 #endif