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_FLAG_POOL 0x04 /* This is a talloc pool */
64 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
65 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
67 /* by default we abort when given a bad pointer (such as when talloc_free() is called
68 on a pointer that came from malloc() */
70 #define TALLOC_ABORT(reason) abort()
73 #ifndef discard_const_p
74 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
75 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
77 # define discard_const_p(type, ptr) ((type *)(ptr))
81 /* these macros gain us a few percent of speed on gcc */
83 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
84 as its first argument */
86 #define likely(x) __builtin_expect(!!(x), 1)
89 #define unlikely(x) __builtin_expect(!!(x), 0)
96 #define unlikely(x) (x)
100 /* this null_context is only used if talloc_enable_leak_report() or
101 talloc_enable_leak_report_full() is called, otherwise it remains
104 static void *null_context;
105 static void *autofree_context;
107 struct talloc_reference_handle {
108 struct talloc_reference_handle *next, *prev;
110 const char *location;
113 typedef int (*talloc_destructor_t)(void *);
115 struct talloc_chunk {
116 struct talloc_chunk *next, *prev;
117 struct talloc_chunk *parent, *child;
118 struct talloc_reference_handle *refs;
119 talloc_destructor_t destructor;
125 * "pool" has dual use:
127 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
128 * marks the end of the currently allocated area.
130 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
131 * is a pointer to the struct talloc_chunk of the pool that it was
132 * allocated from. This way children can quickly find the pool to chew
138 /* 16 byte alignment seems to keep everyone happy */
139 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
140 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
142 static void (*talloc_abort_fn)(const char *reason);
144 void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
146 talloc_abort_fn = abort_fn;
149 static void talloc_abort(const char *reason)
151 if (!talloc_abort_fn) {
152 TALLOC_ABORT(reason);
155 talloc_abort_fn(reason);
158 static void talloc_abort_double_free(void)
160 talloc_abort("Bad talloc magic value - double free");
163 static void talloc_abort_unknown_value(void)
165 talloc_abort("Bad talloc magic value - unknown value");
168 /* panic if we get a bad magic value */
169 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
171 const char *pp = (const char *)ptr;
172 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
173 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
174 if (tc->flags & TALLOC_FLAG_FREE) {
175 talloc_abort_double_free();
177 talloc_abort_unknown_value();
183 /* hook into the front of the list */
184 #define _TLIST_ADD(list, p) \
188 (p)->next = (p)->prev = NULL; \
190 (list)->prev = (p); \
191 (p)->next = (list); \
197 /* remove an element from a list - element doesn't have to be in list. */
198 #define _TLIST_REMOVE(list, p) \
200 if ((p) == (list)) { \
201 (list) = (p)->next; \
202 if (list) (list)->prev = NULL; \
204 if ((p)->prev) (p)->prev->next = (p)->next; \
205 if ((p)->next) (p)->next->prev = (p)->prev; \
207 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
212 return the parent chunk of a pointer
214 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
216 struct talloc_chunk *tc;
218 if (unlikely(ptr == NULL)) {
222 tc = talloc_chunk_from_ptr(ptr);
223 while (tc->prev) tc=tc->prev;
228 void *talloc_parent(const void *ptr)
230 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
231 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
237 const char *talloc_parent_name(const void *ptr)
239 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
240 return tc? tc->name : NULL;
244 A pool carries an in-pool object count count in the first 16 bytes.
245 bytes. This is done to support talloc_steal() to a parent outside of the
246 pool. The count includes the pool itself, so a talloc_free() on a pool will
247 only destroy the pool if the count has dropped to zero. A talloc_free() of a
248 pool member will reduce the count, and eventually also call free(3) on the
251 The object count is not put into "struct talloc_chunk" because it is only
252 relevant for talloc pools and the alignment to 16 bytes would increase the
253 memory footprint of each talloc chunk by those 16 bytes.
256 #define TALLOC_POOL_HDR_SIZE 16
258 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
260 return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
267 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
270 struct talloc_chunk *pool_ctx = NULL;
272 struct talloc_chunk *result;
275 if (parent == NULL) {
279 if (parent->flags & TALLOC_FLAG_POOL) {
282 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
283 pool_ctx = (struct talloc_chunk *)parent->pool;
286 if (pool_ctx == NULL) {
290 space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
291 - ((char *)pool_ctx->pool);
294 * Align size to 16 bytes
296 chunk_size = ((size + 15) & ~15);
298 if (space_left < chunk_size) {
302 result = (struct talloc_chunk *)pool_ctx->pool;
304 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
305 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
308 pool_ctx->pool = (void *)((char *)result + chunk_size);
310 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
311 result->pool = pool_ctx;
313 *talloc_pool_objectcount(pool_ctx) += 1;
319 Allocate a bit of memory as a child of an existing pointer
321 static inline void *__talloc(const void *context, size_t size)
323 struct talloc_chunk *tc = NULL;
325 if (unlikely(context == NULL)) {
326 context = null_context;
329 if (unlikely(size >= MAX_TALLOC_SIZE)) {
333 if (context != NULL) {
334 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
339 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
340 if (unlikely(tc == NULL)) return NULL;
341 tc->flags = TALLOC_MAGIC;
346 tc->destructor = NULL;
351 if (likely(context)) {
352 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
355 parent->child->parent = NULL;
356 tc->next = parent->child;
365 tc->next = tc->prev = tc->parent = NULL;
368 return TC_PTR_FROM_CHUNK(tc);
372 * Create a talloc pool
375 void *talloc_pool(const void *context, size_t size)
377 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
378 struct talloc_chunk *tc;
380 if (unlikely(result == NULL)) {
384 tc = talloc_chunk_from_ptr(result);
386 tc->flags |= TALLOC_FLAG_POOL;
387 tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
389 *talloc_pool_objectcount(tc) = 1;
391 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
392 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
399 setup a destructor to be called on free of a pointer
400 the destructor should return 0 on success, or -1 on failure.
401 if the destructor fails then the free is failed, and the memory can
402 be continued to be used
404 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
406 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
407 tc->destructor = destructor;
411 increase the reference count on a piece of memory.
413 int talloc_increase_ref_count(const void *ptr)
415 if (unlikely(!talloc_reference(null_context, ptr))) {
422 helper for talloc_reference()
424 this is referenced by a function pointer and should not be inline
426 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
428 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
429 _TLIST_REMOVE(ptr_tc->refs, handle);
434 more efficient way to add a name to a pointer - the name must point to a
437 static inline void _talloc_set_name_const(const void *ptr, const char *name)
439 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
444 internal talloc_named_const()
446 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
450 ptr = __talloc(context, size);
451 if (unlikely(ptr == NULL)) {
455 _talloc_set_name_const(ptr, name);
461 make a secondary reference to a pointer, hanging off the given context.
462 the pointer remains valid until both the original caller and this given
465 the major use for this is when two different structures need to reference the
466 same underlying data, and you want to be able to free the two instances separately,
469 void *_talloc_reference(const void *context, const void *ptr, const char *location)
471 struct talloc_chunk *tc;
472 struct talloc_reference_handle *handle;
473 if (unlikely(ptr == NULL)) return NULL;
475 tc = talloc_chunk_from_ptr(ptr);
476 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
477 sizeof(struct talloc_reference_handle),
478 TALLOC_MAGIC_REFERENCE);
479 if (unlikely(handle == NULL)) return NULL;
481 /* note that we hang the destructor off the handle, not the
482 main context as that allows the caller to still setup their
483 own destructor on the context if they want to */
484 talloc_set_destructor(handle, talloc_reference_destructor);
485 handle->ptr = discard_const_p(void, ptr);
486 handle->location = location;
487 _TLIST_ADD(tc->refs, handle);
493 internal talloc_free call
495 static inline int _talloc_free_internal(void *ptr)
497 struct talloc_chunk *tc;
499 if (unlikely(ptr == NULL)) {
503 tc = talloc_chunk_from_ptr(ptr);
505 if (unlikely(tc->refs)) {
507 /* check this is a reference from a child or grantchild
508 * back to it's parent or grantparent
510 * in that case we need to remove the reference and
511 * call another instance of talloc_free() on the current
514 is_child = talloc_is_parent(tc->refs, ptr);
515 _talloc_free_internal(tc->refs);
517 return _talloc_free_internal(ptr);
522 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
523 /* we have a free loop - stop looping */
527 if (unlikely(tc->destructor)) {
528 talloc_destructor_t d = tc->destructor;
529 if (d == (talloc_destructor_t)-1) {
532 tc->destructor = (talloc_destructor_t)-1;
537 tc->destructor = NULL;
541 _TLIST_REMOVE(tc->parent->child, tc);
542 if (tc->parent->child) {
543 tc->parent->child->parent = tc->parent;
546 if (tc->prev) tc->prev->next = tc->next;
547 if (tc->next) tc->next->prev = tc->prev;
550 tc->flags |= TALLOC_FLAG_LOOP;
553 /* we need to work out who will own an abandoned child
554 if it cannot be freed. In priority order, the first
555 choice is owner of any remaining reference to this
556 pointer, the second choice is our parent, and the
557 final choice is the null context. */
558 void *child = TC_PTR_FROM_CHUNK(tc->child);
559 const void *new_parent = null_context;
560 if (unlikely(tc->child->refs)) {
561 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
562 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
564 if (unlikely(_talloc_free_internal(child) == -1)) {
565 if (new_parent == null_context) {
566 struct talloc_chunk *p = talloc_parent_chunk(ptr);
567 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
569 _talloc_steal_internal(new_parent, child);
573 tc->flags |= TALLOC_FLAG_FREE;
575 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
576 struct talloc_chunk *pool;
577 unsigned int *pool_object_count;
579 pool = (tc->flags & TALLOC_FLAG_POOL)
580 ? tc : (struct talloc_chunk *)tc->pool;
582 pool_object_count = talloc_pool_objectcount(pool);
584 if (*pool_object_count == 0) {
585 talloc_abort("Pool object count zero!");
588 *pool_object_count -= 1;
590 if (*pool_object_count == 0) {
601 move a lump of memory from one talloc context to another return the
602 ptr on success, or NULL if it could not be transferred.
603 passing NULL as ptr will always return NULL with no side effects.
605 void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
607 struct talloc_chunk *tc, *new_tc;
609 if (unlikely(!ptr)) {
613 if (unlikely(new_ctx == NULL)) {
614 new_ctx = null_context;
617 tc = talloc_chunk_from_ptr(ptr);
619 if (unlikely(new_ctx == NULL)) {
621 _TLIST_REMOVE(tc->parent->child, tc);
622 if (tc->parent->child) {
623 tc->parent->child->parent = tc->parent;
626 if (tc->prev) tc->prev->next = tc->next;
627 if (tc->next) tc->next->prev = tc->prev;
630 tc->parent = tc->next = tc->prev = NULL;
631 return discard_const_p(void, ptr);
634 new_tc = talloc_chunk_from_ptr(new_ctx);
636 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
637 return discard_const_p(void, ptr);
641 _TLIST_REMOVE(tc->parent->child, tc);
642 if (tc->parent->child) {
643 tc->parent->child->parent = tc->parent;
646 if (tc->prev) tc->prev->next = tc->next;
647 if (tc->next) tc->next->prev = tc->prev;
651 if (new_tc->child) new_tc->child->parent = NULL;
652 _TLIST_ADD(new_tc->child, tc);
654 return discard_const_p(void, ptr);
659 move a lump of memory from one talloc context to another return the
660 ptr on success, or NULL if it could not be transferred.
661 passing NULL as ptr will always return NULL with no side effects.
663 void *_talloc_steal(const void *new_ctx, const void *ptr, const char *location)
665 struct talloc_chunk *tc;
667 if (unlikely(ptr == NULL)) {
671 tc = talloc_chunk_from_ptr(ptr);
673 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
674 struct talloc_reference_handle *h;
675 fprintf(stderr, "ERROR: talloc_steal with references at %s\n", location);
676 for (h=tc->refs; h; h=h->next) {
677 fprintf(stderr, "\treference at %s\n", h->location);
682 return _talloc_steal_internal(new_ctx, ptr);
686 this is like a talloc_steal(), but you must supply the old
687 parent. This resolves the ambiguity in a talloc_steal() which is
688 called on a context that has more than one parent (via references)
690 The old parent can be either a reference or a parent
692 void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
694 struct talloc_chunk *tc;
695 struct talloc_reference_handle *h;
697 if (unlikely(ptr == NULL)) {
701 if (old_parent == talloc_parent(ptr)) {
702 return _talloc_steal_internal(new_parent, ptr);
705 tc = talloc_chunk_from_ptr(ptr);
706 for (h=tc->refs;h;h=h->next) {
707 if (talloc_parent(h) == old_parent) {
708 if (_talloc_steal_internal(new_parent, h) != h) {
715 /* it wasn't a parent */
720 remove a secondary reference to a pointer. This undo's what
721 talloc_reference() has done. The context and pointer arguments
722 must match those given to a talloc_reference()
724 static inline int talloc_unreference(const void *context, const void *ptr)
726 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
727 struct talloc_reference_handle *h;
729 if (unlikely(context == NULL)) {
730 context = null_context;
733 for (h=tc->refs;h;h=h->next) {
734 struct talloc_chunk *p = talloc_parent_chunk(h);
736 if (context == NULL) break;
737 } else if (TC_PTR_FROM_CHUNK(p) == context) {
745 return _talloc_free_internal(h);
749 remove a specific parent context from a pointer. This is a more
750 controlled varient of talloc_free()
752 int talloc_unlink(const void *context, void *ptr)
754 struct talloc_chunk *tc_p, *new_p;
761 if (context == NULL) {
762 context = null_context;
765 if (talloc_unreference(context, ptr) == 0) {
769 if (context == NULL) {
770 if (talloc_parent_chunk(ptr) != NULL) {
774 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
779 tc_p = talloc_chunk_from_ptr(ptr);
781 if (tc_p->refs == NULL) {
782 return _talloc_free_internal(ptr);
785 new_p = talloc_parent_chunk(tc_p->refs);
787 new_parent = TC_PTR_FROM_CHUNK(new_p);
792 if (talloc_unreference(new_parent, ptr) != 0) {
796 _talloc_steal_internal(new_parent, ptr);
802 add a name to an existing pointer - va_list version
804 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
806 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
808 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
809 tc->name = talloc_vasprintf(ptr, fmt, ap);
810 if (likely(tc->name)) {
811 _talloc_set_name_const(tc->name, ".name");
817 add a name to an existing pointer
819 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
824 name = talloc_set_name_v(ptr, fmt, ap);
831 create a named talloc pointer. Any talloc pointer can be named, and
832 talloc_named() operates just like talloc() except that it allows you
835 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
841 ptr = __talloc(context, size);
842 if (unlikely(ptr == NULL)) return NULL;
845 name = talloc_set_name_v(ptr, fmt, ap);
848 if (unlikely(name == NULL)) {
849 _talloc_free_internal(ptr);
857 return the name of a talloc ptr, or "UNNAMED"
859 const char *talloc_get_name(const void *ptr)
861 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
862 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
865 if (likely(tc->name)) {
873 check if a pointer has the given name. If it does, return the pointer,
874 otherwise return NULL
876 void *talloc_check_name(const void *ptr, const char *name)
879 if (unlikely(ptr == NULL)) return NULL;
880 pname = talloc_get_name(ptr);
881 if (likely(pname == name || strcmp(pname, name) == 0)) {
882 return discard_const_p(void, ptr);
887 static void talloc_abort_type_missmatch(const char *location,
889 const char *expected)
893 reason = talloc_asprintf(NULL,
894 "%s: Type mismatch: name[%s] expected[%s]",
899 reason = "Type mismatch";
902 talloc_abort(reason);
905 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
909 if (unlikely(ptr == NULL)) {
910 talloc_abort_type_missmatch(location, NULL, name);
914 pname = talloc_get_name(ptr);
915 if (likely(pname == name || strcmp(pname, name) == 0)) {
916 return discard_const_p(void, ptr);
919 talloc_abort_type_missmatch(location, pname, name);
924 this is for compatibility with older versions of talloc
926 void *talloc_init(const char *fmt, ...)
933 * samba3 expects talloc_report_depth_cb(NULL, ...)
934 * reports all talloc'ed memory, so we need to enable
937 talloc_enable_null_tracking();
939 ptr = __talloc(NULL, 0);
940 if (unlikely(ptr == NULL)) return NULL;
943 name = talloc_set_name_v(ptr, fmt, ap);
946 if (unlikely(name == NULL)) {
947 _talloc_free_internal(ptr);
955 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
956 should probably not be used in new code. It's in here to keep the talloc
957 code consistent across Samba 3 and 4.
959 void talloc_free_children(void *ptr)
961 struct talloc_chunk *tc;
963 if (unlikely(ptr == NULL)) {
967 tc = talloc_chunk_from_ptr(ptr);
970 /* we need to work out who will own an abandoned child
971 if it cannot be freed. In priority order, the first
972 choice is owner of any remaining reference to this
973 pointer, the second choice is our parent, and the
974 final choice is the null context. */
975 void *child = TC_PTR_FROM_CHUNK(tc->child);
976 const void *new_parent = null_context;
977 if (unlikely(tc->child->refs)) {
978 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
979 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
981 if (unlikely(talloc_free(child) == -1)) {
982 if (new_parent == null_context) {
983 struct talloc_chunk *p = talloc_parent_chunk(ptr);
984 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
986 _talloc_steal_internal(new_parent, child);
990 if ((tc->flags & TALLOC_FLAG_POOL)
991 && (*talloc_pool_objectcount(tc) == 1)) {
992 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
993 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
994 VALGRIND_MAKE_MEM_NOACCESS(
995 tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
1001 Allocate a bit of memory as a child of an existing pointer
1003 void *_talloc(const void *context, size_t size)
1005 return __talloc(context, size);
1009 externally callable talloc_set_name_const()
1011 void talloc_set_name_const(const void *ptr, const char *name)
1013 _talloc_set_name_const(ptr, name);
1017 create a named talloc pointer. Any talloc pointer can be named, and
1018 talloc_named() operates just like talloc() except that it allows you
1019 to name the pointer.
1021 void *talloc_named_const(const void *context, size_t size, const char *name)
1023 return _talloc_named_const(context, size, name);
1027 free a talloc pointer. This also frees all child pointers of this
1030 return 0 if the memory is actually freed, otherwise -1. The memory
1031 will not be freed if the ref_count is > 1 or the destructor (if
1032 any) returns non-zero
1034 int _talloc_free(void *ptr, const char *location)
1036 struct talloc_chunk *tc;
1038 if (unlikely(ptr == NULL)) {
1042 tc = talloc_chunk_from_ptr(ptr);
1044 if (unlikely(tc->refs != NULL)) {
1045 struct talloc_reference_handle *h;
1046 fprintf(stderr, "ERROR: talloc_free with references at %s\n", location);
1047 for (h=tc->refs; h; h=h->next) {
1048 fprintf(stderr, "\treference at %s\n", h->location);
1053 return _talloc_free_internal(ptr);
1059 A talloc version of realloc. The context argument is only used if
1062 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1064 struct talloc_chunk *tc;
1066 bool malloced = false;
1068 /* size zero is equivalent to free() */
1069 if (unlikely(size == 0)) {
1070 talloc_unlink(context, ptr);
1074 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1078 /* realloc(NULL) is equivalent to malloc() */
1080 return _talloc_named_const(context, size, name);
1083 tc = talloc_chunk_from_ptr(ptr);
1085 /* don't allow realloc on referenced pointers */
1086 if (unlikely(tc->refs)) {
1090 /* don't let anybody try to realloc a talloc_pool */
1091 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1095 /* don't shrink if we have less than 1k to gain */
1096 if ((size < tc->size) && ((tc->size - size) < 1024)) {
1101 /* by resetting magic we catch users of the old memory */
1102 tc->flags |= TALLOC_FLAG_FREE;
1105 new_ptr = malloc(size + TC_HDR_SIZE);
1107 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
1111 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1113 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1114 *talloc_pool_objectcount((struct talloc_chunk *)
1117 if (new_ptr == NULL) {
1118 new_ptr = malloc(TC_HDR_SIZE+size);
1123 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1127 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1130 if (unlikely(!new_ptr)) {
1131 tc->flags &= ~TALLOC_FLAG_FREE;
1135 tc = (struct talloc_chunk *)new_ptr;
1136 tc->flags &= ~TALLOC_FLAG_FREE;
1138 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1141 tc->parent->child = tc;
1144 tc->child->parent = tc;
1148 tc->prev->next = tc;
1151 tc->next->prev = tc;
1155 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1157 return TC_PTR_FROM_CHUNK(tc);
1161 a wrapper around talloc_steal() for situations where you are moving a pointer
1162 between two structures, and want the old pointer to be set to NULL
1164 void *_talloc_move(const void *new_ctx, const void *_pptr)
1166 const void **pptr = discard_const_p(const void *,_pptr);
1167 void *ret = talloc_steal(new_ctx, *pptr);
1173 return the total size of a talloc pool (subtree)
1175 size_t talloc_total_size(const void *ptr)
1178 struct talloc_chunk *c, *tc;
1187 tc = talloc_chunk_from_ptr(ptr);
1189 if (tc->flags & TALLOC_FLAG_LOOP) {
1193 tc->flags |= TALLOC_FLAG_LOOP;
1196 for (c=tc->child;c;c=c->next) {
1197 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1200 tc->flags &= ~TALLOC_FLAG_LOOP;
1206 return the total number of blocks in a talloc pool (subtree)
1208 size_t talloc_total_blocks(const void *ptr)
1211 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
1213 if (tc->flags & TALLOC_FLAG_LOOP) {
1217 tc->flags |= TALLOC_FLAG_LOOP;
1220 for (c=tc->child;c;c=c->next) {
1221 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1224 tc->flags &= ~TALLOC_FLAG_LOOP;
1230 return the number of external references to a pointer
1232 size_t talloc_reference_count(const void *ptr)
1234 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1235 struct talloc_reference_handle *h;
1238 for (h=tc->refs;h;h=h->next) {
1245 report on memory usage by all children of a pointer, giving a full tree view
1247 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1248 void (*callback)(const void *ptr,
1249 int depth, int max_depth,
1251 void *private_data),
1254 struct talloc_chunk *c, *tc;
1259 if (ptr == NULL) return;
1261 tc = talloc_chunk_from_ptr(ptr);
1263 if (tc->flags & TALLOC_FLAG_LOOP) {
1267 callback(ptr, depth, max_depth, 0, private_data);
1269 if (max_depth >= 0 && depth >= max_depth) {
1273 tc->flags |= TALLOC_FLAG_LOOP;
1274 for (c=tc->child;c;c=c->next) {
1275 if (c->name == TALLOC_MAGIC_REFERENCE) {
1276 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1277 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1279 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1282 tc->flags &= ~TALLOC_FLAG_LOOP;
1285 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1287 const char *name = talloc_get_name(ptr);
1288 FILE *f = (FILE *)_f;
1291 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1296 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1297 (max_depth < 0 ? "full " :""), name,
1298 (unsigned long)talloc_total_size(ptr),
1299 (unsigned long)talloc_total_blocks(ptr));
1303 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1306 (unsigned long)talloc_total_size(ptr),
1307 (unsigned long)talloc_total_blocks(ptr),
1308 (int)talloc_reference_count(ptr), ptr);
1311 fprintf(f, "content: ");
1312 if (talloc_total_size(ptr)) {
1313 int tot = talloc_total_size(ptr);
1316 for (i = 0; i < tot; i++) {
1317 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1318 fprintf(f, "%c", ((char *)ptr)[i]);
1320 fprintf(f, "~%02x", ((char *)ptr)[i]);
1329 report on memory usage by all children of a pointer, giving a full tree view
1331 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1333 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1338 report on memory usage by all children of a pointer, giving a full tree view
1340 void talloc_report_full(const void *ptr, FILE *f)
1342 talloc_report_depth_file(ptr, 0, -1, f);
1346 report on memory usage by all children of a pointer
1348 void talloc_report(const void *ptr, FILE *f)
1350 talloc_report_depth_file(ptr, 0, 1, f);
1354 report on any memory hanging off the null context
1356 static void talloc_report_null(void)
1358 if (talloc_total_size(null_context) != 0) {
1359 talloc_report(null_context, stderr);
1364 report on any memory hanging off the null context
1366 static void talloc_report_null_full(void)
1368 if (talloc_total_size(null_context) != 0) {
1369 talloc_report_full(null_context, stderr);
1374 enable tracking of the NULL context
1376 void talloc_enable_null_tracking(void)
1378 if (null_context == NULL) {
1379 null_context = _talloc_named_const(NULL, 0, "null_context");
1384 disable tracking of the NULL context
1386 void talloc_disable_null_tracking(void)
1388 talloc_free(null_context);
1389 null_context = NULL;
1393 enable leak reporting on exit
1395 void talloc_enable_leak_report(void)
1397 talloc_enable_null_tracking();
1398 atexit(talloc_report_null);
1402 enable full leak reporting on exit
1404 void talloc_enable_leak_report_full(void)
1406 talloc_enable_null_tracking();
1407 atexit(talloc_report_null_full);
1411 talloc and zero memory.
1413 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1415 void *p = _talloc_named_const(ctx, size, name);
1418 memset(p, '\0', size);
1425 memdup with a talloc.
1427 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1429 void *newp = _talloc_named_const(t, size, name);
1432 memcpy(newp, p, size);
1438 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1442 ret = (char *)__talloc(t, len + 1);
1443 if (unlikely(!ret)) return NULL;
1445 memcpy(ret, p, len);
1448 _talloc_set_name_const(ret, ret);
1453 strdup with a talloc
1455 char *talloc_strdup(const void *t, const char *p)
1457 if (unlikely(!p)) return NULL;
1458 return __talloc_strlendup(t, p, strlen(p));
1462 strndup with a talloc
1464 char *talloc_strndup(const void *t, const char *p, size_t n)
1466 if (unlikely(!p)) return NULL;
1467 return __talloc_strlendup(t, p, strnlen(p, n));
1470 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1471 const char *a, size_t alen)
1475 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1476 if (unlikely(!ret)) return NULL;
1478 /* append the string and the trailing \0 */
1479 memcpy(&ret[slen], a, alen);
1482 _talloc_set_name_const(ret, ret);
1487 * Appends at the end of the string.
1489 char *talloc_strdup_append(char *s, const char *a)
1492 return talloc_strdup(NULL, a);
1499 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1503 * Appends at the end of the talloc'ed buffer,
1504 * not the end of the string.
1506 char *talloc_strdup_append_buffer(char *s, const char *a)
1511 return talloc_strdup(NULL, a);
1518 slen = talloc_get_size(s);
1519 if (likely(slen > 0)) {
1523 return __talloc_strlendup_append(s, slen, a, strlen(a));
1527 * Appends at the end of the string.
1529 char *talloc_strndup_append(char *s, const char *a, size_t n)
1532 return talloc_strdup(NULL, a);
1539 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1543 * Appends at the end of the talloc'ed buffer,
1544 * not the end of the string.
1546 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1551 return talloc_strdup(NULL, a);
1558 slen = talloc_get_size(s);
1559 if (likely(slen > 0)) {
1563 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1566 #ifndef HAVE_VA_COPY
1567 #ifdef HAVE___VA_COPY
1568 #define va_copy(dest, src) __va_copy(dest, src)
1570 #define va_copy(dest, src) (dest) = (src)
1574 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1581 /* this call looks strange, but it makes it work on older solaris boxes */
1583 len = vsnprintf(&c, 1, fmt, ap2);
1585 if (unlikely(len < 0)) {
1589 ret = (char *)__talloc(t, len+1);
1590 if (unlikely(!ret)) return NULL;
1593 vsnprintf(ret, len+1, fmt, ap2);
1596 _talloc_set_name_const(ret, ret);
1602 Perform string formatting, and return a pointer to newly allocated
1603 memory holding the result, inside a memory pool.
1605 char *talloc_asprintf(const void *t, const char *fmt, ...)
1611 ret = talloc_vasprintf(t, fmt, ap);
1616 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1617 const char *fmt, va_list ap)
1618 PRINTF_ATTRIBUTE(3,0);
1620 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1621 const char *fmt, va_list ap)
1628 alen = vsnprintf(&c, 1, fmt, ap2);
1632 /* Either the vsnprintf failed or the format resulted in
1633 * no characters being formatted. In the former case, we
1634 * ought to return NULL, in the latter we ought to return
1635 * the original string. Most current callers of this
1636 * function expect it to never return NULL.
1641 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1642 if (!s) return NULL;
1645 vsnprintf(s + slen, alen + 1, fmt, ap2);
1648 _talloc_set_name_const(s, s);
1653 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1654 * and return @p s, which may have moved. Good for gradually
1655 * accumulating output into a string buffer. Appends at the end
1658 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1661 return talloc_vasprintf(NULL, fmt, ap);
1664 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1668 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1669 * and return @p s, which may have moved. Always appends at the
1670 * end of the talloc'ed buffer, not the end of the string.
1672 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1677 return talloc_vasprintf(NULL, fmt, ap);
1680 slen = talloc_get_size(s);
1681 if (likely(slen > 0)) {
1685 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1689 Realloc @p s to append the formatted result of @p fmt and return @p
1690 s, which may have moved. Good for gradually accumulating output
1691 into a string buffer.
1693 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1698 s = talloc_vasprintf_append(s, fmt, ap);
1704 Realloc @p s to append the formatted result of @p fmt and return @p
1705 s, which may have moved. Good for gradually accumulating output
1708 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1713 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1719 alloc an array, checking for integer overflow in the array size
1721 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1723 if (count >= MAX_TALLOC_SIZE/el_size) {
1726 return _talloc_named_const(ctx, el_size * count, name);
1730 alloc an zero array, checking for integer overflow in the array size
1732 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1734 if (count >= MAX_TALLOC_SIZE/el_size) {
1737 return _talloc_zero(ctx, el_size * count, name);
1741 realloc an array, checking for integer overflow in the array size
1743 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1745 if (count >= MAX_TALLOC_SIZE/el_size) {
1748 return _talloc_realloc(ctx, ptr, el_size * count, name);
1752 a function version of talloc_realloc(), so it can be passed as a function pointer
1753 to libraries that want a realloc function (a realloc function encapsulates
1754 all the basic capabilities of an allocation library, which is why this is useful)
1756 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1758 return _talloc_realloc(context, ptr, size, NULL);
1762 static int talloc_autofree_destructor(void *ptr)
1764 autofree_context = NULL;
1768 static void talloc_autofree(void)
1770 talloc_free(autofree_context);
1774 return a context which will be auto-freed on exit
1775 this is useful for reducing the noise in leak reports
1777 void *talloc_autofree_context(void)
1779 if (autofree_context == NULL) {
1780 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1781 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1782 atexit(talloc_autofree);
1784 return autofree_context;
1787 size_t talloc_get_size(const void *context)
1789 struct talloc_chunk *tc;
1791 if (context == NULL)
1794 tc = talloc_chunk_from_ptr(context);
1800 find a parent of this context that has the given name, if any
1802 void *talloc_find_parent_byname(const void *context, const char *name)
1804 struct talloc_chunk *tc;
1806 if (context == NULL) {
1810 tc = talloc_chunk_from_ptr(context);
1812 if (tc->name && strcmp(tc->name, name) == 0) {
1813 return TC_PTR_FROM_CHUNK(tc);
1815 while (tc && tc->prev) tc = tc->prev;
1824 show the parentage of a context
1826 void talloc_show_parents(const void *context, FILE *file)
1828 struct talloc_chunk *tc;
1830 if (context == NULL) {
1831 fprintf(file, "talloc no parents for NULL\n");
1835 tc = talloc_chunk_from_ptr(context);
1836 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1838 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1839 while (tc && tc->prev) tc = tc->prev;
1848 return 1 if ptr is a parent of context
1850 int talloc_is_parent(const void *context, const void *ptr)
1852 struct talloc_chunk *tc;
1854 if (context == NULL) {
1858 tc = talloc_chunk_from_ptr(context);
1860 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1861 while (tc && tc->prev) tc = tc->prev;