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)
68 /* by default we abort when given a bad pointer (such as when talloc_free() is called
69 on a pointer that came from malloc() */
71 #define TALLOC_ABORT(reason) abort()
74 #ifndef discard_const_p
75 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
76 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
78 # define discard_const_p(type, ptr) ((type *)(ptr))
82 /* these macros gain us a few percent of speed on gcc */
84 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
85 as its first argument */
87 #define likely(x) __builtin_expect(!!(x), 1)
90 #define unlikely(x) __builtin_expect(!!(x), 0)
97 #define unlikely(x) (x)
101 /* this null_context is only used if talloc_enable_leak_report() or
102 talloc_enable_leak_report_full() is called, otherwise it remains
105 static void *null_context;
106 static void *autofree_context;
107 static void *no_owner_context = TALLOC_MAGIC_NO_OWNER;
108 static inline int _talloc_free(void *ptr);
110 struct talloc_reference_handle {
111 struct talloc_reference_handle *next, *prev;
115 typedef int (*talloc_destructor_t)(void *);
117 struct talloc_chunk {
118 struct talloc_chunk *next, *prev;
119 struct talloc_chunk *parent, *child;
120 struct talloc_reference_handle *refs;
121 talloc_destructor_t destructor;
127 * "pool" has dual use:
129 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
130 * marks the end of the currently allocated area.
132 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
133 * is a pointer to the struct talloc_chunk of the pool that it was
134 * allocated from. This way children can quickly find the pool to chew
140 /* 16 byte alignment seems to keep everyone happy */
141 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
142 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
144 static void (*talloc_abort_fn)(const char *reason);
146 void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
148 talloc_abort_fn = abort_fn;
151 static void talloc_abort(const char *reason)
153 if (!talloc_abort_fn) {
154 TALLOC_ABORT(reason);
157 talloc_abort_fn(reason);
160 static void talloc_abort_double_free(void)
162 talloc_abort("Bad talloc magic value - double free");
165 static void talloc_abort_no_owner_free(void)
167 talloc_abort("Bad talloc parent - no_owner free");
170 static void talloc_abort_unknown_value(void)
172 talloc_abort("Bad talloc magic value - unknown value");
175 /* panic if we get a bad magic value */
176 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
178 const char *pp = (const char *)ptr;
179 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
180 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
181 if (tc->flags & TALLOC_FLAG_FREE) {
182 talloc_abort_double_free();
185 talloc_abort_unknown_value();
192 /* hook into the front of the list */
193 #define _TLIST_ADD(list, p) \
197 (p)->next = (p)->prev = NULL; \
199 (list)->prev = (p); \
200 (p)->next = (list); \
206 /* remove an element from a list - element doesn't have to be in list. */
207 #define _TLIST_REMOVE(list, p) \
209 if ((p) == (list)) { \
210 (list) = (p)->next; \
211 if (list) (list)->prev = NULL; \
213 if ((p)->prev) (p)->prev->next = (p)->next; \
214 if ((p)->next) (p)->next->prev = (p)->prev; \
216 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
219 static inline talloc_parent_is_no_owner(const void *ptr)
221 struct talloc_chunk *tc;
222 struct talloc_chunk *no_owner_tc = TALLOC_MAGIC_NO_OWNER;
224 tc = talloc_chunk_from_ptr(ptr);
226 if (no_owner_context != TALLOC_MAGIC_NO_OWNER) {
227 no_owner_tc = talloc_chunk_from_ptr(no_owner_context);
230 if (tc->parent == no_owner_tc) {
238 return the parent chunk of a pointer
240 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
242 struct talloc_chunk *tc;
244 if (unlikely(ptr == NULL)) {
248 tc = talloc_chunk_from_ptr(ptr);
249 while (tc->prev) tc=tc->prev;
251 if (unlikely(tc->parent == TALLOC_MAGIC_NO_OWNER)) {
258 void *talloc_parent(const void *ptr)
260 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
261 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
267 const char *talloc_parent_name(const void *ptr)
269 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
270 return tc? tc->name : NULL;
274 A pool carries an in-pool object count count in the first 16 bytes.
275 bytes. This is done to support talloc_steal() to a parent outside of the
276 pool. The count includes the pool itself, so a talloc_free() on a pool will
277 only destroy the pool if the count has dropped to zero. A talloc_free() of a
278 pool member will reduce the count, and eventually also call free(3) on the
281 The object count is not put into "struct talloc_chunk" because it is only
282 relevant for talloc pools and the alignment to 16 bytes would increase the
283 memory footprint of each talloc chunk by those 16 bytes.
286 #define TALLOC_POOL_HDR_SIZE 16
288 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
290 return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
297 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
300 struct talloc_chunk *pool_ctx = NULL;
302 struct talloc_chunk *result;
305 if (parent == NULL) {
309 if (parent->flags & TALLOC_FLAG_POOL) {
312 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
313 pool_ctx = (struct talloc_chunk *)parent->pool;
316 if (pool_ctx == NULL) {
320 space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
321 - ((char *)pool_ctx->pool);
324 * Align size to 16 bytes
326 chunk_size = ((size + 15) & ~15);
328 if (space_left < chunk_size) {
332 result = (struct talloc_chunk *)pool_ctx->pool;
334 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
335 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
338 pool_ctx->pool = (void *)((char *)result + chunk_size);
340 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
341 result->pool = pool_ctx;
343 *talloc_pool_objectcount(pool_ctx) += 1;
349 Allocate a bit of memory as a child of an existing pointer
351 static inline void *__talloc(const void *context, size_t size)
353 struct talloc_chunk *tc = NULL;
355 if (unlikely(context == NULL)) {
356 context = null_context;
359 if (unlikely(size >= MAX_TALLOC_SIZE)) {
363 if (context != NULL) {
364 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
369 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
370 if (unlikely(tc == NULL)) return NULL;
371 tc->flags = TALLOC_MAGIC;
376 tc->destructor = NULL;
381 if (likely(context)) {
382 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
385 parent->child->parent = NULL;
386 tc->next = parent->child;
395 tc->next = tc->prev = tc->parent = NULL;
398 return TC_PTR_FROM_CHUNK(tc);
402 * Create a talloc pool
405 void *talloc_pool(const void *context, size_t size)
407 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
408 struct talloc_chunk *tc;
410 if (unlikely(result == NULL)) {
414 tc = talloc_chunk_from_ptr(result);
416 tc->flags |= TALLOC_FLAG_POOL;
417 tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
419 *talloc_pool_objectcount(tc) = 1;
421 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
422 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
429 setup a destructor to be called on free of a pointer
430 the destructor should return 0 on success, or -1 on failure.
431 if the destructor fails then the free is failed, and the memory can
432 be continued to be used
434 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
436 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
437 tc->destructor = destructor;
441 increase the reference count on a piece of memory.
443 int talloc_increase_ref_count(const void *ptr)
445 if (unlikely(!talloc_reference(null_context, ptr))) {
452 helper for talloc_reference()
454 this is referenced by a function pointer and should not be inline
456 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
458 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
459 _TLIST_REMOVE(ptr_tc->refs, handle);
461 * If ptr_tc has no refs left and is owner by
462 * no_owner_context then we free it
464 if (ptr_tc->refs == NULL &&
465 talloc_parent_is_no_owner(handle->ptr)) {
466 int ret = _talloc_free(handle->ptr);
468 _TLIST_ADD(ptr_tc->refs, handle);
476 more efficient way to add a name to a pointer - the name must point to a
479 static inline void _talloc_set_name_const(const void *ptr, const char *name)
481 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
486 internal talloc_named_const()
488 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
492 ptr = __talloc(context, size);
493 if (unlikely(ptr == NULL)) {
497 _talloc_set_name_const(ptr, name);
503 make a secondary reference to a pointer, hanging off the given context.
504 the pointer remains valid until both the original caller and this given
507 the major use for this is when two different structures need to reference the
508 same underlying data, and you want to be able to free the two instances separately,
511 void *_talloc_reference(const void *context, const void *ptr)
513 struct talloc_chunk *tc;
514 struct talloc_reference_handle *handle;
515 if (unlikely(ptr == NULL)) return NULL;
517 tc = talloc_chunk_from_ptr(ptr);
518 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
519 sizeof(struct talloc_reference_handle),
520 TALLOC_MAGIC_REFERENCE);
521 if (unlikely(handle == NULL)) return NULL;
523 /* note that we hang the destructor off the handle, not the
524 main context as that allows the caller to still setup their
525 own destructor on the context if they want to */
526 talloc_set_destructor(handle, talloc_reference_destructor);
527 handle->ptr = discard_const_p(void, ptr);
528 _TLIST_ADD(tc->refs, handle);
532 static inline void _talloc_free_children(struct talloc_chunk *tc)
534 void *ptr = TC_PTR_FROM_CHUNK(tc);
536 tc->flags |= TALLOC_FLAG_LOOP;
539 void *child = TC_PTR_FROM_CHUNK(tc->child);
540 if (unlikely(_talloc_free(child) == -1)) {
541 const void *new_parent = no_owner_context;
542 struct talloc_chunk *p = talloc_parent_chunk(ptr);
545 * we need to work out who will own an abandoned child
546 * if it cannot be freed. In priority order, the first
547 * choice is our parent, and the final choice is the
551 new_parent = TC_PTR_FROM_CHUNK(p);
555 * if talloc_free fails because of refs and
556 * not due to the destructor, it will already
557 * have a new owner no_owner_context
559 if (!talloc_parent_is_no_owner(child)) {
560 talloc_steal(new_parent, child);
565 tc->flags &= ~TALLOC_FLAG_LOOP;
569 internal talloc_free call
571 static inline int _talloc_free(void *ptr)
573 struct talloc_chunk *tc;
575 if (unlikely(ptr == NULL)) {
579 tc = talloc_chunk_from_ptr(ptr);
581 if (unlikely(tc->refs)) {
582 /*TODO: Fix this loop detection. The code here breaks top reference if
583 it is also one of our children. It is insufficient and can lead
584 to dangling references */
587 /* check this is a reference from a child or grantchild
588 * back to it's parent or grantparent
590 * in that case we need to remove the reference and
591 * call another instance of talloc_free() on the current
594 is_child = talloc_is_parent(tc->refs, ptr);
596 _talloc_free(tc->refs);
597 return _talloc_free(ptr);
600 * we can't free if we have refs,
601 * so no_owner_context becomes the owner
603 _talloc_steal(no_owner_context, ptr);
608 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
609 /* we have a free loop - stop looping */
613 if (unlikely(tc->destructor)) {
614 talloc_destructor_t d = tc->destructor;
615 if (d == (talloc_destructor_t)-1) {
618 tc->destructor = (talloc_destructor_t)-1;
623 tc->destructor = NULL;
627 _TLIST_REMOVE(tc->parent->child, tc);
628 if (tc->parent->child) {
629 tc->parent->child->parent = tc->parent;
632 if (tc->prev) tc->prev->next = tc->next;
633 if (tc->next) tc->next->prev = tc->prev;
636 _talloc_free_children(tc);
638 tc->flags |= TALLOC_FLAG_FREE;
640 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
641 struct talloc_chunk *pool;
642 unsigned int *pool_object_count;
644 pool = (tc->flags & TALLOC_FLAG_POOL)
645 ? tc : (struct talloc_chunk *)tc->pool;
647 pool_object_count = talloc_pool_objectcount(pool);
649 if (*pool_object_count == 0) {
650 talloc_abort("Pool object count zero!");
654 *pool_object_count -= 1;
656 if (*pool_object_count == 0) {
667 move a lump of memory from one talloc context to another return the
668 ptr on success, or NULL if it could not be transferred.
669 passing NULL as ptr will always return NULL with no side effects.
671 void *_talloc_steal(const void *new_ctx, const void *ptr)
673 struct talloc_chunk *tc, *new_tc;
675 if (unlikely(!ptr)) {
679 if (unlikely(talloc_parent_is_no_owner(ptr) &&
680 new_ctx != no_owner_context)) {
681 talloc_abort_no_owner_free();
685 if (unlikely(new_ctx == TALLOC_MAGIC_NO_OWNER)) {
689 if (unlikely(new_ctx == NULL)) {
690 new_ctx = null_context;
693 tc = talloc_chunk_from_ptr(ptr);
695 if (unlikely(new_ctx == NULL)) {
697 _TLIST_REMOVE(tc->parent->child, tc);
698 if (tc->parent->child) {
699 tc->parent->child->parent = tc->parent;
702 if (tc->prev) tc->prev->next = tc->next;
703 if (tc->next) tc->next->prev = tc->prev;
706 tc->parent = tc->next = tc->prev = NULL;
707 return discard_const_p(void, ptr);
710 new_tc = talloc_chunk_from_ptr(new_ctx);
712 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
713 return discard_const_p(void, ptr);
717 _TLIST_REMOVE(tc->parent->child, tc);
718 if (tc->parent->child) {
719 tc->parent->child->parent = tc->parent;
722 if (tc->prev) tc->prev->next = tc->next;
723 if (tc->next) tc->next->prev = tc->prev;
727 if (new_tc->child) new_tc->child->parent = NULL;
728 _TLIST_ADD(new_tc->child, tc);
730 return discard_const_p(void, ptr);
736 remove a secondary reference to a pointer. This undo's what
737 talloc_reference() has done. The context and pointer arguments
738 must match those given to a talloc_reference()
740 static inline int talloc_unreference(const void *context, const void *ptr)
742 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
743 struct talloc_reference_handle *h;
745 if (unlikely(context == NULL)) {
746 context = null_context;
749 for (h=tc->refs;h;h=h->next) {
750 struct talloc_chunk *p = talloc_parent_chunk(h);
752 if (context == NULL) break;
753 } else if (TC_PTR_FROM_CHUNK(p) == context) {
761 return _talloc_free(h);
765 remove a specific parent context from a pointer. This is a more
766 controlled varient of talloc_free()
768 int talloc_unlink(const void *context, void *ptr)
770 struct talloc_chunk *tc_p, *new_p;
777 if (context == NULL) {
778 context = null_context;
781 if (talloc_unreference(context, ptr) == 0) {
785 if (context == NULL) {
786 if (talloc_parent_chunk(ptr) != NULL) {
790 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
795 tc_p = talloc_chunk_from_ptr(ptr);
797 if (tc_p->refs == NULL) {
798 return _talloc_free(ptr);
801 talloc_steal(no_owner_context, ptr);
806 add a name to an existing pointer - va_list version
808 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
810 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
812 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
813 tc->name = talloc_vasprintf(ptr, fmt, ap);
814 if (likely(tc->name)) {
815 _talloc_set_name_const(tc->name, ".name");
821 add a name to an existing pointer
823 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
828 name = talloc_set_name_v(ptr, fmt, ap);
835 create a named talloc pointer. Any talloc pointer can be named, and
836 talloc_named() operates just like talloc() except that it allows you
839 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
845 ptr = __talloc(context, size);
846 if (unlikely(ptr == NULL)) return NULL;
849 name = talloc_set_name_v(ptr, fmt, ap);
852 if (unlikely(name == NULL)) {
861 return the name of a talloc ptr, or "UNNAMED"
863 const char *talloc_get_name(const void *ptr)
865 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
866 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
869 if (likely(tc->name)) {
877 check if a pointer has the given name. If it does, return the pointer,
878 otherwise return NULL
880 void *talloc_check_name(const void *ptr, const char *name)
883 if (unlikely(ptr == NULL)) return NULL;
884 pname = talloc_get_name(ptr);
885 if (likely(pname == name || strcmp(pname, name) == 0)) {
886 return discard_const_p(void, ptr);
891 static void talloc_abort_type_missmatch(const char *location,
893 const char *expected)
897 reason = talloc_asprintf(NULL,
898 "%s: Type mismatch: name[%s] expected[%s]",
903 reason = "Type mismatch";
906 talloc_abort(reason);
909 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
913 if (unlikely(ptr == NULL)) {
914 talloc_abort_type_missmatch(location, NULL, name);
918 pname = talloc_get_name(ptr);
919 if (likely(pname == name || strcmp(pname, name) == 0)) {
920 return discard_const_p(void, ptr);
923 talloc_abort_type_missmatch(location, pname, name);
928 this is for compatibility with older versions of talloc
930 void *talloc_init(const char *fmt, ...)
937 * samba3 expects talloc_report_depth_cb(NULL, ...)
938 * reports all talloc'ed memory, so we need to enable
941 talloc_enable_null_tracking();
943 ptr = __talloc(NULL, 0);
944 if (unlikely(ptr == NULL)) return NULL;
947 name = talloc_set_name_v(ptr, fmt, ap);
950 if (unlikely(name == NULL)) {
959 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
960 should probably not be used in new code. It's in here to keep the talloc
961 code consistent across Samba 3 and 4.
963 void talloc_free_children(void *ptr)
965 struct talloc_chunk *tc;
967 if (unlikely(ptr == NULL)) {
971 tc = talloc_chunk_from_ptr(ptr);
973 _talloc_free_children(tc);
975 if ((tc->flags & TALLOC_FLAG_POOL)
976 && (*talloc_pool_objectcount(tc) == 1)) {
977 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
978 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
979 VALGRIND_MAKE_MEM_NOACCESS(
980 tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
986 Allocate a bit of memory as a child of an existing pointer
988 void *_talloc(const void *context, size_t size)
990 return __talloc(context, size);
994 externally callable talloc_set_name_const()
996 void talloc_set_name_const(const void *ptr, const char *name)
998 _talloc_set_name_const(ptr, name);
1002 create a named talloc pointer. Any talloc pointer can be named, and
1003 talloc_named() operates just like talloc() except that it allows you
1004 to name the pointer.
1006 void *talloc_named_const(const void *context, size_t size, const char *name)
1008 return _talloc_named_const(context, size, name);
1012 free a talloc pointer. This also frees all child pointers of this
1015 return 0 if the memory is actually freed, otherwise -1. The memory
1016 will not be freed if the ref_count is > 1 or the destructor (if
1017 any) returns non-zero
1019 int talloc_free(void *ptr)
1021 struct talloc_chunk *tc;
1023 if (unlikely(ptr == NULL)) {
1027 tc = talloc_chunk_from_ptr(ptr);
1029 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
1030 /* we have a free loop - stop looping */
1034 if (unlikely(talloc_parent_is_no_owner(ptr))) {
1035 talloc_abort_no_owner_free();
1039 return _talloc_free(ptr);
1045 A talloc version of realloc. The context argument is only used if
1048 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1050 struct talloc_chunk *tc;
1052 bool malloced = false;
1054 /* size zero is equivalent to free() */
1055 if (unlikely(size == 0)) {
1060 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1064 /* realloc(NULL) is equivalent to malloc() */
1066 return _talloc_named_const(context, size, name);
1069 tc = talloc_chunk_from_ptr(ptr);
1071 /* don't allow realloc on referenced pointers */
1072 if (unlikely(tc->refs)) {
1076 /* don't let anybody try to realloc a talloc_pool */
1077 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1081 /* don't shrink if we have less than 1k to gain */
1082 if ((size < tc->size) && ((tc->size - size) < 1024)) {
1087 /* by resetting magic we catch users of the old memory */
1088 tc->flags |= TALLOC_FLAG_FREE;
1091 new_ptr = malloc(size + TC_HDR_SIZE);
1093 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
1097 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1099 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1100 *talloc_pool_objectcount((struct talloc_chunk *)
1103 if (new_ptr == NULL) {
1104 new_ptr = malloc(TC_HDR_SIZE+size);
1109 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1113 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1116 if (unlikely(!new_ptr)) {
1117 tc->flags &= ~TALLOC_FLAG_FREE;
1121 tc = (struct talloc_chunk *)new_ptr;
1122 tc->flags &= ~TALLOC_FLAG_FREE;
1124 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1127 tc->parent->child = tc;
1130 tc->child->parent = tc;
1134 tc->prev->next = tc;
1137 tc->next->prev = tc;
1141 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1143 return TC_PTR_FROM_CHUNK(tc);
1147 a wrapper around talloc_steal() for situations where you are moving a pointer
1148 between two structures, and want the old pointer to be set to NULL
1150 void *_talloc_move(const void *new_ctx, const void *_pptr)
1152 const void **pptr = discard_const_p(const void *,_pptr);
1153 void *ret = _talloc_steal(new_ctx, *pptr);
1159 return the total size of a talloc pool (subtree)
1161 size_t talloc_total_size(const void *ptr)
1164 struct talloc_chunk *c, *tc;
1173 tc = talloc_chunk_from_ptr(ptr);
1175 if (tc->flags & TALLOC_FLAG_LOOP) {
1179 tc->flags |= TALLOC_FLAG_LOOP;
1181 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1184 for (c=tc->child;c;c=c->next) {
1185 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1188 tc->flags &= ~TALLOC_FLAG_LOOP;
1194 return the total number of blocks in a talloc pool (subtree)
1196 size_t talloc_total_blocks(const void *ptr)
1199 struct talloc_chunk *c, *tc;
1208 tc = talloc_chunk_from_ptr(ptr);
1210 if (tc->flags & TALLOC_FLAG_LOOP) {
1214 tc->flags |= TALLOC_FLAG_LOOP;
1217 for (c=tc->child;c;c=c->next) {
1218 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1221 tc->flags &= ~TALLOC_FLAG_LOOP;
1227 return the number of external references to a pointer
1229 size_t talloc_reference_count(const void *ptr)
1231 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1232 struct talloc_reference_handle *h;
1235 for (h=tc->refs;h;h=h->next) {
1242 report on memory usage by all children of a pointer, giving a full tree view
1244 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1245 void (*callback)(const void *ptr,
1246 int depth, int max_depth,
1248 void *private_data),
1251 struct talloc_chunk *c, *tc;
1256 if (ptr == NULL) return;
1258 tc = talloc_chunk_from_ptr(ptr);
1260 if (tc->flags & TALLOC_FLAG_LOOP) {
1264 callback(ptr, depth, max_depth, 0, private_data);
1266 if (max_depth >= 0 && depth >= max_depth) {
1270 tc->flags |= TALLOC_FLAG_LOOP;
1271 for (c=tc->child;c;c=c->next) {
1272 if (c->name == TALLOC_MAGIC_REFERENCE) {
1273 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1274 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1276 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1279 tc->flags &= ~TALLOC_FLAG_LOOP;
1282 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1284 const char *name = talloc_get_name(ptr);
1285 FILE *f = (FILE *)_f;
1288 if (talloc_parent_is_no_owner(ptr)) {
1289 fprintf(f, "%*sreference to: %s [no owner]\n", depth*4, "", name);
1291 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1297 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1298 (max_depth < 0 ? "full " :""), name,
1299 (unsigned long)talloc_total_size(ptr),
1300 (unsigned long)talloc_total_blocks(ptr));
1304 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1307 (unsigned long)talloc_total_size(ptr),
1308 (unsigned long)talloc_total_blocks(ptr),
1309 (int)talloc_reference_count(ptr), ptr);
1312 fprintf(f, "content: ");
1313 if (talloc_total_size(ptr)) {
1314 int tot = talloc_total_size(ptr);
1317 for (i = 0; i < tot; i++) {
1318 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1319 fprintf(f, "%c", ((char *)ptr)[i]);
1321 fprintf(f, "~%02x", ((char *)ptr)[i]);
1330 report on memory usage by all children of a pointer, giving a full tree view
1332 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1334 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1339 report on memory usage by all children of a pointer, giving a full tree view
1341 void talloc_report_full(const void *ptr, FILE *f)
1343 talloc_report_depth_file(ptr, 0, -1, f);
1347 report on memory usage by all children of a pointer
1349 void talloc_report(const void *ptr, FILE *f)
1351 talloc_report_depth_file(ptr, 0, 1, f);
1355 report on any memory hanging off the null context
1357 static void talloc_report_null(void)
1359 if (talloc_total_size(null_context) != 0) {
1360 talloc_report(null_context, stderr);
1365 report on any memory hanging off the null context
1367 static void talloc_report_null_full(void)
1369 if (talloc_total_size(null_context) != 0) {
1370 talloc_report_full(null_context, stderr);
1375 enable tracking of the NULL context
1377 void talloc_enable_null_tracking(void)
1379 if (null_context == NULL) {
1380 null_context = _talloc_named_const(NULL, 0, "null_context");
1381 no_owner_context = _talloc_named_const(null_context, 0,
1382 "no_owner_context");
1387 disable tracking of the NULL context
1389 void talloc_disable_null_tracking(void)
1391 _talloc_free(null_context);
1392 null_context = NULL;
1393 no_owner_context = TALLOC_MAGIC_NO_OWNER;
1397 enable leak reporting on exit
1399 void talloc_enable_leak_report(void)
1401 talloc_enable_null_tracking();
1402 atexit(talloc_report_null);
1406 enable full leak reporting on exit
1408 void talloc_enable_leak_report_full(void)
1410 talloc_enable_null_tracking();
1411 atexit(talloc_report_null_full);
1415 talloc and zero memory.
1417 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1419 void *p = _talloc_named_const(ctx, size, name);
1422 memset(p, '\0', size);
1429 memdup with a talloc.
1431 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1433 void *newp = _talloc_named_const(t, size, name);
1436 memcpy(newp, p, size);
1442 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1446 ret = (char *)__talloc(t, len + 1);
1447 if (unlikely(!ret)) return NULL;
1449 memcpy(ret, p, len);
1452 _talloc_set_name_const(ret, ret);
1457 strdup with a talloc
1459 char *talloc_strdup(const void *t, const char *p)
1461 if (unlikely(!p)) return NULL;
1462 return __talloc_strlendup(t, p, strlen(p));
1466 strndup with a talloc
1468 char *talloc_strndup(const void *t, const char *p, size_t n)
1470 if (unlikely(!p)) return NULL;
1471 return __talloc_strlendup(t, p, strnlen(p, n));
1474 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1475 const char *a, size_t alen)
1479 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1480 if (unlikely(!ret)) return NULL;
1482 /* append the string and the trailing \0 */
1483 memcpy(&ret[slen], a, alen);
1486 _talloc_set_name_const(ret, ret);
1491 * Appends at the end of the string.
1493 char *talloc_strdup_append(char *s, const char *a)
1496 return talloc_strdup(NULL, a);
1503 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1507 * Appends at the end of the talloc'ed buffer,
1508 * not the end of the string.
1510 char *talloc_strdup_append_buffer(char *s, const char *a)
1515 return talloc_strdup(NULL, a);
1522 slen = talloc_get_size(s);
1523 if (likely(slen > 0)) {
1527 return __talloc_strlendup_append(s, slen, a, strlen(a));
1531 * Appends at the end of the string.
1533 char *talloc_strndup_append(char *s, const char *a, size_t n)
1536 return talloc_strdup(NULL, a);
1543 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1547 * Appends at the end of the talloc'ed buffer,
1548 * not the end of the string.
1550 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1555 return talloc_strdup(NULL, a);
1562 slen = talloc_get_size(s);
1563 if (likely(slen > 0)) {
1567 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1570 #ifndef HAVE_VA_COPY
1571 #ifdef HAVE___VA_COPY
1572 #define va_copy(dest, src) __va_copy(dest, src)
1574 #define va_copy(dest, src) (dest) = (src)
1578 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1585 /* this call looks strange, but it makes it work on older solaris boxes */
1587 len = vsnprintf(&c, 1, fmt, ap2);
1589 if (unlikely(len < 0)) {
1593 ret = (char *)__talloc(t, len+1);
1594 if (unlikely(!ret)) return NULL;
1597 vsnprintf(ret, len+1, fmt, ap2);
1600 _talloc_set_name_const(ret, ret);
1606 Perform string formatting, and return a pointer to newly allocated
1607 memory holding the result, inside a memory pool.
1609 char *talloc_asprintf(const void *t, const char *fmt, ...)
1615 ret = talloc_vasprintf(t, fmt, ap);
1620 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1621 const char *fmt, va_list ap)
1622 PRINTF_ATTRIBUTE(3,0);
1624 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1625 const char *fmt, va_list ap)
1632 alen = vsnprintf(&c, 1, fmt, ap2);
1636 /* Either the vsnprintf failed or the format resulted in
1637 * no characters being formatted. In the former case, we
1638 * ought to return NULL, in the latter we ought to return
1639 * the original string. Most current callers of this
1640 * function expect it to never return NULL.
1645 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1646 if (!s) return NULL;
1649 vsnprintf(s + slen, alen + 1, fmt, ap2);
1652 _talloc_set_name_const(s, s);
1657 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1658 * and return @p s, which may have moved. Good for gradually
1659 * accumulating output into a string buffer. Appends at the end
1662 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1665 return talloc_vasprintf(NULL, fmt, ap);
1668 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1672 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1673 * and return @p s, which may have moved. Always appends at the
1674 * end of the talloc'ed buffer, not the end of the string.
1676 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1681 return talloc_vasprintf(NULL, fmt, ap);
1684 slen = talloc_get_size(s);
1685 if (likely(slen > 0)) {
1689 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1693 Realloc @p s to append the formatted result of @p fmt and return @p
1694 s, which may have moved. Good for gradually accumulating output
1695 into a string buffer.
1697 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1702 s = talloc_vasprintf_append(s, fmt, ap);
1708 Realloc @p s to append the formatted result of @p fmt and return @p
1709 s, which may have moved. Good for gradually accumulating output
1712 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1717 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1723 alloc an array, checking for integer overflow in the array size
1725 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1727 if (count >= MAX_TALLOC_SIZE/el_size) {
1730 return _talloc_named_const(ctx, el_size * count, name);
1734 alloc an zero array, checking for integer overflow in the array size
1736 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1738 if (count >= MAX_TALLOC_SIZE/el_size) {
1741 return _talloc_zero(ctx, el_size * count, name);
1745 realloc an array, checking for integer overflow in the array size
1747 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1749 if (count >= MAX_TALLOC_SIZE/el_size) {
1752 return _talloc_realloc(ctx, ptr, el_size * count, name);
1756 a function version of talloc_realloc(), so it can be passed as a function pointer
1757 to libraries that want a realloc function (a realloc function encapsulates
1758 all the basic capabilities of an allocation library, which is why this is useful)
1760 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1762 return _talloc_realloc(context, ptr, size, NULL);
1766 static int talloc_autofree_destructor(void *ptr)
1768 autofree_context = NULL;
1772 static void talloc_autofree(void)
1774 _talloc_free(autofree_context);
1778 return a context which will be auto-freed on exit
1779 this is useful for reducing the noise in leak reports
1781 void *talloc_autofree_context(void)
1783 if (autofree_context == NULL) {
1784 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1785 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1786 atexit(talloc_autofree);
1788 return autofree_context;
1791 size_t talloc_get_size(const void *context)
1793 struct talloc_chunk *tc;
1795 if (context == NULL) {
1796 context = null_context;
1798 if (context == NULL) {
1802 tc = talloc_chunk_from_ptr(context);
1808 find a parent of this context that has the given name, if any
1810 void *talloc_find_parent_byname(const void *context, const char *name)
1812 struct talloc_chunk *tc;
1814 if (context == NULL) {
1818 tc = talloc_chunk_from_ptr(context);
1820 if (tc->name && strcmp(tc->name, name) == 0) {
1821 return TC_PTR_FROM_CHUNK(tc);
1823 while (tc && tc->prev) tc = tc->prev;
1832 show the parentage of a context
1834 void talloc_show_parents(const void *context, FILE *file)
1836 struct talloc_chunk *tc;
1838 if (context == NULL) {
1839 fprintf(file, "talloc no parents for NULL\n");
1843 tc = talloc_chunk_from_ptr(context);
1844 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1846 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1847 while (tc && tc->prev) tc = tc->prev;
1856 return 1 if ptr is a parent of context
1858 int talloc_is_parent(const void *context, const void *ptr)
1860 struct talloc_chunk *tc;
1862 if (context == NULL) {
1866 tc = talloc_chunk_from_ptr(context);
1868 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1869 while (tc && tc->prev) tc = tc->prev;