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)
67 /* by default we abort when given a bad pointer (such as when talloc_free() is called
68 on a pointer that came from malloc() */
70 #define TALLOC_ABORT(reason) abort()
73 #ifndef discard_const_p
74 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
75 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
77 # define discard_const_p(type, ptr) ((type *)(ptr))
81 /* these macros gain us a few percent of speed on gcc */
83 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
84 as its first argument */
85 #define likely(x) __builtin_expect(!!(x), 1)
86 #define unlikely(x) __builtin_expect(!!(x), 0)
89 #define unlikely(x) (x)
92 /* this null_context is only used if talloc_enable_leak_report() or
93 talloc_enable_leak_report_full() is called, otherwise it remains
96 static void *null_context;
97 static void *autofree_context;
99 struct talloc_reference_handle {
100 struct talloc_reference_handle *next, *prev;
104 typedef int (*talloc_destructor_t)(void *);
106 struct talloc_chunk {
107 struct talloc_chunk *next, *prev;
108 struct talloc_chunk *parent, *child;
109 struct talloc_reference_handle *refs;
110 talloc_destructor_t destructor;
116 * "pool" has dual use:
118 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
119 * marks the end of the currently allocated area.
121 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
122 * is a pointer to the struct talloc_chunk of the pool that it was
123 * allocated from. This way children can quickly find the pool to chew
129 /* 16 byte alignment seems to keep everyone happy */
130 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
131 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
133 static void talloc_abort_double_free(void)
135 TALLOC_ABORT("Bad talloc magic value - double free");
138 static void talloc_abort_unknown_value(void)
140 TALLOC_ABORT("Bad talloc magic value - unknown value");
143 /* panic if we get a bad magic value */
144 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
146 const char *pp = (const char *)ptr;
147 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
148 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
149 if (tc->flags & TALLOC_FLAG_FREE) {
150 talloc_abort_double_free();
152 talloc_abort_unknown_value();
158 /* hook into the front of the list */
159 #define _TLIST_ADD(list, p) \
163 (p)->next = (p)->prev = NULL; \
165 (list)->prev = (p); \
166 (p)->next = (list); \
172 /* remove an element from a list - element doesn't have to be in list. */
173 #define _TLIST_REMOVE(list, p) \
175 if ((p) == (list)) { \
176 (list) = (p)->next; \
177 if (list) (list)->prev = NULL; \
179 if ((p)->prev) (p)->prev->next = (p)->next; \
180 if ((p)->next) (p)->next->prev = (p)->prev; \
182 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
187 return the parent chunk of a pointer
189 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
191 struct talloc_chunk *tc;
193 if (unlikely(ptr == NULL)) {
197 tc = talloc_chunk_from_ptr(ptr);
198 while (tc->prev) tc=tc->prev;
203 void *talloc_parent(const void *ptr)
205 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
206 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
212 const char *talloc_parent_name(const void *ptr)
214 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
215 return tc? tc->name : NULL;
219 A pool carries an in-pool object count count in the first 16 bytes.
220 bytes. This is done to support talloc_steal() to a parent outside of the
221 pool. The count includes the pool itself, so a talloc_free() on a pool will
222 only destroy the pool if the count has dropped to zero. A talloc_free() of a
223 pool member will reduce the count, and eventually also call free(3) on the
226 The object count is not put into "struct talloc_chunk" because it is only
227 relevant for talloc pools and the alignment to 16 bytes would increase the
228 memory footprint of each talloc chunk by those 16 bytes.
231 #define TALLOC_POOL_HDR_SIZE 16
233 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
235 return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
242 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
245 struct talloc_chunk *pool_ctx = NULL;
247 struct talloc_chunk *result;
250 if (parent == NULL) {
254 if (parent->flags & TALLOC_FLAG_POOL) {
257 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
258 pool_ctx = (struct talloc_chunk *)parent->pool;
261 if (pool_ctx == NULL) {
265 space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
266 - ((char *)pool_ctx->pool);
269 * Align size to 16 bytes
271 chunk_size = ((size + 15) & ~15);
273 if (space_left < chunk_size) {
277 result = (struct talloc_chunk *)pool_ctx->pool;
279 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
280 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
283 pool_ctx->pool = (void *)((char *)result + chunk_size);
285 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
286 result->pool = pool_ctx;
288 *talloc_pool_objectcount(pool_ctx) += 1;
294 Allocate a bit of memory as a child of an existing pointer
296 static inline void *__talloc(const void *context, size_t size)
298 struct talloc_chunk *tc = NULL;
300 if (unlikely(context == NULL)) {
301 context = null_context;
304 if (unlikely(size >= MAX_TALLOC_SIZE)) {
308 if (context != NULL) {
309 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
314 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
315 if (unlikely(tc == NULL)) return NULL;
316 tc->flags = TALLOC_MAGIC;
321 tc->destructor = NULL;
326 if (likely(context)) {
327 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
330 parent->child->parent = NULL;
331 tc->next = parent->child;
340 tc->next = tc->prev = tc->parent = NULL;
343 return TC_PTR_FROM_CHUNK(tc);
347 * Create a talloc pool
350 void *talloc_pool(const void *context, size_t size)
352 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
353 struct talloc_chunk *tc;
355 if (unlikely(result == NULL)) {
359 tc = talloc_chunk_from_ptr(result);
361 tc->flags |= TALLOC_FLAG_POOL;
362 tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
364 *talloc_pool_objectcount(tc) = 1;
366 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
367 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
374 setup a destructor to be called on free of a pointer
375 the destructor should return 0 on success, or -1 on failure.
376 if the destructor fails then the free is failed, and the memory can
377 be continued to be used
379 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
381 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
382 tc->destructor = destructor;
386 increase the reference count on a piece of memory.
388 int talloc_increase_ref_count(const void *ptr)
390 if (unlikely(!talloc_reference(null_context, ptr))) {
397 helper for talloc_reference()
399 this is referenced by a function pointer and should not be inline
401 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
403 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
404 _TLIST_REMOVE(ptr_tc->refs, handle);
409 more efficient way to add a name to a pointer - the name must point to a
412 static inline void _talloc_set_name_const(const void *ptr, const char *name)
414 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
419 internal talloc_named_const()
421 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
425 ptr = __talloc(context, size);
426 if (unlikely(ptr == NULL)) {
430 _talloc_set_name_const(ptr, name);
436 make a secondary reference to a pointer, hanging off the given context.
437 the pointer remains valid until both the original caller and this given
440 the major use for this is when two different structures need to reference the
441 same underlying data, and you want to be able to free the two instances separately,
444 void *_talloc_reference(const void *context, const void *ptr)
446 struct talloc_chunk *tc;
447 struct talloc_reference_handle *handle;
448 if (unlikely(ptr == NULL)) return NULL;
450 tc = talloc_chunk_from_ptr(ptr);
451 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
452 sizeof(struct talloc_reference_handle),
453 TALLOC_MAGIC_REFERENCE);
454 if (unlikely(handle == NULL)) return NULL;
456 /* note that we hang the destructor off the handle, not the
457 main context as that allows the caller to still setup their
458 own destructor on the context if they want to */
459 talloc_set_destructor(handle, talloc_reference_destructor);
460 handle->ptr = discard_const_p(void, ptr);
461 _TLIST_ADD(tc->refs, handle);
467 internal talloc_free call
469 static inline int _talloc_free(void *ptr)
471 struct talloc_chunk *tc;
473 if (unlikely(ptr == NULL)) {
477 tc = talloc_chunk_from_ptr(ptr);
479 if (unlikely(tc->refs)) {
481 /* check this is a reference from a child or grantchild
482 * back to it's parent or grantparent
484 * in that case we need to remove the reference and
485 * call another instance of talloc_free() on the current
488 is_child = talloc_is_parent(tc->refs, ptr);
489 _talloc_free(tc->refs);
491 return _talloc_free(ptr);
496 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
497 /* we have a free loop - stop looping */
501 if (unlikely(tc->destructor)) {
502 talloc_destructor_t d = tc->destructor;
503 if (d == (talloc_destructor_t)-1) {
506 tc->destructor = (talloc_destructor_t)-1;
511 tc->destructor = NULL;
515 _TLIST_REMOVE(tc->parent->child, tc);
516 if (tc->parent->child) {
517 tc->parent->child->parent = tc->parent;
520 if (tc->prev) tc->prev->next = tc->next;
521 if (tc->next) tc->next->prev = tc->prev;
524 tc->flags |= TALLOC_FLAG_LOOP;
527 /* we need to work out who will own an abandoned child
528 if it cannot be freed. In priority order, the first
529 choice is owner of any remaining reference to this
530 pointer, the second choice is our parent, and the
531 final choice is the null context. */
532 void *child = TC_PTR_FROM_CHUNK(tc->child);
533 const void *new_parent = null_context;
534 if (unlikely(tc->child->refs)) {
535 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
536 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
538 if (unlikely(_talloc_free(child) == -1)) {
539 if (new_parent == null_context) {
540 struct talloc_chunk *p = talloc_parent_chunk(ptr);
541 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
543 talloc_steal(new_parent, child);
547 tc->flags |= TALLOC_FLAG_FREE;
549 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
550 struct talloc_chunk *pool;
551 unsigned int *pool_object_count;
553 pool = (tc->flags & TALLOC_FLAG_POOL)
554 ? tc : (struct talloc_chunk *)tc->pool;
556 pool_object_count = talloc_pool_objectcount(pool);
558 if (*pool_object_count == 0) {
559 TALLOC_ABORT("Pool object count zero!");
562 *pool_object_count -= 1;
564 if (*pool_object_count == 0) {
575 move a lump of memory from one talloc context to another return the
576 ptr on success, or NULL if it could not be transferred.
577 passing NULL as ptr will always return NULL with no side effects.
579 void *_talloc_steal(const void *new_ctx, const void *ptr)
581 struct talloc_chunk *tc, *new_tc;
583 if (unlikely(!ptr)) {
587 if (unlikely(new_ctx == NULL)) {
588 new_ctx = null_context;
591 tc = talloc_chunk_from_ptr(ptr);
593 if (unlikely(new_ctx == NULL)) {
595 _TLIST_REMOVE(tc->parent->child, tc);
596 if (tc->parent->child) {
597 tc->parent->child->parent = tc->parent;
600 if (tc->prev) tc->prev->next = tc->next;
601 if (tc->next) tc->next->prev = tc->prev;
604 tc->parent = tc->next = tc->prev = NULL;
605 return discard_const_p(void, ptr);
608 new_tc = talloc_chunk_from_ptr(new_ctx);
610 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
611 return discard_const_p(void, ptr);
615 _TLIST_REMOVE(tc->parent->child, tc);
616 if (tc->parent->child) {
617 tc->parent->child->parent = tc->parent;
620 if (tc->prev) tc->prev->next = tc->next;
621 if (tc->next) tc->next->prev = tc->prev;
625 if (new_tc->child) new_tc->child->parent = NULL;
626 _TLIST_ADD(new_tc->child, tc);
628 return discard_const_p(void, ptr);
634 remove a secondary reference to a pointer. This undo's what
635 talloc_reference() has done. The context and pointer arguments
636 must match those given to a talloc_reference()
638 static inline int talloc_unreference(const void *context, const void *ptr)
640 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
641 struct talloc_reference_handle *h;
643 if (unlikely(context == NULL)) {
644 context = null_context;
647 for (h=tc->refs;h;h=h->next) {
648 struct talloc_chunk *p = talloc_parent_chunk(h);
650 if (context == NULL) break;
651 } else if (TC_PTR_FROM_CHUNK(p) == context) {
659 return _talloc_free(h);
663 remove a specific parent context from a pointer. This is a more
664 controlled varient of talloc_free()
666 int talloc_unlink(const void *context, void *ptr)
668 struct talloc_chunk *tc_p, *new_p;
675 if (context == NULL) {
676 context = null_context;
679 if (talloc_unreference(context, ptr) == 0) {
683 if (context == NULL) {
684 if (talloc_parent_chunk(ptr) != NULL) {
688 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
693 tc_p = talloc_chunk_from_ptr(ptr);
695 if (tc_p->refs == NULL) {
696 return _talloc_free(ptr);
699 new_p = talloc_parent_chunk(tc_p->refs);
701 new_parent = TC_PTR_FROM_CHUNK(new_p);
706 if (talloc_unreference(new_parent, ptr) != 0) {
710 talloc_steal(new_parent, ptr);
716 add a name to an existing pointer - va_list version
718 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
720 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
722 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
723 tc->name = talloc_vasprintf(ptr, fmt, ap);
724 if (likely(tc->name)) {
725 _talloc_set_name_const(tc->name, ".name");
731 add a name to an existing pointer
733 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
738 name = talloc_set_name_v(ptr, fmt, ap);
745 create a named talloc pointer. Any talloc pointer can be named, and
746 talloc_named() operates just like talloc() except that it allows you
749 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
755 ptr = __talloc(context, size);
756 if (unlikely(ptr == NULL)) return NULL;
759 name = talloc_set_name_v(ptr, fmt, ap);
762 if (unlikely(name == NULL)) {
771 return the name of a talloc ptr, or "UNNAMED"
773 const char *talloc_get_name(const void *ptr)
775 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
776 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
779 if (likely(tc->name)) {
787 check if a pointer has the given name. If it does, return the pointer,
788 otherwise return NULL
790 void *talloc_check_name(const void *ptr, const char *name)
793 if (unlikely(ptr == NULL)) return NULL;
794 pname = talloc_get_name(ptr);
795 if (likely(pname == name || strcmp(pname, name) == 0)) {
796 return discard_const_p(void, ptr);
803 this is for compatibility with older versions of talloc
805 void *talloc_init(const char *fmt, ...)
812 * samba3 expects talloc_report_depth_cb(NULL, ...)
813 * reports all talloc'ed memory, so we need to enable
816 talloc_enable_null_tracking();
818 ptr = __talloc(NULL, 0);
819 if (unlikely(ptr == NULL)) return NULL;
822 name = talloc_set_name_v(ptr, fmt, ap);
825 if (unlikely(name == NULL)) {
834 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
835 should probably not be used in new code. It's in here to keep the talloc
836 code consistent across Samba 3 and 4.
838 void talloc_free_children(void *ptr)
840 struct talloc_chunk *tc;
842 if (unlikely(ptr == NULL)) {
846 tc = talloc_chunk_from_ptr(ptr);
849 /* we need to work out who will own an abandoned child
850 if it cannot be freed. In priority order, the first
851 choice is owner of any remaining reference to this
852 pointer, the second choice is our parent, and the
853 final choice is the null context. */
854 void *child = TC_PTR_FROM_CHUNK(tc->child);
855 const void *new_parent = null_context;
856 if (unlikely(tc->child->refs)) {
857 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
858 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
860 if (unlikely(_talloc_free(child) == -1)) {
861 if (new_parent == null_context) {
862 struct talloc_chunk *p = talloc_parent_chunk(ptr);
863 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
865 talloc_steal(new_parent, child);
869 if ((tc->flags & TALLOC_FLAG_POOL)
870 && (*talloc_pool_objectcount(tc) == 1)) {
871 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
872 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
873 VALGRIND_MAKE_MEM_NOACCESS(
874 tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
880 Allocate a bit of memory as a child of an existing pointer
882 void *_talloc(const void *context, size_t size)
884 return __talloc(context, size);
888 externally callable talloc_set_name_const()
890 void talloc_set_name_const(const void *ptr, const char *name)
892 _talloc_set_name_const(ptr, name);
896 create a named talloc pointer. Any talloc pointer can be named, and
897 talloc_named() operates just like talloc() except that it allows you
900 void *talloc_named_const(const void *context, size_t size, const char *name)
902 return _talloc_named_const(context, size, name);
906 free a talloc pointer. This also frees all child pointers of this
909 return 0 if the memory is actually freed, otherwise -1. The memory
910 will not be freed if the ref_count is > 1 or the destructor (if
911 any) returns non-zero
913 int talloc_free(void *ptr)
915 return _talloc_free(ptr);
921 A talloc version of realloc. The context argument is only used if
924 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
926 struct talloc_chunk *tc;
928 bool malloced = false;
930 /* size zero is equivalent to free() */
931 if (unlikely(size == 0)) {
936 if (unlikely(size >= MAX_TALLOC_SIZE)) {
940 /* realloc(NULL) is equivalent to malloc() */
942 return _talloc_named_const(context, size, name);
945 tc = talloc_chunk_from_ptr(ptr);
947 /* don't allow realloc on referenced pointers */
948 if (unlikely(tc->refs)) {
952 /* don't shrink if we have less than 1k to gain */
953 if ((size < tc->size) && ((tc->size - size) < 1024)) {
958 /* by resetting magic we catch users of the old memory */
959 tc->flags |= TALLOC_FLAG_FREE;
962 new_ptr = malloc(size + TC_HDR_SIZE);
964 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
968 if (tc->flags & TALLOC_FLAG_POOLMEM) {
970 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
971 *talloc_pool_objectcount((struct talloc_chunk *)
974 if (new_ptr == NULL) {
975 new_ptr = malloc(TC_HDR_SIZE+size);
980 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
984 new_ptr = realloc(tc, size + TC_HDR_SIZE);
987 if (unlikely(!new_ptr)) {
988 tc->flags &= ~TALLOC_FLAG_FREE;
992 tc = (struct talloc_chunk *)new_ptr;
993 tc->flags &= ~TALLOC_FLAG_FREE;
995 tc->flags &= ~TALLOC_FLAG_POOLMEM;
998 tc->parent->child = tc;
1001 tc->child->parent = tc;
1005 tc->prev->next = tc;
1008 tc->next->prev = tc;
1012 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1014 return TC_PTR_FROM_CHUNK(tc);
1018 a wrapper around talloc_steal() for situations where you are moving a pointer
1019 between two structures, and want the old pointer to be set to NULL
1021 void *_talloc_move(const void *new_ctx, const void *_pptr)
1023 const void **pptr = discard_const_p(const void *,_pptr);
1024 void *ret = _talloc_steal(new_ctx, *pptr);
1030 return the total size of a talloc pool (subtree)
1032 size_t talloc_total_size(const void *ptr)
1035 struct talloc_chunk *c, *tc;
1044 tc = talloc_chunk_from_ptr(ptr);
1046 if (tc->flags & TALLOC_FLAG_LOOP) {
1050 tc->flags |= TALLOC_FLAG_LOOP;
1053 for (c=tc->child;c;c=c->next) {
1054 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1057 tc->flags &= ~TALLOC_FLAG_LOOP;
1063 return the total number of blocks in a talloc pool (subtree)
1065 size_t talloc_total_blocks(const void *ptr)
1068 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
1070 if (tc->flags & TALLOC_FLAG_LOOP) {
1074 tc->flags |= TALLOC_FLAG_LOOP;
1077 for (c=tc->child;c;c=c->next) {
1078 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1081 tc->flags &= ~TALLOC_FLAG_LOOP;
1087 return the number of external references to a pointer
1089 size_t talloc_reference_count(const void *ptr)
1091 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1092 struct talloc_reference_handle *h;
1095 for (h=tc->refs;h;h=h->next) {
1102 report on memory usage by all children of a pointer, giving a full tree view
1104 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1105 void (*callback)(const void *ptr,
1106 int depth, int max_depth,
1108 void *private_data),
1111 struct talloc_chunk *c, *tc;
1116 if (ptr == NULL) return;
1118 tc = talloc_chunk_from_ptr(ptr);
1120 if (tc->flags & TALLOC_FLAG_LOOP) {
1124 callback(ptr, depth, max_depth, 0, private_data);
1126 if (max_depth >= 0 && depth >= max_depth) {
1130 tc->flags |= TALLOC_FLAG_LOOP;
1131 for (c=tc->child;c;c=c->next) {
1132 if (c->name == TALLOC_MAGIC_REFERENCE) {
1133 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1134 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1136 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1139 tc->flags &= ~TALLOC_FLAG_LOOP;
1142 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1144 const char *name = talloc_get_name(ptr);
1145 FILE *f = (FILE *)_f;
1148 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1153 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1154 (max_depth < 0 ? "full " :""), name,
1155 (unsigned long)talloc_total_size(ptr),
1156 (unsigned long)talloc_total_blocks(ptr));
1160 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1163 (unsigned long)talloc_total_size(ptr),
1164 (unsigned long)talloc_total_blocks(ptr),
1165 (int)talloc_reference_count(ptr), ptr);
1168 fprintf(f, "content: ");
1169 if (talloc_total_size(ptr)) {
1170 int tot = talloc_total_size(ptr);
1173 for (i = 0; i < tot; i++) {
1174 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1175 fprintf(f, "%c", ((char *)ptr)[i]);
1177 fprintf(f, "~%02x", ((char *)ptr)[i]);
1186 report on memory usage by all children of a pointer, giving a full tree view
1188 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1190 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1195 report on memory usage by all children of a pointer, giving a full tree view
1197 void talloc_report_full(const void *ptr, FILE *f)
1199 talloc_report_depth_file(ptr, 0, -1, f);
1203 report on memory usage by all children of a pointer
1205 void talloc_report(const void *ptr, FILE *f)
1207 talloc_report_depth_file(ptr, 0, 1, f);
1211 report on any memory hanging off the null context
1213 static void talloc_report_null(void)
1215 if (talloc_total_size(null_context) != 0) {
1216 talloc_report(null_context, stderr);
1221 report on any memory hanging off the null context
1223 static void talloc_report_null_full(void)
1225 if (talloc_total_size(null_context) != 0) {
1226 talloc_report_full(null_context, stderr);
1231 enable tracking of the NULL context
1233 void talloc_enable_null_tracking(void)
1235 if (null_context == NULL) {
1236 null_context = _talloc_named_const(NULL, 0, "null_context");
1241 disable tracking of the NULL context
1243 void talloc_disable_null_tracking(void)
1245 _talloc_free(null_context);
1246 null_context = NULL;
1250 enable leak reporting on exit
1252 void talloc_enable_leak_report(void)
1254 talloc_enable_null_tracking();
1255 atexit(talloc_report_null);
1259 enable full leak reporting on exit
1261 void talloc_enable_leak_report_full(void)
1263 talloc_enable_null_tracking();
1264 atexit(talloc_report_null_full);
1268 talloc and zero memory.
1270 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1272 void *p = _talloc_named_const(ctx, size, name);
1275 memset(p, '\0', size);
1282 memdup with a talloc.
1284 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1286 void *newp = _talloc_named_const(t, size, name);
1289 memcpy(newp, p, size);
1295 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1299 ret = (char *)__talloc(t, len + 1);
1300 if (unlikely(!ret)) return NULL;
1302 memcpy(ret, p, len);
1305 _talloc_set_name_const(ret, ret);
1310 strdup with a talloc
1312 char *talloc_strdup(const void *t, const char *p)
1314 if (unlikely(!p)) return NULL;
1315 return __talloc_strlendup(t, p, strlen(p));
1319 strndup with a talloc
1321 char *talloc_strndup(const void *t, const char *p, size_t n)
1323 if (unlikely(!p)) return NULL;
1324 return __talloc_strlendup(t, p, strnlen(p, n));
1327 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1328 const char *a, size_t alen)
1332 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1333 if (unlikely(!ret)) return NULL;
1335 /* append the string and the trailing \0 */
1336 memcpy(&ret[slen], a, alen);
1339 _talloc_set_name_const(ret, ret);
1344 * Appends at the end of the string.
1346 char *talloc_strdup_append(char *s, const char *a)
1349 return talloc_strdup(NULL, a);
1356 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1360 * Appends at the end of the talloc'ed buffer,
1361 * not the end of the string.
1363 char *talloc_strdup_append_buffer(char *s, const char *a)
1368 return talloc_strdup(NULL, a);
1375 slen = talloc_get_size(s);
1376 if (likely(slen > 0)) {
1380 return __talloc_strlendup_append(s, slen, a, strlen(a));
1384 * Appends at the end of the string.
1386 char *talloc_strndup_append(char *s, const char *a, size_t n)
1389 return talloc_strdup(NULL, a);
1396 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1400 * Appends at the end of the talloc'ed buffer,
1401 * not the end of the string.
1403 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1408 return talloc_strdup(NULL, a);
1415 slen = talloc_get_size(s);
1416 if (likely(slen > 0)) {
1420 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1423 #ifndef HAVE_VA_COPY
1424 #ifdef HAVE___VA_COPY
1425 #define va_copy(dest, src) __va_copy(dest, src)
1427 #define va_copy(dest, src) (dest) = (src)
1431 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1438 /* this call looks strange, but it makes it work on older solaris boxes */
1440 len = vsnprintf(&c, 1, fmt, ap2);
1442 if (unlikely(len < 0)) {
1446 ret = (char *)__talloc(t, len+1);
1447 if (unlikely(!ret)) return NULL;
1450 vsnprintf(ret, len+1, fmt, ap2);
1453 _talloc_set_name_const(ret, ret);
1459 Perform string formatting, and return a pointer to newly allocated
1460 memory holding the result, inside a memory pool.
1462 char *talloc_asprintf(const void *t, const char *fmt, ...)
1468 ret = talloc_vasprintf(t, fmt, ap);
1473 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1474 const char *fmt, va_list ap)
1475 PRINTF_ATTRIBUTE(3,0);
1477 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1478 const char *fmt, va_list ap)
1485 alen = vsnprintf(&c, 1, fmt, ap2);
1489 /* Either the vsnprintf failed or the format resulted in
1490 * no characters being formatted. In the former case, we
1491 * ought to return NULL, in the latter we ought to return
1492 * the original string. Most current callers of this
1493 * function expect it to never return NULL.
1498 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1499 if (!s) return NULL;
1502 vsnprintf(s + slen, alen + 1, fmt, ap2);
1505 _talloc_set_name_const(s, s);
1510 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1511 * and return @p s, which may have moved. Good for gradually
1512 * accumulating output into a string buffer. Appends at the end
1515 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1518 return talloc_vasprintf(NULL, fmt, ap);
1521 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1525 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1526 * and return @p s, which may have moved. Always appends at the
1527 * end of the talloc'ed buffer, not the end of the string.
1529 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1534 return talloc_vasprintf(NULL, fmt, ap);
1537 slen = talloc_get_size(s);
1538 if (likely(slen > 0)) {
1542 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1546 Realloc @p s to append the formatted result of @p fmt and return @p
1547 s, which may have moved. Good for gradually accumulating output
1548 into a string buffer.
1550 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1555 s = talloc_vasprintf_append(s, fmt, ap);
1561 Realloc @p s to append the formatted result of @p fmt and return @p
1562 s, which may have moved. Good for gradually accumulating output
1565 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1570 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1576 alloc an array, checking for integer overflow in the array size
1578 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1580 if (count >= MAX_TALLOC_SIZE/el_size) {
1583 return _talloc_named_const(ctx, el_size * count, name);
1587 alloc an zero array, checking for integer overflow in the array size
1589 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1591 if (count >= MAX_TALLOC_SIZE/el_size) {
1594 return _talloc_zero(ctx, el_size * count, name);
1598 realloc an array, checking for integer overflow in the array size
1600 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1602 if (count >= MAX_TALLOC_SIZE/el_size) {
1605 return _talloc_realloc(ctx, ptr, el_size * count, name);
1609 a function version of talloc_realloc(), so it can be passed as a function pointer
1610 to libraries that want a realloc function (a realloc function encapsulates
1611 all the basic capabilities of an allocation library, which is why this is useful)
1613 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1615 return _talloc_realloc(context, ptr, size, NULL);
1619 static int talloc_autofree_destructor(void *ptr)
1621 autofree_context = NULL;
1625 static void talloc_autofree(void)
1627 _talloc_free(autofree_context);
1631 return a context which will be auto-freed on exit
1632 this is useful for reducing the noise in leak reports
1634 void *talloc_autofree_context(void)
1636 if (autofree_context == NULL) {
1637 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1638 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1639 atexit(talloc_autofree);
1641 return autofree_context;
1644 size_t talloc_get_size(const void *context)
1646 struct talloc_chunk *tc;
1648 if (context == NULL)
1651 tc = talloc_chunk_from_ptr(context);
1657 find a parent of this context that has the given name, if any
1659 void *talloc_find_parent_byname(const void *context, const char *name)
1661 struct talloc_chunk *tc;
1663 if (context == NULL) {
1667 tc = talloc_chunk_from_ptr(context);
1669 if (tc->name && strcmp(tc->name, name) == 0) {
1670 return TC_PTR_FROM_CHUNK(tc);
1672 while (tc && tc->prev) tc = tc->prev;
1681 show the parentage of a context
1683 void talloc_show_parents(const void *context, FILE *file)
1685 struct talloc_chunk *tc;
1687 if (context == NULL) {
1688 fprintf(file, "talloc no parents for NULL\n");
1692 tc = talloc_chunk_from_ptr(context);
1693 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1695 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1696 while (tc && tc->prev) tc = tc->prev;
1705 return 1 if ptr is a parent of context
1707 int talloc_is_parent(const void *context, const void *ptr)
1709 struct talloc_chunk *tc;
1711 if (context == NULL) {
1715 tc = talloc_chunk_from_ptr(context);
1717 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1718 while (tc && tc->prev) tc = tc->prev;