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/
40 #include <atalk/compat.h>
41 #include <atalk/util.h>
45 #define _PUBLIC_ extern
48 * pointer difference macro
50 #define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
53 #ifdef TALLOC_BUILD_VERSION_MAJOR
54 #if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)
55 #error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"
59 #ifdef TALLOC_BUILD_VERSION_MINOR
60 #if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)
61 #error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"
65 /* Special macros that are no-ops except when run under Valgrind on
66 * x86. They've moved a little bit from valgrind 1.0.4 to 1.9.4 */
67 #ifdef HAVE_VALGRIND_MEMCHECK_H
68 /* memcheck.h includes valgrind.h */
69 #include <valgrind/memcheck.h>
70 #elif defined(HAVE_VALGRIND_H)
74 /* use this to force every realloc to change the pointer, to stress test
75 code that might not cope */
76 #define ALWAYS_REALLOC 0
79 #define MAX_TALLOC_SIZE 0x10000000
80 #define TALLOC_MAGIC_BASE 0xe814ec70
81 #define TALLOC_MAGIC ( \
83 (TALLOC_VERSION_MAJOR << 12) + \
84 (TALLOC_VERSION_MINOR << 4) \
87 #define TALLOC_FLAG_FREE 0x01
88 #define TALLOC_FLAG_LOOP 0x02
89 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
90 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
91 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
93 /* by default we abort when given a bad pointer (such as when talloc_free() is called
94 on a pointer that came from malloc() */
96 #define TALLOC_ABORT(reason) abort()
99 #ifndef discard_const_p
100 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
101 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
103 # define discard_const_p(type, ptr) ((type *)(ptr))
107 /* these macros gain us a few percent of speed on gcc */
109 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
110 as its first argument */
112 #define likely(x) __builtin_expect(!!(x), 1)
115 #define unlikely(x) __builtin_expect(!!(x), 0)
119 #define likely(x) (x)
122 #define unlikely(x) (x)
126 /* this null_context is only used if talloc_enable_leak_report() or
127 talloc_enable_leak_report_full() is called, otherwise it remains
130 static void *null_context;
131 static void *autofree_context;
133 /* used to enable fill of memory on free, which can be useful for
134 * catching use after free errors when valgrind is too slow
142 #define TALLOC_FILL_ENV "TALLOC_FREE_FILL"
145 * do not wipe the header, to allow the
146 * double-free logic to still work
148 #define TC_INVALIDATE_FULL_FILL_CHUNK(_tc) do { \
149 if (unlikely(talloc_fill.enabled)) { \
150 size_t _flen = (_tc)->size; \
151 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
152 memset(_fptr, talloc_fill.fill_value, _flen); \
156 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
157 /* Mark the whole chunk as not accessable */
158 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { \
159 size_t _flen = TC_HDR_SIZE + (_tc)->size; \
160 char *_fptr = (char *)(_tc); \
161 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
164 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { } while (0)
167 #define TC_INVALIDATE_FULL_CHUNK(_tc) do { \
168 TC_INVALIDATE_FULL_FILL_CHUNK(_tc); \
169 TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc); \
172 #define TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
173 if (unlikely(talloc_fill.enabled)) { \
174 size_t _flen = (_tc)->size - (_new_size); \
175 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
176 _fptr += (_new_size); \
177 memset(_fptr, talloc_fill.fill_value, _flen); \
181 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
182 /* Mark the unused bytes not accessable */
183 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
184 size_t _flen = (_tc)->size - (_new_size); \
185 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
186 _fptr += (_new_size); \
187 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
190 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
193 #define TC_INVALIDATE_SHRINK_CHUNK(_tc, _new_size) do { \
194 TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size); \
195 TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
198 #define TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
199 if (unlikely(talloc_fill.enabled)) { \
200 size_t _flen = (_tc)->size - (_new_size); \
201 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
202 _fptr += (_new_size); \
203 memset(_fptr, talloc_fill.fill_value, _flen); \
207 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
208 /* Mark the unused bytes as undefined */
209 #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
210 size_t _flen = (_tc)->size - (_new_size); \
211 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
212 _fptr += (_new_size); \
213 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
216 #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
219 #define TC_UNDEFINE_SHRINK_CHUNK(_tc, _new_size) do { \
220 TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size); \
221 TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
224 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
225 /* Mark the new bytes as undefined */
226 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { \
227 size_t _old_used = TC_HDR_SIZE + (_tc)->size; \
228 size_t _new_used = TC_HDR_SIZE + (_new_size); \
229 size_t _flen = _new_used - _old_used; \
230 char *_fptr = _old_used + (char *)(_tc); \
231 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
234 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
237 #define TC_UNDEFINE_GROW_CHUNK(_tc, _new_size) do { \
238 TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
241 struct talloc_reference_handle {
242 struct talloc_reference_handle *next, *prev;
244 const char *location;
247 typedef int (*talloc_destructor_t)(void *);
249 struct talloc_chunk {
250 struct talloc_chunk *next, *prev;
251 struct talloc_chunk *parent, *child;
252 struct talloc_reference_handle *refs;
253 talloc_destructor_t destructor;
259 * "pool" has dual use:
261 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
262 * marks the end of the currently allocated area.
264 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
265 * is a pointer to the struct talloc_chunk of the pool that it was
266 * allocated from. This way children can quickly find the pool to chew
272 /* 16 byte alignment seems to keep everyone happy */
273 #define TC_ALIGN16(s) (((s)+15)&~15)
274 #define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
275 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
277 _PUBLIC_ int talloc_version_major(void)
279 return TALLOC_VERSION_MAJOR;
282 _PUBLIC_ int talloc_version_minor(void)
284 return TALLOC_VERSION_MINOR;
287 static void (*talloc_log_fn)(const char *message);
289 _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
291 talloc_log_fn = log_fn;
294 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
295 static void talloc_log(const char *fmt, ...)
300 if (!talloc_log_fn) {
305 message = talloc_vasprintf(NULL, fmt, ap);
308 talloc_log_fn(message);
309 talloc_free(message);
312 static void talloc_log_stderr(const char *message)
314 fprintf(stderr, "%s", message);
317 _PUBLIC_ void talloc_set_log_stderr(void)
319 talloc_set_log_fn(talloc_log_stderr);
322 static void (*talloc_abort_fn)(const char *reason);
324 _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
326 talloc_abort_fn = abort_fn;
329 static void talloc_abort(const char *reason)
331 talloc_log("%s\n", reason);
333 if (!talloc_abort_fn) {
334 TALLOC_ABORT(reason);
337 talloc_abort_fn(reason);
340 static void talloc_abort_magic(unsigned magic)
342 unsigned striped = magic - TALLOC_MAGIC_BASE;
343 unsigned major = (striped & 0xFFFFF000) >> 12;
344 unsigned minor = (striped & 0x00000FF0) >> 4;
345 talloc_log("Bad talloc magic[0x%08X/%u/%u] expected[0x%08X/%u/%u]\n",
347 TALLOC_MAGIC, TALLOC_VERSION_MAJOR, TALLOC_VERSION_MINOR);
348 talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
351 static void talloc_abort_access_after_free(void)
353 talloc_abort("Bad talloc magic value - access after free");
356 static void talloc_abort_unknown_value(void)
358 talloc_abort("Bad talloc magic value - unknown value");
361 /* panic if we get a bad magic value */
362 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
364 const char *pp = (const char *)ptr;
365 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
366 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
367 if ((tc->flags & (~0xFFF)) == TALLOC_MAGIC_BASE) {
368 talloc_abort_magic(tc->flags & (~0xF));
372 if (tc->flags & TALLOC_FLAG_FREE) {
373 talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
374 talloc_abort_access_after_free();
377 talloc_abort_unknown_value();
384 /* hook into the front of the list */
385 #define _TLIST_ADD(list, p) \
389 (p)->next = (p)->prev = NULL; \
391 (list)->prev = (p); \
392 (p)->next = (list); \
398 /* remove an element from a list - element doesn't have to be in list. */
399 #define _TLIST_REMOVE(list, p) \
401 if ((p) == (list)) { \
402 (list) = (p)->next; \
403 if (list) (list)->prev = NULL; \
405 if ((p)->prev) (p)->prev->next = (p)->next; \
406 if ((p)->next) (p)->next->prev = (p)->prev; \
408 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
413 return the parent chunk of a pointer
415 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
417 struct talloc_chunk *tc;
419 if (unlikely(ptr == NULL)) {
423 tc = talloc_chunk_from_ptr(ptr);
424 while (tc->prev) tc=tc->prev;
429 _PUBLIC_ void *talloc_parent(const void *ptr)
431 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
432 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
438 _PUBLIC_ const char *talloc_parent_name(const void *ptr)
440 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
441 return tc? tc->name : NULL;
445 A pool carries an in-pool object count count in the first 16 bytes.
446 bytes. This is done to support talloc_steal() to a parent outside of the
447 pool. The count includes the pool itself, so a talloc_free() on a pool will
448 only destroy the pool if the count has dropped to zero. A talloc_free() of a
449 pool member will reduce the count, and eventually also call free(3) on the
452 The object count is not put into "struct talloc_chunk" because it is only
453 relevant for talloc pools and the alignment to 16 bytes would increase the
454 memory footprint of each talloc chunk by those 16 bytes.
457 #define TALLOC_POOL_HDR_SIZE 16
459 #define TC_POOL_SPACE_LEFT(_pool_tc) \
460 PTR_DIFF(TC_HDR_SIZE + (_pool_tc)->size + (char *)(_pool_tc), \
463 #define TC_POOL_FIRST_CHUNK(_pool_tc) \
464 ((void *)(TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE + (char *)(_pool_tc)))
466 #define TC_POOLMEM_CHUNK_SIZE(_tc) \
467 TC_ALIGN16(TC_HDR_SIZE + (_tc)->size)
469 #define TC_POOLMEM_NEXT_CHUNK(_tc) \
470 ((void *)(TC_POOLMEM_CHUNK_SIZE(tc) + (char*)(_tc)))
472 /* Mark the whole remaining pool as not accessable */
473 #define TC_INVALIDATE_FILL_POOL(_pool_tc) do { \
474 if (unlikely(talloc_fill.enabled)) { \
475 size_t _flen = TC_POOL_SPACE_LEFT(_pool_tc); \
476 char *_fptr = (char *)(_pool_tc)->pool; \
477 memset(_fptr, talloc_fill.fill_value, _flen); \
481 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
482 /* Mark the whole remaining pool as not accessable */
483 #define TC_INVALIDATE_VALGRIND_POOL(_pool_tc) do { \
484 size_t _flen = TC_POOL_SPACE_LEFT(_pool_tc); \
485 char *_fptr = (char *)(_pool_tc)->pool; \
486 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
489 #define TC_INVALIDATE_VALGRIND_POOL(_pool_tc) do { } while (0)
492 #define TC_INVALIDATE_POOL(_pool_tc) do { \
493 TC_INVALIDATE_FILL_POOL(_pool_tc); \
494 TC_INVALIDATE_VALGRIND_POOL(_pool_tc); \
497 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
499 return (unsigned int *)((char *)tc + TC_HDR_SIZE);
506 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
509 struct talloc_chunk *pool_ctx = NULL;
511 struct talloc_chunk *result;
514 if (parent == NULL) {
518 if (parent->flags & TALLOC_FLAG_POOL) {
521 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
522 pool_ctx = (struct talloc_chunk *)parent->pool;
525 if (pool_ctx == NULL) {
529 space_left = TC_POOL_SPACE_LEFT(pool_ctx);
532 * Align size to 16 bytes
534 chunk_size = TC_ALIGN16(size);
536 if (space_left < chunk_size) {
540 result = (struct talloc_chunk *)pool_ctx->pool;
542 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
543 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
546 pool_ctx->pool = (void *)((char *)result + chunk_size);
548 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
549 result->pool = pool_ctx;
551 *talloc_pool_objectcount(pool_ctx) += 1;
557 Allocate a bit of memory as a child of an existing pointer
559 static inline void *__talloc(const void *context, size_t size)
561 struct talloc_chunk *tc = NULL;
563 if (unlikely(context == NULL)) {
564 context = null_context;
567 if (unlikely(size >= MAX_TALLOC_SIZE)) {
571 if (context != NULL) {
572 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
577 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
578 if (unlikely(tc == NULL)) return NULL;
579 tc->flags = TALLOC_MAGIC;
584 tc->destructor = NULL;
589 if (likely(context)) {
590 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
593 parent->child->parent = NULL;
594 tc->next = parent->child;
603 tc->next = tc->prev = tc->parent = NULL;
606 return TC_PTR_FROM_CHUNK(tc);
610 * Create a talloc pool
613 _PUBLIC_ void *talloc_pool(const void *context, size_t size)
615 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
616 struct talloc_chunk *tc;
618 if (unlikely(result == NULL)) {
622 tc = talloc_chunk_from_ptr(result);
624 tc->flags |= TALLOC_FLAG_POOL;
625 tc->pool = TC_POOL_FIRST_CHUNK(tc);
627 *talloc_pool_objectcount(tc) = 1;
629 TC_INVALIDATE_POOL(tc);
635 setup a destructor to be called on free of a pointer
636 the destructor should return 0 on success, or -1 on failure.
637 if the destructor fails then the free is failed, and the memory can
638 be continued to be used
640 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
642 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
643 tc->destructor = destructor;
647 increase the reference count on a piece of memory.
649 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
651 if (unlikely(!talloc_reference(null_context, ptr))) {
658 helper for talloc_reference()
660 this is referenced by a function pointer and should not be inline
662 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
664 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
665 _TLIST_REMOVE(ptr_tc->refs, handle);
670 more efficient way to add a name to a pointer - the name must point to a
673 static inline void _talloc_set_name_const(const void *ptr, const char *name)
675 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
680 internal talloc_named_const()
682 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
686 ptr = __talloc(context, size);
687 if (unlikely(ptr == NULL)) {
691 _talloc_set_name_const(ptr, name);
697 make a secondary reference to a pointer, hanging off the given context.
698 the pointer remains valid until both the original caller and this given
701 the major use for this is when two different structures need to reference the
702 same underlying data, and you want to be able to free the two instances separately,
705 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
707 struct talloc_chunk *tc;
708 struct talloc_reference_handle *handle;
709 if (unlikely(ptr == NULL)) return NULL;
711 tc = talloc_chunk_from_ptr(ptr);
712 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
713 sizeof(struct talloc_reference_handle),
714 TALLOC_MAGIC_REFERENCE);
715 if (unlikely(handle == NULL)) return NULL;
717 /* note that we hang the destructor off the handle, not the
718 main context as that allows the caller to still setup their
719 own destructor on the context if they want to */
720 talloc_set_destructor(handle, talloc_reference_destructor);
721 handle->ptr = discard_const_p(void, ptr);
722 handle->location = location;
723 _TLIST_ADD(tc->refs, handle);
727 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
729 static inline void _talloc_free_poolmem(struct talloc_chunk *tc,
730 const char *location)
732 struct talloc_chunk *pool;
734 unsigned int *pool_object_count;
736 pool = (struct talloc_chunk *)tc->pool;
737 next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
739 tc->flags |= TALLOC_FLAG_FREE;
741 /* we mark the freed memory with where we called the free
742 * from. This means on a double free error we can report where
743 * the first free came from
747 TC_INVALIDATE_FULL_CHUNK(tc);
749 pool_object_count = talloc_pool_objectcount(pool);
751 if (unlikely(*pool_object_count == 0)) {
752 talloc_abort("Pool object count zero!");
756 *pool_object_count -= 1;
758 if (unlikely(*pool_object_count == 1 && !(pool->flags & TALLOC_FLAG_FREE))) {
760 * if there is just one object left in the pool
761 * and pool->flags does not have TALLOC_FLAG_FREE,
762 * it means this is the pool itself and
763 * the rest is available for new objects
766 pool->pool = TC_POOL_FIRST_CHUNK(pool);
767 TC_INVALIDATE_POOL(pool);
768 } else if (unlikely(*pool_object_count == 0)) {
770 * we mark the freed memory with where we called the free
771 * from. This means on a double free error we can report where
772 * the first free came from
774 pool->name = location;
776 TC_INVALIDATE_FULL_CHUNK(pool);
778 } else if (pool->pool == next_tc) {
780 * if pool->pool still points to end of
781 * 'tc' (which is stored in the 'next_tc' variable),
782 * we can reclaim the memory of 'tc'.
788 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
790 const char *location);
793 internal talloc_free call
795 static inline int _talloc_free_internal(void *ptr, const char *location)
797 struct talloc_chunk *tc;
799 if (unlikely(ptr == NULL)) {
803 /* possibly initialised the talloc fill value */
804 if (unlikely(!talloc_fill.initialised)) {
805 const char *fill = getenv(TALLOC_FILL_ENV);
807 talloc_fill.enabled = true;
808 talloc_fill.fill_value = strtoul(fill, NULL, 0);
810 talloc_fill.initialised = true;
813 tc = talloc_chunk_from_ptr(ptr);
815 if (unlikely(tc->refs)) {
817 /* check if this is a reference from a child or
818 * grandchild back to it's parent or grandparent
820 * in that case we need to remove the reference and
821 * call another instance of talloc_free() on the current
824 is_child = talloc_is_parent(tc->refs, ptr);
825 _talloc_free_internal(tc->refs, location);
827 return _talloc_free_internal(ptr, location);
832 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
833 /* we have a free loop - stop looping */
837 if (unlikely(tc->destructor)) {
838 talloc_destructor_t d = tc->destructor;
839 if (d == (talloc_destructor_t)-1) {
842 tc->destructor = (talloc_destructor_t)-1;
847 tc->destructor = NULL;
851 _TLIST_REMOVE(tc->parent->child, tc);
852 if (tc->parent->child) {
853 tc->parent->child->parent = tc->parent;
856 if (tc->prev) tc->prev->next = tc->next;
857 if (tc->next) tc->next->prev = tc->prev;
858 tc->prev = tc->next = NULL;
861 tc->flags |= TALLOC_FLAG_LOOP;
863 _talloc_free_children_internal(tc, ptr, location);
865 tc->flags |= TALLOC_FLAG_FREE;
867 /* we mark the freed memory with where we called the free
868 * from. This means on a double free error we can report where
869 * the first free came from
873 if (tc->flags & TALLOC_FLAG_POOL) {
874 unsigned int *pool_object_count;
876 pool_object_count = talloc_pool_objectcount(tc);
878 if (unlikely(*pool_object_count == 0)) {
879 talloc_abort("Pool object count zero!");
883 *pool_object_count -= 1;
885 if (unlikely(*pool_object_count == 0)) {
886 TC_INVALIDATE_FULL_CHUNK(tc);
889 } else if (tc->flags & TALLOC_FLAG_POOLMEM) {
890 _talloc_free_poolmem(tc, location);
892 TC_INVALIDATE_FULL_CHUNK(tc);
899 move a lump of memory from one talloc context to another return the
900 ptr on success, or NULL if it could not be transferred.
901 passing NULL as ptr will always return NULL with no side effects.
903 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
905 struct talloc_chunk *tc, *new_tc;
907 if (unlikely(!ptr)) {
911 if (unlikely(new_ctx == NULL)) {
912 new_ctx = null_context;
915 tc = talloc_chunk_from_ptr(ptr);
917 if (unlikely(new_ctx == NULL)) {
919 _TLIST_REMOVE(tc->parent->child, tc);
920 if (tc->parent->child) {
921 tc->parent->child->parent = tc->parent;
924 if (tc->prev) tc->prev->next = tc->next;
925 if (tc->next) tc->next->prev = tc->prev;
928 tc->parent = tc->next = tc->prev = NULL;
929 return discard_const_p(void, ptr);
932 new_tc = talloc_chunk_from_ptr(new_ctx);
934 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
935 return discard_const_p(void, ptr);
939 _TLIST_REMOVE(tc->parent->child, tc);
940 if (tc->parent->child) {
941 tc->parent->child->parent = tc->parent;
944 if (tc->prev) tc->prev->next = tc->next;
945 if (tc->next) tc->next->prev = tc->prev;
946 tc->prev = tc->next = NULL;
950 if (new_tc->child) new_tc->child->parent = NULL;
951 _TLIST_ADD(new_tc->child, tc);
953 return discard_const_p(void, ptr);
957 move a lump of memory from one talloc context to another return the
958 ptr on success, or NULL if it could not be transferred.
959 passing NULL as ptr will always return NULL with no side effects.
961 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
963 struct talloc_chunk *tc;
965 if (unlikely(ptr == NULL)) {
969 tc = talloc_chunk_from_ptr(ptr);
971 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
972 struct talloc_reference_handle *h;
974 talloc_log("WARNING: talloc_steal with references at %s\n",
977 for (h=tc->refs; h; h=h->next) {
978 talloc_log("\treference at %s\n",
984 /* this test is probably too expensive to have on in the
985 normal build, but it useful for debugging */
986 if (talloc_is_parent(new_ctx, ptr)) {
987 talloc_log("WARNING: stealing into talloc child at %s\n", location);
991 return _talloc_steal_internal(new_ctx, ptr);
995 this is like a talloc_steal(), but you must supply the old
996 parent. This resolves the ambiguity in a talloc_steal() which is
997 called on a context that has more than one parent (via references)
999 The old parent can be either a reference or a parent
1001 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
1003 struct talloc_chunk *tc;
1004 struct talloc_reference_handle *h;
1006 if (unlikely(ptr == NULL)) {
1010 if (old_parent == talloc_parent(ptr)) {
1011 return _talloc_steal_internal(new_parent, ptr);
1014 tc = talloc_chunk_from_ptr(ptr);
1015 for (h=tc->refs;h;h=h->next) {
1016 if (talloc_parent(h) == old_parent) {
1017 if (_talloc_steal_internal(new_parent, h) != h) {
1020 return discard_const_p(void, ptr);
1024 /* it wasn't a parent */
1029 remove a secondary reference to a pointer. This undo's what
1030 talloc_reference() has done. The context and pointer arguments
1031 must match those given to a talloc_reference()
1033 static inline int talloc_unreference(const void *context, const void *ptr)
1035 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1036 struct talloc_reference_handle *h;
1038 if (unlikely(context == NULL)) {
1039 context = null_context;
1042 for (h=tc->refs;h;h=h->next) {
1043 struct talloc_chunk *p = talloc_parent_chunk(h);
1045 if (context == NULL) break;
1046 } else if (TC_PTR_FROM_CHUNK(p) == context) {
1054 return _talloc_free_internal(h, __location__);
1058 remove a specific parent context from a pointer. This is a more
1059 controlled varient of talloc_free()
1061 _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
1063 struct talloc_chunk *tc_p, *new_p;
1070 if (context == NULL) {
1071 context = null_context;
1074 if (talloc_unreference(context, ptr) == 0) {
1078 if (context == NULL) {
1079 if (talloc_parent_chunk(ptr) != NULL) {
1083 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
1088 tc_p = talloc_chunk_from_ptr(ptr);
1090 if (tc_p->refs == NULL) {
1091 return _talloc_free_internal(ptr, __location__);
1094 new_p = talloc_parent_chunk(tc_p->refs);
1096 new_parent = TC_PTR_FROM_CHUNK(new_p);
1101 if (talloc_unreference(new_parent, ptr) != 0) {
1105 _talloc_steal_internal(new_parent, ptr);
1111 add a name to an existing pointer - va_list version
1113 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1115 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
1117 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1118 tc->name = talloc_vasprintf(ptr, fmt, ap);
1119 if (likely(tc->name)) {
1120 _talloc_set_name_const(tc->name, ".name");
1126 add a name to an existing pointer
1128 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
1133 name = talloc_set_name_v(ptr, fmt, ap);
1140 create a named talloc pointer. Any talloc pointer can be named, and
1141 talloc_named() operates just like talloc() except that it allows you
1142 to name the pointer.
1144 _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
1150 ptr = __talloc(context, size);
1151 if (unlikely(ptr == NULL)) return NULL;
1154 name = talloc_set_name_v(ptr, fmt, ap);
1157 if (unlikely(name == NULL)) {
1158 _talloc_free_internal(ptr, __location__);
1166 return the name of a talloc ptr, or "UNNAMED"
1168 _PUBLIC_ const char *talloc_get_name(const void *ptr)
1170 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1171 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
1172 return ".reference";
1174 if (likely(tc->name)) {
1182 check if a pointer has the given name. If it does, return the pointer,
1183 otherwise return NULL
1185 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
1188 if (unlikely(ptr == NULL)) return NULL;
1189 pname = talloc_get_name(ptr);
1190 if (likely(pname == name || strcmp(pname, name) == 0)) {
1191 return discard_const_p(void, ptr);
1196 static void talloc_abort_type_missmatch(const char *location,
1198 const char *expected)
1202 reason = talloc_asprintf(NULL,
1203 "%s: Type mismatch: name[%s] expected[%s]",
1208 reason = "Type mismatch";
1211 talloc_abort(reason);
1214 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
1218 if (unlikely(ptr == NULL)) {
1219 talloc_abort_type_missmatch(location, NULL, name);
1223 pname = talloc_get_name(ptr);
1224 if (likely(pname == name || strcmp(pname, name) == 0)) {
1225 return discard_const_p(void, ptr);
1228 talloc_abort_type_missmatch(location, pname, name);
1233 this is for compatibility with older versions of talloc
1235 _PUBLIC_ void *talloc_init(const char *fmt, ...)
1241 ptr = __talloc(NULL, 0);
1242 if (unlikely(ptr == NULL)) return NULL;
1245 name = talloc_set_name_v(ptr, fmt, ap);
1248 if (unlikely(name == NULL)) {
1249 _talloc_free_internal(ptr, __location__);
1256 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
1258 const char *location)
1261 /* we need to work out who will own an abandoned child
1262 if it cannot be freed. In priority order, the first
1263 choice is owner of any remaining reference to this
1264 pointer, the second choice is our parent, and the
1265 final choice is the null context. */
1266 void *child = TC_PTR_FROM_CHUNK(tc->child);
1267 const void *new_parent = null_context;
1268 if (unlikely(tc->child->refs)) {
1269 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1270 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1272 if (unlikely(_talloc_free_internal(child, location) == -1)) {
1273 if (new_parent == null_context) {
1274 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1275 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1277 _talloc_steal_internal(new_parent, child);
1283 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1284 should probably not be used in new code. It's in here to keep the talloc
1285 code consistent across Samba 3 and 4.
1287 _PUBLIC_ void talloc_free_children(void *ptr)
1289 struct talloc_chunk *tc_name = NULL;
1290 struct talloc_chunk *tc;
1292 if (unlikely(ptr == NULL)) {
1296 tc = talloc_chunk_from_ptr(ptr);
1298 /* we do not want to free the context name if it is a child .. */
1299 if (likely(tc->child)) {
1300 for (tc_name = tc->child; tc_name; tc_name = tc_name->next) {
1301 if (tc->name == TC_PTR_FROM_CHUNK(tc_name)) break;
1304 _TLIST_REMOVE(tc->child, tc_name);
1306 tc->child->parent = tc;
1311 _talloc_free_children_internal(tc, ptr, __location__);
1313 /* .. so we put it back after all other children have been freed */
1316 tc->child->parent = NULL;
1318 tc_name->parent = tc;
1319 _TLIST_ADD(tc->child, tc_name);
1324 Allocate a bit of memory as a child of an existing pointer
1326 _PUBLIC_ void *_talloc(const void *context, size_t size)
1328 return __talloc(context, size);
1332 externally callable talloc_set_name_const()
1334 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1336 _talloc_set_name_const(ptr, name);
1340 create a named talloc pointer. Any talloc pointer can be named, and
1341 talloc_named() operates just like talloc() except that it allows you
1342 to name the pointer.
1344 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1346 return _talloc_named_const(context, size, name);
1350 free a talloc pointer. This also frees all child pointers of this
1353 return 0 if the memory is actually freed, otherwise -1. The memory
1354 will not be freed if the ref_count is > 1 or the destructor (if
1355 any) returns non-zero
1357 _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1359 struct talloc_chunk *tc;
1361 if (unlikely(ptr == NULL)) {
1365 tc = talloc_chunk_from_ptr(ptr);
1367 if (unlikely(tc->refs != NULL)) {
1368 struct talloc_reference_handle *h;
1370 if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1371 /* in this case we do know which parent should
1372 get this pointer, as there is really only
1374 return talloc_unlink(null_context, ptr);
1377 talloc_log("ERROR: talloc_free with references at %s\n",
1380 for (h=tc->refs; h; h=h->next) {
1381 talloc_log("\treference at %s\n",
1387 return _talloc_free_internal(ptr, location);
1393 A talloc version of realloc. The context argument is only used if
1396 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1398 struct talloc_chunk *tc;
1400 bool malloced = false;
1401 struct talloc_chunk *pool_tc = NULL;
1403 /* size zero is equivalent to free() */
1404 if (unlikely(size == 0)) {
1405 talloc_unlink(context, ptr);
1409 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1413 /* realloc(NULL) is equivalent to malloc() */
1415 return _talloc_named_const(context, size, name);
1418 tc = talloc_chunk_from_ptr(ptr);
1420 /* don't allow realloc on referenced pointers */
1421 if (unlikely(tc->refs)) {
1425 /* don't let anybody try to realloc a talloc_pool */
1426 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1430 /* don't let anybody try to realloc a talloc_pool */
1431 if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
1432 pool_tc = (struct talloc_chunk *)tc->pool;
1435 #if (ALWAYS_REALLOC == 0)
1436 /* don't shrink if we have less than 1k to gain */
1437 if (size < tc->size) {
1439 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1440 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1442 if (next_tc == pool_tc->pool) {
1443 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1446 } else if ((tc->size - size) < 1024) {
1448 * if we call TC_INVALIDATE_SHRINK_CHUNK() here
1449 * we would need to call TC_UNDEFINE_GROW_CHUNK()
1450 * after each realloc call, which slows down
1451 * testing a lot :-(.
1453 * That is why we only mark memory as undefined here.
1455 TC_UNDEFINE_SHRINK_CHUNK(tc, size);
1457 /* do not shrink if we have less than 1k to gain */
1461 } else if (tc->size == size) {
1463 * do not change the pointer if it is exactly
1470 /* by resetting magic we catch users of the old memory */
1471 tc->flags |= TALLOC_FLAG_FREE;
1475 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1476 *talloc_pool_objectcount(pool_tc) -= 1;
1478 if (new_ptr == NULL) {
1479 new_ptr = malloc(TC_HDR_SIZE+size);
1484 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1485 TC_INVALIDATE_FULL_CHUNK(tc);
1488 new_ptr = malloc(size + TC_HDR_SIZE);
1490 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1496 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1497 size_t old_chunk_size = TC_POOLMEM_CHUNK_SIZE(tc);
1498 size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
1499 size_t space_needed;
1501 unsigned int chunk_count = *talloc_pool_objectcount(pool_tc);
1503 if (!(pool_tc->flags & TALLOC_FLAG_FREE)) {
1507 if (chunk_count == 1) {
1509 * optimize for the case where 'tc' is the only
1510 * chunk in the pool.
1512 space_needed = new_chunk_size;
1513 space_left = pool_tc->size - TALLOC_POOL_HDR_SIZE;
1515 if (space_left >= space_needed) {
1516 size_t old_used = TC_HDR_SIZE + tc->size;
1517 size_t new_used = TC_HDR_SIZE + size;
1518 pool_tc->pool = TC_POOL_FIRST_CHUNK(pool_tc);
1519 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
1521 * we need to prepare the memmove into
1522 * the unaccessable area.
1525 size_t diff = PTR_DIFF(tc, pool_tc->pool);
1526 size_t flen = MIN(diff, old_used);
1527 char *fptr = (char *)pool_tc->pool;
1528 VALGRIND_MAKE_MEM_UNDEFINED(fptr, flen);
1531 memmove(pool_tc->pool, tc, old_used);
1532 new_ptr = pool_tc->pool;
1534 tc = (struct talloc_chunk *)new_ptr;
1535 TC_UNDEFINE_GROW_CHUNK(tc, size);
1538 * first we do not align the pool pointer
1539 * because we want to invalidate the padding
1542 pool_tc->pool = new_used + (char *)new_ptr;
1543 TC_INVALIDATE_POOL(pool_tc);
1545 /* now the aligned pointer */
1546 pool_tc->pool = new_chunk_size + (char *)new_ptr;
1553 if (new_chunk_size == old_chunk_size) {
1554 TC_UNDEFINE_GROW_CHUNK(tc, size);
1555 tc->flags &= ~TALLOC_FLAG_FREE;
1560 if (next_tc == pool_tc->pool) {
1562 * optimize for the case where 'tc' is the last
1563 * chunk in the pool.
1565 space_needed = new_chunk_size - old_chunk_size;
1566 space_left = TC_POOL_SPACE_LEFT(pool_tc);
1568 if (space_left >= space_needed) {
1569 TC_UNDEFINE_GROW_CHUNK(tc, size);
1570 tc->flags &= ~TALLOC_FLAG_FREE;
1572 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1577 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1579 if (new_ptr == NULL) {
1580 new_ptr = malloc(TC_HDR_SIZE+size);
1585 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1587 _talloc_free_poolmem(tc, __location__ "_talloc_realloc");
1591 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1595 if (unlikely(!new_ptr)) {
1596 tc->flags &= ~TALLOC_FLAG_FREE;
1600 tc = (struct talloc_chunk *)new_ptr;
1601 tc->flags &= ~TALLOC_FLAG_FREE;
1603 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1606 tc->parent->child = tc;
1609 tc->child->parent = tc;
1613 tc->prev->next = tc;
1616 tc->next->prev = tc;
1620 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1622 return TC_PTR_FROM_CHUNK(tc);
1626 a wrapper around talloc_steal() for situations where you are moving a pointer
1627 between two structures, and want the old pointer to be set to NULL
1629 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
1631 const void **pptr = discard_const_p(const void *,_pptr);
1632 void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1638 return the total size of a talloc pool (subtree)
1640 _PUBLIC_ size_t talloc_total_size(const void *ptr)
1643 struct talloc_chunk *c, *tc;
1652 tc = talloc_chunk_from_ptr(ptr);
1654 if (tc->flags & TALLOC_FLAG_LOOP) {
1658 tc->flags |= TALLOC_FLAG_LOOP;
1660 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1663 for (c=tc->child;c;c=c->next) {
1664 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1667 tc->flags &= ~TALLOC_FLAG_LOOP;
1673 return the total number of blocks in a talloc pool (subtree)
1675 _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
1678 struct talloc_chunk *c, *tc;
1687 tc = talloc_chunk_from_ptr(ptr);
1689 if (tc->flags & TALLOC_FLAG_LOOP) {
1693 tc->flags |= TALLOC_FLAG_LOOP;
1696 for (c=tc->child;c;c=c->next) {
1697 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1700 tc->flags &= ~TALLOC_FLAG_LOOP;
1706 return the number of external references to a pointer
1708 _PUBLIC_ size_t talloc_reference_count(const void *ptr)
1710 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1711 struct talloc_reference_handle *h;
1714 for (h=tc->refs;h;h=h->next) {
1721 report on memory usage by all children of a pointer, giving a full tree view
1723 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1724 void (*callback)(const void *ptr,
1725 int depth, int max_depth,
1727 void *private_data),
1730 struct talloc_chunk *c, *tc;
1735 if (ptr == NULL) return;
1737 tc = talloc_chunk_from_ptr(ptr);
1739 if (tc->flags & TALLOC_FLAG_LOOP) {
1743 callback(ptr, depth, max_depth, 0, private_data);
1745 if (max_depth >= 0 && depth >= max_depth) {
1749 tc->flags |= TALLOC_FLAG_LOOP;
1750 for (c=tc->child;c;c=c->next) {
1751 if (c->name == TALLOC_MAGIC_REFERENCE) {
1752 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1753 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1755 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1758 tc->flags &= ~TALLOC_FLAG_LOOP;
1761 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1763 const char *name = talloc_get_name(ptr);
1764 FILE *f = (FILE *)_f;
1767 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1772 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1773 (max_depth < 0 ? "full " :""), name,
1774 (unsigned long)talloc_total_size(ptr),
1775 (unsigned long)talloc_total_blocks(ptr));
1779 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1782 (unsigned long)talloc_total_size(ptr),
1783 (unsigned long)talloc_total_blocks(ptr),
1784 (int)talloc_reference_count(ptr), ptr);
1787 fprintf(f, "content: ");
1788 if (talloc_total_size(ptr)) {
1789 int tot = talloc_total_size(ptr);
1792 for (i = 0; i < tot; i++) {
1793 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1794 fprintf(f, "%c", ((char *)ptr)[i]);
1796 fprintf(f, "~%02x", ((char *)ptr)[i]);
1805 report on memory usage by all children of a pointer, giving a full tree view
1807 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1810 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1816 report on memory usage by all children of a pointer, giving a full tree view
1818 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
1820 talloc_report_depth_file(ptr, 0, -1, f);
1824 report on memory usage by all children of a pointer
1826 _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
1828 talloc_report_depth_file(ptr, 0, 1, f);
1832 report on any memory hanging off the null context
1834 static void talloc_report_null(void)
1836 if (talloc_total_size(null_context) != 0) {
1837 talloc_report(null_context, stderr);
1842 report on any memory hanging off the null context
1844 static void talloc_report_null_full(void)
1846 if (talloc_total_size(null_context) != 0) {
1847 talloc_report_full(null_context, stderr);
1852 enable tracking of the NULL context
1854 _PUBLIC_ void talloc_enable_null_tracking(void)
1856 if (null_context == NULL) {
1857 null_context = _talloc_named_const(NULL, 0, "null_context");
1858 if (autofree_context != NULL) {
1859 talloc_reparent(NULL, null_context, autofree_context);
1865 enable tracking of the NULL context, not moving the autofree context
1866 into the NULL context. This is needed for the talloc testsuite
1868 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
1870 if (null_context == NULL) {
1871 null_context = _talloc_named_const(NULL, 0, "null_context");
1876 disable tracking of the NULL context
1878 _PUBLIC_ void talloc_disable_null_tracking(void)
1880 if (null_context != NULL) {
1881 /* we have to move any children onto the real NULL
1883 struct talloc_chunk *tc, *tc2;
1884 tc = talloc_chunk_from_ptr(null_context);
1885 for (tc2 = tc->child; tc2; tc2=tc2->next) {
1886 if (tc2->parent == tc) tc2->parent = NULL;
1887 if (tc2->prev == tc) tc2->prev = NULL;
1889 for (tc2 = tc->next; tc2; tc2=tc2->next) {
1890 if (tc2->parent == tc) tc2->parent = NULL;
1891 if (tc2->prev == tc) tc2->prev = NULL;
1896 talloc_free(null_context);
1897 null_context = NULL;
1901 enable leak reporting on exit
1903 _PUBLIC_ void talloc_enable_leak_report(void)
1905 talloc_enable_null_tracking();
1906 atexit(talloc_report_null);
1910 enable full leak reporting on exit
1912 _PUBLIC_ void talloc_enable_leak_report_full(void)
1914 talloc_enable_null_tracking();
1915 atexit(talloc_report_null_full);
1919 talloc and zero memory.
1921 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
1923 void *p = _talloc_named_const(ctx, size, name);
1926 memset(p, '\0', size);
1933 memdup with a talloc.
1935 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1937 void *newp = _talloc_named_const(t, size, name);
1940 memcpy(newp, p, size);
1946 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1950 ret = (char *)__talloc(t, len + 1);
1951 if (unlikely(!ret)) return NULL;
1953 memcpy(ret, p, len);
1956 _talloc_set_name_const(ret, ret);
1961 strdup with a talloc
1963 _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
1965 if (unlikely(!p)) return NULL;
1966 return __talloc_strlendup(t, p, strlen(p));
1970 strndup with a talloc
1972 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
1974 if (unlikely(!p)) return NULL;
1975 return __talloc_strlendup(t, p, strnlen(p, n));
1978 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1979 const char *a, size_t alen)
1983 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1984 if (unlikely(!ret)) return NULL;
1986 /* append the string and the trailing \0 */
1987 memcpy(&ret[slen], a, alen);
1990 _talloc_set_name_const(ret, ret);
1995 * Appends at the end of the string.
1997 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
2000 return talloc_strdup(NULL, a);
2007 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
2011 * Appends at the end of the talloc'ed buffer,
2012 * not the end of the string.
2014 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
2019 return talloc_strdup(NULL, a);
2026 slen = talloc_get_size(s);
2027 if (likely(slen > 0)) {
2031 return __talloc_strlendup_append(s, slen, a, strlen(a));
2035 * Appends at the end of the string.
2037 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
2040 return talloc_strdup(NULL, a);
2047 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
2051 * Appends at the end of the talloc'ed buffer,
2052 * not the end of the string.
2054 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
2059 return talloc_strdup(NULL, a);
2066 slen = talloc_get_size(s);
2067 if (likely(slen > 0)) {
2071 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
2074 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
2081 /* this call looks strange, but it makes it work on older solaris boxes */
2083 len = vsnprintf(&c, 1, fmt, ap2);
2085 if (unlikely(len < 0)) {
2089 ret = (char *)__talloc(t, len+1);
2090 if (unlikely(!ret)) return NULL;
2093 vsnprintf(ret, len+1, fmt, ap2);
2096 _talloc_set_name_const(ret, ret);
2102 Perform string formatting, and return a pointer to newly allocated
2103 memory holding the result, inside a memory pool.
2105 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
2111 ret = talloc_vasprintf(t, fmt, ap);
2116 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2117 const char *fmt, va_list ap)
2118 PRINTF_ATTRIBUTE(3,0);
2120 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2121 const char *fmt, va_list ap)
2128 alen = vsnprintf(&c, 1, fmt, ap2);
2132 /* Either the vsnprintf failed or the format resulted in
2133 * no characters being formatted. In the former case, we
2134 * ought to return NULL, in the latter we ought to return
2135 * the original string. Most current callers of this
2136 * function expect it to never return NULL.
2141 s = talloc_realloc(NULL, s, char, slen + alen + 1);
2142 if (!s) return NULL;
2145 vsnprintf(s + slen, alen + 1, fmt, ap2);
2148 _talloc_set_name_const(s, s);
2153 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2154 * and return @p s, which may have moved. Good for gradually
2155 * accumulating output into a string buffer. Appends at the end
2158 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
2161 return talloc_vasprintf(NULL, fmt, ap);
2164 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
2168 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2169 * and return @p s, which may have moved. Always appends at the
2170 * end of the talloc'ed buffer, not the end of the string.
2172 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
2177 return talloc_vasprintf(NULL, fmt, ap);
2180 slen = talloc_get_size(s);
2181 if (likely(slen > 0)) {
2185 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
2189 Realloc @p s to append the formatted result of @p fmt and return @p
2190 s, which may have moved. Good for gradually accumulating output
2191 into a string buffer.
2193 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
2198 s = talloc_vasprintf_append(s, fmt, ap);
2204 Realloc @p s to append the formatted result of @p fmt and return @p
2205 s, which may have moved. Good for gradually accumulating output
2208 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
2213 s = talloc_vasprintf_append_buffer(s, fmt, ap);
2219 alloc an array, checking for integer overflow in the array size
2221 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2223 if (count >= MAX_TALLOC_SIZE/el_size) {
2226 return _talloc_named_const(ctx, el_size * count, name);
2230 alloc an zero array, checking for integer overflow in the array size
2232 _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2234 if (count >= MAX_TALLOC_SIZE/el_size) {
2237 return _talloc_zero(ctx, el_size * count, name);
2241 realloc an array, checking for integer overflow in the array size
2243 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
2245 if (count >= MAX_TALLOC_SIZE/el_size) {
2248 return _talloc_realloc(ctx, ptr, el_size * count, name);
2252 a function version of talloc_realloc(), so it can be passed as a function pointer
2253 to libraries that want a realloc function (a realloc function encapsulates
2254 all the basic capabilities of an allocation library, which is why this is useful)
2256 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
2258 return _talloc_realloc(context, ptr, size, NULL);
2262 static int talloc_autofree_destructor(void *ptr)
2264 autofree_context = NULL;
2268 static void talloc_autofree(void)
2270 talloc_free(autofree_context);
2274 return a context which will be auto-freed on exit
2275 this is useful for reducing the noise in leak reports
2277 _PUBLIC_ void *talloc_autofree_context(void)
2279 if (autofree_context == NULL) {
2280 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
2281 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
2282 atexit(talloc_autofree);
2284 return autofree_context;
2287 _PUBLIC_ size_t talloc_get_size(const void *context)
2289 struct talloc_chunk *tc;
2291 if (context == NULL) {
2292 context = null_context;
2294 if (context == NULL) {
2298 tc = talloc_chunk_from_ptr(context);
2304 find a parent of this context that has the given name, if any
2306 _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
2308 struct talloc_chunk *tc;
2310 if (context == NULL) {
2314 tc = talloc_chunk_from_ptr(context);
2316 if (tc->name && strcmp(tc->name, name) == 0) {
2317 return TC_PTR_FROM_CHUNK(tc);
2319 while (tc && tc->prev) tc = tc->prev;
2328 show the parentage of a context
2330 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
2332 struct talloc_chunk *tc;
2334 if (context == NULL) {
2335 fprintf(file, "talloc no parents for NULL\n");
2339 tc = talloc_chunk_from_ptr(context);
2340 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
2342 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
2343 while (tc && tc->prev) tc = tc->prev;
2352 return 1 if ptr is a parent of context
2354 static int _talloc_is_parent(const void *context, const void *ptr, int depth)
2356 struct talloc_chunk *tc;
2358 if (context == NULL) {
2362 tc = talloc_chunk_from_ptr(context);
2363 while (tc && depth > 0) {
2364 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2365 while (tc && tc->prev) tc = tc->prev;
2375 return 1 if ptr is a parent of context
2377 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2379 return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);