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/
35 #if (SAMBA_VERSION_MAJOR<4)
37 /* This is to circumvent SAMBA3's paranoid malloc checker. Here in this file
38 * we trust ourselves... */
45 #define _TALLOC_SAMBA3
46 #endif /* (SAMBA_VERSION_MAJOR<4) */
47 #endif /* _SAMBA_BUILD_ */
49 #ifndef _TALLOC_SAMBA3
52 #endif /* not _TALLOC_SAMBA3 */
54 /* use this to force every realloc to change the pointer, to stress test
55 code that might not cope */
56 #define ALWAYS_REALLOC 0
59 #define MAX_TALLOC_SIZE 0x10000000
60 #define TALLOC_MAGIC 0xe814ec70
61 #define TALLOC_FLAG_FREE 0x01
62 #define TALLOC_FLAG_LOOP 0x02
63 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
65 /* by default we abort when given a bad pointer (such as when talloc_free() is called
66 on a pointer that came from malloc() */
68 #define TALLOC_ABORT(reason) abort()
71 #ifndef discard_const_p
72 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
73 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
75 # define discard_const_p(type, ptr) ((type *)(ptr))
79 /* these macros gain us a few percent of speed on gcc */
81 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
82 as its first argument */
83 #define likely(x) __builtin_expect(!!(x), 1)
84 #define unlikely(x) __builtin_expect(!!(x), 0)
90 /* this null_context is only used if talloc_enable_leak_report() or
91 talloc_enable_leak_report_full() is called, otherwise it remains
94 static void *null_context;
95 static void *autofree_context;
97 struct talloc_reference_handle {
98 struct talloc_reference_handle *next, *prev;
102 typedef int (*talloc_destructor_t)(void *);
104 struct talloc_chunk {
105 struct talloc_chunk *next, *prev;
106 struct talloc_chunk *parent, *child;
107 struct talloc_reference_handle *refs;
108 talloc_destructor_t destructor;
114 /* 16 byte alignment seems to keep everyone happy */
115 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
116 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
118 static void talloc_abort_double_free(void)
120 TALLOC_ABORT("Bad talloc magic value - double free");
123 static void talloc_abort_unknown_value(void)
125 TALLOC_ABORT("Bad talloc magic value - unknown value");
128 /* panic if we get a bad magic value */
129 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
131 const char *pp = (const char *)ptr;
132 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
133 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
134 if (tc->flags & TALLOC_FLAG_FREE) {
135 talloc_abort_double_free();
137 talloc_abort_unknown_value();
143 /* hook into the front of the list */
144 #define _TLIST_ADD(list, p) \
148 (p)->next = (p)->prev = NULL; \
150 (list)->prev = (p); \
151 (p)->next = (list); \
157 /* remove an element from a list - element doesn't have to be in list. */
158 #define _TLIST_REMOVE(list, p) \
160 if ((p) == (list)) { \
161 (list) = (p)->next; \
162 if (list) (list)->prev = NULL; \
164 if ((p)->prev) (p)->prev->next = (p)->next; \
165 if ((p)->next) (p)->next->prev = (p)->prev; \
167 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
172 return the parent chunk of a pointer
174 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
176 struct talloc_chunk *tc;
178 if (unlikely(ptr == NULL)) {
182 tc = talloc_chunk_from_ptr(ptr);
183 while (tc->prev) tc=tc->prev;
188 void *talloc_parent(const void *ptr)
190 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
191 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
197 const char *talloc_parent_name(const void *ptr)
199 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
200 return tc? tc->name : NULL;
204 Allocate a bit of memory as a child of an existing pointer
206 static inline void *__talloc(const void *context, size_t size)
208 struct talloc_chunk *tc;
210 if (unlikely(context == NULL)) {
211 context = null_context;
214 if (unlikely(size >= MAX_TALLOC_SIZE)) {
218 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
219 if (unlikely(tc == NULL)) return NULL;
222 tc->flags = TALLOC_MAGIC;
223 tc->destructor = NULL;
228 if (likely(context)) {
229 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
232 parent->child->parent = NULL;
233 tc->next = parent->child;
242 tc->next = tc->prev = tc->parent = NULL;
245 return TC_PTR_FROM_CHUNK(tc);
249 setup a destructor to be called on free of a pointer
250 the destructor should return 0 on success, or -1 on failure.
251 if the destructor fails then the free is failed, and the memory can
252 be continued to be used
254 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
256 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
257 tc->destructor = destructor;
261 increase the reference count on a piece of memory.
263 int talloc_increase_ref_count(const void *ptr)
265 if (unlikely(!talloc_reference(null_context, ptr))) {
272 helper for talloc_reference()
274 this is referenced by a function pointer and should not be inline
276 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
278 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
279 _TLIST_REMOVE(ptr_tc->refs, handle);
284 more efficient way to add a name to a pointer - the name must point to a
287 static inline void _talloc_set_name_const(const void *ptr, const char *name)
289 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
294 internal talloc_named_const()
296 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
300 ptr = __talloc(context, size);
301 if (unlikely(ptr == NULL)) {
305 _talloc_set_name_const(ptr, name);
311 make a secondary reference to a pointer, hanging off the given context.
312 the pointer remains valid until both the original caller and this given
315 the major use for this is when two different structures need to reference the
316 same underlying data, and you want to be able to free the two instances separately,
319 void *_talloc_reference(const void *context, const void *ptr)
321 struct talloc_chunk *tc;
322 struct talloc_reference_handle *handle;
323 if (unlikely(ptr == NULL)) return NULL;
325 tc = talloc_chunk_from_ptr(ptr);
326 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
327 sizeof(struct talloc_reference_handle),
328 TALLOC_MAGIC_REFERENCE);
329 if (unlikely(handle == NULL)) return NULL;
331 /* note that we hang the destructor off the handle, not the
332 main context as that allows the caller to still setup their
333 own destructor on the context if they want to */
334 talloc_set_destructor(handle, talloc_reference_destructor);
335 handle->ptr = discard_const_p(void, ptr);
336 _TLIST_ADD(tc->refs, handle);
342 internal talloc_free call
344 static inline int _talloc_free(void *ptr)
346 struct talloc_chunk *tc;
348 if (unlikely(ptr == NULL)) {
352 tc = talloc_chunk_from_ptr(ptr);
354 if (unlikely(tc->refs)) {
356 /* check this is a reference from a child or grantchild
357 * back to it's parent or grantparent
359 * in that case we need to remove the reference and
360 * call another instance of talloc_free() on the current
363 is_child = talloc_is_parent(tc->refs, ptr);
364 _talloc_free(tc->refs);
366 return _talloc_free(ptr);
371 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
372 /* we have a free loop - stop looping */
376 if (unlikely(tc->destructor)) {
377 talloc_destructor_t d = tc->destructor;
378 if (d == (talloc_destructor_t)-1) {
381 tc->destructor = (talloc_destructor_t)-1;
386 tc->destructor = NULL;
390 _TLIST_REMOVE(tc->parent->child, tc);
391 if (tc->parent->child) {
392 tc->parent->child->parent = tc->parent;
395 if (tc->prev) tc->prev->next = tc->next;
396 if (tc->next) tc->next->prev = tc->prev;
399 tc->flags |= TALLOC_FLAG_LOOP;
402 /* we need to work out who will own an abandoned child
403 if it cannot be freed. In priority order, the first
404 choice is owner of any remaining reference to this
405 pointer, the second choice is our parent, and the
406 final choice is the null context. */
407 void *child = TC_PTR_FROM_CHUNK(tc->child);
408 const void *new_parent = null_context;
409 if (unlikely(tc->child->refs)) {
410 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
411 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
413 if (unlikely(_talloc_free(child) == -1)) {
414 if (new_parent == null_context) {
415 struct talloc_chunk *p = talloc_parent_chunk(ptr);
416 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
418 talloc_steal(new_parent, child);
422 tc->flags |= TALLOC_FLAG_FREE;
428 move a lump of memory from one talloc context to another return the
429 ptr on success, or NULL if it could not be transferred.
430 passing NULL as ptr will always return NULL with no side effects.
432 void *_talloc_steal(const void *new_ctx, const void *ptr)
434 struct talloc_chunk *tc, *new_tc;
436 if (unlikely(!ptr)) {
440 if (unlikely(new_ctx == NULL)) {
441 new_ctx = null_context;
444 tc = talloc_chunk_from_ptr(ptr);
446 if (unlikely(new_ctx == NULL)) {
448 _TLIST_REMOVE(tc->parent->child, tc);
449 if (tc->parent->child) {
450 tc->parent->child->parent = tc->parent;
453 if (tc->prev) tc->prev->next = tc->next;
454 if (tc->next) tc->next->prev = tc->prev;
457 tc->parent = tc->next = tc->prev = NULL;
458 return discard_const_p(void, ptr);
461 new_tc = talloc_chunk_from_ptr(new_ctx);
463 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
464 return discard_const_p(void, ptr);
468 _TLIST_REMOVE(tc->parent->child, tc);
469 if (tc->parent->child) {
470 tc->parent->child->parent = tc->parent;
473 if (tc->prev) tc->prev->next = tc->next;
474 if (tc->next) tc->next->prev = tc->prev;
478 if (new_tc->child) new_tc->child->parent = NULL;
479 _TLIST_ADD(new_tc->child, tc);
481 return discard_const_p(void, ptr);
487 remove a secondary reference to a pointer. This undo's what
488 talloc_reference() has done. The context and pointer arguments
489 must match those given to a talloc_reference()
491 static inline int talloc_unreference(const void *context, const void *ptr)
493 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
494 struct talloc_reference_handle *h;
496 if (unlikely(context == NULL)) {
497 context = null_context;
500 for (h=tc->refs;h;h=h->next) {
501 struct talloc_chunk *p = talloc_parent_chunk(h);
503 if (context == NULL) break;
504 } else if (TC_PTR_FROM_CHUNK(p) == context) {
512 return _talloc_free(h);
516 remove a specific parent context from a pointer. This is a more
517 controlled varient of talloc_free()
519 int talloc_unlink(const void *context, void *ptr)
521 struct talloc_chunk *tc_p, *new_p;
528 if (context == NULL) {
529 context = null_context;
532 if (talloc_unreference(context, ptr) == 0) {
536 if (context == NULL) {
537 if (talloc_parent_chunk(ptr) != NULL) {
541 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
546 tc_p = talloc_chunk_from_ptr(ptr);
548 if (tc_p->refs == NULL) {
549 return _talloc_free(ptr);
552 new_p = talloc_parent_chunk(tc_p->refs);
554 new_parent = TC_PTR_FROM_CHUNK(new_p);
559 if (talloc_unreference(new_parent, ptr) != 0) {
563 talloc_steal(new_parent, ptr);
569 add a name to an existing pointer - va_list version
571 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
573 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
575 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
576 tc->name = talloc_vasprintf(ptr, fmt, ap);
577 if (likely(tc->name)) {
578 _talloc_set_name_const(tc->name, ".name");
584 add a name to an existing pointer
586 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
591 name = talloc_set_name_v(ptr, fmt, ap);
598 create a named talloc pointer. Any talloc pointer can be named, and
599 talloc_named() operates just like talloc() except that it allows you
602 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
608 ptr = __talloc(context, size);
609 if (unlikely(ptr == NULL)) return NULL;
612 name = talloc_set_name_v(ptr, fmt, ap);
615 if (unlikely(name == NULL)) {
624 return the name of a talloc ptr, or "UNNAMED"
626 const char *talloc_get_name(const void *ptr)
628 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
629 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
632 if (likely(tc->name)) {
640 check if a pointer has the given name. If it does, return the pointer,
641 otherwise return NULL
643 void *talloc_check_name(const void *ptr, const char *name)
646 if (unlikely(ptr == NULL)) return NULL;
647 pname = talloc_get_name(ptr);
648 if (likely(pname == name || strcmp(pname, name) == 0)) {
649 return discard_const_p(void, ptr);
656 this is for compatibility with older versions of talloc
658 void *talloc_init(const char *fmt, ...)
665 * samba3 expects talloc_report_depth_cb(NULL, ...)
666 * reports all talloc'ed memory, so we need to enable
669 talloc_enable_null_tracking();
671 ptr = __talloc(NULL, 0);
672 if (unlikely(ptr == NULL)) return NULL;
675 name = talloc_set_name_v(ptr, fmt, ap);
678 if (unlikely(name == NULL)) {
687 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
688 should probably not be used in new code. It's in here to keep the talloc
689 code consistent across Samba 3 and 4.
691 void talloc_free_children(void *ptr)
693 struct talloc_chunk *tc;
695 if (unlikely(ptr == NULL)) {
699 tc = talloc_chunk_from_ptr(ptr);
702 /* we need to work out who will own an abandoned child
703 if it cannot be freed. In priority order, the first
704 choice is owner of any remaining reference to this
705 pointer, the second choice is our parent, and the
706 final choice is the null context. */
707 void *child = TC_PTR_FROM_CHUNK(tc->child);
708 const void *new_parent = null_context;
709 if (unlikely(tc->child->refs)) {
710 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
711 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
713 if (unlikely(_talloc_free(child) == -1)) {
714 if (new_parent == null_context) {
715 struct talloc_chunk *p = talloc_parent_chunk(ptr);
716 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
718 talloc_steal(new_parent, child);
724 Allocate a bit of memory as a child of an existing pointer
726 void *_talloc(const void *context, size_t size)
728 return __talloc(context, size);
732 externally callable talloc_set_name_const()
734 void talloc_set_name_const(const void *ptr, const char *name)
736 _talloc_set_name_const(ptr, name);
740 create a named talloc pointer. Any talloc pointer can be named, and
741 talloc_named() operates just like talloc() except that it allows you
744 void *talloc_named_const(const void *context, size_t size, const char *name)
746 return _talloc_named_const(context, size, name);
750 free a talloc pointer. This also frees all child pointers of this
753 return 0 if the memory is actually freed, otherwise -1. The memory
754 will not be freed if the ref_count is > 1 or the destructor (if
755 any) returns non-zero
757 int talloc_free(void *ptr)
759 return _talloc_free(ptr);
765 A talloc version of realloc. The context argument is only used if
768 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
770 struct talloc_chunk *tc;
773 /* size zero is equivalent to free() */
774 if (unlikely(size == 0)) {
779 if (unlikely(size >= MAX_TALLOC_SIZE)) {
783 /* realloc(NULL) is equivalent to malloc() */
785 return _talloc_named_const(context, size, name);
788 tc = talloc_chunk_from_ptr(ptr);
790 /* don't allow realloc on referenced pointers */
791 if (unlikely(tc->refs)) {
795 /* by resetting magic we catch users of the old memory */
796 tc->flags |= TALLOC_FLAG_FREE;
799 new_ptr = malloc(size + TC_HDR_SIZE);
801 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
805 new_ptr = realloc(tc, size + TC_HDR_SIZE);
807 if (unlikely(!new_ptr)) {
808 tc->flags &= ~TALLOC_FLAG_FREE;
812 tc = (struct talloc_chunk *)new_ptr;
813 tc->flags &= ~TALLOC_FLAG_FREE;
815 tc->parent->child = tc;
818 tc->child->parent = tc;
829 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
831 return TC_PTR_FROM_CHUNK(tc);
835 a wrapper around talloc_steal() for situations where you are moving a pointer
836 between two structures, and want the old pointer to be set to NULL
838 void *_talloc_move(const void *new_ctx, const void *_pptr)
840 const void **pptr = discard_const_p(const void *,_pptr);
841 void *ret = _talloc_steal(new_ctx, *pptr);
847 return the total size of a talloc pool (subtree)
849 size_t talloc_total_size(const void *ptr)
852 struct talloc_chunk *c, *tc;
861 tc = talloc_chunk_from_ptr(ptr);
863 if (tc->flags & TALLOC_FLAG_LOOP) {
867 tc->flags |= TALLOC_FLAG_LOOP;
870 for (c=tc->child;c;c=c->next) {
871 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
874 tc->flags &= ~TALLOC_FLAG_LOOP;
880 return the total number of blocks in a talloc pool (subtree)
882 size_t talloc_total_blocks(const void *ptr)
885 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
887 if (tc->flags & TALLOC_FLAG_LOOP) {
891 tc->flags |= TALLOC_FLAG_LOOP;
894 for (c=tc->child;c;c=c->next) {
895 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
898 tc->flags &= ~TALLOC_FLAG_LOOP;
904 return the number of external references to a pointer
906 size_t talloc_reference_count(const void *ptr)
908 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
909 struct talloc_reference_handle *h;
912 for (h=tc->refs;h;h=h->next) {
919 report on memory usage by all children of a pointer, giving a full tree view
921 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
922 void (*callback)(const void *ptr,
923 int depth, int max_depth,
928 struct talloc_chunk *c, *tc;
933 if (ptr == NULL) return;
935 tc = talloc_chunk_from_ptr(ptr);
937 if (tc->flags & TALLOC_FLAG_LOOP) {
941 callback(ptr, depth, max_depth, 0, private_data);
943 if (max_depth >= 0 && depth >= max_depth) {
947 tc->flags |= TALLOC_FLAG_LOOP;
948 for (c=tc->child;c;c=c->next) {
949 if (c->name == TALLOC_MAGIC_REFERENCE) {
950 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
951 callback(h->ptr, depth + 1, max_depth, 1, private_data);
953 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
956 tc->flags &= ~TALLOC_FLAG_LOOP;
959 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
961 const char *name = talloc_get_name(ptr);
962 FILE *f = (FILE *)_f;
965 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
970 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
971 (max_depth < 0 ? "full " :""), name,
972 (unsigned long)talloc_total_size(ptr),
973 (unsigned long)talloc_total_blocks(ptr));
977 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
980 (unsigned long)talloc_total_size(ptr),
981 (unsigned long)talloc_total_blocks(ptr),
982 (int)talloc_reference_count(ptr), ptr);
985 fprintf(f, "content: ");
986 if (talloc_total_size(ptr)) {
987 int tot = talloc_total_size(ptr);
990 for (i = 0; i < tot; i++) {
991 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
992 fprintf(f, "%c", ((char *)ptr)[i]);
994 fprintf(f, "~%02x", ((char *)ptr)[i]);
1003 report on memory usage by all children of a pointer, giving a full tree view
1005 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1007 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1012 report on memory usage by all children of a pointer, giving a full tree view
1014 void talloc_report_full(const void *ptr, FILE *f)
1016 talloc_report_depth_file(ptr, 0, -1, f);
1020 report on memory usage by all children of a pointer
1022 void talloc_report(const void *ptr, FILE *f)
1024 talloc_report_depth_file(ptr, 0, 1, f);
1028 report on any memory hanging off the null context
1030 static void talloc_report_null(void)
1032 if (talloc_total_size(null_context) != 0) {
1033 talloc_report(null_context, stderr);
1038 report on any memory hanging off the null context
1040 static void talloc_report_null_full(void)
1042 if (talloc_total_size(null_context) != 0) {
1043 talloc_report_full(null_context, stderr);
1048 enable tracking of the NULL context
1050 void talloc_enable_null_tracking(void)
1052 if (null_context == NULL) {
1053 null_context = _talloc_named_const(NULL, 0, "null_context");
1058 disable tracking of the NULL context
1060 void talloc_disable_null_tracking(void)
1062 _talloc_free(null_context);
1063 null_context = NULL;
1067 enable leak reporting on exit
1069 void talloc_enable_leak_report(void)
1071 talloc_enable_null_tracking();
1072 atexit(talloc_report_null);
1076 enable full leak reporting on exit
1078 void talloc_enable_leak_report_full(void)
1080 talloc_enable_null_tracking();
1081 atexit(talloc_report_null_full);
1085 talloc and zero memory.
1087 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1089 void *p = _talloc_named_const(ctx, size, name);
1092 memset(p, '\0', size);
1099 memdup with a talloc.
1101 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1103 void *newp = _talloc_named_const(t, size, name);
1106 memcpy(newp, p, size);
1112 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1116 ret = (char *)__talloc(t, len + 1);
1117 if (unlikely(!ret)) return NULL;
1119 memcpy(ret, p, len);
1122 _talloc_set_name_const(ret, ret);
1127 strdup with a talloc
1129 char *talloc_strdup(const void *t, const char *p)
1131 if (unlikely(!p)) return NULL;
1132 return __talloc_strlendup(t, p, strlen(p));
1136 append to a talloced string
1138 char *talloc_append_string(const void *t, char *orig, const char *append)
1141 size_t olen = strlen(orig);
1147 alenz = strlen(append) + 1;
1149 ret = talloc_realloc(t, orig, char, olen + alenz);
1153 /* append the string with the trailing \0 */
1154 memcpy(&ret[olen], append, alenz);
1156 _talloc_set_name_const(ret, ret);
1162 strndup with a talloc
1164 char *talloc_strndup(const void *t, const char *p, size_t n)
1166 if (unlikely(!p)) return NULL;
1167 return __talloc_strlendup(t, p, strnlen(p, n));
1170 #ifndef HAVE_VA_COPY
1171 #ifdef HAVE___VA_COPY
1172 #define va_copy(dest, src) __va_copy(dest, src)
1174 #define va_copy(dest, src) (dest) = (src)
1178 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1185 /* this call looks strange, but it makes it work on older solaris boxes */
1187 len = vsnprintf(&c, 1, fmt, ap2);
1193 ret = (char *)__talloc(t, len+1);
1196 vsnprintf(ret, len+1, fmt, ap2);
1198 _talloc_set_name_const(ret, ret);
1206 Perform string formatting, and return a pointer to newly allocated
1207 memory holding the result, inside a memory pool.
1209 char *talloc_asprintf(const void *t, const char *fmt, ...)
1215 ret = talloc_vasprintf(t, fmt, ap);
1222 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1223 * and return @p s, which may have moved. Good for gradually
1224 * accumulating output into a string buffer. Appends at the end
1227 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1234 return talloc_vasprintf(NULL, fmt, ap);
1240 len = vsnprintf(&c, 1, fmt, ap2);
1244 /* Either the vsnprintf failed or the format resulted in
1245 * no characters being formatted. In the former case, we
1246 * ought to return NULL, in the latter we ought to return
1247 * the original string. Most current callers of this
1248 * function expect it to never return NULL.
1253 s = talloc_realloc(NULL, s, char, s_len + len+1);
1254 if (!s) return NULL;
1257 vsnprintf(s+s_len, len+1, fmt, ap2);
1259 _talloc_set_name_const(s, s);
1265 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1266 * and return @p s, which may have moved. Always appends at the
1267 * end of the talloc'ed buffer, not the end of the string.
1269 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1271 struct talloc_chunk *tc;
1277 return talloc_vasprintf(NULL, fmt, ap);
1280 tc = talloc_chunk_from_ptr(s);
1282 s_len = tc->size - 1;
1285 len = vsnprintf(&c, 1, fmt, ap2);
1289 /* Either the vsnprintf failed or the format resulted in
1290 * no characters being formatted. In the former case, we
1291 * ought to return NULL, in the latter we ought to return
1292 * the original string. Most current callers of this
1293 * function expect it to never return NULL.
1298 s = talloc_realloc(NULL, s, char, s_len + len+1);
1299 if (!s) return NULL;
1302 vsnprintf(s+s_len, len+1, fmt, ap2);
1304 _talloc_set_name_const(s, s);
1310 Realloc @p s to append the formatted result of @p fmt and return @p
1311 s, which may have moved. Good for gradually accumulating output
1312 into a string buffer.
1314 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1319 s = talloc_vasprintf_append(s, fmt, ap);
1325 Realloc @p s to append the formatted result of @p fmt and return @p
1326 s, which may have moved. Good for gradually accumulating output
1329 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1334 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1340 alloc an array, checking for integer overflow in the array size
1342 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1344 if (count >= MAX_TALLOC_SIZE/el_size) {
1347 return _talloc_named_const(ctx, el_size * count, name);
1351 alloc an zero array, checking for integer overflow in the array size
1353 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1355 if (count >= MAX_TALLOC_SIZE/el_size) {
1358 return _talloc_zero(ctx, el_size * count, name);
1362 realloc an array, checking for integer overflow in the array size
1364 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1366 if (count >= MAX_TALLOC_SIZE/el_size) {
1369 return _talloc_realloc(ctx, ptr, el_size * count, name);
1373 a function version of talloc_realloc(), so it can be passed as a function pointer
1374 to libraries that want a realloc function (a realloc function encapsulates
1375 all the basic capabilities of an allocation library, which is why this is useful)
1377 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1379 return _talloc_realloc(context, ptr, size, NULL);
1383 static int talloc_autofree_destructor(void *ptr)
1385 autofree_context = NULL;
1389 static void talloc_autofree(void)
1391 _talloc_free(autofree_context);
1395 return a context which will be auto-freed on exit
1396 this is useful for reducing the noise in leak reports
1398 void *talloc_autofree_context(void)
1400 if (autofree_context == NULL) {
1401 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1402 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1403 atexit(talloc_autofree);
1405 return autofree_context;
1408 size_t talloc_get_size(const void *context)
1410 struct talloc_chunk *tc;
1412 if (context == NULL)
1415 tc = talloc_chunk_from_ptr(context);
1421 find a parent of this context that has the given name, if any
1423 void *talloc_find_parent_byname(const void *context, const char *name)
1425 struct talloc_chunk *tc;
1427 if (context == NULL) {
1431 tc = talloc_chunk_from_ptr(context);
1433 if (tc->name && strcmp(tc->name, name) == 0) {
1434 return TC_PTR_FROM_CHUNK(tc);
1436 while (tc && tc->prev) tc = tc->prev;
1445 show the parentage of a context
1447 void talloc_show_parents(const void *context, FILE *file)
1449 struct talloc_chunk *tc;
1451 if (context == NULL) {
1452 fprintf(file, "talloc no parents for NULL\n");
1456 tc = talloc_chunk_from_ptr(context);
1457 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1459 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1460 while (tc && tc->prev) tc = tc->prev;
1469 return 1 if ptr is a parent of context
1471 int talloc_is_parent(const void *context, const void *ptr)
1473 struct talloc_chunk *tc;
1475 if (context == NULL) {
1479 tc = talloc_chunk_from_ptr(context);
1481 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1482 while (tc && tc->prev) tc = tc->prev;