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 /* panic if we get a bad magic value */
119 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
121 const char *pp = (const char *)ptr;
122 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
123 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
124 if (tc->flags & TALLOC_FLAG_FREE) {
125 TALLOC_ABORT("Bad talloc magic value - double free");
127 TALLOC_ABORT("Bad talloc magic value - unknown value");
133 /* hook into the front of the list */
134 #define _TLIST_ADD(list, p) \
138 (p)->next = (p)->prev = NULL; \
140 (list)->prev = (p); \
141 (p)->next = (list); \
147 /* remove an element from a list - element doesn't have to be in list. */
148 #define _TLIST_REMOVE(list, p) \
150 if ((p) == (list)) { \
151 (list) = (p)->next; \
152 if (list) (list)->prev = NULL; \
154 if ((p)->prev) (p)->prev->next = (p)->next; \
155 if ((p)->next) (p)->next->prev = (p)->prev; \
157 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
162 return the parent chunk of a pointer
164 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
166 struct talloc_chunk *tc;
168 if (unlikely(ptr == NULL)) {
172 tc = talloc_chunk_from_ptr(ptr);
173 while (tc->prev) tc=tc->prev;
178 void *talloc_parent(const void *ptr)
180 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
181 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
187 const char *talloc_parent_name(const void *ptr)
189 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
190 return tc? tc->name : NULL;
194 Allocate a bit of memory as a child of an existing pointer
196 static inline void *__talloc(const void *context, size_t size)
198 struct talloc_chunk *tc;
200 if (unlikely(context == NULL)) {
201 context = null_context;
204 if (unlikely(size >= MAX_TALLOC_SIZE)) {
208 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
209 if (unlikely(tc == NULL)) return NULL;
212 tc->flags = TALLOC_MAGIC;
213 tc->destructor = NULL;
218 if (likely(context)) {
219 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
222 parent->child->parent = NULL;
223 tc->next = parent->child;
232 tc->next = tc->prev = tc->parent = NULL;
235 return TC_PTR_FROM_CHUNK(tc);
239 setup a destructor to be called on free of a pointer
240 the destructor should return 0 on success, or -1 on failure.
241 if the destructor fails then the free is failed, and the memory can
242 be continued to be used
244 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
246 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
247 tc->destructor = destructor;
251 increase the reference count on a piece of memory.
253 int talloc_increase_ref_count(const void *ptr)
255 if (unlikely(!talloc_reference(null_context, ptr))) {
262 helper for talloc_reference()
264 this is referenced by a function pointer and should not be inline
266 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
268 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
269 _TLIST_REMOVE(ptr_tc->refs, handle);
274 more efficient way to add a name to a pointer - the name must point to a
277 static inline void _talloc_set_name_const(const void *ptr, const char *name)
279 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
284 internal talloc_named_const()
286 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
290 ptr = __talloc(context, size);
291 if (unlikely(ptr == NULL)) {
295 _talloc_set_name_const(ptr, name);
301 make a secondary reference to a pointer, hanging off the given context.
302 the pointer remains valid until both the original caller and this given
305 the major use for this is when two different structures need to reference the
306 same underlying data, and you want to be able to free the two instances separately,
309 void *_talloc_reference(const void *context, const void *ptr)
311 struct talloc_chunk *tc;
312 struct talloc_reference_handle *handle;
313 if (unlikely(ptr == NULL)) return NULL;
315 tc = talloc_chunk_from_ptr(ptr);
316 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
317 sizeof(struct talloc_reference_handle),
318 TALLOC_MAGIC_REFERENCE);
319 if (unlikely(handle == NULL)) return NULL;
321 /* note that we hang the destructor off the handle, not the
322 main context as that allows the caller to still setup their
323 own destructor on the context if they want to */
324 talloc_set_destructor(handle, talloc_reference_destructor);
325 handle->ptr = discard_const_p(void, ptr);
326 _TLIST_ADD(tc->refs, handle);
332 internal talloc_free call
334 static inline int _talloc_free(void *ptr)
336 struct talloc_chunk *tc;
338 if (unlikely(ptr == NULL)) {
342 tc = talloc_chunk_from_ptr(ptr);
344 if (unlikely(tc->refs)) {
346 /* check this is a reference from a child or grantchild
347 * back to it's parent or grantparent
349 * in that case we need to remove the reference and
350 * call another instance of talloc_free() on the current
353 is_child = talloc_is_parent(tc->refs, ptr);
354 _talloc_free(tc->refs);
356 return _talloc_free(ptr);
361 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
362 /* we have a free loop - stop looping */
366 if (unlikely(tc->destructor)) {
367 talloc_destructor_t d = tc->destructor;
368 if (d == (talloc_destructor_t)-1) {
371 tc->destructor = (talloc_destructor_t)-1;
376 tc->destructor = NULL;
380 _TLIST_REMOVE(tc->parent->child, tc);
381 if (tc->parent->child) {
382 tc->parent->child->parent = tc->parent;
385 if (tc->prev) tc->prev->next = tc->next;
386 if (tc->next) tc->next->prev = tc->prev;
389 tc->flags |= TALLOC_FLAG_LOOP;
392 /* we need to work out who will own an abandoned child
393 if it cannot be freed. In priority order, the first
394 choice is owner of any remaining reference to this
395 pointer, the second choice is our parent, and the
396 final choice is the null context. */
397 void *child = TC_PTR_FROM_CHUNK(tc->child);
398 const void *new_parent = null_context;
399 if (unlikely(tc->child->refs)) {
400 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
401 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
403 if (unlikely(_talloc_free(child) == -1)) {
404 if (new_parent == null_context) {
405 struct talloc_chunk *p = talloc_parent_chunk(ptr);
406 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
408 talloc_steal(new_parent, child);
412 tc->flags |= TALLOC_FLAG_FREE;
418 move a lump of memory from one talloc context to another return the
419 ptr on success, or NULL if it could not be transferred.
420 passing NULL as ptr will always return NULL with no side effects.
422 void *_talloc_steal(const void *new_ctx, const void *ptr)
424 struct talloc_chunk *tc, *new_tc;
426 if (unlikely(!ptr)) {
430 if (unlikely(new_ctx == NULL)) {
431 new_ctx = null_context;
434 tc = talloc_chunk_from_ptr(ptr);
436 if (unlikely(new_ctx == NULL)) {
438 _TLIST_REMOVE(tc->parent->child, tc);
439 if (tc->parent->child) {
440 tc->parent->child->parent = tc->parent;
443 if (tc->prev) tc->prev->next = tc->next;
444 if (tc->next) tc->next->prev = tc->prev;
447 tc->parent = tc->next = tc->prev = NULL;
448 return discard_const_p(void, ptr);
451 new_tc = talloc_chunk_from_ptr(new_ctx);
453 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
454 return discard_const_p(void, ptr);
458 _TLIST_REMOVE(tc->parent->child, tc);
459 if (tc->parent->child) {
460 tc->parent->child->parent = tc->parent;
463 if (tc->prev) tc->prev->next = tc->next;
464 if (tc->next) tc->next->prev = tc->prev;
468 if (new_tc->child) new_tc->child->parent = NULL;
469 _TLIST_ADD(new_tc->child, tc);
471 return discard_const_p(void, ptr);
477 remove a secondary reference to a pointer. This undo's what
478 talloc_reference() has done. The context and pointer arguments
479 must match those given to a talloc_reference()
481 static inline int talloc_unreference(const void *context, const void *ptr)
483 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
484 struct talloc_reference_handle *h;
486 if (unlikely(context == NULL)) {
487 context = null_context;
490 for (h=tc->refs;h;h=h->next) {
491 struct talloc_chunk *p = talloc_parent_chunk(h);
493 if (context == NULL) break;
494 } else if (TC_PTR_FROM_CHUNK(p) == context) {
502 return _talloc_free(h);
506 remove a specific parent context from a pointer. This is a more
507 controlled varient of talloc_free()
509 int talloc_unlink(const void *context, void *ptr)
511 struct talloc_chunk *tc_p, *new_p;
518 if (context == NULL) {
519 context = null_context;
522 if (talloc_unreference(context, ptr) == 0) {
526 if (context == NULL) {
527 if (talloc_parent_chunk(ptr) != NULL) {
531 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
536 tc_p = talloc_chunk_from_ptr(ptr);
538 if (tc_p->refs == NULL) {
539 return _talloc_free(ptr);
542 new_p = talloc_parent_chunk(tc_p->refs);
544 new_parent = TC_PTR_FROM_CHUNK(new_p);
549 if (talloc_unreference(new_parent, ptr) != 0) {
553 talloc_steal(new_parent, ptr);
559 add a name to an existing pointer - va_list version
561 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
563 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
565 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
566 tc->name = talloc_vasprintf(ptr, fmt, ap);
567 if (likely(tc->name)) {
568 _talloc_set_name_const(tc->name, ".name");
574 add a name to an existing pointer
576 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
581 name = talloc_set_name_v(ptr, fmt, ap);
588 create a named talloc pointer. Any talloc pointer can be named, and
589 talloc_named() operates just like talloc() except that it allows you
592 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
598 ptr = __talloc(context, size);
599 if (unlikely(ptr == NULL)) return NULL;
602 name = talloc_set_name_v(ptr, fmt, ap);
605 if (unlikely(name == NULL)) {
614 return the name of a talloc ptr, or "UNNAMED"
616 const char *talloc_get_name(const void *ptr)
618 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
619 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
622 if (likely(tc->name)) {
630 check if a pointer has the given name. If it does, return the pointer,
631 otherwise return NULL
633 void *talloc_check_name(const void *ptr, const char *name)
636 if (unlikely(ptr == NULL)) return NULL;
637 pname = talloc_get_name(ptr);
638 if (likely(pname == name || strcmp(pname, name) == 0)) {
639 return discard_const_p(void, ptr);
646 this is for compatibility with older versions of talloc
648 void *talloc_init(const char *fmt, ...)
655 * samba3 expects talloc_report_depth_cb(NULL, ...)
656 * reports all talloc'ed memory, so we need to enable
659 talloc_enable_null_tracking();
661 ptr = __talloc(NULL, 0);
662 if (unlikely(ptr == NULL)) return NULL;
665 name = talloc_set_name_v(ptr, fmt, ap);
668 if (unlikely(name == NULL)) {
677 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
678 should probably not be used in new code. It's in here to keep the talloc
679 code consistent across Samba 3 and 4.
681 void talloc_free_children(void *ptr)
683 struct talloc_chunk *tc;
685 if (unlikely(ptr == NULL)) {
689 tc = talloc_chunk_from_ptr(ptr);
692 /* we need to work out who will own an abandoned child
693 if it cannot be freed. In priority order, the first
694 choice is owner of any remaining reference to this
695 pointer, the second choice is our parent, and the
696 final choice is the null context. */
697 void *child = TC_PTR_FROM_CHUNK(tc->child);
698 const void *new_parent = null_context;
699 if (unlikely(tc->child->refs)) {
700 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
701 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
703 if (unlikely(_talloc_free(child) == -1)) {
704 if (new_parent == null_context) {
705 struct talloc_chunk *p = talloc_parent_chunk(ptr);
706 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
708 talloc_steal(new_parent, child);
714 Allocate a bit of memory as a child of an existing pointer
716 void *_talloc(const void *context, size_t size)
718 return __talloc(context, size);
722 externally callable talloc_set_name_const()
724 void talloc_set_name_const(const void *ptr, const char *name)
726 _talloc_set_name_const(ptr, name);
730 create a named talloc pointer. Any talloc pointer can be named, and
731 talloc_named() operates just like talloc() except that it allows you
734 void *talloc_named_const(const void *context, size_t size, const char *name)
736 return _talloc_named_const(context, size, name);
740 free a talloc pointer. This also frees all child pointers of this
743 return 0 if the memory is actually freed, otherwise -1. The memory
744 will not be freed if the ref_count is > 1 or the destructor (if
745 any) returns non-zero
747 int talloc_free(void *ptr)
749 return _talloc_free(ptr);
755 A talloc version of realloc. The context argument is only used if
758 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
760 struct talloc_chunk *tc;
763 /* size zero is equivalent to free() */
764 if (unlikely(size == 0)) {
769 if (unlikely(size >= MAX_TALLOC_SIZE)) {
773 /* realloc(NULL) is equivalent to malloc() */
775 return _talloc_named_const(context, size, name);
778 tc = talloc_chunk_from_ptr(ptr);
780 /* don't allow realloc on referenced pointers */
781 if (unlikely(tc->refs)) {
785 /* by resetting magic we catch users of the old memory */
786 tc->flags |= TALLOC_FLAG_FREE;
789 new_ptr = malloc(size + TC_HDR_SIZE);
791 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
795 new_ptr = realloc(tc, size + TC_HDR_SIZE);
797 if (unlikely(!new_ptr)) {
798 tc->flags &= ~TALLOC_FLAG_FREE;
802 tc = (struct talloc_chunk *)new_ptr;
803 tc->flags &= ~TALLOC_FLAG_FREE;
805 tc->parent->child = tc;
808 tc->child->parent = tc;
819 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
821 return TC_PTR_FROM_CHUNK(tc);
825 a wrapper around talloc_steal() for situations where you are moving a pointer
826 between two structures, and want the old pointer to be set to NULL
828 void *_talloc_move(const void *new_ctx, const void *_pptr)
830 const void **pptr = discard_const_p(const void *,_pptr);
831 void *ret = _talloc_steal(new_ctx, *pptr);
837 return the total size of a talloc pool (subtree)
839 size_t talloc_total_size(const void *ptr)
842 struct talloc_chunk *c, *tc;
851 tc = talloc_chunk_from_ptr(ptr);
853 if (tc->flags & TALLOC_FLAG_LOOP) {
857 tc->flags |= TALLOC_FLAG_LOOP;
860 for (c=tc->child;c;c=c->next) {
861 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
864 tc->flags &= ~TALLOC_FLAG_LOOP;
870 return the total number of blocks in a talloc pool (subtree)
872 size_t talloc_total_blocks(const void *ptr)
875 struct talloc_chunk *c, *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_blocks(TC_PTR_FROM_CHUNK(c));
888 tc->flags &= ~TALLOC_FLAG_LOOP;
894 return the number of external references to a pointer
896 size_t talloc_reference_count(const void *ptr)
898 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
899 struct talloc_reference_handle *h;
902 for (h=tc->refs;h;h=h->next) {
909 report on memory usage by all children of a pointer, giving a full tree view
911 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
912 void (*callback)(const void *ptr,
913 int depth, int max_depth,
918 struct talloc_chunk *c, *tc;
923 if (ptr == NULL) return;
925 tc = talloc_chunk_from_ptr(ptr);
927 if (tc->flags & TALLOC_FLAG_LOOP) {
931 callback(ptr, depth, max_depth, 0, private_data);
933 if (max_depth >= 0 && depth >= max_depth) {
937 tc->flags |= TALLOC_FLAG_LOOP;
938 for (c=tc->child;c;c=c->next) {
939 if (c->name == TALLOC_MAGIC_REFERENCE) {
940 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
941 callback(h->ptr, depth + 1, max_depth, 1, private_data);
943 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
946 tc->flags &= ~TALLOC_FLAG_LOOP;
949 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
951 const char *name = talloc_get_name(ptr);
952 FILE *f = (FILE *)_f;
955 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
960 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
961 (max_depth < 0 ? "full " :""), name,
962 (unsigned long)talloc_total_size(ptr),
963 (unsigned long)talloc_total_blocks(ptr));
967 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
970 (unsigned long)talloc_total_size(ptr),
971 (unsigned long)talloc_total_blocks(ptr),
972 (int)talloc_reference_count(ptr), ptr);
975 fprintf(f, "content: ");
976 if (talloc_total_size(ptr)) {
977 int tot = talloc_total_size(ptr);
980 for (i = 0; i < tot; i++) {
981 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
982 fprintf(f, "%c", ((char *)ptr)[i]);
984 fprintf(f, "~%02x", ((char *)ptr)[i]);
993 report on memory usage by all children of a pointer, giving a full tree view
995 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
997 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1002 report on memory usage by all children of a pointer, giving a full tree view
1004 void talloc_report_full(const void *ptr, FILE *f)
1006 talloc_report_depth_file(ptr, 0, -1, f);
1010 report on memory usage by all children of a pointer
1012 void talloc_report(const void *ptr, FILE *f)
1014 talloc_report_depth_file(ptr, 0, 1, f);
1018 report on any memory hanging off the null context
1020 static void talloc_report_null(void)
1022 if (talloc_total_size(null_context) != 0) {
1023 talloc_report(null_context, stderr);
1028 report on any memory hanging off the null context
1030 static void talloc_report_null_full(void)
1032 if (talloc_total_size(null_context) != 0) {
1033 talloc_report_full(null_context, stderr);
1038 enable tracking of the NULL context
1040 void talloc_enable_null_tracking(void)
1042 if (null_context == NULL) {
1043 null_context = _talloc_named_const(NULL, 0, "null_context");
1048 disable tracking of the NULL context
1050 void talloc_disable_null_tracking(void)
1052 _talloc_free(null_context);
1053 null_context = NULL;
1057 enable leak reporting on exit
1059 void talloc_enable_leak_report(void)
1061 talloc_enable_null_tracking();
1062 atexit(talloc_report_null);
1066 enable full leak reporting on exit
1068 void talloc_enable_leak_report_full(void)
1070 talloc_enable_null_tracking();
1071 atexit(talloc_report_null_full);
1075 talloc and zero memory.
1077 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1079 void *p = _talloc_named_const(ctx, size, name);
1082 memset(p, '\0', size);
1089 memdup with a talloc.
1091 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1093 void *newp = _talloc_named_const(t, size, name);
1096 memcpy(newp, p, size);
1103 strdup with a talloc
1105 char *talloc_strdup(const void *t, const char *p)
1111 ret = (char *)talloc_memdup(t, p, strlen(p) + 1);
1113 _talloc_set_name_const(ret, ret);
1119 append to a talloced string
1121 char *talloc_append_string(const void *t, char *orig, const char *append)
1124 size_t olen = strlen(orig);
1130 alenz = strlen(append) + 1;
1132 ret = talloc_realloc(t, orig, char, olen + alenz);
1136 /* append the string with the trailing \0 */
1137 memcpy(&ret[olen], append, alenz);
1139 _talloc_set_name_const(ret, ret);
1145 strndup with a talloc
1147 char *talloc_strndup(const void *t, const char *p, size_t n)
1152 for (len=0; len<n && p[len]; len++) ;
1154 ret = (char *)__talloc(t, len + 1);
1155 if (!ret) { return NULL; }
1156 memcpy(ret, p, len);
1158 _talloc_set_name_const(ret, ret);
1162 #ifndef HAVE_VA_COPY
1163 #ifdef HAVE___VA_COPY
1164 #define va_copy(dest, src) __va_copy(dest, src)
1166 #define va_copy(dest, src) (dest) = (src)
1170 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1177 /* this call looks strange, but it makes it work on older solaris boxes */
1179 len = vsnprintf(&c, 1, fmt, ap2);
1185 ret = (char *)__talloc(t, len+1);
1188 vsnprintf(ret, len+1, fmt, ap2);
1190 _talloc_set_name_const(ret, ret);
1198 Perform string formatting, and return a pointer to newly allocated
1199 memory holding the result, inside a memory pool.
1201 char *talloc_asprintf(const void *t, const char *fmt, ...)
1207 ret = talloc_vasprintf(t, fmt, ap);
1214 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1215 * and return @p s, which may have moved. Good for gradually
1216 * accumulating output into a string buffer.
1218 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1220 struct talloc_chunk *tc;
1226 return talloc_vasprintf(NULL, fmt, ap);
1229 tc = talloc_chunk_from_ptr(s);
1231 s_len = tc->size - 1;
1234 len = vsnprintf(&c, 1, fmt, ap2);
1238 /* Either the vsnprintf failed or the format resulted in
1239 * no characters being formatted. In the former case, we
1240 * ought to return NULL, in the latter we ought to return
1241 * the original string. Most current callers of this
1242 * function expect it to never return NULL.
1247 s = talloc_realloc(NULL, s, char, s_len + len+1);
1248 if (!s) return NULL;
1251 vsnprintf(s+s_len, len+1, fmt, ap2);
1253 _talloc_set_name_const(s, s);
1259 Realloc @p s to append the formatted result of @p fmt and return @p
1260 s, which may have moved. Good for gradually accumulating output
1261 into a string buffer.
1263 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1268 s = talloc_vasprintf_append(s, fmt, ap);
1274 alloc an array, checking for integer overflow in the array size
1276 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1278 if (count >= MAX_TALLOC_SIZE/el_size) {
1281 return _talloc_named_const(ctx, el_size * count, name);
1285 alloc an zero array, checking for integer overflow in the array size
1287 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1289 if (count >= MAX_TALLOC_SIZE/el_size) {
1292 return _talloc_zero(ctx, el_size * count, name);
1296 realloc an array, checking for integer overflow in the array size
1298 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1300 if (count >= MAX_TALLOC_SIZE/el_size) {
1303 return _talloc_realloc(ctx, ptr, el_size * count, name);
1307 a function version of talloc_realloc(), so it can be passed as a function pointer
1308 to libraries that want a realloc function (a realloc function encapsulates
1309 all the basic capabilities of an allocation library, which is why this is useful)
1311 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1313 return _talloc_realloc(context, ptr, size, NULL);
1317 static int talloc_autofree_destructor(void *ptr)
1319 autofree_context = NULL;
1323 static void talloc_autofree(void)
1325 _talloc_free(autofree_context);
1329 return a context which will be auto-freed on exit
1330 this is useful for reducing the noise in leak reports
1332 void *talloc_autofree_context(void)
1334 if (autofree_context == NULL) {
1335 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1336 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1337 atexit(talloc_autofree);
1339 return autofree_context;
1342 size_t talloc_get_size(const void *context)
1344 struct talloc_chunk *tc;
1346 if (context == NULL)
1349 tc = talloc_chunk_from_ptr(context);
1355 find a parent of this context that has the given name, if any
1357 void *talloc_find_parent_byname(const void *context, const char *name)
1359 struct talloc_chunk *tc;
1361 if (context == NULL) {
1365 tc = talloc_chunk_from_ptr(context);
1367 if (tc->name && strcmp(tc->name, name) == 0) {
1368 return TC_PTR_FROM_CHUNK(tc);
1370 while (tc && tc->prev) tc = tc->prev;
1379 show the parentage of a context
1381 void talloc_show_parents(const void *context, FILE *file)
1383 struct talloc_chunk *tc;
1385 if (context == NULL) {
1386 fprintf(file, "talloc no parents for NULL\n");
1390 tc = talloc_chunk_from_ptr(context);
1391 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1393 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1394 while (tc && tc->prev) tc = tc->prev;
1403 return 1 if ptr is a parent of context
1405 int talloc_is_parent(const void *context, const void *ptr)
1407 struct talloc_chunk *tc;
1409 if (context == NULL) {
1413 tc = talloc_chunk_from_ptr(context);
1415 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1416 while (tc && tc->prev) tc = tc->prev;