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/
36 #ifdef TALLOC_BUILD_VERSION_MAJOR
37 #if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)
38 #error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"
42 #ifdef TALLOC_BUILD_VERSION_MINOR
43 #if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)
44 #error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"
48 /* Special macros that are no-ops except when run under Valgrind on
49 * x86. They've moved a little bit from valgrind 1.0.4 to 1.9.4 */
50 #ifdef HAVE_VALGRIND_MEMCHECK_H
51 /* memcheck.h includes valgrind.h */
52 #include <valgrind/memcheck.h>
53 #elif defined(HAVE_VALGRIND_H)
57 /* use this to force every realloc to change the pointer, to stress test
58 code that might not cope */
59 #define ALWAYS_REALLOC 0
62 #define MAX_TALLOC_SIZE 0x10000000
63 #define TALLOC_MAGIC_BASE 0xe814ec70
64 #define TALLOC_MAGIC ( \
66 (TALLOC_VERSION_MAJOR << 12) + \
67 (TALLOC_VERSION_MINOR << 4) \
70 #define TALLOC_FLAG_FREE 0x01
71 #define TALLOC_FLAG_LOOP 0x02
72 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
73 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
74 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
76 /* by default we abort when given a bad pointer (such as when talloc_free() is called
77 on a pointer that came from malloc() */
79 #define TALLOC_ABORT(reason) abort()
82 #ifndef discard_const_p
83 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
84 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
86 # define discard_const_p(type, ptr) ((type *)(ptr))
90 /* these macros gain us a few percent of speed on gcc */
92 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
93 as its first argument */
95 #define likely(x) __builtin_expect(!!(x), 1)
98 #define unlikely(x) __builtin_expect(!!(x), 0)
102 #define likely(x) (x)
105 #define unlikely(x) (x)
109 /* this null_context is only used if talloc_enable_leak_report() or
110 talloc_enable_leak_report_full() is called, otherwise it remains
113 static void *null_context;
114 static void *autofree_context;
116 /* used to enable fill of memory on free, which can be useful for
117 * catching use after free errors when valgrind is too slow
125 #define TALLOC_FILL_ENV "TALLOC_FREE_FILL"
128 * do not wipe the header, to allow the
129 * double-free logic to still work
131 #define TC_INVALIDATE_FULL_FILL_CHUNK(_tc) do { \
132 if (unlikely(talloc_fill.enabled)) { \
133 size_t _flen = (_tc)->size; \
134 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
135 memset(_fptr, talloc_fill.fill_value, _flen); \
139 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
140 /* Mark the whole chunk as not accessable */
141 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { \
142 size_t _flen = TC_HDR_SIZE + (_tc)->size; \
143 char *_fptr = (char *)(_tc); \
144 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
147 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { } while (0)
150 #define TC_INVALIDATE_FULL_CHUNK(_tc) do { \
151 TC_INVALIDATE_FULL_FILL_CHUNK(_tc); \
152 TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc); \
155 #define TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
156 if (unlikely(talloc_fill.enabled)) { \
157 size_t _flen = (_tc)->size - (_new_size); \
158 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
159 _fptr += (_new_size); \
160 memset(_fptr, talloc_fill.fill_value, _flen); \
164 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
165 /* Mark the unused bytes not accessable */
166 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
167 size_t _flen = (_tc)->size - (_new_size); \
168 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
169 _fptr += (_new_size); \
170 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
173 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
176 #define TC_INVALIDATE_SHRINK_CHUNK(_tc, _new_size) do { \
177 TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size); \
178 TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
181 #define TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
182 if (unlikely(talloc_fill.enabled)) { \
183 size_t _flen = (_tc)->size - (_new_size); \
184 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
185 _fptr += (_new_size); \
186 memset(_fptr, talloc_fill.fill_value, _flen); \
190 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
191 /* Mark the unused bytes as undefined */
192 #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
193 size_t _flen = (_tc)->size - (_new_size); \
194 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
195 _fptr += (_new_size); \
196 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
199 #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
202 #define TC_UNDEFINE_SHRINK_CHUNK(_tc, _new_size) do { \
203 TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size); \
204 TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
207 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
208 /* Mark the new bytes as undefined */
209 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { \
210 size_t _old_used = TC_HDR_SIZE + (_tc)->size; \
211 size_t _new_used = TC_HDR_SIZE + (_new_size); \
212 size_t _flen = _new_used - _old_used; \
213 char *_fptr = _old_used + (char *)(_tc); \
214 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
217 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
220 #define TC_UNDEFINE_GROW_CHUNK(_tc, _new_size) do { \
221 TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
224 struct talloc_reference_handle {
225 struct talloc_reference_handle *next, *prev;
227 const char *location;
230 typedef int (*talloc_destructor_t)(void *);
232 struct talloc_chunk {
233 struct talloc_chunk *next, *prev;
234 struct talloc_chunk *parent, *child;
235 struct talloc_reference_handle *refs;
236 talloc_destructor_t destructor;
242 * "pool" has dual use:
244 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
245 * marks the end of the currently allocated area.
247 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
248 * is a pointer to the struct talloc_chunk of the pool that it was
249 * allocated from. This way children can quickly find the pool to chew
255 /* 16 byte alignment seems to keep everyone happy */
256 #define TC_ALIGN16(s) (((s)+15)&~15)
257 #define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
258 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
260 _PUBLIC_ int talloc_version_major(void)
262 return TALLOC_VERSION_MAJOR;
265 _PUBLIC_ int talloc_version_minor(void)
267 return TALLOC_VERSION_MINOR;
270 static void (*talloc_log_fn)(const char *message);
272 _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
274 talloc_log_fn = log_fn;
277 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
278 static void talloc_log(const char *fmt, ...)
283 if (!talloc_log_fn) {
288 message = talloc_vasprintf(NULL, fmt, ap);
291 talloc_log_fn(message);
292 talloc_free(message);
295 static void talloc_log_stderr(const char *message)
297 fprintf(stderr, "%s", message);
300 _PUBLIC_ void talloc_set_log_stderr(void)
302 talloc_set_log_fn(talloc_log_stderr);
305 static void (*talloc_abort_fn)(const char *reason);
307 _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
309 talloc_abort_fn = abort_fn;
312 static void talloc_abort(const char *reason)
314 talloc_log("%s\n", reason);
316 if (!talloc_abort_fn) {
317 TALLOC_ABORT(reason);
320 talloc_abort_fn(reason);
323 static void talloc_abort_magic(unsigned magic)
325 unsigned striped = magic - TALLOC_MAGIC_BASE;
326 unsigned major = (striped & 0xFFFFF000) >> 12;
327 unsigned minor = (striped & 0x00000FF0) >> 4;
328 talloc_log("Bad talloc magic[0x%08X/%u/%u] expected[0x%08X/%u/%u]\n",
330 TALLOC_MAGIC, TALLOC_VERSION_MAJOR, TALLOC_VERSION_MINOR);
331 talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
334 static void talloc_abort_access_after_free(void)
336 talloc_abort("Bad talloc magic value - access after free");
339 static void talloc_abort_unknown_value(void)
341 talloc_abort("Bad talloc magic value - unknown value");
344 /* panic if we get a bad magic value */
345 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
347 const char *pp = (const char *)ptr;
348 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
349 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
350 if ((tc->flags & (~0xFFF)) == TALLOC_MAGIC_BASE) {
351 talloc_abort_magic(tc->flags & (~0xF));
355 if (tc->flags & TALLOC_FLAG_FREE) {
356 talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
357 talloc_abort_access_after_free();
360 talloc_abort_unknown_value();
367 /* hook into the front of the list */
368 #define _TLIST_ADD(list, p) \
372 (p)->next = (p)->prev = NULL; \
374 (list)->prev = (p); \
375 (p)->next = (list); \
381 /* remove an element from a list - element doesn't have to be in list. */
382 #define _TLIST_REMOVE(list, p) \
384 if ((p) == (list)) { \
385 (list) = (p)->next; \
386 if (list) (list)->prev = NULL; \
388 if ((p)->prev) (p)->prev->next = (p)->next; \
389 if ((p)->next) (p)->next->prev = (p)->prev; \
391 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
396 return the parent chunk of a pointer
398 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
400 struct talloc_chunk *tc;
402 if (unlikely(ptr == NULL)) {
406 tc = talloc_chunk_from_ptr(ptr);
407 while (tc->prev) tc=tc->prev;
412 _PUBLIC_ void *talloc_parent(const void *ptr)
414 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
415 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
421 _PUBLIC_ const char *talloc_parent_name(const void *ptr)
423 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
424 return tc? tc->name : NULL;
428 A pool carries an in-pool object count count in the first 16 bytes.
429 bytes. This is done to support talloc_steal() to a parent outside of the
430 pool. The count includes the pool itself, so a talloc_free() on a pool will
431 only destroy the pool if the count has dropped to zero. A talloc_free() of a
432 pool member will reduce the count, and eventually also call free(3) on the
435 The object count is not put into "struct talloc_chunk" because it is only
436 relevant for talloc pools and the alignment to 16 bytes would increase the
437 memory footprint of each talloc chunk by those 16 bytes.
440 #define TALLOC_POOL_HDR_SIZE 16
442 #define TC_POOL_SPACE_LEFT(_pool_tc) \
443 PTR_DIFF(TC_HDR_SIZE + (_pool_tc)->size + (char *)(_pool_tc), \
446 #define TC_POOL_FIRST_CHUNK(_pool_tc) \
447 ((void *)(TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE + (char *)(_pool_tc)))
449 #define TC_POOLMEM_CHUNK_SIZE(_tc) \
450 TC_ALIGN16(TC_HDR_SIZE + (_tc)->size)
452 #define TC_POOLMEM_NEXT_CHUNK(_tc) \
453 ((void *)(TC_POOLMEM_CHUNK_SIZE(tc) + (char*)(_tc)))
455 /* Mark the whole remaining pool as not accessable */
456 #define TC_INVALIDATE_FILL_POOL(_pool_tc) do { \
457 if (unlikely(talloc_fill.enabled)) { \
458 size_t _flen = TC_POOL_SPACE_LEFT(_pool_tc); \
459 char *_fptr = (char *)(_pool_tc)->pool; \
460 memset(_fptr, talloc_fill.fill_value, _flen); \
464 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
465 /* Mark the whole remaining pool as not accessable */
466 #define TC_INVALIDATE_VALGRIND_POOL(_pool_tc) do { \
467 size_t _flen = TC_POOL_SPACE_LEFT(_pool_tc); \
468 char *_fptr = (char *)(_pool_tc)->pool; \
469 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
472 #define TC_INVALIDATE_VALGRIND_POOL(_pool_tc) do { } while (0)
475 #define TC_INVALIDATE_POOL(_pool_tc) do { \
476 TC_INVALIDATE_FILL_POOL(_pool_tc); \
477 TC_INVALIDATE_VALGRIND_POOL(_pool_tc); \
480 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
482 return (unsigned int *)((char *)tc + TC_HDR_SIZE);
489 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
492 struct talloc_chunk *pool_ctx = NULL;
494 struct talloc_chunk *result;
497 if (parent == NULL) {
501 if (parent->flags & TALLOC_FLAG_POOL) {
504 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
505 pool_ctx = (struct talloc_chunk *)parent->pool;
508 if (pool_ctx == NULL) {
512 space_left = TC_POOL_SPACE_LEFT(pool_ctx);
515 * Align size to 16 bytes
517 chunk_size = TC_ALIGN16(size);
519 if (space_left < chunk_size) {
523 result = (struct talloc_chunk *)pool_ctx->pool;
525 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
526 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
529 pool_ctx->pool = (void *)((char *)result + chunk_size);
531 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
532 result->pool = pool_ctx;
534 *talloc_pool_objectcount(pool_ctx) += 1;
540 Allocate a bit of memory as a child of an existing pointer
542 static inline void *__talloc(const void *context, size_t size)
544 struct talloc_chunk *tc = NULL;
546 if (unlikely(context == NULL)) {
547 context = null_context;
550 if (unlikely(size >= MAX_TALLOC_SIZE)) {
554 if (context != NULL) {
555 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
560 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
561 if (unlikely(tc == NULL)) return NULL;
562 tc->flags = TALLOC_MAGIC;
567 tc->destructor = NULL;
572 if (likely(context)) {
573 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
576 parent->child->parent = NULL;
577 tc->next = parent->child;
586 tc->next = tc->prev = tc->parent = NULL;
589 return TC_PTR_FROM_CHUNK(tc);
593 * Create a talloc pool
596 _PUBLIC_ void *talloc_pool(const void *context, size_t size)
598 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
599 struct talloc_chunk *tc;
601 if (unlikely(result == NULL)) {
605 tc = talloc_chunk_from_ptr(result);
607 tc->flags |= TALLOC_FLAG_POOL;
608 tc->pool = TC_POOL_FIRST_CHUNK(tc);
610 *talloc_pool_objectcount(tc) = 1;
612 TC_INVALIDATE_POOL(tc);
618 setup a destructor to be called on free of a pointer
619 the destructor should return 0 on success, or -1 on failure.
620 if the destructor fails then the free is failed, and the memory can
621 be continued to be used
623 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
625 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
626 tc->destructor = destructor;
630 increase the reference count on a piece of memory.
632 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
634 if (unlikely(!talloc_reference(null_context, ptr))) {
641 helper for talloc_reference()
643 this is referenced by a function pointer and should not be inline
645 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
647 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
648 _TLIST_REMOVE(ptr_tc->refs, handle);
653 more efficient way to add a name to a pointer - the name must point to a
656 static inline void _talloc_set_name_const(const void *ptr, const char *name)
658 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
663 internal talloc_named_const()
665 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
669 ptr = __talloc(context, size);
670 if (unlikely(ptr == NULL)) {
674 _talloc_set_name_const(ptr, name);
680 make a secondary reference to a pointer, hanging off the given context.
681 the pointer remains valid until both the original caller and this given
684 the major use for this is when two different structures need to reference the
685 same underlying data, and you want to be able to free the two instances separately,
688 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
690 struct talloc_chunk *tc;
691 struct talloc_reference_handle *handle;
692 if (unlikely(ptr == NULL)) return NULL;
694 tc = talloc_chunk_from_ptr(ptr);
695 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
696 sizeof(struct talloc_reference_handle),
697 TALLOC_MAGIC_REFERENCE);
698 if (unlikely(handle == NULL)) return NULL;
700 /* note that we hang the destructor off the handle, not the
701 main context as that allows the caller to still setup their
702 own destructor on the context if they want to */
703 talloc_set_destructor(handle, talloc_reference_destructor);
704 handle->ptr = discard_const_p(void, ptr);
705 handle->location = location;
706 _TLIST_ADD(tc->refs, handle);
710 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
712 static inline void _talloc_free_poolmem(struct talloc_chunk *tc,
713 const char *location)
715 struct talloc_chunk *pool;
717 unsigned int *pool_object_count;
719 pool = (struct talloc_chunk *)tc->pool;
720 next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
722 tc->flags |= TALLOC_FLAG_FREE;
724 /* we mark the freed memory with where we called the free
725 * from. This means on a double free error we can report where
726 * the first free came from
730 TC_INVALIDATE_FULL_CHUNK(tc);
732 pool_object_count = talloc_pool_objectcount(pool);
734 if (unlikely(*pool_object_count == 0)) {
735 talloc_abort("Pool object count zero!");
739 *pool_object_count -= 1;
741 if (unlikely(*pool_object_count == 1 && !(pool->flags & TALLOC_FLAG_FREE))) {
743 * if there is just one object left in the pool
744 * and pool->flags does not have TALLOC_FLAG_FREE,
745 * it means this is the pool itself and
746 * the rest is available for new objects
749 pool->pool = TC_POOL_FIRST_CHUNK(pool);
750 TC_INVALIDATE_POOL(pool);
751 } else if (unlikely(*pool_object_count == 0)) {
753 * we mark the freed memory with where we called the free
754 * from. This means on a double free error we can report where
755 * the first free came from
757 pool->name = location;
759 TC_INVALIDATE_FULL_CHUNK(pool);
761 } else if (pool->pool == next_tc) {
763 * if pool->pool still points to end of
764 * 'tc' (which is stored in the 'next_tc' variable),
765 * we can reclaim the memory of 'tc'.
771 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
773 const char *location);
776 internal talloc_free call
778 static inline int _talloc_free_internal(void *ptr, const char *location)
780 struct talloc_chunk *tc;
782 if (unlikely(ptr == NULL)) {
786 /* possibly initialised the talloc fill value */
787 if (unlikely(!talloc_fill.initialised)) {
788 const char *fill = getenv(TALLOC_FILL_ENV);
790 talloc_fill.enabled = true;
791 talloc_fill.fill_value = strtoul(fill, NULL, 0);
793 talloc_fill.initialised = true;
796 tc = talloc_chunk_from_ptr(ptr);
798 if (unlikely(tc->refs)) {
800 /* check if this is a reference from a child or
801 * grandchild back to it's parent or grandparent
803 * in that case we need to remove the reference and
804 * call another instance of talloc_free() on the current
807 is_child = talloc_is_parent(tc->refs, ptr);
808 _talloc_free_internal(tc->refs, location);
810 return _talloc_free_internal(ptr, location);
815 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
816 /* we have a free loop - stop looping */
820 if (unlikely(tc->destructor)) {
821 talloc_destructor_t d = tc->destructor;
822 if (d == (talloc_destructor_t)-1) {
825 tc->destructor = (talloc_destructor_t)-1;
830 tc->destructor = NULL;
834 _TLIST_REMOVE(tc->parent->child, tc);
835 if (tc->parent->child) {
836 tc->parent->child->parent = tc->parent;
839 if (tc->prev) tc->prev->next = tc->next;
840 if (tc->next) tc->next->prev = tc->prev;
843 tc->flags |= TALLOC_FLAG_LOOP;
845 _talloc_free_children_internal(tc, ptr, location);
847 tc->flags |= TALLOC_FLAG_FREE;
849 /* we mark the freed memory with where we called the free
850 * from. This means on a double free error we can report where
851 * the first free came from
855 if (tc->flags & TALLOC_FLAG_POOL) {
856 unsigned int *pool_object_count;
858 pool_object_count = talloc_pool_objectcount(tc);
860 if (unlikely(*pool_object_count == 0)) {
861 talloc_abort("Pool object count zero!");
865 *pool_object_count -= 1;
867 if (unlikely(*pool_object_count == 0)) {
868 TC_INVALIDATE_FULL_CHUNK(tc);
871 } else if (tc->flags & TALLOC_FLAG_POOLMEM) {
872 _talloc_free_poolmem(tc, location);
874 TC_INVALIDATE_FULL_CHUNK(tc);
881 move a lump of memory from one talloc context to another return the
882 ptr on success, or NULL if it could not be transferred.
883 passing NULL as ptr will always return NULL with no side effects.
885 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
887 struct talloc_chunk *tc, *new_tc;
889 if (unlikely(!ptr)) {
893 if (unlikely(new_ctx == NULL)) {
894 new_ctx = null_context;
897 tc = talloc_chunk_from_ptr(ptr);
899 if (unlikely(new_ctx == NULL)) {
901 _TLIST_REMOVE(tc->parent->child, tc);
902 if (tc->parent->child) {
903 tc->parent->child->parent = tc->parent;
906 if (tc->prev) tc->prev->next = tc->next;
907 if (tc->next) tc->next->prev = tc->prev;
910 tc->parent = tc->next = tc->prev = NULL;
911 return discard_const_p(void, ptr);
914 new_tc = talloc_chunk_from_ptr(new_ctx);
916 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
917 return discard_const_p(void, ptr);
921 _TLIST_REMOVE(tc->parent->child, tc);
922 if (tc->parent->child) {
923 tc->parent->child->parent = tc->parent;
926 if (tc->prev) tc->prev->next = tc->next;
927 if (tc->next) tc->next->prev = tc->prev;
931 if (new_tc->child) new_tc->child->parent = NULL;
932 _TLIST_ADD(new_tc->child, tc);
934 return discard_const_p(void, ptr);
938 move a lump of memory from one talloc context to another return the
939 ptr on success, or NULL if it could not be transferred.
940 passing NULL as ptr will always return NULL with no side effects.
942 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
944 struct talloc_chunk *tc;
946 if (unlikely(ptr == NULL)) {
950 tc = talloc_chunk_from_ptr(ptr);
952 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
953 struct talloc_reference_handle *h;
955 talloc_log("WARNING: talloc_steal with references at %s\n",
958 for (h=tc->refs; h; h=h->next) {
959 talloc_log("\treference at %s\n",
965 /* this test is probably too expensive to have on in the
966 normal build, but it useful for debugging */
967 if (talloc_is_parent(new_ctx, ptr)) {
968 talloc_log("WARNING: stealing into talloc child at %s\n", location);
972 return _talloc_steal_internal(new_ctx, ptr);
976 this is like a talloc_steal(), but you must supply the old
977 parent. This resolves the ambiguity in a talloc_steal() which is
978 called on a context that has more than one parent (via references)
980 The old parent can be either a reference or a parent
982 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
984 struct talloc_chunk *tc;
985 struct talloc_reference_handle *h;
987 if (unlikely(ptr == NULL)) {
991 if (old_parent == talloc_parent(ptr)) {
992 return _talloc_steal_internal(new_parent, ptr);
995 tc = talloc_chunk_from_ptr(ptr);
996 for (h=tc->refs;h;h=h->next) {
997 if (talloc_parent(h) == old_parent) {
998 if (_talloc_steal_internal(new_parent, h) != h) {
1001 return discard_const_p(void, ptr);
1005 /* it wasn't a parent */
1010 remove a secondary reference to a pointer. This undo's what
1011 talloc_reference() has done. The context and pointer arguments
1012 must match those given to a talloc_reference()
1014 static inline int talloc_unreference(const void *context, const void *ptr)
1016 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1017 struct talloc_reference_handle *h;
1019 if (unlikely(context == NULL)) {
1020 context = null_context;
1023 for (h=tc->refs;h;h=h->next) {
1024 struct talloc_chunk *p = talloc_parent_chunk(h);
1026 if (context == NULL) break;
1027 } else if (TC_PTR_FROM_CHUNK(p) == context) {
1035 return _talloc_free_internal(h, __location__);
1039 remove a specific parent context from a pointer. This is a more
1040 controlled varient of talloc_free()
1042 _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
1044 struct talloc_chunk *tc_p, *new_p;
1051 if (context == NULL) {
1052 context = null_context;
1055 if (talloc_unreference(context, ptr) == 0) {
1059 if (context == NULL) {
1060 if (talloc_parent_chunk(ptr) != NULL) {
1064 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
1069 tc_p = talloc_chunk_from_ptr(ptr);
1071 if (tc_p->refs == NULL) {
1072 return _talloc_free_internal(ptr, __location__);
1075 new_p = talloc_parent_chunk(tc_p->refs);
1077 new_parent = TC_PTR_FROM_CHUNK(new_p);
1082 if (talloc_unreference(new_parent, ptr) != 0) {
1086 _talloc_steal_internal(new_parent, ptr);
1092 add a name to an existing pointer - va_list version
1094 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1096 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
1098 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1099 tc->name = talloc_vasprintf(ptr, fmt, ap);
1100 if (likely(tc->name)) {
1101 _talloc_set_name_const(tc->name, ".name");
1107 add a name to an existing pointer
1109 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
1114 name = talloc_set_name_v(ptr, fmt, ap);
1121 create a named talloc pointer. Any talloc pointer can be named, and
1122 talloc_named() operates just like talloc() except that it allows you
1123 to name the pointer.
1125 _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
1131 ptr = __talloc(context, size);
1132 if (unlikely(ptr == NULL)) return NULL;
1135 name = talloc_set_name_v(ptr, fmt, ap);
1138 if (unlikely(name == NULL)) {
1139 _talloc_free_internal(ptr, __location__);
1147 return the name of a talloc ptr, or "UNNAMED"
1149 _PUBLIC_ const char *talloc_get_name(const void *ptr)
1151 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1152 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
1153 return ".reference";
1155 if (likely(tc->name)) {
1163 check if a pointer has the given name. If it does, return the pointer,
1164 otherwise return NULL
1166 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
1169 if (unlikely(ptr == NULL)) return NULL;
1170 pname = talloc_get_name(ptr);
1171 if (likely(pname == name || strcmp(pname, name) == 0)) {
1172 return discard_const_p(void, ptr);
1177 static void talloc_abort_type_missmatch(const char *location,
1179 const char *expected)
1183 reason = talloc_asprintf(NULL,
1184 "%s: Type mismatch: name[%s] expected[%s]",
1189 reason = "Type mismatch";
1192 talloc_abort(reason);
1195 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
1199 if (unlikely(ptr == NULL)) {
1200 talloc_abort_type_missmatch(location, NULL, name);
1204 pname = talloc_get_name(ptr);
1205 if (likely(pname == name || strcmp(pname, name) == 0)) {
1206 return discard_const_p(void, ptr);
1209 talloc_abort_type_missmatch(location, pname, name);
1214 this is for compatibility with older versions of talloc
1216 _PUBLIC_ void *talloc_init(const char *fmt, ...)
1222 ptr = __talloc(NULL, 0);
1223 if (unlikely(ptr == NULL)) return NULL;
1226 name = talloc_set_name_v(ptr, fmt, ap);
1229 if (unlikely(name == NULL)) {
1230 _talloc_free_internal(ptr, __location__);
1237 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
1239 const char *location)
1242 /* we need to work out who will own an abandoned child
1243 if it cannot be freed. In priority order, the first
1244 choice is owner of any remaining reference to this
1245 pointer, the second choice is our parent, and the
1246 final choice is the null context. */
1247 void *child = TC_PTR_FROM_CHUNK(tc->child);
1248 const void *new_parent = null_context;
1249 struct talloc_chunk *old_parent = NULL;
1250 if (unlikely(tc->child->refs)) {
1251 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1252 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1254 /* finding the parent here is potentially quite
1255 expensive, but the alternative, which is to change
1256 talloc to always have a valid tc->parent pointer,
1257 makes realloc more expensive where there are a
1258 large number of children.
1260 The reason we need the parent pointer here is that
1261 if _talloc_free_internal() fails due to references
1262 or a failing destructor we need to re-parent, but
1263 the free call can invalidate the prev pointer.
1265 if (new_parent == null_context && (tc->child->refs || tc->child->destructor)) {
1266 old_parent = talloc_parent_chunk(ptr);
1268 if (unlikely(_talloc_free_internal(child, location) == -1)) {
1269 if (new_parent == null_context) {
1270 struct talloc_chunk *p = old_parent;
1271 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1273 _talloc_steal_internal(new_parent, child);
1279 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1280 should probably not be used in new code. It's in here to keep the talloc
1281 code consistent across Samba 3 and 4.
1283 _PUBLIC_ void talloc_free_children(void *ptr)
1285 struct talloc_chunk *tc_name = NULL;
1286 struct talloc_chunk *tc;
1288 if (unlikely(ptr == NULL)) {
1292 tc = talloc_chunk_from_ptr(ptr);
1294 /* we do not want to free the context name if it is a child .. */
1295 if (likely(tc->child)) {
1296 for (tc_name = tc->child; tc_name; tc_name = tc_name->next) {
1297 if (tc->name == TC_PTR_FROM_CHUNK(tc_name)) break;
1300 _TLIST_REMOVE(tc->child, tc_name);
1302 tc->child->parent = tc;
1307 _talloc_free_children_internal(tc, ptr, __location__);
1309 /* .. so we put it back after all other children have been freed */
1312 tc->child->parent = NULL;
1314 tc_name->parent = tc;
1315 _TLIST_ADD(tc->child, tc_name);
1320 Allocate a bit of memory as a child of an existing pointer
1322 _PUBLIC_ void *_talloc(const void *context, size_t size)
1324 return __talloc(context, size);
1328 externally callable talloc_set_name_const()
1330 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1332 _talloc_set_name_const(ptr, name);
1336 create a named talloc pointer. Any talloc pointer can be named, and
1337 talloc_named() operates just like talloc() except that it allows you
1338 to name the pointer.
1340 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1342 return _talloc_named_const(context, size, name);
1346 free a talloc pointer. This also frees all child pointers of this
1349 return 0 if the memory is actually freed, otherwise -1. The memory
1350 will not be freed if the ref_count is > 1 or the destructor (if
1351 any) returns non-zero
1353 _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1355 struct talloc_chunk *tc;
1357 if (unlikely(ptr == NULL)) {
1361 tc = talloc_chunk_from_ptr(ptr);
1363 if (unlikely(tc->refs != NULL)) {
1364 struct talloc_reference_handle *h;
1366 if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1367 /* in this case we do know which parent should
1368 get this pointer, as there is really only
1370 return talloc_unlink(null_context, ptr);
1373 talloc_log("ERROR: talloc_free with references at %s\n",
1376 for (h=tc->refs; h; h=h->next) {
1377 talloc_log("\treference at %s\n",
1383 return _talloc_free_internal(ptr, location);
1389 A talloc version of realloc. The context argument is only used if
1392 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1394 struct talloc_chunk *tc;
1396 bool malloced = false;
1397 struct talloc_chunk *pool_tc = NULL;
1399 /* size zero is equivalent to free() */
1400 if (unlikely(size == 0)) {
1401 talloc_unlink(context, ptr);
1405 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1409 /* realloc(NULL) is equivalent to malloc() */
1411 return _talloc_named_const(context, size, name);
1414 tc = talloc_chunk_from_ptr(ptr);
1416 /* don't allow realloc on referenced pointers */
1417 if (unlikely(tc->refs)) {
1421 /* don't let anybody try to realloc a talloc_pool */
1422 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1426 /* don't let anybody try to realloc a talloc_pool */
1427 if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
1428 pool_tc = (struct talloc_chunk *)tc->pool;
1431 #if (ALWAYS_REALLOC == 0)
1432 /* don't shrink if we have less than 1k to gain */
1433 if (size < tc->size) {
1435 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1436 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1438 if (next_tc == pool_tc->pool) {
1439 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1442 } else if ((tc->size - size) < 1024) {
1444 * if we call TC_INVALIDATE_SHRINK_CHUNK() here
1445 * we would need to call TC_UNDEFINE_GROW_CHUNK()
1446 * after each realloc call, which slows down
1447 * testing a lot :-(.
1449 * That is why we only mark memory as undefined here.
1451 TC_UNDEFINE_SHRINK_CHUNK(tc, size);
1453 /* do not shrink if we have less than 1k to gain */
1457 } else if (tc->size == size) {
1459 * do not change the pointer if it is exactly
1466 /* by resetting magic we catch users of the old memory */
1467 tc->flags |= TALLOC_FLAG_FREE;
1471 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1472 *talloc_pool_objectcount(pool_tc) -= 1;
1474 if (new_ptr == NULL) {
1475 new_ptr = malloc(TC_HDR_SIZE+size);
1480 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1481 TC_INVALIDATE_FULL_CHUNK(tc);
1484 new_ptr = malloc(size + TC_HDR_SIZE);
1486 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1492 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1493 size_t old_chunk_size = TC_POOLMEM_CHUNK_SIZE(tc);
1494 size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
1495 size_t space_needed;
1497 unsigned int chunk_count = *talloc_pool_objectcount(pool_tc);
1499 if (!(pool_tc->flags & TALLOC_FLAG_FREE)) {
1503 if (chunk_count == 1) {
1505 * optimize for the case where 'tc' is the only
1506 * chunk in the pool.
1508 space_needed = new_chunk_size;
1509 space_left = pool_tc->size - TALLOC_POOL_HDR_SIZE;
1511 if (space_left >= space_needed) {
1512 size_t old_used = TC_HDR_SIZE + tc->size;
1513 size_t new_used = TC_HDR_SIZE + size;
1514 pool_tc->pool = TC_POOL_FIRST_CHUNK(pool_tc);
1515 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
1517 * we need to prepare the memmove into
1518 * the unaccessable area.
1521 size_t diff = PTR_DIFF(tc, pool_tc->pool);
1522 size_t flen = MIN(diff, old_used);
1523 char *fptr = (char *)pool_tc->pool;
1524 VALGRIND_MAKE_MEM_UNDEFINED(fptr, flen);
1527 memmove(pool_tc->pool, tc, old_used);
1528 new_ptr = pool_tc->pool;
1530 tc = (struct talloc_chunk *)new_ptr;
1531 TC_UNDEFINE_GROW_CHUNK(tc, size);
1534 * first we do not align the pool pointer
1535 * because we want to invalidate the padding
1538 pool_tc->pool = new_used + (char *)new_ptr;
1539 TC_INVALIDATE_POOL(pool_tc);
1541 /* now the aligned pointer */
1542 pool_tc->pool = new_chunk_size + (char *)new_ptr;
1549 if (new_chunk_size == old_chunk_size) {
1550 TC_UNDEFINE_GROW_CHUNK(tc, size);
1551 tc->flags &= ~TALLOC_FLAG_FREE;
1556 if (next_tc == pool_tc->pool) {
1558 * optimize for the case where 'tc' is the last
1559 * chunk in the pool.
1561 space_needed = new_chunk_size - old_chunk_size;
1562 space_left = TC_POOL_SPACE_LEFT(pool_tc);
1564 if (space_left >= space_needed) {
1565 TC_UNDEFINE_GROW_CHUNK(tc, size);
1566 tc->flags &= ~TALLOC_FLAG_FREE;
1568 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1573 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1575 if (new_ptr == NULL) {
1576 new_ptr = malloc(TC_HDR_SIZE+size);
1581 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1583 _talloc_free_poolmem(tc, __location__ "_talloc_realloc");
1587 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1591 if (unlikely(!new_ptr)) {
1592 tc->flags &= ~TALLOC_FLAG_FREE;
1596 tc = (struct talloc_chunk *)new_ptr;
1597 tc->flags &= ~TALLOC_FLAG_FREE;
1599 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1602 tc->parent->child = tc;
1605 tc->child->parent = tc;
1609 tc->prev->next = tc;
1612 tc->next->prev = tc;
1616 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1618 return TC_PTR_FROM_CHUNK(tc);
1622 a wrapper around talloc_steal() for situations where you are moving a pointer
1623 between two structures, and want the old pointer to be set to NULL
1625 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
1627 const void **pptr = discard_const_p(const void *,_pptr);
1628 void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1634 return the total size of a talloc pool (subtree)
1636 _PUBLIC_ size_t talloc_total_size(const void *ptr)
1639 struct talloc_chunk *c, *tc;
1648 tc = talloc_chunk_from_ptr(ptr);
1650 if (tc->flags & TALLOC_FLAG_LOOP) {
1654 tc->flags |= TALLOC_FLAG_LOOP;
1656 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1659 for (c=tc->child;c;c=c->next) {
1660 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1663 tc->flags &= ~TALLOC_FLAG_LOOP;
1669 return the total number of blocks in a talloc pool (subtree)
1671 _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
1674 struct talloc_chunk *c, *tc;
1683 tc = talloc_chunk_from_ptr(ptr);
1685 if (tc->flags & TALLOC_FLAG_LOOP) {
1689 tc->flags |= TALLOC_FLAG_LOOP;
1692 for (c=tc->child;c;c=c->next) {
1693 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1696 tc->flags &= ~TALLOC_FLAG_LOOP;
1702 return the number of external references to a pointer
1704 _PUBLIC_ size_t talloc_reference_count(const void *ptr)
1706 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1707 struct talloc_reference_handle *h;
1710 for (h=tc->refs;h;h=h->next) {
1717 report on memory usage by all children of a pointer, giving a full tree view
1719 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1720 void (*callback)(const void *ptr,
1721 int depth, int max_depth,
1723 void *private_data),
1726 struct talloc_chunk *c, *tc;
1731 if (ptr == NULL) return;
1733 tc = talloc_chunk_from_ptr(ptr);
1735 if (tc->flags & TALLOC_FLAG_LOOP) {
1739 callback(ptr, depth, max_depth, 0, private_data);
1741 if (max_depth >= 0 && depth >= max_depth) {
1745 tc->flags |= TALLOC_FLAG_LOOP;
1746 for (c=tc->child;c;c=c->next) {
1747 if (c->name == TALLOC_MAGIC_REFERENCE) {
1748 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1749 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1751 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1754 tc->flags &= ~TALLOC_FLAG_LOOP;
1757 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1759 const char *name = talloc_get_name(ptr);
1760 FILE *f = (FILE *)_f;
1763 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1768 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1769 (max_depth < 0 ? "full " :""), name,
1770 (unsigned long)talloc_total_size(ptr),
1771 (unsigned long)talloc_total_blocks(ptr));
1775 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1778 (unsigned long)talloc_total_size(ptr),
1779 (unsigned long)talloc_total_blocks(ptr),
1780 (int)talloc_reference_count(ptr), ptr);
1783 fprintf(f, "content: ");
1784 if (talloc_total_size(ptr)) {
1785 int tot = talloc_total_size(ptr);
1788 for (i = 0; i < tot; i++) {
1789 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1790 fprintf(f, "%c", ((char *)ptr)[i]);
1792 fprintf(f, "~%02x", ((char *)ptr)[i]);
1801 report on memory usage by all children of a pointer, giving a full tree view
1803 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1806 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1812 report on memory usage by all children of a pointer, giving a full tree view
1814 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
1816 talloc_report_depth_file(ptr, 0, -1, f);
1820 report on memory usage by all children of a pointer
1822 _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
1824 talloc_report_depth_file(ptr, 0, 1, f);
1828 report on any memory hanging off the null context
1830 static void talloc_report_null(void)
1832 if (talloc_total_size(null_context) != 0) {
1833 talloc_report(null_context, stderr);
1838 report on any memory hanging off the null context
1840 static void talloc_report_null_full(void)
1842 if (talloc_total_size(null_context) != 0) {
1843 talloc_report_full(null_context, stderr);
1848 enable tracking of the NULL context
1850 _PUBLIC_ void talloc_enable_null_tracking(void)
1852 if (null_context == NULL) {
1853 null_context = _talloc_named_const(NULL, 0, "null_context");
1854 if (autofree_context != NULL) {
1855 talloc_reparent(NULL, null_context, autofree_context);
1861 enable tracking of the NULL context, not moving the autofree context
1862 into the NULL context. This is needed for the talloc testsuite
1864 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
1866 if (null_context == NULL) {
1867 null_context = _talloc_named_const(NULL, 0, "null_context");
1872 disable tracking of the NULL context
1874 _PUBLIC_ void talloc_disable_null_tracking(void)
1876 if (null_context != NULL) {
1877 /* we have to move any children onto the real NULL
1879 struct talloc_chunk *tc, *tc2;
1880 tc = talloc_chunk_from_ptr(null_context);
1881 for (tc2 = tc->child; tc2; tc2=tc2->next) {
1882 if (tc2->parent == tc) tc2->parent = NULL;
1883 if (tc2->prev == tc) tc2->prev = NULL;
1885 for (tc2 = tc->next; tc2; tc2=tc2->next) {
1886 if (tc2->parent == tc) tc2->parent = NULL;
1887 if (tc2->prev == tc) tc2->prev = NULL;
1892 talloc_free(null_context);
1893 null_context = NULL;
1897 enable leak reporting on exit
1899 _PUBLIC_ void talloc_enable_leak_report(void)
1901 talloc_enable_null_tracking();
1902 atexit(talloc_report_null);
1906 enable full leak reporting on exit
1908 _PUBLIC_ void talloc_enable_leak_report_full(void)
1910 talloc_enable_null_tracking();
1911 atexit(talloc_report_null_full);
1915 talloc and zero memory.
1917 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
1919 void *p = _talloc_named_const(ctx, size, name);
1922 memset(p, '\0', size);
1929 memdup with a talloc.
1931 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1933 void *newp = _talloc_named_const(t, size, name);
1936 memcpy(newp, p, size);
1942 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1946 ret = (char *)__talloc(t, len + 1);
1947 if (unlikely(!ret)) return NULL;
1949 memcpy(ret, p, len);
1952 _talloc_set_name_const(ret, ret);
1957 strdup with a talloc
1959 _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
1961 if (unlikely(!p)) return NULL;
1962 return __talloc_strlendup(t, p, strlen(p));
1966 strndup with a talloc
1968 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
1970 if (unlikely(!p)) return NULL;
1971 return __talloc_strlendup(t, p, strnlen(p, n));
1974 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1975 const char *a, size_t alen)
1979 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1980 if (unlikely(!ret)) return NULL;
1982 /* append the string and the trailing \0 */
1983 memcpy(&ret[slen], a, alen);
1986 _talloc_set_name_const(ret, ret);
1991 * Appends at the end of the string.
1993 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
1996 return talloc_strdup(NULL, a);
2003 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
2007 * Appends at the end of the talloc'ed buffer,
2008 * not the end of the string.
2010 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
2015 return talloc_strdup(NULL, a);
2022 slen = talloc_get_size(s);
2023 if (likely(slen > 0)) {
2027 return __talloc_strlendup_append(s, slen, a, strlen(a));
2031 * Appends at the end of the string.
2033 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
2036 return talloc_strdup(NULL, a);
2043 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
2047 * Appends at the end of the talloc'ed buffer,
2048 * not the end of the string.
2050 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
2055 return talloc_strdup(NULL, a);
2062 slen = talloc_get_size(s);
2063 if (likely(slen > 0)) {
2067 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
2070 #ifndef HAVE_VA_COPY
2071 #ifdef HAVE___VA_COPY
2072 #define va_copy(dest, src) __va_copy(dest, src)
2074 #define va_copy(dest, src) (dest) = (src)
2078 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
2085 /* this call looks strange, but it makes it work on older solaris boxes */
2087 len = vsnprintf(&c, 1, fmt, ap2);
2089 if (unlikely(len < 0)) {
2093 ret = (char *)__talloc(t, len+1);
2094 if (unlikely(!ret)) return NULL;
2097 vsnprintf(ret, len+1, fmt, ap2);
2100 _talloc_set_name_const(ret, ret);
2106 Perform string formatting, and return a pointer to newly allocated
2107 memory holding the result, inside a memory pool.
2109 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
2115 ret = talloc_vasprintf(t, fmt, ap);
2120 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2121 const char *fmt, va_list ap)
2122 PRINTF_ATTRIBUTE(3,0);
2124 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2125 const char *fmt, va_list ap)
2132 alen = vsnprintf(&c, 1, fmt, ap2);
2136 /* Either the vsnprintf failed or the format resulted in
2137 * no characters being formatted. In the former case, we
2138 * ought to return NULL, in the latter we ought to return
2139 * the original string. Most current callers of this
2140 * function expect it to never return NULL.
2145 s = talloc_realloc(NULL, s, char, slen + alen + 1);
2146 if (!s) return NULL;
2149 vsnprintf(s + slen, alen + 1, fmt, ap2);
2152 _talloc_set_name_const(s, s);
2157 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2158 * and return @p s, which may have moved. Good for gradually
2159 * accumulating output into a string buffer. Appends at the end
2162 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
2165 return talloc_vasprintf(NULL, fmt, ap);
2168 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
2172 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2173 * and return @p s, which may have moved. Always appends at the
2174 * end of the talloc'ed buffer, not the end of the string.
2176 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
2181 return talloc_vasprintf(NULL, fmt, ap);
2184 slen = talloc_get_size(s);
2185 if (likely(slen > 0)) {
2189 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
2193 Realloc @p s to append the formatted result of @p fmt and return @p
2194 s, which may have moved. Good for gradually accumulating output
2195 into a string buffer.
2197 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
2202 s = talloc_vasprintf_append(s, fmt, ap);
2208 Realloc @p s to append the formatted result of @p fmt and return @p
2209 s, which may have moved. Good for gradually accumulating output
2212 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
2217 s = talloc_vasprintf_append_buffer(s, fmt, ap);
2223 alloc an array, checking for integer overflow in the array size
2225 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2227 if (count >= MAX_TALLOC_SIZE/el_size) {
2230 return _talloc_named_const(ctx, el_size * count, name);
2234 alloc an zero array, checking for integer overflow in the array size
2236 _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2238 if (count >= MAX_TALLOC_SIZE/el_size) {
2241 return _talloc_zero(ctx, el_size * count, name);
2245 realloc an array, checking for integer overflow in the array size
2247 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
2249 if (count >= MAX_TALLOC_SIZE/el_size) {
2252 return _talloc_realloc(ctx, ptr, el_size * count, name);
2256 a function version of talloc_realloc(), so it can be passed as a function pointer
2257 to libraries that want a realloc function (a realloc function encapsulates
2258 all the basic capabilities of an allocation library, which is why this is useful)
2260 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
2262 return _talloc_realloc(context, ptr, size, NULL);
2266 static int talloc_autofree_destructor(void *ptr)
2268 autofree_context = NULL;
2272 static void talloc_autofree(void)
2274 talloc_free(autofree_context);
2278 return a context which will be auto-freed on exit
2279 this is useful for reducing the noise in leak reports
2281 _PUBLIC_ void *talloc_autofree_context(void)
2283 if (autofree_context == NULL) {
2284 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
2285 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
2286 atexit(talloc_autofree);
2288 return autofree_context;
2291 _PUBLIC_ size_t talloc_get_size(const void *context)
2293 struct talloc_chunk *tc;
2295 if (context == NULL) {
2296 context = null_context;
2298 if (context == NULL) {
2302 tc = talloc_chunk_from_ptr(context);
2308 find a parent of this context that has the given name, if any
2310 _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
2312 struct talloc_chunk *tc;
2314 if (context == NULL) {
2318 tc = talloc_chunk_from_ptr(context);
2320 if (tc->name && strcmp(tc->name, name) == 0) {
2321 return TC_PTR_FROM_CHUNK(tc);
2323 while (tc && tc->prev) tc = tc->prev;
2332 show the parentage of a context
2334 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
2336 struct talloc_chunk *tc;
2338 if (context == NULL) {
2339 fprintf(file, "talloc no parents for NULL\n");
2343 tc = talloc_chunk_from_ptr(context);
2344 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
2346 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
2347 while (tc && tc->prev) tc = tc->prev;
2356 return 1 if ptr is a parent of context
2358 static int _talloc_is_parent(const void *context, const void *ptr, int depth)
2360 struct talloc_chunk *tc;
2362 if (context == NULL) {
2366 tc = talloc_chunk_from_ptr(context);
2367 while (tc && depth > 0) {
2368 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2369 while (tc && tc->prev) tc = tc->prev;
2379 return 1 if ptr is a parent of context
2381 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2383 return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);