2 Samba Unix SMB/CIFS implementation.
4 Samba trivial allocation library - new interface
6 NOTE: Please read talloc_guide.txt for full documentation
8 Copyright (C) Andrew Tridgell 2004
9 Copyright (C) Stefan Metzmacher 2006
11 ** NOTE! The following LGPL license applies to the talloc
12 ** library. This does NOT imply that all of Samba is released
15 This library is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Lesser General Public
17 License as published by the Free Software Foundation; either
18 version 3 of the License, or (at your option) any later version.
20 This library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Lesser General Public License for more details.
25 You should have received a copy of the GNU Lesser General Public
26 License along with this library; if not, see <http://www.gnu.org/licenses/>.
30 inspired by http://swapped.cc/halloc/
35 #if (SAMBA_VERSION_MAJOR<4)
37 /* This is to circumvent SAMBA3's paranoid malloc checker. Here in this file
38 * we trust ourselves... */
45 #define _TALLOC_SAMBA3
46 #endif /* (SAMBA_VERSION_MAJOR<4) */
47 #endif /* _SAMBA_BUILD_ */
49 #ifndef _TALLOC_SAMBA3
52 #endif /* not _TALLOC_SAMBA3 */
54 /* use this to force every realloc to change the pointer, to stress test
55 code that might not cope */
56 #define ALWAYS_REALLOC 0
59 #define MAX_TALLOC_SIZE 0x10000000
60 #define TALLOC_MAGIC 0xe814ec70
61 #define TALLOC_FLAG_FREE 0x01
62 #define TALLOC_FLAG_LOOP 0x02
63 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
64 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
65 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
66 #define TALLOC_MAGIC_NO_OWNER ((void *)1)
67 #define TALLOC_MAGIC_NO_OWNER_TC ((struct talloc_chunk *)1)
69 /* by default we abort when given a bad pointer (such as when talloc_free() is called
70 on a pointer that came from malloc() */
72 #define TALLOC_ABORT(reason) abort()
75 #ifndef discard_const_p
76 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
77 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
79 # define discard_const_p(type, ptr) ((type *)(ptr))
83 /* these macros gain us a few percent of speed on gcc */
85 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
86 as its first argument */
88 #define likely(x) __builtin_expect(!!(x), 1)
91 #define unlikely(x) __builtin_expect(!!(x), 0)
98 #define unlikely(x) (x)
102 /* this null_context is only used if talloc_enable_leak_report() or
103 talloc_enable_leak_report_full() is called, otherwise it remains
106 static void *null_context;
107 static void *autofree_context;
108 static void *no_owner_context = TALLOC_MAGIC_NO_OWNER;
109 static const char *strict_owner_mode;
110 static inline int _talloc_free(void *ptr);
112 struct talloc_reference_handle {
113 struct talloc_reference_handle *next, *prev;
117 typedef int (*talloc_destructor_t)(void *);
119 struct talloc_chunk {
120 struct talloc_chunk *next, *prev;
121 struct talloc_chunk *parent, *child;
122 struct talloc_reference_handle *refs;
123 talloc_destructor_t destructor;
129 * "pool" has dual use:
131 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
132 * marks the end of the currently allocated area.
134 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
135 * is a pointer to the struct talloc_chunk of the pool that it was
136 * allocated from. This way children can quickly find the pool to chew
142 /* 16 byte alignment seems to keep everyone happy */
143 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
144 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
146 void _talloc_set_strict_owner_mode(const char *location)
148 strict_owner_mode = location;
151 static void (*talloc_abort_fn)(const char *reason);
153 void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
155 talloc_abort_fn = abort_fn;
158 static void talloc_abort(const char *reason)
160 if (!talloc_abort_fn) {
161 TALLOC_ABORT(reason);
164 talloc_abort_fn(reason);
167 static void talloc_abort_double_free(void)
169 talloc_abort("Bad talloc magic value - double free");
172 static void talloc_abort_no_owner_free(void)
174 talloc_abort("Bad talloc parent - no_owner free");
177 static void talloc_abort_unknown_value(void)
179 talloc_abort("Bad talloc magic value - unknown value");
182 /* panic if we get a bad magic value */
183 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
185 const char *pp = (const char *)ptr;
186 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
187 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
188 if (tc->flags & TALLOC_FLAG_FREE) {
189 talloc_abort_double_free();
192 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; \
226 static inline bool talloc_parent_is_no_owner(const void *ptr)
228 struct talloc_chunk *tc;
229 struct talloc_chunk *no_owner_tc = TALLOC_MAGIC_NO_OWNER_TC;
231 tc = talloc_chunk_from_ptr(ptr);
233 if (no_owner_context != TALLOC_MAGIC_NO_OWNER) {
234 no_owner_tc = talloc_chunk_from_ptr(no_owner_context);
237 if (tc->parent == no_owner_tc) {
244 #define TALLOC_INVALID_PARENT(tc) \
245 ((((tc)->parent == NULL)||((tc)->parent == TALLOC_MAGIC_NO_OWNER_TC)))
248 return the parent chunk of a pointer
250 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
252 struct talloc_chunk *tc;
254 if (unlikely(ptr == NULL)) {
258 tc = talloc_chunk_from_ptr(ptr);
259 while (tc->prev) tc=tc->prev;
261 if (unlikely(TALLOC_INVALID_PARENT(tc))) {
268 void *talloc_parent(const void *ptr)
270 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
271 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
277 const char *talloc_parent_name(const void *ptr)
279 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
280 return tc? tc->name : NULL;
283 static inline void talloc_remove_from_parent(struct talloc_chunk *tc)
285 if (!TALLOC_INVALID_PARENT(tc)) {
286 _TLIST_REMOVE(tc->parent->child, tc);
287 if (tc->parent->child) {
288 tc->parent->child->parent = tc->parent;
291 if (tc->prev) tc->prev->next = tc->next;
292 if (tc->next) tc->next->prev = tc->prev;
294 // tc->parent = tc->prec = tc->next = NULL;
298 A pool carries an in-pool object count count in the first 16 bytes.
299 bytes. This is done to support talloc_steal() to a parent outside of the
300 pool. The count includes the pool itself, so a talloc_free() on a pool will
301 only destroy the pool if the count has dropped to zero. A talloc_free() of a
302 pool member will reduce the count, and eventually also call free(3) on the
305 The object count is not put into "struct talloc_chunk" because it is only
306 relevant for talloc pools and the alignment to 16 bytes would increase the
307 memory footprint of each talloc chunk by those 16 bytes.
310 #define TALLOC_POOL_HDR_SIZE 16
312 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
314 return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
321 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
324 struct talloc_chunk *pool_ctx = NULL;
326 struct talloc_chunk *result;
329 if (parent == NULL) {
333 if (parent->flags & TALLOC_FLAG_POOL) {
336 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
337 pool_ctx = (struct talloc_chunk *)parent->pool;
340 if (pool_ctx == NULL) {
344 space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
345 - ((char *)pool_ctx->pool);
348 * Align size to 16 bytes
350 chunk_size = ((size + 15) & ~15);
352 if (space_left < chunk_size) {
356 result = (struct talloc_chunk *)pool_ctx->pool;
358 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
359 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
362 pool_ctx->pool = (void *)((char *)result + chunk_size);
364 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
365 result->pool = pool_ctx;
367 *talloc_pool_objectcount(pool_ctx) += 1;
373 Allocate a bit of memory as a child of an existing pointer
375 static inline void *__talloc(const void *context, size_t size)
377 struct talloc_chunk *tc = NULL;
379 if (unlikely(context == NULL)) {
380 context = null_context;
383 if (unlikely(size >= MAX_TALLOC_SIZE)) {
387 if (context != NULL) {
388 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
393 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
394 if (unlikely(tc == NULL)) return NULL;
395 tc->flags = TALLOC_MAGIC;
400 tc->destructor = NULL;
405 if (likely(context)) {
406 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
409 parent->child->parent = NULL;
410 tc->next = parent->child;
419 tc->next = tc->prev = tc->parent = NULL;
422 return TC_PTR_FROM_CHUNK(tc);
426 * Create a talloc pool
429 void *talloc_pool(const void *context, size_t size)
431 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
432 struct talloc_chunk *tc;
434 if (unlikely(result == NULL)) {
438 tc = talloc_chunk_from_ptr(result);
440 tc->flags |= TALLOC_FLAG_POOL;
441 tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
443 *talloc_pool_objectcount(tc) = 1;
445 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
446 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
453 setup a destructor to be called on free of a pointer
454 the destructor should return 0 on success, or -1 on failure.
455 if the destructor fails then the free is failed, and the memory can
456 be continued to be used
458 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
460 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
461 tc->destructor = destructor;
465 increase the reference count on a piece of memory.
467 int talloc_increase_ref_count(const void *ptr)
469 if (unlikely(!talloc_reference(null_context, ptr))) {
476 helper for talloc_reference()
478 this is referenced by a function pointer and should not be inline
480 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
482 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
483 _TLIST_REMOVE(ptr_tc->refs, handle);
485 * If ptr_tc has no refs left and is owner by
486 * no_owner_context then we free it
488 if (ptr_tc->refs == NULL &&
489 talloc_parent_is_no_owner(handle->ptr)) {
490 int ret = _talloc_free(handle->ptr);
492 _TLIST_ADD(ptr_tc->refs, handle);
500 more efficient way to add a name to a pointer - the name must point to a
503 static inline void _talloc_set_name_const(const void *ptr, const char *name)
505 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
510 internal talloc_named_const()
512 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
516 ptr = __talloc(context, size);
517 if (unlikely(ptr == NULL)) {
521 _talloc_set_name_const(ptr, name);
527 make a secondary reference to a pointer, hanging off the given context.
528 the pointer remains valid until both the original caller and this given
531 the major use for this is when two different structures need to reference the
532 same underlying data, and you want to be able to free the two instances separately,
535 void *_talloc_reference(const void *context, const void *ptr)
537 struct talloc_chunk *tc;
538 struct talloc_reference_handle *handle;
539 if (unlikely(ptr == NULL)) return NULL;
541 tc = talloc_chunk_from_ptr(ptr);
542 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
543 sizeof(struct talloc_reference_handle),
544 TALLOC_MAGIC_REFERENCE);
545 if (unlikely(handle == NULL)) return NULL;
547 /* note that we hang the destructor off the handle, not the
548 main context as that allows the caller to still setup their
549 own destructor on the context if they want to */
550 talloc_set_destructor(handle, talloc_reference_destructor);
551 handle->ptr = discard_const_p(void, ptr);
552 _TLIST_ADD(tc->refs, handle);
556 static inline void _talloc_free_children(struct talloc_chunk *tc)
558 void *ptr = TC_PTR_FROM_CHUNK(tc);
560 tc->flags |= TALLOC_FLAG_LOOP;
563 void *child = TC_PTR_FROM_CHUNK(tc->child);
564 if (unlikely(_talloc_free(child) == -1)) {
565 const void *new_parent = no_owner_context;
566 struct talloc_chunk *p = talloc_parent_chunk(ptr);
569 * we need to work out who will own an abandoned child
570 * if it cannot be freed. In priority order, the first
571 * choice is our parent, and the final choice is the
575 new_parent = TC_PTR_FROM_CHUNK(p);
579 * if talloc_free fails because of refs and
580 * not due to the destructor, it will already
581 * have a new owner no_owner_context
583 if (!talloc_parent_is_no_owner(child)) {
584 talloc_steal(new_parent, child);
589 tc->flags &= ~TALLOC_FLAG_LOOP;
593 internal talloc_free call
595 static inline int _talloc_free(void *ptr)
597 struct talloc_chunk *tc;
599 if (unlikely(ptr == NULL)) {
603 tc = talloc_chunk_from_ptr(ptr);
605 if (unlikely(tc->refs)) {
606 /*TODO: Fix this loop detection. The code here breaks top reference if
607 it is also one of our children. It is insufficient and can lead
608 to dangling references */
611 /* check this is a reference from a child or grantchild
612 * back to it's parent or grantparent
614 * in that case we need to remove the reference and
615 * call another instance of talloc_free() on the current
618 is_child = talloc_is_parent(tc->refs, ptr);
620 _talloc_free(tc->refs);
621 return _talloc_free(ptr);
622 } else if (talloc_parent_is_no_owner(ptr)) {
624 * if no_owner_context is already the owner
625 * we free the last reference (first in the list)
627 * This path is ownly reached if
628 * talloc_set_strict_owner_mode() wasn't used
630 return _talloc_free(tc->refs);
633 * we can't free if we have refs,
634 * so no_owner_context becomes the owner
636 _talloc_steal(no_owner_context, ptr);
641 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
642 /* we have a free loop - stop looping */
646 if (unlikely(tc->destructor)) {
647 talloc_destructor_t d = tc->destructor;
648 if (d == (talloc_destructor_t)-1) {
651 tc->destructor = (talloc_destructor_t)-1;
656 tc->destructor = NULL;
659 talloc_remove_from_parent(tc);
661 _talloc_free_children(tc);
663 tc->flags |= TALLOC_FLAG_FREE;
665 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
666 struct talloc_chunk *pool;
667 unsigned int *pool_object_count;
669 pool = (tc->flags & TALLOC_FLAG_POOL)
670 ? tc : (struct talloc_chunk *)tc->pool;
672 pool_object_count = talloc_pool_objectcount(pool);
674 if (*pool_object_count == 0) {
675 talloc_abort("Pool object count zero!");
679 *pool_object_count -= 1;
681 if (*pool_object_count == 0) {
692 move a lump of memory from one talloc context to another return the
693 ptr on success, or NULL if it could not be transferred.
694 passing NULL as ptr will always return NULL with no side effects.
696 void *_talloc_steal(const void *new_ctx, const void *ptr)
698 struct talloc_chunk *tc, *new_tc;
699 bool no_owner_null = false;
701 if (unlikely(!ptr)) {
705 if (unlikely(strict_owner_mode &&
706 talloc_parent_is_no_owner(ptr) &&
707 new_ctx != no_owner_context)) {
708 talloc_abort_no_owner_free();
712 if (unlikely(new_ctx == TALLOC_MAGIC_NO_OWNER)) {
713 no_owner_null = true;
717 if (unlikely(new_ctx == NULL)) {
718 new_ctx = null_context;
721 tc = talloc_chunk_from_ptr(ptr);
723 if (unlikely(new_ctx == NULL)) {
724 talloc_remove_from_parent(tc);
726 tc->parent = tc->next = tc->prev = NULL;
727 if (unlikely(no_owner_null)) {
728 tc->parent = TALLOC_MAGIC_NO_OWNER_TC;
730 return discard_const_p(void, ptr);
733 new_tc = talloc_chunk_from_ptr(new_ctx);
735 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
736 return discard_const_p(void, ptr);
739 talloc_remove_from_parent(tc);
742 if (new_tc->child) new_tc->child->parent = NULL;
743 _TLIST_ADD(new_tc->child, tc);
745 return discard_const_p(void, ptr);
751 remove a secondary reference to a pointer. This undo's what
752 talloc_reference() has done. The context and pointer arguments
753 must match those given to a talloc_reference()
755 static inline int talloc_unreference(const void *context, const void *ptr)
757 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
758 struct talloc_reference_handle *h;
760 if (unlikely(context == NULL)) {
761 context = null_context;
764 for (h=tc->refs;h;h=h->next) {
765 struct talloc_chunk *p = talloc_parent_chunk(h);
767 if (context == NULL) break;
768 } else if (TC_PTR_FROM_CHUNK(p) == context) {
776 return _talloc_free(h);
780 remove a specific parent context from a pointer. This is a more
781 controlled varient of talloc_free()
783 int talloc_unlink(const void *context, void *ptr)
785 struct talloc_chunk *tc_p;
791 if (context == NULL) {
792 context = null_context;
795 if (talloc_unreference(context, ptr) == 0) {
799 if (context == NULL) {
800 if (talloc_parent_chunk(ptr) != NULL) {
804 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
809 tc_p = talloc_chunk_from_ptr(ptr);
811 if (tc_p->refs == NULL) {
812 return _talloc_free(ptr);
815 talloc_steal(no_owner_context, 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)) {
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)) {
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);
987 _talloc_free_children(tc);
989 if ((tc->flags & TALLOC_FLAG_POOL)
990 && (*talloc_pool_objectcount(tc) == 1)) {
991 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
992 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
993 VALGRIND_MAKE_MEM_NOACCESS(
994 tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
1000 Allocate a bit of memory as a child of an existing pointer
1002 void *_talloc(const void *context, size_t size)
1004 return __talloc(context, size);
1008 externally callable talloc_set_name_const()
1010 void talloc_set_name_const(const void *ptr, const char *name)
1012 _talloc_set_name_const(ptr, name);
1016 create a named talloc pointer. Any talloc pointer can be named, and
1017 talloc_named() operates just like talloc() except that it allows you
1018 to name the pointer.
1020 void *talloc_named_const(const void *context, size_t size, const char *name)
1022 return _talloc_named_const(context, size, name);
1026 free a talloc pointer. This also frees all child pointers of this
1029 return 0 if the memory is actually freed, otherwise -1. The memory
1030 will not be freed if the ref_count is > 1 or the destructor (if
1031 any) returns non-zero
1033 int talloc_free(void *ptr)
1035 struct talloc_chunk *tc;
1037 if (unlikely(ptr == NULL)) {
1041 tc = talloc_chunk_from_ptr(ptr);
1043 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
1044 /* we have a free loop - stop looping */
1048 if (unlikely(strict_owner_mode &&
1049 talloc_parent_is_no_owner(ptr))) {
1050 talloc_abort_no_owner_free();
1054 return _talloc_free(ptr);
1060 A talloc version of realloc. The context argument is only used if
1063 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1065 struct talloc_chunk *tc;
1067 bool malloced = false;
1069 /* size zero is equivalent to free() */
1070 if (unlikely(size == 0)) {
1075 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1079 /* realloc(NULL) is equivalent to malloc() */
1081 return _talloc_named_const(context, size, name);
1084 tc = talloc_chunk_from_ptr(ptr);
1086 /* don't allow realloc on referenced pointers */
1087 if (unlikely(tc->refs)) {
1091 /* don't let anybody try to realloc a talloc_pool */
1092 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1096 /* don't shrink if we have less than 1k to gain */
1097 if ((size < tc->size) && ((tc->size - size) < 1024)) {
1102 /* by resetting magic we catch users of the old memory */
1103 tc->flags |= TALLOC_FLAG_FREE;
1106 new_ptr = malloc(size + TC_HDR_SIZE);
1108 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
1112 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1114 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1115 *talloc_pool_objectcount((struct talloc_chunk *)
1118 if (new_ptr == NULL) {
1119 new_ptr = malloc(TC_HDR_SIZE+size);
1124 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1128 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1131 if (unlikely(!new_ptr)) {
1132 tc->flags &= ~TALLOC_FLAG_FREE;
1136 tc = (struct talloc_chunk *)new_ptr;
1137 tc->flags &= ~TALLOC_FLAG_FREE;
1139 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1141 if (!TALLOC_INVALID_PARENT(tc)) {
1142 tc->parent->child = tc;
1145 tc->child->parent = tc;
1149 tc->prev->next = tc;
1152 tc->next->prev = tc;
1156 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1158 return TC_PTR_FROM_CHUNK(tc);
1162 a wrapper around talloc_steal() for situations where you are moving a pointer
1163 between two structures, and want the old pointer to be set to NULL
1165 void *_talloc_move(const void *new_ctx, const void *_pptr)
1167 const void **pptr = discard_const_p(const void *,_pptr);
1168 void *ret = _talloc_steal(new_ctx, *pptr);
1174 return the total size of a talloc pool (subtree)
1176 size_t talloc_total_size(const void *ptr)
1179 struct talloc_chunk *c, *tc;
1188 tc = talloc_chunk_from_ptr(ptr);
1190 if (tc->flags & TALLOC_FLAG_LOOP) {
1194 tc->flags |= TALLOC_FLAG_LOOP;
1196 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1199 for (c=tc->child;c;c=c->next) {
1200 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1203 tc->flags &= ~TALLOC_FLAG_LOOP;
1209 return the total number of blocks in a talloc pool (subtree)
1211 size_t talloc_total_blocks(const void *ptr)
1214 struct talloc_chunk *c, *tc;
1223 tc = talloc_chunk_from_ptr(ptr);
1225 if (tc->flags & TALLOC_FLAG_LOOP) {
1229 tc->flags |= TALLOC_FLAG_LOOP;
1232 for (c=tc->child;c;c=c->next) {
1233 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1236 tc->flags &= ~TALLOC_FLAG_LOOP;
1242 return the number of external references to a pointer
1244 size_t talloc_reference_count(const void *ptr)
1246 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1247 struct talloc_reference_handle *h;
1250 for (h=tc->refs;h;h=h->next) {
1257 report on memory usage by all children of a pointer, giving a full tree view
1259 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1260 void (*callback)(const void *ptr,
1261 int depth, int max_depth,
1263 void *private_data),
1266 struct talloc_chunk *c, *tc;
1271 if (ptr == NULL) return;
1273 tc = talloc_chunk_from_ptr(ptr);
1275 if (tc->flags & TALLOC_FLAG_LOOP) {
1279 callback(ptr, depth, max_depth, 0, private_data);
1281 if (max_depth >= 0 && depth >= max_depth) {
1285 tc->flags |= TALLOC_FLAG_LOOP;
1286 for (c=tc->child;c;c=c->next) {
1287 if (c->name == TALLOC_MAGIC_REFERENCE) {
1288 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1289 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1291 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1294 tc->flags &= ~TALLOC_FLAG_LOOP;
1297 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1299 const char *name = talloc_get_name(ptr);
1300 FILE *f = (FILE *)_f;
1303 if (talloc_parent_is_no_owner(ptr)) {
1304 fprintf(f, "%*sreference to: %s [no owner]\n", depth*4, "", name);
1306 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1312 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1313 (max_depth < 0 ? "full " :""), name,
1314 (unsigned long)talloc_total_size(ptr),
1315 (unsigned long)talloc_total_blocks(ptr));
1319 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1322 (unsigned long)talloc_total_size(ptr),
1323 (unsigned long)talloc_total_blocks(ptr),
1324 (int)talloc_reference_count(ptr), ptr);
1327 fprintf(f, "content: ");
1328 if (talloc_total_size(ptr)) {
1329 int tot = talloc_total_size(ptr);
1332 for (i = 0; i < tot; i++) {
1333 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1334 fprintf(f, "%c", ((char *)ptr)[i]);
1336 fprintf(f, "~%02x", ((char *)ptr)[i]);
1345 report on memory usage by all children of a pointer, giving a full tree view
1347 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1349 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1354 report on memory usage by all children of a pointer, giving a full tree view
1356 void talloc_report_full(const void *ptr, FILE *f)
1358 talloc_report_depth_file(ptr, 0, -1, f);
1362 report on memory usage by all children of a pointer
1364 void talloc_report(const void *ptr, FILE *f)
1366 talloc_report_depth_file(ptr, 0, 1, f);
1370 report on any memory hanging off the null context
1372 static void talloc_report_null(void)
1374 if (talloc_total_size(null_context) != 0) {
1375 talloc_report(null_context, stderr);
1380 report on any memory hanging off the null context
1382 static void talloc_report_null_full(void)
1384 if (talloc_total_size(null_context) != 0) {
1385 talloc_report_full(null_context, stderr);
1390 enable tracking of the NULL context
1392 void talloc_enable_null_tracking(void)
1394 if (null_context == NULL) {
1395 null_context = _talloc_named_const(NULL, 0, "null_context");
1396 no_owner_context = _talloc_named_const(null_context, 0,
1397 "no_owner_context");
1402 disable tracking of the NULL context
1404 void talloc_disable_null_tracking(void)
1406 _talloc_free(null_context);
1407 null_context = NULL;
1408 no_owner_context = TALLOC_MAGIC_NO_OWNER;
1412 enable leak reporting on exit
1414 void talloc_enable_leak_report(void)
1416 talloc_enable_null_tracking();
1417 atexit(talloc_report_null);
1421 enable full leak reporting on exit
1423 void talloc_enable_leak_report_full(void)
1425 talloc_enable_null_tracking();
1426 atexit(talloc_report_null_full);
1430 talloc and zero memory.
1432 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1434 void *p = _talloc_named_const(ctx, size, name);
1437 memset(p, '\0', size);
1444 memdup with a talloc.
1446 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1448 void *newp = _talloc_named_const(t, size, name);
1451 memcpy(newp, p, size);
1457 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1461 ret = (char *)__talloc(t, len + 1);
1462 if (unlikely(!ret)) return NULL;
1464 memcpy(ret, p, len);
1467 _talloc_set_name_const(ret, ret);
1472 strdup with a talloc
1474 char *talloc_strdup(const void *t, const char *p)
1476 if (unlikely(!p)) return NULL;
1477 return __talloc_strlendup(t, p, strlen(p));
1481 strndup with a talloc
1483 char *talloc_strndup(const void *t, const char *p, size_t n)
1485 if (unlikely(!p)) return NULL;
1486 return __talloc_strlendup(t, p, strnlen(p, n));
1489 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1490 const char *a, size_t alen)
1494 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1495 if (unlikely(!ret)) return NULL;
1497 /* append the string and the trailing \0 */
1498 memcpy(&ret[slen], a, alen);
1501 _talloc_set_name_const(ret, ret);
1506 * Appends at the end of the string.
1508 char *talloc_strdup_append(char *s, const char *a)
1511 return talloc_strdup(NULL, a);
1518 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1522 * Appends at the end of the talloc'ed buffer,
1523 * not the end of the string.
1525 char *talloc_strdup_append_buffer(char *s, const char *a)
1530 return talloc_strdup(NULL, a);
1537 slen = talloc_get_size(s);
1538 if (likely(slen > 0)) {
1542 return __talloc_strlendup_append(s, slen, a, strlen(a));
1546 * Appends at the end of the string.
1548 char *talloc_strndup_append(char *s, const char *a, size_t n)
1551 return talloc_strdup(NULL, a);
1558 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1562 * Appends at the end of the talloc'ed buffer,
1563 * not the end of the string.
1565 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1570 return talloc_strdup(NULL, a);
1577 slen = talloc_get_size(s);
1578 if (likely(slen > 0)) {
1582 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1585 #ifndef HAVE_VA_COPY
1586 #ifdef HAVE___VA_COPY
1587 #define va_copy(dest, src) __va_copy(dest, src)
1589 #define va_copy(dest, src) (dest) = (src)
1593 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1600 /* this call looks strange, but it makes it work on older solaris boxes */
1602 len = vsnprintf(&c, 1, fmt, ap2);
1604 if (unlikely(len < 0)) {
1608 ret = (char *)__talloc(t, len+1);
1609 if (unlikely(!ret)) return NULL;
1612 vsnprintf(ret, len+1, fmt, ap2);
1615 _talloc_set_name_const(ret, ret);
1621 Perform string formatting, and return a pointer to newly allocated
1622 memory holding the result, inside a memory pool.
1624 char *talloc_asprintf(const void *t, const char *fmt, ...)
1630 ret = talloc_vasprintf(t, fmt, ap);
1635 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1636 const char *fmt, va_list ap)
1637 PRINTF_ATTRIBUTE(3,0);
1639 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1640 const char *fmt, va_list ap)
1647 alen = vsnprintf(&c, 1, fmt, ap2);
1651 /* Either the vsnprintf failed or the format resulted in
1652 * no characters being formatted. In the former case, we
1653 * ought to return NULL, in the latter we ought to return
1654 * the original string. Most current callers of this
1655 * function expect it to never return NULL.
1660 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1661 if (!s) return NULL;
1664 vsnprintf(s + slen, alen + 1, fmt, ap2);
1667 _talloc_set_name_const(s, s);
1672 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1673 * and return @p s, which may have moved. Good for gradually
1674 * accumulating output into a string buffer. Appends at the end
1677 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1680 return talloc_vasprintf(NULL, fmt, ap);
1683 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1687 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1688 * and return @p s, which may have moved. Always appends at the
1689 * end of the talloc'ed buffer, not the end of the string.
1691 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1696 return talloc_vasprintf(NULL, fmt, ap);
1699 slen = talloc_get_size(s);
1700 if (likely(slen > 0)) {
1704 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1708 Realloc @p s to append the formatted result of @p fmt and return @p
1709 s, which may have moved. Good for gradually accumulating output
1710 into a string buffer.
1712 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1717 s = talloc_vasprintf_append(s, fmt, ap);
1723 Realloc @p s to append the formatted result of @p fmt and return @p
1724 s, which may have moved. Good for gradually accumulating output
1727 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1732 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1738 alloc an array, checking for integer overflow in the array size
1740 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1742 if (count >= MAX_TALLOC_SIZE/el_size) {
1745 return _talloc_named_const(ctx, el_size * count, name);
1749 alloc an zero array, checking for integer overflow in the array size
1751 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1753 if (count >= MAX_TALLOC_SIZE/el_size) {
1756 return _talloc_zero(ctx, el_size * count, name);
1760 realloc an array, checking for integer overflow in the array size
1762 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1764 if (count >= MAX_TALLOC_SIZE/el_size) {
1767 return _talloc_realloc(ctx, ptr, el_size * count, name);
1771 a function version of talloc_realloc(), so it can be passed as a function pointer
1772 to libraries that want a realloc function (a realloc function encapsulates
1773 all the basic capabilities of an allocation library, which is why this is useful)
1775 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1777 return _talloc_realloc(context, ptr, size, NULL);
1781 static int talloc_autofree_destructor(void *ptr)
1783 autofree_context = NULL;
1787 static void talloc_autofree(void)
1789 _talloc_free(autofree_context);
1793 return a context which will be auto-freed on exit
1794 this is useful for reducing the noise in leak reports
1796 void *talloc_autofree_context(void)
1798 if (autofree_context == NULL) {
1799 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1800 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1801 atexit(talloc_autofree);
1803 return autofree_context;
1806 size_t talloc_get_size(const void *context)
1808 struct talloc_chunk *tc;
1810 if (context == NULL) {
1811 context = null_context;
1813 if (context == NULL) {
1817 tc = talloc_chunk_from_ptr(context);
1823 find a parent of this context that has the given name, if any
1825 void *talloc_find_parent_byname(const void *context, const char *name)
1827 struct talloc_chunk *tc;
1829 if (context == NULL) {
1833 tc = talloc_chunk_from_ptr(context);
1835 if (tc->name && strcmp(tc->name, name) == 0) {
1836 return TC_PTR_FROM_CHUNK(tc);
1838 while (tc && tc->prev) tc = tc->prev;
1840 if (TALLOC_INVALID_PARENT(tc)) {
1850 show the parentage of a context
1852 void talloc_show_parents(const void *context, FILE *file)
1854 struct talloc_chunk *tc;
1856 if (context == NULL) {
1857 fprintf(file, "talloc no parents for NULL\n");
1861 tc = talloc_chunk_from_ptr(context);
1862 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1864 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1865 while (tc && tc->prev) tc = tc->prev;
1867 if (tc->parent == TALLOC_MAGIC_NO_OWNER_TC) {
1868 fprintf(file, "\t'no_owner_context'\n");
1878 return 1 if ptr is a parent of context
1880 int talloc_is_parent(const void *context, const void *ptr)
1882 struct talloc_chunk *tc;
1884 if (context == NULL) {
1888 tc = talloc_chunk_from_ptr(context);
1890 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1891 while (tc && tc->prev) tc = tc->prev;
1893 if (TALLOC_INVALID_PARENT(tc)) {