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_V1 0xe814ec70
61 #define TALLOC_MAGIC_V2 0xe814ec80
62 #define TALLOC_MAGIC TALLOC_MAGIC_V2
63 #define TALLOC_FLAG_FREE 0x01
64 #define TALLOC_FLAG_LOOP 0x02
65 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
66 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
67 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
69 /* by default we abort when given a bad pointer (such as when talloc_free() is called
70 on a pointer that came from malloc() */
72 #define TALLOC_ABORT(reason) abort()
75 #ifndef discard_const_p
76 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
77 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
79 # define discard_const_p(type, ptr) ((type *)(ptr))
83 /* these macros gain us a few percent of speed on gcc */
85 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
86 as its first argument */
88 #define likely(x) __builtin_expect(!!(x), 1)
91 #define unlikely(x) __builtin_expect(!!(x), 0)
98 #define unlikely(x) (x)
102 /* this null_context is only used if talloc_enable_leak_report() or
103 talloc_enable_leak_report_full() is called, otherwise it remains
106 static void *null_context;
107 static void *autofree_context;
109 struct talloc_reference_handle {
110 struct talloc_reference_handle *next, *prev;
112 const char *location;
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_magic_v1(void)
162 talloc_abort("Bad talloc magic value - old magic v1 used");
165 static void talloc_abort_double_free(void)
167 talloc_abort("Bad talloc magic value - double 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 & (~0xF)) == TALLOC_MAGIC_V1) {
182 talloc_abort_magic_v1();
186 if (tc->flags & TALLOC_FLAG_FREE) {
187 talloc_abort_double_free();
190 talloc_abort_unknown_value();
197 /* hook into the front of the list */
198 #define _TLIST_ADD(list, p) \
202 (p)->next = (p)->prev = NULL; \
204 (list)->prev = (p); \
205 (p)->next = (list); \
211 /* remove an element from a list - element doesn't have to be in list. */
212 #define _TLIST_REMOVE(list, p) \
214 if ((p) == (list)) { \
215 (list) = (p)->next; \
216 if (list) (list)->prev = NULL; \
218 if ((p)->prev) (p)->prev->next = (p)->next; \
219 if ((p)->next) (p)->next->prev = (p)->prev; \
221 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
226 return the parent chunk of a pointer
228 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
230 struct talloc_chunk *tc;
232 if (unlikely(ptr == NULL)) {
236 tc = talloc_chunk_from_ptr(ptr);
237 while (tc->prev) tc=tc->prev;
242 void *talloc_parent(const void *ptr)
244 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
245 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
251 const char *talloc_parent_name(const void *ptr)
253 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
254 return tc? tc->name : NULL;
258 A pool carries an in-pool object count count in the first 16 bytes.
259 bytes. This is done to support talloc_steal() to a parent outside of the
260 pool. The count includes the pool itself, so a talloc_free() on a pool will
261 only destroy the pool if the count has dropped to zero. A talloc_free() of a
262 pool member will reduce the count, and eventually also call free(3) on the
265 The object count is not put into "struct talloc_chunk" because it is only
266 relevant for talloc pools and the alignment to 16 bytes would increase the
267 memory footprint of each talloc chunk by those 16 bytes.
270 #define TALLOC_POOL_HDR_SIZE 16
272 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
274 return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
281 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
284 struct talloc_chunk *pool_ctx = NULL;
286 struct talloc_chunk *result;
289 if (parent == NULL) {
293 if (parent->flags & TALLOC_FLAG_POOL) {
296 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
297 pool_ctx = (struct talloc_chunk *)parent->pool;
300 if (pool_ctx == NULL) {
304 space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
305 - ((char *)pool_ctx->pool);
308 * Align size to 16 bytes
310 chunk_size = ((size + 15) & ~15);
312 if (space_left < chunk_size) {
316 result = (struct talloc_chunk *)pool_ctx->pool;
318 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
319 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
322 pool_ctx->pool = (void *)((char *)result + chunk_size);
324 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
325 result->pool = pool_ctx;
327 *talloc_pool_objectcount(pool_ctx) += 1;
333 Allocate a bit of memory as a child of an existing pointer
335 static inline void *__talloc(const void *context, size_t size)
337 struct talloc_chunk *tc = NULL;
339 if (unlikely(context == NULL)) {
340 context = null_context;
343 if (unlikely(size >= MAX_TALLOC_SIZE)) {
347 if (context != NULL) {
348 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
353 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
354 if (unlikely(tc == NULL)) return NULL;
355 tc->flags = TALLOC_MAGIC;
360 tc->destructor = NULL;
365 if (likely(context)) {
366 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
369 parent->child->parent = NULL;
370 tc->next = parent->child;
379 tc->next = tc->prev = tc->parent = NULL;
382 return TC_PTR_FROM_CHUNK(tc);
386 * Create a talloc pool
389 void *talloc_pool(const void *context, size_t size)
391 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
392 struct talloc_chunk *tc;
394 if (unlikely(result == NULL)) {
398 tc = talloc_chunk_from_ptr(result);
400 tc->flags |= TALLOC_FLAG_POOL;
401 tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
403 *talloc_pool_objectcount(tc) = 1;
405 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
406 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
413 setup a destructor to be called on free of a pointer
414 the destructor should return 0 on success, or -1 on failure.
415 if the destructor fails then the free is failed, and the memory can
416 be continued to be used
418 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
420 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
421 tc->destructor = destructor;
425 increase the reference count on a piece of memory.
427 int talloc_increase_ref_count(const void *ptr)
429 if (unlikely(!talloc_reference(null_context, ptr))) {
436 helper for talloc_reference()
438 this is referenced by a function pointer and should not be inline
440 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
442 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
443 _TLIST_REMOVE(ptr_tc->refs, handle);
448 more efficient way to add a name to a pointer - the name must point to a
451 static inline void _talloc_set_name_const(const void *ptr, const char *name)
453 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
458 internal talloc_named_const()
460 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
464 ptr = __talloc(context, size);
465 if (unlikely(ptr == NULL)) {
469 _talloc_set_name_const(ptr, name);
475 make a secondary reference to a pointer, hanging off the given context.
476 the pointer remains valid until both the original caller and this given
479 the major use for this is when two different structures need to reference the
480 same underlying data, and you want to be able to free the two instances separately,
483 void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
485 struct talloc_chunk *tc;
486 struct talloc_reference_handle *handle;
487 if (unlikely(ptr == NULL)) return NULL;
489 tc = talloc_chunk_from_ptr(ptr);
490 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
491 sizeof(struct talloc_reference_handle),
492 TALLOC_MAGIC_REFERENCE);
493 if (unlikely(handle == NULL)) return NULL;
495 /* note that we hang the destructor off the handle, not the
496 main context as that allows the caller to still setup their
497 own destructor on the context if they want to */
498 talloc_set_destructor(handle, talloc_reference_destructor);
499 handle->ptr = discard_const_p(void, ptr);
500 handle->location = location;
501 _TLIST_ADD(tc->refs, handle);
505 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
508 internal talloc_free call
510 static inline int _talloc_free_internal(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_internal(tc->refs);
532 return _talloc_free_internal(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_internal(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_internal(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!");
604 *pool_object_count -= 1;
606 if (*pool_object_count == 0) {
617 move a lump of memory from one talloc context to another return the
618 ptr on success, or NULL if it could not be transferred.
619 passing NULL as ptr will always return NULL with no side effects.
621 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
623 struct talloc_chunk *tc, *new_tc;
625 if (unlikely(!ptr)) {
629 if (unlikely(new_ctx == NULL)) {
630 new_ctx = null_context;
633 tc = talloc_chunk_from_ptr(ptr);
635 if (unlikely(new_ctx == NULL)) {
637 _TLIST_REMOVE(tc->parent->child, tc);
638 if (tc->parent->child) {
639 tc->parent->child->parent = tc->parent;
642 if (tc->prev) tc->prev->next = tc->next;
643 if (tc->next) tc->next->prev = tc->prev;
646 tc->parent = tc->next = tc->prev = NULL;
647 return discard_const_p(void, ptr);
650 new_tc = talloc_chunk_from_ptr(new_ctx);
652 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
653 return discard_const_p(void, ptr);
657 _TLIST_REMOVE(tc->parent->child, tc);
658 if (tc->parent->child) {
659 tc->parent->child->parent = tc->parent;
662 if (tc->prev) tc->prev->next = tc->next;
663 if (tc->next) tc->next->prev = tc->prev;
667 if (new_tc->child) new_tc->child->parent = NULL;
668 _TLIST_ADD(new_tc->child, tc);
670 return discard_const_p(void, ptr);
674 move a lump of memory from one talloc context to another return the
675 ptr on success, or NULL if it could not be transferred.
676 passing NULL as ptr will always return NULL with no side effects.
678 void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
680 struct talloc_chunk *tc;
682 if (unlikely(ptr == NULL)) {
686 tc = talloc_chunk_from_ptr(ptr);
688 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
689 struct talloc_reference_handle *h;
691 fprintf(stderr, "WARNING: talloc_steal with references at %s\n", location);
693 for (h=tc->refs; h; h=h->next) {
695 fprintf(stderr, "\treference at %s\n", h->location);
700 return _talloc_steal_internal(new_ctx, ptr);
704 this is like a talloc_steal(), but you must supply the old
705 parent. This resolves the ambiguity in a talloc_steal() which is
706 called on a context that has more than one parent (via references)
708 The old parent can be either a reference or a parent
710 void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
712 struct talloc_chunk *tc;
713 struct talloc_reference_handle *h;
715 if (unlikely(ptr == NULL)) {
719 if (old_parent == talloc_parent(ptr)) {
720 return _talloc_steal_internal(new_parent, ptr);
723 tc = talloc_chunk_from_ptr(ptr);
724 for (h=tc->refs;h;h=h->next) {
725 if (talloc_parent(h) == old_parent) {
726 if (_talloc_steal_internal(new_parent, h) != h) {
733 /* it wasn't a parent */
738 remove a secondary reference to a pointer. This undo's what
739 talloc_reference() has done. The context and pointer arguments
740 must match those given to a talloc_reference()
742 static inline int talloc_unreference(const void *context, const void *ptr)
744 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
745 struct talloc_reference_handle *h;
747 if (unlikely(context == NULL)) {
748 context = null_context;
751 for (h=tc->refs;h;h=h->next) {
752 struct talloc_chunk *p = talloc_parent_chunk(h);
754 if (context == NULL) break;
755 } else if (TC_PTR_FROM_CHUNK(p) == context) {
763 return _talloc_free_internal(h);
767 remove a specific parent context from a pointer. This is a more
768 controlled varient of talloc_free()
770 int talloc_unlink(const void *context, void *ptr)
772 struct talloc_chunk *tc_p, *new_p;
779 if (context == NULL) {
780 context = null_context;
783 if (talloc_unreference(context, ptr) == 0) {
787 if (context == NULL) {
788 if (talloc_parent_chunk(ptr) != NULL) {
792 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
797 tc_p = talloc_chunk_from_ptr(ptr);
799 if (tc_p->refs == NULL) {
800 return _talloc_free_internal(ptr);
803 new_p = talloc_parent_chunk(tc_p->refs);
805 new_parent = TC_PTR_FROM_CHUNK(new_p);
810 if (talloc_unreference(new_parent, ptr) != 0) {
814 _talloc_steal_internal(new_parent, ptr);
820 add a name to an existing pointer - va_list version
822 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
824 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
826 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
827 tc->name = talloc_vasprintf(ptr, fmt, ap);
828 if (likely(tc->name)) {
829 _talloc_set_name_const(tc->name, ".name");
835 add a name to an existing pointer
837 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
842 name = talloc_set_name_v(ptr, fmt, ap);
849 create a named talloc pointer. Any talloc pointer can be named, and
850 talloc_named() operates just like talloc() except that it allows you
853 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
859 ptr = __talloc(context, size);
860 if (unlikely(ptr == NULL)) return NULL;
863 name = talloc_set_name_v(ptr, fmt, ap);
866 if (unlikely(name == NULL)) {
867 _talloc_free_internal(ptr);
875 return the name of a talloc ptr, or "UNNAMED"
877 const char *talloc_get_name(const void *ptr)
879 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
880 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
883 if (likely(tc->name)) {
891 check if a pointer has the given name. If it does, return the pointer,
892 otherwise return NULL
894 void *talloc_check_name(const void *ptr, const char *name)
897 if (unlikely(ptr == NULL)) return NULL;
898 pname = talloc_get_name(ptr);
899 if (likely(pname == name || strcmp(pname, name) == 0)) {
900 return discard_const_p(void, ptr);
905 static void talloc_abort_type_missmatch(const char *location,
907 const char *expected)
911 reason = talloc_asprintf(NULL,
912 "%s: Type mismatch: name[%s] expected[%s]",
917 reason = "Type mismatch";
920 talloc_abort(reason);
923 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
927 if (unlikely(ptr == NULL)) {
928 talloc_abort_type_missmatch(location, NULL, name);
932 pname = talloc_get_name(ptr);
933 if (likely(pname == name || strcmp(pname, name) == 0)) {
934 return discard_const_p(void, ptr);
937 talloc_abort_type_missmatch(location, pname, name);
942 this is for compatibility with older versions of talloc
944 void *talloc_init(const char *fmt, ...)
951 * samba3 expects talloc_report_depth_cb(NULL, ...)
952 * reports all talloc'ed memory, so we need to enable
955 talloc_enable_null_tracking();
957 ptr = __talloc(NULL, 0);
958 if (unlikely(ptr == NULL)) return NULL;
961 name = talloc_set_name_v(ptr, fmt, ap);
964 if (unlikely(name == NULL)) {
965 _talloc_free_internal(ptr);
973 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
974 should probably not be used in new code. It's in here to keep the talloc
975 code consistent across Samba 3 and 4.
977 void talloc_free_children(void *ptr)
979 struct talloc_chunk *tc;
981 if (unlikely(ptr == NULL)) {
985 tc = talloc_chunk_from_ptr(ptr);
988 /* we need to work out who will own an abandoned child
989 if it cannot be freed. In priority order, the first
990 choice is owner of any remaining reference to this
991 pointer, the second choice is our parent, and the
992 final choice is the null context. */
993 void *child = TC_PTR_FROM_CHUNK(tc->child);
994 const void *new_parent = null_context;
995 if (unlikely(tc->child->refs)) {
996 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
997 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
999 if (unlikely(talloc_free(child) == -1)) {
1000 if (new_parent == null_context) {
1001 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1002 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1004 _talloc_steal_internal(new_parent, child);
1008 if ((tc->flags & TALLOC_FLAG_POOL)
1009 && (*talloc_pool_objectcount(tc) == 1)) {
1010 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
1011 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
1012 VALGRIND_MAKE_MEM_NOACCESS(
1013 tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
1019 Allocate a bit of memory as a child of an existing pointer
1021 void *_talloc(const void *context, size_t size)
1023 return __talloc(context, size);
1027 externally callable talloc_set_name_const()
1029 void talloc_set_name_const(const void *ptr, const char *name)
1031 _talloc_set_name_const(ptr, name);
1035 create a named talloc pointer. Any talloc pointer can be named, and
1036 talloc_named() operates just like talloc() except that it allows you
1037 to name the pointer.
1039 void *talloc_named_const(const void *context, size_t size, const char *name)
1041 return _talloc_named_const(context, size, name);
1045 free a talloc pointer. This also frees all child pointers of this
1048 return 0 if the memory is actually freed, otherwise -1. The memory
1049 will not be freed if the ref_count is > 1 or the destructor (if
1050 any) returns non-zero
1052 int _talloc_free(void *ptr, const char *location)
1054 struct talloc_chunk *tc;
1056 if (unlikely(ptr == NULL)) {
1060 tc = talloc_chunk_from_ptr(ptr);
1062 if (unlikely(tc->refs != NULL)) {
1063 struct talloc_reference_handle *h;
1065 fprintf(stderr, "ERROR: talloc_free with references at %s\n", location);
1067 for (h=tc->refs; h; h=h->next) {
1069 fprintf(stderr, "\treference at %s\n", h->location);
1075 return _talloc_free_internal(ptr);
1081 A talloc version of realloc. The context argument is only used if
1084 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1086 struct talloc_chunk *tc;
1088 bool malloced = false;
1090 /* size zero is equivalent to free() */
1091 if (unlikely(size == 0)) {
1092 talloc_unlink(context, ptr);
1096 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1100 /* realloc(NULL) is equivalent to malloc() */
1102 return _talloc_named_const(context, size, name);
1105 tc = talloc_chunk_from_ptr(ptr);
1107 /* don't allow realloc on referenced pointers */
1108 if (unlikely(tc->refs)) {
1112 /* don't let anybody try to realloc a talloc_pool */
1113 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1117 /* don't shrink if we have less than 1k to gain */
1118 if ((size < tc->size) && ((tc->size - size) < 1024)) {
1123 /* by resetting magic we catch users of the old memory */
1124 tc->flags |= TALLOC_FLAG_FREE;
1127 new_ptr = malloc(size + TC_HDR_SIZE);
1129 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
1133 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1135 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1136 *talloc_pool_objectcount((struct talloc_chunk *)
1139 if (new_ptr == NULL) {
1140 new_ptr = malloc(TC_HDR_SIZE+size);
1145 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1149 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1152 if (unlikely(!new_ptr)) {
1153 tc->flags &= ~TALLOC_FLAG_FREE;
1157 tc = (struct talloc_chunk *)new_ptr;
1158 tc->flags &= ~TALLOC_FLAG_FREE;
1160 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1163 tc->parent->child = tc;
1166 tc->child->parent = tc;
1170 tc->prev->next = tc;
1173 tc->next->prev = tc;
1177 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1179 return TC_PTR_FROM_CHUNK(tc);
1183 a wrapper around talloc_steal() for situations where you are moving a pointer
1184 between two structures, and want the old pointer to be set to NULL
1186 void *_talloc_move(const void *new_ctx, const void *_pptr)
1188 const void **pptr = discard_const_p(const void *,_pptr);
1189 void *ret = talloc_steal(new_ctx, (void *)*pptr);
1195 return the total size of a talloc pool (subtree)
1197 size_t talloc_total_size(const void *ptr)
1200 struct talloc_chunk *c, *tc;
1209 tc = talloc_chunk_from_ptr(ptr);
1211 if (tc->flags & TALLOC_FLAG_LOOP) {
1215 tc->flags |= TALLOC_FLAG_LOOP;
1217 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1220 for (c=tc->child;c;c=c->next) {
1221 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1224 tc->flags &= ~TALLOC_FLAG_LOOP;
1230 return the total number of blocks in a talloc pool (subtree)
1232 size_t talloc_total_blocks(const void *ptr)
1235 struct talloc_chunk *c, *tc;
1244 tc = talloc_chunk_from_ptr(ptr);
1246 if (tc->flags & TALLOC_FLAG_LOOP) {
1250 tc->flags |= TALLOC_FLAG_LOOP;
1253 for (c=tc->child;c;c=c->next) {
1254 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1257 tc->flags &= ~TALLOC_FLAG_LOOP;
1263 return the number of external references to a pointer
1265 size_t talloc_reference_count(const void *ptr)
1267 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1268 struct talloc_reference_handle *h;
1271 for (h=tc->refs;h;h=h->next) {
1278 report on memory usage by all children of a pointer, giving a full tree view
1280 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1281 void (*callback)(const void *ptr,
1282 int depth, int max_depth,
1284 void *private_data),
1287 struct talloc_chunk *c, *tc;
1292 if (ptr == NULL) return;
1294 tc = talloc_chunk_from_ptr(ptr);
1296 if (tc->flags & TALLOC_FLAG_LOOP) {
1300 callback(ptr, depth, max_depth, 0, private_data);
1302 if (max_depth >= 0 && depth >= max_depth) {
1306 tc->flags |= TALLOC_FLAG_LOOP;
1307 for (c=tc->child;c;c=c->next) {
1308 if (c->name == TALLOC_MAGIC_REFERENCE) {
1309 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1310 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1312 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1315 tc->flags &= ~TALLOC_FLAG_LOOP;
1318 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1320 const char *name = talloc_get_name(ptr);
1321 FILE *f = (FILE *)_f;
1324 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1329 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1330 (max_depth < 0 ? "full " :""), name,
1331 (unsigned long)talloc_total_size(ptr),
1332 (unsigned long)talloc_total_blocks(ptr));
1336 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1339 (unsigned long)talloc_total_size(ptr),
1340 (unsigned long)talloc_total_blocks(ptr),
1341 (int)talloc_reference_count(ptr), ptr);
1344 fprintf(f, "content: ");
1345 if (talloc_total_size(ptr)) {
1346 int tot = talloc_total_size(ptr);
1349 for (i = 0; i < tot; i++) {
1350 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1351 fprintf(f, "%c", ((char *)ptr)[i]);
1353 fprintf(f, "~%02x", ((char *)ptr)[i]);
1362 report on memory usage by all children of a pointer, giving a full tree view
1364 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1366 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1371 report on memory usage by all children of a pointer, giving a full tree view
1373 void talloc_report_full(const void *ptr, FILE *f)
1375 talloc_report_depth_file(ptr, 0, -1, f);
1379 report on memory usage by all children of a pointer
1381 void talloc_report(const void *ptr, FILE *f)
1383 talloc_report_depth_file(ptr, 0, 1, f);
1387 report on any memory hanging off the null context
1389 static void talloc_report_null(void)
1391 if (talloc_total_size(null_context) != 0) {
1392 talloc_report(null_context, stderr);
1397 report on any memory hanging off the null context
1399 static void talloc_report_null_full(void)
1401 if (talloc_total_size(null_context) != 0) {
1402 talloc_report_full(null_context, stderr);
1407 enable tracking of the NULL context
1409 void talloc_enable_null_tracking(void)
1411 if (null_context == NULL) {
1412 null_context = _talloc_named_const(NULL, 0, "null_context");
1417 disable tracking of the NULL context
1419 void talloc_disable_null_tracking(void)
1421 talloc_free(null_context);
1422 null_context = NULL;
1426 enable leak reporting on exit
1428 void talloc_enable_leak_report(void)
1430 talloc_enable_null_tracking();
1431 atexit(talloc_report_null);
1435 enable full leak reporting on exit
1437 void talloc_enable_leak_report_full(void)
1439 talloc_enable_null_tracking();
1440 atexit(talloc_report_null_full);
1444 talloc and zero memory.
1446 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1448 void *p = _talloc_named_const(ctx, size, name);
1451 memset(p, '\0', size);
1458 memdup with a talloc.
1460 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1462 void *newp = _talloc_named_const(t, size, name);
1465 memcpy(newp, p, size);
1471 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1475 ret = (char *)__talloc(t, len + 1);
1476 if (unlikely(!ret)) return NULL;
1478 memcpy(ret, p, len);
1481 _talloc_set_name_const(ret, ret);
1486 strdup with a talloc
1488 char *talloc_strdup(const void *t, const char *p)
1490 if (unlikely(!p)) return NULL;
1491 return __talloc_strlendup(t, p, strlen(p));
1495 strndup with a talloc
1497 char *talloc_strndup(const void *t, const char *p, size_t n)
1499 if (unlikely(!p)) return NULL;
1500 return __talloc_strlendup(t, p, strnlen(p, n));
1503 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1504 const char *a, size_t alen)
1508 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1509 if (unlikely(!ret)) return NULL;
1511 /* append the string and the trailing \0 */
1512 memcpy(&ret[slen], a, alen);
1515 _talloc_set_name_const(ret, ret);
1520 * Appends at the end of the string.
1522 char *talloc_strdup_append(char *s, const char *a)
1525 return talloc_strdup(NULL, a);
1532 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1536 * Appends at the end of the talloc'ed buffer,
1537 * not the end of the string.
1539 char *talloc_strdup_append_buffer(char *s, const char *a)
1544 return talloc_strdup(NULL, a);
1551 slen = talloc_get_size(s);
1552 if (likely(slen > 0)) {
1556 return __talloc_strlendup_append(s, slen, a, strlen(a));
1560 * Appends at the end of the string.
1562 char *talloc_strndup_append(char *s, const char *a, size_t n)
1565 return talloc_strdup(NULL, a);
1572 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1576 * Appends at the end of the talloc'ed buffer,
1577 * not the end of the string.
1579 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1584 return talloc_strdup(NULL, a);
1591 slen = talloc_get_size(s);
1592 if (likely(slen > 0)) {
1596 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1599 #ifndef HAVE_VA_COPY
1600 #ifdef HAVE___VA_COPY
1601 #define va_copy(dest, src) __va_copy(dest, src)
1603 #define va_copy(dest, src) (dest) = (src)
1607 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1614 /* this call looks strange, but it makes it work on older solaris boxes */
1616 len = vsnprintf(&c, 1, fmt, ap2);
1618 if (unlikely(len < 0)) {
1622 ret = (char *)__talloc(t, len+1);
1623 if (unlikely(!ret)) return NULL;
1626 vsnprintf(ret, len+1, fmt, ap2);
1629 _talloc_set_name_const(ret, ret);
1635 Perform string formatting, and return a pointer to newly allocated
1636 memory holding the result, inside a memory pool.
1638 char *talloc_asprintf(const void *t, const char *fmt, ...)
1644 ret = talloc_vasprintf(t, fmt, ap);
1649 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1650 const char *fmt, va_list ap)
1651 PRINTF_ATTRIBUTE(3,0);
1653 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1654 const char *fmt, va_list ap)
1661 alen = vsnprintf(&c, 1, fmt, ap2);
1665 /* Either the vsnprintf failed or the format resulted in
1666 * no characters being formatted. In the former case, we
1667 * ought to return NULL, in the latter we ought to return
1668 * the original string. Most current callers of this
1669 * function expect it to never return NULL.
1674 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1675 if (!s) return NULL;
1678 vsnprintf(s + slen, alen + 1, fmt, ap2);
1681 _talloc_set_name_const(s, s);
1686 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1687 * and return @p s, which may have moved. Good for gradually
1688 * accumulating output into a string buffer. Appends at the end
1691 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1694 return talloc_vasprintf(NULL, fmt, ap);
1697 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1701 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1702 * and return @p s, which may have moved. Always appends at the
1703 * end of the talloc'ed buffer, not the end of the string.
1705 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1710 return talloc_vasprintf(NULL, fmt, ap);
1713 slen = talloc_get_size(s);
1714 if (likely(slen > 0)) {
1718 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1722 Realloc @p s to append the formatted result of @p fmt and return @p
1723 s, which may have moved. Good for gradually accumulating output
1724 into a string buffer.
1726 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1731 s = talloc_vasprintf_append(s, fmt, ap);
1737 Realloc @p s to append the formatted result of @p fmt and return @p
1738 s, which may have moved. Good for gradually accumulating output
1741 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1746 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1752 alloc an array, checking for integer overflow in the array size
1754 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1756 if (count >= MAX_TALLOC_SIZE/el_size) {
1759 return _talloc_named_const(ctx, el_size * count, name);
1763 alloc an zero array, checking for integer overflow in the array size
1765 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1767 if (count >= MAX_TALLOC_SIZE/el_size) {
1770 return _talloc_zero(ctx, el_size * count, name);
1774 realloc an array, checking for integer overflow in the array size
1776 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1778 if (count >= MAX_TALLOC_SIZE/el_size) {
1781 return _talloc_realloc(ctx, ptr, el_size * count, name);
1785 a function version of talloc_realloc(), so it can be passed as a function pointer
1786 to libraries that want a realloc function (a realloc function encapsulates
1787 all the basic capabilities of an allocation library, which is why this is useful)
1789 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1791 return _talloc_realloc(context, ptr, size, NULL);
1795 static int talloc_autofree_destructor(void *ptr)
1797 autofree_context = NULL;
1801 static void talloc_autofree(void)
1803 talloc_free(autofree_context);
1807 return a context which will be auto-freed on exit
1808 this is useful for reducing the noise in leak reports
1810 void *talloc_autofree_context(void)
1812 if (autofree_context == NULL) {
1813 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1814 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1815 atexit(talloc_autofree);
1817 return autofree_context;
1820 size_t talloc_get_size(const void *context)
1822 struct talloc_chunk *tc;
1824 if (context == NULL) {
1825 context = null_context;
1827 if (context == NULL) {
1831 tc = talloc_chunk_from_ptr(context);
1837 find a parent of this context that has the given name, if any
1839 void *talloc_find_parent_byname(const void *context, const char *name)
1841 struct talloc_chunk *tc;
1843 if (context == NULL) {
1847 tc = talloc_chunk_from_ptr(context);
1849 if (tc->name && strcmp(tc->name, name) == 0) {
1850 return TC_PTR_FROM_CHUNK(tc);
1852 while (tc && tc->prev) tc = tc->prev;
1861 show the parentage of a context
1863 void talloc_show_parents(const void *context, FILE *file)
1865 struct talloc_chunk *tc;
1867 if (context == NULL) {
1868 fprintf(file, "talloc no parents for NULL\n");
1872 tc = talloc_chunk_from_ptr(context);
1873 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1875 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1876 while (tc && tc->prev) tc = tc->prev;
1885 return 1 if ptr is a parent of context
1887 int talloc_is_parent(const void *context, const void *ptr)
1889 struct talloc_chunk *tc;
1891 if (context == NULL) {
1895 tc = talloc_chunk_from_ptr(context);
1897 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1898 while (tc && tc->prev) tc = tc->prev;
1909 /* ABI compat functions (do NOT append anything beyond thess functions,
1910 * keep them as the last ones in the file) */
1912 static const char *talloc_ABI_compat_location = "Called from compatibility function";
1914 /* ABI compat function (don't use) */
1915 void *_talloc_reference(const void *context, const void *ptr) {
1916 return _talloc_reference_loc(context, ptr, talloc_ABI_compat_location);
1919 /* ABI compat function (don't use) */
1920 void *_talloc_steal(const void *new_ctx, const void *ptr)
1922 return _talloc_steal_internal(new_ctx, ptr);
1926 int talloc_free(void *ptr);
1927 int talloc_free(void *ptr)
1929 return _talloc_free_internal(ptr);
1932 /* DO NOT APPEND ANYTHING BEYOND THIS POINT */