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_alloc)(void *parent, size_t size);
86 static void (*tc_external_free)(void *ptr, void *parent);
87 static void *(*tc_external_realloc)(void *ptr, void *parent, size_t size);
89 struct talloc_reference_handle {
90 struct talloc_reference_handle *next, *prev;
94 typedef int (*talloc_destructor_t)(void *);
97 struct talloc_chunk *next, *prev;
98 struct talloc_chunk *parent, *child;
99 struct talloc_reference_handle *refs;
100 talloc_destructor_t destructor;
106 /* 16 byte alignment seems to keep everyone happy */
107 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
108 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
110 /* panic if we get a bad magic value */
111 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
113 const char *pp = (const char *)ptr;
114 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
115 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
116 if (tc->flags & TALLOC_FLAG_FREE) {
117 TALLOC_ABORT("Bad talloc magic value - double free");
119 TALLOC_ABORT("Bad talloc magic value - unknown value");
125 /* hook into the front of the list */
126 #define _TLIST_ADD(list, p) \
130 (p)->next = (p)->prev = NULL; \
132 (list)->prev = (p); \
133 (p)->next = (list); \
139 /* remove an element from a list - element doesn't have to be in list. */
140 #define _TLIST_REMOVE(list, p) \
142 if ((p) == (list)) { \
143 (list) = (p)->next; \
144 if (list) (list)->prev = NULL; \
146 if ((p)->prev) (p)->prev->next = (p)->next; \
147 if ((p)->next) (p)->next->prev = (p)->prev; \
149 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
154 return the parent chunk of a pointer
156 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
158 struct talloc_chunk *tc;
160 if (unlikely(ptr == NULL)) {
164 tc = talloc_chunk_from_ptr(ptr);
165 while (tc->prev) tc=tc->prev;
170 void *talloc_parent(const void *ptr)
172 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
173 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
179 const char *talloc_parent_name(const void *ptr)
181 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
182 return tc? tc->name : NULL;
186 Allocate a bit of memory as a child of an existing pointer
188 static inline void *__talloc(const void *context, size_t size)
190 struct talloc_chunk *tc;
191 struct talloc_chunk *parent = NULL; /* Prevent spurious gcc warning */
192 unsigned flags = TALLOC_MAGIC;
194 if (unlikely(context == NULL)) {
195 context = null_context;
198 if (unlikely(size >= MAX_TALLOC_SIZE)) {
202 if (likely(context)) {
203 parent = talloc_chunk_from_ptr(context);
204 if (unlikely(parent->flags & TALLOC_FLAG_EXT_ALLOC)) {
205 tc = tc_external_alloc(TC_PTR_FROM_CHUNK(parent),
207 flags |= TALLOC_FLAG_EXT_ALLOC;
212 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
214 if (unlikely(tc == NULL)) return NULL;
218 tc->destructor = NULL;
223 if (likely(context)) {
225 parent->child->parent = NULL;
226 tc->next = parent->child;
235 tc->next = tc->prev = tc->parent = NULL;
238 return TC_PTR_FROM_CHUNK(tc);
242 setup a destructor to be called on free of a pointer
243 the destructor should return 0 on success, or -1 on failure.
244 if the destructor fails then the free is failed, and the memory can
245 be continued to be used
247 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
249 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
250 tc->destructor = destructor;
254 increase the reference count on a piece of memory.
256 int talloc_increase_ref_count(const void *ptr)
258 if (unlikely(!talloc_reference(null_context, ptr))) {
265 helper for talloc_reference()
267 this is referenced by a function pointer and should not be inline
269 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
271 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
272 _TLIST_REMOVE(ptr_tc->refs, handle);
277 more efficient way to add a name to a pointer - the name must point to a
280 static inline void _talloc_set_name_const(const void *ptr, const char *name)
282 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
287 internal talloc_named_const()
289 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
293 ptr = __talloc(context, size);
294 if (unlikely(ptr == NULL)) {
298 _talloc_set_name_const(ptr, name);
304 make a secondary reference to a pointer, hanging off the given context.
305 the pointer remains valid until both the original caller and this given
308 the major use for this is when two different structures need to reference the
309 same underlying data, and you want to be able to free the two instances separately,
312 void *_talloc_reference(const void *context, const void *ptr)
314 struct talloc_chunk *tc;
315 struct talloc_reference_handle *handle;
316 if (unlikely(ptr == NULL)) return NULL;
318 tc = talloc_chunk_from_ptr(ptr);
319 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
320 sizeof(struct talloc_reference_handle),
321 TALLOC_MAGIC_REFERENCE);
322 if (unlikely(handle == NULL)) return NULL;
324 /* note that we hang the destructor off the handle, not the
325 main context as that allows the caller to still setup their
326 own destructor on the context if they want to */
327 talloc_set_destructor(handle, talloc_reference_destructor);
328 handle->ptr = discard_const_p(void, ptr);
329 _TLIST_ADD(tc->refs, handle);
335 internal talloc_free call
337 static inline int _talloc_free(void *ptr)
339 struct talloc_chunk *tc;
340 void *oldparent = NULL;
342 if (unlikely(ptr == NULL)) {
346 tc = talloc_chunk_from_ptr(ptr);
348 if (unlikely(tc->refs)) {
350 /* check this is a reference from a child or grantchild
351 * back to it's parent or grantparent
353 * in that case we need to remove the reference and
354 * call another instance of talloc_free() on the current
357 is_child = talloc_is_parent(tc->refs, ptr);
358 _talloc_free(tc->refs);
360 return _talloc_free(ptr);
365 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
366 /* we have a free loop - stop looping */
370 if (unlikely(tc->destructor)) {
371 talloc_destructor_t d = tc->destructor;
372 if (d == (talloc_destructor_t)-1) {
375 tc->destructor = (talloc_destructor_t)-1;
380 tc->destructor = NULL;
383 if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC))
384 oldparent = talloc_parent(ptr);
387 _TLIST_REMOVE(tc->parent->child, tc);
388 if (tc->parent->child) {
389 tc->parent->child->parent = tc->parent;
392 if (tc->prev) tc->prev->next = tc->next;
393 if (tc->next) tc->next->prev = tc->prev;
396 tc->flags |= TALLOC_FLAG_LOOP;
399 /* we need to work out who will own an abandoned child
400 if it cannot be freed. In priority order, the first
401 choice is owner of any remaining reference to this
402 pointer, the second choice is our parent, and the
403 final choice is the null context. */
404 void *child = TC_PTR_FROM_CHUNK(tc->child);
405 const void *new_parent = null_context;
406 if (unlikely(tc->child->refs)) {
407 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
408 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
410 if (unlikely(_talloc_free(child) == -1)) {
411 if (new_parent == null_context) {
412 struct talloc_chunk *p = talloc_parent_chunk(ptr);
413 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
415 talloc_steal(new_parent, child);
419 tc->flags |= TALLOC_FLAG_FREE;
421 if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC))
422 tc_external_free(tc, oldparent);
430 move a lump of memory from one talloc context to another return the
431 ptr on success, or NULL if it could not be transferred.
432 passing NULL as ptr will always return NULL with no side effects.
434 void *_talloc_steal(const void *new_ctx, const void *ptr)
436 struct talloc_chunk *tc, *new_tc;
438 if (unlikely(!ptr)) {
442 if (unlikely(new_ctx == NULL)) {
443 new_ctx = null_context;
446 tc = talloc_chunk_from_ptr(ptr);
448 if (unlikely(new_ctx == NULL)) {
450 _TLIST_REMOVE(tc->parent->child, tc);
451 if (tc->parent->child) {
452 tc->parent->child->parent = tc->parent;
455 if (tc->prev) tc->prev->next = tc->next;
456 if (tc->next) tc->next->prev = tc->prev;
459 tc->parent = tc->next = tc->prev = NULL;
460 return discard_const_p(void, ptr);
463 new_tc = talloc_chunk_from_ptr(new_ctx);
465 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
466 return discard_const_p(void, ptr);
470 _TLIST_REMOVE(tc->parent->child, tc);
471 if (tc->parent->child) {
472 tc->parent->child->parent = tc->parent;
475 if (tc->prev) tc->prev->next = tc->next;
476 if (tc->next) tc->next->prev = tc->prev;
480 if (new_tc->child) new_tc->child->parent = NULL;
481 _TLIST_ADD(new_tc->child, tc);
483 return discard_const_p(void, ptr);
489 remove a secondary reference to a pointer. This undo's what
490 talloc_reference() has done. The context and pointer arguments
491 must match those given to a talloc_reference()
493 static inline int talloc_unreference(const void *context, const void *ptr)
495 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
496 struct talloc_reference_handle *h;
498 if (unlikely(context == NULL)) {
499 context = null_context;
502 for (h=tc->refs;h;h=h->next) {
503 struct talloc_chunk *p = talloc_parent_chunk(h);
505 if (context == NULL) break;
506 } else if (TC_PTR_FROM_CHUNK(p) == context) {
514 return _talloc_free(h);
518 remove a specific parent context from a pointer. This is a more
519 controlled varient of talloc_free()
521 int talloc_unlink(const void *context, void *ptr)
523 struct talloc_chunk *tc_p, *new_p;
530 if (context == NULL) {
531 context = null_context;
534 if (talloc_unreference(context, ptr) == 0) {
538 if (context == NULL) {
539 if (talloc_parent_chunk(ptr) != NULL) {
543 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
548 tc_p = talloc_chunk_from_ptr(ptr);
550 if (tc_p->refs == NULL) {
551 return _talloc_free(ptr);
554 new_p = talloc_parent_chunk(tc_p->refs);
556 new_parent = TC_PTR_FROM_CHUNK(new_p);
561 if (talloc_unreference(new_parent, ptr) != 0) {
565 talloc_steal(new_parent, ptr);
571 add a name to an existing pointer - va_list version
573 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
575 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
577 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
578 tc->name = talloc_vasprintf(ptr, fmt, ap);
579 if (likely(tc->name)) {
580 _talloc_set_name_const(tc->name, ".name");
586 add a name to an existing pointer
588 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
593 name = talloc_set_name_v(ptr, fmt, ap);
600 create a named talloc pointer. Any talloc pointer can be named, and
601 talloc_named() operates just like talloc() except that it allows you
604 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
610 ptr = __talloc(context, size);
611 if (unlikely(ptr == NULL)) return NULL;
614 name = talloc_set_name_v(ptr, fmt, ap);
617 if (unlikely(name == NULL)) {
626 return the name of a talloc ptr, or "UNNAMED"
628 const char *talloc_get_name(const void *ptr)
630 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
631 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
634 if (likely(tc->name)) {
642 check if a pointer has the given name. If it does, return the pointer,
643 otherwise return NULL
645 void *talloc_check_name(const void *ptr, const char *name)
648 if (unlikely(ptr == NULL)) return NULL;
649 pname = talloc_get_name(ptr);
650 if (likely(pname == name || strcmp(pname, name) == 0)) {
651 return discard_const_p(void, ptr);
658 this is for compatibility with older versions of talloc
660 void *talloc_init(const char *fmt, ...)
667 * samba3 expects talloc_report_depth_cb(NULL, ...)
668 * reports all talloc'ed memory, so we need to enable
671 talloc_enable_null_tracking();
673 ptr = __talloc(NULL, 0);
674 if (unlikely(ptr == NULL)) return NULL;
677 name = talloc_set_name_v(ptr, fmt, ap);
680 if (unlikely(name == NULL)) {
689 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
690 should probably not be used in new code. It's in here to keep the talloc
691 code consistent across Samba 3 and 4.
693 void talloc_free_children(void *ptr)
695 struct talloc_chunk *tc;
697 if (unlikely(ptr == NULL)) {
701 tc = talloc_chunk_from_ptr(ptr);
704 /* we need to work out who will own an abandoned child
705 if it cannot be freed. In priority order, the first
706 choice is owner of any remaining reference to this
707 pointer, the second choice is our parent, and the
708 final choice is the null context. */
709 void *child = TC_PTR_FROM_CHUNK(tc->child);
710 const void *new_parent = null_context;
711 if (unlikely(tc->child->refs)) {
712 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
713 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
715 if (unlikely(_talloc_free(child) == -1)) {
716 if (new_parent == null_context) {
717 struct talloc_chunk *p = talloc_parent_chunk(ptr);
718 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
720 talloc_steal(new_parent, child);
726 Allocate a bit of memory as a child of an existing pointer
728 void *_talloc(const void *context, size_t size)
730 return __talloc(context, size);
734 externally callable talloc_set_name_const()
736 void talloc_set_name_const(const void *ptr, const char *name)
738 _talloc_set_name_const(ptr, name);
742 create a named talloc pointer. Any talloc pointer can be named, and
743 talloc_named() operates just like talloc() except that it allows you
746 void *talloc_named_const(const void *context, size_t size, const char *name)
748 return _talloc_named_const(context, size, name);
752 free a talloc pointer. This also frees all child pointers of this
755 return 0 if the memory is actually freed, otherwise -1. The memory
756 will not be freed if the ref_count is > 1 or the destructor (if
757 any) returns non-zero
759 int talloc_free(void *ptr)
761 int saved_errno = errno, ret;
762 ret = _talloc_free(ptr);
771 A talloc version of realloc. The context argument is only used if
774 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
776 struct talloc_chunk *tc;
779 /* size zero is equivalent to free() */
780 if (unlikely(size == 0)) {
785 if (unlikely(size >= MAX_TALLOC_SIZE)) {
789 /* realloc(NULL) is equivalent to malloc() */
791 return _talloc_named_const(context, size, name);
794 tc = talloc_chunk_from_ptr(ptr);
796 /* don't allow realloc on referenced pointers */
797 if (unlikely(tc->refs)) {
801 if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC)) {
802 /* need to get parent before setting free flag. */
803 void *parent = talloc_parent(ptr);
804 tc->flags |= TALLOC_FLAG_FREE;
805 new_ptr = tc_external_realloc(tc, parent, size + TC_HDR_SIZE);
807 /* by resetting magic we catch users of the old memory */
808 tc->flags |= TALLOC_FLAG_FREE;
811 new_ptr = malloc(size + TC_HDR_SIZE);
813 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
817 new_ptr = realloc(tc, size + TC_HDR_SIZE);
821 if (unlikely(!new_ptr)) {
822 tc->flags &= ~TALLOC_FLAG_FREE;
826 tc = (struct talloc_chunk *)new_ptr;
827 tc->flags &= ~TALLOC_FLAG_FREE;
829 tc->parent->child = tc;
832 tc->child->parent = tc;
843 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
845 return TC_PTR_FROM_CHUNK(tc);
849 a wrapper around talloc_steal() for situations where you are moving a pointer
850 between two structures, and want the old pointer to be set to NULL
852 void *_talloc_move(const void *new_ctx, const void *_pptr)
854 const void **pptr = discard_const_p(const void *,_pptr);
855 void *ret = _talloc_steal(new_ctx, *pptr);
861 return the total size of a talloc pool (subtree)
863 size_t talloc_total_size(const void *ptr)
866 struct talloc_chunk *c, *tc;
875 tc = talloc_chunk_from_ptr(ptr);
877 if (tc->flags & TALLOC_FLAG_LOOP) {
881 tc->flags |= TALLOC_FLAG_LOOP;
884 for (c=tc->child;c;c=c->next) {
885 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
888 tc->flags &= ~TALLOC_FLAG_LOOP;
894 return the total number of blocks in a talloc pool (subtree)
896 size_t talloc_total_blocks(const void *ptr)
899 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
901 if (tc->flags & TALLOC_FLAG_LOOP) {
905 tc->flags |= TALLOC_FLAG_LOOP;
908 for (c=tc->child;c;c=c->next) {
909 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
912 tc->flags &= ~TALLOC_FLAG_LOOP;
918 return the number of external references to a pointer
920 size_t talloc_reference_count(const void *ptr)
922 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
923 struct talloc_reference_handle *h;
926 for (h=tc->refs;h;h=h->next) {
933 report on memory usage by all children of a pointer, giving a full tree view
935 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
936 void (*callback)(const void *ptr,
937 int depth, int max_depth,
942 struct talloc_chunk *c, *tc;
947 if (ptr == NULL) return;
949 tc = talloc_chunk_from_ptr(ptr);
951 if (tc->flags & TALLOC_FLAG_LOOP) {
955 callback(ptr, depth, max_depth, 0, private_data);
957 if (max_depth >= 0 && depth >= max_depth) {
961 tc->flags |= TALLOC_FLAG_LOOP;
962 for (c=tc->child;c;c=c->next) {
963 if (c->name == TALLOC_MAGIC_REFERENCE) {
964 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
965 callback(h->ptr, depth + 1, max_depth, 1, private_data);
967 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
970 tc->flags &= ~TALLOC_FLAG_LOOP;
973 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
975 const char *name = talloc_get_name(ptr);
976 FILE *f = (FILE *)_f;
979 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
984 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
985 (max_depth < 0 ? "full " :""), name,
986 (unsigned long)talloc_total_size(ptr),
987 (unsigned long)talloc_total_blocks(ptr));
991 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
994 (unsigned long)talloc_total_size(ptr),
995 (unsigned long)talloc_total_blocks(ptr),
996 (int)talloc_reference_count(ptr), ptr);
999 fprintf(f, "content: ");
1000 if (talloc_total_size(ptr)) {
1001 int tot = talloc_total_size(ptr);
1004 for (i = 0; i < tot; i++) {
1005 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1006 fprintf(f, "%c", ((char *)ptr)[i]);
1008 fprintf(f, "~%02x", ((char *)ptr)[i]);
1017 report on memory usage by all children of a pointer, giving a full tree view
1019 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1021 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1026 report on memory usage by all children of a pointer, giving a full tree view
1028 void talloc_report_full(const void *ptr, FILE *f)
1030 talloc_report_depth_file(ptr, 0, -1, f);
1034 report on memory usage by all children of a pointer
1036 void talloc_report(const void *ptr, FILE *f)
1038 talloc_report_depth_file(ptr, 0, 1, f);
1042 report on any memory hanging off the null context
1044 static void talloc_report_null(void)
1046 if (talloc_total_size(null_context) != 0) {
1047 talloc_report(null_context, stderr);
1052 report on any memory hanging off the null context
1054 static void talloc_report_null_full(void)
1056 if (talloc_total_size(null_context) != 0) {
1057 talloc_report_full(null_context, stderr);
1062 enable tracking of the NULL context
1064 void talloc_enable_null_tracking(void)
1066 if (null_context == NULL) {
1067 null_context = _talloc_named_const(NULL, 0, "null_context");
1072 disable tracking of the NULL context
1074 void talloc_disable_null_tracking(void)
1076 _talloc_free(null_context);
1077 null_context = NULL;
1081 enable leak reporting on exit
1083 void talloc_enable_leak_report(void)
1085 talloc_enable_null_tracking();
1086 atexit(talloc_report_null);
1090 enable full leak reporting on exit
1092 void talloc_enable_leak_report_full(void)
1094 talloc_enable_null_tracking();
1095 atexit(talloc_report_null_full);
1099 talloc and zero memory.
1101 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1103 void *p = _talloc_named_const(ctx, size, name);
1106 memset(p, '\0', size);
1113 memdup with a talloc.
1115 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1117 void *newp = _talloc_named_const(t, size, name);
1120 memcpy(newp, p, size);
1127 strdup with a talloc
1129 char *talloc_strdup(const void *t, const char *p)
1135 ret = (char *)talloc_memdup(t, p, strlen(p) + 1);
1137 _talloc_set_name_const(ret, ret);
1143 append to a talloced string
1145 char *talloc_append_string(char *orig, const char *append)
1148 size_t olen = strlen(orig);
1154 alenz = strlen(append) + 1;
1156 ret = talloc_realloc(NULL, orig, char, olen + alenz);
1160 /* append the string with the trailing \0 */
1161 memcpy(&ret[olen], append, alenz);
1163 _talloc_set_name_const(ret, ret);
1169 strndup with a talloc
1171 char *talloc_strndup(const void *t, const char *p, size_t n)
1176 for (len=0; len<n && p[len]; len++) ;
1178 ret = (char *)__talloc(t, len + 1);
1179 if (!ret) { return NULL; }
1180 memcpy(ret, p, len);
1182 _talloc_set_name_const(ret, ret);
1186 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1193 /* this call looks strange, but it makes it work on older solaris boxes */
1195 len = vsnprintf(&c, 1, fmt, ap2);
1201 ret = (char *)__talloc(t, len+1);
1204 vsnprintf(ret, len+1, fmt, ap2);
1206 _talloc_set_name_const(ret, ret);
1214 Perform string formatting, and return a pointer to newly allocated
1215 memory holding the result, inside a memory pool.
1217 char *talloc_asprintf(const void *t, const char *fmt, ...)
1223 ret = talloc_vasprintf(t, fmt, ap);
1230 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1231 * and return @p s, which may have moved. Good for gradually
1232 * accumulating output into a string buffer.
1234 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1236 struct talloc_chunk *tc;
1242 return talloc_vasprintf(NULL, fmt, ap);
1245 tc = talloc_chunk_from_ptr(s);
1247 s_len = tc->size - 1;
1250 len = vsnprintf(&c, 1, fmt, ap2);
1254 /* Either the vsnprintf failed or the format resulted in
1255 * no characters being formatted. In the former case, we
1256 * ought to return NULL, in the latter we ought to return
1257 * the original string. Most current callers of this
1258 * function expect it to never return NULL.
1263 s = talloc_realloc(NULL, s, char, s_len + len+1);
1264 if (!s) return NULL;
1267 vsnprintf(s+s_len, len+1, fmt, ap2);
1269 _talloc_set_name_const(s, s);
1275 Realloc @p s to append the formatted result of @p fmt and return @p
1276 s, which may have moved. Good for gradually accumulating output
1277 into a string buffer.
1279 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1284 s = talloc_vasprintf_append(s, fmt, ap);
1290 alloc an array, checking for integer overflow in the array size
1292 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1294 if (count >= MAX_TALLOC_SIZE/el_size) {
1297 return _talloc_named_const(ctx, el_size * count, name);
1301 alloc an zero array, checking for integer overflow in the array size
1303 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1305 if (count >= MAX_TALLOC_SIZE/el_size) {
1308 return _talloc_zero(ctx, el_size * count, name);
1312 realloc an array, checking for integer overflow in the array size
1314 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1316 if (count >= MAX_TALLOC_SIZE/el_size) {
1319 return _talloc_realloc(ctx, ptr, el_size * count, name);
1323 a function version of talloc_realloc(), so it can be passed as a function pointer
1324 to libraries that want a realloc function (a realloc function encapsulates
1325 all the basic capabilities of an allocation library, which is why this is useful)
1327 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1329 return _talloc_realloc(context, ptr, size, NULL);
1333 static int talloc_autofree_destructor(void *ptr)
1335 autofree_context = NULL;
1339 static void talloc_autofree(void)
1341 if (autofree_context && *autofree_context == getpid())
1342 _talloc_free(autofree_context);
1346 return a context which will be auto-freed on exit
1347 this is useful for reducing the noise in leak reports
1349 void *talloc_autofree_context(void)
1351 if (autofree_context == NULL || *autofree_context != getpid()) {
1352 autofree_context = talloc(NULL, pid_t);
1353 *autofree_context = getpid();
1354 talloc_set_name_const(autofree_context, "autofree_context");
1356 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1357 atexit(talloc_autofree);
1359 return autofree_context;
1362 size_t talloc_get_size(const void *context)
1364 struct talloc_chunk *tc;
1366 if (context == NULL)
1369 tc = talloc_chunk_from_ptr(context);
1375 find a parent of this context that has the given name, if any
1377 void *talloc_find_parent_byname(const void *context, const char *name)
1379 struct talloc_chunk *tc;
1381 if (context == NULL) {
1385 tc = talloc_chunk_from_ptr(context);
1387 if (tc->name && strcmp(tc->name, name) == 0) {
1388 return TC_PTR_FROM_CHUNK(tc);
1390 while (tc && tc->prev) tc = tc->prev;
1399 show the parentage of a context
1401 void talloc_show_parents(const void *context, FILE *file)
1403 struct talloc_chunk *tc;
1405 if (context == NULL) {
1406 fprintf(file, "talloc no parents for NULL\n");
1410 tc = talloc_chunk_from_ptr(context);
1411 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1413 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1414 while (tc && tc->prev) tc = tc->prev;
1423 return 1 if ptr is a parent of context
1425 int talloc_is_parent(const void *context, const void *ptr)
1427 struct talloc_chunk *tc;
1429 if (context == NULL) {
1433 tc = talloc_chunk_from_ptr(context);
1435 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1436 while (tc && tc->prev) tc = tc->prev;
1444 void talloc_external_enable(void *(*alloc)(void *parent, size_t size),
1445 void (*free)(void *ptr, void *parent),
1446 void *(*realloc)(void *ptr, void *parent, size_t))
1448 tc_external_alloc = alloc;
1449 tc_external_free = free;
1450 tc_external_realloc = realloc;
1453 void talloc_mark_external(void *context)
1455 struct talloc_chunk *tc;
1457 if (unlikely(context == NULL)) {
1458 context = null_context;
1461 tc = talloc_chunk_from_ptr(context);
1462 tc->flags |= TALLOC_FLAG_EXT_ALLOC;