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 */
86 #define likely(x) __builtin_expect(!!(x), 1)
89 #define unlikely(x) __builtin_expect(!!(x), 0)
96 #define unlikely(x) (x)
100 /* this null_context is only used if talloc_enable_leak_report() or
101 talloc_enable_leak_report_full() is called, otherwise it remains
104 static void *null_context;
105 static void *autofree_context;
106 static void *no_owner_context;
107 void *talloc_no_owner_context(void);
108 static inline int _talloc_free(void *ptr);
110 struct talloc_reference_handle {
111 struct talloc_reference_handle *next, *prev;
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_double_free(void)
162 talloc_abort("Bad talloc magic value - double free");
165 static void talloc_abort_unknown_value(void)
167 talloc_abort("Bad talloc magic value - unknown value");
170 /* panic if we get a bad magic value */
171 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
173 const char *pp = (const char *)ptr;
174 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
175 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
176 if (tc->flags & TALLOC_FLAG_FREE) {
177 talloc_abort_double_free();
179 talloc_abort_unknown_value();
185 /* hook into the front of the list */
186 #define _TLIST_ADD(list, p) \
190 (p)->next = (p)->prev = NULL; \
192 (list)->prev = (p); \
193 (p)->next = (list); \
199 /* remove an element from a list - element doesn't have to be in list. */
200 #define _TLIST_REMOVE(list, p) \
202 if ((p) == (list)) { \
203 (list) = (p)->next; \
204 if (list) (list)->prev = NULL; \
206 if ((p)->prev) (p)->prev->next = (p)->next; \
207 if ((p)->next) (p)->next->prev = (p)->prev; \
209 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
214 return the parent chunk of a pointer
216 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
218 struct talloc_chunk *tc;
220 if (unlikely(ptr == NULL)) {
224 tc = talloc_chunk_from_ptr(ptr);
225 while (tc->prev) tc=tc->prev;
230 void *talloc_parent(const void *ptr)
232 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
233 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
239 const char *talloc_parent_name(const void *ptr)
241 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
242 return tc? tc->name : NULL;
246 A pool carries an in-pool object count count in the first 16 bytes.
247 bytes. This is done to support talloc_steal() to a parent outside of the
248 pool. The count includes the pool itself, so a talloc_free() on a pool will
249 only destroy the pool if the count has dropped to zero. A talloc_free() of a
250 pool member will reduce the count, and eventually also call free(3) on the
253 The object count is not put into "struct talloc_chunk" because it is only
254 relevant for talloc pools and the alignment to 16 bytes would increase the
255 memory footprint of each talloc chunk by those 16 bytes.
258 #define TALLOC_POOL_HDR_SIZE 16
260 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
262 return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
269 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
272 struct talloc_chunk *pool_ctx = NULL;
274 struct talloc_chunk *result;
277 if (parent == NULL) {
281 if (parent->flags & TALLOC_FLAG_POOL) {
284 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
285 pool_ctx = (struct talloc_chunk *)parent->pool;
288 if (pool_ctx == NULL) {
292 space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
293 - ((char *)pool_ctx->pool);
296 * Align size to 16 bytes
298 chunk_size = ((size + 15) & ~15);
300 if (space_left < chunk_size) {
304 result = (struct talloc_chunk *)pool_ctx->pool;
306 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
307 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
310 pool_ctx->pool = (void *)((char *)result + chunk_size);
312 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
313 result->pool = pool_ctx;
315 *talloc_pool_objectcount(pool_ctx) += 1;
321 Allocate a bit of memory as a child of an existing pointer
323 static inline void *__talloc(const void *context, size_t size)
325 struct talloc_chunk *tc = NULL;
327 if (unlikely(context == NULL)) {
328 context = null_context;
331 if (unlikely(size >= MAX_TALLOC_SIZE)) {
335 if (context != NULL) {
336 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
341 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
342 if (unlikely(tc == NULL)) return NULL;
343 tc->flags = TALLOC_MAGIC;
348 tc->destructor = NULL;
353 if (likely(context)) {
354 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
357 parent->child->parent = NULL;
358 tc->next = parent->child;
367 tc->next = tc->prev = tc->parent = NULL;
370 return TC_PTR_FROM_CHUNK(tc);
374 * Create a talloc pool
377 void *talloc_pool(const void *context, size_t size)
379 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
380 struct talloc_chunk *tc;
382 if (unlikely(result == NULL)) {
386 tc = talloc_chunk_from_ptr(result);
388 tc->flags |= TALLOC_FLAG_POOL;
389 tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
391 *talloc_pool_objectcount(tc) = 1;
393 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
394 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
401 setup a destructor to be called on free of a pointer
402 the destructor should return 0 on success, or -1 on failure.
403 if the destructor fails then the free is failed, and the memory can
404 be continued to be used
406 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
408 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
409 tc->destructor = destructor;
413 increase the reference count on a piece of memory.
415 int talloc_increase_ref_count(const void *ptr)
417 if (unlikely(!talloc_reference(null_context, ptr))) {
424 helper for talloc_reference()
426 this is referenced by a function pointer and should not be inline
428 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
430 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
431 _TLIST_REMOVE(ptr_tc->refs, handle);
432 /* If ptr_tc has no refs left and is owner by no_owner_context then we free it */
433 if (ptr_tc->refs == NULL &&
434 no_owner_context && talloc_parent(handle->ptr) == no_owner_context) {
435 _talloc_free(handle->ptr);
441 more efficient way to add a name to a pointer - the name must point to a
444 static inline void _talloc_set_name_const(const void *ptr, const char *name)
446 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
451 internal talloc_named_const()
453 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
457 ptr = __talloc(context, size);
458 if (unlikely(ptr == NULL)) {
462 _talloc_set_name_const(ptr, name);
468 make a secondary reference to a pointer, hanging off the given context.
469 the pointer remains valid until both the original caller and this given
472 the major use for this is when two different structures need to reference the
473 same underlying data, and you want to be able to free the two instances separately,
476 void *_talloc_reference(const void *context, const void *ptr)
478 struct talloc_chunk *tc;
479 struct talloc_reference_handle *handle;
480 if (unlikely(ptr == NULL)) return NULL;
482 tc = talloc_chunk_from_ptr(ptr);
483 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
484 sizeof(struct talloc_reference_handle),
485 TALLOC_MAGIC_REFERENCE);
486 if (unlikely(handle == NULL)) return NULL;
488 /* note that we hang the destructor off the handle, not the
489 main context as that allows the caller to still setup their
490 own destructor on the context if they want to */
491 talloc_set_destructor(handle, talloc_reference_destructor);
492 handle->ptr = discard_const_p(void, ptr);
493 _TLIST_ADD(tc->refs, handle);
499 internal talloc_free call
501 static inline int _talloc_free(void *ptr)
503 struct talloc_chunk *tc;
505 if (unlikely(ptr == NULL)) {
509 tc = talloc_chunk_from_ptr(ptr);
511 if (unlikely(tc->refs)) {
512 /*TODO: Fix this loop detection. The code here breaks top reference if
513 it is also one of our children. It is insufficient and can lead
514 to dangling references */
517 /* check this is a reference from a child or grantchild
518 * back to it's parent or grantparent
520 * in that case we need to remove the reference and
521 * call another instance of talloc_free() on the current
524 is_child = talloc_is_parent(tc->refs, ptr);
526 _talloc_free(tc->refs);
527 return _talloc_free(ptr);
529 /* we can't free if we have refs, so no_owner_context becomes the owner */
530 _talloc_steal(talloc_no_owner_context(), ptr);
535 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
536 /* we have a free loop - stop looping */
540 if (unlikely(tc->destructor)) {
541 talloc_destructor_t d = tc->destructor;
542 if (d == (talloc_destructor_t)-1) {
545 tc->destructor = (talloc_destructor_t)-1;
550 tc->destructor = NULL;
554 _TLIST_REMOVE(tc->parent->child, tc);
555 if (tc->parent->child) {
556 tc->parent->child->parent = tc->parent;
559 if (tc->prev) tc->prev->next = tc->next;
560 if (tc->next) tc->next->prev = tc->prev;
563 tc->flags |= TALLOC_FLAG_LOOP;
566 void *child = TC_PTR_FROM_CHUNK(tc->child);
567 const void *new_parent = talloc_no_owner_context();
568 /* if talloc_free fails because of refs and not due to the destructor,
569 it will already have a new owner no_owner_context */
570 if (unlikely(_talloc_free(child) == -1)) {
571 talloc_steal(new_parent, child);
575 tc->flags |= TALLOC_FLAG_FREE;
577 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
578 struct talloc_chunk *pool;
579 unsigned int *pool_object_count;
581 pool = (tc->flags & TALLOC_FLAG_POOL)
582 ? tc : (struct talloc_chunk *)tc->pool;
584 pool_object_count = talloc_pool_objectcount(pool);
586 if (*pool_object_count == 0) {
587 talloc_abort("Pool object count zero!");
590 *pool_object_count -= 1;
592 if (*pool_object_count == 0) {
603 move a lump of memory from one talloc context to another return the
604 ptr on success, or NULL if it could not be transferred.
605 passing NULL as ptr will always return NULL with no side effects.
607 void *_talloc_steal(const void *new_ctx, const void *ptr)
609 struct talloc_chunk *tc, *new_tc;
611 if (unlikely(!ptr)) {
615 if (unlikely(new_ctx == NULL)) {
616 new_ctx = null_context;
619 tc = talloc_chunk_from_ptr(ptr);
621 if (unlikely(new_ctx == NULL)) {
623 _TLIST_REMOVE(tc->parent->child, tc);
624 if (tc->parent->child) {
625 tc->parent->child->parent = tc->parent;
628 if (tc->prev) tc->prev->next = tc->next;
629 if (tc->next) tc->next->prev = tc->prev;
632 tc->parent = tc->next = tc->prev = NULL;
633 return discard_const_p(void, ptr);
636 new_tc = talloc_chunk_from_ptr(new_ctx);
638 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
639 return discard_const_p(void, ptr);
643 _TLIST_REMOVE(tc->parent->child, tc);
644 if (tc->parent->child) {
645 tc->parent->child->parent = tc->parent;
648 if (tc->prev) tc->prev->next = tc->next;
649 if (tc->next) tc->next->prev = tc->prev;
653 if (new_tc->child) new_tc->child->parent = NULL;
654 _TLIST_ADD(new_tc->child, tc);
656 return discard_const_p(void, ptr);
662 remove a secondary reference to a pointer. This undo's what
663 talloc_reference() has done. The context and pointer arguments
664 must match those given to a talloc_reference()
666 static inline int talloc_unreference(const void *context, const void *ptr)
668 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
669 struct talloc_reference_handle *h;
671 if (unlikely(context == NULL)) {
672 context = null_context;
675 for (h=tc->refs;h;h=h->next) {
676 struct talloc_chunk *p = talloc_parent_chunk(h);
678 if (context == NULL) break;
679 } else if (TC_PTR_FROM_CHUNK(p) == context) {
687 return _talloc_free(h);
691 remove a specific parent context from a pointer. This is a more
692 controlled varient of talloc_free()
694 int talloc_unlink(const void *context, void *ptr)
696 struct talloc_chunk *tc_p, *new_p;
703 if (context == NULL) {
704 context = null_context;
707 if (talloc_unreference(context, ptr) == 0) {
711 if (context == NULL) {
712 if (talloc_parent_chunk(ptr) != NULL) {
716 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
721 tc_p = talloc_chunk_from_ptr(ptr);
723 if (tc_p->refs == NULL) {
724 return _talloc_free(ptr);
727 talloc_steal(talloc_no_owner_context(), ptr);
732 add a name to an existing pointer - va_list version
734 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
736 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
738 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
739 tc->name = talloc_vasprintf(ptr, fmt, ap);
740 if (likely(tc->name)) {
741 _talloc_set_name_const(tc->name, ".name");
747 add a name to an existing pointer
749 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
754 name = talloc_set_name_v(ptr, fmt, ap);
761 create a named talloc pointer. Any talloc pointer can be named, and
762 talloc_named() operates just like talloc() except that it allows you
765 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
771 ptr = __talloc(context, size);
772 if (unlikely(ptr == NULL)) return NULL;
775 name = talloc_set_name_v(ptr, fmt, ap);
778 if (unlikely(name == NULL)) {
787 return the name of a talloc ptr, or "UNNAMED"
789 const char *talloc_get_name(const void *ptr)
791 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
792 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
795 if (likely(tc->name)) {
803 check if a pointer has the given name. If it does, return the pointer,
804 otherwise return NULL
806 void *talloc_check_name(const void *ptr, const char *name)
809 if (unlikely(ptr == NULL)) return NULL;
810 pname = talloc_get_name(ptr);
811 if (likely(pname == name || strcmp(pname, name) == 0)) {
812 return discard_const_p(void, ptr);
817 static void talloc_abort_type_missmatch(const char *location,
819 const char *expected)
823 reason = talloc_asprintf(NULL,
824 "%s: Type mismatch: name[%s] expected[%s]",
829 reason = "Type mismatch";
832 talloc_abort(reason);
835 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
839 if (unlikely(ptr == NULL)) {
840 talloc_abort_type_missmatch(location, NULL, name);
844 pname = talloc_get_name(ptr);
845 if (likely(pname == name || strcmp(pname, name) == 0)) {
846 return discard_const_p(void, ptr);
849 talloc_abort_type_missmatch(location, pname, name);
854 this is for compatibility with older versions of talloc
856 void *talloc_init(const char *fmt, ...)
863 * samba3 expects talloc_report_depth_cb(NULL, ...)
864 * reports all talloc'ed memory, so we need to enable
867 talloc_enable_null_tracking();
869 ptr = __talloc(NULL, 0);
870 if (unlikely(ptr == NULL)) return NULL;
873 name = talloc_set_name_v(ptr, fmt, ap);
876 if (unlikely(name == NULL)) {
885 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
886 should probably not be used in new code. It's in here to keep the talloc
887 code consistent across Samba 3 and 4.
889 void talloc_free_children(void *ptr)
891 struct talloc_chunk *tc;
893 if (unlikely(ptr == NULL)) {
897 tc = talloc_chunk_from_ptr(ptr);
900 /* we need to work out who will own an abandoned child
901 if it cannot be freed. In priority order, the first
902 choice is owner of any remaining reference to this
903 pointer, the second choice is our parent, and the
904 final choice is the null context. */
905 void *child = TC_PTR_FROM_CHUNK(tc->child);
906 const void *new_parent = talloc_no_owner_context();
908 if (unlikely(tc->child->refs)) {
909 new_parent = talloc_no_owner_context();
911 if (unlikely(_talloc_free(child) == -1)) {
912 if (new_parent == null_context) {
913 struct talloc_chunk *p = talloc_parent_chunk(ptr);
914 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
916 talloc_steal(new_parent, child);
920 if ((tc->flags & TALLOC_FLAG_POOL)
921 && (*talloc_pool_objectcount(tc) == 1)) {
922 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
923 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
924 VALGRIND_MAKE_MEM_NOACCESS(
925 tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
931 Allocate a bit of memory as a child of an existing pointer
933 void *_talloc(const void *context, size_t size)
935 return __talloc(context, size);
939 externally callable talloc_set_name_const()
941 void talloc_set_name_const(const void *ptr, const char *name)
943 _talloc_set_name_const(ptr, name);
947 create a named talloc pointer. Any talloc pointer can be named, and
948 talloc_named() operates just like talloc() except that it allows you
951 void *talloc_named_const(const void *context, size_t size, const char *name)
953 return _talloc_named_const(context, size, name);
957 free a talloc pointer. This also frees all child pointers of this
960 return 0 if the memory is actually freed, otherwise -1. The memory
961 will not be freed if the ref_count is > 1 or the destructor (if
962 any) returns non-zero
964 int talloc_free(void *ptr)
966 struct talloc_chunk *tc;
968 if (unlikely(ptr == NULL)) {
972 tc = talloc_chunk_from_ptr(ptr);
974 if (no_owner_context && tc->parent == no_owner_context) {
975 talloc_abort_double_free();
978 return _talloc_free(ptr);
984 A talloc version of realloc. The context argument is only used if
987 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
989 struct talloc_chunk *tc;
991 bool malloced = false;
993 /* size zero is equivalent to free() */
994 if (unlikely(size == 0)) {
999 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1003 /* realloc(NULL) is equivalent to malloc() */
1005 return _talloc_named_const(context, size, name);
1008 tc = talloc_chunk_from_ptr(ptr);
1010 /* don't allow realloc on referenced pointers */
1011 if (unlikely(tc->refs)) {
1015 /* don't let anybody try to realloc a talloc_pool */
1016 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1020 /* don't shrink if we have less than 1k to gain */
1021 if ((size < tc->size) && ((tc->size - size) < 1024)) {
1026 /* by resetting magic we catch users of the old memory */
1027 tc->flags |= TALLOC_FLAG_FREE;
1030 new_ptr = malloc(size + TC_HDR_SIZE);
1032 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
1036 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1038 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1039 *talloc_pool_objectcount((struct talloc_chunk *)
1042 if (new_ptr == NULL) {
1043 new_ptr = malloc(TC_HDR_SIZE+size);
1048 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1052 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1055 if (unlikely(!new_ptr)) {
1056 tc->flags &= ~TALLOC_FLAG_FREE;
1060 tc = (struct talloc_chunk *)new_ptr;
1061 tc->flags &= ~TALLOC_FLAG_FREE;
1063 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1066 tc->parent->child = tc;
1069 tc->child->parent = tc;
1073 tc->prev->next = tc;
1076 tc->next->prev = tc;
1080 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1082 return TC_PTR_FROM_CHUNK(tc);
1086 a wrapper around talloc_steal() for situations where you are moving a pointer
1087 between two structures, and want the old pointer to be set to NULL
1089 void *_talloc_move(const void *new_ctx, const void *_pptr)
1091 const void **pptr = discard_const_p(const void *,_pptr);
1092 void *ret = _talloc_steal(new_ctx, *pptr);
1098 return the total size of a talloc pool (subtree)
1100 size_t talloc_total_size(const void *ptr)
1103 struct talloc_chunk *c, *tc;
1112 tc = talloc_chunk_from_ptr(ptr);
1114 if (tc->flags & TALLOC_FLAG_LOOP) {
1118 tc->flags |= TALLOC_FLAG_LOOP;
1120 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1123 for (c=tc->child;c;c=c->next) {
1124 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1127 tc->flags &= ~TALLOC_FLAG_LOOP;
1133 return the total number of blocks in a talloc pool (subtree)
1135 size_t talloc_total_blocks(const void *ptr)
1138 struct talloc_chunk *c, *tc;
1147 tc = talloc_chunk_from_ptr(ptr);
1149 if (tc->flags & TALLOC_FLAG_LOOP) {
1153 tc->flags |= TALLOC_FLAG_LOOP;
1156 for (c=tc->child;c;c=c->next) {
1157 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1160 tc->flags &= ~TALLOC_FLAG_LOOP;
1166 return the number of external references to a pointer
1168 size_t talloc_reference_count(const void *ptr)
1170 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1171 struct talloc_reference_handle *h;
1174 for (h=tc->refs;h;h=h->next) {
1181 report on memory usage by all children of a pointer, giving a full tree view
1183 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1184 void (*callback)(const void *ptr,
1185 int depth, int max_depth,
1187 void *private_data),
1190 struct talloc_chunk *c, *tc;
1195 if (ptr == NULL) return;
1197 tc = talloc_chunk_from_ptr(ptr);
1199 if (tc->flags & TALLOC_FLAG_LOOP) {
1203 callback(ptr, depth, max_depth, 0, private_data);
1205 if (max_depth >= 0 && depth >= max_depth) {
1209 tc->flags |= TALLOC_FLAG_LOOP;
1210 for (c=tc->child;c;c=c->next) {
1211 if (c->name == TALLOC_MAGIC_REFERENCE) {
1212 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1213 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1215 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1218 tc->flags &= ~TALLOC_FLAG_LOOP;
1221 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1223 const char *name = talloc_get_name(ptr);
1224 FILE *f = (FILE *)_f;
1227 if (talloc_parent(ptr) == no_owner_context && no_owner_context) {
1228 fprintf(f, "%*sreference to: %s [no owner]\n", depth*4, "", name);
1230 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1236 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1237 (max_depth < 0 ? "full " :""), name,
1238 (unsigned long)talloc_total_size(ptr),
1239 (unsigned long)talloc_total_blocks(ptr));
1243 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1246 (unsigned long)talloc_total_size(ptr),
1247 (unsigned long)talloc_total_blocks(ptr),
1248 (int)talloc_reference_count(ptr), ptr);
1251 fprintf(f, "content: ");
1252 if (talloc_total_size(ptr)) {
1253 int tot = talloc_total_size(ptr);
1256 for (i = 0; i < tot; i++) {
1257 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1258 fprintf(f, "%c", ((char *)ptr)[i]);
1260 fprintf(f, "~%02x", ((char *)ptr)[i]);
1269 report on memory usage by all children of a pointer, giving a full tree view
1271 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1273 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1278 report on memory usage by all children of a pointer, giving a full tree view
1280 void talloc_report_full(const void *ptr, FILE *f)
1282 talloc_report_depth_file(ptr, 0, -1, f);
1286 report on memory usage by all children of a pointer
1288 void talloc_report(const void *ptr, FILE *f)
1290 talloc_report_depth_file(ptr, 0, 1, f);
1294 report on any memory hanging off the null context
1296 static void talloc_report_null(void)
1298 if (talloc_total_size(null_context) != 0) {
1299 talloc_report(null_context, stderr);
1304 report on any memory hanging off the null context
1306 static void talloc_report_null_full(void)
1308 if (talloc_total_size(null_context) != 0) {
1309 talloc_report_full(null_context, stderr);
1314 enable tracking of the NULL context
1316 void talloc_enable_null_tracking(void)
1318 if (null_context == NULL) {
1319 null_context = _talloc_named_const(NULL, 0, "null_context");
1324 disable tracking of the NULL context
1326 void talloc_disable_null_tracking(void)
1328 _talloc_free(null_context);
1329 null_context = NULL;
1333 enable leak reporting on exit
1335 void talloc_enable_leak_report(void)
1337 talloc_enable_null_tracking();
1338 atexit(talloc_report_null);
1342 enable full leak reporting on exit
1344 void talloc_enable_leak_report_full(void)
1346 talloc_enable_null_tracking();
1347 atexit(talloc_report_null_full);
1351 talloc and zero memory.
1353 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1355 void *p = _talloc_named_const(ctx, size, name);
1358 memset(p, '\0', size);
1365 memdup with a talloc.
1367 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1369 void *newp = _talloc_named_const(t, size, name);
1372 memcpy(newp, p, size);
1378 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1382 ret = (char *)__talloc(t, len + 1);
1383 if (unlikely(!ret)) return NULL;
1385 memcpy(ret, p, len);
1388 _talloc_set_name_const(ret, ret);
1393 strdup with a talloc
1395 char *talloc_strdup(const void *t, const char *p)
1397 if (unlikely(!p)) return NULL;
1398 return __talloc_strlendup(t, p, strlen(p));
1402 strndup with a talloc
1404 char *talloc_strndup(const void *t, const char *p, size_t n)
1406 if (unlikely(!p)) return NULL;
1407 return __talloc_strlendup(t, p, strnlen(p, n));
1410 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1411 const char *a, size_t alen)
1415 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1416 if (unlikely(!ret)) return NULL;
1418 /* append the string and the trailing \0 */
1419 memcpy(&ret[slen], a, alen);
1422 _talloc_set_name_const(ret, ret);
1427 * Appends at the end of the string.
1429 char *talloc_strdup_append(char *s, const char *a)
1432 return talloc_strdup(NULL, a);
1439 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1443 * Appends at the end of the talloc'ed buffer,
1444 * not the end of the string.
1446 char *talloc_strdup_append_buffer(char *s, const char *a)
1451 return talloc_strdup(NULL, a);
1458 slen = talloc_get_size(s);
1459 if (likely(slen > 0)) {
1463 return __talloc_strlendup_append(s, slen, a, strlen(a));
1467 * Appends at the end of the string.
1469 char *talloc_strndup_append(char *s, const char *a, size_t n)
1472 return talloc_strdup(NULL, a);
1479 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1483 * Appends at the end of the talloc'ed buffer,
1484 * not the end of the string.
1486 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1491 return talloc_strdup(NULL, a);
1498 slen = talloc_get_size(s);
1499 if (likely(slen > 0)) {
1503 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1506 #ifndef HAVE_VA_COPY
1507 #ifdef HAVE___VA_COPY
1508 #define va_copy(dest, src) __va_copy(dest, src)
1510 #define va_copy(dest, src) (dest) = (src)
1514 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1521 /* this call looks strange, but it makes it work on older solaris boxes */
1523 len = vsnprintf(&c, 1, fmt, ap2);
1525 if (unlikely(len < 0)) {
1529 ret = (char *)__talloc(t, len+1);
1530 if (unlikely(!ret)) return NULL;
1533 vsnprintf(ret, len+1, fmt, ap2);
1536 _talloc_set_name_const(ret, ret);
1542 Perform string formatting, and return a pointer to newly allocated
1543 memory holding the result, inside a memory pool.
1545 char *talloc_asprintf(const void *t, const char *fmt, ...)
1551 ret = talloc_vasprintf(t, fmt, ap);
1556 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1557 const char *fmt, va_list ap)
1558 PRINTF_ATTRIBUTE(3,0);
1560 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1561 const char *fmt, va_list ap)
1568 alen = vsnprintf(&c, 1, fmt, ap2);
1572 /* Either the vsnprintf failed or the format resulted in
1573 * no characters being formatted. In the former case, we
1574 * ought to return NULL, in the latter we ought to return
1575 * the original string. Most current callers of this
1576 * function expect it to never return NULL.
1581 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1582 if (!s) return NULL;
1585 vsnprintf(s + slen, alen + 1, fmt, ap2);
1588 _talloc_set_name_const(s, s);
1593 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1594 * and return @p s, which may have moved. Good for gradually
1595 * accumulating output into a string buffer. Appends at the end
1598 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1601 return talloc_vasprintf(NULL, fmt, ap);
1604 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1608 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1609 * and return @p s, which may have moved. Always appends at the
1610 * end of the talloc'ed buffer, not the end of the string.
1612 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1617 return talloc_vasprintf(NULL, fmt, ap);
1620 slen = talloc_get_size(s);
1621 if (likely(slen > 0)) {
1625 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1629 Realloc @p s to append the formatted result of @p fmt and return @p
1630 s, which may have moved. Good for gradually accumulating output
1631 into a string buffer.
1633 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1638 s = talloc_vasprintf_append(s, fmt, ap);
1644 Realloc @p s to append the formatted result of @p fmt and return @p
1645 s, which may have moved. Good for gradually accumulating output
1648 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1653 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1659 alloc an array, checking for integer overflow in the array size
1661 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1663 if (count >= MAX_TALLOC_SIZE/el_size) {
1666 return _talloc_named_const(ctx, el_size * count, name);
1670 alloc an zero array, checking for integer overflow in the array size
1672 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1674 if (count >= MAX_TALLOC_SIZE/el_size) {
1677 return _talloc_zero(ctx, el_size * count, name);
1681 realloc an array, checking for integer overflow in the array size
1683 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1685 if (count >= MAX_TALLOC_SIZE/el_size) {
1688 return _talloc_realloc(ctx, ptr, el_size * count, name);
1692 a function version of talloc_realloc(), so it can be passed as a function pointer
1693 to libraries that want a realloc function (a realloc function encapsulates
1694 all the basic capabilities of an allocation library, which is why this is useful)
1696 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1698 return _talloc_realloc(context, ptr, size, NULL);
1702 static int talloc_autofree_destructor(void *ptr)
1704 autofree_context = NULL;
1708 static void talloc_autofree(void)
1710 _talloc_free(autofree_context);
1714 return a context which will be auto-freed on exit
1715 this is useful for reducing the noise in leak reports
1717 void *talloc_autofree_context(void)
1719 if (autofree_context == NULL) {
1720 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1721 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1722 atexit(talloc_autofree);
1724 return autofree_context;
1727 /* this is only used by the test suite so that failures from on test don't
1728 confuse the results of the next test */
1729 void talloc_erase_no_owner_context(void)
1731 /* force free all children */
1732 if (no_owner_context) {
1733 struct talloc_chunk *tc;
1734 tc = talloc_chunk_from_ptr(no_owner_context);
1737 _talloc_free(no_owner_context);
1738 no_owner_context=NULL;
1742 void *talloc_no_owner_context(void)
1744 if (no_owner_context == NULL) {
1745 no_owner_context = _talloc_named_const(NULL, 0, "no_owner_context");
1747 return no_owner_context;
1750 size_t talloc_get_size(const void *context)
1752 struct talloc_chunk *tc;
1754 if (context == NULL) {
1755 context = null_context;
1757 if (context == NULL) {
1761 tc = talloc_chunk_from_ptr(context);
1767 find a parent of this context that has the given name, if any
1769 void *talloc_find_parent_byname(const void *context, const char *name)
1771 struct talloc_chunk *tc;
1773 if (context == NULL) {
1777 tc = talloc_chunk_from_ptr(context);
1779 if (tc->name && strcmp(tc->name, name) == 0) {
1780 return TC_PTR_FROM_CHUNK(tc);
1782 while (tc && tc->prev) tc = tc->prev;
1791 show the parentage of a context
1793 void talloc_show_parents(const void *context, FILE *file)
1795 struct talloc_chunk *tc;
1797 if (context == NULL) {
1798 fprintf(file, "talloc no parents for NULL\n");
1802 tc = talloc_chunk_from_ptr(context);
1803 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1805 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1806 while (tc && tc->prev) tc = tc->prev;
1815 return 1 if ptr is a parent of context
1817 int talloc_is_parent(const void *context, const void *ptr)
1819 struct talloc_chunk *tc;
1821 if (context == NULL) {
1825 tc = talloc_chunk_from_ptr(context);
1827 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1828 while (tc && tc->prev) tc = tc->prev;