talloc: splitout _talloc_free_children_internal()
authorStefan Metzmacher <metze@samba.org>
Tue, 17 May 2011 06:50:45 +0000 (08:50 +0200)
committerStefan Metzmacher <metze@samba.org>
Tue, 17 May 2011 08:49:13 +0000 (10:49 +0200)
metze

Autobuild-User: Stefan Metzmacher <metze@samba.org>
Autobuild-Date: Tue May 17 10:49:13 CEST 2011 on sn-devel-104

lib/talloc/talloc.c

index 8333f41c07d62a7453cdf25cee9aaa3d28e2c55f..4700aa99e8c472314f3fbb4d68046d42062cbc79 100644 (file)
@@ -768,6 +768,10 @@ static inline void _talloc_free_poolmem(struct talloc_chunk *tc,
        }
 }
 
+static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
+                                                 void *ptr,
+                                                 const char *location);
+
 /* 
    internal talloc_free call
 */
@@ -838,41 +842,7 @@ static inline int _talloc_free_internal(void *ptr, const char *location)
 
        tc->flags |= TALLOC_FLAG_LOOP;
 
-       while (tc->child) {
-               /* we need to work out who will own an abandoned child
-                  if it cannot be freed. In priority order, the first
-                  choice is owner of any remaining reference to this
-                  pointer, the second choice is our parent, and the
-                  final choice is the null context. */
-               void *child = TC_PTR_FROM_CHUNK(tc->child);
-               const void *new_parent = null_context;
-               struct talloc_chunk *old_parent = NULL;
-               if (unlikely(tc->child->refs)) {
-                       struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
-                       if (p) new_parent = TC_PTR_FROM_CHUNK(p);
-               }
-               /* finding the parent here is potentially quite
-                  expensive, but the alternative, which is to change
-                  talloc to always have a valid tc->parent pointer,
-                  makes realloc more expensive where there are a
-                  large number of children.
-
-                  The reason we need the parent pointer here is that
-                  if _talloc_free_internal() fails due to references
-                  or a failing destructor we need to re-parent, but
-                  the free call can invalidate the prev pointer.
-               */
-               if (new_parent == null_context && (tc->child->refs || tc->child->destructor)) {
-                       old_parent = talloc_parent_chunk(ptr);
-               }
-               if (unlikely(_talloc_free_internal(child, location) == -1)) {
-                       if (new_parent == null_context) {
-                               struct talloc_chunk *p = old_parent;
-                               if (p) new_parent = TC_PTR_FROM_CHUNK(p);
-                       }
-                       _talloc_steal_internal(new_parent, child);
-               }
-       }
+       _talloc_free_children_internal(tc, ptr, location);
 
        tc->flags |= TALLOC_FLAG_FREE;
 
@@ -1264,21 +1234,10 @@ _PUBLIC_ void *talloc_init(const char *fmt, ...)
        return ptr;
 }
 
-/*
-  this is a replacement for the Samba3 talloc_destroy_pool functionality. It
-  should probably not be used in new code. It's in here to keep the talloc
-  code consistent across Samba 3 and 4.
-*/
-_PUBLIC_ void talloc_free_children(void *ptr)
+static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
+                                                 void *ptr,
+                                                 const char *location)
 {
-       struct talloc_chunk *tc;
-
-       if (unlikely(ptr == NULL)) {
-               return;
-       }
-
-       tc = talloc_chunk_from_ptr(ptr);
-
        while (tc->child) {
                /* we need to work out who will own an abandoned child
                   if it cannot be freed. In priority order, the first
@@ -1306,7 +1265,7 @@ _PUBLIC_ void talloc_free_children(void *ptr)
                if (new_parent == null_context && (tc->child->refs || tc->child->destructor)) {
                        old_parent = talloc_parent_chunk(ptr);
                }
-               if (unlikely(_talloc_free_internal(child, __location__) == -1)) {
+               if (unlikely(_talloc_free_internal(child, location) == -1)) {
                        if (new_parent == null_context) {
                                struct talloc_chunk *p = old_parent;
                                if (p) new_parent = TC_PTR_FROM_CHUNK(p);
@@ -1316,6 +1275,24 @@ _PUBLIC_ void talloc_free_children(void *ptr)
        }
 }
 
+/*
+  this is a replacement for the Samba3 talloc_destroy_pool functionality. It
+  should probably not be used in new code. It's in here to keep the talloc
+  code consistent across Samba 3 and 4.
+*/
+_PUBLIC_ void talloc_free_children(void *ptr)
+{
+       struct talloc_chunk *tc;
+
+       if (unlikely(ptr == NULL)) {
+               return;
+       }
+
+       tc = talloc_chunk_from_ptr(ptr);
+
+       _talloc_free_children_internal(tc, ptr, __location__);
+}
+
 /* 
    Allocate a bit of memory as a child of an existing pointer
 */