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
50 //#include "replace.h"
51 #include <sys/types.h>
58 #include <osmocore/talloc.h>
59 #define MIN(x,y) ((x) < (y) ? (x) : (y))
60 #endif /* not _TALLOC_SAMBA3 */
62 /* use this to force every realloc to change the pointer, to stress test
63 code that might not cope */
64 #define ALWAYS_REALLOC 0
67 #define MAX_TALLOC_SIZE 0x10000000
68 #define TALLOC_MAGIC 0xe814ec70
69 #define TALLOC_FLAG_FREE 0x01
70 #define TALLOC_FLAG_LOOP 0x02
71 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
72 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
73 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
75 /* by default we abort when given a bad pointer (such as when talloc_free() is called
76 on a pointer that came from malloc() */
78 #define TALLOC_ABORT(reason) abort()
81 #ifndef discard_const_p
82 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
83 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
85 # define discard_const_p(type, ptr) ((type *)(ptr))
89 /* these macros gain us a few percent of speed on gcc */
91 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
92 as its first argument */
94 #define likely(x) __builtin_expect(!!(x), 1)
97 #define unlikely(x) __builtin_expect(!!(x), 0)
101 #define likely(x) (x)
104 #define unlikely(x) (x)
109 /* taken from http://insanecoding.blogspot.com/2007/03/methods-for-safe-string-handling.html */
110 size_t strnlen(const char *s, size_t n)
112 const char *p = (const char *)memchr(s, 0, n);
117 /* this null_context is only used if talloc_enable_leak_report() or
118 talloc_enable_leak_report_full() is called, otherwise it remains
121 static void *null_context;
122 static void *autofree_context;
124 struct talloc_reference_handle {
125 struct talloc_reference_handle *next, *prev;
129 typedef int (*talloc_destructor_t)(void *);
131 struct talloc_chunk {
132 struct talloc_chunk *next, *prev;
133 struct talloc_chunk *parent, *child;
134 struct talloc_reference_handle *refs;
135 talloc_destructor_t destructor;
141 * "pool" has dual use:
143 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
144 * marks the end of the currently allocated area.
146 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
147 * is a pointer to the struct talloc_chunk of the pool that it was
148 * allocated from. This way children can quickly find the pool to chew
154 /* 16 byte alignment seems to keep everyone happy */
155 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
156 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
158 static void (*talloc_abort_fn)(const char *reason);
160 void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
162 talloc_abort_fn = abort_fn;
165 static void talloc_abort(const char *reason)
167 if (!talloc_abort_fn) {
168 TALLOC_ABORT(reason);
171 talloc_abort_fn(reason);
174 static void talloc_abort_double_free(void)
176 talloc_abort("Bad talloc magic value - double free");
179 static void talloc_abort_unknown_value(void)
181 talloc_abort("Bad talloc magic value - unknown value");
184 /* panic if we get a bad magic value */
185 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
187 const char *pp = (const char *)ptr;
188 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
189 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
190 if (tc->flags & TALLOC_FLAG_FREE) {
191 talloc_abort_double_free();
193 talloc_abort_unknown_value();
199 /* hook into the front of the list */
200 #define _TLIST_ADD(list, p) \
204 (p)->next = (p)->prev = NULL; \
206 (list)->prev = (p); \
207 (p)->next = (list); \
213 /* remove an element from a list - element doesn't have to be in list. */
214 #define _TLIST_REMOVE(list, p) \
216 if ((p) == (list)) { \
217 (list) = (p)->next; \
218 if (list) (list)->prev = NULL; \
220 if ((p)->prev) (p)->prev->next = (p)->next; \
221 if ((p)->next) (p)->next->prev = (p)->prev; \
223 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
228 return the parent chunk of a pointer
230 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
232 struct talloc_chunk *tc;
234 if (unlikely(ptr == NULL)) {
238 tc = talloc_chunk_from_ptr(ptr);
239 while (tc->prev) tc=tc->prev;
244 void *talloc_parent(const void *ptr)
246 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
247 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
253 const char *talloc_parent_name(const void *ptr)
255 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
256 return tc? tc->name : NULL;
260 A pool carries an in-pool object count count in the first 16 bytes.
261 bytes. This is done to support talloc_steal() to a parent outside of the
262 pool. The count includes the pool itself, so a talloc_free() on a pool will
263 only destroy the pool if the count has dropped to zero. A talloc_free() of a
264 pool member will reduce the count, and eventually also call free(3) on the
267 The object count is not put into "struct talloc_chunk" because it is only
268 relevant for talloc pools and the alignment to 16 bytes would increase the
269 memory footprint of each talloc chunk by those 16 bytes.
272 #define TALLOC_POOL_HDR_SIZE 16
274 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
276 return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
283 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
286 struct talloc_chunk *pool_ctx = NULL;
288 struct talloc_chunk *result;
291 if (parent == NULL) {
295 if (parent->flags & TALLOC_FLAG_POOL) {
298 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
299 pool_ctx = (struct talloc_chunk *)parent->pool;
302 if (pool_ctx == NULL) {
306 space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
307 - ((char *)pool_ctx->pool);
310 * Align size to 16 bytes
312 chunk_size = ((size + 15) & ~15);
314 if (space_left < chunk_size) {
318 result = (struct talloc_chunk *)pool_ctx->pool;
320 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
321 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
324 pool_ctx->pool = (void *)((char *)result + chunk_size);
326 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
327 result->pool = pool_ctx;
329 *talloc_pool_objectcount(pool_ctx) += 1;
335 Allocate a bit of memory as a child of an existing pointer
337 static inline void *__talloc(const void *context, size_t size)
339 struct talloc_chunk *tc = NULL;
341 if (unlikely(context == NULL)) {
342 context = null_context;
345 if (unlikely(size >= MAX_TALLOC_SIZE)) {
349 if (context != NULL) {
350 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
355 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
356 if (unlikely(tc == NULL)) return NULL;
357 tc->flags = TALLOC_MAGIC;
362 tc->destructor = NULL;
367 if (likely(context)) {
368 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
371 parent->child->parent = NULL;
372 tc->next = parent->child;
381 tc->next = tc->prev = tc->parent = NULL;
384 return TC_PTR_FROM_CHUNK(tc);
388 * Create a talloc pool
391 void *talloc_pool(const void *context, size_t size)
393 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
394 struct talloc_chunk *tc;
396 if (unlikely(result == NULL)) {
400 tc = talloc_chunk_from_ptr(result);
402 tc->flags |= TALLOC_FLAG_POOL;
403 tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
405 *talloc_pool_objectcount(tc) = 1;
407 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
408 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
415 setup a destructor to be called on free of a pointer
416 the destructor should return 0 on success, or -1 on failure.
417 if the destructor fails then the free is failed, and the memory can
418 be continued to be used
420 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
422 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
423 tc->destructor = destructor;
427 increase the reference count on a piece of memory.
429 int talloc_increase_ref_count(const void *ptr)
431 if (unlikely(!talloc_reference(null_context, ptr))) {
438 helper for talloc_reference()
440 this is referenced by a function pointer and should not be inline
442 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
444 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
445 _TLIST_REMOVE(ptr_tc->refs, handle);
450 more efficient way to add a name to a pointer - the name must point to a
453 static inline void _talloc_set_name_const(const void *ptr, const char *name)
455 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
460 internal talloc_named_const()
462 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
466 ptr = __talloc(context, size);
467 if (unlikely(ptr == NULL)) {
471 _talloc_set_name_const(ptr, name);
477 make a secondary reference to a pointer, hanging off the given context.
478 the pointer remains valid until both the original caller and this given
481 the major use for this is when two different structures need to reference the
482 same underlying data, and you want to be able to free the two instances separately,
485 void *_talloc_reference(const void *context, const void *ptr)
487 struct talloc_chunk *tc;
488 struct talloc_reference_handle *handle;
489 if (unlikely(ptr == NULL)) return NULL;
491 tc = talloc_chunk_from_ptr(ptr);
492 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
493 sizeof(struct talloc_reference_handle),
494 TALLOC_MAGIC_REFERENCE);
495 if (unlikely(handle == NULL)) return NULL;
497 /* note that we hang the destructor off the handle, not the
498 main context as that allows the caller to still setup their
499 own destructor on the context if they want to */
500 talloc_set_destructor(handle, talloc_reference_destructor);
501 handle->ptr = discard_const_p(void, ptr);
502 _TLIST_ADD(tc->refs, handle);
508 internal talloc_free call
510 static inline int _talloc_free(void *ptr)
512 struct talloc_chunk *tc;
514 if (unlikely(ptr == NULL)) {
518 tc = talloc_chunk_from_ptr(ptr);
520 if (unlikely(tc->refs)) {
522 /* check this is a reference from a child or grantchild
523 * back to it's parent or grantparent
525 * in that case we need to remove the reference and
526 * call another instance of talloc_free() on the current
529 is_child = talloc_is_parent(tc->refs, ptr);
530 _talloc_free(tc->refs);
532 return _talloc_free(ptr);
537 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
538 /* we have a free loop - stop looping */
542 if (unlikely(tc->destructor)) {
543 talloc_destructor_t d = tc->destructor;
544 if (d == (talloc_destructor_t)-1) {
547 tc->destructor = (talloc_destructor_t)-1;
552 tc->destructor = NULL;
556 _TLIST_REMOVE(tc->parent->child, tc);
557 if (tc->parent->child) {
558 tc->parent->child->parent = tc->parent;
561 if (tc->prev) tc->prev->next = tc->next;
562 if (tc->next) tc->next->prev = tc->prev;
565 tc->flags |= TALLOC_FLAG_LOOP;
568 /* we need to work out who will own an abandoned child
569 if it cannot be freed. In priority order, the first
570 choice is owner of any remaining reference to this
571 pointer, the second choice is our parent, and the
572 final choice is the null context. */
573 void *child = TC_PTR_FROM_CHUNK(tc->child);
574 const void *new_parent = null_context;
575 if (unlikely(tc->child->refs)) {
576 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
577 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
579 if (unlikely(_talloc_free(child) == -1)) {
580 if (new_parent == null_context) {
581 struct talloc_chunk *p = talloc_parent_chunk(ptr);
582 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
584 talloc_steal(new_parent, child);
588 tc->flags |= TALLOC_FLAG_FREE;
590 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
591 struct talloc_chunk *pool;
592 unsigned int *pool_object_count;
594 pool = (tc->flags & TALLOC_FLAG_POOL)
595 ? tc : (struct talloc_chunk *)tc->pool;
597 pool_object_count = talloc_pool_objectcount(pool);
599 if (*pool_object_count == 0) {
600 talloc_abort("Pool object count zero!");
603 *pool_object_count -= 1;
605 if (*pool_object_count == 0) {
616 move a lump of memory from one talloc context to another return the
617 ptr on success, or NULL if it could not be transferred.
618 passing NULL as ptr will always return NULL with no side effects.
620 void *_talloc_steal(const void *new_ctx, const void *ptr)
622 struct talloc_chunk *tc, *new_tc;
624 if (unlikely(!ptr)) {
628 if (unlikely(new_ctx == NULL)) {
629 new_ctx = null_context;
632 tc = talloc_chunk_from_ptr(ptr);
634 if (unlikely(new_ctx == NULL)) {
636 _TLIST_REMOVE(tc->parent->child, tc);
637 if (tc->parent->child) {
638 tc->parent->child->parent = tc->parent;
641 if (tc->prev) tc->prev->next = tc->next;
642 if (tc->next) tc->next->prev = tc->prev;
645 tc->parent = tc->next = tc->prev = NULL;
646 return discard_const_p(void, ptr);
649 new_tc = talloc_chunk_from_ptr(new_ctx);
651 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
652 return discard_const_p(void, ptr);
656 _TLIST_REMOVE(tc->parent->child, tc);
657 if (tc->parent->child) {
658 tc->parent->child->parent = tc->parent;
661 if (tc->prev) tc->prev->next = tc->next;
662 if (tc->next) tc->next->prev = tc->prev;
666 if (new_tc->child) new_tc->child->parent = NULL;
667 _TLIST_ADD(new_tc->child, tc);
669 return discard_const_p(void, ptr);
675 remove a secondary reference to a pointer. This undo's what
676 talloc_reference() has done. The context and pointer arguments
677 must match those given to a talloc_reference()
679 static inline int talloc_unreference(const void *context, const void *ptr)
681 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
682 struct talloc_reference_handle *h;
684 if (unlikely(context == NULL)) {
685 context = null_context;
688 for (h=tc->refs;h;h=h->next) {
689 struct talloc_chunk *p = talloc_parent_chunk(h);
691 if (context == NULL) break;
692 } else if (TC_PTR_FROM_CHUNK(p) == context) {
700 return _talloc_free(h);
704 remove a specific parent context from a pointer. This is a more
705 controlled varient of talloc_free()
707 int talloc_unlink(const void *context, void *ptr)
709 struct talloc_chunk *tc_p, *new_p;
716 if (context == NULL) {
717 context = null_context;
720 if (talloc_unreference(context, ptr) == 0) {
724 if (context == NULL) {
725 if (talloc_parent_chunk(ptr) != NULL) {
729 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
734 tc_p = talloc_chunk_from_ptr(ptr);
736 if (tc_p->refs == NULL) {
737 return _talloc_free(ptr);
740 new_p = talloc_parent_chunk(tc_p->refs);
742 new_parent = TC_PTR_FROM_CHUNK(new_p);
747 if (talloc_unreference(new_parent, ptr) != 0) {
751 talloc_steal(new_parent, ptr);
757 add a name to an existing pointer - va_list version
759 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
761 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
763 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
764 tc->name = talloc_vasprintf(ptr, fmt, ap);
765 if (likely(tc->name)) {
766 _talloc_set_name_const(tc->name, ".name");
772 add a name to an existing pointer
774 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
779 name = talloc_set_name_v(ptr, fmt, ap);
786 create a named talloc pointer. Any talloc pointer can be named, and
787 talloc_named() operates just like talloc() except that it allows you
790 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
796 ptr = __talloc(context, size);
797 if (unlikely(ptr == NULL)) return NULL;
800 name = talloc_set_name_v(ptr, fmt, ap);
803 if (unlikely(name == NULL)) {
812 return the name of a talloc ptr, or "UNNAMED"
814 const char *talloc_get_name(const void *ptr)
816 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
817 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
820 if (likely(tc->name)) {
828 check if a pointer has the given name. If it does, return the pointer,
829 otherwise return NULL
831 void *talloc_check_name(const void *ptr, const char *name)
834 if (unlikely(ptr == NULL)) return NULL;
835 pname = talloc_get_name(ptr);
836 if (likely(pname == name || strcmp(pname, name) == 0)) {
837 return discard_const_p(void, ptr);
842 static void talloc_abort_type_missmatch(const char *location,
844 const char *expected)
848 reason = talloc_asprintf(NULL,
849 "%s: Type mismatch: name[%s] expected[%s]",
854 reason = "Type mismatch";
857 talloc_abort(reason);
860 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
864 if (unlikely(ptr == NULL)) {
865 talloc_abort_type_missmatch(location, NULL, name);
869 pname = talloc_get_name(ptr);
870 if (likely(pname == name || strcmp(pname, name) == 0)) {
871 return discard_const_p(void, ptr);
874 talloc_abort_type_missmatch(location, pname, name);
879 this is for compatibility with older versions of talloc
881 void *talloc_init(const char *fmt, ...)
888 * samba3 expects talloc_report_depth_cb(NULL, ...)
889 * reports all talloc'ed memory, so we need to enable
892 talloc_enable_null_tracking();
894 ptr = __talloc(NULL, 0);
895 if (unlikely(ptr == NULL)) return NULL;
898 name = talloc_set_name_v(ptr, fmt, ap);
901 if (unlikely(name == NULL)) {
910 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
911 should probably not be used in new code. It's in here to keep the talloc
912 code consistent across Samba 3 and 4.
914 void talloc_free_children(void *ptr)
916 struct talloc_chunk *tc;
918 if (unlikely(ptr == NULL)) {
922 tc = talloc_chunk_from_ptr(ptr);
925 /* we need to work out who will own an abandoned child
926 if it cannot be freed. In priority order, the first
927 choice is owner of any remaining reference to this
928 pointer, the second choice is our parent, and the
929 final choice is the null context. */
930 void *child = TC_PTR_FROM_CHUNK(tc->child);
931 const void *new_parent = null_context;
932 if (unlikely(tc->child->refs)) {
933 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
934 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
936 if (unlikely(_talloc_free(child) == -1)) {
937 if (new_parent == null_context) {
938 struct talloc_chunk *p = talloc_parent_chunk(ptr);
939 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
941 talloc_steal(new_parent, child);
945 if ((tc->flags & TALLOC_FLAG_POOL)
946 && (*talloc_pool_objectcount(tc) == 1)) {
947 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
948 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
949 VALGRIND_MAKE_MEM_NOACCESS(
950 tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
956 Allocate a bit of memory as a child of an existing pointer
958 void *_talloc(const void *context, size_t size)
960 return __talloc(context, size);
964 externally callable talloc_set_name_const()
966 void talloc_set_name_const(const void *ptr, const char *name)
968 _talloc_set_name_const(ptr, name);
972 create a named talloc pointer. Any talloc pointer can be named, and
973 talloc_named() operates just like talloc() except that it allows you
976 void *talloc_named_const(const void *context, size_t size, const char *name)
978 return _talloc_named_const(context, size, name);
982 free a talloc pointer. This also frees all child pointers of this
985 return 0 if the memory is actually freed, otherwise -1. The memory
986 will not be freed if the ref_count is > 1 or the destructor (if
987 any) returns non-zero
989 int talloc_free(void *ptr)
991 return _talloc_free(ptr);
997 A talloc version of realloc. The context argument is only used if
1000 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1002 struct talloc_chunk *tc;
1004 bool malloced = false;
1006 /* size zero is equivalent to free() */
1007 if (unlikely(size == 0)) {
1012 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1016 /* realloc(NULL) is equivalent to malloc() */
1018 return _talloc_named_const(context, size, name);
1021 tc = talloc_chunk_from_ptr(ptr);
1023 /* don't allow realloc on referenced pointers */
1024 if (unlikely(tc->refs)) {
1028 /* don't let anybody try to realloc a talloc_pool */
1029 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1033 /* don't shrink if we have less than 1k to gain */
1034 if ((size < tc->size) && ((tc->size - size) < 1024)) {
1039 /* by resetting magic we catch users of the old memory */
1040 tc->flags |= TALLOC_FLAG_FREE;
1043 new_ptr = malloc(size + TC_HDR_SIZE);
1045 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
1049 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1051 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1052 *talloc_pool_objectcount((struct talloc_chunk *)
1055 if (new_ptr == NULL) {
1056 new_ptr = malloc(TC_HDR_SIZE+size);
1061 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1065 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1068 if (unlikely(!new_ptr)) {
1069 tc->flags &= ~TALLOC_FLAG_FREE;
1073 tc = (struct talloc_chunk *)new_ptr;
1074 tc->flags &= ~TALLOC_FLAG_FREE;
1076 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1079 tc->parent->child = tc;
1082 tc->child->parent = tc;
1086 tc->prev->next = tc;
1089 tc->next->prev = tc;
1093 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1095 return TC_PTR_FROM_CHUNK(tc);
1099 a wrapper around talloc_steal() for situations where you are moving a pointer
1100 between two structures, and want the old pointer to be set to NULL
1102 void *_talloc_move(const void *new_ctx, const void *_pptr)
1104 const void **pptr = discard_const_p(const void *,_pptr);
1105 void *ret = _talloc_steal(new_ctx, *pptr);
1111 return the total size of a talloc pool (subtree)
1113 size_t talloc_total_size(const void *ptr)
1116 struct talloc_chunk *c, *tc;
1125 tc = talloc_chunk_from_ptr(ptr);
1127 if (tc->flags & TALLOC_FLAG_LOOP) {
1131 tc->flags |= TALLOC_FLAG_LOOP;
1134 for (c=tc->child;c;c=c->next) {
1135 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1138 tc->flags &= ~TALLOC_FLAG_LOOP;
1144 return the total number of blocks in a talloc pool (subtree)
1146 size_t talloc_total_blocks(const void *ptr)
1149 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
1151 if (tc->flags & TALLOC_FLAG_LOOP) {
1155 tc->flags |= TALLOC_FLAG_LOOP;
1158 for (c=tc->child;c;c=c->next) {
1159 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1162 tc->flags &= ~TALLOC_FLAG_LOOP;
1168 return the number of external references to a pointer
1170 size_t talloc_reference_count(const void *ptr)
1172 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1173 struct talloc_reference_handle *h;
1176 for (h=tc->refs;h;h=h->next) {
1183 report on memory usage by all children of a pointer, giving a full tree view
1185 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1186 void (*callback)(const void *ptr,
1187 int depth, int max_depth,
1189 void *private_data),
1192 struct talloc_chunk *c, *tc;
1197 if (ptr == NULL) return;
1199 tc = talloc_chunk_from_ptr(ptr);
1201 if (tc->flags & TALLOC_FLAG_LOOP) {
1205 callback(ptr, depth, max_depth, 0, private_data);
1207 if (max_depth >= 0 && depth >= max_depth) {
1211 tc->flags |= TALLOC_FLAG_LOOP;
1212 for (c=tc->child;c;c=c->next) {
1213 if (c->name == TALLOC_MAGIC_REFERENCE) {
1214 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1215 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1217 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1220 tc->flags &= ~TALLOC_FLAG_LOOP;
1223 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1225 const char *name = talloc_get_name(ptr);
1226 FILE *f = (FILE *)_f;
1229 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1234 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1235 (max_depth < 0 ? "full " :""), name,
1236 (unsigned long)talloc_total_size(ptr),
1237 (unsigned long)talloc_total_blocks(ptr));
1241 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1244 (unsigned long)talloc_total_size(ptr),
1245 (unsigned long)talloc_total_blocks(ptr),
1246 (int)talloc_reference_count(ptr), ptr);
1249 fprintf(f, "content: ");
1250 if (talloc_total_size(ptr)) {
1251 int tot = talloc_total_size(ptr);
1254 for (i = 0; i < tot; i++) {
1255 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1256 fprintf(f, "%c", ((char *)ptr)[i]);
1258 fprintf(f, "~%02x", ((char *)ptr)[i]);
1267 report on memory usage by all children of a pointer, giving a full tree view
1269 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1271 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1276 report on memory usage by all children of a pointer, giving a full tree view
1278 void talloc_report_full(const void *ptr, FILE *f)
1280 talloc_report_depth_file(ptr, 0, -1, f);
1284 report on memory usage by all children of a pointer
1286 void talloc_report(const void *ptr, FILE *f)
1288 talloc_report_depth_file(ptr, 0, 1, f);
1292 report on any memory hanging off the null context
1294 static void talloc_report_null(void)
1296 if (talloc_total_size(null_context) != 0) {
1297 talloc_report(null_context, stderr);
1302 report on any memory hanging off the null context
1304 static void talloc_report_null_full(void)
1306 if (talloc_total_size(null_context) != 0) {
1307 talloc_report_full(null_context, stderr);
1312 enable tracking of the NULL context
1314 void talloc_enable_null_tracking(void)
1316 if (null_context == NULL) {
1317 null_context = _talloc_named_const(NULL, 0, "null_context");
1322 disable tracking of the NULL context
1324 void talloc_disable_null_tracking(void)
1326 _talloc_free(null_context);
1327 null_context = NULL;
1331 enable leak reporting on exit
1333 void talloc_enable_leak_report(void)
1335 talloc_enable_null_tracking();
1336 atexit(talloc_report_null);
1340 enable full leak reporting on exit
1342 void talloc_enable_leak_report_full(void)
1344 talloc_enable_null_tracking();
1345 atexit(talloc_report_null_full);
1349 talloc and zero memory.
1351 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1353 void *p = _talloc_named_const(ctx, size, name);
1356 memset(p, '\0', size);
1363 memdup with a talloc.
1365 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1367 void *newp = _talloc_named_const(t, size, name);
1370 memcpy(newp, p, size);
1376 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1380 ret = (char *)__talloc(t, len + 1);
1381 if (unlikely(!ret)) return NULL;
1383 memcpy(ret, p, len);
1386 _talloc_set_name_const(ret, ret);
1391 strdup with a talloc
1393 char *talloc_strdup(const void *t, const char *p)
1395 if (unlikely(!p)) return NULL;
1396 return __talloc_strlendup(t, p, strlen(p));
1400 strndup with a talloc
1402 char *talloc_strndup(const void *t, const char *p, size_t n)
1404 if (unlikely(!p)) return NULL;
1405 return __talloc_strlendup(t, p, strnlen(p, n));
1408 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1409 const char *a, size_t alen)
1413 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1414 if (unlikely(!ret)) return NULL;
1416 /* append the string and the trailing \0 */
1417 memcpy(&ret[slen], a, alen);
1420 _talloc_set_name_const(ret, ret);
1425 * Appends at the end of the string.
1427 char *talloc_strdup_append(char *s, const char *a)
1430 return talloc_strdup(NULL, a);
1437 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1441 * Appends at the end of the talloc'ed buffer,
1442 * not the end of the string.
1444 char *talloc_strdup_append_buffer(char *s, const char *a)
1449 return talloc_strdup(NULL, a);
1456 slen = talloc_get_size(s);
1457 if (likely(slen > 0)) {
1461 return __talloc_strlendup_append(s, slen, a, strlen(a));
1465 * Appends at the end of the string.
1467 char *talloc_strndup_append(char *s, const char *a, size_t n)
1470 return talloc_strdup(NULL, a);
1477 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1481 * Appends at the end of the talloc'ed buffer,
1482 * not the end of the string.
1484 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1489 return talloc_strdup(NULL, a);
1496 slen = talloc_get_size(s);
1497 if (likely(slen > 0)) {
1501 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1504 #ifndef HAVE_VA_COPY
1505 #ifdef HAVE___VA_COPY
1506 #define va_copy(dest, src) __va_copy(dest, src)
1508 #define va_copy(dest, src) (dest) = (src)
1512 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1519 /* this call looks strange, but it makes it work on older solaris boxes */
1521 len = vsnprintf(&c, 1, fmt, ap2);
1523 if (unlikely(len < 0)) {
1527 ret = (char *)__talloc(t, len+1);
1528 if (unlikely(!ret)) return NULL;
1531 vsnprintf(ret, len+1, fmt, ap2);
1534 _talloc_set_name_const(ret, ret);
1540 Perform string formatting, and return a pointer to newly allocated
1541 memory holding the result, inside a memory pool.
1543 char *talloc_asprintf(const void *t, const char *fmt, ...)
1549 ret = talloc_vasprintf(t, fmt, ap);
1554 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1555 const char *fmt, va_list ap)
1556 PRINTF_ATTRIBUTE(3,0);
1558 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1559 const char *fmt, va_list ap)
1566 alen = vsnprintf(&c, 1, fmt, ap2);
1570 /* Either the vsnprintf failed or the format resulted in
1571 * no characters being formatted. In the former case, we
1572 * ought to return NULL, in the latter we ought to return
1573 * the original string. Most current callers of this
1574 * function expect it to never return NULL.
1579 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1580 if (!s) return NULL;
1583 vsnprintf(s + slen, alen + 1, fmt, ap2);
1586 _talloc_set_name_const(s, s);
1591 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1592 * and return @p s, which may have moved. Good for gradually
1593 * accumulating output into a string buffer. Appends at the end
1596 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1599 return talloc_vasprintf(NULL, fmt, ap);
1602 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1606 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1607 * and return @p s, which may have moved. Always appends at the
1608 * end of the talloc'ed buffer, not the end of the string.
1610 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1615 return talloc_vasprintf(NULL, fmt, ap);
1618 slen = talloc_get_size(s);
1619 if (likely(slen > 0)) {
1623 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1627 Realloc @p s to append the formatted result of @p fmt and return @p
1628 s, which may have moved. Good for gradually accumulating output
1629 into a string buffer.
1631 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1636 s = talloc_vasprintf_append(s, fmt, ap);
1642 Realloc @p s to append the formatted result of @p fmt and return @p
1643 s, which may have moved. Good for gradually accumulating output
1646 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1651 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1657 alloc an array, checking for integer overflow in the array size
1659 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1661 if (count >= MAX_TALLOC_SIZE/el_size) {
1664 return _talloc_named_const(ctx, el_size * count, name);
1668 alloc an zero array, checking for integer overflow in the array size
1670 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1672 if (count >= MAX_TALLOC_SIZE/el_size) {
1675 return _talloc_zero(ctx, el_size * count, name);
1679 realloc an array, checking for integer overflow in the array size
1681 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1683 if (count >= MAX_TALLOC_SIZE/el_size) {
1686 return _talloc_realloc(ctx, ptr, el_size * count, name);
1690 a function version of talloc_realloc(), so it can be passed as a function pointer
1691 to libraries that want a realloc function (a realloc function encapsulates
1692 all the basic capabilities of an allocation library, which is why this is useful)
1694 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1696 return _talloc_realloc(context, ptr, size, NULL);
1700 static int talloc_autofree_destructor(void *ptr)
1702 autofree_context = NULL;
1706 static void talloc_autofree(void)
1708 _talloc_free(autofree_context);
1712 return a context which will be auto-freed on exit
1713 this is useful for reducing the noise in leak reports
1715 void *talloc_autofree_context(void)
1717 if (autofree_context == NULL) {
1718 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1719 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1720 atexit(talloc_autofree);
1722 return autofree_context;
1725 size_t talloc_get_size(const void *context)
1727 struct talloc_chunk *tc;
1729 if (context == NULL)
1732 tc = talloc_chunk_from_ptr(context);
1738 find a parent of this context that has the given name, if any
1740 void *talloc_find_parent_byname(const void *context, const char *name)
1742 struct talloc_chunk *tc;
1744 if (context == NULL) {
1748 tc = talloc_chunk_from_ptr(context);
1750 if (tc->name && strcmp(tc->name, name) == 0) {
1751 return TC_PTR_FROM_CHUNK(tc);
1753 while (tc && tc->prev) tc = tc->prev;
1762 show the parentage of a context
1764 void talloc_show_parents(const void *context, FILE *file)
1766 struct talloc_chunk *tc;
1768 if (context == NULL) {
1769 fprintf(file, "talloc no parents for NULL\n");
1773 tc = talloc_chunk_from_ptr(context);
1774 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1776 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1777 while (tc && tc->prev) tc = tc->prev;
1786 return 1 if ptr is a parent of context
1788 int talloc_is_parent(const void *context, const void *ptr)
1790 struct talloc_chunk *tc;
1792 if (context == NULL) {
1796 tc = talloc_chunk_from_ptr(context);
1798 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1799 while (tc && tc->prev) tc = tc->prev;