2 Samba Unix SMB/CIFS implementation.
4 Samba trivial allocation library - new interface
6 NOTE: Please read talloc_guide.txt for full documentation
8 Copyright (C) Andrew Tridgell 2004
9 Copyright (C) Stefan Metzmacher 2006
11 ** NOTE! The following LGPL license applies to the talloc
12 ** library. This does NOT imply that all of Samba is released
15 This library is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Lesser General Public
17 License as published by the Free Software Foundation; either
18 version 3 of the License, or (at your option) any later version.
20 This library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Lesser General Public License for more details.
25 You should have received a copy of the GNU Lesser General Public
26 License along with this library; if not, see <http://www.gnu.org/licenses/>.
30 inspired by http://swapped.cc/halloc/
36 #ifdef TALLOC_BUILD_VERSION_MAJOR
37 #if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)
38 #error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"
42 #ifdef TALLOC_BUILD_VERSION_MINOR
43 #if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)
44 #error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"
48 /* Special macros that are no-ops except when run under Valgrind on
49 * x86. They've moved a little bit from valgrind 1.0.4 to 1.9.4 */
50 #ifdef HAVE_VALGRIND_MEMCHECK_H
51 /* memcheck.h includes valgrind.h */
52 #include <valgrind/memcheck.h>
53 #elif defined(HAVE_VALGRIND_H)
57 /* use this to force every realloc to change the pointer, to stress test
58 code that might not cope */
59 #define ALWAYS_REALLOC 0
62 #define MAX_TALLOC_SIZE 0x10000000
63 #define TALLOC_MAGIC_BASE 0xe814ec70
64 #define TALLOC_MAGIC ( \
66 (TALLOC_VERSION_MAJOR << 12) + \
67 (TALLOC_VERSION_MINOR << 4) \
70 #define TALLOC_FLAG_FREE 0x01
71 #define TALLOC_FLAG_LOOP 0x02
72 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
73 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
74 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
76 /* by default we abort when given a bad pointer (such as when talloc_free() is called
77 on a pointer that came from malloc() */
79 #define TALLOC_ABORT(reason) abort()
82 #ifndef discard_const_p
83 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
84 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
86 # define discard_const_p(type, ptr) ((type *)(ptr))
90 /* these macros gain us a few percent of speed on gcc */
92 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
93 as its first argument */
95 #define likely(x) __builtin_expect(!!(x), 1)
98 #define unlikely(x) __builtin_expect(!!(x), 0)
102 #define likely(x) (x)
105 #define unlikely(x) (x)
109 /* this null_context is only used if talloc_enable_leak_report() or
110 talloc_enable_leak_report_full() is called, otherwise it remains
113 static void *null_context;
114 static void *autofree_context;
116 /* used to enable fill of memory on free, which can be useful for
117 * catching use after free errors when valgrind is too slow
125 #define TALLOC_FILL_ENV "TALLOC_FREE_FILL"
128 * do not wipe the header, to allow the
129 * double-free logic to still work
131 #define TC_INVALIDATE_FULL_FILL_CHUNK(_tc) do { \
132 if (unlikely(talloc_fill.enabled)) { \
133 size_t _flen = (_tc)->size; \
134 char *_fptr = TC_PTR_FROM_CHUNK(_tc); \
135 memset(_fptr, talloc_fill.fill_value, _flen); \
139 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
140 /* Mark the whole chunk as not accessable */
141 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { \
142 size_t _flen = TC_HDR_SIZE + (_tc)->size; \
143 char *_fptr = (char *)(_tc); \
144 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
147 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { } while (0)
150 #define TC_INVALIDATE_FULL_CHUNK(_tc) do { \
151 TC_INVALIDATE_FULL_FILL_CHUNK(_tc); \
152 TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc); \
155 #define TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
156 if (unlikely(talloc_fill.enabled)) { \
157 size_t _flen = (_tc)->size - (_new_size); \
158 char *_fptr = TC_PTR_FROM_CHUNK(_tc); \
159 _fptr += (_new_size); \
160 memset(_fptr, talloc_fill.fill_value, _flen); \
164 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
165 /* Mark the unused bytes not accessable */
166 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
167 size_t _flen = (_tc)->size - (_new_size); \
168 char *_fptr = TC_PTR_FROM_CHUNK(_tc); \
169 _fptr += (_new_size); \
170 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
173 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
176 #define TC_INVALIDATE_SHRINK_CHUNK(_tc, _new_size) do { \
177 TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size); \
178 TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
181 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
182 /* Mark the new bytes as undefined */
183 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { \
184 size_t _old_used = TC_HDR_SIZE + (_tc)->size; \
185 size_t _new_used = TC_HDR_SIZE + (_new_size); \
186 size_t _flen = _new_used - _old_used; \
187 char *_fptr = _old_used + (char *)(_tc); \
188 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
191 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
194 #define TC_UNDEFINE_GROW_CHUNK(_tc, _new_size) do { \
195 TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
198 struct talloc_reference_handle {
199 struct talloc_reference_handle *next, *prev;
201 const char *location;
204 typedef int (*talloc_destructor_t)(void *);
206 struct talloc_chunk {
207 struct talloc_chunk *next, *prev;
208 struct talloc_chunk *parent, *child;
209 struct talloc_reference_handle *refs;
210 talloc_destructor_t destructor;
216 * "pool" has dual use:
218 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
219 * marks the end of the currently allocated area.
221 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
222 * is a pointer to the struct talloc_chunk of the pool that it was
223 * allocated from. This way children can quickly find the pool to chew
229 /* 16 byte alignment seems to keep everyone happy */
230 #define TC_ALIGN16(s) (((s)+15)&~15)
231 #define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
232 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
234 _PUBLIC_ int talloc_version_major(void)
236 return TALLOC_VERSION_MAJOR;
239 _PUBLIC_ int talloc_version_minor(void)
241 return TALLOC_VERSION_MINOR;
244 static void (*talloc_log_fn)(const char *message);
246 _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
248 talloc_log_fn = log_fn;
251 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
252 static void talloc_log(const char *fmt, ...)
257 if (!talloc_log_fn) {
262 message = talloc_vasprintf(NULL, fmt, ap);
265 talloc_log_fn(message);
266 talloc_free(message);
269 static void talloc_log_stderr(const char *message)
271 fprintf(stderr, "%s", message);
274 _PUBLIC_ void talloc_set_log_stderr(void)
276 talloc_set_log_fn(talloc_log_stderr);
279 static void (*talloc_abort_fn)(const char *reason);
281 _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
283 talloc_abort_fn = abort_fn;
286 static void talloc_abort(const char *reason)
288 talloc_log("%s\n", reason);
290 if (!talloc_abort_fn) {
291 TALLOC_ABORT(reason);
294 talloc_abort_fn(reason);
297 static void talloc_abort_magic(unsigned magic)
299 unsigned striped = magic - TALLOC_MAGIC_BASE;
300 unsigned major = (striped & 0xFFFFF000) >> 12;
301 unsigned minor = (striped & 0x00000FF0) >> 4;
302 talloc_log("Bad talloc magic[0x%08X/%u/%u] expected[0x%08X/%u/%u]\n",
304 TALLOC_MAGIC, TALLOC_VERSION_MAJOR, TALLOC_VERSION_MINOR);
305 talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
308 static void talloc_abort_access_after_free(void)
310 talloc_abort("Bad talloc magic value - access after free");
313 static void talloc_abort_unknown_value(void)
315 talloc_abort("Bad talloc magic value - unknown value");
318 /* panic if we get a bad magic value */
319 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
321 const char *pp = (const char *)ptr;
322 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
323 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
324 if ((tc->flags & (~0xFFF)) == TALLOC_MAGIC_BASE) {
325 talloc_abort_magic(tc->flags & (~0xF));
329 if (tc->flags & TALLOC_FLAG_FREE) {
330 talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
331 talloc_abort_access_after_free();
334 talloc_abort_unknown_value();
341 /* hook into the front of the list */
342 #define _TLIST_ADD(list, p) \
346 (p)->next = (p)->prev = NULL; \
348 (list)->prev = (p); \
349 (p)->next = (list); \
355 /* remove an element from a list - element doesn't have to be in list. */
356 #define _TLIST_REMOVE(list, p) \
358 if ((p) == (list)) { \
359 (list) = (p)->next; \
360 if (list) (list)->prev = NULL; \
362 if ((p)->prev) (p)->prev->next = (p)->next; \
363 if ((p)->next) (p)->next->prev = (p)->prev; \
365 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
370 return the parent chunk of a pointer
372 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
374 struct talloc_chunk *tc;
376 if (unlikely(ptr == NULL)) {
380 tc = talloc_chunk_from_ptr(ptr);
381 while (tc->prev) tc=tc->prev;
386 _PUBLIC_ void *talloc_parent(const void *ptr)
388 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
389 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
395 _PUBLIC_ const char *talloc_parent_name(const void *ptr)
397 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
398 return tc? tc->name : NULL;
402 A pool carries an in-pool object count count in the first 16 bytes.
403 bytes. This is done to support talloc_steal() to a parent outside of the
404 pool. The count includes the pool itself, so a talloc_free() on a pool will
405 only destroy the pool if the count has dropped to zero. A talloc_free() of a
406 pool member will reduce the count, and eventually also call free(3) on the
409 The object count is not put into "struct talloc_chunk" because it is only
410 relevant for talloc pools and the alignment to 16 bytes would increase the
411 memory footprint of each talloc chunk by those 16 bytes.
414 #define TALLOC_POOL_HDR_SIZE 16
416 #define TC_POOL_SPACE_LEFT(_pool_tc) \
417 PTR_DIFF(TC_HDR_SIZE + (_pool_tc)->size + (char *)(_pool_tc), \
420 #define TC_POOL_FIRST_CHUNK(_pool_tc) \
421 ((void *)(TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE + (char *)(_pool_tc)))
423 #define TC_POOLMEM_CHUNK_SIZE(_tc) \
424 TC_ALIGN16(TC_HDR_SIZE + (_tc)->size)
426 #define TC_POOLMEM_NEXT_CHUNK(_tc) \
427 ((void *)(TC_POOLMEM_CHUNK_SIZE(tc) + (char*)(_tc)))
429 /* Mark the whole remaining pool as not accessable */
430 #define TC_INVALIDATE_FILL_POOL(_pool_tc) do { \
431 if (unlikely(talloc_fill.enabled)) { \
432 size_t _flen = TC_POOL_SPACE_LEFT(_pool_tc); \
433 char *_fptr = (_pool_tc)->pool; \
434 memset(_fptr, talloc_fill.fill_value, _flen); \
438 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
439 /* Mark the whole remaining pool as not accessable */
440 #define TC_INVALIDATE_VALGRIND_POOL(_pool_tc) do { \
441 size_t _flen = TC_POOL_SPACE_LEFT(_pool_tc); \
442 char *_fptr = (_pool_tc)->pool; \
443 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
446 #define TC_INVALIDATE_VALGRIND_POOL(_pool_tc) do { } while (0)
449 #define TC_INVALIDATE_POOL(_pool_tc) do { \
450 TC_INVALIDATE_FILL_POOL(_pool_tc); \
451 TC_INVALIDATE_VALGRIND_POOL(_pool_tc); \
454 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
456 return (unsigned int *)((char *)tc + TC_HDR_SIZE);
463 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
466 struct talloc_chunk *pool_ctx = NULL;
468 struct talloc_chunk *result;
471 if (parent == NULL) {
475 if (parent->flags & TALLOC_FLAG_POOL) {
478 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
479 pool_ctx = (struct talloc_chunk *)parent->pool;
482 if (pool_ctx == NULL) {
486 space_left = TC_POOL_SPACE_LEFT(pool_ctx);
489 * Align size to 16 bytes
491 chunk_size = TC_ALIGN16(size);
493 if (space_left < chunk_size) {
497 result = (struct talloc_chunk *)pool_ctx->pool;
499 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
500 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
503 pool_ctx->pool = (void *)((char *)result + chunk_size);
505 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
506 result->pool = pool_ctx;
508 *talloc_pool_objectcount(pool_ctx) += 1;
514 Allocate a bit of memory as a child of an existing pointer
516 static inline void *__talloc(const void *context, size_t size)
518 struct talloc_chunk *tc = NULL;
520 if (unlikely(context == NULL)) {
521 context = null_context;
524 if (unlikely(size >= MAX_TALLOC_SIZE)) {
528 if (context != NULL) {
529 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
534 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
535 if (unlikely(tc == NULL)) return NULL;
536 tc->flags = TALLOC_MAGIC;
541 tc->destructor = NULL;
546 if (likely(context)) {
547 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
550 parent->child->parent = NULL;
551 tc->next = parent->child;
560 tc->next = tc->prev = tc->parent = NULL;
563 return TC_PTR_FROM_CHUNK(tc);
567 * Create a talloc pool
570 _PUBLIC_ void *talloc_pool(const void *context, size_t size)
572 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
573 struct talloc_chunk *tc;
575 if (unlikely(result == NULL)) {
579 tc = talloc_chunk_from_ptr(result);
581 tc->flags |= TALLOC_FLAG_POOL;
582 tc->pool = TC_POOL_FIRST_CHUNK(tc);
584 *talloc_pool_objectcount(tc) = 1;
586 TC_INVALIDATE_POOL(tc);
592 setup a destructor to be called on free of a pointer
593 the destructor should return 0 on success, or -1 on failure.
594 if the destructor fails then the free is failed, and the memory can
595 be continued to be used
597 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
599 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
600 tc->destructor = destructor;
604 increase the reference count on a piece of memory.
606 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
608 if (unlikely(!talloc_reference(null_context, ptr))) {
615 helper for talloc_reference()
617 this is referenced by a function pointer and should not be inline
619 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
621 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
622 _TLIST_REMOVE(ptr_tc->refs, handle);
627 more efficient way to add a name to a pointer - the name must point to a
630 static inline void _talloc_set_name_const(const void *ptr, const char *name)
632 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
637 internal talloc_named_const()
639 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
643 ptr = __talloc(context, size);
644 if (unlikely(ptr == NULL)) {
648 _talloc_set_name_const(ptr, name);
654 make a secondary reference to a pointer, hanging off the given context.
655 the pointer remains valid until both the original caller and this given
658 the major use for this is when two different structures need to reference the
659 same underlying data, and you want to be able to free the two instances separately,
662 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
664 struct talloc_chunk *tc;
665 struct talloc_reference_handle *handle;
666 if (unlikely(ptr == NULL)) return NULL;
668 tc = talloc_chunk_from_ptr(ptr);
669 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
670 sizeof(struct talloc_reference_handle),
671 TALLOC_MAGIC_REFERENCE);
672 if (unlikely(handle == NULL)) return NULL;
674 /* note that we hang the destructor off the handle, not the
675 main context as that allows the caller to still setup their
676 own destructor on the context if they want to */
677 talloc_set_destructor(handle, talloc_reference_destructor);
678 handle->ptr = discard_const_p(void, ptr);
679 handle->location = location;
680 _TLIST_ADD(tc->refs, handle);
684 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
687 internal talloc_free call
689 static inline int _talloc_free_internal(void *ptr, const char *location)
691 struct talloc_chunk *tc;
693 if (unlikely(ptr == NULL)) {
697 /* possibly initialised the talloc fill value */
698 if (unlikely(!talloc_fill.initialised)) {
699 const char *fill = getenv(TALLOC_FILL_ENV);
701 talloc_fill.enabled = true;
702 talloc_fill.fill_value = strtoul(fill, NULL, 0);
704 talloc_fill.initialised = true;
707 tc = talloc_chunk_from_ptr(ptr);
709 if (unlikely(tc->refs)) {
711 /* check if this is a reference from a child or
712 * grandchild back to it's parent or grandparent
714 * in that case we need to remove the reference and
715 * call another instance of talloc_free() on the current
718 is_child = talloc_is_parent(tc->refs, ptr);
719 _talloc_free_internal(tc->refs, location);
721 return _talloc_free_internal(ptr, location);
726 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
727 /* we have a free loop - stop looping */
731 if (unlikely(tc->destructor)) {
732 talloc_destructor_t d = tc->destructor;
733 if (d == (talloc_destructor_t)-1) {
736 tc->destructor = (talloc_destructor_t)-1;
741 tc->destructor = NULL;
745 _TLIST_REMOVE(tc->parent->child, tc);
746 if (tc->parent->child) {
747 tc->parent->child->parent = tc->parent;
750 if (tc->prev) tc->prev->next = tc->next;
751 if (tc->next) tc->next->prev = tc->prev;
754 tc->flags |= TALLOC_FLAG_LOOP;
757 /* we need to work out who will own an abandoned child
758 if it cannot be freed. In priority order, the first
759 choice is owner of any remaining reference to this
760 pointer, the second choice is our parent, and the
761 final choice is the null context. */
762 void *child = TC_PTR_FROM_CHUNK(tc->child);
763 const void *new_parent = null_context;
764 struct talloc_chunk *old_parent = NULL;
765 if (unlikely(tc->child->refs)) {
766 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
767 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
769 /* finding the parent here is potentially quite
770 expensive, but the alternative, which is to change
771 talloc to always have a valid tc->parent pointer,
772 makes realloc more expensive where there are a
773 large number of children.
775 The reason we need the parent pointer here is that
776 if _talloc_free_internal() fails due to references
777 or a failing destructor we need to re-parent, but
778 the free call can invalidate the prev pointer.
780 if (new_parent == null_context && (tc->child->refs || tc->child->destructor)) {
781 old_parent = talloc_parent_chunk(ptr);
783 if (unlikely(_talloc_free_internal(child, location) == -1)) {
784 if (new_parent == null_context) {
785 struct talloc_chunk *p = old_parent;
786 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
788 _talloc_steal_internal(new_parent, child);
792 tc->flags |= TALLOC_FLAG_FREE;
794 /* we mark the freed memory with where we called the free
795 * from. This means on a double free error we can report where
796 * the first free came from
800 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
801 struct talloc_chunk *pool;
802 void *next_tc = NULL;
803 unsigned int *pool_object_count;
805 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
808 pool = (struct talloc_chunk *)tc->pool;
809 next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
811 TC_INVALIDATE_FULL_CHUNK(tc);
814 pool_object_count = talloc_pool_objectcount(pool);
816 if (unlikely(*pool_object_count == 0)) {
817 talloc_abort("Pool object count zero!");
821 *pool_object_count -= 1;
823 if (unlikely(*pool_object_count == 1)) {
825 * if there is just object left in the pool
826 * it means this is the pool itself and
827 * the rest is available for new objects
830 pool->pool = TC_POOL_FIRST_CHUNK(pool);
831 TC_INVALIDATE_POOL(pool);
832 } else if (unlikely(*pool_object_count == 0)) {
833 TC_INVALIDATE_FULL_CHUNK(pool);
835 } else if (pool->pool == next_tc) {
837 * if pool->pool still points to end of
838 * 'tc' (which is stored in the 'next_tc' variable),
839 * we can reclaim the memory of 'tc'.
844 TC_INVALIDATE_FULL_CHUNK(tc);
851 move a lump of memory from one talloc context to another return the
852 ptr on success, or NULL if it could not be transferred.
853 passing NULL as ptr will always return NULL with no side effects.
855 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
857 struct talloc_chunk *tc, *new_tc;
859 if (unlikely(!ptr)) {
863 if (unlikely(new_ctx == NULL)) {
864 new_ctx = null_context;
867 tc = talloc_chunk_from_ptr(ptr);
869 if (unlikely(new_ctx == NULL)) {
871 _TLIST_REMOVE(tc->parent->child, tc);
872 if (tc->parent->child) {
873 tc->parent->child->parent = tc->parent;
876 if (tc->prev) tc->prev->next = tc->next;
877 if (tc->next) tc->next->prev = tc->prev;
880 tc->parent = tc->next = tc->prev = NULL;
881 return discard_const_p(void, ptr);
884 new_tc = talloc_chunk_from_ptr(new_ctx);
886 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
887 return discard_const_p(void, ptr);
891 _TLIST_REMOVE(tc->parent->child, tc);
892 if (tc->parent->child) {
893 tc->parent->child->parent = tc->parent;
896 if (tc->prev) tc->prev->next = tc->next;
897 if (tc->next) tc->next->prev = tc->prev;
901 if (new_tc->child) new_tc->child->parent = NULL;
902 _TLIST_ADD(new_tc->child, tc);
904 return discard_const_p(void, ptr);
908 move a lump of memory from one talloc context to another return the
909 ptr on success, or NULL if it could not be transferred.
910 passing NULL as ptr will always return NULL with no side effects.
912 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
914 struct talloc_chunk *tc;
916 if (unlikely(ptr == NULL)) {
920 tc = talloc_chunk_from_ptr(ptr);
922 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
923 struct talloc_reference_handle *h;
925 talloc_log("WARNING: talloc_steal with references at %s\n",
928 for (h=tc->refs; h; h=h->next) {
929 talloc_log("\treference at %s\n",
935 /* this test is probably too expensive to have on in the
936 normal build, but it useful for debugging */
937 if (talloc_is_parent(new_ctx, ptr)) {
938 talloc_log("WARNING: stealing into talloc child at %s\n", location);
942 return _talloc_steal_internal(new_ctx, ptr);
946 this is like a talloc_steal(), but you must supply the old
947 parent. This resolves the ambiguity in a talloc_steal() which is
948 called on a context that has more than one parent (via references)
950 The old parent can be either a reference or a parent
952 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
954 struct talloc_chunk *tc;
955 struct talloc_reference_handle *h;
957 if (unlikely(ptr == NULL)) {
961 if (old_parent == talloc_parent(ptr)) {
962 return _talloc_steal_internal(new_parent, ptr);
965 tc = talloc_chunk_from_ptr(ptr);
966 for (h=tc->refs;h;h=h->next) {
967 if (talloc_parent(h) == old_parent) {
968 if (_talloc_steal_internal(new_parent, h) != h) {
971 return discard_const_p(void, ptr);
975 /* it wasn't a parent */
980 remove a secondary reference to a pointer. This undo's what
981 talloc_reference() has done. The context and pointer arguments
982 must match those given to a talloc_reference()
984 static inline int talloc_unreference(const void *context, const void *ptr)
986 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
987 struct talloc_reference_handle *h;
989 if (unlikely(context == NULL)) {
990 context = null_context;
993 for (h=tc->refs;h;h=h->next) {
994 struct talloc_chunk *p = talloc_parent_chunk(h);
996 if (context == NULL) break;
997 } else if (TC_PTR_FROM_CHUNK(p) == context) {
1005 return _talloc_free_internal(h, __location__);
1009 remove a specific parent context from a pointer. This is a more
1010 controlled varient of talloc_free()
1012 _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
1014 struct talloc_chunk *tc_p, *new_p;
1021 if (context == NULL) {
1022 context = null_context;
1025 if (talloc_unreference(context, ptr) == 0) {
1029 if (context == NULL) {
1030 if (talloc_parent_chunk(ptr) != NULL) {
1034 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
1039 tc_p = talloc_chunk_from_ptr(ptr);
1041 if (tc_p->refs == NULL) {
1042 return _talloc_free_internal(ptr, __location__);
1045 new_p = talloc_parent_chunk(tc_p->refs);
1047 new_parent = TC_PTR_FROM_CHUNK(new_p);
1052 if (talloc_unreference(new_parent, ptr) != 0) {
1056 _talloc_steal_internal(new_parent, ptr);
1062 add a name to an existing pointer - va_list version
1064 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1066 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
1068 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1069 tc->name = talloc_vasprintf(ptr, fmt, ap);
1070 if (likely(tc->name)) {
1071 _talloc_set_name_const(tc->name, ".name");
1077 add a name to an existing pointer
1079 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
1084 name = talloc_set_name_v(ptr, fmt, ap);
1091 create a named talloc pointer. Any talloc pointer can be named, and
1092 talloc_named() operates just like talloc() except that it allows you
1093 to name the pointer.
1095 _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
1101 ptr = __talloc(context, size);
1102 if (unlikely(ptr == NULL)) return NULL;
1105 name = talloc_set_name_v(ptr, fmt, ap);
1108 if (unlikely(name == NULL)) {
1109 _talloc_free_internal(ptr, __location__);
1117 return the name of a talloc ptr, or "UNNAMED"
1119 _PUBLIC_ const char *talloc_get_name(const void *ptr)
1121 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1122 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
1123 return ".reference";
1125 if (likely(tc->name)) {
1133 check if a pointer has the given name. If it does, return the pointer,
1134 otherwise return NULL
1136 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
1139 if (unlikely(ptr == NULL)) return NULL;
1140 pname = talloc_get_name(ptr);
1141 if (likely(pname == name || strcmp(pname, name) == 0)) {
1142 return discard_const_p(void, ptr);
1147 static void talloc_abort_type_missmatch(const char *location,
1149 const char *expected)
1153 reason = talloc_asprintf(NULL,
1154 "%s: Type mismatch: name[%s] expected[%s]",
1159 reason = "Type mismatch";
1162 talloc_abort(reason);
1165 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
1169 if (unlikely(ptr == NULL)) {
1170 talloc_abort_type_missmatch(location, NULL, name);
1174 pname = talloc_get_name(ptr);
1175 if (likely(pname == name || strcmp(pname, name) == 0)) {
1176 return discard_const_p(void, ptr);
1179 talloc_abort_type_missmatch(location, pname, name);
1184 this is for compatibility with older versions of talloc
1186 _PUBLIC_ void *talloc_init(const char *fmt, ...)
1192 ptr = __talloc(NULL, 0);
1193 if (unlikely(ptr == NULL)) return NULL;
1196 name = talloc_set_name_v(ptr, fmt, ap);
1199 if (unlikely(name == NULL)) {
1200 _talloc_free_internal(ptr, __location__);
1208 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1209 should probably not be used in new code. It's in here to keep the talloc
1210 code consistent across Samba 3 and 4.
1212 _PUBLIC_ void talloc_free_children(void *ptr)
1214 struct talloc_chunk *tc;
1216 if (unlikely(ptr == NULL)) {
1220 tc = talloc_chunk_from_ptr(ptr);
1223 /* we need to work out who will own an abandoned child
1224 if it cannot be freed. In priority order, the first
1225 choice is owner of any remaining reference to this
1226 pointer, the second choice is our parent, and the
1227 final choice is the null context. */
1228 void *child = TC_PTR_FROM_CHUNK(tc->child);
1229 const void *new_parent = null_context;
1230 if (unlikely(tc->child->refs)) {
1231 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1232 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1234 if (unlikely(talloc_free(child) == -1)) {
1235 if (new_parent == null_context) {
1236 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1237 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1239 _talloc_steal_internal(new_parent, child);
1245 Allocate a bit of memory as a child of an existing pointer
1247 _PUBLIC_ void *_talloc(const void *context, size_t size)
1249 return __talloc(context, size);
1253 externally callable talloc_set_name_const()
1255 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1257 _talloc_set_name_const(ptr, name);
1261 create a named talloc pointer. Any talloc pointer can be named, and
1262 talloc_named() operates just like talloc() except that it allows you
1263 to name the pointer.
1265 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1267 return _talloc_named_const(context, size, name);
1271 free a talloc pointer. This also frees all child pointers of this
1274 return 0 if the memory is actually freed, otherwise -1. The memory
1275 will not be freed if the ref_count is > 1 or the destructor (if
1276 any) returns non-zero
1278 _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1280 struct talloc_chunk *tc;
1282 if (unlikely(ptr == NULL)) {
1286 tc = talloc_chunk_from_ptr(ptr);
1288 if (unlikely(tc->refs != NULL)) {
1289 struct talloc_reference_handle *h;
1291 if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1292 /* in this case we do know which parent should
1293 get this pointer, as there is really only
1295 return talloc_unlink(null_context, ptr);
1298 talloc_log("ERROR: talloc_free with references at %s\n",
1301 for (h=tc->refs; h; h=h->next) {
1302 talloc_log("\treference at %s\n",
1308 return _talloc_free_internal(ptr, location);
1314 A talloc version of realloc. The context argument is only used if
1317 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1319 struct talloc_chunk *tc;
1321 bool malloced = false;
1322 struct talloc_chunk *pool_tc = NULL;
1324 /* size zero is equivalent to free() */
1325 if (unlikely(size == 0)) {
1326 talloc_unlink(context, ptr);
1330 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1334 /* realloc(NULL) is equivalent to malloc() */
1336 return _talloc_named_const(context, size, name);
1339 tc = talloc_chunk_from_ptr(ptr);
1341 /* don't allow realloc on referenced pointers */
1342 if (unlikely(tc->refs)) {
1346 /* don't let anybody try to realloc a talloc_pool */
1347 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1351 /* don't let anybody try to realloc a talloc_pool */
1352 if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
1353 pool_tc = (struct talloc_chunk *)tc->pool;
1356 #if (ALWAYS_REALLOC == 0)
1357 /* don't shrink if we have less than 1k to gain */
1358 if (size < tc->size) {
1360 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1361 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1363 if (next_tc == pool_tc->pool) {
1364 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1367 } else if ((tc->size - size) < 1024) {
1368 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1369 /* do not shrink if we have less than 1k to gain */
1373 } else if (tc->size == size) {
1375 * do not change the pointer if it is exactly
1382 /* by resetting magic we catch users of the old memory */
1383 tc->flags |= TALLOC_FLAG_FREE;
1387 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1388 *talloc_pool_objectcount(pool_tc) -= 1;
1390 if (new_ptr == NULL) {
1391 new_ptr = malloc(TC_HDR_SIZE+size);
1396 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1397 TC_INVALIDATE_FULL_CHUNK(tc);
1400 new_ptr = malloc(size + TC_HDR_SIZE);
1402 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1408 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1409 size_t old_chunk_size = TC_POOLMEM_CHUNK_SIZE(tc);
1410 size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
1411 size_t space_needed;
1414 if (*talloc_pool_objectcount(pool_tc) == 2) {
1416 * optimize for the case where 'tc' is the only
1417 * chunk in the pool.
1419 space_needed = new_chunk_size;
1420 space_left = pool_tc->size - TALLOC_POOL_HDR_SIZE;
1422 if (space_left >= space_needed) {
1423 size_t old_used = TC_HDR_SIZE + tc->size;
1424 size_t new_used = TC_HDR_SIZE + size;
1425 pool_tc->pool = TC_POOL_FIRST_CHUNK(pool_tc);
1426 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
1428 * we need to prepare the memmove into
1429 * the unaccessable area.
1432 size_t diff = PTR_DIFF(tc, pool_tc->pool);
1433 size_t flen = MIN(diff, old_used);
1434 char *fptr = (char *)pool_tc->pool;
1435 VALGRIND_MAKE_MEM_UNDEFINED(fptr, flen);
1438 memmove(pool_tc->pool, tc, old_used);
1439 new_ptr = pool_tc->pool;
1441 TC_UNDEFINE_GROW_CHUNK(tc, size);
1444 * first we do not align the pool pointer
1445 * because we want to invalidate the padding
1448 pool_tc->pool = new_used + (char *)new_ptr;
1449 TC_INVALIDATE_POOL(pool_tc);
1451 /* now the aligned pointer */
1452 pool_tc->pool = new_chunk_size + (char *)new_ptr;
1459 if (new_chunk_size == old_chunk_size) {
1460 TC_UNDEFINE_GROW_CHUNK(tc, size);
1461 tc->flags &= ~TALLOC_FLAG_FREE;
1466 if (next_tc == pool_tc->pool) {
1468 * optimize for the case where 'tc' is the last
1469 * chunk in the pool.
1471 space_needed = new_chunk_size - old_chunk_size;
1472 space_left = TC_POOL_SPACE_LEFT(pool_tc);
1474 if (space_left >= space_needed) {
1475 TC_UNDEFINE_GROW_CHUNK(tc, size);
1476 tc->flags &= ~TALLOC_FLAG_FREE;
1478 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1483 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1484 *talloc_pool_objectcount(pool_tc) -= 1;
1486 if (new_ptr == NULL) {
1487 new_ptr = malloc(TC_HDR_SIZE+size);
1492 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1493 TC_INVALIDATE_FULL_CHUNK(tc);
1495 if (*talloc_pool_objectcount(pool_tc) == 1) {
1497 * If the pool is empty now reclaim everything.
1499 pool_tc->pool = TC_POOL_FIRST_CHUNK(pool_tc);
1500 TC_INVALIDATE_POOL(pool_tc);
1501 } else if (next_tc == pool_tc->pool) {
1503 * If it was reallocated and tc was the last
1504 * chunk, we can reclaim the memory of tc.
1511 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1515 if (unlikely(!new_ptr)) {
1516 tc->flags &= ~TALLOC_FLAG_FREE;
1520 tc = (struct talloc_chunk *)new_ptr;
1521 tc->flags &= ~TALLOC_FLAG_FREE;
1523 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1526 tc->parent->child = tc;
1529 tc->child->parent = tc;
1533 tc->prev->next = tc;
1536 tc->next->prev = tc;
1540 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1542 return TC_PTR_FROM_CHUNK(tc);
1546 a wrapper around talloc_steal() for situations where you are moving a pointer
1547 between two structures, and want the old pointer to be set to NULL
1549 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
1551 const void **pptr = discard_const_p(const void *,_pptr);
1552 void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1558 return the total size of a talloc pool (subtree)
1560 _PUBLIC_ size_t talloc_total_size(const void *ptr)
1563 struct talloc_chunk *c, *tc;
1572 tc = talloc_chunk_from_ptr(ptr);
1574 if (tc->flags & TALLOC_FLAG_LOOP) {
1578 tc->flags |= TALLOC_FLAG_LOOP;
1580 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1583 for (c=tc->child;c;c=c->next) {
1584 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1587 tc->flags &= ~TALLOC_FLAG_LOOP;
1593 return the total number of blocks in a talloc pool (subtree)
1595 _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
1598 struct talloc_chunk *c, *tc;
1607 tc = talloc_chunk_from_ptr(ptr);
1609 if (tc->flags & TALLOC_FLAG_LOOP) {
1613 tc->flags |= TALLOC_FLAG_LOOP;
1616 for (c=tc->child;c;c=c->next) {
1617 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1620 tc->flags &= ~TALLOC_FLAG_LOOP;
1626 return the number of external references to a pointer
1628 _PUBLIC_ size_t talloc_reference_count(const void *ptr)
1630 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1631 struct talloc_reference_handle *h;
1634 for (h=tc->refs;h;h=h->next) {
1641 report on memory usage by all children of a pointer, giving a full tree view
1643 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1644 void (*callback)(const void *ptr,
1645 int depth, int max_depth,
1647 void *private_data),
1650 struct talloc_chunk *c, *tc;
1655 if (ptr == NULL) return;
1657 tc = talloc_chunk_from_ptr(ptr);
1659 if (tc->flags & TALLOC_FLAG_LOOP) {
1663 callback(ptr, depth, max_depth, 0, private_data);
1665 if (max_depth >= 0 && depth >= max_depth) {
1669 tc->flags |= TALLOC_FLAG_LOOP;
1670 for (c=tc->child;c;c=c->next) {
1671 if (c->name == TALLOC_MAGIC_REFERENCE) {
1672 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1673 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1675 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1678 tc->flags &= ~TALLOC_FLAG_LOOP;
1681 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1683 const char *name = talloc_get_name(ptr);
1684 FILE *f = (FILE *)_f;
1687 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1692 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1693 (max_depth < 0 ? "full " :""), name,
1694 (unsigned long)talloc_total_size(ptr),
1695 (unsigned long)talloc_total_blocks(ptr));
1699 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1702 (unsigned long)talloc_total_size(ptr),
1703 (unsigned long)talloc_total_blocks(ptr),
1704 (int)talloc_reference_count(ptr), ptr);
1707 fprintf(f, "content: ");
1708 if (talloc_total_size(ptr)) {
1709 int tot = talloc_total_size(ptr);
1712 for (i = 0; i < tot; i++) {
1713 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1714 fprintf(f, "%c", ((char *)ptr)[i]);
1716 fprintf(f, "~%02x", ((char *)ptr)[i]);
1725 report on memory usage by all children of a pointer, giving a full tree view
1727 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1730 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1736 report on memory usage by all children of a pointer, giving a full tree view
1738 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
1740 talloc_report_depth_file(ptr, 0, -1, f);
1744 report on memory usage by all children of a pointer
1746 _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
1748 talloc_report_depth_file(ptr, 0, 1, f);
1752 report on any memory hanging off the null context
1754 static void talloc_report_null(void)
1756 if (talloc_total_size(null_context) != 0) {
1757 talloc_report(null_context, stderr);
1762 report on any memory hanging off the null context
1764 static void talloc_report_null_full(void)
1766 if (talloc_total_size(null_context) != 0) {
1767 talloc_report_full(null_context, stderr);
1772 enable tracking of the NULL context
1774 _PUBLIC_ void talloc_enable_null_tracking(void)
1776 if (null_context == NULL) {
1777 null_context = _talloc_named_const(NULL, 0, "null_context");
1778 if (autofree_context != NULL) {
1779 talloc_reparent(NULL, null_context, autofree_context);
1785 enable tracking of the NULL context, not moving the autofree context
1786 into the NULL context. This is needed for the talloc testsuite
1788 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
1790 if (null_context == NULL) {
1791 null_context = _talloc_named_const(NULL, 0, "null_context");
1796 disable tracking of the NULL context
1798 _PUBLIC_ void talloc_disable_null_tracking(void)
1800 if (null_context != NULL) {
1801 /* we have to move any children onto the real NULL
1803 struct talloc_chunk *tc, *tc2;
1804 tc = talloc_chunk_from_ptr(null_context);
1805 for (tc2 = tc->child; tc2; tc2=tc2->next) {
1806 if (tc2->parent == tc) tc2->parent = NULL;
1807 if (tc2->prev == tc) tc2->prev = NULL;
1809 for (tc2 = tc->next; tc2; tc2=tc2->next) {
1810 if (tc2->parent == tc) tc2->parent = NULL;
1811 if (tc2->prev == tc) tc2->prev = NULL;
1816 talloc_free(null_context);
1817 null_context = NULL;
1821 enable leak reporting on exit
1823 _PUBLIC_ void talloc_enable_leak_report(void)
1825 talloc_enable_null_tracking();
1826 atexit(talloc_report_null);
1830 enable full leak reporting on exit
1832 _PUBLIC_ void talloc_enable_leak_report_full(void)
1834 talloc_enable_null_tracking();
1835 atexit(talloc_report_null_full);
1839 talloc and zero memory.
1841 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
1843 void *p = _talloc_named_const(ctx, size, name);
1846 memset(p, '\0', size);
1853 memdup with a talloc.
1855 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1857 void *newp = _talloc_named_const(t, size, name);
1860 memcpy(newp, p, size);
1866 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1870 ret = (char *)__talloc(t, len + 1);
1871 if (unlikely(!ret)) return NULL;
1873 memcpy(ret, p, len);
1876 _talloc_set_name_const(ret, ret);
1881 strdup with a talloc
1883 _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
1885 if (unlikely(!p)) return NULL;
1886 return __talloc_strlendup(t, p, strlen(p));
1890 strndup with a talloc
1892 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
1894 if (unlikely(!p)) return NULL;
1895 return __talloc_strlendup(t, p, strnlen(p, n));
1898 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1899 const char *a, size_t alen)
1903 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1904 if (unlikely(!ret)) return NULL;
1906 /* append the string and the trailing \0 */
1907 memcpy(&ret[slen], a, alen);
1910 _talloc_set_name_const(ret, ret);
1915 * Appends at the end of the string.
1917 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
1920 return talloc_strdup(NULL, a);
1927 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1931 * Appends at the end of the talloc'ed buffer,
1932 * not the end of the string.
1934 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
1939 return talloc_strdup(NULL, a);
1946 slen = talloc_get_size(s);
1947 if (likely(slen > 0)) {
1951 return __talloc_strlendup_append(s, slen, a, strlen(a));
1955 * Appends at the end of the string.
1957 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
1960 return talloc_strdup(NULL, a);
1967 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1971 * Appends at the end of the talloc'ed buffer,
1972 * not the end of the string.
1974 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1979 return talloc_strdup(NULL, a);
1986 slen = talloc_get_size(s);
1987 if (likely(slen > 0)) {
1991 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1994 #ifndef HAVE_VA_COPY
1995 #ifdef HAVE___VA_COPY
1996 #define va_copy(dest, src) __va_copy(dest, src)
1998 #define va_copy(dest, src) (dest) = (src)
2002 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
2009 /* this call looks strange, but it makes it work on older solaris boxes */
2011 len = vsnprintf(&c, 1, fmt, ap2);
2013 if (unlikely(len < 0)) {
2017 ret = (char *)__talloc(t, len+1);
2018 if (unlikely(!ret)) return NULL;
2021 vsnprintf(ret, len+1, fmt, ap2);
2024 _talloc_set_name_const(ret, ret);
2030 Perform string formatting, and return a pointer to newly allocated
2031 memory holding the result, inside a memory pool.
2033 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
2039 ret = talloc_vasprintf(t, fmt, ap);
2044 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2045 const char *fmt, va_list ap)
2046 PRINTF_ATTRIBUTE(3,0);
2048 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2049 const char *fmt, va_list ap)
2056 alen = vsnprintf(&c, 1, fmt, ap2);
2060 /* Either the vsnprintf failed or the format resulted in
2061 * no characters being formatted. In the former case, we
2062 * ought to return NULL, in the latter we ought to return
2063 * the original string. Most current callers of this
2064 * function expect it to never return NULL.
2069 s = talloc_realloc(NULL, s, char, slen + alen + 1);
2070 if (!s) return NULL;
2073 vsnprintf(s + slen, alen + 1, fmt, ap2);
2076 _talloc_set_name_const(s, s);
2081 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2082 * and return @p s, which may have moved. Good for gradually
2083 * accumulating output into a string buffer. Appends at the end
2086 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
2089 return talloc_vasprintf(NULL, fmt, ap);
2092 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
2096 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2097 * and return @p s, which may have moved. Always appends at the
2098 * end of the talloc'ed buffer, not the end of the string.
2100 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
2105 return talloc_vasprintf(NULL, fmt, ap);
2108 slen = talloc_get_size(s);
2109 if (likely(slen > 0)) {
2113 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
2117 Realloc @p s to append the formatted result of @p fmt and return @p
2118 s, which may have moved. Good for gradually accumulating output
2119 into a string buffer.
2121 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
2126 s = talloc_vasprintf_append(s, fmt, ap);
2132 Realloc @p s to append the formatted result of @p fmt and return @p
2133 s, which may have moved. Good for gradually accumulating output
2136 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
2141 s = talloc_vasprintf_append_buffer(s, fmt, ap);
2147 alloc an array, checking for integer overflow in the array size
2149 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2151 if (count >= MAX_TALLOC_SIZE/el_size) {
2154 return _talloc_named_const(ctx, el_size * count, name);
2158 alloc an zero array, checking for integer overflow in the array size
2160 _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2162 if (count >= MAX_TALLOC_SIZE/el_size) {
2165 return _talloc_zero(ctx, el_size * count, name);
2169 realloc an array, checking for integer overflow in the array size
2171 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
2173 if (count >= MAX_TALLOC_SIZE/el_size) {
2176 return _talloc_realloc(ctx, ptr, el_size * count, name);
2180 a function version of talloc_realloc(), so it can be passed as a function pointer
2181 to libraries that want a realloc function (a realloc function encapsulates
2182 all the basic capabilities of an allocation library, which is why this is useful)
2184 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
2186 return _talloc_realloc(context, ptr, size, NULL);
2190 static int talloc_autofree_destructor(void *ptr)
2192 autofree_context = NULL;
2196 static void talloc_autofree(void)
2198 talloc_free(autofree_context);
2202 return a context which will be auto-freed on exit
2203 this is useful for reducing the noise in leak reports
2205 _PUBLIC_ void *talloc_autofree_context(void)
2207 if (autofree_context == NULL) {
2208 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
2209 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
2210 atexit(talloc_autofree);
2212 return autofree_context;
2215 _PUBLIC_ size_t talloc_get_size(const void *context)
2217 struct talloc_chunk *tc;
2219 if (context == NULL) {
2220 context = null_context;
2222 if (context == NULL) {
2226 tc = talloc_chunk_from_ptr(context);
2232 find a parent of this context that has the given name, if any
2234 _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
2236 struct talloc_chunk *tc;
2238 if (context == NULL) {
2242 tc = talloc_chunk_from_ptr(context);
2244 if (tc->name && strcmp(tc->name, name) == 0) {
2245 return TC_PTR_FROM_CHUNK(tc);
2247 while (tc && tc->prev) tc = tc->prev;
2256 show the parentage of a context
2258 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
2260 struct talloc_chunk *tc;
2262 if (context == NULL) {
2263 fprintf(file, "talloc no parents for NULL\n");
2267 tc = talloc_chunk_from_ptr(context);
2268 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
2270 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
2271 while (tc && tc->prev) tc = tc->prev;
2280 return 1 if ptr is a parent of context
2282 static int _talloc_is_parent(const void *context, const void *ptr, int depth)
2284 struct talloc_chunk *tc;
2286 if (context == NULL) {
2290 tc = talloc_chunk_from_ptr(context);
2291 while (tc && depth > 0) {
2292 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2293 while (tc && tc->prev) tc = tc->prev;
2303 return 1 if ptr is a parent of context
2305 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2307 return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);