talloc: add TC_INVALIDATE_FULL_CHUNK() macro
[Samba.git] / lib / talloc / talloc.c
blob522d60ff8f4e2171702875ab64295132c75e82d6
1 /*
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
13 ** under the LGPL
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/
33 #include "replace.h"
34 #include "talloc.h"
36 #ifdef TALLOC_BUILD_VERSION_MAJOR
37 #if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)
38 #error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"
39 #endif
40 #endif
42 #ifdef TALLOC_BUILD_VERSION_MINOR
43 #if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)
44 #error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"
45 #endif
46 #endif
48 /* use this to force every realloc to change the pointer, to stress test
49 code that might not cope */
50 #define ALWAYS_REALLOC 0
53 #define MAX_TALLOC_SIZE 0x10000000
54 #define TALLOC_MAGIC_BASE 0xe814ec70
55 #define TALLOC_MAGIC ( \
56 TALLOC_MAGIC_BASE + \
57 (TALLOC_VERSION_MAJOR << 12) + \
58 (TALLOC_VERSION_MINOR << 4) \
61 #define TALLOC_FLAG_FREE 0x01
62 #define TALLOC_FLAG_LOOP 0x02
63 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
64 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
65 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
67 /* by default we abort when given a bad pointer (such as when talloc_free() is called
68 on a pointer that came from malloc() */
69 #ifndef TALLOC_ABORT
70 #define TALLOC_ABORT(reason) abort()
71 #endif
73 #ifndef discard_const_p
74 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
75 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
76 #else
77 # define discard_const_p(type, ptr) ((type *)(ptr))
78 #endif
79 #endif
81 /* these macros gain us a few percent of speed on gcc */
82 #if (__GNUC__ >= 3)
83 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
84 as its first argument */
85 #ifndef likely
86 #define likely(x) __builtin_expect(!!(x), 1)
87 #endif
88 #ifndef unlikely
89 #define unlikely(x) __builtin_expect(!!(x), 0)
90 #endif
91 #else
92 #ifndef likely
93 #define likely(x) (x)
94 #endif
95 #ifndef unlikely
96 #define unlikely(x) (x)
97 #endif
98 #endif
100 /* this null_context is only used if talloc_enable_leak_report() or
101 talloc_enable_leak_report_full() is called, otherwise it remains
102 NULL
104 static void *null_context;
105 static void *autofree_context;
107 /* used to enable fill of memory on free, which can be useful for
108 * catching use after free errors when valgrind is too slow
110 static struct {
111 bool initialised;
112 bool enabled;
113 uint8_t fill_value;
114 } talloc_fill;
116 #define TALLOC_FILL_ENV "TALLOC_FREE_FILL"
119 * do not wipe the header, to allow the
120 * double-free logic to still work
122 #define TC_INVALIDATE_FULL_FILL_CHUNK(_tc) do { \
123 if (unlikely(talloc_fill.enabled)) { \
124 size_t _flen = (_tc)->size; \
125 char *_fptr = TC_PTR_FROM_CHUNK(_tc); \
126 memset(_fptr, talloc_fill.fill_value, _flen); \
128 } while (0)
130 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
131 /* Mark the whole chunk as not accessable */
132 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { \
133 size_t _flen = TC_HDR_SIZE + (_tc)->size; \
134 char *_fptr = (char *)(_tc); \
135 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
136 } while(0)
137 #else
138 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { } while (0)
139 #endif
141 #define TC_INVALIDATE_FULL_CHUNK(_tc) do { \
142 TC_INVALIDATE_FULL_FILL_CHUNK(_tc); \
143 TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc); \
144 } while (0)
146 struct talloc_reference_handle {
147 struct talloc_reference_handle *next, *prev;
148 void *ptr;
149 const char *location;
152 typedef int (*talloc_destructor_t)(void *);
154 struct talloc_chunk {
155 struct talloc_chunk *next, *prev;
156 struct talloc_chunk *parent, *child;
157 struct talloc_reference_handle *refs;
158 talloc_destructor_t destructor;
159 const char *name;
160 size_t size;
161 unsigned flags;
164 * "pool" has dual use:
166 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
167 * marks the end of the currently allocated area.
169 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
170 * is a pointer to the struct talloc_chunk of the pool that it was
171 * allocated from. This way children can quickly find the pool to chew
172 * from.
174 void *pool;
177 /* 16 byte alignment seems to keep everyone happy */
178 #define TC_ALIGN16(s) (((s)+15)&~15)
179 #define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
180 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
182 _PUBLIC_ int talloc_version_major(void)
184 return TALLOC_VERSION_MAJOR;
187 _PUBLIC_ int talloc_version_minor(void)
189 return TALLOC_VERSION_MINOR;
192 static void (*talloc_log_fn)(const char *message);
194 _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
196 talloc_log_fn = log_fn;
199 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
200 static void talloc_log(const char *fmt, ...)
202 va_list ap;
203 char *message;
205 if (!talloc_log_fn) {
206 return;
209 va_start(ap, fmt);
210 message = talloc_vasprintf(NULL, fmt, ap);
211 va_end(ap);
213 talloc_log_fn(message);
214 talloc_free(message);
217 static void talloc_log_stderr(const char *message)
219 fprintf(stderr, "%s", message);
222 _PUBLIC_ void talloc_set_log_stderr(void)
224 talloc_set_log_fn(talloc_log_stderr);
227 static void (*talloc_abort_fn)(const char *reason);
229 _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
231 talloc_abort_fn = abort_fn;
234 static void talloc_abort(const char *reason)
236 talloc_log("%s\n", reason);
238 if (!talloc_abort_fn) {
239 TALLOC_ABORT(reason);
242 talloc_abort_fn(reason);
245 static void talloc_abort_magic(unsigned magic)
247 unsigned striped = magic - TALLOC_MAGIC_BASE;
248 unsigned major = (striped & 0xFFFFF000) >> 12;
249 unsigned minor = (striped & 0x00000FF0) >> 4;
250 talloc_log("Bad talloc magic[0x%08X/%u/%u] expected[0x%08X/%u/%u]\n",
251 magic, major, minor,
252 TALLOC_MAGIC, TALLOC_VERSION_MAJOR, TALLOC_VERSION_MINOR);
253 talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
256 static void talloc_abort_access_after_free(void)
258 talloc_abort("Bad talloc magic value - access after free");
261 static void talloc_abort_unknown_value(void)
263 talloc_abort("Bad talloc magic value - unknown value");
266 /* panic if we get a bad magic value */
267 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
269 const char *pp = (const char *)ptr;
270 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
271 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
272 if ((tc->flags & (~0xFFF)) == TALLOC_MAGIC_BASE) {
273 talloc_abort_magic(tc->flags & (~0xF));
274 return NULL;
277 if (tc->flags & TALLOC_FLAG_FREE) {
278 talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
279 talloc_abort_access_after_free();
280 return NULL;
281 } else {
282 talloc_abort_unknown_value();
283 return NULL;
286 return tc;
289 /* hook into the front of the list */
290 #define _TLIST_ADD(list, p) \
291 do { \
292 if (!(list)) { \
293 (list) = (p); \
294 (p)->next = (p)->prev = NULL; \
295 } else { \
296 (list)->prev = (p); \
297 (p)->next = (list); \
298 (p)->prev = NULL; \
299 (list) = (p); \
301 } while (0)
303 /* remove an element from a list - element doesn't have to be in list. */
304 #define _TLIST_REMOVE(list, p) \
305 do { \
306 if ((p) == (list)) { \
307 (list) = (p)->next; \
308 if (list) (list)->prev = NULL; \
309 } else { \
310 if ((p)->prev) (p)->prev->next = (p)->next; \
311 if ((p)->next) (p)->next->prev = (p)->prev; \
313 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
314 } while (0)
318 return the parent chunk of a pointer
320 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
322 struct talloc_chunk *tc;
324 if (unlikely(ptr == NULL)) {
325 return NULL;
328 tc = talloc_chunk_from_ptr(ptr);
329 while (tc->prev) tc=tc->prev;
331 return tc->parent;
334 _PUBLIC_ void *talloc_parent(const void *ptr)
336 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
337 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
341 find parents name
343 _PUBLIC_ const char *talloc_parent_name(const void *ptr)
345 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
346 return tc? tc->name : NULL;
350 A pool carries an in-pool object count count in the first 16 bytes.
351 bytes. This is done to support talloc_steal() to a parent outside of the
352 pool. The count includes the pool itself, so a talloc_free() on a pool will
353 only destroy the pool if the count has dropped to zero. A talloc_free() of a
354 pool member will reduce the count, and eventually also call free(3) on the
355 pool memory.
357 The object count is not put into "struct talloc_chunk" because it is only
358 relevant for talloc pools and the alignment to 16 bytes would increase the
359 memory footprint of each talloc chunk by those 16 bytes.
362 #define TALLOC_POOL_HDR_SIZE 16
364 #define TC_POOL_SPACE_LEFT(_pool_tc) \
365 PTR_DIFF(TC_HDR_SIZE + (_pool_tc)->size + (char *)(_pool_tc), \
366 (_pool_tc)->pool)
368 #define TC_POOL_FIRST_CHUNK(_pool_tc) \
369 ((void *)(TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE + (char *)(_pool_tc)))
371 #define TC_POOLMEM_CHUNK_SIZE(_tc) \
372 TC_ALIGN16(TC_HDR_SIZE + (_tc)->size)
374 #define TC_POOLMEM_NEXT_CHUNK(_tc) \
375 ((void *)(TC_POOLMEM_CHUNK_SIZE(tc) + (char*)(_tc)))
377 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
379 return (unsigned int *)((char *)tc + TC_HDR_SIZE);
383 Allocate from a pool
386 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
387 size_t size)
389 struct talloc_chunk *pool_ctx = NULL;
390 size_t space_left;
391 struct talloc_chunk *result;
392 size_t chunk_size;
394 if (parent == NULL) {
395 return NULL;
398 if (parent->flags & TALLOC_FLAG_POOL) {
399 pool_ctx = parent;
401 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
402 pool_ctx = (struct talloc_chunk *)parent->pool;
405 if (pool_ctx == NULL) {
406 return NULL;
409 space_left = TC_POOL_SPACE_LEFT(pool_ctx);
412 * Align size to 16 bytes
414 chunk_size = TC_ALIGN16(size);
416 if (space_left < chunk_size) {
417 return NULL;
420 result = (struct talloc_chunk *)pool_ctx->pool;
422 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
423 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
424 #endif
426 pool_ctx->pool = (void *)((char *)result + chunk_size);
428 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
429 result->pool = pool_ctx;
431 *talloc_pool_objectcount(pool_ctx) += 1;
433 return result;
437 Allocate a bit of memory as a child of an existing pointer
439 static inline void *__talloc(const void *context, size_t size)
441 struct talloc_chunk *tc = NULL;
443 if (unlikely(context == NULL)) {
444 context = null_context;
447 if (unlikely(size >= MAX_TALLOC_SIZE)) {
448 return NULL;
451 if (context != NULL) {
452 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
453 TC_HDR_SIZE+size);
456 if (tc == NULL) {
457 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
458 if (unlikely(tc == NULL)) return NULL;
459 tc->flags = TALLOC_MAGIC;
460 tc->pool = NULL;
463 tc->size = size;
464 tc->destructor = NULL;
465 tc->child = NULL;
466 tc->name = NULL;
467 tc->refs = NULL;
469 if (likely(context)) {
470 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
472 if (parent->child) {
473 parent->child->parent = NULL;
474 tc->next = parent->child;
475 tc->next->prev = tc;
476 } else {
477 tc->next = NULL;
479 tc->parent = parent;
480 tc->prev = NULL;
481 parent->child = tc;
482 } else {
483 tc->next = tc->prev = tc->parent = NULL;
486 return TC_PTR_FROM_CHUNK(tc);
490 * Create a talloc pool
493 _PUBLIC_ void *talloc_pool(const void *context, size_t size)
495 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
496 struct talloc_chunk *tc;
498 if (unlikely(result == NULL)) {
499 return NULL;
502 tc = talloc_chunk_from_ptr(result);
504 tc->flags |= TALLOC_FLAG_POOL;
505 tc->pool = TC_POOL_FIRST_CHUNK(tc);
507 *talloc_pool_objectcount(tc) = 1;
509 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
510 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
511 #endif
513 return result;
517 setup a destructor to be called on free of a pointer
518 the destructor should return 0 on success, or -1 on failure.
519 if the destructor fails then the free is failed, and the memory can
520 be continued to be used
522 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
524 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
525 tc->destructor = destructor;
529 increase the reference count on a piece of memory.
531 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
533 if (unlikely(!talloc_reference(null_context, ptr))) {
534 return -1;
536 return 0;
540 helper for talloc_reference()
542 this is referenced by a function pointer and should not be inline
544 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
546 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
547 _TLIST_REMOVE(ptr_tc->refs, handle);
548 return 0;
552 more efficient way to add a name to a pointer - the name must point to a
553 true string constant
555 static inline void _talloc_set_name_const(const void *ptr, const char *name)
557 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
558 tc->name = name;
562 internal talloc_named_const()
564 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
566 void *ptr;
568 ptr = __talloc(context, size);
569 if (unlikely(ptr == NULL)) {
570 return NULL;
573 _talloc_set_name_const(ptr, name);
575 return ptr;
579 make a secondary reference to a pointer, hanging off the given context.
580 the pointer remains valid until both the original caller and this given
581 context are freed.
583 the major use for this is when two different structures need to reference the
584 same underlying data, and you want to be able to free the two instances separately,
585 and in either order
587 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
589 struct talloc_chunk *tc;
590 struct talloc_reference_handle *handle;
591 if (unlikely(ptr == NULL)) return NULL;
593 tc = talloc_chunk_from_ptr(ptr);
594 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
595 sizeof(struct talloc_reference_handle),
596 TALLOC_MAGIC_REFERENCE);
597 if (unlikely(handle == NULL)) return NULL;
599 /* note that we hang the destructor off the handle, not the
600 main context as that allows the caller to still setup their
601 own destructor on the context if they want to */
602 talloc_set_destructor(handle, talloc_reference_destructor);
603 handle->ptr = discard_const_p(void, ptr);
604 handle->location = location;
605 _TLIST_ADD(tc->refs, handle);
606 return handle->ptr;
609 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
612 internal talloc_free call
614 static inline int _talloc_free_internal(void *ptr, const char *location)
616 struct talloc_chunk *tc;
618 if (unlikely(ptr == NULL)) {
619 return -1;
622 /* possibly initialised the talloc fill value */
623 if (unlikely(!talloc_fill.initialised)) {
624 const char *fill = getenv(TALLOC_FILL_ENV);
625 if (fill != NULL) {
626 talloc_fill.enabled = true;
627 talloc_fill.fill_value = strtoul(fill, NULL, 0);
629 talloc_fill.initialised = true;
632 tc = talloc_chunk_from_ptr(ptr);
634 if (unlikely(tc->refs)) {
635 int is_child;
636 /* check if this is a reference from a child or
637 * grandchild back to it's parent or grandparent
639 * in that case we need to remove the reference and
640 * call another instance of talloc_free() on the current
641 * pointer.
643 is_child = talloc_is_parent(tc->refs, ptr);
644 _talloc_free_internal(tc->refs, location);
645 if (is_child) {
646 return _talloc_free_internal(ptr, location);
648 return -1;
651 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
652 /* we have a free loop - stop looping */
653 return 0;
656 if (unlikely(tc->destructor)) {
657 talloc_destructor_t d = tc->destructor;
658 if (d == (talloc_destructor_t)-1) {
659 return -1;
661 tc->destructor = (talloc_destructor_t)-1;
662 if (d(ptr) == -1) {
663 tc->destructor = d;
664 return -1;
666 tc->destructor = NULL;
669 if (tc->parent) {
670 _TLIST_REMOVE(tc->parent->child, tc);
671 if (tc->parent->child) {
672 tc->parent->child->parent = tc->parent;
674 } else {
675 if (tc->prev) tc->prev->next = tc->next;
676 if (tc->next) tc->next->prev = tc->prev;
679 tc->flags |= TALLOC_FLAG_LOOP;
681 while (tc->child) {
682 /* we need to work out who will own an abandoned child
683 if it cannot be freed. In priority order, the first
684 choice is owner of any remaining reference to this
685 pointer, the second choice is our parent, and the
686 final choice is the null context. */
687 void *child = TC_PTR_FROM_CHUNK(tc->child);
688 const void *new_parent = null_context;
689 struct talloc_chunk *old_parent = NULL;
690 if (unlikely(tc->child->refs)) {
691 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
692 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
694 /* finding the parent here is potentially quite
695 expensive, but the alternative, which is to change
696 talloc to always have a valid tc->parent pointer,
697 makes realloc more expensive where there are a
698 large number of children.
700 The reason we need the parent pointer here is that
701 if _talloc_free_internal() fails due to references
702 or a failing destructor we need to re-parent, but
703 the free call can invalidate the prev pointer.
705 if (new_parent == null_context && (tc->child->refs || tc->child->destructor)) {
706 old_parent = talloc_parent_chunk(ptr);
708 if (unlikely(_talloc_free_internal(child, location) == -1)) {
709 if (new_parent == null_context) {
710 struct talloc_chunk *p = old_parent;
711 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
713 _talloc_steal_internal(new_parent, child);
717 tc->flags |= TALLOC_FLAG_FREE;
719 /* we mark the freed memory with where we called the free
720 * from. This means on a double free error we can report where
721 * the first free came from
723 tc->name = location;
725 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
726 struct talloc_chunk *pool;
727 void *next_tc = NULL;
728 unsigned int *pool_object_count;
730 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
731 pool = tc;
732 } else {
733 pool = (struct talloc_chunk *)tc->pool;
734 next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
736 TC_INVALIDATE_FULL_CHUNK(tc);
739 pool_object_count = talloc_pool_objectcount(pool);
741 if (unlikely(*pool_object_count == 0)) {
742 talloc_abort("Pool object count zero!");
743 return 0;
746 *pool_object_count -= 1;
748 if (unlikely(*pool_object_count == 1)) {
750 * if there is just object left in the pool
751 * it means this is the pool itself and
752 * the rest is available for new objects
753 * again.
755 pool->pool = TC_POOL_FIRST_CHUNK(pool);
756 } else if (unlikely(*pool_object_count == 0)) {
757 TC_INVALIDATE_FULL_CHUNK(pool);
758 free(pool);
759 } else if (pool->pool == next_tc) {
761 * if pool->pool still points to end of
762 * 'tc' (which is stored in the 'next_tc' variable),
763 * we can reclaim the memory of 'tc'.
765 pool->pool = tc;
767 } else {
768 TC_INVALIDATE_FULL_CHUNK(tc);
769 free(tc);
771 return 0;
775 move a lump of memory from one talloc context to another return the
776 ptr on success, or NULL if it could not be transferred.
777 passing NULL as ptr will always return NULL with no side effects.
779 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
781 struct talloc_chunk *tc, *new_tc;
783 if (unlikely(!ptr)) {
784 return NULL;
787 if (unlikely(new_ctx == NULL)) {
788 new_ctx = null_context;
791 tc = talloc_chunk_from_ptr(ptr);
793 if (unlikely(new_ctx == NULL)) {
794 if (tc->parent) {
795 _TLIST_REMOVE(tc->parent->child, tc);
796 if (tc->parent->child) {
797 tc->parent->child->parent = tc->parent;
799 } else {
800 if (tc->prev) tc->prev->next = tc->next;
801 if (tc->next) tc->next->prev = tc->prev;
804 tc->parent = tc->next = tc->prev = NULL;
805 return discard_const_p(void, ptr);
808 new_tc = talloc_chunk_from_ptr(new_ctx);
810 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
811 return discard_const_p(void, ptr);
814 if (tc->parent) {
815 _TLIST_REMOVE(tc->parent->child, tc);
816 if (tc->parent->child) {
817 tc->parent->child->parent = tc->parent;
819 } else {
820 if (tc->prev) tc->prev->next = tc->next;
821 if (tc->next) tc->next->prev = tc->prev;
824 tc->parent = new_tc;
825 if (new_tc->child) new_tc->child->parent = NULL;
826 _TLIST_ADD(new_tc->child, tc);
828 return discard_const_p(void, ptr);
832 move a lump of memory from one talloc context to another return the
833 ptr on success, or NULL if it could not be transferred.
834 passing NULL as ptr will always return NULL with no side effects.
836 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
838 struct talloc_chunk *tc;
840 if (unlikely(ptr == NULL)) {
841 return NULL;
844 tc = talloc_chunk_from_ptr(ptr);
846 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
847 struct talloc_reference_handle *h;
849 talloc_log("WARNING: talloc_steal with references at %s\n",
850 location);
852 for (h=tc->refs; h; h=h->next) {
853 talloc_log("\treference at %s\n",
854 h->location);
858 #if 0
859 /* this test is probably too expensive to have on in the
860 normal build, but it useful for debugging */
861 if (talloc_is_parent(new_ctx, ptr)) {
862 talloc_log("WARNING: stealing into talloc child at %s\n", location);
864 #endif
866 return _talloc_steal_internal(new_ctx, ptr);
870 this is like a talloc_steal(), but you must supply the old
871 parent. This resolves the ambiguity in a talloc_steal() which is
872 called on a context that has more than one parent (via references)
874 The old parent can be either a reference or a parent
876 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
878 struct talloc_chunk *tc;
879 struct talloc_reference_handle *h;
881 if (unlikely(ptr == NULL)) {
882 return NULL;
885 if (old_parent == talloc_parent(ptr)) {
886 return _talloc_steal_internal(new_parent, ptr);
889 tc = talloc_chunk_from_ptr(ptr);
890 for (h=tc->refs;h;h=h->next) {
891 if (talloc_parent(h) == old_parent) {
892 if (_talloc_steal_internal(new_parent, h) != h) {
893 return NULL;
895 return discard_const_p(void, ptr);
899 /* it wasn't a parent */
900 return NULL;
904 remove a secondary reference to a pointer. This undo's what
905 talloc_reference() has done. The context and pointer arguments
906 must match those given to a talloc_reference()
908 static inline int talloc_unreference(const void *context, const void *ptr)
910 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
911 struct talloc_reference_handle *h;
913 if (unlikely(context == NULL)) {
914 context = null_context;
917 for (h=tc->refs;h;h=h->next) {
918 struct talloc_chunk *p = talloc_parent_chunk(h);
919 if (p == NULL) {
920 if (context == NULL) break;
921 } else if (TC_PTR_FROM_CHUNK(p) == context) {
922 break;
925 if (h == NULL) {
926 return -1;
929 return _talloc_free_internal(h, __location__);
933 remove a specific parent context from a pointer. This is a more
934 controlled varient of talloc_free()
936 _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
938 struct talloc_chunk *tc_p, *new_p;
939 void *new_parent;
941 if (ptr == NULL) {
942 return -1;
945 if (context == NULL) {
946 context = null_context;
949 if (talloc_unreference(context, ptr) == 0) {
950 return 0;
953 if (context == NULL) {
954 if (talloc_parent_chunk(ptr) != NULL) {
955 return -1;
957 } else {
958 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
959 return -1;
963 tc_p = talloc_chunk_from_ptr(ptr);
965 if (tc_p->refs == NULL) {
966 return _talloc_free_internal(ptr, __location__);
969 new_p = talloc_parent_chunk(tc_p->refs);
970 if (new_p) {
971 new_parent = TC_PTR_FROM_CHUNK(new_p);
972 } else {
973 new_parent = NULL;
976 if (talloc_unreference(new_parent, ptr) != 0) {
977 return -1;
980 _talloc_steal_internal(new_parent, ptr);
982 return 0;
986 add a name to an existing pointer - va_list version
988 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
990 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
992 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
993 tc->name = talloc_vasprintf(ptr, fmt, ap);
994 if (likely(tc->name)) {
995 _talloc_set_name_const(tc->name, ".name");
997 return tc->name;
1001 add a name to an existing pointer
1003 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
1005 const char *name;
1006 va_list ap;
1007 va_start(ap, fmt);
1008 name = talloc_set_name_v(ptr, fmt, ap);
1009 va_end(ap);
1010 return name;
1015 create a named talloc pointer. Any talloc pointer can be named, and
1016 talloc_named() operates just like talloc() except that it allows you
1017 to name the pointer.
1019 _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
1021 va_list ap;
1022 void *ptr;
1023 const char *name;
1025 ptr = __talloc(context, size);
1026 if (unlikely(ptr == NULL)) return NULL;
1028 va_start(ap, fmt);
1029 name = talloc_set_name_v(ptr, fmt, ap);
1030 va_end(ap);
1032 if (unlikely(name == NULL)) {
1033 _talloc_free_internal(ptr, __location__);
1034 return NULL;
1037 return ptr;
1041 return the name of a talloc ptr, or "UNNAMED"
1043 _PUBLIC_ const char *talloc_get_name(const void *ptr)
1045 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1046 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
1047 return ".reference";
1049 if (likely(tc->name)) {
1050 return tc->name;
1052 return "UNNAMED";
1057 check if a pointer has the given name. If it does, return the pointer,
1058 otherwise return NULL
1060 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
1062 const char *pname;
1063 if (unlikely(ptr == NULL)) return NULL;
1064 pname = talloc_get_name(ptr);
1065 if (likely(pname == name || strcmp(pname, name) == 0)) {
1066 return discard_const_p(void, ptr);
1068 return NULL;
1071 static void talloc_abort_type_missmatch(const char *location,
1072 const char *name,
1073 const char *expected)
1075 const char *reason;
1077 reason = talloc_asprintf(NULL,
1078 "%s: Type mismatch: name[%s] expected[%s]",
1079 location,
1080 name?name:"NULL",
1081 expected);
1082 if (!reason) {
1083 reason = "Type mismatch";
1086 talloc_abort(reason);
1089 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
1091 const char *pname;
1093 if (unlikely(ptr == NULL)) {
1094 talloc_abort_type_missmatch(location, NULL, name);
1095 return NULL;
1098 pname = talloc_get_name(ptr);
1099 if (likely(pname == name || strcmp(pname, name) == 0)) {
1100 return discard_const_p(void, ptr);
1103 talloc_abort_type_missmatch(location, pname, name);
1104 return NULL;
1108 this is for compatibility with older versions of talloc
1110 _PUBLIC_ void *talloc_init(const char *fmt, ...)
1112 va_list ap;
1113 void *ptr;
1114 const char *name;
1116 ptr = __talloc(NULL, 0);
1117 if (unlikely(ptr == NULL)) return NULL;
1119 va_start(ap, fmt);
1120 name = talloc_set_name_v(ptr, fmt, ap);
1121 va_end(ap);
1123 if (unlikely(name == NULL)) {
1124 _talloc_free_internal(ptr, __location__);
1125 return NULL;
1128 return ptr;
1132 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1133 should probably not be used in new code. It's in here to keep the talloc
1134 code consistent across Samba 3 and 4.
1136 _PUBLIC_ void talloc_free_children(void *ptr)
1138 struct talloc_chunk *tc;
1140 if (unlikely(ptr == NULL)) {
1141 return;
1144 tc = talloc_chunk_from_ptr(ptr);
1146 while (tc->child) {
1147 /* we need to work out who will own an abandoned child
1148 if it cannot be freed. In priority order, the first
1149 choice is owner of any remaining reference to this
1150 pointer, the second choice is our parent, and the
1151 final choice is the null context. */
1152 void *child = TC_PTR_FROM_CHUNK(tc->child);
1153 const void *new_parent = null_context;
1154 if (unlikely(tc->child->refs)) {
1155 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1156 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1158 if (unlikely(talloc_free(child) == -1)) {
1159 if (new_parent == null_context) {
1160 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1161 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1163 _talloc_steal_internal(new_parent, child);
1169 Allocate a bit of memory as a child of an existing pointer
1171 _PUBLIC_ void *_talloc(const void *context, size_t size)
1173 return __talloc(context, size);
1177 externally callable talloc_set_name_const()
1179 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1181 _talloc_set_name_const(ptr, name);
1185 create a named talloc pointer. Any talloc pointer can be named, and
1186 talloc_named() operates just like talloc() except that it allows you
1187 to name the pointer.
1189 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1191 return _talloc_named_const(context, size, name);
1195 free a talloc pointer. This also frees all child pointers of this
1196 pointer recursively
1198 return 0 if the memory is actually freed, otherwise -1. The memory
1199 will not be freed if the ref_count is > 1 or the destructor (if
1200 any) returns non-zero
1202 _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1204 struct talloc_chunk *tc;
1206 if (unlikely(ptr == NULL)) {
1207 return -1;
1210 tc = talloc_chunk_from_ptr(ptr);
1212 if (unlikely(tc->refs != NULL)) {
1213 struct talloc_reference_handle *h;
1215 if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1216 /* in this case we do know which parent should
1217 get this pointer, as there is really only
1218 one parent */
1219 return talloc_unlink(null_context, ptr);
1222 talloc_log("ERROR: talloc_free with references at %s\n",
1223 location);
1225 for (h=tc->refs; h; h=h->next) {
1226 talloc_log("\treference at %s\n",
1227 h->location);
1229 return -1;
1232 return _talloc_free_internal(ptr, location);
1238 A talloc version of realloc. The context argument is only used if
1239 ptr is NULL
1241 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1243 struct talloc_chunk *tc;
1244 void *new_ptr;
1245 bool malloced = false;
1246 struct talloc_chunk *pool_tc = NULL;
1248 /* size zero is equivalent to free() */
1249 if (unlikely(size == 0)) {
1250 talloc_unlink(context, ptr);
1251 return NULL;
1254 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1255 return NULL;
1258 /* realloc(NULL) is equivalent to malloc() */
1259 if (ptr == NULL) {
1260 return _talloc_named_const(context, size, name);
1263 tc = talloc_chunk_from_ptr(ptr);
1265 /* don't allow realloc on referenced pointers */
1266 if (unlikely(tc->refs)) {
1267 return NULL;
1270 /* don't let anybody try to realloc a talloc_pool */
1271 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1272 return NULL;
1275 /* don't let anybody try to realloc a talloc_pool */
1276 if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
1277 pool_tc = (struct talloc_chunk *)tc->pool;
1280 #if (ALWAYS_REALLOC == 0)
1281 /* don't shrink if we have less than 1k to gain */
1282 if (size < tc->size) {
1283 if (pool_tc) {
1284 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1285 tc->size = size;
1286 if (next_tc == pool_tc->pool) {
1287 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1289 return ptr;
1290 } else if ((tc->size - size) < 1024) {
1291 /* do not shrink if we have less than 1k to gain */
1292 tc->size = size;
1293 return ptr;
1295 } else if (tc->size == size) {
1297 * do not change the pointer if it is exactly
1298 * the same size.
1300 return ptr;
1302 #endif
1304 /* by resetting magic we catch users of the old memory */
1305 tc->flags |= TALLOC_FLAG_FREE;
1307 #if ALWAYS_REALLOC
1308 if (pool_tc) {
1309 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1310 *talloc_pool_objectcount(pool_tc) -= 1;
1312 if (new_ptr == NULL) {
1313 new_ptr = malloc(TC_HDR_SIZE+size);
1314 malloced = true;
1317 if (new_ptr) {
1318 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1319 TC_INVALIDATE_FULL_CHUNK(tc);
1321 } else {
1322 new_ptr = malloc(size + TC_HDR_SIZE);
1323 if (new_ptr) {
1324 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1325 free(tc);
1328 #else
1329 if (pool_tc) {
1330 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1331 size_t old_chunk_size = TC_POOLMEM_CHUNK_SIZE(tc);
1332 size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
1333 size_t space_needed;
1334 size_t space_left;
1336 if (*talloc_pool_objectcount(pool_tc) == 2) {
1338 * optimize for the case where 'tc' is the only
1339 * chunk in the pool.
1341 space_needed = new_chunk_size;
1342 space_left = pool_tc->size - TALLOC_POOL_HDR_SIZE;
1344 if (space_left >= space_needed) {
1345 size_t old_used = TC_HDR_SIZE + tc->size;
1346 pool_tc->pool = TC_POOL_FIRST_CHUNK(pool_tc);
1347 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
1349 * we need to prepare the memmove into
1350 * the unaccessable area.
1353 size_t diff = PTR_DIFF(tc, pool_tc->pool);
1354 size_t flen = MIN(diff, old_used);
1355 char *fptr = (char *)pool_tc->pool;
1356 VALGRIND_MAKE_MEM_UNDEFINED(fptr, flen);
1358 #endif
1359 memmove(pool_tc->pool, tc, old_used);
1360 new_ptr = pool_tc->pool;
1362 pool_tc->pool = new_chunk_size + (char *)new_ptr;
1363 goto got_new_ptr;
1366 next_tc = NULL;
1369 if (new_chunk_size == old_chunk_size) {
1370 tc->flags &= ~TALLOC_FLAG_FREE;
1371 tc->size = size;
1372 return ptr;
1375 if (next_tc == pool_tc->pool) {
1377 * optimize for the case where 'tc' is the last
1378 * chunk in the pool.
1380 space_needed = new_chunk_size - old_chunk_size;
1381 space_left = TC_POOL_SPACE_LEFT(pool_tc);
1383 if (space_left >= space_needed) {
1384 tc->flags &= ~TALLOC_FLAG_FREE;
1385 tc->size = size;
1386 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1387 return ptr;
1391 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1392 *talloc_pool_objectcount(pool_tc) -= 1;
1394 if (new_ptr == NULL) {
1395 new_ptr = malloc(TC_HDR_SIZE+size);
1396 malloced = true;
1399 if (new_ptr) {
1400 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1401 TC_INVALIDATE_FULL_CHUNK(tc);
1403 if (*talloc_pool_objectcount(pool_tc) == 1) {
1405 * If the pool is empty now reclaim everything.
1407 pool_tc->pool = TC_POOL_FIRST_CHUNK(pool_tc);
1408 } else if (next_tc == pool_tc->pool) {
1410 * If it was reallocated and tc was the last
1411 * chunk, we can reclaim the memory of tc.
1413 pool_tc->pool = tc;
1417 else {
1418 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1420 got_new_ptr:
1421 #endif
1422 if (unlikely(!new_ptr)) {
1423 tc->flags &= ~TALLOC_FLAG_FREE;
1424 return NULL;
1427 tc = (struct talloc_chunk *)new_ptr;
1428 tc->flags &= ~TALLOC_FLAG_FREE;
1429 if (malloced) {
1430 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1432 if (tc->parent) {
1433 tc->parent->child = tc;
1435 if (tc->child) {
1436 tc->child->parent = tc;
1439 if (tc->prev) {
1440 tc->prev->next = tc;
1442 if (tc->next) {
1443 tc->next->prev = tc;
1446 tc->size = size;
1447 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1449 return TC_PTR_FROM_CHUNK(tc);
1453 a wrapper around talloc_steal() for situations where you are moving a pointer
1454 between two structures, and want the old pointer to be set to NULL
1456 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
1458 const void **pptr = discard_const_p(const void *,_pptr);
1459 void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1460 (*pptr) = NULL;
1461 return ret;
1465 return the total size of a talloc pool (subtree)
1467 _PUBLIC_ size_t talloc_total_size(const void *ptr)
1469 size_t total = 0;
1470 struct talloc_chunk *c, *tc;
1472 if (ptr == NULL) {
1473 ptr = null_context;
1475 if (ptr == NULL) {
1476 return 0;
1479 tc = talloc_chunk_from_ptr(ptr);
1481 if (tc->flags & TALLOC_FLAG_LOOP) {
1482 return 0;
1485 tc->flags |= TALLOC_FLAG_LOOP;
1487 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1488 total = tc->size;
1490 for (c=tc->child;c;c=c->next) {
1491 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1494 tc->flags &= ~TALLOC_FLAG_LOOP;
1496 return total;
1500 return the total number of blocks in a talloc pool (subtree)
1502 _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
1504 size_t total = 0;
1505 struct talloc_chunk *c, *tc;
1507 if (ptr == NULL) {
1508 ptr = null_context;
1510 if (ptr == NULL) {
1511 return 0;
1514 tc = talloc_chunk_from_ptr(ptr);
1516 if (tc->flags & TALLOC_FLAG_LOOP) {
1517 return 0;
1520 tc->flags |= TALLOC_FLAG_LOOP;
1522 total++;
1523 for (c=tc->child;c;c=c->next) {
1524 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1527 tc->flags &= ~TALLOC_FLAG_LOOP;
1529 return total;
1533 return the number of external references to a pointer
1535 _PUBLIC_ size_t talloc_reference_count(const void *ptr)
1537 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1538 struct talloc_reference_handle *h;
1539 size_t ret = 0;
1541 for (h=tc->refs;h;h=h->next) {
1542 ret++;
1544 return ret;
1548 report on memory usage by all children of a pointer, giving a full tree view
1550 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1551 void (*callback)(const void *ptr,
1552 int depth, int max_depth,
1553 int is_ref,
1554 void *private_data),
1555 void *private_data)
1557 struct talloc_chunk *c, *tc;
1559 if (ptr == NULL) {
1560 ptr = null_context;
1562 if (ptr == NULL) return;
1564 tc = talloc_chunk_from_ptr(ptr);
1566 if (tc->flags & TALLOC_FLAG_LOOP) {
1567 return;
1570 callback(ptr, depth, max_depth, 0, private_data);
1572 if (max_depth >= 0 && depth >= max_depth) {
1573 return;
1576 tc->flags |= TALLOC_FLAG_LOOP;
1577 for (c=tc->child;c;c=c->next) {
1578 if (c->name == TALLOC_MAGIC_REFERENCE) {
1579 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1580 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1581 } else {
1582 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1585 tc->flags &= ~TALLOC_FLAG_LOOP;
1588 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1590 const char *name = talloc_get_name(ptr);
1591 FILE *f = (FILE *)_f;
1593 if (is_ref) {
1594 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1595 return;
1598 if (depth == 0) {
1599 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1600 (max_depth < 0 ? "full " :""), name,
1601 (unsigned long)talloc_total_size(ptr),
1602 (unsigned long)talloc_total_blocks(ptr));
1603 return;
1606 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1607 depth*4, "",
1608 name,
1609 (unsigned long)talloc_total_size(ptr),
1610 (unsigned long)talloc_total_blocks(ptr),
1611 (int)talloc_reference_count(ptr), ptr);
1613 #if 0
1614 fprintf(f, "content: ");
1615 if (talloc_total_size(ptr)) {
1616 int tot = talloc_total_size(ptr);
1617 int i;
1619 for (i = 0; i < tot; i++) {
1620 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1621 fprintf(f, "%c", ((char *)ptr)[i]);
1622 } else {
1623 fprintf(f, "~%02x", ((char *)ptr)[i]);
1627 fprintf(f, "\n");
1628 #endif
1632 report on memory usage by all children of a pointer, giving a full tree view
1634 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1636 if (f) {
1637 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1638 fflush(f);
1643 report on memory usage by all children of a pointer, giving a full tree view
1645 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
1647 talloc_report_depth_file(ptr, 0, -1, f);
1651 report on memory usage by all children of a pointer
1653 _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
1655 talloc_report_depth_file(ptr, 0, 1, f);
1659 report on any memory hanging off the null context
1661 static void talloc_report_null(void)
1663 if (talloc_total_size(null_context) != 0) {
1664 talloc_report(null_context, stderr);
1669 report on any memory hanging off the null context
1671 static void talloc_report_null_full(void)
1673 if (talloc_total_size(null_context) != 0) {
1674 talloc_report_full(null_context, stderr);
1679 enable tracking of the NULL context
1681 _PUBLIC_ void talloc_enable_null_tracking(void)
1683 if (null_context == NULL) {
1684 null_context = _talloc_named_const(NULL, 0, "null_context");
1685 if (autofree_context != NULL) {
1686 talloc_reparent(NULL, null_context, autofree_context);
1692 enable tracking of the NULL context, not moving the autofree context
1693 into the NULL context. This is needed for the talloc testsuite
1695 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
1697 if (null_context == NULL) {
1698 null_context = _talloc_named_const(NULL, 0, "null_context");
1703 disable tracking of the NULL context
1705 _PUBLIC_ void talloc_disable_null_tracking(void)
1707 if (null_context != NULL) {
1708 /* we have to move any children onto the real NULL
1709 context */
1710 struct talloc_chunk *tc, *tc2;
1711 tc = talloc_chunk_from_ptr(null_context);
1712 for (tc2 = tc->child; tc2; tc2=tc2->next) {
1713 if (tc2->parent == tc) tc2->parent = NULL;
1714 if (tc2->prev == tc) tc2->prev = NULL;
1716 for (tc2 = tc->next; tc2; tc2=tc2->next) {
1717 if (tc2->parent == tc) tc2->parent = NULL;
1718 if (tc2->prev == tc) tc2->prev = NULL;
1720 tc->child = NULL;
1721 tc->next = NULL;
1723 talloc_free(null_context);
1724 null_context = NULL;
1728 enable leak reporting on exit
1730 _PUBLIC_ void talloc_enable_leak_report(void)
1732 talloc_enable_null_tracking();
1733 atexit(talloc_report_null);
1737 enable full leak reporting on exit
1739 _PUBLIC_ void talloc_enable_leak_report_full(void)
1741 talloc_enable_null_tracking();
1742 atexit(talloc_report_null_full);
1746 talloc and zero memory.
1748 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
1750 void *p = _talloc_named_const(ctx, size, name);
1752 if (p) {
1753 memset(p, '\0', size);
1756 return p;
1760 memdup with a talloc.
1762 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1764 void *newp = _talloc_named_const(t, size, name);
1766 if (likely(newp)) {
1767 memcpy(newp, p, size);
1770 return newp;
1773 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1775 char *ret;
1777 ret = (char *)__talloc(t, len + 1);
1778 if (unlikely(!ret)) return NULL;
1780 memcpy(ret, p, len);
1781 ret[len] = 0;
1783 _talloc_set_name_const(ret, ret);
1784 return ret;
1788 strdup with a talloc
1790 _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
1792 if (unlikely(!p)) return NULL;
1793 return __talloc_strlendup(t, p, strlen(p));
1797 strndup with a talloc
1799 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
1801 if (unlikely(!p)) return NULL;
1802 return __talloc_strlendup(t, p, strnlen(p, n));
1805 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1806 const char *a, size_t alen)
1808 char *ret;
1810 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1811 if (unlikely(!ret)) return NULL;
1813 /* append the string and the trailing \0 */
1814 memcpy(&ret[slen], a, alen);
1815 ret[slen+alen] = 0;
1817 _talloc_set_name_const(ret, ret);
1818 return ret;
1822 * Appends at the end of the string.
1824 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
1826 if (unlikely(!s)) {
1827 return talloc_strdup(NULL, a);
1830 if (unlikely(!a)) {
1831 return s;
1834 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1838 * Appends at the end of the talloc'ed buffer,
1839 * not the end of the string.
1841 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
1843 size_t slen;
1845 if (unlikely(!s)) {
1846 return talloc_strdup(NULL, a);
1849 if (unlikely(!a)) {
1850 return s;
1853 slen = talloc_get_size(s);
1854 if (likely(slen > 0)) {
1855 slen--;
1858 return __talloc_strlendup_append(s, slen, a, strlen(a));
1862 * Appends at the end of the string.
1864 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
1866 if (unlikely(!s)) {
1867 return talloc_strdup(NULL, a);
1870 if (unlikely(!a)) {
1871 return s;
1874 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1878 * Appends at the end of the talloc'ed buffer,
1879 * not the end of the string.
1881 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1883 size_t slen;
1885 if (unlikely(!s)) {
1886 return talloc_strdup(NULL, a);
1889 if (unlikely(!a)) {
1890 return s;
1893 slen = talloc_get_size(s);
1894 if (likely(slen > 0)) {
1895 slen--;
1898 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1901 #ifndef HAVE_VA_COPY
1902 #ifdef HAVE___VA_COPY
1903 #define va_copy(dest, src) __va_copy(dest, src)
1904 #else
1905 #define va_copy(dest, src) (dest) = (src)
1906 #endif
1907 #endif
1909 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1911 int len;
1912 char *ret;
1913 va_list ap2;
1914 char c;
1916 /* this call looks strange, but it makes it work on older solaris boxes */
1917 va_copy(ap2, ap);
1918 len = vsnprintf(&c, 1, fmt, ap2);
1919 va_end(ap2);
1920 if (unlikely(len < 0)) {
1921 return NULL;
1924 ret = (char *)__talloc(t, len+1);
1925 if (unlikely(!ret)) return NULL;
1927 va_copy(ap2, ap);
1928 vsnprintf(ret, len+1, fmt, ap2);
1929 va_end(ap2);
1931 _talloc_set_name_const(ret, ret);
1932 return ret;
1937 Perform string formatting, and return a pointer to newly allocated
1938 memory holding the result, inside a memory pool.
1940 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
1942 va_list ap;
1943 char *ret;
1945 va_start(ap, fmt);
1946 ret = talloc_vasprintf(t, fmt, ap);
1947 va_end(ap);
1948 return ret;
1951 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1952 const char *fmt, va_list ap)
1953 PRINTF_ATTRIBUTE(3,0);
1955 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1956 const char *fmt, va_list ap)
1958 ssize_t alen;
1959 va_list ap2;
1960 char c;
1962 va_copy(ap2, ap);
1963 alen = vsnprintf(&c, 1, fmt, ap2);
1964 va_end(ap2);
1966 if (alen <= 0) {
1967 /* Either the vsnprintf failed or the format resulted in
1968 * no characters being formatted. In the former case, we
1969 * ought to return NULL, in the latter we ought to return
1970 * the original string. Most current callers of this
1971 * function expect it to never return NULL.
1973 return s;
1976 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1977 if (!s) return NULL;
1979 va_copy(ap2, ap);
1980 vsnprintf(s + slen, alen + 1, fmt, ap2);
1981 va_end(ap2);
1983 _talloc_set_name_const(s, s);
1984 return s;
1988 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1989 * and return @p s, which may have moved. Good for gradually
1990 * accumulating output into a string buffer. Appends at the end
1991 * of the string.
1993 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1995 if (unlikely(!s)) {
1996 return talloc_vasprintf(NULL, fmt, ap);
1999 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
2003 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2004 * and return @p s, which may have moved. Always appends at the
2005 * end of the talloc'ed buffer, not the end of the string.
2007 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
2009 size_t slen;
2011 if (unlikely(!s)) {
2012 return talloc_vasprintf(NULL, fmt, ap);
2015 slen = talloc_get_size(s);
2016 if (likely(slen > 0)) {
2017 slen--;
2020 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
2024 Realloc @p s to append the formatted result of @p fmt and return @p
2025 s, which may have moved. Good for gradually accumulating output
2026 into a string buffer.
2028 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
2030 va_list ap;
2032 va_start(ap, fmt);
2033 s = talloc_vasprintf_append(s, fmt, ap);
2034 va_end(ap);
2035 return s;
2039 Realloc @p s to append the formatted result of @p fmt and return @p
2040 s, which may have moved. Good for gradually accumulating output
2041 into a buffer.
2043 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
2045 va_list ap;
2047 va_start(ap, fmt);
2048 s = talloc_vasprintf_append_buffer(s, fmt, ap);
2049 va_end(ap);
2050 return s;
2054 alloc an array, checking for integer overflow in the array size
2056 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2058 if (count >= MAX_TALLOC_SIZE/el_size) {
2059 return NULL;
2061 return _talloc_named_const(ctx, el_size * count, name);
2065 alloc an zero array, checking for integer overflow in the array size
2067 _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2069 if (count >= MAX_TALLOC_SIZE/el_size) {
2070 return NULL;
2072 return _talloc_zero(ctx, el_size * count, name);
2076 realloc an array, checking for integer overflow in the array size
2078 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
2080 if (count >= MAX_TALLOC_SIZE/el_size) {
2081 return NULL;
2083 return _talloc_realloc(ctx, ptr, el_size * count, name);
2087 a function version of talloc_realloc(), so it can be passed as a function pointer
2088 to libraries that want a realloc function (a realloc function encapsulates
2089 all the basic capabilities of an allocation library, which is why this is useful)
2091 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
2093 return _talloc_realloc(context, ptr, size, NULL);
2097 static int talloc_autofree_destructor(void *ptr)
2099 autofree_context = NULL;
2100 return 0;
2103 static void talloc_autofree(void)
2105 talloc_free(autofree_context);
2109 return a context which will be auto-freed on exit
2110 this is useful for reducing the noise in leak reports
2112 _PUBLIC_ void *talloc_autofree_context(void)
2114 if (autofree_context == NULL) {
2115 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
2116 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
2117 atexit(talloc_autofree);
2119 return autofree_context;
2122 _PUBLIC_ size_t talloc_get_size(const void *context)
2124 struct talloc_chunk *tc;
2126 if (context == NULL) {
2127 context = null_context;
2129 if (context == NULL) {
2130 return 0;
2133 tc = talloc_chunk_from_ptr(context);
2135 return tc->size;
2139 find a parent of this context that has the given name, if any
2141 _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
2143 struct talloc_chunk *tc;
2145 if (context == NULL) {
2146 return NULL;
2149 tc = talloc_chunk_from_ptr(context);
2150 while (tc) {
2151 if (tc->name && strcmp(tc->name, name) == 0) {
2152 return TC_PTR_FROM_CHUNK(tc);
2154 while (tc && tc->prev) tc = tc->prev;
2155 if (tc) {
2156 tc = tc->parent;
2159 return NULL;
2163 show the parentage of a context
2165 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
2167 struct talloc_chunk *tc;
2169 if (context == NULL) {
2170 fprintf(file, "talloc no parents for NULL\n");
2171 return;
2174 tc = talloc_chunk_from_ptr(context);
2175 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
2176 while (tc) {
2177 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
2178 while (tc && tc->prev) tc = tc->prev;
2179 if (tc) {
2180 tc = tc->parent;
2183 fflush(file);
2187 return 1 if ptr is a parent of context
2189 static int _talloc_is_parent(const void *context, const void *ptr, int depth)
2191 struct talloc_chunk *tc;
2193 if (context == NULL) {
2194 return 0;
2197 tc = talloc_chunk_from_ptr(context);
2198 while (tc && depth > 0) {
2199 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2200 while (tc && tc->prev) tc = tc->prev;
2201 if (tc) {
2202 tc = tc->parent;
2203 depth--;
2206 return 0;
2210 return 1 if ptr is a parent of context
2212 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2214 return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);