r16945: Sync trunk -> 3.0 for 3.0.24 code. Still need
[samba.git] / source3 / lib / talloc.c
index 445ae8f3e99acde87265d1fd70d69efd51073de5..9d82441137917eb3270912a25bbfc378d9a4b868 100644 (file)
@@ -85,6 +85,7 @@
 static const void *null_context;
 static void *cleanup_context;
 
+static void *talloc_steal(const void *new_ctx, const void *ptr);
 
 struct talloc_reference_handle {
        struct talloc_reference_handle *next, *prev;
@@ -289,7 +290,11 @@ static int talloc_unreference(const void *context, const void *ptr)
 
        for (h=tc->refs;h;h=h->next) {
                struct talloc_chunk *p = talloc_parent_chunk(h);
-               if ((p==NULL && context==NULL) || TC_PTR_FROM_CHUNK(p) == context) break;
+               if (p == NULL) {
+                       if (context == NULL) break;
+               } else if (TC_PTR_FROM_CHUNK(p) == context) {
+                       break;
+               }
        }
        if (h == NULL) {
                return -1;
@@ -536,7 +541,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;
        }
 
@@ -655,7 +666,7 @@ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *n
    ptr on success, or NULL if it could not be transferred.
    passing NULL as ptr will always return NULL with no side effects.
 */
-void *talloc_steal(const void *new_ctx, const void *ptr)
+static void *talloc_steal(const void *new_ctx, const void *ptr)
 {
        struct talloc_chunk *tc, *new_tc;
 
@@ -686,7 +697,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);
        }
 
@@ -878,6 +889,16 @@ static void talloc_report_null_full(void)
        }
 }
 
+/*
+ free allocated global memory
+*/
+
+void talloc_nc_free(void)
+{
+       if ( null_context )
+               talloc_free( (void*)null_context ); 
+}
+
 /*
   enable tracking of the NULL context
 */
@@ -1076,10 +1097,14 @@ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
        int len;
        char *ret;
        va_list ap2;
+       char c;
        
        VA_COPY(ap2, ap);
 
-       len = vsnprintf(NULL, 0, fmt, ap2);
+       /* this call looks strange, but it makes it work on older solaris boxes */
+       if ((len = vsnprintf(&c, 1, fmt, ap2)) < 0) {
+               return NULL;
+       }
 
        ret = _talloc(t, len+1);
        if (ret) {
@@ -1114,9 +1139,7 @@ char *talloc_asprintf(const void *t, const char *fmt, ...)
  * accumulating output into a string buffer.
  **/
 
-static char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
-
-static char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
+char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
 {      
        struct talloc_chunk *tc;
        int len, s_len;
@@ -1131,7 +1154,15 @@ static char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
        VA_COPY(ap2, ap);
 
        s_len = tc->size - 1;
-       len = vsnprintf(NULL, 0, fmt, ap2);
+       if ((len = vsnprintf(NULL, 0, fmt, ap2)) <= 0) {
+               /* Either the vsnprintf failed or the format resulted in
+                * no characters being formatted. In the former case, we
+                * ought to return NULL, in the latter we ought to return
+                * the original string. Most current callers of this 
+                * function expect it to never return NULL.
+                */
+               return s;
+       }
 
        s = talloc_realloc(NULL, s, char, s_len + len+1);
        if (!s) return NULL;
@@ -1252,7 +1283,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;
 }
@@ -1274,6 +1308,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;
 }