2 Samba Unix SMB/CIFS implementation.
4 Samba trivial allocation library - new interface
6 NOTE: Please read talloc_guide.txt for full documentation
8 Copyright (C) Andrew Tridgell 2004
9 Copyright (C) Stefan Metzmacher 2006
11 ** NOTE! The following LGPL license applies to the talloc
12 ** library. This does NOT imply that all of Samba is released
15 This library is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Lesser General Public
17 License as published by the Free Software Foundation; either
18 version 3 of the License, or (at your option) any later version.
20 This library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Lesser General Public License for more details.
25 You should have received a copy of the GNU Lesser General Public
26 License along with this library; if not, see <http://www.gnu.org/licenses/>.
30 inspired by http://swapped.cc/halloc/
36 #include <atalk/compat.h>
37 #include <atalk/util.h>
38 #include <atalk/talloc.h>
40 #ifdef TALLOC_BUILD_VERSION_MAJOR
41 #if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)
42 #error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"
46 #ifdef TALLOC_BUILD_VERSION_MINOR
47 #if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)
48 #error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"
52 /* use this to force every realloc to change the pointer, to stress test
53 code that might not cope */
54 #define ALWAYS_REALLOC 0
57 #define MAX_TALLOC_SIZE 0x10000000
58 #define TALLOC_MAGIC_BASE 0xe814ec70
59 #define TALLOC_MAGIC ( \
61 (TALLOC_VERSION_MAJOR << 12) + \
62 (TALLOC_VERSION_MINOR << 4) \
65 #define TALLOC_FLAG_FREE 0x01
66 #define TALLOC_FLAG_LOOP 0x02
67 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
68 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
69 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
71 /* by default we abort when given a bad pointer (such as when talloc_free() is called
72 on a pointer that came from malloc() */
74 #define TALLOC_ABORT(reason) abort()
77 #ifndef discard_const_p
78 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
79 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
81 # define discard_const_p(type, ptr) ((type *)(ptr))
85 /* these macros gain us a few percent of speed on gcc */
87 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
88 as its first argument */
90 #define likely(x) __builtin_expect(!!(x), 1)
93 #define unlikely(x) __builtin_expect(!!(x), 0)
100 #define unlikely(x) (x)
104 /* this null_context is only used if talloc_enable_leak_report() or
105 talloc_enable_leak_report_full() is called, otherwise it remains
108 static void *null_context;
109 static void *autofree_context;
111 struct talloc_reference_handle {
112 struct talloc_reference_handle *next, *prev;
114 const char *location;
117 typedef int (*talloc_destructor_t)(void *);
119 struct talloc_chunk {
120 struct talloc_chunk *next, *prev;
121 struct talloc_chunk *parent, *child;
122 struct talloc_reference_handle *refs;
123 talloc_destructor_t destructor;
129 * "pool" has dual use:
131 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
132 * marks the end of the currently allocated area.
134 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
135 * is a pointer to the struct talloc_chunk of the pool that it was
136 * allocated from. This way children can quickly find the pool to chew
142 /* 16 byte alignment seems to keep everyone happy */
143 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
144 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
146 int talloc_version_major(void)
148 return TALLOC_VERSION_MAJOR;
151 int talloc_version_minor(void)
153 return TALLOC_VERSION_MINOR;
156 static void (*talloc_log_fn)(const char *message);
158 void talloc_set_log_fn(void (*log_fn)(const char *message))
160 talloc_log_fn = log_fn;
163 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
164 static void talloc_log(const char *fmt, ...)
169 if (!talloc_log_fn) {
174 message = talloc_vasprintf(NULL, fmt, ap);
177 talloc_log_fn(message);
178 talloc_free(message);
181 static void talloc_log_stderr(const char *message)
183 fprintf(stderr, "%s", message);
186 void talloc_set_log_stderr(void)
188 talloc_set_log_fn(talloc_log_stderr);
191 static void (*talloc_abort_fn)(const char *reason);
193 void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
195 talloc_abort_fn = abort_fn;
198 static void talloc_abort(const char *reason)
200 talloc_log("%s\n", reason);
202 if (!talloc_abort_fn) {
203 TALLOC_ABORT(reason);
206 talloc_abort_fn(reason);
209 static void talloc_abort_magic(unsigned magic)
211 unsigned striped = magic - TALLOC_MAGIC_BASE;
212 unsigned major = (striped & 0xFFFFF000) >> 12;
213 unsigned minor = (striped & 0x00000FF0) >> 4;
214 talloc_log("Bad talloc magic[0x%08X/%u/%u] expected[0x%08X/%u/%u]\n",
216 TALLOC_MAGIC, TALLOC_VERSION_MAJOR, TALLOC_VERSION_MINOR);
217 talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
220 static void talloc_abort_double_free(void)
222 talloc_abort("Bad talloc magic value - double free");
225 static void talloc_abort_unknown_value(void)
227 talloc_abort("Bad talloc magic value - unknown value");
230 /* panic if we get a bad magic value */
231 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
233 const char *pp = (const char *)ptr;
234 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
235 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
236 if ((tc->flags & (~0xFFF)) == TALLOC_MAGIC_BASE) {
237 talloc_abort_magic(tc->flags & (~0xF));
241 if (tc->flags & TALLOC_FLAG_FREE) {
242 talloc_log("talloc: double free error - first free may be at %s\n", tc->name);
243 talloc_abort_double_free();
246 talloc_abort_unknown_value();
253 /* hook into the front of the list */
254 #define _TLIST_ADD(list, p) \
258 (p)->next = (p)->prev = NULL; \
260 (list)->prev = (p); \
261 (p)->next = (list); \
267 /* remove an element from a list - element doesn't have to be in list. */
268 #define _TLIST_REMOVE(list, p) \
270 if ((p) == (list)) { \
271 (list) = (p)->next; \
272 if (list) (list)->prev = NULL; \
274 if ((p)->prev) (p)->prev->next = (p)->next; \
275 if ((p)->next) (p)->next->prev = (p)->prev; \
277 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
282 return the parent chunk of a pointer
284 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
286 struct talloc_chunk *tc;
288 if (unlikely(ptr == NULL)) {
292 tc = talloc_chunk_from_ptr(ptr);
293 while (tc->prev) tc=tc->prev;
298 void *talloc_parent(const void *ptr)
300 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
301 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
307 const char *talloc_parent_name(const void *ptr)
309 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
310 return tc? tc->name : NULL;
314 A pool carries an in-pool object count count in the first 16 bytes.
315 bytes. This is done to support talloc_steal() to a parent outside of the
316 pool. The count includes the pool itself, so a talloc_free() on a pool will
317 only destroy the pool if the count has dropped to zero. A talloc_free() of a
318 pool member will reduce the count, and eventually also call free(3) on the
321 The object count is not put into "struct talloc_chunk" because it is only
322 relevant for talloc pools and the alignment to 16 bytes would increase the
323 memory footprint of each talloc chunk by those 16 bytes.
326 #define TALLOC_POOL_HDR_SIZE 16
328 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
330 return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
337 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
340 struct talloc_chunk *pool_ctx = NULL;
342 struct talloc_chunk *result;
345 if (parent == NULL) {
349 if (parent->flags & TALLOC_FLAG_POOL) {
352 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
353 pool_ctx = (struct talloc_chunk *)parent->pool;
356 if (pool_ctx == NULL) {
360 space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
361 - ((char *)pool_ctx->pool);
364 * Align size to 16 bytes
366 chunk_size = ((size + 15) & ~15);
368 if (space_left < chunk_size) {
372 result = (struct talloc_chunk *)pool_ctx->pool;
374 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
375 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
378 pool_ctx->pool = (void *)((char *)result + chunk_size);
380 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
381 result->pool = pool_ctx;
383 *talloc_pool_objectcount(pool_ctx) += 1;
389 Allocate a bit of memory as a child of an existing pointer
391 static inline void *__talloc(const void *context, size_t size)
393 struct talloc_chunk *tc = NULL;
395 if (unlikely(context == NULL)) {
396 context = null_context;
399 if (unlikely(size >= MAX_TALLOC_SIZE)) {
403 if (context != NULL) {
404 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
409 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
410 if (unlikely(tc == NULL)) return NULL;
411 tc->flags = TALLOC_MAGIC;
416 tc->destructor = NULL;
421 if (likely(context)) {
422 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
425 parent->child->parent = NULL;
426 tc->next = parent->child;
435 tc->next = tc->prev = tc->parent = NULL;
438 return TC_PTR_FROM_CHUNK(tc);
442 * Create a talloc pool
445 void *talloc_pool(const void *context, size_t size)
447 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
448 struct talloc_chunk *tc;
450 if (unlikely(result == NULL)) {
454 tc = talloc_chunk_from_ptr(result);
456 tc->flags |= TALLOC_FLAG_POOL;
457 tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
459 *talloc_pool_objectcount(tc) = 1;
461 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
462 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
469 setup a destructor to be called on free of a pointer
470 the destructor should return 0 on success, or -1 on failure.
471 if the destructor fails then the free is failed, and the memory can
472 be continued to be used
474 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
476 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
477 tc->destructor = destructor;
481 increase the reference count on a piece of memory.
483 int talloc_increase_ref_count(const void *ptr)
485 if (unlikely(!talloc_reference(null_context, ptr))) {
492 helper for talloc_reference()
494 this is referenced by a function pointer and should not be inline
496 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
498 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
499 _TLIST_REMOVE(ptr_tc->refs, handle);
504 more efficient way to add a name to a pointer - the name must point to a
507 static inline void _talloc_set_name_const(const void *ptr, const char *name)
509 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
514 internal talloc_named_const()
516 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
520 ptr = __talloc(context, size);
521 if (unlikely(ptr == NULL)) {
525 _talloc_set_name_const(ptr, name);
531 make a secondary reference to a pointer, hanging off the given context.
532 the pointer remains valid until both the original caller and this given
535 the major use for this is when two different structures need to reference the
536 same underlying data, and you want to be able to free the two instances separately,
539 void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
541 struct talloc_chunk *tc;
542 struct talloc_reference_handle *handle;
543 if (unlikely(ptr == NULL)) return NULL;
545 tc = talloc_chunk_from_ptr(ptr);
546 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
547 sizeof(struct talloc_reference_handle),
548 TALLOC_MAGIC_REFERENCE);
549 if (unlikely(handle == NULL)) return NULL;
551 /* note that we hang the destructor off the handle, not the
552 main context as that allows the caller to still setup their
553 own destructor on the context if they want to */
554 talloc_set_destructor(handle, talloc_reference_destructor);
555 handle->ptr = discard_const_p(void, ptr);
556 handle->location = location;
557 _TLIST_ADD(tc->refs, handle);
561 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
564 internal talloc_free call
566 static inline int _talloc_free_internal(void *ptr, const char *location)
568 struct talloc_chunk *tc;
570 if (unlikely(ptr == NULL)) {
574 tc = talloc_chunk_from_ptr(ptr);
576 if (unlikely(tc->refs)) {
578 /* check this is a reference from a child or grantchild
579 * back to it's parent or grantparent
581 * in that case we need to remove the reference and
582 * call another instance of talloc_free() on the current
585 is_child = talloc_is_parent(tc->refs, ptr);
586 _talloc_free_internal(tc->refs, location);
588 return _talloc_free_internal(ptr, location);
593 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
594 /* we have a free loop - stop looping */
598 if (unlikely(tc->destructor)) {
599 talloc_destructor_t d = tc->destructor;
600 if (d == (talloc_destructor_t)-1) {
603 tc->destructor = (talloc_destructor_t)-1;
608 tc->destructor = NULL;
612 _TLIST_REMOVE(tc->parent->child, tc);
613 if (tc->parent->child) {
614 tc->parent->child->parent = tc->parent;
617 if (tc->prev) tc->prev->next = tc->next;
618 if (tc->next) tc->next->prev = tc->prev;
621 tc->flags |= TALLOC_FLAG_LOOP;
624 /* we need to work out who will own an abandoned child
625 if it cannot be freed. In priority order, the first
626 choice is owner of any remaining reference to this
627 pointer, the second choice is our parent, and the
628 final choice is the null context. */
629 void *child = TC_PTR_FROM_CHUNK(tc->child);
630 const void *new_parent = null_context;
631 if (unlikely(tc->child->refs)) {
632 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
633 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
635 if (unlikely(_talloc_free_internal(child, location) == -1)) {
636 if (new_parent == null_context) {
637 struct talloc_chunk *p = talloc_parent_chunk(ptr);
638 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
640 _talloc_steal_internal(new_parent, child);
644 tc->flags |= TALLOC_FLAG_FREE;
646 /* we mark the freed memory with where we called the free
647 * from. This means on a double free error we can report where
648 * the first free came from
652 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
653 struct talloc_chunk *pool;
654 unsigned int *pool_object_count;
656 pool = (tc->flags & TALLOC_FLAG_POOL)
657 ? tc : (struct talloc_chunk *)tc->pool;
659 pool_object_count = talloc_pool_objectcount(pool);
661 if (*pool_object_count == 0) {
662 talloc_abort("Pool object count zero!");
666 *pool_object_count -= 1;
668 if (*pool_object_count == 0) {
679 move a lump of memory from one talloc context to another return the
680 ptr on success, or NULL if it could not be transferred.
681 passing NULL as ptr will always return NULL with no side effects.
683 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
685 struct talloc_chunk *tc, *new_tc;
687 if (unlikely(!ptr)) {
691 if (unlikely(new_ctx == NULL)) {
692 new_ctx = null_context;
695 tc = talloc_chunk_from_ptr(ptr);
697 if (unlikely(new_ctx == NULL)) {
699 _TLIST_REMOVE(tc->parent->child, tc);
700 if (tc->parent->child) {
701 tc->parent->child->parent = tc->parent;
704 if (tc->prev) tc->prev->next = tc->next;
705 if (tc->next) tc->next->prev = tc->prev;
708 tc->parent = tc->next = tc->prev = NULL;
709 return discard_const_p(void, ptr);
712 new_tc = talloc_chunk_from_ptr(new_ctx);
714 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
715 return discard_const_p(void, ptr);
719 _TLIST_REMOVE(tc->parent->child, tc);
720 if (tc->parent->child) {
721 tc->parent->child->parent = tc->parent;
724 if (tc->prev) tc->prev->next = tc->next;
725 if (tc->next) tc->next->prev = tc->prev;
729 if (new_tc->child) new_tc->child->parent = NULL;
730 _TLIST_ADD(new_tc->child, tc);
732 return discard_const_p(void, ptr);
736 move a lump of memory from one talloc context to another return the
737 ptr on success, or NULL if it could not be transferred.
738 passing NULL as ptr will always return NULL with no side effects.
740 void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
742 struct talloc_chunk *tc;
744 if (unlikely(ptr == NULL)) {
748 tc = talloc_chunk_from_ptr(ptr);
750 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
751 struct talloc_reference_handle *h;
753 talloc_log("WARNING: talloc_steal with references at %s\n",
756 for (h=tc->refs; h; h=h->next) {
757 talloc_log("\treference at %s\n",
762 return _talloc_steal_internal(new_ctx, ptr);
766 this is like a talloc_steal(), but you must supply the old
767 parent. This resolves the ambiguity in a talloc_steal() which is
768 called on a context that has more than one parent (via references)
770 The old parent can be either a reference or a parent
772 void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
774 struct talloc_chunk *tc;
775 struct talloc_reference_handle *h;
777 if (unlikely(ptr == NULL)) {
781 if (old_parent == talloc_parent(ptr)) {
782 return _talloc_steal_internal(new_parent, ptr);
785 tc = talloc_chunk_from_ptr(ptr);
786 for (h=tc->refs;h;h=h->next) {
787 if (talloc_parent(h) == old_parent) {
788 if (_talloc_steal_internal(new_parent, h) != h) {
791 return discard_const_p(void, ptr);
795 /* it wasn't a parent */
800 remove a secondary reference to a pointer. This undo's what
801 talloc_reference() has done. The context and pointer arguments
802 must match those given to a talloc_reference()
804 static inline int talloc_unreference(const void *context, const void *ptr)
806 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
807 struct talloc_reference_handle *h;
809 if (unlikely(context == NULL)) {
810 context = null_context;
813 for (h=tc->refs;h;h=h->next) {
814 struct talloc_chunk *p = talloc_parent_chunk(h);
816 if (context == NULL) break;
817 } else if (TC_PTR_FROM_CHUNK(p) == context) {
825 return _talloc_free_internal(h, __location__);
829 remove a specific parent context from a pointer. This is a more
830 controlled varient of talloc_free()
832 int talloc_unlink(const void *context, void *ptr)
834 struct talloc_chunk *tc_p, *new_p;
841 if (context == NULL) {
842 context = null_context;
845 if (talloc_unreference(context, ptr) == 0) {
849 if (context == NULL) {
850 if (talloc_parent_chunk(ptr) != NULL) {
854 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
859 tc_p = talloc_chunk_from_ptr(ptr);
861 if (tc_p->refs == NULL) {
862 return _talloc_free_internal(ptr, __location__);
865 new_p = talloc_parent_chunk(tc_p->refs);
867 new_parent = TC_PTR_FROM_CHUNK(new_p);
872 if (talloc_unreference(new_parent, ptr) != 0) {
876 _talloc_steal_internal(new_parent, ptr);
882 add a name to an existing pointer - va_list version
884 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
886 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
888 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
889 tc->name = talloc_vasprintf(ptr, fmt, ap);
890 if (likely(tc->name)) {
891 _talloc_set_name_const(tc->name, ".name");
897 add a name to an existing pointer
899 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
904 name = talloc_set_name_v(ptr, fmt, ap);
911 create a named talloc pointer. Any talloc pointer can be named, and
912 talloc_named() operates just like talloc() except that it allows you
915 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
921 ptr = __talloc(context, size);
922 if (unlikely(ptr == NULL)) return NULL;
925 name = talloc_set_name_v(ptr, fmt, ap);
928 if (unlikely(name == NULL)) {
929 _talloc_free_internal(ptr, __location__);
937 return the name of a talloc ptr, or "UNNAMED"
939 const char *talloc_get_name(const void *ptr)
941 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
942 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
945 if (likely(tc->name)) {
953 check if a pointer has the given name. If it does, return the pointer,
954 otherwise return NULL
956 void *talloc_check_name(const void *ptr, const char *name)
959 if (unlikely(ptr == NULL)) return NULL;
960 pname = talloc_get_name(ptr);
961 if (likely(pname == name || strcmp(pname, name) == 0)) {
962 return discard_const_p(void, ptr);
967 static void talloc_abort_type_missmatch(const char *location,
969 const char *expected)
973 reason = talloc_asprintf(NULL,
974 "%s: Type mismatch: name[%s] expected[%s]",
979 reason = "Type mismatch";
982 talloc_abort(reason);
985 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
989 if (unlikely(ptr == NULL)) {
990 talloc_abort_type_missmatch(location, NULL, name);
994 pname = talloc_get_name(ptr);
995 if (likely(pname == name || strcmp(pname, name) == 0)) {
996 return discard_const_p(void, ptr);
999 talloc_abort_type_missmatch(location, pname, name);
1004 this is for compatibility with older versions of talloc
1006 void *talloc_init(const char *fmt, ...)
1013 * samba3 expects talloc_report_depth_cb(NULL, ...)
1014 * reports all talloc'ed memory, so we need to enable
1017 talloc_enable_null_tracking();
1019 ptr = __talloc(NULL, 0);
1020 if (unlikely(ptr == NULL)) return NULL;
1023 name = talloc_set_name_v(ptr, fmt, ap);
1026 if (unlikely(name == NULL)) {
1027 _talloc_free_internal(ptr, __location__);
1035 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1036 should probably not be used in new code. It's in here to keep the talloc
1037 code consistent across Samba 3 and 4.
1039 void talloc_free_children(void *ptr)
1041 struct talloc_chunk *tc;
1043 if (unlikely(ptr == NULL)) {
1047 tc = talloc_chunk_from_ptr(ptr);
1050 /* we need to work out who will own an abandoned child
1051 if it cannot be freed. In priority order, the first
1052 choice is owner of any remaining reference to this
1053 pointer, the second choice is our parent, and the
1054 final choice is the null context. */
1055 void *child = TC_PTR_FROM_CHUNK(tc->child);
1056 const void *new_parent = null_context;
1057 if (unlikely(tc->child->refs)) {
1058 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1059 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1061 if (unlikely(talloc_free(child) == -1)) {
1062 if (new_parent == null_context) {
1063 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1064 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1066 _talloc_steal_internal(new_parent, child);
1070 if ((tc->flags & TALLOC_FLAG_POOL)
1071 && (*talloc_pool_objectcount(tc) == 1)) {
1072 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
1073 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
1074 VALGRIND_MAKE_MEM_NOACCESS(
1075 tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
1081 Allocate a bit of memory as a child of an existing pointer
1083 void *_talloc(const void *context, size_t size)
1085 return __talloc(context, size);
1089 externally callable talloc_set_name_const()
1091 void talloc_set_name_const(const void *ptr, const char *name)
1093 _talloc_set_name_const(ptr, name);
1097 create a named talloc pointer. Any talloc pointer can be named, and
1098 talloc_named() operates just like talloc() except that it allows you
1099 to name the pointer.
1101 void *talloc_named_const(const void *context, size_t size, const char *name)
1103 return _talloc_named_const(context, size, name);
1107 free a talloc pointer. This also frees all child pointers of this
1110 return 0 if the memory is actually freed, otherwise -1. The memory
1111 will not be freed if the ref_count is > 1 or the destructor (if
1112 any) returns non-zero
1114 int _talloc_free(void *ptr, const char *location)
1116 struct talloc_chunk *tc;
1118 if (unlikely(ptr == NULL)) {
1122 tc = talloc_chunk_from_ptr(ptr);
1124 if (unlikely(tc->refs != NULL)) {
1125 struct talloc_reference_handle *h;
1127 talloc_log("ERROR: talloc_free with references at %s\n",
1130 for (h=tc->refs; h; h=h->next) {
1131 talloc_log("\treference at %s\n",
1137 return _talloc_free_internal(ptr, location);
1143 A talloc version of realloc. The context argument is only used if
1146 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1148 struct talloc_chunk *tc;
1150 bool malloced = false;
1152 /* size zero is equivalent to free() */
1153 if (unlikely(size == 0)) {
1154 talloc_unlink(context, ptr);
1158 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1162 /* realloc(NULL) is equivalent to malloc() */
1164 return _talloc_named_const(context, size, name);
1167 tc = talloc_chunk_from_ptr(ptr);
1169 /* don't allow realloc on referenced pointers */
1170 if (unlikely(tc->refs)) {
1174 /* don't let anybody try to realloc a talloc_pool */
1175 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1179 /* don't shrink if we have less than 1k to gain */
1180 if ((size < tc->size) && ((tc->size - size) < 1024)) {
1185 /* by resetting magic we catch users of the old memory */
1186 tc->flags |= TALLOC_FLAG_FREE;
1189 new_ptr = malloc(size + TC_HDR_SIZE);
1191 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1195 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1197 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1198 *talloc_pool_objectcount((struct talloc_chunk *)
1201 if (new_ptr == NULL) {
1202 new_ptr = malloc(TC_HDR_SIZE+size);
1207 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1211 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1214 if (unlikely(!new_ptr)) {
1215 tc->flags &= ~TALLOC_FLAG_FREE;
1219 tc = (struct talloc_chunk *)new_ptr;
1220 tc->flags &= ~TALLOC_FLAG_FREE;
1222 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1225 tc->parent->child = tc;
1228 tc->child->parent = tc;
1232 tc->prev->next = tc;
1235 tc->next->prev = tc;
1239 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1241 return TC_PTR_FROM_CHUNK(tc);
1245 a wrapper around talloc_steal() for situations where you are moving a pointer
1246 between two structures, and want the old pointer to be set to NULL
1248 void *_talloc_move(const void *new_ctx, const void *_pptr)
1250 const void **pptr = discard_const_p(const void *,_pptr);
1251 void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1257 return the total size of a talloc pool (subtree)
1259 size_t talloc_total_size(const void *ptr)
1262 struct talloc_chunk *c, *tc;
1271 tc = talloc_chunk_from_ptr(ptr);
1273 if (tc->flags & TALLOC_FLAG_LOOP) {
1277 tc->flags |= TALLOC_FLAG_LOOP;
1279 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1282 for (c=tc->child;c;c=c->next) {
1283 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1286 tc->flags &= ~TALLOC_FLAG_LOOP;
1292 return the total number of blocks in a talloc pool (subtree)
1294 size_t talloc_total_blocks(const void *ptr)
1297 struct talloc_chunk *c, *tc;
1306 tc = talloc_chunk_from_ptr(ptr);
1308 if (tc->flags & TALLOC_FLAG_LOOP) {
1312 tc->flags |= TALLOC_FLAG_LOOP;
1315 for (c=tc->child;c;c=c->next) {
1316 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1319 tc->flags &= ~TALLOC_FLAG_LOOP;
1325 return the number of external references to a pointer
1327 size_t talloc_reference_count(const void *ptr)
1329 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1330 struct talloc_reference_handle *h;
1333 for (h=tc->refs;h;h=h->next) {
1340 report on memory usage by all children of a pointer, giving a full tree view
1342 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1343 void (*callback)(const void *ptr,
1344 int depth, int max_depth,
1346 void *private_data),
1349 struct talloc_chunk *c, *tc;
1354 if (ptr == NULL) return;
1356 tc = talloc_chunk_from_ptr(ptr);
1358 if (tc->flags & TALLOC_FLAG_LOOP) {
1362 callback(ptr, depth, max_depth, 0, private_data);
1364 if (max_depth >= 0 && depth >= max_depth) {
1368 tc->flags |= TALLOC_FLAG_LOOP;
1369 for (c=tc->child;c;c=c->next) {
1370 if (c->name == TALLOC_MAGIC_REFERENCE) {
1371 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1372 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1374 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1377 tc->flags &= ~TALLOC_FLAG_LOOP;
1380 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1382 const char *name = talloc_get_name(ptr);
1383 FILE *f = (FILE *)_f;
1386 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1391 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1392 (max_depth < 0 ? "full " :""), name,
1393 (unsigned long)talloc_total_size(ptr),
1394 (unsigned long)talloc_total_blocks(ptr));
1398 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1401 (unsigned long)talloc_total_size(ptr),
1402 (unsigned long)talloc_total_blocks(ptr),
1403 (int)talloc_reference_count(ptr), ptr);
1406 fprintf(f, "content: ");
1407 if (talloc_total_size(ptr)) {
1408 int tot = talloc_total_size(ptr);
1411 for (i = 0; i < tot; i++) {
1412 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1413 fprintf(f, "%c", ((char *)ptr)[i]);
1415 fprintf(f, "~%02x", ((char *)ptr)[i]);
1424 report on memory usage by all children of a pointer, giving a full tree view
1426 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1429 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1435 report on memory usage by all children of a pointer, giving a full tree view
1437 void talloc_report_full(const void *ptr, FILE *f)
1439 talloc_report_depth_file(ptr, 0, -1, f);
1443 report on memory usage by all children of a pointer
1445 void talloc_report(const void *ptr, FILE *f)
1447 talloc_report_depth_file(ptr, 0, 1, f);
1451 report on any memory hanging off the null context
1453 static void talloc_report_null(void)
1455 if (talloc_total_size(null_context) != 0) {
1456 talloc_report(null_context, stderr);
1461 report on any memory hanging off the null context
1463 static void talloc_report_null_full(void)
1465 if (talloc_total_size(null_context) != 0) {
1466 talloc_report_full(null_context, stderr);
1471 enable tracking of the NULL context
1473 void talloc_enable_null_tracking(void)
1475 if (null_context == NULL) {
1476 null_context = _talloc_named_const(NULL, 0, "null_context");
1477 if (autofree_context != NULL) {
1478 talloc_reparent(NULL, null_context, autofree_context);
1484 enable tracking of the NULL context, not moving the autofree context
1485 into the NULL context. This is needed for the talloc testsuite
1487 void talloc_enable_null_tracking_no_autofree(void)
1489 if (null_context == NULL) {
1490 null_context = _talloc_named_const(NULL, 0, "null_context");
1495 disable tracking of the NULL context
1497 void talloc_disable_null_tracking(void)
1499 if (null_context != NULL) {
1500 /* we have to move any children onto the real NULL
1502 struct talloc_chunk *tc, *tc2;
1503 tc = talloc_chunk_from_ptr(null_context);
1504 for (tc2 = tc->child; tc2; tc2=tc2->next) {
1505 if (tc2->parent == tc) tc2->parent = NULL;
1506 if (tc2->prev == tc) tc2->prev = NULL;
1508 for (tc2 = tc->next; tc2; tc2=tc2->next) {
1509 if (tc2->parent == tc) tc2->parent = NULL;
1510 if (tc2->prev == tc) tc2->prev = NULL;
1515 talloc_free(null_context);
1516 null_context = NULL;
1520 enable leak reporting on exit
1522 void talloc_enable_leak_report(void)
1524 talloc_enable_null_tracking();
1525 atexit(talloc_report_null);
1529 enable full leak reporting on exit
1531 void talloc_enable_leak_report_full(void)
1533 talloc_enable_null_tracking();
1534 atexit(talloc_report_null_full);
1538 talloc and zero memory.
1540 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1542 void *p = _talloc_named_const(ctx, size, name);
1545 memset(p, '\0', size);
1552 memdup with a talloc.
1554 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1556 void *newp = _talloc_named_const(t, size, name);
1559 memcpy(newp, p, size);
1565 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1569 ret = (char *)__talloc(t, len + 1);
1570 if (unlikely(!ret)) return NULL;
1572 memcpy(ret, p, len);
1575 _talloc_set_name_const(ret, ret);
1580 strdup with a talloc
1582 char *talloc_strdup(const void *t, const char *p)
1584 if (unlikely(!p)) return NULL;
1585 return __talloc_strlendup(t, p, strlen(p));
1589 strndup with a talloc
1591 char *talloc_strndup(const void *t, const char *p, size_t n)
1593 if (unlikely(!p)) return NULL;
1594 return __talloc_strlendup(t, p, strnlen(p, n));
1597 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1598 const char *a, size_t alen)
1602 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1603 if (unlikely(!ret)) return NULL;
1605 /* append the string and the trailing \0 */
1606 memcpy(&ret[slen], a, alen);
1609 _talloc_set_name_const(ret, ret);
1614 * Appends at the end of the string.
1616 char *talloc_strdup_append(char *s, const char *a)
1619 return talloc_strdup(NULL, a);
1626 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1630 * Appends at the end of the talloc'ed buffer,
1631 * not the end of the string.
1633 char *talloc_strdup_append_buffer(char *s, const char *a)
1638 return talloc_strdup(NULL, a);
1645 slen = talloc_get_size(s);
1646 if (likely(slen > 0)) {
1650 return __talloc_strlendup_append(s, slen, a, strlen(a));
1654 * Appends at the end of the string.
1656 char *talloc_strndup_append(char *s, const char *a, size_t n)
1659 return talloc_strdup(NULL, a);
1666 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1670 * Appends at the end of the talloc'ed buffer,
1671 * not the end of the string.
1673 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1678 return talloc_strdup(NULL, a);
1685 slen = talloc_get_size(s);
1686 if (likely(slen > 0)) {
1690 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1693 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1700 /* this call looks strange, but it makes it work on older solaris boxes */
1702 len = vsnprintf(&c, 1, fmt, ap2);
1704 if (unlikely(len < 0)) {
1708 ret = (char *)__talloc(t, len+1);
1709 if (unlikely(!ret)) return NULL;
1712 vsnprintf(ret, len+1, fmt, ap2);
1715 _talloc_set_name_const(ret, ret);
1721 Perform string formatting, and return a pointer to newly allocated
1722 memory holding the result, inside a memory pool.
1724 char *talloc_asprintf(const void *t, const char *fmt, ...)
1730 ret = talloc_vasprintf(t, fmt, ap);
1735 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1736 const char *fmt, va_list ap)
1737 PRINTF_ATTRIBUTE(3,0);
1739 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1740 const char *fmt, va_list ap)
1747 alen = vsnprintf(&c, 1, fmt, ap2);
1751 /* Either the vsnprintf failed or the format resulted in
1752 * no characters being formatted. In the former case, we
1753 * ought to return NULL, in the latter we ought to return
1754 * the original string. Most current callers of this
1755 * function expect it to never return NULL.
1760 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1761 if (!s) return NULL;
1764 vsnprintf(s + slen, alen + 1, fmt, ap2);
1767 _talloc_set_name_const(s, s);
1772 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1773 * and return @p s, which may have moved. Good for gradually
1774 * accumulating output into a string buffer. Appends at the end
1777 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1780 return talloc_vasprintf(NULL, fmt, ap);
1783 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1787 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1788 * and return @p s, which may have moved. Always appends at the
1789 * end of the talloc'ed buffer, not the end of the string.
1791 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1796 return talloc_vasprintf(NULL, fmt, ap);
1799 slen = talloc_get_size(s);
1800 if (likely(slen > 0)) {
1804 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1808 Realloc @p s to append the formatted result of @p fmt and return @p
1809 s, which may have moved. Good for gradually accumulating output
1810 into a string buffer.
1812 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1817 s = talloc_vasprintf_append(s, fmt, ap);
1823 Realloc @p s to append the formatted result of @p fmt and return @p
1824 s, which may have moved. Good for gradually accumulating output
1827 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1832 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1838 alloc an array, checking for integer overflow in the array size
1840 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1842 if (count >= MAX_TALLOC_SIZE/el_size) {
1845 return _talloc_named_const(ctx, el_size * count, name);
1849 alloc an zero array, checking for integer overflow in the array size
1851 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1853 if (count >= MAX_TALLOC_SIZE/el_size) {
1856 return _talloc_zero(ctx, el_size * count, name);
1860 realloc an array, checking for integer overflow in the array size
1862 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1864 if (count >= MAX_TALLOC_SIZE/el_size) {
1867 return _talloc_realloc(ctx, ptr, el_size * count, name);
1871 a function version of talloc_realloc(), so it can be passed as a function pointer
1872 to libraries that want a realloc function (a realloc function encapsulates
1873 all the basic capabilities of an allocation library, which is why this is useful)
1875 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1877 return _talloc_realloc(context, ptr, size, NULL);
1881 static int talloc_autofree_destructor(void *ptr)
1883 autofree_context = NULL;
1887 static void talloc_autofree(void)
1889 talloc_free(autofree_context);
1893 return a context which will be auto-freed on exit
1894 this is useful for reducing the noise in leak reports
1896 void *talloc_autofree_context(void)
1898 if (autofree_context == NULL) {
1899 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1900 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1901 atexit(talloc_autofree);
1903 return autofree_context;
1906 size_t talloc_get_size(const void *context)
1908 struct talloc_chunk *tc;
1910 if (context == NULL) {
1911 context = null_context;
1913 if (context == NULL) {
1917 tc = talloc_chunk_from_ptr(context);
1923 find a parent of this context that has the given name, if any
1925 void *talloc_find_parent_byname(const void *context, const char *name)
1927 struct talloc_chunk *tc;
1929 if (context == NULL) {
1933 tc = talloc_chunk_from_ptr(context);
1935 if (tc->name && strcmp(tc->name, name) == 0) {
1936 return TC_PTR_FROM_CHUNK(tc);
1938 while (tc && tc->prev) tc = tc->prev;
1947 show the parentage of a context
1949 void talloc_show_parents(const void *context, FILE *file)
1951 struct talloc_chunk *tc;
1953 if (context == NULL) {
1954 fprintf(file, "talloc no parents for NULL\n");
1958 tc = talloc_chunk_from_ptr(context);
1959 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1961 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1962 while (tc && tc->prev) tc = tc->prev;
1971 return 1 if ptr is a parent of context
1973 int talloc_is_parent(const void *context, const void *ptr)
1975 struct talloc_chunk *tc;
1977 if (context == NULL) {
1981 tc = talloc_chunk_from_ptr(context);
1983 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1984 while (tc && tc->prev) tc = tc->prev;