2 Samba Unix SMB/CIFS implementation.
4 Samba trivial allocation library - new interface
6 NOTE: Please read talloc_guide.txt for full documentation
8 Copyright (C) Andrew Tridgell 2004
9 Copyright (C) Stefan Metzmacher 2006
11 ** NOTE! The following LGPL license applies to the talloc
12 ** library. This does NOT imply that all of Samba is released
15 This library is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Lesser General Public
17 License as published by the Free Software Foundation; either
18 version 3 of the License, or (at your option) any later version.
20 This library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Lesser General Public License for more details.
25 You should have received a copy of the GNU Lesser General Public
26 License along with this library; if not, see <http://www.gnu.org/licenses/>.
30 inspired by http://swapped.cc/halloc/
35 #if (SAMBA_VERSION_MAJOR<4)
37 /* This is to circumvent SAMBA3's paranoid malloc checker. Here in this file
38 * we trust ourselves... */
45 #define _TALLOC_SAMBA3
46 #endif /* (SAMBA_VERSION_MAJOR<4) */
47 #endif /* _SAMBA_BUILD_ */
49 #ifndef _TALLOC_SAMBA3
52 #endif /* not _TALLOC_SAMBA3 */
54 /* use this to force every realloc to change the pointer, to stress test
55 code that might not cope */
56 #define ALWAYS_REALLOC 0
59 #define MAX_TALLOC_SIZE 0x10000000
60 #define TALLOC_MAGIC_V1 0xe814ec70
61 #define TALLOC_MAGIC_V2 0xe814ec80
62 #define TALLOC_MAGIC TALLOC_MAGIC_V2
63 #define TALLOC_FLAG_FREE 0x01
64 #define TALLOC_FLAG_LOOP 0x02
65 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
66 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
67 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
69 /* by default we abort when given a bad pointer (such as when talloc_free() is called
70 on a pointer that came from malloc() */
72 #define TALLOC_ABORT(reason) abort()
75 #ifndef discard_const_p
76 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
77 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
79 # define discard_const_p(type, ptr) ((type *)(ptr))
83 /* these macros gain us a few percent of speed on gcc */
85 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
86 as its first argument */
88 #define likely(x) __builtin_expect(!!(x), 1)
91 #define unlikely(x) __builtin_expect(!!(x), 0)
98 #define unlikely(x) (x)
102 /* this null_context is only used if talloc_enable_leak_report() or
103 talloc_enable_leak_report_full() is called, otherwise it remains
106 static void *null_context;
107 static void *autofree_context;
109 struct talloc_reference_handle {
110 struct talloc_reference_handle *next, *prev;
114 typedef int (*talloc_destructor_t)(void *);
116 struct talloc_chunk {
117 struct talloc_chunk *next, *prev;
118 struct talloc_chunk *parent, *child;
119 struct talloc_reference_handle *refs;
120 talloc_destructor_t destructor;
126 * "pool" has dual use:
128 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
129 * marks the end of the currently allocated area.
131 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
132 * is a pointer to the struct talloc_chunk of the pool that it was
133 * allocated from. This way children can quickly find the pool to chew
139 /* 16 byte alignment seems to keep everyone happy */
140 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
141 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
143 static void (*talloc_abort_fn)(const char *reason);
145 void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
147 talloc_abort_fn = abort_fn;
150 static void talloc_abort(const char *reason)
152 if (!talloc_abort_fn) {
153 TALLOC_ABORT(reason);
156 talloc_abort_fn(reason);
159 static void talloc_abort_magic_v1(void)
161 talloc_abort("Bad talloc magic value - old magic v1 used");
164 static void talloc_abort_double_free(void)
166 talloc_abort("Bad talloc magic value - double free");
169 static void talloc_abort_unknown_value(void)
171 talloc_abort("Bad talloc magic value - unknown value");
174 /* panic if we get a bad magic value */
175 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
177 const char *pp = (const char *)ptr;
178 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
179 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
180 if ((tc->flags & (~0xF)) == TALLOC_MAGIC_V1)) {
181 talloc_abort_magic_v1();
184 if (tc->flags & TALLOC_FLAG_FREE) {
185 talloc_abort_double_free();
187 talloc_abort_unknown_value();
193 /* hook into the front of the list */
194 #define _TLIST_ADD(list, p) \
198 (p)->next = (p)->prev = NULL; \
200 (list)->prev = (p); \
201 (p)->next = (list); \
207 /* remove an element from a list - element doesn't have to be in list. */
208 #define _TLIST_REMOVE(list, p) \
210 if ((p) == (list)) { \
211 (list) = (p)->next; \
212 if (list) (list)->prev = NULL; \
214 if ((p)->prev) (p)->prev->next = (p)->next; \
215 if ((p)->next) (p)->next->prev = (p)->prev; \
217 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
222 return the parent chunk of a pointer
224 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
226 struct talloc_chunk *tc;
228 if (unlikely(ptr == NULL)) {
232 tc = talloc_chunk_from_ptr(ptr);
233 while (tc->prev) tc=tc->prev;
238 void *talloc_parent(const void *ptr)
240 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
241 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
247 const char *talloc_parent_name(const void *ptr)
249 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
250 return tc? tc->name : NULL;
254 A pool carries an in-pool object count count in the first 16 bytes.
255 bytes. This is done to support talloc_steal() to a parent outside of the
256 pool. The count includes the pool itself, so a talloc_free() on a pool will
257 only destroy the pool if the count has dropped to zero. A talloc_free() of a
258 pool member will reduce the count, and eventually also call free(3) on the
261 The object count is not put into "struct talloc_chunk" because it is only
262 relevant for talloc pools and the alignment to 16 bytes would increase the
263 memory footprint of each talloc chunk by those 16 bytes.
266 #define TALLOC_POOL_HDR_SIZE 16
268 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
270 return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
277 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
280 struct talloc_chunk *pool_ctx = NULL;
282 struct talloc_chunk *result;
285 if (parent == NULL) {
289 if (parent->flags & TALLOC_FLAG_POOL) {
292 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
293 pool_ctx = (struct talloc_chunk *)parent->pool;
296 if (pool_ctx == NULL) {
300 space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
301 - ((char *)pool_ctx->pool);
304 * Align size to 16 bytes
306 chunk_size = ((size + 15) & ~15);
308 if (space_left < chunk_size) {
312 result = (struct talloc_chunk *)pool_ctx->pool;
314 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
315 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
318 pool_ctx->pool = (void *)((char *)result + chunk_size);
320 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
321 result->pool = pool_ctx;
323 *talloc_pool_objectcount(pool_ctx) += 1;
329 Allocate a bit of memory as a child of an existing pointer
331 static inline void *__talloc(const void *context, size_t size)
333 struct talloc_chunk *tc = NULL;
335 if (unlikely(context == NULL)) {
336 context = null_context;
339 if (unlikely(size >= MAX_TALLOC_SIZE)) {
343 if (context != NULL) {
344 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
349 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
350 if (unlikely(tc == NULL)) return NULL;
351 tc->flags = TALLOC_MAGIC;
356 tc->destructor = NULL;
361 if (likely(context)) {
362 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
365 parent->child->parent = NULL;
366 tc->next = parent->child;
375 tc->next = tc->prev = tc->parent = NULL;
378 return TC_PTR_FROM_CHUNK(tc);
382 * Create a talloc pool
385 void *talloc_pool(const void *context, size_t size)
387 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
388 struct talloc_chunk *tc;
390 if (unlikely(result == NULL)) {
394 tc = talloc_chunk_from_ptr(result);
396 tc->flags |= TALLOC_FLAG_POOL;
397 tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
399 *talloc_pool_objectcount(tc) = 1;
401 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
402 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
409 setup a destructor to be called on free of a pointer
410 the destructor should return 0 on success, or -1 on failure.
411 if the destructor fails then the free is failed, and the memory can
412 be continued to be used
414 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
416 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
417 tc->destructor = destructor;
421 increase the reference count on a piece of memory.
423 int talloc_increase_ref_count(const void *ptr)
425 if (unlikely(!talloc_reference(null_context, ptr))) {
432 helper for talloc_reference()
434 this is referenced by a function pointer and should not be inline
436 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
438 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
439 _TLIST_REMOVE(ptr_tc->refs, handle);
444 more efficient way to add a name to a pointer - the name must point to a
447 static inline void _talloc_set_name_const(const void *ptr, const char *name)
449 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
454 internal talloc_named_const()
456 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
460 ptr = __talloc(context, size);
461 if (unlikely(ptr == NULL)) {
465 _talloc_set_name_const(ptr, name);
471 make a secondary reference to a pointer, hanging off the given context.
472 the pointer remains valid until both the original caller and this given
475 the major use for this is when two different structures need to reference the
476 same underlying data, and you want to be able to free the two instances separately,
479 void *_talloc_reference(const void *context, const void *ptr)
481 struct talloc_chunk *tc;
482 struct talloc_reference_handle *handle;
483 if (unlikely(ptr == NULL)) return NULL;
485 tc = talloc_chunk_from_ptr(ptr);
486 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
487 sizeof(struct talloc_reference_handle),
488 TALLOC_MAGIC_REFERENCE);
489 if (unlikely(handle == NULL)) return NULL;
491 /* note that we hang the destructor off the handle, not the
492 main context as that allows the caller to still setup their
493 own destructor on the context if they want to */
494 talloc_set_destructor(handle, talloc_reference_destructor);
495 handle->ptr = discard_const_p(void, ptr);
496 _TLIST_ADD(tc->refs, handle);
502 internal talloc_free call
504 static inline int _talloc_free(void *ptr)
506 struct talloc_chunk *tc;
508 if (unlikely(ptr == NULL)) {
512 tc = talloc_chunk_from_ptr(ptr);
514 if (unlikely(tc->refs)) {
516 /* check this is a reference from a child or grantchild
517 * back to it's parent or grantparent
519 * in that case we need to remove the reference and
520 * call another instance of talloc_free() on the current
523 is_child = talloc_is_parent(tc->refs, ptr);
524 _talloc_free(tc->refs);
526 return _talloc_free(ptr);
531 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
532 /* we have a free loop - stop looping */
536 if (unlikely(tc->destructor)) {
537 talloc_destructor_t d = tc->destructor;
538 if (d == (talloc_destructor_t)-1) {
541 tc->destructor = (talloc_destructor_t)-1;
546 tc->destructor = NULL;
550 _TLIST_REMOVE(tc->parent->child, tc);
551 if (tc->parent->child) {
552 tc->parent->child->parent = tc->parent;
555 if (tc->prev) tc->prev->next = tc->next;
556 if (tc->next) tc->next->prev = tc->prev;
559 tc->flags |= TALLOC_FLAG_LOOP;
562 /* we need to work out who will own an abandoned child
563 if it cannot be freed. In priority order, the first
564 choice is owner of any remaining reference to this
565 pointer, the second choice is our parent, and the
566 final choice is the null context. */
567 void *child = TC_PTR_FROM_CHUNK(tc->child);
568 const void *new_parent = null_context;
569 if (unlikely(tc->child->refs)) {
570 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
571 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
573 if (unlikely(_talloc_free(child) == -1)) {
574 if (new_parent == null_context) {
575 struct talloc_chunk *p = talloc_parent_chunk(ptr);
576 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
578 talloc_steal(new_parent, child);
582 tc->flags |= TALLOC_FLAG_FREE;
584 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
585 struct talloc_chunk *pool;
586 unsigned int *pool_object_count;
588 pool = (tc->flags & TALLOC_FLAG_POOL)
589 ? tc : (struct talloc_chunk *)tc->pool;
591 pool_object_count = talloc_pool_objectcount(pool);
593 if (*pool_object_count == 0) {
594 talloc_abort("Pool object count zero!");
597 *pool_object_count -= 1;
599 if (*pool_object_count == 0) {
610 move a lump of memory from one talloc context to another return the
611 ptr on success, or NULL if it could not be transferred.
612 passing NULL as ptr will always return NULL with no side effects.
614 void *_talloc_steal(const void *new_ctx, const void *ptr)
616 struct talloc_chunk *tc, *new_tc;
618 if (unlikely(!ptr)) {
622 if (unlikely(new_ctx == NULL)) {
623 new_ctx = null_context;
626 tc = talloc_chunk_from_ptr(ptr);
628 if (unlikely(new_ctx == NULL)) {
630 _TLIST_REMOVE(tc->parent->child, tc);
631 if (tc->parent->child) {
632 tc->parent->child->parent = tc->parent;
635 if (tc->prev) tc->prev->next = tc->next;
636 if (tc->next) tc->next->prev = tc->prev;
639 tc->parent = tc->next = tc->prev = NULL;
640 return discard_const_p(void, ptr);
643 new_tc = talloc_chunk_from_ptr(new_ctx);
645 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
646 return discard_const_p(void, ptr);
650 _TLIST_REMOVE(tc->parent->child, tc);
651 if (tc->parent->child) {
652 tc->parent->child->parent = tc->parent;
655 if (tc->prev) tc->prev->next = tc->next;
656 if (tc->next) tc->next->prev = tc->prev;
660 if (new_tc->child) new_tc->child->parent = NULL;
661 _TLIST_ADD(new_tc->child, tc);
663 return discard_const_p(void, ptr);
669 remove a secondary reference to a pointer. This undo's what
670 talloc_reference() has done. The context and pointer arguments
671 must match those given to a talloc_reference()
673 static inline int talloc_unreference(const void *context, const void *ptr)
675 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
676 struct talloc_reference_handle *h;
678 if (unlikely(context == NULL)) {
679 context = null_context;
682 for (h=tc->refs;h;h=h->next) {
683 struct talloc_chunk *p = talloc_parent_chunk(h);
685 if (context == NULL) break;
686 } else if (TC_PTR_FROM_CHUNK(p) == context) {
694 return _talloc_free(h);
698 remove a specific parent context from a pointer. This is a more
699 controlled varient of talloc_free()
701 int talloc_unlink(const void *context, void *ptr)
703 struct talloc_chunk *tc_p, *new_p;
710 if (context == NULL) {
711 context = null_context;
714 if (talloc_unreference(context, ptr) == 0) {
718 if (context == NULL) {
719 if (talloc_parent_chunk(ptr) != NULL) {
723 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
728 tc_p = talloc_chunk_from_ptr(ptr);
730 if (tc_p->refs == NULL) {
731 return _talloc_free(ptr);
734 new_p = talloc_parent_chunk(tc_p->refs);
736 new_parent = TC_PTR_FROM_CHUNK(new_p);
741 if (talloc_unreference(new_parent, ptr) != 0) {
745 talloc_steal(new_parent, ptr);
751 add a name to an existing pointer - va_list version
753 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
755 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
757 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
758 tc->name = talloc_vasprintf(ptr, fmt, ap);
759 if (likely(tc->name)) {
760 _talloc_set_name_const(tc->name, ".name");
766 add a name to an existing pointer
768 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
773 name = talloc_set_name_v(ptr, fmt, ap);
780 create a named talloc pointer. Any talloc pointer can be named, and
781 talloc_named() operates just like talloc() except that it allows you
784 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
790 ptr = __talloc(context, size);
791 if (unlikely(ptr == NULL)) return NULL;
794 name = talloc_set_name_v(ptr, fmt, ap);
797 if (unlikely(name == NULL)) {
806 return the name of a talloc ptr, or "UNNAMED"
808 const char *talloc_get_name(const void *ptr)
810 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
811 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
814 if (likely(tc->name)) {
822 check if a pointer has the given name. If it does, return the pointer,
823 otherwise return NULL
825 void *talloc_check_name(const void *ptr, const char *name)
828 if (unlikely(ptr == NULL)) return NULL;
829 pname = talloc_get_name(ptr);
830 if (likely(pname == name || strcmp(pname, name) == 0)) {
831 return discard_const_p(void, ptr);
836 static void talloc_abort_type_missmatch(const char *location,
838 const char *expected)
842 reason = talloc_asprintf(NULL,
843 "%s: Type mismatch: name[%s] expected[%s]",
848 reason = "Type mismatch";
851 talloc_abort(reason);
854 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
858 if (unlikely(ptr == NULL)) {
859 talloc_abort_type_missmatch(location, NULL, name);
863 pname = talloc_get_name(ptr);
864 if (likely(pname == name || strcmp(pname, name) == 0)) {
865 return discard_const_p(void, ptr);
868 talloc_abort_type_missmatch(location, pname, name);
873 this is for compatibility with older versions of talloc
875 void *talloc_init(const char *fmt, ...)
882 * samba3 expects talloc_report_depth_cb(NULL, ...)
883 * reports all talloc'ed memory, so we need to enable
886 talloc_enable_null_tracking();
888 ptr = __talloc(NULL, 0);
889 if (unlikely(ptr == NULL)) return NULL;
892 name = talloc_set_name_v(ptr, fmt, ap);
895 if (unlikely(name == NULL)) {
904 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
905 should probably not be used in new code. It's in here to keep the talloc
906 code consistent across Samba 3 and 4.
908 void talloc_free_children(void *ptr)
910 struct talloc_chunk *tc;
912 if (unlikely(ptr == NULL)) {
916 tc = talloc_chunk_from_ptr(ptr);
919 /* we need to work out who will own an abandoned child
920 if it cannot be freed. In priority order, the first
921 choice is owner of any remaining reference to this
922 pointer, the second choice is our parent, and the
923 final choice is the null context. */
924 void *child = TC_PTR_FROM_CHUNK(tc->child);
925 const void *new_parent = null_context;
926 if (unlikely(tc->child->refs)) {
927 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
928 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
930 if (unlikely(_talloc_free(child) == -1)) {
931 if (new_parent == null_context) {
932 struct talloc_chunk *p = talloc_parent_chunk(ptr);
933 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
935 talloc_steal(new_parent, child);
939 if ((tc->flags & TALLOC_FLAG_POOL)
940 && (*talloc_pool_objectcount(tc) == 1)) {
941 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
942 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
943 VALGRIND_MAKE_MEM_NOACCESS(
944 tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
950 Allocate a bit of memory as a child of an existing pointer
952 void *_talloc(const void *context, size_t size)
954 return __talloc(context, size);
958 externally callable talloc_set_name_const()
960 void talloc_set_name_const(const void *ptr, const char *name)
962 _talloc_set_name_const(ptr, name);
966 create a named talloc pointer. Any talloc pointer can be named, and
967 talloc_named() operates just like talloc() except that it allows you
970 void *talloc_named_const(const void *context, size_t size, const char *name)
972 return _talloc_named_const(context, size, name);
976 free a talloc pointer. This also frees all child pointers of this
979 return 0 if the memory is actually freed, otherwise -1. The memory
980 will not be freed if the ref_count is > 1 or the destructor (if
981 any) returns non-zero
983 int talloc_free(void *ptr)
985 return _talloc_free(ptr);
991 A talloc version of realloc. The context argument is only used if
994 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
996 struct talloc_chunk *tc;
998 bool malloced = false;
1000 /* size zero is equivalent to free() */
1001 if (unlikely(size == 0)) {
1006 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1010 /* realloc(NULL) is equivalent to malloc() */
1012 return _talloc_named_const(context, size, name);
1015 tc = talloc_chunk_from_ptr(ptr);
1017 /* don't allow realloc on referenced pointers */
1018 if (unlikely(tc->refs)) {
1022 /* don't let anybody try to realloc a talloc_pool */
1023 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1027 /* don't shrink if we have less than 1k to gain */
1028 if ((size < tc->size) && ((tc->size - size) < 1024)) {
1033 /* by resetting magic we catch users of the old memory */
1034 tc->flags |= TALLOC_FLAG_FREE;
1037 new_ptr = malloc(size + TC_HDR_SIZE);
1039 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
1043 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1045 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1046 *talloc_pool_objectcount((struct talloc_chunk *)
1049 if (new_ptr == NULL) {
1050 new_ptr = malloc(TC_HDR_SIZE+size);
1055 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1059 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1062 if (unlikely(!new_ptr)) {
1063 tc->flags &= ~TALLOC_FLAG_FREE;
1067 tc = (struct talloc_chunk *)new_ptr;
1068 tc->flags &= ~TALLOC_FLAG_FREE;
1070 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1073 tc->parent->child = tc;
1076 tc->child->parent = tc;
1080 tc->prev->next = tc;
1083 tc->next->prev = tc;
1087 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1089 return TC_PTR_FROM_CHUNK(tc);
1093 a wrapper around talloc_steal() for situations where you are moving a pointer
1094 between two structures, and want the old pointer to be set to NULL
1096 void *_talloc_move(const void *new_ctx, const void *_pptr)
1098 const void **pptr = discard_const_p(const void *,_pptr);
1099 void *ret = _talloc_steal(new_ctx, *pptr);
1105 return the total size of a talloc pool (subtree)
1107 size_t talloc_total_size(const void *ptr)
1110 struct talloc_chunk *c, *tc;
1119 tc = talloc_chunk_from_ptr(ptr);
1121 if (tc->flags & TALLOC_FLAG_LOOP) {
1125 tc->flags |= TALLOC_FLAG_LOOP;
1128 for (c=tc->child;c;c=c->next) {
1129 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1132 tc->flags &= ~TALLOC_FLAG_LOOP;
1138 return the total number of blocks in a talloc pool (subtree)
1140 size_t talloc_total_blocks(const void *ptr)
1143 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
1145 if (tc->flags & TALLOC_FLAG_LOOP) {
1149 tc->flags |= TALLOC_FLAG_LOOP;
1152 for (c=tc->child;c;c=c->next) {
1153 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1156 tc->flags &= ~TALLOC_FLAG_LOOP;
1162 return the number of external references to a pointer
1164 size_t talloc_reference_count(const void *ptr)
1166 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1167 struct talloc_reference_handle *h;
1170 for (h=tc->refs;h;h=h->next) {
1177 report on memory usage by all children of a pointer, giving a full tree view
1179 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1180 void (*callback)(const void *ptr,
1181 int depth, int max_depth,
1183 void *private_data),
1186 struct talloc_chunk *c, *tc;
1191 if (ptr == NULL) return;
1193 tc = talloc_chunk_from_ptr(ptr);
1195 if (tc->flags & TALLOC_FLAG_LOOP) {
1199 callback(ptr, depth, max_depth, 0, private_data);
1201 if (max_depth >= 0 && depth >= max_depth) {
1205 tc->flags |= TALLOC_FLAG_LOOP;
1206 for (c=tc->child;c;c=c->next) {
1207 if (c->name == TALLOC_MAGIC_REFERENCE) {
1208 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1209 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1211 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1214 tc->flags &= ~TALLOC_FLAG_LOOP;
1217 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1219 const char *name = talloc_get_name(ptr);
1220 FILE *f = (FILE *)_f;
1223 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1228 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1229 (max_depth < 0 ? "full " :""), name,
1230 (unsigned long)talloc_total_size(ptr),
1231 (unsigned long)talloc_total_blocks(ptr));
1235 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1238 (unsigned long)talloc_total_size(ptr),
1239 (unsigned long)talloc_total_blocks(ptr),
1240 (int)talloc_reference_count(ptr), ptr);
1243 fprintf(f, "content: ");
1244 if (talloc_total_size(ptr)) {
1245 int tot = talloc_total_size(ptr);
1248 for (i = 0; i < tot; i++) {
1249 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1250 fprintf(f, "%c", ((char *)ptr)[i]);
1252 fprintf(f, "~%02x", ((char *)ptr)[i]);
1261 report on memory usage by all children of a pointer, giving a full tree view
1263 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1265 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1270 report on memory usage by all children of a pointer, giving a full tree view
1272 void talloc_report_full(const void *ptr, FILE *f)
1274 talloc_report_depth_file(ptr, 0, -1, f);
1278 report on memory usage by all children of a pointer
1280 void talloc_report(const void *ptr, FILE *f)
1282 talloc_report_depth_file(ptr, 0, 1, f);
1286 report on any memory hanging off the null context
1288 static void talloc_report_null(void)
1290 if (talloc_total_size(null_context) != 0) {
1291 talloc_report(null_context, stderr);
1296 report on any memory hanging off the null context
1298 static void talloc_report_null_full(void)
1300 if (talloc_total_size(null_context) != 0) {
1301 talloc_report_full(null_context, stderr);
1306 enable tracking of the NULL context
1308 void talloc_enable_null_tracking(void)
1310 if (null_context == NULL) {
1311 null_context = _talloc_named_const(NULL, 0, "null_context");
1316 disable tracking of the NULL context
1318 void talloc_disable_null_tracking(void)
1320 _talloc_free(null_context);
1321 null_context = NULL;
1325 enable leak reporting on exit
1327 void talloc_enable_leak_report(void)
1329 talloc_enable_null_tracking();
1330 atexit(talloc_report_null);
1334 enable full leak reporting on exit
1336 void talloc_enable_leak_report_full(void)
1338 talloc_enable_null_tracking();
1339 atexit(talloc_report_null_full);
1343 talloc and zero memory.
1345 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1347 void *p = _talloc_named_const(ctx, size, name);
1350 memset(p, '\0', size);
1357 memdup with a talloc.
1359 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1361 void *newp = _talloc_named_const(t, size, name);
1364 memcpy(newp, p, size);
1370 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1374 ret = (char *)__talloc(t, len + 1);
1375 if (unlikely(!ret)) return NULL;
1377 memcpy(ret, p, len);
1380 _talloc_set_name_const(ret, ret);
1385 strdup with a talloc
1387 char *talloc_strdup(const void *t, const char *p)
1389 if (unlikely(!p)) return NULL;
1390 return __talloc_strlendup(t, p, strlen(p));
1394 strndup with a talloc
1396 char *talloc_strndup(const void *t, const char *p, size_t n)
1398 if (unlikely(!p)) return NULL;
1399 return __talloc_strlendup(t, p, strnlen(p, n));
1402 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1403 const char *a, size_t alen)
1407 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1408 if (unlikely(!ret)) return NULL;
1410 /* append the string and the trailing \0 */
1411 memcpy(&ret[slen], a, alen);
1414 _talloc_set_name_const(ret, ret);
1419 * Appends at the end of the string.
1421 char *talloc_strdup_append(char *s, const char *a)
1424 return talloc_strdup(NULL, a);
1431 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1435 * Appends at the end of the talloc'ed buffer,
1436 * not the end of the string.
1438 char *talloc_strdup_append_buffer(char *s, const char *a)
1443 return talloc_strdup(NULL, a);
1450 slen = talloc_get_size(s);
1451 if (likely(slen > 0)) {
1455 return __talloc_strlendup_append(s, slen, a, strlen(a));
1459 * Appends at the end of the string.
1461 char *talloc_strndup_append(char *s, const char *a, size_t n)
1464 return talloc_strdup(NULL, a);
1471 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1475 * Appends at the end of the talloc'ed buffer,
1476 * not the end of the string.
1478 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1483 return talloc_strdup(NULL, a);
1490 slen = talloc_get_size(s);
1491 if (likely(slen > 0)) {
1495 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1498 #ifndef HAVE_VA_COPY
1499 #ifdef HAVE___VA_COPY
1500 #define va_copy(dest, src) __va_copy(dest, src)
1502 #define va_copy(dest, src) (dest) = (src)
1506 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1513 /* this call looks strange, but it makes it work on older solaris boxes */
1515 len = vsnprintf(&c, 1, fmt, ap2);
1517 if (unlikely(len < 0)) {
1521 ret = (char *)__talloc(t, len+1);
1522 if (unlikely(!ret)) return NULL;
1525 vsnprintf(ret, len+1, fmt, ap2);
1528 _talloc_set_name_const(ret, ret);
1534 Perform string formatting, and return a pointer to newly allocated
1535 memory holding the result, inside a memory pool.
1537 char *talloc_asprintf(const void *t, const char *fmt, ...)
1543 ret = talloc_vasprintf(t, fmt, ap);
1548 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1549 const char *fmt, va_list ap)
1550 PRINTF_ATTRIBUTE(3,0);
1552 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1553 const char *fmt, va_list ap)
1560 alen = vsnprintf(&c, 1, fmt, ap2);
1564 /* Either the vsnprintf failed or the format resulted in
1565 * no characters being formatted. In the former case, we
1566 * ought to return NULL, in the latter we ought to return
1567 * the original string. Most current callers of this
1568 * function expect it to never return NULL.
1573 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1574 if (!s) return NULL;
1577 vsnprintf(s + slen, alen + 1, fmt, ap2);
1580 _talloc_set_name_const(s, s);
1585 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1586 * and return @p s, which may have moved. Good for gradually
1587 * accumulating output into a string buffer. Appends at the end
1590 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1593 return talloc_vasprintf(NULL, fmt, ap);
1596 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1600 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1601 * and return @p s, which may have moved. Always appends at the
1602 * end of the talloc'ed buffer, not the end of the string.
1604 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1609 return talloc_vasprintf(NULL, fmt, ap);
1612 slen = talloc_get_size(s);
1613 if (likely(slen > 0)) {
1617 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1621 Realloc @p s to append the formatted result of @p fmt and return @p
1622 s, which may have moved. Good for gradually accumulating output
1623 into a string buffer.
1625 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1630 s = talloc_vasprintf_append(s, fmt, ap);
1636 Realloc @p s to append the formatted result of @p fmt and return @p
1637 s, which may have moved. Good for gradually accumulating output
1640 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1645 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1651 alloc an array, checking for integer overflow in the array size
1653 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1655 if (count >= MAX_TALLOC_SIZE/el_size) {
1658 return _talloc_named_const(ctx, el_size * count, name);
1662 alloc an zero array, checking for integer overflow in the array size
1664 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1666 if (count >= MAX_TALLOC_SIZE/el_size) {
1669 return _talloc_zero(ctx, el_size * count, name);
1673 realloc an array, checking for integer overflow in the array size
1675 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1677 if (count >= MAX_TALLOC_SIZE/el_size) {
1680 return _talloc_realloc(ctx, ptr, el_size * count, name);
1684 a function version of talloc_realloc(), so it can be passed as a function pointer
1685 to libraries that want a realloc function (a realloc function encapsulates
1686 all the basic capabilities of an allocation library, which is why this is useful)
1688 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1690 return _talloc_realloc(context, ptr, size, NULL);
1694 static int talloc_autofree_destructor(void *ptr)
1696 autofree_context = NULL;
1700 static void talloc_autofree(void)
1702 _talloc_free(autofree_context);
1706 return a context which will be auto-freed on exit
1707 this is useful for reducing the noise in leak reports
1709 void *talloc_autofree_context(void)
1711 if (autofree_context == NULL) {
1712 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1713 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1714 atexit(talloc_autofree);
1716 return autofree_context;
1719 size_t talloc_get_size(const void *context)
1721 struct talloc_chunk *tc;
1723 if (context == NULL)
1726 tc = talloc_chunk_from_ptr(context);
1732 find a parent of this context that has the given name, if any
1734 void *talloc_find_parent_byname(const void *context, const char *name)
1736 struct talloc_chunk *tc;
1738 if (context == NULL) {
1742 tc = talloc_chunk_from_ptr(context);
1744 if (tc->name && strcmp(tc->name, name) == 0) {
1745 return TC_PTR_FROM_CHUNK(tc);
1747 while (tc && tc->prev) tc = tc->prev;
1756 show the parentage of a context
1758 void talloc_show_parents(const void *context, FILE *file)
1760 struct talloc_chunk *tc;
1762 if (context == NULL) {
1763 fprintf(file, "talloc no parents for NULL\n");
1767 tc = talloc_chunk_from_ptr(context);
1768 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1770 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1771 while (tc && tc->prev) tc = tc->prev;
1780 return 1 if ptr is a parent of context
1782 int talloc_is_parent(const void *context, const void *ptr)
1784 struct talloc_chunk *tc;
1786 if (context == NULL) {
1790 tc = talloc_chunk_from_ptr(context);
1792 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1793 while (tc && tc->prev) tc = tc->prev;