r23798: updated old Temple Place FSF addresses to new URL
[kai/samba.git] / source3 / lib / talloc / talloc.c
index 91e02d7e1e4fff8fe05e5f36765bea4c58e1dad7..6ebdada42f8ef17f7223a96e4f8d63b389f60cda 100644 (file)
@@ -15,7 +15,7 @@
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
+   version 3 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -23,8 +23,7 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
@@ -162,10 +161,17 @@ do { \
 /*
   return the parent chunk of a pointer
 */
-static struct talloc_chunk *talloc_parent_chunk(const void *ptr)
+static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
 {
-       struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
+       struct talloc_chunk *tc;
+
+       if (unlikely(ptr == NULL)) {
+               return NULL;
+       }
+
+       tc = talloc_chunk_from_ptr(ptr);
        while (tc->prev) tc=tc->prev;
+
        return tc->parent;
 }
 
@@ -178,23 +184,12 @@ void *talloc_parent(const void *ptr)
 /*
   find parents name
 */
-const char *talloc_parent_name(const void *context)
+const char *talloc_parent_name(const void *ptr)
 {
-       struct talloc_chunk *tc;
-
-       if (unlikely(context == NULL)) {
-               return NULL;
-       }
-
-       tc = talloc_chunk_from_ptr(context);
-       while (tc && tc->prev) tc = tc->prev;
-       if (tc) {
-               tc = tc->parent;
-       }
-       return tc->name;
+       struct talloc_chunk *tc = talloc_parent_chunk(ptr);
+       return tc? tc->name : NULL;
 }
 
-
 /* 
    Allocate a bit of memory as a child of an existing pointer
 */
@@ -265,6 +260,8 @@ int talloc_increase_ref_count(const void *ptr)
 
 /*
   helper for talloc_reference()
+
+  this is referenced by a function pointer and should not be inline
 */
 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
 {
@@ -481,7 +478,7 @@ void *_talloc_steal(const void *new_ctx, const void *ptr)
   talloc_reference() has done. The context and pointer arguments
   must match those given to a talloc_reference()
 */
-static int talloc_unreference(const void *context, const void *ptr)
+static inline int talloc_unreference(const void *context, const void *ptr)
 {
        struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
        struct talloc_reference_handle *h;
@@ -561,9 +558,9 @@ int talloc_unlink(const void *context, void *ptr)
 /*
   add a name to an existing pointer - va_list version
 */
-static const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
+static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
 
-static const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
+static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
 {
        struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
        tc->name = talloc_vasprintf(ptr, fmt, ap);
@@ -1088,7 +1085,6 @@ void *_talloc_zero(const void *ctx, size_t size, const char *name)
        return p;
 }
 
-
 /*
   memdup with a talloc. 
 */
@@ -1140,6 +1136,8 @@ char *talloc_append_string(const void *t, char *orig, const char *append)
        /* append the string with the trailing \0 */
        memcpy(&ret[olen], append, alenz);
 
+       _talloc_set_name_const(ret, ret);
+
        return ret;
 }
 
@@ -1176,10 +1174,11 @@ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
        va_list ap2;
        char c;
        
-       va_copy(ap2, ap);
-
        /* this call looks strange, but it makes it work on older solaris boxes */
-       if ((len = vsnprintf(&c, 1, fmt, ap2)) < 0) {
+       va_copy(ap2, ap);
+       len = vsnprintf(&c, 1, fmt, ap2);
+       va_end(ap2);
+       if (len < 0) {
                return NULL;
        }
 
@@ -1187,6 +1186,7 @@ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
        if (ret) {
                va_copy(ap2, ap);
                vsnprintf(ret, len+1, fmt, ap2);
+               va_end(ap2);
                _talloc_set_name_const(ret, ret);
        }
 
@@ -1228,10 +1228,13 @@ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
 
        tc = talloc_chunk_from_ptr(s);
 
+       s_len = tc->size - 1;
+
        va_copy(ap2, ap);
+       len = vsnprintf(&c, 1, fmt, ap2);
+       va_end(ap2);
 
-       s_len = tc->size - 1;
-       if ((len = vsnprintf(&c, 1, fmt, ap2)) <= 0) {
+       if (len <= 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
@@ -1245,8 +1248,8 @@ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
        if (!s) return NULL;
 
        va_copy(ap2, ap);
-
        vsnprintf(s+s_len, len+1, fmt, ap2);
+       va_end(ap2);
        _talloc_set_name_const(s, s);
 
        return s;
@@ -1289,7 +1292,6 @@ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const
        return _talloc_zero(ctx, el_size * count, name);
 }
 
-
 /*
   realloc an array, checking for integer overflow in the array size
 */