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 2 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, write to the Free Software
27 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 inspired by http://swapped.cc/halloc/
38 #include <sys/types.h>
41 /* use this to force every realloc to change the pointer, to stress test
42 code that might not cope */
43 #define ALWAYS_REALLOC 0
46 #define MAX_TALLOC_SIZE 0x7FFFFFFF
47 #define TALLOC_MAGIC 0xe814ec70
48 #define TALLOC_FLAG_FREE 0x01
49 #define TALLOC_FLAG_LOOP 0x02
50 #define TALLOC_FLAG_EXT_ALLOC 0x04
51 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
53 /* by default we abort when given a bad pointer (such as when talloc_free() is called
54 on a pointer that came from malloc() */
56 #define TALLOC_ABORT(reason) abort()
59 #ifndef discard_const_p
60 #if defined(INTPTR_MIN)
61 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
63 # define discard_const_p(type, ptr) ((type *)(ptr))
67 /* these macros gain us a few percent of speed on gcc */
68 #if HAVE_BUILTIN_EXPECT
69 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
70 as its first argument */
71 #define likely(x) __builtin_expect(!!(x), 1)
72 #define unlikely(x) __builtin_expect(!!(x), 0)
78 /* this null_context is only used if talloc_enable_leak_report() or
79 talloc_enable_leak_report_full() is called, otherwise it remains
82 static void *null_context;
83 static pid_t *autofree_context;
85 static void *(*tc_malloc)(size_t size) = malloc;
86 static void (*tc_free)(void *ptr) = free;
87 static void *(*tc_realloc)(void *ptr, size_t size) = realloc;
89 static void *(*tc_external_realloc)(const void *parent, void *ptr, size_t size);
90 static void (*tc_lock)(const void *ctx);
91 static void (*tc_unlock)(void);
93 struct talloc_reference_handle {
94 struct talloc_reference_handle *next, *prev;
98 typedef int (*talloc_destructor_t)(void *);
100 struct talloc_chunk {
101 struct talloc_chunk *next, *prev;
102 struct talloc_chunk *parent, *child;
103 struct talloc_reference_handle *refs;
104 talloc_destructor_t destructor;
110 /* 16 byte alignment seems to keep everyone happy */
111 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
112 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
114 /* panic if we get a bad magic value */
115 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
117 const char *pp = (const char *)ptr;
118 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
119 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
120 if (tc->flags & TALLOC_FLAG_FREE) {
121 TALLOC_ABORT("Bad talloc magic value - double free");
123 TALLOC_ABORT("Bad talloc magic value - unknown value");
129 /* hook into the front of the list */
130 #define _TLIST_ADD(list, p) \
134 (p)->next = (p)->prev = NULL; \
136 (list)->prev = (p); \
137 (p)->next = (list); \
143 /* remove an element from a list - element doesn't have to be in list. */
144 #define _TLIST_REMOVE(list, p) \
146 if ((p) == (list)) { \
147 (list) = (p)->next; \
148 if (list) (list)->prev = NULL; \
150 if ((p)->prev) (p)->prev->next = (p)->next; \
151 if ((p)->next) (p)->next->prev = (p)->prev; \
153 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
157 static inline void lock(const void *p)
160 struct talloc_chunk *tc = talloc_chunk_from_ptr(p);
162 if (tc->flags & TALLOC_FLAG_EXT_ALLOC) {
164 TALLOC_ABORT("nested locking");
171 static inline void unlock(void)
180 return the parent chunk of a pointer
182 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
184 struct talloc_chunk *tc;
186 if (unlikely(ptr == NULL)) {
190 tc = talloc_chunk_from_ptr(ptr);
191 while (tc->prev) tc=tc->prev;
196 /* This version doesn't do locking, so you must already have it. */
197 static void *talloc_parent_nolock(const void *ptr)
199 struct talloc_chunk *tc;
201 tc = talloc_parent_chunk(ptr);
202 return tc ? TC_PTR_FROM_CHUNK(tc) : NULL;
205 void *talloc_parent(const void *ptr)
210 parent = talloc_parent_nolock(ptr);
218 const char *talloc_parent_name(const void *ptr)
220 struct talloc_chunk *tc;
223 tc = talloc_parent_chunk(ptr);
226 return tc? tc->name : NULL;
229 static void *init_talloc(struct talloc_chunk *parent,
230 struct talloc_chunk *tc,
231 size_t size, int external)
233 if (unlikely(tc == NULL))
237 tc->flags = TALLOC_MAGIC;
239 tc->flags |= TALLOC_FLAG_EXT_ALLOC;
240 tc->destructor = NULL;
245 if (likely(parent)) {
247 parent->child->parent = NULL;
248 tc->next = parent->child;
257 tc->next = tc->prev = tc->parent = NULL;
260 return TC_PTR_FROM_CHUNK(tc);
264 Allocate a bit of memory as a child of an existing pointer
266 static inline void *__talloc(const void *context, size_t size)
268 struct talloc_chunk *tc;
269 struct talloc_chunk *parent = NULL;
272 if (unlikely(context == NULL)) {
273 context = null_context;
276 if (unlikely(size >= MAX_TALLOC_SIZE)) {
280 if (likely(context)) {
281 parent = talloc_chunk_from_ptr(context);
282 if (unlikely(parent->flags & TALLOC_FLAG_EXT_ALLOC)) {
283 tc = tc_external_realloc(context, NULL,
290 tc = (struct talloc_chunk *)tc_malloc(TC_HDR_SIZE+size);
292 return init_talloc(parent, tc, size, external);
296 setup a destructor to be called on free of a pointer
297 the destructor should return 0 on success, or -1 on failure.
298 if the destructor fails then the free is failed, and the memory can
299 be continued to be used
301 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
303 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
304 tc->destructor = destructor;
308 increase the reference count on a piece of memory.
310 int talloc_increase_ref_count(const void *ptr)
312 if (unlikely(!talloc_reference(null_context, ptr))) {
319 helper for talloc_reference()
321 this is referenced by a function pointer and should not be inline
323 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
325 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
326 _TLIST_REMOVE(ptr_tc->refs, handle);
331 more efficient way to add a name to a pointer - the name must point to a
334 static inline void _talloc_set_name_const(const void *ptr, const char *name)
336 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
341 internal talloc_named_const()
343 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
347 ptr = __talloc(context, size);
348 if (unlikely(ptr == NULL)) {
352 _talloc_set_name_const(ptr, name);
358 make a secondary reference to a pointer, hanging off the given context.
359 the pointer remains valid until both the original caller and this given
362 the major use for this is when two different structures need to reference the
363 same underlying data, and you want to be able to free the two instances separately,
366 void *_talloc_reference(const void *context, const void *ptr)
368 struct talloc_chunk *tc;
369 struct talloc_reference_handle *handle;
370 if (unlikely(ptr == NULL)) return NULL;
373 tc = talloc_chunk_from_ptr(ptr);
374 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
375 sizeof(struct talloc_reference_handle),
376 TALLOC_MAGIC_REFERENCE);
377 if (unlikely(handle == NULL)) {
382 /* note that we hang the destructor off the handle, not the
383 main context as that allows the caller to still setup their
384 own destructor on the context if they want to */
385 talloc_set_destructor(handle, talloc_reference_destructor);
386 handle->ptr = discard_const_p(void, ptr);
387 _TLIST_ADD(tc->refs, handle);
393 return 1 if ptr is a parent of context
395 static int _talloc_is_parent(const void *context, const void *ptr)
397 struct talloc_chunk *tc;
399 if (context == NULL) {
403 tc = talloc_chunk_from_ptr(context);
405 if (TC_PTR_FROM_CHUNK(tc) == ptr) {
408 while (tc && tc->prev) tc = tc->prev;
417 move a lump of memory from one talloc context to another return the
418 ptr on success, or NULL if it could not be transferred.
419 passing NULL as ptr will always return NULL with no side effects.
421 static void *__talloc_steal(const void *new_ctx, const void *ptr)
423 struct talloc_chunk *tc, *new_tc;
425 if (unlikely(!ptr)) {
429 if (unlikely(new_ctx == NULL)) {
430 new_ctx = null_context;
433 tc = talloc_chunk_from_ptr(ptr);
435 if (unlikely(new_ctx == NULL)) {
437 _TLIST_REMOVE(tc->parent->child, tc);
438 if (tc->parent->child) {
439 tc->parent->child->parent = tc->parent;
442 if (tc->prev) tc->prev->next = tc->next;
443 if (tc->next) tc->next->prev = tc->prev;
446 tc->parent = tc->next = tc->prev = NULL;
447 return discard_const_p(void, ptr);
450 new_tc = talloc_chunk_from_ptr(new_ctx);
452 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
453 return discard_const_p(void, ptr);
457 _TLIST_REMOVE(tc->parent->child, tc);
458 if (tc->parent->child) {
459 tc->parent->child->parent = tc->parent;
462 if (tc->prev) tc->prev->next = tc->next;
463 if (tc->next) tc->next->prev = tc->prev;
467 if (new_tc->child) new_tc->child->parent = NULL;
468 _TLIST_ADD(new_tc->child, tc);
470 return discard_const_p(void, ptr);
474 internal talloc_free call
476 static inline int _talloc_free(const void *ptr)
478 struct talloc_chunk *tc;
479 void *oldparent = NULL;
481 if (unlikely(ptr == NULL)) {
485 tc = talloc_chunk_from_ptr(ptr);
487 if (unlikely(tc->refs)) {
489 /* check this is a reference from a child or grantchild
490 * back to it's parent or grantparent
492 * in that case we need to remove the reference and
493 * call another instance of talloc_free() on the current
496 is_child = _talloc_is_parent(tc->refs, ptr);
497 _talloc_free(tc->refs);
499 return _talloc_free(ptr);
504 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
505 /* we have a free loop - stop looping */
509 if (unlikely(tc->destructor)) {
510 talloc_destructor_t d = tc->destructor;
511 if (d == (talloc_destructor_t)-1) {
514 tc->destructor = (talloc_destructor_t)-1;
515 if (d(discard_const_p(void, ptr)) == -1) {
519 tc->destructor = NULL;
522 if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC))
523 oldparent = talloc_parent_nolock(ptr);
526 _TLIST_REMOVE(tc->parent->child, tc);
527 if (tc->parent->child) {
528 tc->parent->child->parent = tc->parent;
531 if (tc->prev) tc->prev->next = tc->next;
532 if (tc->next) tc->next->prev = tc->prev;
535 tc->flags |= TALLOC_FLAG_LOOP;
538 /* we need to work out who will own an abandoned child
539 if it cannot be freed. In priority order, the first
540 choice is owner of any remaining reference to this
541 pointer, the second choice is our parent, and the
542 final choice is the null context. */
543 void *child = TC_PTR_FROM_CHUNK(tc->child);
544 const void *new_parent = null_context;
545 struct talloc_chunk *old_parent = NULL;
546 if (unlikely(tc->child->refs)) {
547 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
548 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
550 /* finding the parent here is potentially quite
551 expensive, but the alternative, which is to change
552 talloc to always have a valid tc->parent pointer,
553 makes realloc more expensive where there are a
554 large number of children.
556 The reason we need the parent pointer here is that
557 if _talloc_free_internal() fails due to references
558 or a failing destructor we need to re-parent, but
559 the free call can invalidate the prev pointer.
561 if (new_parent == null_context && (tc->child->refs || tc->child->destructor)) {
562 old_parent = talloc_parent_chunk(ptr);
564 if (unlikely(_talloc_free(child) == -1)) {
565 if (new_parent == null_context) {
566 struct talloc_chunk *p = old_parent;
567 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
569 __talloc_steal(new_parent, child);
573 tc->flags |= TALLOC_FLAG_FREE;
575 if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC))
576 tc_external_realloc(oldparent, tc, 0);
583 void *_talloc_steal(const void *new_ctx, const void *ptr)
588 p = __talloc_steal(new_ctx, ptr);
594 remove a secondary reference to a pointer. This undo's what
595 talloc_reference() has done. The context and pointer arguments
596 must match those given to a talloc_reference()
598 static inline int talloc_unreference(const void *context, const void *ptr)
600 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
601 struct talloc_reference_handle *h;
603 if (unlikely(context == NULL)) {
604 context = null_context;
607 for (h=tc->refs;h;h=h->next) {
608 struct talloc_chunk *p = talloc_parent_chunk(h);
610 if (context == NULL) break;
611 } else if (TC_PTR_FROM_CHUNK(p) == context) {
619 return _talloc_free(h);
623 remove a specific parent context from a pointer. This is a more
624 controlled varient of talloc_free()
626 int talloc_unlink(const void *context, void *ptr)
628 struct talloc_chunk *tc_p, *new_p;
635 if (context == NULL) {
636 context = null_context;
640 if (talloc_unreference(context, ptr) == 0) {
645 if (context == NULL) {
646 if (talloc_parent_chunk(ptr) != NULL) {
651 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
657 tc_p = talloc_chunk_from_ptr(ptr);
659 if (tc_p->refs == NULL) {
660 int ret = _talloc_free(ptr);
665 new_p = talloc_parent_chunk(tc_p->refs);
667 new_parent = TC_PTR_FROM_CHUNK(new_p);
672 if (talloc_unreference(new_parent, ptr) != 0) {
677 __talloc_steal(new_parent, ptr);
684 add a name to an existing pointer - va_list version
686 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_FMT(2,0);
688 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
690 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
691 tc->name = talloc_vasprintf(ptr, fmt, ap);
692 if (likely(tc->name)) {
693 _talloc_set_name_const(tc->name, ".name");
699 add a name to an existing pointer
701 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
706 name = talloc_set_name_v(ptr, fmt, ap);
713 create a named talloc pointer. Any talloc pointer can be named, and
714 talloc_named() operates just like talloc() except that it allows you
717 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
724 ptr = __talloc(context, size);
726 if (unlikely(ptr == NULL)) return NULL;
729 name = talloc_set_name_v(ptr, fmt, ap);
732 if (unlikely(name == NULL)) {
741 return the name of a talloc ptr, or "UNNAMED"
743 const char *talloc_get_name(const void *ptr)
745 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
746 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
749 if (likely(tc->name)) {
757 check if a pointer has the given name. If it does, return the pointer,
758 otherwise return NULL
760 void *talloc_check_name(const void *ptr, const char *name)
763 if (unlikely(ptr == NULL)) return NULL;
764 pname = talloc_get_name(ptr);
765 if (likely(pname == name || strcmp(pname, name) == 0)) {
766 return discard_const_p(void, ptr);
773 this is for compatibility with older versions of talloc
775 void *talloc_init(const char *fmt, ...)
782 * samba3 expects talloc_report_depth_cb(NULL, ...)
783 * reports all talloc'ed memory, so we need to enable
786 talloc_enable_null_tracking();
788 ptr = __talloc(NULL, 0);
789 if (unlikely(ptr == NULL)) return NULL;
792 name = talloc_set_name_v(ptr, fmt, ap);
795 if (unlikely(name == NULL)) {
804 Allocate a bit of memory as a child of an existing pointer
806 void *_talloc(const void *context, size_t size)
808 return __talloc(context, size);
811 static int talloc_destroy_pointer(void ***pptr)
813 if ((uintptr_t)**pptr < getpagesize())
814 TALLOC_ABORT("Double free or invalid talloc_set?");
815 /* Invalidate pointer so it can't be used again. */
820 void _talloc_set(void *ptr, const void *ctx, size_t size, const char *name)
825 p = talloc_named_const(ctx, size, name);
829 child = talloc(p, void **);
830 if (unlikely(!child)) {
836 talloc_set_name_const(child, "talloc_set destructor");
837 talloc_set_destructor(child, talloc_destroy_pointer);
840 /* memcpy rather than cast avoids aliasing problems. */
841 memcpy(ptr, &p, sizeof(p));
845 externally callable talloc_set_name_const()
847 void talloc_set_name_const(const void *ptr, const char *name)
849 _talloc_set_name_const(ptr, name);
853 create a named talloc pointer. Any talloc pointer can be named, and
854 talloc_named() operates just like talloc() except that it allows you
857 void *talloc_named_const(const void *context, size_t size, const char *name)
861 p = _talloc_named_const(context, size, name);
867 free a talloc pointer. This also frees all child pointers of this
870 return 0 if the memory is actually freed, otherwise -1. The memory
871 will not be freed if the ref_count is > 1 or the destructor (if
872 any) returns non-zero
874 int talloc_free(const void *ptr)
876 int saved_errno = errno, ret;
879 ret = _talloc_free(discard_const_p(void, ptr));
889 A talloc version of realloc. The context argument is only used if
892 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
894 struct talloc_chunk *tc;
897 /* size zero is equivalent to free() */
898 if (unlikely(size == 0)) {
903 if (unlikely(size >= MAX_TALLOC_SIZE)) {
907 /* realloc(NULL) is equivalent to malloc() */
909 return talloc_named_const(context, size, name);
912 tc = talloc_chunk_from_ptr(ptr);
914 /* don't allow realloc on referenced pointers */
915 if (unlikely(tc->refs)) {
920 if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC)) {
921 /* need to get parent before setting free flag. */
922 void *parent = talloc_parent_nolock(ptr);
923 tc->flags |= TALLOC_FLAG_FREE;
924 new_ptr = tc_external_realloc(parent, tc, size + TC_HDR_SIZE);
926 /* by resetting magic we catch users of the old memory */
927 tc->flags |= TALLOC_FLAG_FREE;
930 new_ptr = tc_malloc(size + TC_HDR_SIZE);
932 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
936 new_ptr = tc_realloc(tc, size + TC_HDR_SIZE);
940 if (unlikely(!new_ptr)) {
941 tc->flags &= ~TALLOC_FLAG_FREE;
946 tc = (struct talloc_chunk *)new_ptr;
947 tc->flags &= ~TALLOC_FLAG_FREE;
949 tc->parent->child = tc;
952 tc->child->parent = tc;
963 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
966 return TC_PTR_FROM_CHUNK(tc);
970 a wrapper around talloc_steal() for situations where you are moving a pointer
971 between two structures, and want the old pointer to be set to NULL
973 void *_talloc_move(const void *new_ctx, const void *_pptr)
975 const void **pptr = discard_const_p(const void *,_pptr);
976 void *ret = _talloc_steal(new_ctx, *pptr);
982 return the total size of a talloc pool (subtree)
984 static size_t _talloc_total_size(const void *ptr)
987 struct talloc_chunk *c, *tc;
989 tc = talloc_chunk_from_ptr(ptr);
991 if (tc->flags & TALLOC_FLAG_LOOP) {
995 tc->flags |= TALLOC_FLAG_LOOP;
998 for (c=tc->child;c;c=c->next) {
999 total += _talloc_total_size(TC_PTR_FROM_CHUNK(c));
1002 tc->flags &= ~TALLOC_FLAG_LOOP;
1007 size_t talloc_total_size(const void *ptr)
1019 total = _talloc_total_size(ptr);
1025 return the total number of blocks in a talloc pool (subtree)
1027 static size_t _talloc_total_blocks(const void *ptr)
1030 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
1032 if (tc->flags & TALLOC_FLAG_LOOP) {
1036 tc->flags |= TALLOC_FLAG_LOOP;
1039 for (c=tc->child;c;c=c->next) {
1040 total += _talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1043 tc->flags &= ~TALLOC_FLAG_LOOP;
1048 size_t talloc_total_blocks(const void *ptr)
1053 total = _talloc_total_blocks(ptr);
1059 static size_t _talloc_reference_count(const void *ptr)
1061 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1062 struct talloc_reference_handle *h;
1065 for (h=tc->refs;h;h=h->next) {
1072 return the number of external references to a pointer
1074 size_t talloc_reference_count(const void *ptr)
1078 lock(talloc_chunk_from_ptr(ptr));
1079 ret = _talloc_reference_count(ptr);
1085 report on memory usage by all children of a pointer, giving a full tree view
1087 static void _talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1088 void (*callback)(const void *ptr,
1089 int depth, int max_depth,
1091 void *private_data),
1094 struct talloc_chunk *c, *tc;
1096 tc = talloc_chunk_from_ptr(ptr);
1098 if (tc->flags & TALLOC_FLAG_LOOP) {
1102 callback(ptr, depth, max_depth, 0, private_data);
1104 if (max_depth >= 0 && depth >= max_depth) {
1108 tc->flags |= TALLOC_FLAG_LOOP;
1109 for (c=tc->child;c;c=c->next) {
1110 if (c->name == TALLOC_MAGIC_REFERENCE) {
1111 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1112 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1114 _talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1117 tc->flags &= ~TALLOC_FLAG_LOOP;
1120 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1121 void (*callback)(const void *ptr,
1122 int depth, int max_depth,
1124 void *private_data),
1130 if (ptr == NULL) return;
1133 _talloc_report_depth_cb(ptr, depth, max_depth, callback, private_data);
1137 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1139 const char *name = talloc_get_name(ptr);
1140 FILE *f = (FILE *)_f;
1143 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1148 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1149 (max_depth < 0 ? "full " :""), name,
1150 (unsigned long)_talloc_total_size(ptr),
1151 (unsigned long)_talloc_total_blocks(ptr));
1155 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1158 (unsigned long)_talloc_total_size(ptr),
1159 (unsigned long)_talloc_total_blocks(ptr),
1160 (int)_talloc_reference_count(ptr), ptr);
1163 fprintf(f, "content: ");
1164 if (talloc_total_size(ptr)) {
1165 int tot = talloc_total_size(ptr);
1168 for (i = 0; i < tot; i++) {
1169 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1170 fprintf(f, "%c", ((char *)ptr)[i]);
1172 fprintf(f, "~%02x", ((char *)ptr)[i]);
1181 report on memory usage by all children of a pointer, giving a full tree view
1183 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1185 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1190 report on memory usage by all children of a pointer, giving a full tree view
1192 void talloc_report_full(const void *ptr, FILE *f)
1194 talloc_report_depth_file(ptr, 0, -1, f);
1198 report on memory usage by all children of a pointer
1200 void talloc_report(const void *ptr, FILE *f)
1202 talloc_report_depth_file(ptr, 0, 1, f);
1206 report on any memory hanging off the null context
1208 static void talloc_report_null(void)
1210 if (talloc_total_size(null_context) != 0) {
1211 talloc_report(null_context, stderr);
1216 report on any memory hanging off the null context
1218 static void talloc_report_null_full(void)
1220 if (talloc_total_size(null_context) != 0) {
1221 talloc_report_full(null_context, stderr);
1226 enable tracking of the NULL context
1228 void talloc_enable_null_tracking(void)
1230 if (null_context == NULL) {
1231 null_context = _talloc_named_const(NULL, 0, "null_context");
1236 disable tracking of the NULL context
1238 void talloc_disable_null_tracking(void)
1240 _talloc_free(null_context);
1241 null_context = NULL;
1245 enable leak reporting on exit
1247 void talloc_enable_leak_report(void)
1249 talloc_enable_null_tracking();
1250 atexit(talloc_report_null);
1254 enable full leak reporting on exit
1256 void talloc_enable_leak_report_full(void)
1258 talloc_enable_null_tracking();
1259 atexit(talloc_report_null_full);
1263 talloc and zero memory.
1265 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1270 p = _talloc_named_const(ctx, size, name);
1274 memset(p, '\0', size);
1281 memdup with a talloc.
1283 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1288 newp = _talloc_named_const(t, size, name);
1292 memcpy(newp, p, size);
1299 strdup with a talloc
1301 char *talloc_strdup(const void *t, const char *p)
1307 ret = (char *)talloc_memdup(t, p, strlen(p) + 1);
1309 _talloc_set_name_const(ret, ret);
1315 append to a talloced string
1317 char *talloc_append_string(char *orig, const char *append)
1320 size_t olen = strlen(orig);
1326 alenz = strlen(append) + 1;
1328 ret = talloc_realloc(NULL, orig, char, olen + alenz);
1332 /* append the string with the trailing \0 */
1333 memcpy(&ret[olen], append, alenz);
1335 _talloc_set_name_const(ret, ret);
1341 strndup with a talloc
1343 char *talloc_strndup(const void *t, const char *p, size_t n)
1348 for (len=0; len<n && p[len]; len++) ;
1351 ret = (char *)__talloc(t, len + 1);
1353 if (!ret) { return NULL; }
1354 memcpy(ret, p, len);
1356 _talloc_set_name_const(ret, ret);
1360 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1367 /* this call looks strange, but it makes it work on older solaris boxes */
1369 len = vsnprintf(&c, 1, fmt, ap2);
1376 ret = (char *)__talloc(t, len+1);
1380 vsnprintf(ret, len+1, fmt, ap2);
1382 _talloc_set_name_const(ret, ret);
1390 Perform string formatting, and return a pointer to newly allocated
1391 memory holding the result, inside a memory pool.
1393 char *talloc_asprintf(const void *t, const char *fmt, ...)
1399 ret = talloc_vasprintf(t, fmt, ap);
1406 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1407 * and return @p s, which may have moved. Good for gradually
1408 * accumulating output into a string buffer.
1410 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1412 struct talloc_chunk *tc;
1418 return talloc_vasprintf(NULL, fmt, ap);
1421 tc = talloc_chunk_from_ptr(s);
1423 s_len = tc->size - 1;
1426 len = vsnprintf(&c, 1, fmt, ap2);
1430 /* Either the vsnprintf failed or the format resulted in
1431 * no characters being formatted. In the former case, we
1432 * ought to return NULL, in the latter we ought to return
1433 * the original string. Most current callers of this
1434 * function expect it to never return NULL.
1439 s = talloc_realloc(NULL, s, char, s_len + len+1);
1440 if (!s) return NULL;
1443 vsnprintf(s+s_len, len+1, fmt, ap2);
1445 _talloc_set_name_const(s, s);
1451 Realloc @p s to append the formatted result of @p fmt and return @p
1452 s, which may have moved. Good for gradually accumulating output
1453 into a string buffer.
1455 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1460 s = talloc_vasprintf_append(s, fmt, ap);
1466 alloc an array, checking for integer overflow in the array size
1468 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1472 if (count >= MAX_TALLOC_SIZE/el_size) {
1476 p = _talloc_named_const(ctx, el_size * count, name);
1482 alloc an zero array, checking for integer overflow in the array size
1484 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1488 if (count >= MAX_TALLOC_SIZE/el_size) {
1491 p = _talloc_zero(ctx, el_size * count, name);
1496 realloc an array, checking for integer overflow in the array size
1498 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1500 if (count >= MAX_TALLOC_SIZE/el_size) {
1503 return _talloc_realloc(ctx, ptr, el_size * count, name);
1507 a function version of talloc_realloc(), so it can be passed as a function pointer
1508 to libraries that want a realloc function (a realloc function encapsulates
1509 all the basic capabilities of an allocation library, which is why this is useful)
1511 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1513 return _talloc_realloc(context, ptr, size, NULL);
1517 static int talloc_autofree_destructor(void *ptr)
1519 autofree_context = NULL;
1523 static void talloc_autofree(void)
1525 if (autofree_context && *autofree_context == getpid())
1526 talloc_free(autofree_context);
1530 return a context which will be auto-freed on exit
1531 this is useful for reducing the noise in leak reports
1533 void *talloc_autofree_context(void)
1535 if (autofree_context == NULL || *autofree_context != getpid()) {
1536 autofree_context = talloc(NULL, pid_t);
1537 *autofree_context = getpid();
1538 talloc_set_name_const(autofree_context, "autofree_context");
1540 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1541 atexit(talloc_autofree);
1543 return autofree_context;
1546 size_t talloc_get_size(const void *context)
1548 struct talloc_chunk *tc;
1550 if (context == NULL)
1553 tc = talloc_chunk_from_ptr(context);
1559 find a parent of this context that has the given name, if any
1561 void *talloc_find_parent_byname(const void *context, const char *name)
1563 struct talloc_chunk *tc;
1565 if (context == NULL) {
1570 tc = talloc_chunk_from_ptr(context);
1572 if (tc->name && strcmp(tc->name, name) == 0) {
1574 return TC_PTR_FROM_CHUNK(tc);
1576 while (tc && tc->prev) tc = tc->prev;
1586 show the parentage of a context
1588 void talloc_show_parents(const void *context, FILE *file)
1590 struct talloc_chunk *tc;
1592 if (context == NULL) {
1593 fprintf(file, "talloc no parents for NULL\n");
1598 tc = talloc_chunk_from_ptr(context);
1599 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1601 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1602 while (tc && tc->prev) tc = tc->prev;
1611 int talloc_is_parent(const void *context, const void *ptr)
1615 ret = _talloc_is_parent(context, ptr);
1620 void talloc_set_allocator(void *(*malloc)(size_t size),
1621 void (*free)(void *ptr),
1622 void *(*realloc)(void *ptr, size_t size))
1626 tc_realloc = realloc;
1629 void *talloc_add_external(const void *ctx,
1630 void *(*realloc)(const void *, void *, size_t),
1631 void (*lock)(const void *p),
1632 void (*unlock)(void))
1634 struct talloc_chunk *tc, *parent;
1637 if (tc_external_realloc && tc_external_realloc != realloc)
1638 TALLOC_ABORT("talloc_add_external realloc replaced");
1639 tc_external_realloc = realloc;
1641 if (unlikely(ctx == NULL)) {
1645 parent = talloc_chunk_from_ptr(ctx);
1647 tc = tc_external_realloc(ctx, NULL, TC_HDR_SIZE);
1648 p = init_talloc(parent, tc, 0, 1);