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 0x10000000
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_external_realloc)(const void *parent, void *ptr, size_t size);
86 static void (*tc_lock)(void *);
87 static void (*tc_unlock)(void *);
88 static void *tc_lock_data;
90 struct talloc_reference_handle {
91 struct talloc_reference_handle *next, *prev;
95 typedef int (*talloc_destructor_t)(void *);
98 struct talloc_chunk *next, *prev;
99 struct talloc_chunk *parent, *child;
100 struct talloc_reference_handle *refs;
101 talloc_destructor_t destructor;
107 /* 16 byte alignment seems to keep everyone happy */
108 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
109 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
111 /* panic if we get a bad magic value */
112 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
114 const char *pp = (const char *)ptr;
115 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
116 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
117 if (tc->flags & TALLOC_FLAG_FREE) {
118 TALLOC_ABORT("Bad talloc magic value - double free");
120 TALLOC_ABORT("Bad talloc magic value - unknown value");
126 /* hook into the front of the list */
127 #define _TLIST_ADD(list, p) \
131 (p)->next = (p)->prev = NULL; \
133 (list)->prev = (p); \
134 (p)->next = (list); \
140 /* remove an element from a list - element doesn't have to be in list. */
141 #define _TLIST_REMOVE(list, p) \
143 if ((p) == (list)) { \
144 (list) = (p)->next; \
145 if (list) (list)->prev = NULL; \
147 if ((p)->prev) (p)->prev->next = (p)->next; \
148 if ((p)->next) (p)->next->prev = (p)->prev; \
150 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
153 static inline void lock(void)
156 tc_lock(tc_lock_data);
159 static inline void unlock(void)
162 tc_unlock(tc_lock_data);
166 return the parent chunk of a pointer
168 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
170 struct talloc_chunk *tc;
172 if (unlikely(ptr == NULL)) {
176 tc = talloc_chunk_from_ptr(ptr);
177 while (tc->prev) tc=tc->prev;
182 void *talloc_parent(const void *ptr)
184 struct talloc_chunk *tc;
187 tc = talloc_parent_chunk(ptr);
189 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
195 const char *talloc_parent_name(const void *ptr)
197 struct talloc_chunk *tc;
200 tc = talloc_parent_chunk(ptr);
203 return tc? tc->name : NULL;
206 static void *init_talloc(struct talloc_chunk *parent,
207 struct talloc_chunk *tc,
208 size_t size, int external)
210 if (unlikely(tc == NULL))
214 tc->flags = TALLOC_MAGIC;
216 tc->flags |= TALLOC_FLAG_EXT_ALLOC;
217 tc->destructor = NULL;
222 if (likely(parent)) {
224 parent->child->parent = NULL;
225 tc->next = parent->child;
234 tc->next = tc->prev = tc->parent = NULL;
237 return TC_PTR_FROM_CHUNK(tc);
241 Allocate a bit of memory as a child of an existing pointer
243 static inline void *__talloc(const void *context, size_t size)
245 struct talloc_chunk *tc;
246 struct talloc_chunk *parent = NULL;
249 if (unlikely(context == NULL)) {
250 context = null_context;
253 if (unlikely(size >= MAX_TALLOC_SIZE)) {
257 if (likely(context)) {
258 parent = talloc_chunk_from_ptr(context);
259 if (unlikely(parent->flags & TALLOC_FLAG_EXT_ALLOC)) {
260 tc = tc_external_realloc(context, NULL,
267 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
269 return init_talloc(parent, tc, size, external);
273 setup a destructor to be called on free of a pointer
274 the destructor should return 0 on success, or -1 on failure.
275 if the destructor fails then the free is failed, and the memory can
276 be continued to be used
278 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
280 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
281 tc->destructor = destructor;
285 increase the reference count on a piece of memory.
287 int talloc_increase_ref_count(const void *ptr)
289 if (unlikely(!talloc_reference(null_context, ptr))) {
296 helper for talloc_reference()
298 this is referenced by a function pointer and should not be inline
300 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
302 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
303 _TLIST_REMOVE(ptr_tc->refs, handle);
308 more efficient way to add a name to a pointer - the name must point to a
311 static inline void _talloc_set_name_const(const void *ptr, const char *name)
313 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
318 internal talloc_named_const()
320 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
324 ptr = __talloc(context, size);
325 if (unlikely(ptr == NULL)) {
329 _talloc_set_name_const(ptr, name);
335 make a secondary reference to a pointer, hanging off the given context.
336 the pointer remains valid until both the original caller and this given
339 the major use for this is when two different structures need to reference the
340 same underlying data, and you want to be able to free the two instances separately,
343 void *_talloc_reference(const void *context, const void *ptr)
345 struct talloc_chunk *tc;
346 struct talloc_reference_handle *handle;
347 if (unlikely(ptr == NULL)) return NULL;
350 tc = talloc_chunk_from_ptr(ptr);
351 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
352 sizeof(struct talloc_reference_handle),
353 TALLOC_MAGIC_REFERENCE);
354 if (unlikely(handle == NULL)) {
359 /* note that we hang the destructor off the handle, not the
360 main context as that allows the caller to still setup their
361 own destructor on the context if they want to */
362 talloc_set_destructor(handle, talloc_reference_destructor);
363 handle->ptr = discard_const_p(void, ptr);
364 _TLIST_ADD(tc->refs, handle);
370 return 1 if ptr is a parent of context
372 static int _talloc_is_parent(const void *context, const void *ptr)
374 struct talloc_chunk *tc;
376 if (context == NULL) {
380 tc = talloc_chunk_from_ptr(context);
382 if (TC_PTR_FROM_CHUNK(tc) == ptr) {
385 while (tc && tc->prev) tc = tc->prev;
394 move a lump of memory from one talloc context to another return the
395 ptr on success, or NULL if it could not be transferred.
396 passing NULL as ptr will always return NULL with no side effects.
398 static void *__talloc_steal(const void *new_ctx, const void *ptr)
400 struct talloc_chunk *tc, *new_tc;
402 if (unlikely(!ptr)) {
406 if (unlikely(new_ctx == NULL)) {
407 new_ctx = null_context;
410 tc = talloc_chunk_from_ptr(ptr);
412 if (unlikely(new_ctx == NULL)) {
414 _TLIST_REMOVE(tc->parent->child, tc);
415 if (tc->parent->child) {
416 tc->parent->child->parent = tc->parent;
419 if (tc->prev) tc->prev->next = tc->next;
420 if (tc->next) tc->next->prev = tc->prev;
423 tc->parent = tc->next = tc->prev = NULL;
424 return discard_const_p(void, ptr);
427 new_tc = talloc_chunk_from_ptr(new_ctx);
429 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
430 return discard_const_p(void, ptr);
434 _TLIST_REMOVE(tc->parent->child, tc);
435 if (tc->parent->child) {
436 tc->parent->child->parent = tc->parent;
439 if (tc->prev) tc->prev->next = tc->next;
440 if (tc->next) tc->next->prev = tc->prev;
444 if (new_tc->child) new_tc->child->parent = NULL;
445 _TLIST_ADD(new_tc->child, tc);
447 return discard_const_p(void, ptr);
451 internal talloc_free call
453 static inline int _talloc_free(void *ptr)
455 struct talloc_chunk *tc;
456 void *oldparent = NULL;
458 if (unlikely(ptr == NULL)) {
462 tc = talloc_chunk_from_ptr(ptr);
464 if (unlikely(tc->refs)) {
466 /* check this is a reference from a child or grantchild
467 * back to it's parent or grantparent
469 * in that case we need to remove the reference and
470 * call another instance of talloc_free() on the current
473 is_child = _talloc_is_parent(tc->refs, ptr);
474 _talloc_free(tc->refs);
476 return _talloc_free(ptr);
481 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
482 /* we have a free loop - stop looping */
486 if (unlikely(tc->destructor)) {
487 talloc_destructor_t d = tc->destructor;
488 if (d == (talloc_destructor_t)-1) {
491 tc->destructor = (talloc_destructor_t)-1;
496 tc->destructor = NULL;
499 if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC))
500 oldparent = talloc_parent(ptr);
503 _TLIST_REMOVE(tc->parent->child, tc);
504 if (tc->parent->child) {
505 tc->parent->child->parent = tc->parent;
508 if (tc->prev) tc->prev->next = tc->next;
509 if (tc->next) tc->next->prev = tc->prev;
512 tc->flags |= TALLOC_FLAG_LOOP;
515 /* we need to work out who will own an abandoned child
516 if it cannot be freed. In priority order, the first
517 choice is owner of any remaining reference to this
518 pointer, the second choice is our parent, and the
519 final choice is the null context. */
520 void *child = TC_PTR_FROM_CHUNK(tc->child);
521 const void *new_parent = null_context;
522 if (unlikely(tc->child->refs)) {
523 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
524 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
526 if (unlikely(_talloc_free(child) == -1)) {
527 if (new_parent == null_context) {
528 struct talloc_chunk *p = talloc_parent_chunk(ptr);
529 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
531 __talloc_steal(new_parent, child);
535 tc->flags |= TALLOC_FLAG_FREE;
537 if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC))
538 tc_external_realloc(oldparent, tc, 0);
545 void *_talloc_steal(const void *new_ctx, const void *ptr)
550 p = __talloc_steal(new_ctx, ptr);
556 remove a secondary reference to a pointer. This undo's what
557 talloc_reference() has done. The context and pointer arguments
558 must match those given to a talloc_reference()
560 static inline int talloc_unreference(const void *context, const void *ptr)
562 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
563 struct talloc_reference_handle *h;
565 if (unlikely(context == NULL)) {
566 context = null_context;
569 for (h=tc->refs;h;h=h->next) {
570 struct talloc_chunk *p = talloc_parent_chunk(h);
572 if (context == NULL) break;
573 } else if (TC_PTR_FROM_CHUNK(p) == context) {
581 return _talloc_free(h);
585 remove a specific parent context from a pointer. This is a more
586 controlled varient of talloc_free()
588 int talloc_unlink(const void *context, void *ptr)
590 struct talloc_chunk *tc_p, *new_p;
597 if (context == NULL) {
598 context = null_context;
602 if (talloc_unreference(context, ptr) == 0) {
607 if (context == NULL) {
608 if (talloc_parent_chunk(ptr) != NULL) {
613 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
619 tc_p = talloc_chunk_from_ptr(ptr);
621 if (tc_p->refs == NULL) {
622 int ret = _talloc_free(ptr);
627 new_p = talloc_parent_chunk(tc_p->refs);
629 new_parent = TC_PTR_FROM_CHUNK(new_p);
634 if (talloc_unreference(new_parent, ptr) != 0) {
639 __talloc_steal(new_parent, ptr);
646 add a name to an existing pointer - va_list version
648 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
650 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
652 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
653 tc->name = talloc_vasprintf(ptr, fmt, ap);
654 if (likely(tc->name)) {
655 _talloc_set_name_const(tc->name, ".name");
661 add a name to an existing pointer
663 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
668 name = talloc_set_name_v(ptr, fmt, ap);
675 create a named talloc pointer. Any talloc pointer can be named, and
676 talloc_named() operates just like talloc() except that it allows you
679 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
686 ptr = __talloc(context, size);
688 if (unlikely(ptr == NULL)) return NULL;
691 name = talloc_set_name_v(ptr, fmt, ap);
694 if (unlikely(name == NULL)) {
703 return the name of a talloc ptr, or "UNNAMED"
705 const char *talloc_get_name(const void *ptr)
707 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
708 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
711 if (likely(tc->name)) {
719 check if a pointer has the given name. If it does, return the pointer,
720 otherwise return NULL
722 void *talloc_check_name(const void *ptr, const char *name)
725 if (unlikely(ptr == NULL)) return NULL;
726 pname = talloc_get_name(ptr);
727 if (likely(pname == name || strcmp(pname, name) == 0)) {
728 return discard_const_p(void, ptr);
735 this is for compatibility with older versions of talloc
737 void *talloc_init(const char *fmt, ...)
744 * samba3 expects talloc_report_depth_cb(NULL, ...)
745 * reports all talloc'ed memory, so we need to enable
748 talloc_enable_null_tracking();
751 ptr = __talloc(NULL, 0);
753 if (unlikely(ptr == NULL)) return NULL;
756 name = talloc_set_name_v(ptr, fmt, ap);
759 if (unlikely(name == NULL)) {
768 Allocate a bit of memory as a child of an existing pointer
770 void *_talloc(const void *context, size_t size)
772 return __talloc(context, size);
776 externally callable talloc_set_name_const()
778 void talloc_set_name_const(const void *ptr, const char *name)
780 _talloc_set_name_const(ptr, name);
784 create a named talloc pointer. Any talloc pointer can be named, and
785 talloc_named() operates just like talloc() except that it allows you
788 void *talloc_named_const(const void *context, size_t size, const char *name)
792 p = _talloc_named_const(context, size, name);
798 free a talloc pointer. This also frees all child pointers of this
801 return 0 if the memory is actually freed, otherwise -1. The memory
802 will not be freed if the ref_count is > 1 or the destructor (if
803 any) returns non-zero
805 int talloc_free(void *ptr)
807 int saved_errno = errno, ret;
809 ret = _talloc_free(ptr);
819 A talloc version of realloc. The context argument is only used if
822 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
824 struct talloc_chunk *tc;
827 /* size zero is equivalent to free() */
828 if (unlikely(size == 0)) {
833 if (unlikely(size >= MAX_TALLOC_SIZE)) {
837 /* realloc(NULL) is equivalent to malloc() */
839 return talloc_named_const(context, size, name);
842 tc = talloc_chunk_from_ptr(ptr);
844 /* don't allow realloc on referenced pointers */
845 if (unlikely(tc->refs)) {
850 if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC)) {
851 /* need to get parent before setting free flag. */
852 void *parent = talloc_parent(ptr);
853 tc->flags |= TALLOC_FLAG_FREE;
854 new_ptr = tc_external_realloc(parent, tc, size + TC_HDR_SIZE);
856 /* by resetting magic we catch users of the old memory */
857 tc->flags |= TALLOC_FLAG_FREE;
860 new_ptr = malloc(size + TC_HDR_SIZE);
862 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
866 new_ptr = realloc(tc, size + TC_HDR_SIZE);
870 if (unlikely(!new_ptr)) {
871 tc->flags &= ~TALLOC_FLAG_FREE;
876 tc = (struct talloc_chunk *)new_ptr;
877 tc->flags &= ~TALLOC_FLAG_FREE;
879 tc->parent->child = tc;
882 tc->child->parent = tc;
893 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
896 return TC_PTR_FROM_CHUNK(tc);
900 a wrapper around talloc_steal() for situations where you are moving a pointer
901 between two structures, and want the old pointer to be set to NULL
903 void *_talloc_move(const void *new_ctx, const void *_pptr)
905 const void **pptr = discard_const_p(const void *,_pptr);
906 void *ret = _talloc_steal(new_ctx, *pptr);
912 return the total size of a talloc pool (subtree)
914 static size_t _talloc_total_size(const void *ptr)
917 struct talloc_chunk *c, *tc;
919 tc = talloc_chunk_from_ptr(ptr);
921 if (tc->flags & TALLOC_FLAG_LOOP) {
925 tc->flags |= TALLOC_FLAG_LOOP;
928 for (c=tc->child;c;c=c->next) {
929 total += _talloc_total_size(TC_PTR_FROM_CHUNK(c));
932 tc->flags &= ~TALLOC_FLAG_LOOP;
937 size_t talloc_total_size(const void *ptr)
949 total = _talloc_total_size(ptr);
955 return the total number of blocks in a talloc pool (subtree)
957 static size_t _talloc_total_blocks(const void *ptr)
960 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
962 if (tc->flags & TALLOC_FLAG_LOOP) {
966 tc->flags |= TALLOC_FLAG_LOOP;
969 for (c=tc->child;c;c=c->next) {
970 total += _talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
973 tc->flags &= ~TALLOC_FLAG_LOOP;
978 size_t talloc_total_blocks(const void *ptr)
983 total = _talloc_total_blocks(ptr);
990 return the number of external references to a pointer
992 size_t talloc_reference_count(const void *ptr)
994 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
995 struct talloc_reference_handle *h;
999 for (h=tc->refs;h;h=h->next) {
1007 report on memory usage by all children of a pointer, giving a full tree view
1009 static void _talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1010 void (*callback)(const void *ptr,
1011 int depth, int max_depth,
1013 void *private_data),
1016 struct talloc_chunk *c, *tc;
1018 tc = talloc_chunk_from_ptr(ptr);
1020 if (tc->flags & TALLOC_FLAG_LOOP) {
1024 callback(ptr, depth, max_depth, 0, private_data);
1026 if (max_depth >= 0 && depth >= max_depth) {
1030 tc->flags |= TALLOC_FLAG_LOOP;
1031 for (c=tc->child;c;c=c->next) {
1032 if (c->name == TALLOC_MAGIC_REFERENCE) {
1033 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1034 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1036 _talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1039 tc->flags &= ~TALLOC_FLAG_LOOP;
1042 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1043 void (*callback)(const void *ptr,
1044 int depth, int max_depth,
1046 void *private_data),
1052 if (ptr == NULL) return;
1055 _talloc_report_depth_cb(ptr, depth, max_depth, callback, private_data);
1059 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1061 const char *name = talloc_get_name(ptr);
1062 FILE *f = (FILE *)_f;
1065 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1070 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1071 (max_depth < 0 ? "full " :""), name,
1072 (unsigned long)talloc_total_size(ptr),
1073 (unsigned long)talloc_total_blocks(ptr));
1077 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1080 (unsigned long)talloc_total_size(ptr),
1081 (unsigned long)talloc_total_blocks(ptr),
1082 (int)talloc_reference_count(ptr), ptr);
1085 fprintf(f, "content: ");
1086 if (talloc_total_size(ptr)) {
1087 int tot = talloc_total_size(ptr);
1090 for (i = 0; i < tot; i++) {
1091 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1092 fprintf(f, "%c", ((char *)ptr)[i]);
1094 fprintf(f, "~%02x", ((char *)ptr)[i]);
1103 report on memory usage by all children of a pointer, giving a full tree view
1105 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1107 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1112 report on memory usage by all children of a pointer, giving a full tree view
1114 void talloc_report_full(const void *ptr, FILE *f)
1116 talloc_report_depth_file(ptr, 0, -1, f);
1120 report on memory usage by all children of a pointer
1122 void talloc_report(const void *ptr, FILE *f)
1124 talloc_report_depth_file(ptr, 0, 1, f);
1128 report on any memory hanging off the null context
1130 static void talloc_report_null(void)
1132 if (talloc_total_size(null_context) != 0) {
1133 talloc_report(null_context, stderr);
1138 report on any memory hanging off the null context
1140 static void talloc_report_null_full(void)
1142 if (talloc_total_size(null_context) != 0) {
1143 talloc_report_full(null_context, stderr);
1148 enable tracking of the NULL context
1150 void talloc_enable_null_tracking(void)
1153 if (null_context == NULL) {
1154 null_context = _talloc_named_const(NULL, 0, "null_context");
1160 disable tracking of the NULL context
1162 void talloc_disable_null_tracking(void)
1165 _talloc_free(null_context);
1166 null_context = NULL;
1171 enable leak reporting on exit
1173 void talloc_enable_leak_report(void)
1175 talloc_enable_null_tracking();
1176 atexit(talloc_report_null);
1180 enable full leak reporting on exit
1182 void talloc_enable_leak_report_full(void)
1184 talloc_enable_null_tracking();
1185 atexit(talloc_report_null_full);
1189 talloc and zero memory.
1191 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1196 p = _talloc_named_const(ctx, size, name);
1200 memset(p, '\0', size);
1207 memdup with a talloc.
1209 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1214 newp = _talloc_named_const(t, size, name);
1218 memcpy(newp, p, size);
1225 strdup with a talloc
1227 char *talloc_strdup(const void *t, const char *p)
1233 ret = (char *)talloc_memdup(t, p, strlen(p) + 1);
1235 _talloc_set_name_const(ret, ret);
1241 append to a talloced string
1243 char *talloc_append_string(char *orig, const char *append)
1246 size_t olen = strlen(orig);
1252 alenz = strlen(append) + 1;
1254 ret = talloc_realloc(NULL, orig, char, olen + alenz);
1258 /* append the string with the trailing \0 */
1259 memcpy(&ret[olen], append, alenz);
1261 _talloc_set_name_const(ret, ret);
1267 strndup with a talloc
1269 char *talloc_strndup(const void *t, const char *p, size_t n)
1274 for (len=0; len<n && p[len]; len++) ;
1277 ret = (char *)__talloc(t, len + 1);
1279 if (!ret) { return NULL; }
1280 memcpy(ret, p, len);
1282 _talloc_set_name_const(ret, ret);
1286 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1293 /* this call looks strange, but it makes it work on older solaris boxes */
1295 len = vsnprintf(&c, 1, fmt, ap2);
1302 ret = (char *)__talloc(t, len+1);
1306 vsnprintf(ret, len+1, fmt, ap2);
1308 _talloc_set_name_const(ret, ret);
1316 Perform string formatting, and return a pointer to newly allocated
1317 memory holding the result, inside a memory pool.
1319 char *talloc_asprintf(const void *t, const char *fmt, ...)
1325 ret = talloc_vasprintf(t, fmt, ap);
1332 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1333 * and return @p s, which may have moved. Good for gradually
1334 * accumulating output into a string buffer.
1336 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1338 struct talloc_chunk *tc;
1344 return talloc_vasprintf(NULL, fmt, ap);
1347 tc = talloc_chunk_from_ptr(s);
1349 s_len = tc->size - 1;
1352 len = vsnprintf(&c, 1, fmt, ap2);
1356 /* Either the vsnprintf failed or the format resulted in
1357 * no characters being formatted. In the former case, we
1358 * ought to return NULL, in the latter we ought to return
1359 * the original string. Most current callers of this
1360 * function expect it to never return NULL.
1365 s = talloc_realloc(NULL, s, char, s_len + len+1);
1366 if (!s) return NULL;
1369 vsnprintf(s+s_len, len+1, fmt, ap2);
1371 _talloc_set_name_const(s, s);
1377 Realloc @p s to append the formatted result of @p fmt and return @p
1378 s, which may have moved. Good for gradually accumulating output
1379 into a string buffer.
1381 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1386 s = talloc_vasprintf_append(s, fmt, ap);
1392 alloc an array, checking for integer overflow in the array size
1394 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1398 if (count >= MAX_TALLOC_SIZE/el_size) {
1402 p = _talloc_named_const(ctx, el_size * count, name);
1408 alloc an zero array, checking for integer overflow in the array size
1410 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1414 if (count >= MAX_TALLOC_SIZE/el_size) {
1418 p = _talloc_zero(ctx, el_size * count, name);
1424 realloc an array, checking for integer overflow in the array size
1426 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1428 if (count >= MAX_TALLOC_SIZE/el_size) {
1431 return _talloc_realloc(ctx, ptr, el_size * count, name);
1435 a function version of talloc_realloc(), so it can be passed as a function pointer
1436 to libraries that want a realloc function (a realloc function encapsulates
1437 all the basic capabilities of an allocation library, which is why this is useful)
1439 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1441 return _talloc_realloc(context, ptr, size, NULL);
1445 static int talloc_autofree_destructor(void *ptr)
1447 autofree_context = NULL;
1451 static void talloc_autofree(void)
1453 if (autofree_context && *autofree_context == getpid())
1454 talloc_free(autofree_context);
1458 return a context which will be auto-freed on exit
1459 this is useful for reducing the noise in leak reports
1461 void *talloc_autofree_context(void)
1463 if (autofree_context == NULL || *autofree_context != getpid()) {
1464 autofree_context = talloc(NULL, pid_t);
1465 *autofree_context = getpid();
1466 talloc_set_name_const(autofree_context, "autofree_context");
1468 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1469 atexit(talloc_autofree);
1471 return autofree_context;
1474 size_t talloc_get_size(const void *context)
1476 struct talloc_chunk *tc;
1478 if (context == NULL)
1481 tc = talloc_chunk_from_ptr(context);
1487 find a parent of this context that has the given name, if any
1489 void *talloc_find_parent_byname(const void *context, const char *name)
1491 struct talloc_chunk *tc;
1493 if (context == NULL) {
1498 tc = talloc_chunk_from_ptr(context);
1500 if (tc->name && strcmp(tc->name, name) == 0) {
1502 return TC_PTR_FROM_CHUNK(tc);
1504 while (tc && tc->prev) tc = tc->prev;
1514 show the parentage of a context
1516 void talloc_show_parents(const void *context, FILE *file)
1518 struct talloc_chunk *tc;
1520 if (context == NULL) {
1521 fprintf(file, "talloc no parents for NULL\n");
1526 tc = talloc_chunk_from_ptr(context);
1527 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1529 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1530 while (tc && tc->prev) tc = tc->prev;
1539 int talloc_is_parent(const void *context, const void *ptr)
1543 ret = _talloc_is_parent(context, ptr);
1548 void *talloc_add_external(const void *ctx,
1549 void *(*realloc)(const void *, void *, size_t))
1551 struct talloc_chunk *tc, *parent;
1555 if (tc_external_realloc && tc_external_realloc != realloc)
1556 TALLOC_ABORT("talloc_add_external realloc replaced");
1557 tc_external_realloc = realloc;
1559 if (unlikely(ctx == NULL)) {
1563 parent = talloc_chunk_from_ptr(ctx);
1565 tc = tc_external_realloc(ctx, NULL, TC_HDR_SIZE);
1566 p = init_talloc(parent, tc, 0, 1);
1572 void _talloc_locksafe(void (*lock)(void *), void (*unlock)(void *), void *data)
1576 tc_lock_data = data;