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 /* 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 ( \
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() */
70 #define TALLOC_ABORT(reason) abort()
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)))
77 # define discard_const_p(type, ptr) ((type *)(ptr))
81 /* these macros gain us a few percent of speed on gcc */
83 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
84 as its first argument */
86 #define likely(x) __builtin_expect(!!(x), 1)
89 #define unlikely(x) __builtin_expect(!!(x), 0)
96 #define unlikely(x) (x)
100 /* this null_context is only used if talloc_enable_leak_report() or
101 talloc_enable_leak_report_full() is called, otherwise it remains
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
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); \
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); \
138 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { } while (0)
141 #define TC_INVALIDATE_FULL_CHUNK(_tc) do { \
142 TC_INVALIDATE_FULL_FILL_CHUNK(_tc); \
143 TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc); \
146 #define TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
147 if (unlikely(talloc_fill.enabled)) { \
148 size_t _flen = (_tc)->size - (_new_size); \
149 char *_fptr = TC_PTR_FROM_CHUNK(_tc); \
150 _fptr += (_new_size); \
151 memset(_fptr, talloc_fill.fill_value, _flen); \
155 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
156 /* Mark the unused bytes not accessable */
157 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
158 size_t _flen = (_tc)->size - (_new_size); \
159 char *_fptr = TC_PTR_FROM_CHUNK(_tc); \
160 _fptr += (_new_size); \
161 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
164 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
167 #define TC_INVALIDATE_SHRINK_CHUNK(_tc, _new_size) do { \
168 TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size); \
169 TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
172 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
173 /* Mark the new bytes as undefined */
174 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { \
175 size_t _old_used = TC_HDR_SIZE + (_tc)->size; \
176 size_t _new_used = TC_HDR_SIZE + (_new_size); \
177 size_t _flen = _new_used - _old_used; \
178 char *_fptr = _old_used + (char *)(_tc); \
179 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
182 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
185 #define TC_UNDEFINE_GROW_CHUNK(_tc, _new_size) do { \
186 TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
189 struct talloc_reference_handle {
190 struct talloc_reference_handle *next, *prev;
192 const char *location;
195 typedef int (*talloc_destructor_t)(void *);
197 struct talloc_chunk {
198 struct talloc_chunk *next, *prev;
199 struct talloc_chunk *parent, *child;
200 struct talloc_reference_handle *refs;
201 talloc_destructor_t destructor;
207 * "pool" has dual use:
209 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
210 * marks the end of the currently allocated area.
212 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
213 * is a pointer to the struct talloc_chunk of the pool that it was
214 * allocated from. This way children can quickly find the pool to chew
220 /* 16 byte alignment seems to keep everyone happy */
221 #define TC_ALIGN16(s) (((s)+15)&~15)
222 #define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
223 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
225 _PUBLIC_ int talloc_version_major(void)
227 return TALLOC_VERSION_MAJOR;
230 _PUBLIC_ int talloc_version_minor(void)
232 return TALLOC_VERSION_MINOR;
235 static void (*talloc_log_fn)(const char *message);
237 _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
239 talloc_log_fn = log_fn;
242 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
243 static void talloc_log(const char *fmt, ...)
248 if (!talloc_log_fn) {
253 message = talloc_vasprintf(NULL, fmt, ap);
256 talloc_log_fn(message);
257 talloc_free(message);
260 static void talloc_log_stderr(const char *message)
262 fprintf(stderr, "%s", message);
265 _PUBLIC_ void talloc_set_log_stderr(void)
267 talloc_set_log_fn(talloc_log_stderr);
270 static void (*talloc_abort_fn)(const char *reason);
272 _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
274 talloc_abort_fn = abort_fn;
277 static void talloc_abort(const char *reason)
279 talloc_log("%s\n", reason);
281 if (!talloc_abort_fn) {
282 TALLOC_ABORT(reason);
285 talloc_abort_fn(reason);
288 static void talloc_abort_magic(unsigned magic)
290 unsigned striped = magic - TALLOC_MAGIC_BASE;
291 unsigned major = (striped & 0xFFFFF000) >> 12;
292 unsigned minor = (striped & 0x00000FF0) >> 4;
293 talloc_log("Bad talloc magic[0x%08X/%u/%u] expected[0x%08X/%u/%u]\n",
295 TALLOC_MAGIC, TALLOC_VERSION_MAJOR, TALLOC_VERSION_MINOR);
296 talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
299 static void talloc_abort_access_after_free(void)
301 talloc_abort("Bad talloc magic value - access after free");
304 static void talloc_abort_unknown_value(void)
306 talloc_abort("Bad talloc magic value - unknown value");
309 /* panic if we get a bad magic value */
310 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
312 const char *pp = (const char *)ptr;
313 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
314 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
315 if ((tc->flags & (~0xFFF)) == TALLOC_MAGIC_BASE) {
316 talloc_abort_magic(tc->flags & (~0xF));
320 if (tc->flags & TALLOC_FLAG_FREE) {
321 talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
322 talloc_abort_access_after_free();
325 talloc_abort_unknown_value();
332 /* hook into the front of the list */
333 #define _TLIST_ADD(list, p) \
337 (p)->next = (p)->prev = NULL; \
339 (list)->prev = (p); \
340 (p)->next = (list); \
346 /* remove an element from a list - element doesn't have to be in list. */
347 #define _TLIST_REMOVE(list, p) \
349 if ((p) == (list)) { \
350 (list) = (p)->next; \
351 if (list) (list)->prev = NULL; \
353 if ((p)->prev) (p)->prev->next = (p)->next; \
354 if ((p)->next) (p)->next->prev = (p)->prev; \
356 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
361 return the parent chunk of a pointer
363 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
365 struct talloc_chunk *tc;
367 if (unlikely(ptr == NULL)) {
371 tc = talloc_chunk_from_ptr(ptr);
372 while (tc->prev) tc=tc->prev;
377 _PUBLIC_ void *talloc_parent(const void *ptr)
379 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
380 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
386 _PUBLIC_ const char *talloc_parent_name(const void *ptr)
388 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
389 return tc? tc->name : NULL;
393 A pool carries an in-pool object count count in the first 16 bytes.
394 bytes. This is done to support talloc_steal() to a parent outside of the
395 pool. The count includes the pool itself, so a talloc_free() on a pool will
396 only destroy the pool if the count has dropped to zero. A talloc_free() of a
397 pool member will reduce the count, and eventually also call free(3) on the
400 The object count is not put into "struct talloc_chunk" because it is only
401 relevant for talloc pools and the alignment to 16 bytes would increase the
402 memory footprint of each talloc chunk by those 16 bytes.
405 #define TALLOC_POOL_HDR_SIZE 16
407 #define TC_POOL_SPACE_LEFT(_pool_tc) \
408 PTR_DIFF(TC_HDR_SIZE + (_pool_tc)->size + (char *)(_pool_tc), \
411 #define TC_POOL_FIRST_CHUNK(_pool_tc) \
412 ((void *)(TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE + (char *)(_pool_tc)))
414 #define TC_POOLMEM_CHUNK_SIZE(_tc) \
415 TC_ALIGN16(TC_HDR_SIZE + (_tc)->size)
417 #define TC_POOLMEM_NEXT_CHUNK(_tc) \
418 ((void *)(TC_POOLMEM_CHUNK_SIZE(tc) + (char*)(_tc)))
420 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
422 return (unsigned int *)((char *)tc + TC_HDR_SIZE);
429 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
432 struct talloc_chunk *pool_ctx = NULL;
434 struct talloc_chunk *result;
437 if (parent == NULL) {
441 if (parent->flags & TALLOC_FLAG_POOL) {
444 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
445 pool_ctx = (struct talloc_chunk *)parent->pool;
448 if (pool_ctx == NULL) {
452 space_left = TC_POOL_SPACE_LEFT(pool_ctx);
455 * Align size to 16 bytes
457 chunk_size = TC_ALIGN16(size);
459 if (space_left < chunk_size) {
463 result = (struct talloc_chunk *)pool_ctx->pool;
465 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
466 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
469 pool_ctx->pool = (void *)((char *)result + chunk_size);
471 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
472 result->pool = pool_ctx;
474 *talloc_pool_objectcount(pool_ctx) += 1;
480 Allocate a bit of memory as a child of an existing pointer
482 static inline void *__talloc(const void *context, size_t size)
484 struct talloc_chunk *tc = NULL;
486 if (unlikely(context == NULL)) {
487 context = null_context;
490 if (unlikely(size >= MAX_TALLOC_SIZE)) {
494 if (context != NULL) {
495 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
500 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
501 if (unlikely(tc == NULL)) return NULL;
502 tc->flags = TALLOC_MAGIC;
507 tc->destructor = NULL;
512 if (likely(context)) {
513 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
516 parent->child->parent = NULL;
517 tc->next = parent->child;
526 tc->next = tc->prev = tc->parent = NULL;
529 return TC_PTR_FROM_CHUNK(tc);
533 * Create a talloc pool
536 _PUBLIC_ void *talloc_pool(const void *context, size_t size)
538 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
539 struct talloc_chunk *tc;
541 if (unlikely(result == NULL)) {
545 tc = talloc_chunk_from_ptr(result);
547 tc->flags |= TALLOC_FLAG_POOL;
548 tc->pool = TC_POOL_FIRST_CHUNK(tc);
550 *talloc_pool_objectcount(tc) = 1;
552 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
553 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
560 setup a destructor to be called on free of a pointer
561 the destructor should return 0 on success, or -1 on failure.
562 if the destructor fails then the free is failed, and the memory can
563 be continued to be used
565 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
567 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
568 tc->destructor = destructor;
572 increase the reference count on a piece of memory.
574 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
576 if (unlikely(!talloc_reference(null_context, ptr))) {
583 helper for talloc_reference()
585 this is referenced by a function pointer and should not be inline
587 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
589 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
590 _TLIST_REMOVE(ptr_tc->refs, handle);
595 more efficient way to add a name to a pointer - the name must point to a
598 static inline void _talloc_set_name_const(const void *ptr, const char *name)
600 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
605 internal talloc_named_const()
607 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
611 ptr = __talloc(context, size);
612 if (unlikely(ptr == NULL)) {
616 _talloc_set_name_const(ptr, name);
622 make a secondary reference to a pointer, hanging off the given context.
623 the pointer remains valid until both the original caller and this given
626 the major use for this is when two different structures need to reference the
627 same underlying data, and you want to be able to free the two instances separately,
630 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
632 struct talloc_chunk *tc;
633 struct talloc_reference_handle *handle;
634 if (unlikely(ptr == NULL)) return NULL;
636 tc = talloc_chunk_from_ptr(ptr);
637 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
638 sizeof(struct talloc_reference_handle),
639 TALLOC_MAGIC_REFERENCE);
640 if (unlikely(handle == NULL)) return NULL;
642 /* note that we hang the destructor off the handle, not the
643 main context as that allows the caller to still setup their
644 own destructor on the context if they want to */
645 talloc_set_destructor(handle, talloc_reference_destructor);
646 handle->ptr = discard_const_p(void, ptr);
647 handle->location = location;
648 _TLIST_ADD(tc->refs, handle);
652 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
655 internal talloc_free call
657 static inline int _talloc_free_internal(void *ptr, const char *location)
659 struct talloc_chunk *tc;
661 if (unlikely(ptr == NULL)) {
665 /* possibly initialised the talloc fill value */
666 if (unlikely(!talloc_fill.initialised)) {
667 const char *fill = getenv(TALLOC_FILL_ENV);
669 talloc_fill.enabled = true;
670 talloc_fill.fill_value = strtoul(fill, NULL, 0);
672 talloc_fill.initialised = true;
675 tc = talloc_chunk_from_ptr(ptr);
677 if (unlikely(tc->refs)) {
679 /* check if this is a reference from a child or
680 * grandchild back to it's parent or grandparent
682 * in that case we need to remove the reference and
683 * call another instance of talloc_free() on the current
686 is_child = talloc_is_parent(tc->refs, ptr);
687 _talloc_free_internal(tc->refs, location);
689 return _talloc_free_internal(ptr, location);
694 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
695 /* we have a free loop - stop looping */
699 if (unlikely(tc->destructor)) {
700 talloc_destructor_t d = tc->destructor;
701 if (d == (talloc_destructor_t)-1) {
704 tc->destructor = (talloc_destructor_t)-1;
709 tc->destructor = NULL;
713 _TLIST_REMOVE(tc->parent->child, tc);
714 if (tc->parent->child) {
715 tc->parent->child->parent = tc->parent;
718 if (tc->prev) tc->prev->next = tc->next;
719 if (tc->next) tc->next->prev = tc->prev;
722 tc->flags |= TALLOC_FLAG_LOOP;
725 /* we need to work out who will own an abandoned child
726 if it cannot be freed. In priority order, the first
727 choice is owner of any remaining reference to this
728 pointer, the second choice is our parent, and the
729 final choice is the null context. */
730 void *child = TC_PTR_FROM_CHUNK(tc->child);
731 const void *new_parent = null_context;
732 struct talloc_chunk *old_parent = NULL;
733 if (unlikely(tc->child->refs)) {
734 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
735 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
737 /* finding the parent here is potentially quite
738 expensive, but the alternative, which is to change
739 talloc to always have a valid tc->parent pointer,
740 makes realloc more expensive where there are a
741 large number of children.
743 The reason we need the parent pointer here is that
744 if _talloc_free_internal() fails due to references
745 or a failing destructor we need to re-parent, but
746 the free call can invalidate the prev pointer.
748 if (new_parent == null_context && (tc->child->refs || tc->child->destructor)) {
749 old_parent = talloc_parent_chunk(ptr);
751 if (unlikely(_talloc_free_internal(child, location) == -1)) {
752 if (new_parent == null_context) {
753 struct talloc_chunk *p = old_parent;
754 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
756 _talloc_steal_internal(new_parent, child);
760 tc->flags |= TALLOC_FLAG_FREE;
762 /* we mark the freed memory with where we called the free
763 * from. This means on a double free error we can report where
764 * the first free came from
768 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
769 struct talloc_chunk *pool;
770 void *next_tc = NULL;
771 unsigned int *pool_object_count;
773 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
776 pool = (struct talloc_chunk *)tc->pool;
777 next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
779 TC_INVALIDATE_FULL_CHUNK(tc);
782 pool_object_count = talloc_pool_objectcount(pool);
784 if (unlikely(*pool_object_count == 0)) {
785 talloc_abort("Pool object count zero!");
789 *pool_object_count -= 1;
791 if (unlikely(*pool_object_count == 1)) {
793 * if there is just object left in the pool
794 * it means this is the pool itself and
795 * the rest is available for new objects
798 pool->pool = TC_POOL_FIRST_CHUNK(pool);
799 } else if (unlikely(*pool_object_count == 0)) {
800 TC_INVALIDATE_FULL_CHUNK(pool);
802 } else if (pool->pool == next_tc) {
804 * if pool->pool still points to end of
805 * 'tc' (which is stored in the 'next_tc' variable),
806 * we can reclaim the memory of 'tc'.
811 TC_INVALIDATE_FULL_CHUNK(tc);
818 move a lump of memory from one talloc context to another return the
819 ptr on success, or NULL if it could not be transferred.
820 passing NULL as ptr will always return NULL with no side effects.
822 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
824 struct talloc_chunk *tc, *new_tc;
826 if (unlikely(!ptr)) {
830 if (unlikely(new_ctx == NULL)) {
831 new_ctx = null_context;
834 tc = talloc_chunk_from_ptr(ptr);
836 if (unlikely(new_ctx == NULL)) {
838 _TLIST_REMOVE(tc->parent->child, tc);
839 if (tc->parent->child) {
840 tc->parent->child->parent = tc->parent;
843 if (tc->prev) tc->prev->next = tc->next;
844 if (tc->next) tc->next->prev = tc->prev;
847 tc->parent = tc->next = tc->prev = NULL;
848 return discard_const_p(void, ptr);
851 new_tc = talloc_chunk_from_ptr(new_ctx);
853 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
854 return discard_const_p(void, ptr);
858 _TLIST_REMOVE(tc->parent->child, tc);
859 if (tc->parent->child) {
860 tc->parent->child->parent = tc->parent;
863 if (tc->prev) tc->prev->next = tc->next;
864 if (tc->next) tc->next->prev = tc->prev;
868 if (new_tc->child) new_tc->child->parent = NULL;
869 _TLIST_ADD(new_tc->child, tc);
871 return discard_const_p(void, ptr);
875 move a lump of memory from one talloc context to another return the
876 ptr on success, or NULL if it could not be transferred.
877 passing NULL as ptr will always return NULL with no side effects.
879 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
881 struct talloc_chunk *tc;
883 if (unlikely(ptr == NULL)) {
887 tc = talloc_chunk_from_ptr(ptr);
889 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
890 struct talloc_reference_handle *h;
892 talloc_log("WARNING: talloc_steal with references at %s\n",
895 for (h=tc->refs; h; h=h->next) {
896 talloc_log("\treference at %s\n",
902 /* this test is probably too expensive to have on in the
903 normal build, but it useful for debugging */
904 if (talloc_is_parent(new_ctx, ptr)) {
905 talloc_log("WARNING: stealing into talloc child at %s\n", location);
909 return _talloc_steal_internal(new_ctx, ptr);
913 this is like a talloc_steal(), but you must supply the old
914 parent. This resolves the ambiguity in a talloc_steal() which is
915 called on a context that has more than one parent (via references)
917 The old parent can be either a reference or a parent
919 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
921 struct talloc_chunk *tc;
922 struct talloc_reference_handle *h;
924 if (unlikely(ptr == NULL)) {
928 if (old_parent == talloc_parent(ptr)) {
929 return _talloc_steal_internal(new_parent, ptr);
932 tc = talloc_chunk_from_ptr(ptr);
933 for (h=tc->refs;h;h=h->next) {
934 if (talloc_parent(h) == old_parent) {
935 if (_talloc_steal_internal(new_parent, h) != h) {
938 return discard_const_p(void, ptr);
942 /* it wasn't a parent */
947 remove a secondary reference to a pointer. This undo's what
948 talloc_reference() has done. The context and pointer arguments
949 must match those given to a talloc_reference()
951 static inline int talloc_unreference(const void *context, const void *ptr)
953 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
954 struct talloc_reference_handle *h;
956 if (unlikely(context == NULL)) {
957 context = null_context;
960 for (h=tc->refs;h;h=h->next) {
961 struct talloc_chunk *p = talloc_parent_chunk(h);
963 if (context == NULL) break;
964 } else if (TC_PTR_FROM_CHUNK(p) == context) {
972 return _talloc_free_internal(h, __location__);
976 remove a specific parent context from a pointer. This is a more
977 controlled varient of talloc_free()
979 _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
981 struct talloc_chunk *tc_p, *new_p;
988 if (context == NULL) {
989 context = null_context;
992 if (talloc_unreference(context, ptr) == 0) {
996 if (context == NULL) {
997 if (talloc_parent_chunk(ptr) != NULL) {
1001 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
1006 tc_p = talloc_chunk_from_ptr(ptr);
1008 if (tc_p->refs == NULL) {
1009 return _talloc_free_internal(ptr, __location__);
1012 new_p = talloc_parent_chunk(tc_p->refs);
1014 new_parent = TC_PTR_FROM_CHUNK(new_p);
1019 if (talloc_unreference(new_parent, ptr) != 0) {
1023 _talloc_steal_internal(new_parent, ptr);
1029 add a name to an existing pointer - va_list version
1031 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1033 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
1035 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1036 tc->name = talloc_vasprintf(ptr, fmt, ap);
1037 if (likely(tc->name)) {
1038 _talloc_set_name_const(tc->name, ".name");
1044 add a name to an existing pointer
1046 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
1051 name = talloc_set_name_v(ptr, fmt, ap);
1058 create a named talloc pointer. Any talloc pointer can be named, and
1059 talloc_named() operates just like talloc() except that it allows you
1060 to name the pointer.
1062 _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
1068 ptr = __talloc(context, size);
1069 if (unlikely(ptr == NULL)) return NULL;
1072 name = talloc_set_name_v(ptr, fmt, ap);
1075 if (unlikely(name == NULL)) {
1076 _talloc_free_internal(ptr, __location__);
1084 return the name of a talloc ptr, or "UNNAMED"
1086 _PUBLIC_ const char *talloc_get_name(const void *ptr)
1088 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1089 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
1090 return ".reference";
1092 if (likely(tc->name)) {
1100 check if a pointer has the given name. If it does, return the pointer,
1101 otherwise return NULL
1103 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
1106 if (unlikely(ptr == NULL)) return NULL;
1107 pname = talloc_get_name(ptr);
1108 if (likely(pname == name || strcmp(pname, name) == 0)) {
1109 return discard_const_p(void, ptr);
1114 static void talloc_abort_type_missmatch(const char *location,
1116 const char *expected)
1120 reason = talloc_asprintf(NULL,
1121 "%s: Type mismatch: name[%s] expected[%s]",
1126 reason = "Type mismatch";
1129 talloc_abort(reason);
1132 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
1136 if (unlikely(ptr == NULL)) {
1137 talloc_abort_type_missmatch(location, NULL, name);
1141 pname = talloc_get_name(ptr);
1142 if (likely(pname == name || strcmp(pname, name) == 0)) {
1143 return discard_const_p(void, ptr);
1146 talloc_abort_type_missmatch(location, pname, name);
1151 this is for compatibility with older versions of talloc
1153 _PUBLIC_ void *talloc_init(const char *fmt, ...)
1159 ptr = __talloc(NULL, 0);
1160 if (unlikely(ptr == NULL)) return NULL;
1163 name = talloc_set_name_v(ptr, fmt, ap);
1166 if (unlikely(name == NULL)) {
1167 _talloc_free_internal(ptr, __location__);
1175 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1176 should probably not be used in new code. It's in here to keep the talloc
1177 code consistent across Samba 3 and 4.
1179 _PUBLIC_ void talloc_free_children(void *ptr)
1181 struct talloc_chunk *tc;
1183 if (unlikely(ptr == NULL)) {
1187 tc = talloc_chunk_from_ptr(ptr);
1190 /* we need to work out who will own an abandoned child
1191 if it cannot be freed. In priority order, the first
1192 choice is owner of any remaining reference to this
1193 pointer, the second choice is our parent, and the
1194 final choice is the null context. */
1195 void *child = TC_PTR_FROM_CHUNK(tc->child);
1196 const void *new_parent = null_context;
1197 if (unlikely(tc->child->refs)) {
1198 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1199 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1201 if (unlikely(talloc_free(child) == -1)) {
1202 if (new_parent == null_context) {
1203 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1204 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1206 _talloc_steal_internal(new_parent, child);
1212 Allocate a bit of memory as a child of an existing pointer
1214 _PUBLIC_ void *_talloc(const void *context, size_t size)
1216 return __talloc(context, size);
1220 externally callable talloc_set_name_const()
1222 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1224 _talloc_set_name_const(ptr, name);
1228 create a named talloc pointer. Any talloc pointer can be named, and
1229 talloc_named() operates just like talloc() except that it allows you
1230 to name the pointer.
1232 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1234 return _talloc_named_const(context, size, name);
1238 free a talloc pointer. This also frees all child pointers of this
1241 return 0 if the memory is actually freed, otherwise -1. The memory
1242 will not be freed if the ref_count is > 1 or the destructor (if
1243 any) returns non-zero
1245 _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1247 struct talloc_chunk *tc;
1249 if (unlikely(ptr == NULL)) {
1253 tc = talloc_chunk_from_ptr(ptr);
1255 if (unlikely(tc->refs != NULL)) {
1256 struct talloc_reference_handle *h;
1258 if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1259 /* in this case we do know which parent should
1260 get this pointer, as there is really only
1262 return talloc_unlink(null_context, ptr);
1265 talloc_log("ERROR: talloc_free with references at %s\n",
1268 for (h=tc->refs; h; h=h->next) {
1269 talloc_log("\treference at %s\n",
1275 return _talloc_free_internal(ptr, location);
1281 A talloc version of realloc. The context argument is only used if
1284 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1286 struct talloc_chunk *tc;
1288 bool malloced = false;
1289 struct talloc_chunk *pool_tc = NULL;
1291 /* size zero is equivalent to free() */
1292 if (unlikely(size == 0)) {
1293 talloc_unlink(context, ptr);
1297 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1301 /* realloc(NULL) is equivalent to malloc() */
1303 return _talloc_named_const(context, size, name);
1306 tc = talloc_chunk_from_ptr(ptr);
1308 /* don't allow realloc on referenced pointers */
1309 if (unlikely(tc->refs)) {
1313 /* don't let anybody try to realloc a talloc_pool */
1314 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1318 /* don't let anybody try to realloc a talloc_pool */
1319 if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
1320 pool_tc = (struct talloc_chunk *)tc->pool;
1323 #if (ALWAYS_REALLOC == 0)
1324 /* don't shrink if we have less than 1k to gain */
1325 if (size < tc->size) {
1327 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1328 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1330 if (next_tc == pool_tc->pool) {
1331 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1334 } else if ((tc->size - size) < 1024) {
1335 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1336 /* do not shrink if we have less than 1k to gain */
1340 } else if (tc->size == size) {
1342 * do not change the pointer if it is exactly
1349 /* by resetting magic we catch users of the old memory */
1350 tc->flags |= TALLOC_FLAG_FREE;
1354 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1355 *talloc_pool_objectcount(pool_tc) -= 1;
1357 if (new_ptr == NULL) {
1358 new_ptr = malloc(TC_HDR_SIZE+size);
1363 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1364 TC_INVALIDATE_FULL_CHUNK(tc);
1367 new_ptr = malloc(size + TC_HDR_SIZE);
1369 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1375 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1376 size_t old_chunk_size = TC_POOLMEM_CHUNK_SIZE(tc);
1377 size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
1378 size_t space_needed;
1381 if (*talloc_pool_objectcount(pool_tc) == 2) {
1383 * optimize for the case where 'tc' is the only
1384 * chunk in the pool.
1386 space_needed = new_chunk_size;
1387 space_left = pool_tc->size - TALLOC_POOL_HDR_SIZE;
1389 if (space_left >= space_needed) {
1390 size_t old_used = TC_HDR_SIZE + tc->size;
1391 pool_tc->pool = TC_POOL_FIRST_CHUNK(pool_tc);
1392 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
1394 * we need to prepare the memmove into
1395 * the unaccessable area.
1398 size_t diff = PTR_DIFF(tc, pool_tc->pool);
1399 size_t flen = MIN(diff, old_used);
1400 char *fptr = (char *)pool_tc->pool;
1401 VALGRIND_MAKE_MEM_UNDEFINED(fptr, flen);
1404 memmove(pool_tc->pool, tc, old_used);
1405 new_ptr = pool_tc->pool;
1407 TC_UNDEFINE_GROW_CHUNK(tc, size);
1409 pool_tc->pool = new_chunk_size + (char *)new_ptr;
1416 if (new_chunk_size == old_chunk_size) {
1417 TC_UNDEFINE_GROW_CHUNK(tc, size);
1418 tc->flags &= ~TALLOC_FLAG_FREE;
1423 if (next_tc == pool_tc->pool) {
1425 * optimize for the case where 'tc' is the last
1426 * chunk in the pool.
1428 space_needed = new_chunk_size - old_chunk_size;
1429 space_left = TC_POOL_SPACE_LEFT(pool_tc);
1431 if (space_left >= space_needed) {
1432 TC_UNDEFINE_GROW_CHUNK(tc, size);
1433 tc->flags &= ~TALLOC_FLAG_FREE;
1435 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1440 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1441 *talloc_pool_objectcount(pool_tc) -= 1;
1443 if (new_ptr == NULL) {
1444 new_ptr = malloc(TC_HDR_SIZE+size);
1449 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1450 TC_INVALIDATE_FULL_CHUNK(tc);
1452 if (*talloc_pool_objectcount(pool_tc) == 1) {
1454 * If the pool is empty now reclaim everything.
1456 pool_tc->pool = TC_POOL_FIRST_CHUNK(pool_tc);
1457 } else if (next_tc == pool_tc->pool) {
1459 * If it was reallocated and tc was the last
1460 * chunk, we can reclaim the memory of tc.
1467 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1471 if (unlikely(!new_ptr)) {
1472 tc->flags &= ~TALLOC_FLAG_FREE;
1476 tc = (struct talloc_chunk *)new_ptr;
1477 tc->flags &= ~TALLOC_FLAG_FREE;
1479 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1482 tc->parent->child = tc;
1485 tc->child->parent = tc;
1489 tc->prev->next = tc;
1492 tc->next->prev = tc;
1496 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1498 return TC_PTR_FROM_CHUNK(tc);
1502 a wrapper around talloc_steal() for situations where you are moving a pointer
1503 between two structures, and want the old pointer to be set to NULL
1505 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
1507 const void **pptr = discard_const_p(const void *,_pptr);
1508 void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1514 return the total size of a talloc pool (subtree)
1516 _PUBLIC_ size_t talloc_total_size(const void *ptr)
1519 struct talloc_chunk *c, *tc;
1528 tc = talloc_chunk_from_ptr(ptr);
1530 if (tc->flags & TALLOC_FLAG_LOOP) {
1534 tc->flags |= TALLOC_FLAG_LOOP;
1536 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1539 for (c=tc->child;c;c=c->next) {
1540 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1543 tc->flags &= ~TALLOC_FLAG_LOOP;
1549 return the total number of blocks in a talloc pool (subtree)
1551 _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
1554 struct talloc_chunk *c, *tc;
1563 tc = talloc_chunk_from_ptr(ptr);
1565 if (tc->flags & TALLOC_FLAG_LOOP) {
1569 tc->flags |= TALLOC_FLAG_LOOP;
1572 for (c=tc->child;c;c=c->next) {
1573 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1576 tc->flags &= ~TALLOC_FLAG_LOOP;
1582 return the number of external references to a pointer
1584 _PUBLIC_ size_t talloc_reference_count(const void *ptr)
1586 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1587 struct talloc_reference_handle *h;
1590 for (h=tc->refs;h;h=h->next) {
1597 report on memory usage by all children of a pointer, giving a full tree view
1599 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1600 void (*callback)(const void *ptr,
1601 int depth, int max_depth,
1603 void *private_data),
1606 struct talloc_chunk *c, *tc;
1611 if (ptr == NULL) return;
1613 tc = talloc_chunk_from_ptr(ptr);
1615 if (tc->flags & TALLOC_FLAG_LOOP) {
1619 callback(ptr, depth, max_depth, 0, private_data);
1621 if (max_depth >= 0 && depth >= max_depth) {
1625 tc->flags |= TALLOC_FLAG_LOOP;
1626 for (c=tc->child;c;c=c->next) {
1627 if (c->name == TALLOC_MAGIC_REFERENCE) {
1628 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1629 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1631 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1634 tc->flags &= ~TALLOC_FLAG_LOOP;
1637 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1639 const char *name = talloc_get_name(ptr);
1640 FILE *f = (FILE *)_f;
1643 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1648 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1649 (max_depth < 0 ? "full " :""), name,
1650 (unsigned long)talloc_total_size(ptr),
1651 (unsigned long)talloc_total_blocks(ptr));
1655 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1658 (unsigned long)talloc_total_size(ptr),
1659 (unsigned long)talloc_total_blocks(ptr),
1660 (int)talloc_reference_count(ptr), ptr);
1663 fprintf(f, "content: ");
1664 if (talloc_total_size(ptr)) {
1665 int tot = talloc_total_size(ptr);
1668 for (i = 0; i < tot; i++) {
1669 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1670 fprintf(f, "%c", ((char *)ptr)[i]);
1672 fprintf(f, "~%02x", ((char *)ptr)[i]);
1681 report on memory usage by all children of a pointer, giving a full tree view
1683 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1686 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1692 report on memory usage by all children of a pointer, giving a full tree view
1694 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
1696 talloc_report_depth_file(ptr, 0, -1, f);
1700 report on memory usage by all children of a pointer
1702 _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
1704 talloc_report_depth_file(ptr, 0, 1, f);
1708 report on any memory hanging off the null context
1710 static void talloc_report_null(void)
1712 if (talloc_total_size(null_context) != 0) {
1713 talloc_report(null_context, stderr);
1718 report on any memory hanging off the null context
1720 static void talloc_report_null_full(void)
1722 if (talloc_total_size(null_context) != 0) {
1723 talloc_report_full(null_context, stderr);
1728 enable tracking of the NULL context
1730 _PUBLIC_ void talloc_enable_null_tracking(void)
1732 if (null_context == NULL) {
1733 null_context = _talloc_named_const(NULL, 0, "null_context");
1734 if (autofree_context != NULL) {
1735 talloc_reparent(NULL, null_context, autofree_context);
1741 enable tracking of the NULL context, not moving the autofree context
1742 into the NULL context. This is needed for the talloc testsuite
1744 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
1746 if (null_context == NULL) {
1747 null_context = _talloc_named_const(NULL, 0, "null_context");
1752 disable tracking of the NULL context
1754 _PUBLIC_ void talloc_disable_null_tracking(void)
1756 if (null_context != NULL) {
1757 /* we have to move any children onto the real NULL
1759 struct talloc_chunk *tc, *tc2;
1760 tc = talloc_chunk_from_ptr(null_context);
1761 for (tc2 = tc->child; tc2; tc2=tc2->next) {
1762 if (tc2->parent == tc) tc2->parent = NULL;
1763 if (tc2->prev == tc) tc2->prev = NULL;
1765 for (tc2 = tc->next; tc2; tc2=tc2->next) {
1766 if (tc2->parent == tc) tc2->parent = NULL;
1767 if (tc2->prev == tc) tc2->prev = NULL;
1772 talloc_free(null_context);
1773 null_context = NULL;
1777 enable leak reporting on exit
1779 _PUBLIC_ void talloc_enable_leak_report(void)
1781 talloc_enable_null_tracking();
1782 atexit(talloc_report_null);
1786 enable full leak reporting on exit
1788 _PUBLIC_ void talloc_enable_leak_report_full(void)
1790 talloc_enable_null_tracking();
1791 atexit(talloc_report_null_full);
1795 talloc and zero memory.
1797 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
1799 void *p = _talloc_named_const(ctx, size, name);
1802 memset(p, '\0', size);
1809 memdup with a talloc.
1811 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1813 void *newp = _talloc_named_const(t, size, name);
1816 memcpy(newp, p, size);
1822 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1826 ret = (char *)__talloc(t, len + 1);
1827 if (unlikely(!ret)) return NULL;
1829 memcpy(ret, p, len);
1832 _talloc_set_name_const(ret, ret);
1837 strdup with a talloc
1839 _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
1841 if (unlikely(!p)) return NULL;
1842 return __talloc_strlendup(t, p, strlen(p));
1846 strndup with a talloc
1848 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
1850 if (unlikely(!p)) return NULL;
1851 return __talloc_strlendup(t, p, strnlen(p, n));
1854 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1855 const char *a, size_t alen)
1859 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1860 if (unlikely(!ret)) return NULL;
1862 /* append the string and the trailing \0 */
1863 memcpy(&ret[slen], a, alen);
1866 _talloc_set_name_const(ret, ret);
1871 * Appends at the end of the string.
1873 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
1876 return talloc_strdup(NULL, a);
1883 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1887 * Appends at the end of the talloc'ed buffer,
1888 * not the end of the string.
1890 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
1895 return talloc_strdup(NULL, a);
1902 slen = talloc_get_size(s);
1903 if (likely(slen > 0)) {
1907 return __talloc_strlendup_append(s, slen, a, strlen(a));
1911 * Appends at the end of the string.
1913 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
1916 return talloc_strdup(NULL, a);
1923 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1927 * Appends at the end of the talloc'ed buffer,
1928 * not the end of the string.
1930 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1935 return talloc_strdup(NULL, a);
1942 slen = talloc_get_size(s);
1943 if (likely(slen > 0)) {
1947 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1950 #ifndef HAVE_VA_COPY
1951 #ifdef HAVE___VA_COPY
1952 #define va_copy(dest, src) __va_copy(dest, src)
1954 #define va_copy(dest, src) (dest) = (src)
1958 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1965 /* this call looks strange, but it makes it work on older solaris boxes */
1967 len = vsnprintf(&c, 1, fmt, ap2);
1969 if (unlikely(len < 0)) {
1973 ret = (char *)__talloc(t, len+1);
1974 if (unlikely(!ret)) return NULL;
1977 vsnprintf(ret, len+1, fmt, ap2);
1980 _talloc_set_name_const(ret, ret);
1986 Perform string formatting, and return a pointer to newly allocated
1987 memory holding the result, inside a memory pool.
1989 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
1995 ret = talloc_vasprintf(t, fmt, ap);
2000 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2001 const char *fmt, va_list ap)
2002 PRINTF_ATTRIBUTE(3,0);
2004 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2005 const char *fmt, va_list ap)
2012 alen = vsnprintf(&c, 1, fmt, ap2);
2016 /* Either the vsnprintf failed or the format resulted in
2017 * no characters being formatted. In the former case, we
2018 * ought to return NULL, in the latter we ought to return
2019 * the original string. Most current callers of this
2020 * function expect it to never return NULL.
2025 s = talloc_realloc(NULL, s, char, slen + alen + 1);
2026 if (!s) return NULL;
2029 vsnprintf(s + slen, alen + 1, fmt, ap2);
2032 _talloc_set_name_const(s, s);
2037 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2038 * and return @p s, which may have moved. Good for gradually
2039 * accumulating output into a string buffer. Appends at the end
2042 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
2045 return talloc_vasprintf(NULL, fmt, ap);
2048 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
2052 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2053 * and return @p s, which may have moved. Always appends at the
2054 * end of the talloc'ed buffer, not the end of the string.
2056 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
2061 return talloc_vasprintf(NULL, fmt, ap);
2064 slen = talloc_get_size(s);
2065 if (likely(slen > 0)) {
2069 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
2073 Realloc @p s to append the formatted result of @p fmt and return @p
2074 s, which may have moved. Good for gradually accumulating output
2075 into a string buffer.
2077 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
2082 s = talloc_vasprintf_append(s, fmt, ap);
2088 Realloc @p s to append the formatted result of @p fmt and return @p
2089 s, which may have moved. Good for gradually accumulating output
2092 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
2097 s = talloc_vasprintf_append_buffer(s, fmt, ap);
2103 alloc an array, checking for integer overflow in the array size
2105 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2107 if (count >= MAX_TALLOC_SIZE/el_size) {
2110 return _talloc_named_const(ctx, el_size * count, name);
2114 alloc an zero array, checking for integer overflow in the array size
2116 _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2118 if (count >= MAX_TALLOC_SIZE/el_size) {
2121 return _talloc_zero(ctx, el_size * count, name);
2125 realloc an array, checking for integer overflow in the array size
2127 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
2129 if (count >= MAX_TALLOC_SIZE/el_size) {
2132 return _talloc_realloc(ctx, ptr, el_size * count, name);
2136 a function version of talloc_realloc(), so it can be passed as a function pointer
2137 to libraries that want a realloc function (a realloc function encapsulates
2138 all the basic capabilities of an allocation library, which is why this is useful)
2140 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
2142 return _talloc_realloc(context, ptr, size, NULL);
2146 static int talloc_autofree_destructor(void *ptr)
2148 autofree_context = NULL;
2152 static void talloc_autofree(void)
2154 talloc_free(autofree_context);
2158 return a context which will be auto-freed on exit
2159 this is useful for reducing the noise in leak reports
2161 _PUBLIC_ void *talloc_autofree_context(void)
2163 if (autofree_context == NULL) {
2164 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
2165 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
2166 atexit(talloc_autofree);
2168 return autofree_context;
2171 _PUBLIC_ size_t talloc_get_size(const void *context)
2173 struct talloc_chunk *tc;
2175 if (context == NULL) {
2176 context = null_context;
2178 if (context == NULL) {
2182 tc = talloc_chunk_from_ptr(context);
2188 find a parent of this context that has the given name, if any
2190 _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
2192 struct talloc_chunk *tc;
2194 if (context == NULL) {
2198 tc = talloc_chunk_from_ptr(context);
2200 if (tc->name && strcmp(tc->name, name) == 0) {
2201 return TC_PTR_FROM_CHUNK(tc);
2203 while (tc && tc->prev) tc = tc->prev;
2212 show the parentage of a context
2214 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
2216 struct talloc_chunk *tc;
2218 if (context == NULL) {
2219 fprintf(file, "talloc no parents for NULL\n");
2223 tc = talloc_chunk_from_ptr(context);
2224 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
2226 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
2227 while (tc && tc->prev) tc = tc->prev;
2236 return 1 if ptr is a parent of context
2238 static int _talloc_is_parent(const void *context, const void *ptr, int depth)
2240 struct talloc_chunk *tc;
2242 if (context == NULL) {
2246 tc = talloc_chunk_from_ptr(context);
2247 while (tc && depth > 0) {
2248 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2249 while (tc && tc->prev) tc = tc->prev;
2259 return 1 if ptr is a parent of context
2261 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2263 return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);