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/
36 /* use this to force every realloc to change the pointer, to stress test
37 code that might not cope */
38 #define ALWAYS_REALLOC 0
41 #define MAX_TALLOC_SIZE 0x10000000
42 #define TALLOC_MAGIC_V1 0xe814ec70
43 #define TALLOC_MAGIC_V2 0xe814ec80
44 #define TALLOC_MAGIC TALLOC_MAGIC_V2
45 #define TALLOC_FLAG_FREE 0x01
46 #define TALLOC_FLAG_LOOP 0x02
47 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
48 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
49 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
51 /* by default we abort when given a bad pointer (such as when talloc_free() is called
52 on a pointer that came from malloc() */
54 #define TALLOC_ABORT(reason) abort()
57 #ifndef discard_const_p
58 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
59 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
61 # define discard_const_p(type, ptr) ((type *)(ptr))
65 /* these macros gain us a few percent of speed on gcc */
67 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
68 as its first argument */
70 #define likely(x) __builtin_expect(!!(x), 1)
73 #define unlikely(x) __builtin_expect(!!(x), 0)
80 #define unlikely(x) (x)
84 /* this null_context is only used if talloc_enable_leak_report() or
85 talloc_enable_leak_report_full() is called, otherwise it remains
88 static void *null_context;
89 static void *autofree_context;
91 struct talloc_reference_handle {
92 struct talloc_reference_handle *next, *prev;
97 typedef int (*talloc_destructor_t)(void *);
100 struct talloc_chunk *next, *prev;
101 struct talloc_chunk *parent, *child;
102 struct talloc_reference_handle *refs;
103 talloc_destructor_t destructor;
109 * "pool" has dual use:
111 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
112 * marks the end of the currently allocated area.
114 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
115 * is a pointer to the struct talloc_chunk of the pool that it was
116 * allocated from. This way children can quickly find the pool to chew
122 /* 16 byte alignment seems to keep everyone happy */
123 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
124 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
126 static void (*talloc_log_fn)(const char *message);
128 void talloc_set_log_fn(void (*log_fn)(const char *message))
130 talloc_log_fn = log_fn;
133 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
134 static void talloc_log(const char *fmt, ...)
139 if (!talloc_log_fn) {
144 message = talloc_vasprintf(NULL, fmt, ap);
147 talloc_log_fn(message);
148 talloc_free(message);
151 static void talloc_log_stderr(const char *message)
153 fprintf(stderr, "%s", message);
156 void talloc_set_log_stderr(void)
158 talloc_set_log_fn(talloc_log_stderr);
161 static void (*talloc_abort_fn)(const char *reason);
163 void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
165 talloc_abort_fn = abort_fn;
168 static void talloc_abort(const char *reason)
170 talloc_log("%s\n", reason);
172 if (!talloc_abort_fn) {
173 TALLOC_ABORT(reason);
176 talloc_abort_fn(reason);
179 static void talloc_abort_magic_v1(void)
181 talloc_abort("Bad talloc magic value - old magic v1 used");
184 static void talloc_abort_double_free(void)
186 talloc_abort("Bad talloc magic value - double free");
189 static void talloc_abort_unknown_value(void)
191 talloc_abort("Bad talloc magic value - unknown value");
194 /* panic if we get a bad magic value */
195 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
197 const char *pp = (const char *)ptr;
198 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
199 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
200 if ((tc->flags & (~0xF)) == TALLOC_MAGIC_V1) {
201 talloc_abort_magic_v1();
205 if (tc->flags & TALLOC_FLAG_FREE) {
206 talloc_abort_double_free();
209 talloc_abort_unknown_value();
216 /* hook into the front of the list */
217 #define _TLIST_ADD(list, p) \
221 (p)->next = (p)->prev = NULL; \
223 (list)->prev = (p); \
224 (p)->next = (list); \
230 /* remove an element from a list - element doesn't have to be in list. */
231 #define _TLIST_REMOVE(list, p) \
233 if ((p) == (list)) { \
234 (list) = (p)->next; \
235 if (list) (list)->prev = NULL; \
237 if ((p)->prev) (p)->prev->next = (p)->next; \
238 if ((p)->next) (p)->next->prev = (p)->prev; \
240 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
245 return the parent chunk of a pointer
247 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
249 struct talloc_chunk *tc;
251 if (unlikely(ptr == NULL)) {
255 tc = talloc_chunk_from_ptr(ptr);
256 while (tc->prev) tc=tc->prev;
261 void *talloc_parent(const void *ptr)
263 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
264 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
270 const char *talloc_parent_name(const void *ptr)
272 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
273 return tc? tc->name : NULL;
277 A pool carries an in-pool object count count in the first 16 bytes.
278 bytes. This is done to support talloc_steal() to a parent outside of the
279 pool. The count includes the pool itself, so a talloc_free() on a pool will
280 only destroy the pool if the count has dropped to zero. A talloc_free() of a
281 pool member will reduce the count, and eventually also call free(3) on the
284 The object count is not put into "struct talloc_chunk" because it is only
285 relevant for talloc pools and the alignment to 16 bytes would increase the
286 memory footprint of each talloc chunk by those 16 bytes.
289 #define TALLOC_POOL_HDR_SIZE 16
291 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
293 return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
300 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
303 struct talloc_chunk *pool_ctx = NULL;
305 struct talloc_chunk *result;
308 if (parent == NULL) {
312 if (parent->flags & TALLOC_FLAG_POOL) {
315 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
316 pool_ctx = (struct talloc_chunk *)parent->pool;
319 if (pool_ctx == NULL) {
323 space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
324 - ((char *)pool_ctx->pool);
327 * Align size to 16 bytes
329 chunk_size = ((size + 15) & ~15);
331 if (space_left < chunk_size) {
335 result = (struct talloc_chunk *)pool_ctx->pool;
337 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
338 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
341 pool_ctx->pool = (void *)((char *)result + chunk_size);
343 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
344 result->pool = pool_ctx;
346 *talloc_pool_objectcount(pool_ctx) += 1;
352 Allocate a bit of memory as a child of an existing pointer
354 static inline void *__talloc(const void *context, size_t size)
356 struct talloc_chunk *tc = NULL;
358 if (unlikely(context == NULL)) {
359 context = null_context;
362 if (unlikely(size >= MAX_TALLOC_SIZE)) {
366 if (context != NULL) {
367 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
372 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
373 if (unlikely(tc == NULL)) return NULL;
374 tc->flags = TALLOC_MAGIC;
379 tc->destructor = NULL;
384 if (likely(context)) {
385 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
388 parent->child->parent = NULL;
389 tc->next = parent->child;
398 tc->next = tc->prev = tc->parent = NULL;
401 return TC_PTR_FROM_CHUNK(tc);
405 * Create a talloc pool
408 void *talloc_pool(const void *context, size_t size)
410 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
411 struct talloc_chunk *tc;
413 if (unlikely(result == NULL)) {
417 tc = talloc_chunk_from_ptr(result);
419 tc->flags |= TALLOC_FLAG_POOL;
420 tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
422 *talloc_pool_objectcount(tc) = 1;
424 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
425 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
432 setup a destructor to be called on free of a pointer
433 the destructor should return 0 on success, or -1 on failure.
434 if the destructor fails then the free is failed, and the memory can
435 be continued to be used
437 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
439 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
440 tc->destructor = destructor;
444 increase the reference count on a piece of memory.
446 int talloc_increase_ref_count(const void *ptr)
448 if (unlikely(!talloc_reference(null_context, ptr))) {
455 helper for talloc_reference()
457 this is referenced by a function pointer and should not be inline
459 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
461 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
462 _TLIST_REMOVE(ptr_tc->refs, handle);
467 more efficient way to add a name to a pointer - the name must point to a
470 static inline void _talloc_set_name_const(const void *ptr, const char *name)
472 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
477 internal talloc_named_const()
479 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
483 ptr = __talloc(context, size);
484 if (unlikely(ptr == NULL)) {
488 _talloc_set_name_const(ptr, name);
494 make a secondary reference to a pointer, hanging off the given context.
495 the pointer remains valid until both the original caller and this given
498 the major use for this is when two different structures need to reference the
499 same underlying data, and you want to be able to free the two instances separately,
502 void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
504 struct talloc_chunk *tc;
505 struct talloc_reference_handle *handle;
506 if (unlikely(ptr == NULL)) return NULL;
508 tc = talloc_chunk_from_ptr(ptr);
509 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
510 sizeof(struct talloc_reference_handle),
511 TALLOC_MAGIC_REFERENCE);
512 if (unlikely(handle == NULL)) return NULL;
514 /* note that we hang the destructor off the handle, not the
515 main context as that allows the caller to still setup their
516 own destructor on the context if they want to */
517 talloc_set_destructor(handle, talloc_reference_destructor);
518 handle->ptr = discard_const_p(void, ptr);
519 handle->location = location;
520 _TLIST_ADD(tc->refs, handle);
524 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
527 internal talloc_free call
529 static inline int _talloc_free_internal(void *ptr)
531 struct talloc_chunk *tc;
533 if (unlikely(ptr == NULL)) {
537 tc = talloc_chunk_from_ptr(ptr);
539 if (unlikely(tc->refs)) {
541 /* check this is a reference from a child or grantchild
542 * back to it's parent or grantparent
544 * in that case we need to remove the reference and
545 * call another instance of talloc_free() on the current
548 is_child = talloc_is_parent(tc->refs, ptr);
549 _talloc_free_internal(tc->refs);
551 return _talloc_free_internal(ptr);
556 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
557 /* we have a free loop - stop looping */
561 if (unlikely(tc->destructor)) {
562 talloc_destructor_t d = tc->destructor;
563 if (d == (talloc_destructor_t)-1) {
566 tc->destructor = (talloc_destructor_t)-1;
571 tc->destructor = NULL;
575 _TLIST_REMOVE(tc->parent->child, tc);
576 if (tc->parent->child) {
577 tc->parent->child->parent = tc->parent;
580 if (tc->prev) tc->prev->next = tc->next;
581 if (tc->next) tc->next->prev = tc->prev;
584 tc->flags |= TALLOC_FLAG_LOOP;
587 /* we need to work out who will own an abandoned child
588 if it cannot be freed. In priority order, the first
589 choice is owner of any remaining reference to this
590 pointer, the second choice is our parent, and the
591 final choice is the null context. */
592 void *child = TC_PTR_FROM_CHUNK(tc->child);
593 const void *new_parent = null_context;
594 if (unlikely(tc->child->refs)) {
595 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
596 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
598 if (unlikely(_talloc_free_internal(child) == -1)) {
599 if (new_parent == null_context) {
600 struct talloc_chunk *p = talloc_parent_chunk(ptr);
601 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
603 _talloc_steal_internal(new_parent, child);
607 tc->flags |= TALLOC_FLAG_FREE;
609 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
610 struct talloc_chunk *pool;
611 unsigned int *pool_object_count;
613 pool = (tc->flags & TALLOC_FLAG_POOL)
614 ? tc : (struct talloc_chunk *)tc->pool;
616 pool_object_count = talloc_pool_objectcount(pool);
618 if (*pool_object_count == 0) {
619 talloc_abort("Pool object count zero!");
623 *pool_object_count -= 1;
625 if (*pool_object_count == 0) {
636 move a lump of memory from one talloc context to another return the
637 ptr on success, or NULL if it could not be transferred.
638 passing NULL as ptr will always return NULL with no side effects.
640 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
642 struct talloc_chunk *tc, *new_tc;
644 if (unlikely(!ptr)) {
648 if (unlikely(new_ctx == NULL)) {
649 new_ctx = null_context;
652 tc = talloc_chunk_from_ptr(ptr);
654 if (unlikely(new_ctx == NULL)) {
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;
665 tc->parent = tc->next = tc->prev = NULL;
666 return discard_const_p(void, ptr);
669 new_tc = talloc_chunk_from_ptr(new_ctx);
671 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
672 return discard_const_p(void, ptr);
676 _TLIST_REMOVE(tc->parent->child, tc);
677 if (tc->parent->child) {
678 tc->parent->child->parent = tc->parent;
681 if (tc->prev) tc->prev->next = tc->next;
682 if (tc->next) tc->next->prev = tc->prev;
686 if (new_tc->child) new_tc->child->parent = NULL;
687 _TLIST_ADD(new_tc->child, tc);
689 return discard_const_p(void, ptr);
693 move a lump of memory from one talloc context to another return the
694 ptr on success, or NULL if it could not be transferred.
695 passing NULL as ptr will always return NULL with no side effects.
697 void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
699 struct talloc_chunk *tc;
701 if (unlikely(ptr == NULL)) {
705 tc = talloc_chunk_from_ptr(ptr);
707 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
708 struct talloc_reference_handle *h;
710 talloc_log("WARNING: talloc_steal with references at %s\n",
713 for (h=tc->refs; h; h=h->next) {
714 talloc_log("\treference at %s\n",
719 return _talloc_steal_internal(new_ctx, ptr);
723 this is like a talloc_steal(), but you must supply the old
724 parent. This resolves the ambiguity in a talloc_steal() which is
725 called on a context that has more than one parent (via references)
727 The old parent can be either a reference or a parent
729 void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
731 struct talloc_chunk *tc;
732 struct talloc_reference_handle *h;
734 if (unlikely(ptr == NULL)) {
738 if (old_parent == talloc_parent(ptr)) {
739 return _talloc_steal_internal(new_parent, ptr);
742 tc = talloc_chunk_from_ptr(ptr);
743 for (h=tc->refs;h;h=h->next) {
744 if (talloc_parent(h) == old_parent) {
745 if (_talloc_steal_internal(new_parent, h) != h) {
752 /* it wasn't a parent */
757 remove a secondary reference to a pointer. This undo's what
758 talloc_reference() has done. The context and pointer arguments
759 must match those given to a talloc_reference()
761 static inline int talloc_unreference(const void *context, const void *ptr)
763 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
764 struct talloc_reference_handle *h;
766 if (unlikely(context == NULL)) {
767 context = null_context;
770 for (h=tc->refs;h;h=h->next) {
771 struct talloc_chunk *p = talloc_parent_chunk(h);
773 if (context == NULL) break;
774 } else if (TC_PTR_FROM_CHUNK(p) == context) {
782 return _talloc_free_internal(h);
786 remove a specific parent context from a pointer. This is a more
787 controlled varient of talloc_free()
789 int talloc_unlink(const void *context, void *ptr)
791 struct talloc_chunk *tc_p, *new_p;
798 if (context == NULL) {
799 context = null_context;
802 if (talloc_unreference(context, ptr) == 0) {
806 if (context == NULL) {
807 if (talloc_parent_chunk(ptr) != NULL) {
811 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
816 tc_p = talloc_chunk_from_ptr(ptr);
818 if (tc_p->refs == NULL) {
819 return _talloc_free_internal(ptr);
822 new_p = talloc_parent_chunk(tc_p->refs);
824 new_parent = TC_PTR_FROM_CHUNK(new_p);
829 if (talloc_unreference(new_parent, ptr) != 0) {
833 _talloc_steal_internal(new_parent, ptr);
839 add a name to an existing pointer - va_list version
841 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
843 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
845 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
846 tc->name = talloc_vasprintf(ptr, fmt, ap);
847 if (likely(tc->name)) {
848 _talloc_set_name_const(tc->name, ".name");
854 add a name to an existing pointer
856 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
861 name = talloc_set_name_v(ptr, fmt, ap);
868 create a named talloc pointer. Any talloc pointer can be named, and
869 talloc_named() operates just like talloc() except that it allows you
872 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
878 ptr = __talloc(context, size);
879 if (unlikely(ptr == NULL)) return NULL;
882 name = talloc_set_name_v(ptr, fmt, ap);
885 if (unlikely(name == NULL)) {
886 _talloc_free_internal(ptr);
894 return the name of a talloc ptr, or "UNNAMED"
896 const char *talloc_get_name(const void *ptr)
898 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
899 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
902 if (likely(tc->name)) {
910 check if a pointer has the given name. If it does, return the pointer,
911 otherwise return NULL
913 void *talloc_check_name(const void *ptr, const char *name)
916 if (unlikely(ptr == NULL)) return NULL;
917 pname = talloc_get_name(ptr);
918 if (likely(pname == name || strcmp(pname, name) == 0)) {
919 return discard_const_p(void, ptr);
924 static void talloc_abort_type_missmatch(const char *location,
926 const char *expected)
930 reason = talloc_asprintf(NULL,
931 "%s: Type mismatch: name[%s] expected[%s]",
936 reason = "Type mismatch";
939 talloc_abort(reason);
942 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
946 if (unlikely(ptr == NULL)) {
947 talloc_abort_type_missmatch(location, NULL, name);
951 pname = talloc_get_name(ptr);
952 if (likely(pname == name || strcmp(pname, name) == 0)) {
953 return discard_const_p(void, ptr);
956 talloc_abort_type_missmatch(location, pname, name);
961 this is for compatibility with older versions of talloc
963 void *talloc_init(const char *fmt, ...)
970 * samba3 expects talloc_report_depth_cb(NULL, ...)
971 * reports all talloc'ed memory, so we need to enable
974 talloc_enable_null_tracking();
976 ptr = __talloc(NULL, 0);
977 if (unlikely(ptr == NULL)) return NULL;
980 name = talloc_set_name_v(ptr, fmt, ap);
983 if (unlikely(name == NULL)) {
984 _talloc_free_internal(ptr);
992 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
993 should probably not be used in new code. It's in here to keep the talloc
994 code consistent across Samba 3 and 4.
996 void talloc_free_children(void *ptr)
998 struct talloc_chunk *tc;
1000 if (unlikely(ptr == NULL)) {
1004 tc = talloc_chunk_from_ptr(ptr);
1007 /* we need to work out who will own an abandoned child
1008 if it cannot be freed. In priority order, the first
1009 choice is owner of any remaining reference to this
1010 pointer, the second choice is our parent, and the
1011 final choice is the null context. */
1012 void *child = TC_PTR_FROM_CHUNK(tc->child);
1013 const void *new_parent = null_context;
1014 if (unlikely(tc->child->refs)) {
1015 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1016 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1018 if (unlikely(talloc_free(child) == -1)) {
1019 if (new_parent == null_context) {
1020 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1021 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1023 _talloc_steal_internal(new_parent, child);
1027 if ((tc->flags & TALLOC_FLAG_POOL)
1028 && (*talloc_pool_objectcount(tc) == 1)) {
1029 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
1030 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
1031 VALGRIND_MAKE_MEM_NOACCESS(
1032 tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
1038 Allocate a bit of memory as a child of an existing pointer
1040 void *_talloc(const void *context, size_t size)
1042 return __talloc(context, size);
1046 externally callable talloc_set_name_const()
1048 void talloc_set_name_const(const void *ptr, const char *name)
1050 _talloc_set_name_const(ptr, name);
1054 create a named talloc pointer. Any talloc pointer can be named, and
1055 talloc_named() operates just like talloc() except that it allows you
1056 to name the pointer.
1058 void *talloc_named_const(const void *context, size_t size, const char *name)
1060 return _talloc_named_const(context, size, name);
1064 free a talloc pointer. This also frees all child pointers of this
1067 return 0 if the memory is actually freed, otherwise -1. The memory
1068 will not be freed if the ref_count is > 1 or the destructor (if
1069 any) returns non-zero
1071 int _talloc_free(void *ptr, const char *location)
1073 struct talloc_chunk *tc;
1075 if (unlikely(ptr == NULL)) {
1079 tc = talloc_chunk_from_ptr(ptr);
1081 if (unlikely(tc->refs != NULL)) {
1082 struct talloc_reference_handle *h;
1084 talloc_log("ERROR: talloc_free with references at %s\n",
1087 for (h=tc->refs; h; h=h->next) {
1088 talloc_log("\treference at %s\n",
1094 return _talloc_free_internal(ptr);
1100 A talloc version of realloc. The context argument is only used if
1103 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1105 struct talloc_chunk *tc;
1107 bool malloced = false;
1109 /* size zero is equivalent to free() */
1110 if (unlikely(size == 0)) {
1111 talloc_unlink(context, ptr);
1115 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1119 /* realloc(NULL) is equivalent to malloc() */
1121 return _talloc_named_const(context, size, name);
1124 tc = talloc_chunk_from_ptr(ptr);
1126 /* don't allow realloc on referenced pointers */
1127 if (unlikely(tc->refs)) {
1131 /* don't let anybody try to realloc a talloc_pool */
1132 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1136 /* don't shrink if we have less than 1k to gain */
1137 if ((size < tc->size) && ((tc->size - size) < 1024)) {
1142 /* by resetting magic we catch users of the old memory */
1143 tc->flags |= TALLOC_FLAG_FREE;
1146 new_ptr = malloc(size + TC_HDR_SIZE);
1148 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
1152 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1154 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1155 *talloc_pool_objectcount((struct talloc_chunk *)
1158 if (new_ptr == NULL) {
1159 new_ptr = malloc(TC_HDR_SIZE+size);
1164 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1168 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1171 if (unlikely(!new_ptr)) {
1172 tc->flags &= ~TALLOC_FLAG_FREE;
1176 tc = (struct talloc_chunk *)new_ptr;
1177 tc->flags &= ~TALLOC_FLAG_FREE;
1179 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1182 tc->parent->child = tc;
1185 tc->child->parent = tc;
1189 tc->prev->next = tc;
1192 tc->next->prev = tc;
1196 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1198 return TC_PTR_FROM_CHUNK(tc);
1202 a wrapper around talloc_steal() for situations where you are moving a pointer
1203 between two structures, and want the old pointer to be set to NULL
1205 void *_talloc_move(const void *new_ctx, const void *_pptr)
1207 const void **pptr = discard_const_p(const void *,_pptr);
1208 void *ret = talloc_steal(new_ctx, (void *)*pptr);
1214 return the total size of a talloc pool (subtree)
1216 size_t talloc_total_size(const void *ptr)
1219 struct talloc_chunk *c, *tc;
1228 tc = talloc_chunk_from_ptr(ptr);
1230 if (tc->flags & TALLOC_FLAG_LOOP) {
1234 tc->flags |= TALLOC_FLAG_LOOP;
1236 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1239 for (c=tc->child;c;c=c->next) {
1240 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1243 tc->flags &= ~TALLOC_FLAG_LOOP;
1249 return the total number of blocks in a talloc pool (subtree)
1251 size_t talloc_total_blocks(const void *ptr)
1254 struct talloc_chunk *c, *tc;
1263 tc = talloc_chunk_from_ptr(ptr);
1265 if (tc->flags & TALLOC_FLAG_LOOP) {
1269 tc->flags |= TALLOC_FLAG_LOOP;
1272 for (c=tc->child;c;c=c->next) {
1273 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1276 tc->flags &= ~TALLOC_FLAG_LOOP;
1282 return the number of external references to a pointer
1284 size_t talloc_reference_count(const void *ptr)
1286 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1287 struct talloc_reference_handle *h;
1290 for (h=tc->refs;h;h=h->next) {
1297 report on memory usage by all children of a pointer, giving a full tree view
1299 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1300 void (*callback)(const void *ptr,
1301 int depth, int max_depth,
1303 void *private_data),
1306 struct talloc_chunk *c, *tc;
1311 if (ptr == NULL) return;
1313 tc = talloc_chunk_from_ptr(ptr);
1315 if (tc->flags & TALLOC_FLAG_LOOP) {
1319 callback(ptr, depth, max_depth, 0, private_data);
1321 if (max_depth >= 0 && depth >= max_depth) {
1325 tc->flags |= TALLOC_FLAG_LOOP;
1326 for (c=tc->child;c;c=c->next) {
1327 if (c->name == TALLOC_MAGIC_REFERENCE) {
1328 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1329 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1331 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1334 tc->flags &= ~TALLOC_FLAG_LOOP;
1337 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1339 const char *name = talloc_get_name(ptr);
1340 FILE *f = (FILE *)_f;
1343 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1348 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1349 (max_depth < 0 ? "full " :""), name,
1350 (unsigned long)talloc_total_size(ptr),
1351 (unsigned long)talloc_total_blocks(ptr));
1355 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1358 (unsigned long)talloc_total_size(ptr),
1359 (unsigned long)talloc_total_blocks(ptr),
1360 (int)talloc_reference_count(ptr), ptr);
1363 fprintf(f, "content: ");
1364 if (talloc_total_size(ptr)) {
1365 int tot = talloc_total_size(ptr);
1368 for (i = 0; i < tot; i++) {
1369 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1370 fprintf(f, "%c", ((char *)ptr)[i]);
1372 fprintf(f, "~%02x", ((char *)ptr)[i]);
1381 report on memory usage by all children of a pointer, giving a full tree view
1383 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1385 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1390 report on memory usage by all children of a pointer, giving a full tree view
1392 void talloc_report_full(const void *ptr, FILE *f)
1394 talloc_report_depth_file(ptr, 0, -1, f);
1398 report on memory usage by all children of a pointer
1400 void talloc_report(const void *ptr, FILE *f)
1402 talloc_report_depth_file(ptr, 0, 1, f);
1406 report on any memory hanging off the null context
1408 static void talloc_report_null(void)
1410 if (talloc_total_size(null_context) != 0) {
1411 talloc_report(null_context, stderr);
1416 report on any memory hanging off the null context
1418 static void talloc_report_null_full(void)
1420 if (talloc_total_size(null_context) != 0) {
1421 talloc_report_full(null_context, stderr);
1426 enable tracking of the NULL context
1428 void talloc_enable_null_tracking(void)
1430 if (null_context == NULL) {
1431 null_context = _talloc_named_const(NULL, 0, "null_context");
1436 disable tracking of the NULL context
1438 void talloc_disable_null_tracking(void)
1440 talloc_free(null_context);
1441 null_context = NULL;
1445 enable leak reporting on exit
1447 void talloc_enable_leak_report(void)
1449 talloc_enable_null_tracking();
1450 atexit(talloc_report_null);
1454 enable full leak reporting on exit
1456 void talloc_enable_leak_report_full(void)
1458 talloc_enable_null_tracking();
1459 atexit(talloc_report_null_full);
1463 talloc and zero memory.
1465 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1467 void *p = _talloc_named_const(ctx, size, name);
1470 memset(p, '\0', size);
1477 memdup with a talloc.
1479 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1481 void *newp = _talloc_named_const(t, size, name);
1484 memcpy(newp, p, size);
1490 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1494 ret = (char *)__talloc(t, len + 1);
1495 if (unlikely(!ret)) return NULL;
1497 memcpy(ret, p, len);
1500 _talloc_set_name_const(ret, ret);
1505 strdup with a talloc
1507 char *talloc_strdup(const void *t, const char *p)
1509 if (unlikely(!p)) return NULL;
1510 return __talloc_strlendup(t, p, strlen(p));
1514 strndup with a talloc
1516 char *talloc_strndup(const void *t, const char *p, size_t n)
1518 if (unlikely(!p)) return NULL;
1519 return __talloc_strlendup(t, p, strnlen(p, n));
1522 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1523 const char *a, size_t alen)
1527 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1528 if (unlikely(!ret)) return NULL;
1530 /* append the string and the trailing \0 */
1531 memcpy(&ret[slen], a, alen);
1534 _talloc_set_name_const(ret, ret);
1539 * Appends at the end of the string.
1541 char *talloc_strdup_append(char *s, const char *a)
1544 return talloc_strdup(NULL, a);
1551 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1555 * Appends at the end of the talloc'ed buffer,
1556 * not the end of the string.
1558 char *talloc_strdup_append_buffer(char *s, const char *a)
1563 return talloc_strdup(NULL, a);
1570 slen = talloc_get_size(s);
1571 if (likely(slen > 0)) {
1575 return __talloc_strlendup_append(s, slen, a, strlen(a));
1579 * Appends at the end of the string.
1581 char *talloc_strndup_append(char *s, const char *a, size_t n)
1584 return talloc_strdup(NULL, a);
1591 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1595 * Appends at the end of the talloc'ed buffer,
1596 * not the end of the string.
1598 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1603 return talloc_strdup(NULL, a);
1610 slen = talloc_get_size(s);
1611 if (likely(slen > 0)) {
1615 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1618 #ifndef HAVE_VA_COPY
1619 #ifdef HAVE___VA_COPY
1620 #define va_copy(dest, src) __va_copy(dest, src)
1622 #define va_copy(dest, src) (dest) = (src)
1626 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1633 /* this call looks strange, but it makes it work on older solaris boxes */
1635 len = vsnprintf(&c, 1, fmt, ap2);
1637 if (unlikely(len < 0)) {
1641 ret = (char *)__talloc(t, len+1);
1642 if (unlikely(!ret)) return NULL;
1645 vsnprintf(ret, len+1, fmt, ap2);
1648 _talloc_set_name_const(ret, ret);
1654 Perform string formatting, and return a pointer to newly allocated
1655 memory holding the result, inside a memory pool.
1657 char *talloc_asprintf(const void *t, const char *fmt, ...)
1663 ret = talloc_vasprintf(t, fmt, ap);
1668 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1669 const char *fmt, va_list ap)
1670 PRINTF_ATTRIBUTE(3,0);
1672 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1673 const char *fmt, va_list ap)
1680 alen = vsnprintf(&c, 1, fmt, ap2);
1684 /* Either the vsnprintf failed or the format resulted in
1685 * no characters being formatted. In the former case, we
1686 * ought to return NULL, in the latter we ought to return
1687 * the original string. Most current callers of this
1688 * function expect it to never return NULL.
1693 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1694 if (!s) return NULL;
1697 vsnprintf(s + slen, alen + 1, fmt, ap2);
1700 _talloc_set_name_const(s, s);
1705 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1706 * and return @p s, which may have moved. Good for gradually
1707 * accumulating output into a string buffer. Appends at the end
1710 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1713 return talloc_vasprintf(NULL, fmt, ap);
1716 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1720 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1721 * and return @p s, which may have moved. Always appends at the
1722 * end of the talloc'ed buffer, not the end of the string.
1724 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1729 return talloc_vasprintf(NULL, fmt, ap);
1732 slen = talloc_get_size(s);
1733 if (likely(slen > 0)) {
1737 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1741 Realloc @p s to append the formatted result of @p fmt and return @p
1742 s, which may have moved. Good for gradually accumulating output
1743 into a string buffer.
1745 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1750 s = talloc_vasprintf_append(s, fmt, ap);
1756 Realloc @p s to append the formatted result of @p fmt and return @p
1757 s, which may have moved. Good for gradually accumulating output
1760 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1765 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1771 alloc an array, checking for integer overflow in the array size
1773 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1775 if (count >= MAX_TALLOC_SIZE/el_size) {
1778 return _talloc_named_const(ctx, el_size * count, name);
1782 alloc an zero array, checking for integer overflow in the array size
1784 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1786 if (count >= MAX_TALLOC_SIZE/el_size) {
1789 return _talloc_zero(ctx, el_size * count, name);
1793 realloc an array, checking for integer overflow in the array size
1795 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1797 if (count >= MAX_TALLOC_SIZE/el_size) {
1800 return _talloc_realloc(ctx, ptr, el_size * count, name);
1804 a function version of talloc_realloc(), so it can be passed as a function pointer
1805 to libraries that want a realloc function (a realloc function encapsulates
1806 all the basic capabilities of an allocation library, which is why this is useful)
1808 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1810 return _talloc_realloc(context, ptr, size, NULL);
1814 static int talloc_autofree_destructor(void *ptr)
1816 autofree_context = NULL;
1820 static void talloc_autofree(void)
1822 talloc_free(autofree_context);
1826 return a context which will be auto-freed on exit
1827 this is useful for reducing the noise in leak reports
1829 void *talloc_autofree_context(void)
1831 if (autofree_context == NULL) {
1832 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1833 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1834 atexit(talloc_autofree);
1836 return autofree_context;
1839 size_t talloc_get_size(const void *context)
1841 struct talloc_chunk *tc;
1843 if (context == NULL) {
1844 context = null_context;
1846 if (context == NULL) {
1850 tc = talloc_chunk_from_ptr(context);
1856 find a parent of this context that has the given name, if any
1858 void *talloc_find_parent_byname(const void *context, const char *name)
1860 struct talloc_chunk *tc;
1862 if (context == NULL) {
1866 tc = talloc_chunk_from_ptr(context);
1868 if (tc->name && strcmp(tc->name, name) == 0) {
1869 return TC_PTR_FROM_CHUNK(tc);
1871 while (tc && tc->prev) tc = tc->prev;
1880 show the parentage of a context
1882 void talloc_show_parents(const void *context, FILE *file)
1884 struct talloc_chunk *tc;
1886 if (context == NULL) {
1887 fprintf(file, "talloc no parents for NULL\n");
1891 tc = talloc_chunk_from_ptr(context);
1892 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1894 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1895 while (tc && tc->prev) tc = tc->prev;
1904 return 1 if ptr is a parent of context
1906 int talloc_is_parent(const void *context, const void *ptr)
1908 struct talloc_chunk *tc;
1910 if (context == NULL) {
1914 tc = talloc_chunk_from_ptr(context);
1916 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1917 while (tc && tc->prev) tc = tc->prev;