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)
66 #define TALLOC_MAGIC_NO_OWNER ((void *)1)
67 #define TALLOC_MAGIC_NO_OWNER_TC ((struct talloc_chunk *)1)
69 /* by default we abort when given a bad pointer (such as when talloc_free() is called
70 on a pointer that came from malloc() */
72 #define TALLOC_ABORT(reason) abort()
75 #ifndef discard_const_p
76 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
77 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
79 # define discard_const_p(type, ptr) ((type *)(ptr))
83 /* these macros gain us a few percent of speed on gcc */
85 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
86 as its first argument */
88 #define likely(x) __builtin_expect(!!(x), 1)
91 #define unlikely(x) __builtin_expect(!!(x), 0)
98 #define unlikely(x) (x)
102 /* this null_context is only used if talloc_enable_leak_report() or
103 talloc_enable_leak_report_full() is called, otherwise it remains
106 static void *null_context;
107 static void *autofree_context;
108 static void *no_owner_context = TALLOC_MAGIC_NO_OWNER;
109 static inline int _talloc_free(void *ptr);
111 struct talloc_reference_handle {
112 struct talloc_reference_handle *next, *prev;
116 typedef int (*talloc_destructor_t)(void *);
118 struct talloc_chunk {
119 struct talloc_chunk *next, *prev;
120 struct talloc_chunk *parent, *child;
121 struct talloc_reference_handle *refs;
122 talloc_destructor_t destructor;
128 * "pool" has dual use:
130 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
131 * marks the end of the currently allocated area.
133 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
134 * is a pointer to the struct talloc_chunk of the pool that it was
135 * allocated from. This way children can quickly find the pool to chew
141 /* 16 byte alignment seems to keep everyone happy */
142 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
143 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
145 static void (*talloc_abort_fn)(const char *reason);
147 void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
149 talloc_abort_fn = abort_fn;
152 static void talloc_abort(const char *reason)
154 if (!talloc_abort_fn) {
155 TALLOC_ABORT(reason);
158 talloc_abort_fn(reason);
161 static void talloc_abort_double_free(void)
163 talloc_abort("Bad talloc magic value - double free");
166 static void talloc_abort_no_owner_free(void)
168 talloc_abort("Bad talloc parent - no_owner free");
171 static void talloc_abort_unknown_value(void)
173 talloc_abort("Bad talloc magic value - unknown value");
176 /* panic if we get a bad magic value */
177 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
179 const char *pp = (const char *)ptr;
180 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
181 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
182 if (tc->flags & TALLOC_FLAG_FREE) {
183 talloc_abort_double_free();
186 talloc_abort_unknown_value();
193 /* hook into the front of the list */
194 #define _TLIST_ADD(list, p) \
198 (p)->next = (p)->prev = NULL; \
200 (list)->prev = (p); \
201 (p)->next = (list); \
207 /* remove an element from a list - element doesn't have to be in list. */
208 #define _TLIST_REMOVE(list, p) \
210 if ((p) == (list)) { \
211 (list) = (p)->next; \
212 if (list) (list)->prev = NULL; \
214 if ((p)->prev) (p)->prev->next = (p)->next; \
215 if ((p)->next) (p)->next->prev = (p)->prev; \
217 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
220 static inline bool talloc_parent_is_no_owner(const void *ptr)
222 struct talloc_chunk *tc;
223 struct talloc_chunk *no_owner_tc = TALLOC_MAGIC_NO_OWNER_TC;
225 tc = talloc_chunk_from_ptr(ptr);
227 if (no_owner_context != TALLOC_MAGIC_NO_OWNER) {
228 no_owner_tc = talloc_chunk_from_ptr(no_owner_context);
231 if (tc->parent == no_owner_tc) {
238 #define TALLOC_INVALID_PARENT(tc) \
239 ((((tc)->parent == NULL)||((tc)->parent == TALLOC_MAGIC_NO_OWNER_TC)))
242 return the parent chunk of a pointer
244 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
246 struct talloc_chunk *tc;
248 if (unlikely(ptr == NULL)) {
252 tc = talloc_chunk_from_ptr(ptr);
253 while (tc->prev) tc=tc->prev;
255 if (unlikely(TALLOC_INVALID_PARENT(tc))) {
262 void *talloc_parent(const void *ptr)
264 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
265 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
271 const char *talloc_parent_name(const void *ptr)
273 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
274 return tc? tc->name : NULL;
277 static inline void talloc_remove_from_parent(struct talloc_chunk *tc)
279 if (!TALLOC_INVALID_PARENT(tc)) {
280 _TLIST_REMOVE(tc->parent->child, tc);
281 if (tc->parent->child) {
282 tc->parent->child->parent = tc->parent;
285 if (tc->prev) tc->prev->next = tc->next;
286 if (tc->next) tc->next->prev = tc->prev;
288 // tc->parent = tc->prec = tc->next = NULL;
292 A pool carries an in-pool object count count in the first 16 bytes.
293 bytes. This is done to support talloc_steal() to a parent outside of the
294 pool. The count includes the pool itself, so a talloc_free() on a pool will
295 only destroy the pool if the count has dropped to zero. A talloc_free() of a
296 pool member will reduce the count, and eventually also call free(3) on the
299 The object count is not put into "struct talloc_chunk" because it is only
300 relevant for talloc pools and the alignment to 16 bytes would increase the
301 memory footprint of each talloc chunk by those 16 bytes.
304 #define TALLOC_POOL_HDR_SIZE 16
306 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
308 return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
315 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
318 struct talloc_chunk *pool_ctx = NULL;
320 struct talloc_chunk *result;
323 if (parent == NULL) {
327 if (parent->flags & TALLOC_FLAG_POOL) {
330 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
331 pool_ctx = (struct talloc_chunk *)parent->pool;
334 if (pool_ctx == NULL) {
338 space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
339 - ((char *)pool_ctx->pool);
342 * Align size to 16 bytes
344 chunk_size = ((size + 15) & ~15);
346 if (space_left < chunk_size) {
350 result = (struct talloc_chunk *)pool_ctx->pool;
352 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
353 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
356 pool_ctx->pool = (void *)((char *)result + chunk_size);
358 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
359 result->pool = pool_ctx;
361 *talloc_pool_objectcount(pool_ctx) += 1;
367 Allocate a bit of memory as a child of an existing pointer
369 static inline void *__talloc(const void *context, size_t size)
371 struct talloc_chunk *tc = NULL;
373 if (unlikely(context == NULL)) {
374 context = null_context;
377 if (unlikely(size >= MAX_TALLOC_SIZE)) {
381 if (context != NULL) {
382 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
387 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
388 if (unlikely(tc == NULL)) return NULL;
389 tc->flags = TALLOC_MAGIC;
394 tc->destructor = NULL;
399 if (likely(context)) {
400 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
403 parent->child->parent = NULL;
404 tc->next = parent->child;
413 tc->next = tc->prev = tc->parent = NULL;
416 return TC_PTR_FROM_CHUNK(tc);
420 * Create a talloc pool
423 void *talloc_pool(const void *context, size_t size)
425 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
426 struct talloc_chunk *tc;
428 if (unlikely(result == NULL)) {
432 tc = talloc_chunk_from_ptr(result);
434 tc->flags |= TALLOC_FLAG_POOL;
435 tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
437 *talloc_pool_objectcount(tc) = 1;
439 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
440 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
447 setup a destructor to be called on free of a pointer
448 the destructor should return 0 on success, or -1 on failure.
449 if the destructor fails then the free is failed, and the memory can
450 be continued to be used
452 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
454 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
455 tc->destructor = destructor;
459 increase the reference count on a piece of memory.
461 int talloc_increase_ref_count(const void *ptr)
463 if (unlikely(!talloc_reference(null_context, ptr))) {
470 helper for talloc_reference()
472 this is referenced by a function pointer and should not be inline
474 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
476 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
477 _TLIST_REMOVE(ptr_tc->refs, handle);
479 * If ptr_tc has no refs left and is owner by
480 * no_owner_context then we free it
482 if (ptr_tc->refs == NULL &&
483 talloc_parent_is_no_owner(handle->ptr)) {
484 int ret = _talloc_free(handle->ptr);
486 _TLIST_ADD(ptr_tc->refs, handle);
494 more efficient way to add a name to a pointer - the name must point to a
497 static inline void _talloc_set_name_const(const void *ptr, const char *name)
499 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
504 internal talloc_named_const()
506 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
510 ptr = __talloc(context, size);
511 if (unlikely(ptr == NULL)) {
515 _talloc_set_name_const(ptr, name);
521 make a secondary reference to a pointer, hanging off the given context.
522 the pointer remains valid until both the original caller and this given
525 the major use for this is when two different structures need to reference the
526 same underlying data, and you want to be able to free the two instances separately,
529 void *_talloc_reference(const void *context, const void *ptr)
531 struct talloc_chunk *tc;
532 struct talloc_reference_handle *handle;
533 if (unlikely(ptr == NULL)) return NULL;
535 tc = talloc_chunk_from_ptr(ptr);
536 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
537 sizeof(struct talloc_reference_handle),
538 TALLOC_MAGIC_REFERENCE);
539 if (unlikely(handle == NULL)) return NULL;
541 /* note that we hang the destructor off the handle, not the
542 main context as that allows the caller to still setup their
543 own destructor on the context if they want to */
544 talloc_set_destructor(handle, talloc_reference_destructor);
545 handle->ptr = discard_const_p(void, ptr);
546 _TLIST_ADD(tc->refs, handle);
550 static inline void _talloc_free_children(struct talloc_chunk *tc)
552 void *ptr = TC_PTR_FROM_CHUNK(tc);
554 tc->flags |= TALLOC_FLAG_LOOP;
557 void *child = TC_PTR_FROM_CHUNK(tc->child);
558 if (unlikely(_talloc_free(child) == -1)) {
559 const void *new_parent = no_owner_context;
560 struct talloc_chunk *p = talloc_parent_chunk(ptr);
563 * we need to work out who will own an abandoned child
564 * if it cannot be freed. In priority order, the first
565 * choice is our parent, and the final choice is the
569 new_parent = TC_PTR_FROM_CHUNK(p);
573 * if talloc_free fails because of refs and
574 * not due to the destructor, it will already
575 * have a new owner no_owner_context
577 if (!talloc_parent_is_no_owner(child)) {
578 talloc_steal(new_parent, child);
583 tc->flags &= ~TALLOC_FLAG_LOOP;
587 internal talloc_free call
589 static inline int _talloc_free(void *ptr)
591 struct talloc_chunk *tc;
593 if (unlikely(ptr == NULL)) {
597 tc = talloc_chunk_from_ptr(ptr);
599 if (unlikely(tc->refs)) {
600 /*TODO: Fix this loop detection. The code here breaks top reference if
601 it is also one of our children. It is insufficient and can lead
602 to dangling references */
605 /* check this is a reference from a child or grantchild
606 * back to it's parent or grantparent
608 * in that case we need to remove the reference and
609 * call another instance of talloc_free() on the current
612 is_child = talloc_is_parent(tc->refs, ptr);
614 _talloc_free(tc->refs);
615 return _talloc_free(ptr);
618 * we can't free if we have refs,
619 * so no_owner_context becomes the owner
621 _talloc_steal(no_owner_context, ptr);
626 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
627 /* we have a free loop - stop looping */
631 if (unlikely(tc->destructor)) {
632 talloc_destructor_t d = tc->destructor;
633 if (d == (talloc_destructor_t)-1) {
636 tc->destructor = (talloc_destructor_t)-1;
641 tc->destructor = NULL;
644 talloc_remove_from_parent(tc);
646 _talloc_free_children(tc);
648 tc->flags |= TALLOC_FLAG_FREE;
650 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
651 struct talloc_chunk *pool;
652 unsigned int *pool_object_count;
654 pool = (tc->flags & TALLOC_FLAG_POOL)
655 ? tc : (struct talloc_chunk *)tc->pool;
657 pool_object_count = talloc_pool_objectcount(pool);
659 if (*pool_object_count == 0) {
660 talloc_abort("Pool object count zero!");
664 *pool_object_count -= 1;
666 if (*pool_object_count == 0) {
677 move a lump of memory from one talloc context to another return the
678 ptr on success, or NULL if it could not be transferred.
679 passing NULL as ptr will always return NULL with no side effects.
681 void *_talloc_steal(const void *new_ctx, const void *ptr)
683 struct talloc_chunk *tc, *new_tc;
684 bool no_owner_null = false;
686 if (unlikely(!ptr)) {
690 if (unlikely(talloc_parent_is_no_owner(ptr) &&
691 new_ctx != no_owner_context)) {
692 // talloc_abort_no_owner_free();
696 if (unlikely(new_ctx == TALLOC_MAGIC_NO_OWNER)) {
697 no_owner_null = true;
701 if (unlikely(new_ctx == NULL)) {
702 new_ctx = null_context;
705 tc = talloc_chunk_from_ptr(ptr);
707 if (unlikely(new_ctx == NULL)) {
708 talloc_remove_from_parent(tc);
710 tc->parent = tc->next = tc->prev = NULL;
711 if (unlikely(no_owner_null)) {
712 tc->parent = TALLOC_MAGIC_NO_OWNER_TC;
714 return discard_const_p(void, ptr);
717 new_tc = talloc_chunk_from_ptr(new_ctx);
719 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
720 return discard_const_p(void, ptr);
723 talloc_remove_from_parent(tc);
726 if (new_tc->child) new_tc->child->parent = NULL;
727 _TLIST_ADD(new_tc->child, tc);
729 return discard_const_p(void, ptr);
735 remove a secondary reference to a pointer. This undo's what
736 talloc_reference() has done. The context and pointer arguments
737 must match those given to a talloc_reference()
739 static inline int talloc_unreference(const void *context, const void *ptr)
741 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
742 struct talloc_reference_handle *h;
744 if (unlikely(context == NULL)) {
745 context = null_context;
748 for (h=tc->refs;h;h=h->next) {
749 struct talloc_chunk *p = talloc_parent_chunk(h);
751 if (context == NULL) break;
752 } else if (TC_PTR_FROM_CHUNK(p) == context) {
760 return _talloc_free(h);
764 remove a specific parent context from a pointer. This is a more
765 controlled varient of talloc_free()
767 int talloc_unlink(const void *context, void *ptr)
769 struct talloc_chunk *tc_p;
775 if (context == NULL) {
776 context = null_context;
779 if (talloc_unreference(context, ptr) == 0) {
783 if (context == NULL) {
784 if (talloc_parent_chunk(ptr) != NULL) {
788 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
793 tc_p = talloc_chunk_from_ptr(ptr);
795 if (tc_p->refs == NULL) {
796 return _talloc_free(ptr);
799 talloc_steal(no_owner_context, ptr);
804 add a name to an existing pointer - va_list version
806 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
808 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
810 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
811 tc->name = talloc_vasprintf(ptr, fmt, ap);
812 if (likely(tc->name)) {
813 _talloc_set_name_const(tc->name, ".name");
819 add a name to an existing pointer
821 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
826 name = talloc_set_name_v(ptr, fmt, ap);
833 create a named talloc pointer. Any talloc pointer can be named, and
834 talloc_named() operates just like talloc() except that it allows you
837 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
843 ptr = __talloc(context, size);
844 if (unlikely(ptr == NULL)) return NULL;
847 name = talloc_set_name_v(ptr, fmt, ap);
850 if (unlikely(name == NULL)) {
859 return the name of a talloc ptr, or "UNNAMED"
861 const char *talloc_get_name(const void *ptr)
863 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
864 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
867 if (likely(tc->name)) {
875 check if a pointer has the given name. If it does, return the pointer,
876 otherwise return NULL
878 void *talloc_check_name(const void *ptr, const char *name)
881 if (unlikely(ptr == NULL)) return NULL;
882 pname = talloc_get_name(ptr);
883 if (likely(pname == name || strcmp(pname, name) == 0)) {
884 return discard_const_p(void, ptr);
889 static void talloc_abort_type_missmatch(const char *location,
891 const char *expected)
895 reason = talloc_asprintf(NULL,
896 "%s: Type mismatch: name[%s] expected[%s]",
901 reason = "Type mismatch";
904 talloc_abort(reason);
907 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
911 if (unlikely(ptr == NULL)) {
912 talloc_abort_type_missmatch(location, NULL, name);
916 pname = talloc_get_name(ptr);
917 if (likely(pname == name || strcmp(pname, name) == 0)) {
918 return discard_const_p(void, ptr);
921 talloc_abort_type_missmatch(location, pname, name);
926 this is for compatibility with older versions of talloc
928 void *talloc_init(const char *fmt, ...)
935 * samba3 expects talloc_report_depth_cb(NULL, ...)
936 * reports all talloc'ed memory, so we need to enable
939 talloc_enable_null_tracking();
941 ptr = __talloc(NULL, 0);
942 if (unlikely(ptr == NULL)) return NULL;
945 name = talloc_set_name_v(ptr, fmt, ap);
948 if (unlikely(name == NULL)) {
957 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
958 should probably not be used in new code. It's in here to keep the talloc
959 code consistent across Samba 3 and 4.
961 void talloc_free_children(void *ptr)
963 struct talloc_chunk *tc;
965 if (unlikely(ptr == NULL)) {
969 tc = talloc_chunk_from_ptr(ptr);
971 _talloc_free_children(tc);
973 if ((tc->flags & TALLOC_FLAG_POOL)
974 && (*talloc_pool_objectcount(tc) == 1)) {
975 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
976 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
977 VALGRIND_MAKE_MEM_NOACCESS(
978 tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
984 Allocate a bit of memory as a child of an existing pointer
986 void *_talloc(const void *context, size_t size)
988 return __talloc(context, size);
992 externally callable talloc_set_name_const()
994 void talloc_set_name_const(const void *ptr, const char *name)
996 _talloc_set_name_const(ptr, name);
1000 create a named talloc pointer. Any talloc pointer can be named, and
1001 talloc_named() operates just like talloc() except that it allows you
1002 to name the pointer.
1004 void *talloc_named_const(const void *context, size_t size, const char *name)
1006 return _talloc_named_const(context, size, name);
1010 free a talloc pointer. This also frees all child pointers of this
1013 return 0 if the memory is actually freed, otherwise -1. The memory
1014 will not be freed if the ref_count is > 1 or the destructor (if
1015 any) returns non-zero
1017 int talloc_free(void *ptr)
1019 struct talloc_chunk *tc;
1021 if (unlikely(ptr == NULL)) {
1025 tc = talloc_chunk_from_ptr(ptr);
1027 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
1028 /* we have a free loop - stop looping */
1032 if (unlikely(talloc_parent_is_no_owner(ptr))) {
1033 talloc_abort_no_owner_free();
1037 return _talloc_free(ptr);
1043 A talloc version of realloc. The context argument is only used if
1046 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1048 struct talloc_chunk *tc;
1050 bool malloced = false;
1052 /* size zero is equivalent to free() */
1053 if (unlikely(size == 0)) {
1058 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1062 /* realloc(NULL) is equivalent to malloc() */
1064 return _talloc_named_const(context, size, name);
1067 tc = talloc_chunk_from_ptr(ptr);
1069 /* don't allow realloc on referenced pointers */
1070 if (unlikely(tc->refs)) {
1074 /* don't let anybody try to realloc a talloc_pool */
1075 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1079 /* don't shrink if we have less than 1k to gain */
1080 if ((size < tc->size) && ((tc->size - size) < 1024)) {
1085 /* by resetting magic we catch users of the old memory */
1086 tc->flags |= TALLOC_FLAG_FREE;
1089 new_ptr = malloc(size + TC_HDR_SIZE);
1091 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
1095 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1097 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1098 *talloc_pool_objectcount((struct talloc_chunk *)
1101 if (new_ptr == NULL) {
1102 new_ptr = malloc(TC_HDR_SIZE+size);
1107 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1111 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1114 if (unlikely(!new_ptr)) {
1115 tc->flags &= ~TALLOC_FLAG_FREE;
1119 tc = (struct talloc_chunk *)new_ptr;
1120 tc->flags &= ~TALLOC_FLAG_FREE;
1122 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1124 if (!TALLOC_INVALID_PARENT(tc)) {
1125 tc->parent->child = tc;
1128 tc->child->parent = tc;
1132 tc->prev->next = tc;
1135 tc->next->prev = tc;
1139 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1141 return TC_PTR_FROM_CHUNK(tc);
1145 a wrapper around talloc_steal() for situations where you are moving a pointer
1146 between two structures, and want the old pointer to be set to NULL
1148 void *_talloc_move(const void *new_ctx, const void *_pptr)
1150 const void **pptr = discard_const_p(const void *,_pptr);
1151 void *ret = _talloc_steal(new_ctx, *pptr);
1157 return the total size of a talloc pool (subtree)
1159 size_t talloc_total_size(const void *ptr)
1162 struct talloc_chunk *c, *tc;
1171 tc = talloc_chunk_from_ptr(ptr);
1173 if (tc->flags & TALLOC_FLAG_LOOP) {
1177 tc->flags |= TALLOC_FLAG_LOOP;
1179 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1182 for (c=tc->child;c;c=c->next) {
1183 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1186 tc->flags &= ~TALLOC_FLAG_LOOP;
1192 return the total number of blocks in a talloc pool (subtree)
1194 size_t talloc_total_blocks(const void *ptr)
1197 struct talloc_chunk *c, *tc;
1206 tc = talloc_chunk_from_ptr(ptr);
1208 if (tc->flags & TALLOC_FLAG_LOOP) {
1212 tc->flags |= TALLOC_FLAG_LOOP;
1215 for (c=tc->child;c;c=c->next) {
1216 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1219 tc->flags &= ~TALLOC_FLAG_LOOP;
1225 return the number of external references to a pointer
1227 size_t talloc_reference_count(const void *ptr)
1229 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1230 struct talloc_reference_handle *h;
1233 for (h=tc->refs;h;h=h->next) {
1240 report on memory usage by all children of a pointer, giving a full tree view
1242 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1243 void (*callback)(const void *ptr,
1244 int depth, int max_depth,
1246 void *private_data),
1249 struct talloc_chunk *c, *tc;
1254 if (ptr == NULL) return;
1256 tc = talloc_chunk_from_ptr(ptr);
1258 if (tc->flags & TALLOC_FLAG_LOOP) {
1262 callback(ptr, depth, max_depth, 0, private_data);
1264 if (max_depth >= 0 && depth >= max_depth) {
1268 tc->flags |= TALLOC_FLAG_LOOP;
1269 for (c=tc->child;c;c=c->next) {
1270 if (c->name == TALLOC_MAGIC_REFERENCE) {
1271 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1272 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1274 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1277 tc->flags &= ~TALLOC_FLAG_LOOP;
1280 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1282 const char *name = talloc_get_name(ptr);
1283 FILE *f = (FILE *)_f;
1286 if (talloc_parent_is_no_owner(ptr)) {
1287 fprintf(f, "%*sreference to: %s [no owner]\n", depth*4, "", name);
1289 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1295 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1296 (max_depth < 0 ? "full " :""), name,
1297 (unsigned long)talloc_total_size(ptr),
1298 (unsigned long)talloc_total_blocks(ptr));
1302 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1305 (unsigned long)talloc_total_size(ptr),
1306 (unsigned long)talloc_total_blocks(ptr),
1307 (int)talloc_reference_count(ptr), ptr);
1310 fprintf(f, "content: ");
1311 if (talloc_total_size(ptr)) {
1312 int tot = talloc_total_size(ptr);
1315 for (i = 0; i < tot; i++) {
1316 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1317 fprintf(f, "%c", ((char *)ptr)[i]);
1319 fprintf(f, "~%02x", ((char *)ptr)[i]);
1328 report on memory usage by all children of a pointer, giving a full tree view
1330 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1332 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1337 report on memory usage by all children of a pointer, giving a full tree view
1339 void talloc_report_full(const void *ptr, FILE *f)
1341 talloc_report_depth_file(ptr, 0, -1, f);
1345 report on memory usage by all children of a pointer
1347 void talloc_report(const void *ptr, FILE *f)
1349 talloc_report_depth_file(ptr, 0, 1, f);
1353 report on any memory hanging off the null context
1355 static void talloc_report_null(void)
1357 if (talloc_total_size(null_context) != 0) {
1358 talloc_report(null_context, stderr);
1363 report on any memory hanging off the null context
1365 static void talloc_report_null_full(void)
1367 if (talloc_total_size(null_context) != 0) {
1368 talloc_report_full(null_context, stderr);
1373 enable tracking of the NULL context
1375 void talloc_enable_null_tracking(void)
1377 if (null_context == NULL) {
1378 null_context = _talloc_named_const(NULL, 0, "null_context");
1379 no_owner_context = _talloc_named_const(null_context, 0,
1380 "no_owner_context");
1385 disable tracking of the NULL context
1387 void talloc_disable_null_tracking(void)
1389 _talloc_free(null_context);
1390 null_context = NULL;
1391 no_owner_context = TALLOC_MAGIC_NO_OWNER;
1395 enable leak reporting on exit
1397 void talloc_enable_leak_report(void)
1399 talloc_enable_null_tracking();
1400 atexit(talloc_report_null);
1404 enable full leak reporting on exit
1406 void talloc_enable_leak_report_full(void)
1408 talloc_enable_null_tracking();
1409 atexit(talloc_report_null_full);
1413 talloc and zero memory.
1415 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1417 void *p = _talloc_named_const(ctx, size, name);
1420 memset(p, '\0', size);
1427 memdup with a talloc.
1429 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1431 void *newp = _talloc_named_const(t, size, name);
1434 memcpy(newp, p, size);
1440 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1444 ret = (char *)__talloc(t, len + 1);
1445 if (unlikely(!ret)) return NULL;
1447 memcpy(ret, p, len);
1450 _talloc_set_name_const(ret, ret);
1455 strdup with a talloc
1457 char *talloc_strdup(const void *t, const char *p)
1459 if (unlikely(!p)) return NULL;
1460 return __talloc_strlendup(t, p, strlen(p));
1464 strndup with a talloc
1466 char *talloc_strndup(const void *t, const char *p, size_t n)
1468 if (unlikely(!p)) return NULL;
1469 return __talloc_strlendup(t, p, strnlen(p, n));
1472 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1473 const char *a, size_t alen)
1477 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1478 if (unlikely(!ret)) return NULL;
1480 /* append the string and the trailing \0 */
1481 memcpy(&ret[slen], a, alen);
1484 _talloc_set_name_const(ret, ret);
1489 * Appends at the end of the string.
1491 char *talloc_strdup_append(char *s, const char *a)
1494 return talloc_strdup(NULL, a);
1501 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1505 * Appends at the end of the talloc'ed buffer,
1506 * not the end of the string.
1508 char *talloc_strdup_append_buffer(char *s, const char *a)
1513 return talloc_strdup(NULL, a);
1520 slen = talloc_get_size(s);
1521 if (likely(slen > 0)) {
1525 return __talloc_strlendup_append(s, slen, a, strlen(a));
1529 * Appends at the end of the string.
1531 char *talloc_strndup_append(char *s, const char *a, size_t n)
1534 return talloc_strdup(NULL, a);
1541 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1545 * Appends at the end of the talloc'ed buffer,
1546 * not the end of the string.
1548 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1553 return talloc_strdup(NULL, a);
1560 slen = talloc_get_size(s);
1561 if (likely(slen > 0)) {
1565 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1568 #ifndef HAVE_VA_COPY
1569 #ifdef HAVE___VA_COPY
1570 #define va_copy(dest, src) __va_copy(dest, src)
1572 #define va_copy(dest, src) (dest) = (src)
1576 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1583 /* this call looks strange, but it makes it work on older solaris boxes */
1585 len = vsnprintf(&c, 1, fmt, ap2);
1587 if (unlikely(len < 0)) {
1591 ret = (char *)__talloc(t, len+1);
1592 if (unlikely(!ret)) return NULL;
1595 vsnprintf(ret, len+1, fmt, ap2);
1598 _talloc_set_name_const(ret, ret);
1604 Perform string formatting, and return a pointer to newly allocated
1605 memory holding the result, inside a memory pool.
1607 char *talloc_asprintf(const void *t, const char *fmt, ...)
1613 ret = talloc_vasprintf(t, fmt, ap);
1618 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1619 const char *fmt, va_list ap)
1620 PRINTF_ATTRIBUTE(3,0);
1622 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1623 const char *fmt, va_list ap)
1630 alen = vsnprintf(&c, 1, fmt, ap2);
1634 /* Either the vsnprintf failed or the format resulted in
1635 * no characters being formatted. In the former case, we
1636 * ought to return NULL, in the latter we ought to return
1637 * the original string. Most current callers of this
1638 * function expect it to never return NULL.
1643 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1644 if (!s) return NULL;
1647 vsnprintf(s + slen, alen + 1, fmt, ap2);
1650 _talloc_set_name_const(s, s);
1655 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1656 * and return @p s, which may have moved. Good for gradually
1657 * accumulating output into a string buffer. Appends at the end
1660 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1663 return talloc_vasprintf(NULL, fmt, ap);
1666 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1670 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1671 * and return @p s, which may have moved. Always appends at the
1672 * end of the talloc'ed buffer, not the end of the string.
1674 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1679 return talloc_vasprintf(NULL, fmt, ap);
1682 slen = talloc_get_size(s);
1683 if (likely(slen > 0)) {
1687 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1691 Realloc @p s to append the formatted result of @p fmt and return @p
1692 s, which may have moved. Good for gradually accumulating output
1693 into a string buffer.
1695 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1700 s = talloc_vasprintf_append(s, fmt, ap);
1706 Realloc @p s to append the formatted result of @p fmt and return @p
1707 s, which may have moved. Good for gradually accumulating output
1710 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1715 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1721 alloc an array, checking for integer overflow in the array size
1723 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1725 if (count >= MAX_TALLOC_SIZE/el_size) {
1728 return _talloc_named_const(ctx, el_size * count, name);
1732 alloc an zero array, checking for integer overflow in the array size
1734 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1736 if (count >= MAX_TALLOC_SIZE/el_size) {
1739 return _talloc_zero(ctx, el_size * count, name);
1743 realloc an array, checking for integer overflow in the array size
1745 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1747 if (count >= MAX_TALLOC_SIZE/el_size) {
1750 return _talloc_realloc(ctx, ptr, el_size * count, name);
1754 a function version of talloc_realloc(), so it can be passed as a function pointer
1755 to libraries that want a realloc function (a realloc function encapsulates
1756 all the basic capabilities of an allocation library, which is why this is useful)
1758 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1760 return _talloc_realloc(context, ptr, size, NULL);
1764 static int talloc_autofree_destructor(void *ptr)
1766 autofree_context = NULL;
1770 static void talloc_autofree(void)
1772 _talloc_free(autofree_context);
1776 return a context which will be auto-freed on exit
1777 this is useful for reducing the noise in leak reports
1779 void *talloc_autofree_context(void)
1781 if (autofree_context == NULL) {
1782 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1783 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1784 atexit(talloc_autofree);
1786 return autofree_context;
1789 size_t talloc_get_size(const void *context)
1791 struct talloc_chunk *tc;
1793 if (context == NULL) {
1794 context = null_context;
1796 if (context == NULL) {
1800 tc = talloc_chunk_from_ptr(context);
1806 find a parent of this context that has the given name, if any
1808 void *talloc_find_parent_byname(const void *context, const char *name)
1810 struct talloc_chunk *tc;
1812 if (context == NULL) {
1816 tc = talloc_chunk_from_ptr(context);
1818 if (tc->name && strcmp(tc->name, name) == 0) {
1819 return TC_PTR_FROM_CHUNK(tc);
1821 while (tc && tc->prev) tc = tc->prev;
1823 if (TALLOC_INVALID_PARENT(tc)) {
1833 show the parentage of a context
1835 void talloc_show_parents(const void *context, FILE *file)
1837 struct talloc_chunk *tc;
1839 if (context == NULL) {
1840 fprintf(file, "talloc no parents for NULL\n");
1844 tc = talloc_chunk_from_ptr(context);
1845 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1847 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1848 while (tc && tc->prev) tc = tc->prev;
1850 if (tc->parent == TALLOC_MAGIC_NO_OWNER_TC) {
1851 fprintf(file, "\t'no_owner_context'\n");
1861 return 1 if ptr is a parent of context
1863 int talloc_is_parent(const void *context, const void *ptr)
1865 struct talloc_chunk *tc;
1867 if (context == NULL) {
1871 tc = talloc_chunk_from_ptr(context);
1873 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1874 while (tc && tc->prev) tc = tc->prev;
1876 if (TALLOC_INVALID_PARENT(tc)) {