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 #if (SAMBA_VERSION_MAJOR<4)
38 /* This is to circumvent SAMBA3's paranoid malloc checker. Here in this file
39 * we trust ourselves... */
46 #define _TALLOC_SAMBA3
47 #endif /* (SAMBA_VERSION_MAJOR<4) */
48 #endif /* _SAMBA_BUILD_ */
50 #ifndef _TALLOC_SAMBA3
54 #endif /* not _TALLOC_SAMBA3 */
56 /* use this to force every realloc to change the pointer, to stress test
57 code that might not cope */
58 #define ALWAYS_REALLOC 0
62 static unsigned int talloc_log_suppress_count;
64 #define talloc_log(x) _talloc_log x
66 static void _talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
67 static void _talloc_log(const char *fmt, ...)
70 static int logfd = -1;
73 if (talloc_log_suppress_count) {
74 talloc_log_suppress_count--;
78 if (getpid() != pid) {
82 sprintf(fname, "/tmp/talloc.log.%i", pid);
83 logfd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0600);
85 fprintf(stderr, "Cannot open %s: %m", fname);
91 vdprintf(logfd, fmt, ap);
96 static void talloc_log_suppress(void)
98 talloc_log_suppress_count++;
101 #define talloc_log(x)
102 #define talloc_log_suppress()
105 #define MAX_TALLOC_SIZE 0x10000000
106 #define TALLOC_MAGIC 0xe814ec70
107 #define TALLOC_FLAG_FREE 0x01
108 #define TALLOC_FLAG_LOOP 0x02
109 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
111 /* by default we abort when given a bad pointer (such as when talloc_free() is called
112 on a pointer that came from malloc() */
114 #define TALLOC_ABORT(reason) abort()
117 #ifndef discard_const_p
118 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
119 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
121 # define discard_const_p(type, ptr) ((type *)(ptr))
125 /* these macros gain us a few percent of speed on gcc */
127 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
128 as its first argument */
129 #define likely(x) __builtin_expect(!!(x), 1)
130 #define unlikely(x) __builtin_expect(!!(x), 0)
133 #define unlikely(x) x
136 /* this null_context is only used if talloc_enable_leak_report() or
137 talloc_enable_leak_report_full() is called, otherwise it remains
140 static void *null_context;
141 static void *autofree_context;
143 struct talloc_reference_handle {
144 struct talloc_reference_handle *next, *prev;
148 typedef int (*talloc_destructor_t)(void *);
150 struct talloc_chunk {
151 struct talloc_chunk *next, *prev;
152 struct talloc_chunk *parent, *child;
153 struct talloc_reference_handle *refs;
154 talloc_destructor_t destructor;
160 /* 16 byte alignment seems to keep everyone happy */
161 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
162 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
164 static void talloc_abort_double_free(void)
166 TALLOC_ABORT("Bad talloc magic value - double free");
169 static void talloc_abort_unknown_value(void)
171 TALLOC_ABORT("Bad talloc magic value - unknown value");
174 /* panic if we get a bad magic value */
175 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
177 const char *pp = (const char *)ptr;
178 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
179 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
180 if (tc->flags & TALLOC_FLAG_FREE) {
181 talloc_abort_double_free();
183 talloc_abort_unknown_value();
189 /* hook into the front of the list */
190 #define _TLIST_ADD(list, p) \
194 (p)->next = (p)->prev = NULL; \
196 (list)->prev = (p); \
197 (p)->next = (list); \
203 /* remove an element from a list - element doesn't have to be in list. */
204 #define _TLIST_REMOVE(list, p) \
206 if ((p) == (list)) { \
207 (list) = (p)->next; \
208 if (list) (list)->prev = NULL; \
210 if ((p)->prev) (p)->prev->next = (p)->next; \
211 if ((p)->next) (p)->next->prev = (p)->prev; \
213 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
218 return the parent chunk of a pointer
220 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
222 struct talloc_chunk *tc;
224 if (unlikely(ptr == NULL)) {
228 tc = talloc_chunk_from_ptr(ptr);
229 while (tc->prev) tc=tc->prev;
234 void *talloc_parent(const void *ptr)
236 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
237 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
243 const char *talloc_parent_name(const void *ptr)
245 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
246 return tc? tc->name : NULL;
250 Allocate a bit of memory as a child of an existing pointer
252 static inline void *__talloc(const void *context, size_t size)
254 struct talloc_chunk *tc;
256 if (unlikely(context == NULL)) {
257 context = null_context;
260 if (unlikely(size >= MAX_TALLOC_SIZE)) {
261 talloc_log(("talloc %p %zu %p", context, size, NULL));
265 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
266 talloc_log(("talloc %p %zu %p", context, size, TC_PTR_FROM_CHUNK(tc)));
267 if (unlikely(tc == NULL)) return NULL;
270 tc->flags = TALLOC_MAGIC;
271 tc->destructor = NULL;
276 if (likely(context)) {
277 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
280 parent->child->parent = NULL;
281 tc->next = parent->child;
290 tc->next = tc->prev = tc->parent = NULL;
293 return TC_PTR_FROM_CHUNK(tc);
297 setup a destructor to be called on free of a pointer
298 the destructor should return 0 on success, or -1 on failure.
299 if the destructor fails then the free is failed, and the memory can
300 be continued to be used
302 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
304 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
305 talloc_log(("set_destructor %p %p", ptr, destructor));
306 tc->destructor = destructor;
310 increase the reference count on a piece of memory.
312 int talloc_increase_ref_count(const void *ptr)
314 talloc_log(("increase_ref_count %p", ptr));
315 if (unlikely(!talloc_reference(null_context, ptr))) {
322 helper for talloc_reference()
324 this is referenced by a function pointer and should not be inline
326 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
328 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
329 _TLIST_REMOVE(ptr_tc->refs, handle);
334 more efficient way to add a name to a pointer - the name must point to a
337 static inline void _talloc_set_name_const(const void *ptr, const char *name)
339 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
344 internal talloc_named_const()
346 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
350 talloc_log_suppress();
351 ptr = __talloc(context, size);
352 if (unlikely(ptr == NULL)) {
356 _talloc_set_name_const(ptr, name);
362 make a secondary reference to a pointer, hanging off the given context.
363 the pointer remains valid until both the original caller and this given
366 the major use for this is when two different structures need to reference the
367 same underlying data, and you want to be able to free the two instances separately,
370 void *_talloc_reference(const void *context, const void *ptr)
372 struct talloc_chunk *tc;
373 struct talloc_reference_handle *handle;
374 talloc_log(("reference %p %p", context, ptr));
375 if (unlikely(ptr == NULL)) return NULL;
377 tc = talloc_chunk_from_ptr(ptr);
378 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
379 sizeof(struct talloc_reference_handle),
380 TALLOC_MAGIC_REFERENCE);
381 if (unlikely(handle == NULL)) return NULL;
383 /* note that we hang the destructor off the handle, not the
384 main context as that allows the caller to still setup their
385 own destructor on the context if they want to */
386 talloc_log_suppress();
387 talloc_set_destructor(handle, talloc_reference_destructor);
388 handle->ptr = discard_const_p(void, ptr);
389 _TLIST_ADD(tc->refs, handle);
395 internal talloc_free call
397 static inline int _talloc_free(void *ptr)
399 struct talloc_chunk *tc;
401 if (unlikely(ptr == NULL)) {
405 tc = talloc_chunk_from_ptr(ptr);
407 if (unlikely(tc->refs)) {
409 /* check this is a reference from a child or grantchild
410 * back to it's parent or grantparent
412 * in that case we need to remove the reference and
413 * call another instance of talloc_free() on the current
416 is_child = talloc_is_parent(tc->refs, ptr);
417 _talloc_free(tc->refs);
419 return _talloc_free(ptr);
424 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
425 /* we have a free loop - stop looping */
429 if (unlikely(tc->destructor)) {
430 talloc_destructor_t d = tc->destructor;
431 if (d == (talloc_destructor_t)-1) {
434 tc->destructor = (talloc_destructor_t)-1;
439 tc->destructor = NULL;
443 _TLIST_REMOVE(tc->parent->child, tc);
444 if (tc->parent->child) {
445 tc->parent->child->parent = tc->parent;
448 if (tc->prev) tc->prev->next = tc->next;
449 if (tc->next) tc->next->prev = tc->prev;
452 tc->flags |= TALLOC_FLAG_LOOP;
455 /* we need to work out who will own an abandoned child
456 if it cannot be freed. In priority order, the first
457 choice is owner of any remaining reference to this
458 pointer, the second choice is our parent, and the
459 final choice is the null context. */
460 void *child = TC_PTR_FROM_CHUNK(tc->child);
461 const void *new_parent = null_context;
462 if (unlikely(tc->child->refs)) {
463 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
464 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
466 if (unlikely(_talloc_free(child) == -1)) {
467 if (new_parent == null_context) {
468 struct talloc_chunk *p = talloc_parent_chunk(ptr);
469 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
471 talloc_steal(new_parent, child);
473 talloc_log(("free %p", child));
476 tc->flags |= TALLOC_FLAG_FREE;
482 move a lump of memory from one talloc context to another return the
483 ptr on success, or NULL if it could not be transferred.
484 passing NULL as ptr will always return NULL with no side effects.
486 void *_talloc_steal(const void *new_ctx, const void *ptr)
488 struct talloc_chunk *tc, *new_tc;
490 talloc_log(("steal %p %p", new_ctx, ptr));
492 if (unlikely(!ptr)) {
496 if (unlikely(new_ctx == NULL)) {
497 new_ctx = null_context;
500 tc = talloc_chunk_from_ptr(ptr);
502 if (unlikely(new_ctx == NULL)) {
504 _TLIST_REMOVE(tc->parent->child, tc);
505 if (tc->parent->child) {
506 tc->parent->child->parent = tc->parent;
509 if (tc->prev) tc->prev->next = tc->next;
510 if (tc->next) tc->next->prev = tc->prev;
513 tc->parent = tc->next = tc->prev = NULL;
514 return discard_const_p(void, ptr);
517 new_tc = talloc_chunk_from_ptr(new_ctx);
519 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
520 return discard_const_p(void, ptr);
524 _TLIST_REMOVE(tc->parent->child, tc);
525 if (tc->parent->child) {
526 tc->parent->child->parent = tc->parent;
529 if (tc->prev) tc->prev->next = tc->next;
530 if (tc->next) tc->next->prev = tc->prev;
534 if (new_tc->child) new_tc->child->parent = NULL;
535 _TLIST_ADD(new_tc->child, tc);
537 return discard_const_p(void, ptr);
543 remove a secondary reference to a pointer. This undo's what
544 talloc_reference() has done. The context and pointer arguments
545 must match those given to a talloc_reference()
547 static inline int talloc_unreference(const void *context, const void *ptr)
549 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
550 struct talloc_reference_handle *h;
552 if (unlikely(context == NULL)) {
553 context = null_context;
556 for (h=tc->refs;h;h=h->next) {
557 struct talloc_chunk *p = talloc_parent_chunk(h);
559 if (context == NULL) break;
560 } else if (TC_PTR_FROM_CHUNK(p) == context) {
568 return _talloc_free(h);
572 remove a specific parent context from a pointer. This is a more
573 controlled varient of talloc_free()
575 int talloc_unlink(const void *context, void *ptr)
577 struct talloc_chunk *tc_p, *new_p;
580 talloc_log(("unlink %p %p", context, ptr));
586 if (context == NULL) {
587 context = null_context;
590 if (talloc_unreference(context, ptr) == 0) {
594 if (context == NULL) {
595 if (talloc_parent_chunk(ptr) != NULL) {
599 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
604 tc_p = talloc_chunk_from_ptr(ptr);
606 if (tc_p->refs == NULL) {
607 return _talloc_free(ptr);
610 new_p = talloc_parent_chunk(tc_p->refs);
612 new_parent = TC_PTR_FROM_CHUNK(new_p);
617 if (talloc_unreference(new_parent, ptr) != 0) {
621 talloc_steal(new_parent, ptr);
627 add a name to an existing pointer - va_list version
629 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
631 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
633 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
634 talloc_log_suppress();
635 tc->name = talloc_vasprintf(ptr, fmt, ap);
636 if (likely(tc->name)) {
637 _talloc_set_name_const(tc->name, ".name");
639 talloc_log(("set_name %p %p %s", ptr, tc->name));
644 add a name to an existing pointer
646 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
651 name = talloc_set_name_v(ptr, fmt, ap);
658 create a named talloc pointer. Any talloc pointer can be named, and
659 talloc_named() operates just like talloc() except that it allows you
662 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
668 ptr = __talloc(context, size);
669 if (unlikely(ptr == NULL)) return NULL;
672 name = talloc_set_name_v(ptr, fmt, ap);
675 if (unlikely(name == NULL)) {
684 return the name of a talloc ptr, or "UNNAMED"
686 const char *talloc_get_name(const void *ptr)
688 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
689 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
692 if (likely(tc->name)) {
700 check if a pointer has the given name. If it does, return the pointer,
701 otherwise return NULL
703 void *talloc_check_name(const void *ptr, const char *name)
706 if (unlikely(ptr == NULL)) return NULL;
707 pname = talloc_get_name(ptr);
708 if (likely(pname == name || strcmp(pname, name) == 0)) {
709 return discard_const_p(void, ptr);
716 this is for compatibility with older versions of talloc
718 void *talloc_init(const char *fmt, ...)
725 * samba3 expects talloc_report_depth_cb(NULL, ...)
726 * reports all talloc'ed memory, so we need to enable
729 talloc_enable_null_tracking();
731 ptr = __talloc(NULL, 0);
732 if (unlikely(ptr == NULL)) return NULL;
735 name = talloc_set_name_v(ptr, fmt, ap);
738 if (unlikely(name == NULL)) {
747 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
748 should probably not be used in new code. It's in here to keep the talloc
749 code consistent across Samba 3 and 4.
751 void talloc_free_children(void *ptr)
753 struct talloc_chunk *tc;
755 talloc_log(("free_children %p", ptr));
756 if (unlikely(ptr == NULL)) {
760 tc = talloc_chunk_from_ptr(ptr);
763 /* we need to work out who will own an abandoned child
764 if it cannot be freed. In priority order, the first
765 choice is owner of any remaining reference to this
766 pointer, the second choice is our parent, and the
767 final choice is the null context. */
768 void *child = TC_PTR_FROM_CHUNK(tc->child);
769 const void *new_parent = null_context;
770 if (unlikely(tc->child->refs)) {
771 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
772 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
774 if (unlikely(_talloc_free(child) == -1)) {
775 if (new_parent == null_context) {
776 struct talloc_chunk *p = talloc_parent_chunk(ptr);
777 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
779 talloc_steal(new_parent, child);
785 Allocate a bit of memory as a child of an existing pointer
787 void *_talloc(const void *context, size_t size)
789 return __talloc(context, size);
793 externally callable talloc_set_name_const()
795 void talloc_set_name_const(const void *ptr, const char *name)
797 talloc_log(("set_name_const %p %p %s", ptr, name, name));
798 _talloc_set_name_const(ptr, name);
802 create a named talloc pointer. Any talloc pointer can be named, and
803 talloc_named() operates just like talloc() except that it allows you
806 void *talloc_named_const(const void *context, size_t size, const char *name)
808 void *p = _talloc_named_const(context, size, name);
809 talloc_log(("talloc %p %zu %p", context, size, p));
810 talloc_log(("set_name_const %p %p %s", p, name, name));
815 free a talloc pointer. This also frees all child pointers of this
818 return 0 if the memory is actually freed, otherwise -1. The memory
819 will not be freed if the ref_count is > 1 or the destructor (if
820 any) returns non-zero
822 int talloc_free(void *ptr)
824 talloc_log(("free %p", ptr));
825 return _talloc_free(ptr);
831 A talloc version of realloc. The context argument is only used if
834 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
836 struct talloc_chunk *tc;
839 /* size zero is equivalent to free() */
840 if (unlikely(size == 0)) {
841 talloc_log(("free %p", ptr));
846 if (unlikely(size >= MAX_TALLOC_SIZE)) {
847 talloc_log(("realloc %p %p %zu %p", context, ptr, size, NULL));
851 /* realloc(NULL) is equivalent to malloc() */
853 return talloc_named_const(context, size, name);
856 tc = talloc_chunk_from_ptr(ptr);
858 /* don't allow realloc on referenced pointers */
859 if (unlikely(tc->refs)) {
860 talloc_log(("realloc %p %p %zu %p", context, ptr, size, NULL));
864 /* by resetting magic we catch users of the old memory */
865 tc->flags |= TALLOC_FLAG_FREE;
868 new_ptr = malloc(size + TC_HDR_SIZE);
870 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
874 new_ptr = realloc(tc, size + TC_HDR_SIZE);
876 if (unlikely(!new_ptr)) {
877 tc->flags &= ~TALLOC_FLAG_FREE;
878 talloc_log(("realloc %p %p %zu %p", context, ptr, size, NULL));
882 tc = (struct talloc_chunk *)new_ptr;
883 tc->flags &= ~TALLOC_FLAG_FREE;
885 tc->parent->child = tc;
888 tc->child->parent = tc;
899 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
901 talloc_log(("realloc %p %p %zu %p", context, ptr, size, TC_PTR_FROM_CHUNK(tc)));
902 return TC_PTR_FROM_CHUNK(tc);
906 a wrapper around talloc_steal() for situations where you are moving a pointer
907 between two structures, and want the old pointer to be set to NULL
909 void *_talloc_move(const void *new_ctx, const void *_pptr)
911 const void **pptr = discard_const_p(const void *,_pptr);
912 void *ret = _talloc_steal(new_ctx, *pptr);
918 return the total size of a talloc pool (subtree)
920 size_t talloc_total_size(const void *ptr)
923 struct talloc_chunk *c, *tc;
932 tc = talloc_chunk_from_ptr(ptr);
934 if (tc->flags & TALLOC_FLAG_LOOP) {
938 tc->flags |= TALLOC_FLAG_LOOP;
941 for (c=tc->child;c;c=c->next) {
942 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
945 tc->flags &= ~TALLOC_FLAG_LOOP;
951 return the total number of blocks in a talloc pool (subtree)
953 size_t talloc_total_blocks(const void *ptr)
956 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
958 if (tc->flags & TALLOC_FLAG_LOOP) {
962 tc->flags |= TALLOC_FLAG_LOOP;
965 for (c=tc->child;c;c=c->next) {
966 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
969 tc->flags &= ~TALLOC_FLAG_LOOP;
975 return the number of external references to a pointer
977 size_t talloc_reference_count(const void *ptr)
979 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
980 struct talloc_reference_handle *h;
983 for (h=tc->refs;h;h=h->next) {
990 report on memory usage by all children of a pointer, giving a full tree view
992 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
993 void (*callback)(const void *ptr,
994 int depth, int max_depth,
999 struct talloc_chunk *c, *tc;
1004 if (ptr == NULL) return;
1006 tc = talloc_chunk_from_ptr(ptr);
1008 if (tc->flags & TALLOC_FLAG_LOOP) {
1012 callback(ptr, depth, max_depth, 0, private_data);
1014 if (max_depth >= 0 && depth >= max_depth) {
1018 tc->flags |= TALLOC_FLAG_LOOP;
1019 for (c=tc->child;c;c=c->next) {
1020 if (c->name == TALLOC_MAGIC_REFERENCE) {
1021 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1022 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1024 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1027 tc->flags &= ~TALLOC_FLAG_LOOP;
1030 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1032 const char *name = talloc_get_name(ptr);
1033 FILE *f = (FILE *)_f;
1036 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1041 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1042 (max_depth < 0 ? "full " :""), name,
1043 (unsigned long)talloc_total_size(ptr),
1044 (unsigned long)talloc_total_blocks(ptr));
1048 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1051 (unsigned long)talloc_total_size(ptr),
1052 (unsigned long)talloc_total_blocks(ptr),
1053 (int)talloc_reference_count(ptr), ptr);
1056 fprintf(f, "content: ");
1057 if (talloc_total_size(ptr)) {
1058 int tot = talloc_total_size(ptr);
1061 for (i = 0; i < tot; i++) {
1062 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1063 fprintf(f, "%c", ((char *)ptr)[i]);
1065 fprintf(f, "~%02x", ((char *)ptr)[i]);
1074 report on memory usage by all children of a pointer, giving a full tree view
1076 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1078 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1083 report on memory usage by all children of a pointer, giving a full tree view
1085 void talloc_report_full(const void *ptr, FILE *f)
1087 talloc_report_depth_file(ptr, 0, -1, f);
1091 report on memory usage by all children of a pointer
1093 void talloc_report(const void *ptr, FILE *f)
1095 talloc_report_depth_file(ptr, 0, 1, f);
1099 report on any memory hanging off the null context
1101 static void talloc_report_null(void)
1103 if (talloc_total_size(null_context) != 0) {
1104 talloc_report(null_context, stderr);
1109 report on any memory hanging off the null context
1111 static void talloc_report_null_full(void)
1113 if (talloc_total_size(null_context) != 0) {
1114 talloc_report_full(null_context, stderr);
1119 enable tracking of the NULL context
1121 void talloc_enable_null_tracking(void)
1123 if (null_context == NULL) {
1124 null_context = _talloc_named_const(NULL, 0, "null_context");
1129 disable tracking of the NULL context
1131 void talloc_disable_null_tracking(void)
1133 _talloc_free(null_context);
1134 null_context = NULL;
1138 enable leak reporting on exit
1140 void talloc_enable_leak_report(void)
1142 talloc_enable_null_tracking();
1143 atexit(talloc_report_null);
1147 enable full leak reporting on exit
1149 void talloc_enable_leak_report_full(void)
1151 talloc_enable_null_tracking();
1152 atexit(talloc_report_null_full);
1156 talloc and zero memory.
1158 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1160 void *p = _talloc_named_const(ctx, size, name);
1162 talloc_log(("talloc %p %zu %p", ctx, size, p));
1163 talloc_log(("set_name_const %p %p %s", p, name, name));
1165 memset(p, '\0', size);
1172 memdup with a talloc.
1174 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1176 void *newp = _talloc_named_const(t, size, name);
1178 talloc_log(("talloc %p %zu %p", t, size, p));
1179 talloc_log(("set_name_const %p %p %s", p, name, name));
1181 memcpy(newp, p, size);
1187 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1191 ret = (char *)__talloc(t, len + 1);
1192 if (unlikely(!ret)) return NULL;
1194 memcpy(ret, p, len);
1197 talloc_log(("set_name_const %p %p %s", ret, ret, ret));
1198 _talloc_set_name_const(ret, ret);
1203 strdup with a talloc
1205 char *talloc_strdup(const void *t, const char *p)
1207 if (unlikely(!p)) return NULL;
1208 return __talloc_strlendup(t, p, strlen(p));
1212 strndup with a talloc
1214 char *talloc_strndup(const void *t, const char *p, size_t n)
1216 if (unlikely(!p)) return NULL;
1217 return __talloc_strlendup(t, p, strnlen(p, n));
1220 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1221 const char *a, size_t alen)
1225 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1226 if (unlikely(!ret)) return NULL;
1228 /* append the string and the trailing \0 */
1229 memcpy(&ret[slen], a, alen);
1232 talloc_log(("set_name_const %p %p %s", ret, ret, ret));
1233 _talloc_set_name_const(ret, ret);
1238 * Appends at the end of the string.
1240 char *talloc_strdup_append(char *s, const char *a)
1243 return talloc_strdup(NULL, a);
1250 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1254 * Appends at the end of the talloc'ed buffer,
1255 * not the end of the string.
1257 char *talloc_strdup_append_buffer(char *s, const char *a)
1262 return talloc_strdup(NULL, a);
1269 slen = talloc_get_size(s);
1270 if (likely(slen > 0)) {
1274 return __talloc_strlendup_append(s, slen, a, strlen(a));
1278 * Appends at the end of the string.
1280 char *talloc_strndup_append(char *s, const char *a, size_t n)
1283 return talloc_strdup(NULL, a);
1290 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1294 * Appends at the end of the talloc'ed buffer,
1295 * not the end of the string.
1297 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1302 return talloc_strdup(NULL, a);
1309 slen = talloc_get_size(s);
1310 if (likely(slen > 0)) {
1314 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1317 #ifndef HAVE_VA_COPY
1318 #ifdef HAVE___VA_COPY
1319 #define va_copy(dest, src) __va_copy(dest, src)
1321 #define va_copy(dest, src) (dest) = (src)
1325 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1332 /* this call looks strange, but it makes it work on older solaris boxes */
1334 len = vsnprintf(&c, 1, fmt, ap2);
1336 if (unlikely(len < 0)) {
1340 ret = (char *)__talloc(t, len+1);
1341 if (unlikely(!ret)) return NULL;
1344 vsnprintf(ret, len+1, fmt, ap2);
1347 talloc_log(("set_name_const %p %p %s", ret, ret, ret));
1348 _talloc_set_name_const(ret, ret);
1354 Perform string formatting, and return a pointer to newly allocated
1355 memory holding the result, inside a memory pool.
1357 char *talloc_asprintf(const void *t, const char *fmt, ...)
1363 ret = talloc_vasprintf(t, fmt, ap);
1368 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1369 const char *fmt, va_list ap)
1370 PRINTF_ATTRIBUTE(3,0);
1372 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1373 const char *fmt, va_list ap)
1380 alen = vsnprintf(&c, 1, fmt, ap2);
1384 /* Either the vsnprintf failed or the format resulted in
1385 * no characters being formatted. In the former case, we
1386 * ought to return NULL, in the latter we ought to return
1387 * the original string. Most current callers of this
1388 * function expect it to never return NULL.
1393 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1394 if (!s) return NULL;
1397 vsnprintf(s + slen, alen + 1, fmt, ap2);
1400 talloc_log(("set_name_const %p %p %s", s, s, s));
1401 _talloc_set_name_const(s, s);
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. Appends at the end
1411 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1414 return talloc_vasprintf(NULL, fmt, ap);
1417 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1421 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1422 * and return @p s, which may have moved. Always appends at the
1423 * end of the talloc'ed buffer, not the end of the string.
1425 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1430 return talloc_vasprintf(NULL, fmt, ap);
1433 slen = talloc_get_size(s);
1434 if (likely(slen > 0)) {
1438 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1442 Realloc @p s to append the formatted result of @p fmt and return @p
1443 s, which may have moved. Good for gradually accumulating output
1444 into a string buffer.
1446 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1451 s = talloc_vasprintf_append(s, fmt, ap);
1457 Realloc @p s to append the formatted result of @p fmt and return @p
1458 s, which may have moved. Good for gradually accumulating output
1461 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1466 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1472 alloc an array, checking for integer overflow in the array size
1474 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1477 if (count >= MAX_TALLOC_SIZE/el_size) {
1480 p = _talloc_named_const(ctx, el_size * count, name);
1481 talloc_log(("talloc %p %zu %p", ctx, el_size * count, p));
1482 talloc_log(("set_name_const %p %p %s", p, name, name));
1487 alloc an zero array, checking for integer overflow in the array size
1489 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1491 if (count >= MAX_TALLOC_SIZE/el_size) {
1494 return _talloc_zero(ctx, el_size * count, name);
1498 realloc an array, checking for integer overflow in the array size
1500 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1502 if (count >= MAX_TALLOC_SIZE/el_size) {
1505 return _talloc_realloc(ctx, ptr, el_size * count, name);
1509 a function version of talloc_realloc(), so it can be passed as a function pointer
1510 to libraries that want a realloc function (a realloc function encapsulates
1511 all the basic capabilities of an allocation library, which is why this is useful)
1513 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1515 return _talloc_realloc(context, ptr, size, NULL);
1519 static int talloc_autofree_destructor(void *ptr)
1521 autofree_context = NULL;
1525 static void talloc_autofree(void)
1527 _talloc_free(autofree_context);
1531 return a context which will be auto-freed on exit
1532 this is useful for reducing the noise in leak reports
1534 void *talloc_autofree_context(void)
1536 if (autofree_context == NULL) {
1537 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1538 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1539 atexit(talloc_autofree);
1541 return autofree_context;
1544 size_t talloc_get_size(const void *context)
1546 struct talloc_chunk *tc;
1548 if (context == NULL)
1551 tc = talloc_chunk_from_ptr(context);
1557 find a parent of this context that has the given name, if any
1559 void *talloc_find_parent_byname(const void *context, const char *name)
1561 struct talloc_chunk *tc;
1563 if (context == NULL) {
1567 tc = talloc_chunk_from_ptr(context);
1569 if (tc->name && strcmp(tc->name, name) == 0) {
1570 return TC_PTR_FROM_CHUNK(tc);
1572 while (tc && tc->prev) tc = tc->prev;
1581 show the parentage of a context
1583 void talloc_show_parents(const void *context, FILE *file)
1585 struct talloc_chunk *tc;
1587 if (context == NULL) {
1588 fprintf(file, "talloc no parents for NULL\n");
1592 tc = talloc_chunk_from_ptr(context);
1593 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1595 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1596 while (tc && tc->prev) tc = tc->prev;
1605 return 1 if ptr is a parent of context
1607 int talloc_is_parent(const void *context, const void *ptr)
1609 struct talloc_chunk *tc;
1611 if (context == NULL) {
1615 tc = talloc_chunk_from_ptr(context);
1617 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1618 while (tc && tc->prev) tc = tc->prev;