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 /* Mark the whole remaining pool as not accessable */
421 #define TC_INVALIDATE_FILL_POOL(_pool_tc) do { \
422 if (unlikely(talloc_fill.enabled)) { \
423 size_t _flen = TC_POOL_SPACE_LEFT(_pool_tc); \
424 char *_fptr = (_pool_tc)->pool; \
425 memset(_fptr, talloc_fill.fill_value, _flen); \
429 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
430 /* Mark the whole remaining pool as not accessable */
431 #define TC_INVALIDATE_VALGRIND_POOL(_pool_tc) do { \
432 size_t _flen = TC_POOL_SPACE_LEFT(_pool_tc); \
433 char *_fptr = (_pool_tc)->pool; \
434 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
437 #define TC_INVALIDATE_VALGRIND_POOL(_pool_tc) do { } while (0)
440 #define TC_INVALIDATE_POOL(_pool_tc) do { \
441 TC_INVALIDATE_FILL_POOL(_pool_tc); \
442 TC_INVALIDATE_VALGRIND_POOL(_pool_tc); \
445 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
447 return (unsigned int *)((char *)tc + TC_HDR_SIZE);
454 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
457 struct talloc_chunk *pool_ctx = NULL;
459 struct talloc_chunk *result;
462 if (parent == NULL) {
466 if (parent->flags & TALLOC_FLAG_POOL) {
469 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
470 pool_ctx = (struct talloc_chunk *)parent->pool;
473 if (pool_ctx == NULL) {
477 space_left = TC_POOL_SPACE_LEFT(pool_ctx);
480 * Align size to 16 bytes
482 chunk_size = TC_ALIGN16(size);
484 if (space_left < chunk_size) {
488 result = (struct talloc_chunk *)pool_ctx->pool;
490 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
491 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
494 pool_ctx->pool = (void *)((char *)result + chunk_size);
496 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
497 result->pool = pool_ctx;
499 *talloc_pool_objectcount(pool_ctx) += 1;
505 Allocate a bit of memory as a child of an existing pointer
507 static inline void *__talloc(const void *context, size_t size)
509 struct talloc_chunk *tc = NULL;
511 if (unlikely(context == NULL)) {
512 context = null_context;
515 if (unlikely(size >= MAX_TALLOC_SIZE)) {
519 if (context != NULL) {
520 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
525 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
526 if (unlikely(tc == NULL)) return NULL;
527 tc->flags = TALLOC_MAGIC;
532 tc->destructor = NULL;
537 if (likely(context)) {
538 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
541 parent->child->parent = NULL;
542 tc->next = parent->child;
551 tc->next = tc->prev = tc->parent = NULL;
554 return TC_PTR_FROM_CHUNK(tc);
558 * Create a talloc pool
561 _PUBLIC_ void *talloc_pool(const void *context, size_t size)
563 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
564 struct talloc_chunk *tc;
566 if (unlikely(result == NULL)) {
570 tc = talloc_chunk_from_ptr(result);
572 tc->flags |= TALLOC_FLAG_POOL;
573 tc->pool = TC_POOL_FIRST_CHUNK(tc);
575 *talloc_pool_objectcount(tc) = 1;
577 TC_INVALIDATE_POOL(tc);
583 setup a destructor to be called on free of a pointer
584 the destructor should return 0 on success, or -1 on failure.
585 if the destructor fails then the free is failed, and the memory can
586 be continued to be used
588 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
590 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
591 tc->destructor = destructor;
595 increase the reference count on a piece of memory.
597 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
599 if (unlikely(!talloc_reference(null_context, ptr))) {
606 helper for talloc_reference()
608 this is referenced by a function pointer and should not be inline
610 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
612 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
613 _TLIST_REMOVE(ptr_tc->refs, handle);
618 more efficient way to add a name to a pointer - the name must point to a
621 static inline void _talloc_set_name_const(const void *ptr, const char *name)
623 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
628 internal talloc_named_const()
630 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
634 ptr = __talloc(context, size);
635 if (unlikely(ptr == NULL)) {
639 _talloc_set_name_const(ptr, name);
645 make a secondary reference to a pointer, hanging off the given context.
646 the pointer remains valid until both the original caller and this given
649 the major use for this is when two different structures need to reference the
650 same underlying data, and you want to be able to free the two instances separately,
653 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
655 struct talloc_chunk *tc;
656 struct talloc_reference_handle *handle;
657 if (unlikely(ptr == NULL)) return NULL;
659 tc = talloc_chunk_from_ptr(ptr);
660 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
661 sizeof(struct talloc_reference_handle),
662 TALLOC_MAGIC_REFERENCE);
663 if (unlikely(handle == NULL)) return NULL;
665 /* note that we hang the destructor off the handle, not the
666 main context as that allows the caller to still setup their
667 own destructor on the context if they want to */
668 talloc_set_destructor(handle, talloc_reference_destructor);
669 handle->ptr = discard_const_p(void, ptr);
670 handle->location = location;
671 _TLIST_ADD(tc->refs, handle);
675 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
678 internal talloc_free call
680 static inline int _talloc_free_internal(void *ptr, const char *location)
682 struct talloc_chunk *tc;
684 if (unlikely(ptr == NULL)) {
688 /* possibly initialised the talloc fill value */
689 if (unlikely(!talloc_fill.initialised)) {
690 const char *fill = getenv(TALLOC_FILL_ENV);
692 talloc_fill.enabled = true;
693 talloc_fill.fill_value = strtoul(fill, NULL, 0);
695 talloc_fill.initialised = true;
698 tc = talloc_chunk_from_ptr(ptr);
700 if (unlikely(tc->refs)) {
702 /* check if this is a reference from a child or
703 * grandchild back to it's parent or grandparent
705 * in that case we need to remove the reference and
706 * call another instance of talloc_free() on the current
709 is_child = talloc_is_parent(tc->refs, ptr);
710 _talloc_free_internal(tc->refs, location);
712 return _talloc_free_internal(ptr, location);
717 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
718 /* we have a free loop - stop looping */
722 if (unlikely(tc->destructor)) {
723 talloc_destructor_t d = tc->destructor;
724 if (d == (talloc_destructor_t)-1) {
727 tc->destructor = (talloc_destructor_t)-1;
732 tc->destructor = NULL;
736 _TLIST_REMOVE(tc->parent->child, tc);
737 if (tc->parent->child) {
738 tc->parent->child->parent = tc->parent;
741 if (tc->prev) tc->prev->next = tc->next;
742 if (tc->next) tc->next->prev = tc->prev;
745 tc->flags |= TALLOC_FLAG_LOOP;
748 /* we need to work out who will own an abandoned child
749 if it cannot be freed. In priority order, the first
750 choice is owner of any remaining reference to this
751 pointer, the second choice is our parent, and the
752 final choice is the null context. */
753 void *child = TC_PTR_FROM_CHUNK(tc->child);
754 const void *new_parent = null_context;
755 struct talloc_chunk *old_parent = NULL;
756 if (unlikely(tc->child->refs)) {
757 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
758 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
760 /* finding the parent here is potentially quite
761 expensive, but the alternative, which is to change
762 talloc to always have a valid tc->parent pointer,
763 makes realloc more expensive where there are a
764 large number of children.
766 The reason we need the parent pointer here is that
767 if _talloc_free_internal() fails due to references
768 or a failing destructor we need to re-parent, but
769 the free call can invalidate the prev pointer.
771 if (new_parent == null_context && (tc->child->refs || tc->child->destructor)) {
772 old_parent = talloc_parent_chunk(ptr);
774 if (unlikely(_talloc_free_internal(child, location) == -1)) {
775 if (new_parent == null_context) {
776 struct talloc_chunk *p = old_parent;
777 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
779 _talloc_steal_internal(new_parent, child);
783 tc->flags |= TALLOC_FLAG_FREE;
785 /* we mark the freed memory with where we called the free
786 * from. This means on a double free error we can report where
787 * the first free came from
791 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
792 struct talloc_chunk *pool;
793 void *next_tc = NULL;
794 unsigned int *pool_object_count;
796 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
799 pool = (struct talloc_chunk *)tc->pool;
800 next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
802 TC_INVALIDATE_FULL_CHUNK(tc);
805 pool_object_count = talloc_pool_objectcount(pool);
807 if (unlikely(*pool_object_count == 0)) {
808 talloc_abort("Pool object count zero!");
812 *pool_object_count -= 1;
814 if (unlikely(*pool_object_count == 1)) {
816 * if there is just object left in the pool
817 * it means this is the pool itself and
818 * the rest is available for new objects
821 pool->pool = TC_POOL_FIRST_CHUNK(pool);
822 TC_INVALIDATE_POOL(pool);
823 } else if (unlikely(*pool_object_count == 0)) {
824 TC_INVALIDATE_FULL_CHUNK(pool);
826 } else if (pool->pool == next_tc) {
828 * if pool->pool still points to end of
829 * 'tc' (which is stored in the 'next_tc' variable),
830 * we can reclaim the memory of 'tc'.
835 TC_INVALIDATE_FULL_CHUNK(tc);
842 move a lump of memory from one talloc context to another return the
843 ptr on success, or NULL if it could not be transferred.
844 passing NULL as ptr will always return NULL with no side effects.
846 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
848 struct talloc_chunk *tc, *new_tc;
850 if (unlikely(!ptr)) {
854 if (unlikely(new_ctx == NULL)) {
855 new_ctx = null_context;
858 tc = talloc_chunk_from_ptr(ptr);
860 if (unlikely(new_ctx == NULL)) {
862 _TLIST_REMOVE(tc->parent->child, tc);
863 if (tc->parent->child) {
864 tc->parent->child->parent = tc->parent;
867 if (tc->prev) tc->prev->next = tc->next;
868 if (tc->next) tc->next->prev = tc->prev;
871 tc->parent = tc->next = tc->prev = NULL;
872 return discard_const_p(void, ptr);
875 new_tc = talloc_chunk_from_ptr(new_ctx);
877 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
878 return discard_const_p(void, ptr);
882 _TLIST_REMOVE(tc->parent->child, tc);
883 if (tc->parent->child) {
884 tc->parent->child->parent = tc->parent;
887 if (tc->prev) tc->prev->next = tc->next;
888 if (tc->next) tc->next->prev = tc->prev;
892 if (new_tc->child) new_tc->child->parent = NULL;
893 _TLIST_ADD(new_tc->child, tc);
895 return discard_const_p(void, ptr);
899 move a lump of memory from one talloc context to another return the
900 ptr on success, or NULL if it could not be transferred.
901 passing NULL as ptr will always return NULL with no side effects.
903 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
905 struct talloc_chunk *tc;
907 if (unlikely(ptr == NULL)) {
911 tc = talloc_chunk_from_ptr(ptr);
913 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
914 struct talloc_reference_handle *h;
916 talloc_log("WARNING: talloc_steal with references at %s\n",
919 for (h=tc->refs; h; h=h->next) {
920 talloc_log("\treference at %s\n",
926 /* this test is probably too expensive to have on in the
927 normal build, but it useful for debugging */
928 if (talloc_is_parent(new_ctx, ptr)) {
929 talloc_log("WARNING: stealing into talloc child at %s\n", location);
933 return _talloc_steal_internal(new_ctx, ptr);
937 this is like a talloc_steal(), but you must supply the old
938 parent. This resolves the ambiguity in a talloc_steal() which is
939 called on a context that has more than one parent (via references)
941 The old parent can be either a reference or a parent
943 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
945 struct talloc_chunk *tc;
946 struct talloc_reference_handle *h;
948 if (unlikely(ptr == NULL)) {
952 if (old_parent == talloc_parent(ptr)) {
953 return _talloc_steal_internal(new_parent, ptr);
956 tc = talloc_chunk_from_ptr(ptr);
957 for (h=tc->refs;h;h=h->next) {
958 if (talloc_parent(h) == old_parent) {
959 if (_talloc_steal_internal(new_parent, h) != h) {
962 return discard_const_p(void, ptr);
966 /* it wasn't a parent */
971 remove a secondary reference to a pointer. This undo's what
972 talloc_reference() has done. The context and pointer arguments
973 must match those given to a talloc_reference()
975 static inline int talloc_unreference(const void *context, const void *ptr)
977 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
978 struct talloc_reference_handle *h;
980 if (unlikely(context == NULL)) {
981 context = null_context;
984 for (h=tc->refs;h;h=h->next) {
985 struct talloc_chunk *p = talloc_parent_chunk(h);
987 if (context == NULL) break;
988 } else if (TC_PTR_FROM_CHUNK(p) == context) {
996 return _talloc_free_internal(h, __location__);
1000 remove a specific parent context from a pointer. This is a more
1001 controlled varient of talloc_free()
1003 _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
1005 struct talloc_chunk *tc_p, *new_p;
1012 if (context == NULL) {
1013 context = null_context;
1016 if (talloc_unreference(context, ptr) == 0) {
1020 if (context == NULL) {
1021 if (talloc_parent_chunk(ptr) != NULL) {
1025 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
1030 tc_p = talloc_chunk_from_ptr(ptr);
1032 if (tc_p->refs == NULL) {
1033 return _talloc_free_internal(ptr, __location__);
1036 new_p = talloc_parent_chunk(tc_p->refs);
1038 new_parent = TC_PTR_FROM_CHUNK(new_p);
1043 if (talloc_unreference(new_parent, ptr) != 0) {
1047 _talloc_steal_internal(new_parent, ptr);
1053 add a name to an existing pointer - va_list version
1055 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1057 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
1059 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1060 tc->name = talloc_vasprintf(ptr, fmt, ap);
1061 if (likely(tc->name)) {
1062 _talloc_set_name_const(tc->name, ".name");
1068 add a name to an existing pointer
1070 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
1075 name = talloc_set_name_v(ptr, fmt, ap);
1082 create a named talloc pointer. Any talloc pointer can be named, and
1083 talloc_named() operates just like talloc() except that it allows you
1084 to name the pointer.
1086 _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
1092 ptr = __talloc(context, size);
1093 if (unlikely(ptr == NULL)) return NULL;
1096 name = talloc_set_name_v(ptr, fmt, ap);
1099 if (unlikely(name == NULL)) {
1100 _talloc_free_internal(ptr, __location__);
1108 return the name of a talloc ptr, or "UNNAMED"
1110 _PUBLIC_ const char *talloc_get_name(const void *ptr)
1112 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1113 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
1114 return ".reference";
1116 if (likely(tc->name)) {
1124 check if a pointer has the given name. If it does, return the pointer,
1125 otherwise return NULL
1127 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
1130 if (unlikely(ptr == NULL)) return NULL;
1131 pname = talloc_get_name(ptr);
1132 if (likely(pname == name || strcmp(pname, name) == 0)) {
1133 return discard_const_p(void, ptr);
1138 static void talloc_abort_type_missmatch(const char *location,
1140 const char *expected)
1144 reason = talloc_asprintf(NULL,
1145 "%s: Type mismatch: name[%s] expected[%s]",
1150 reason = "Type mismatch";
1153 talloc_abort(reason);
1156 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
1160 if (unlikely(ptr == NULL)) {
1161 talloc_abort_type_missmatch(location, NULL, name);
1165 pname = talloc_get_name(ptr);
1166 if (likely(pname == name || strcmp(pname, name) == 0)) {
1167 return discard_const_p(void, ptr);
1170 talloc_abort_type_missmatch(location, pname, name);
1175 this is for compatibility with older versions of talloc
1177 _PUBLIC_ void *talloc_init(const char *fmt, ...)
1183 ptr = __talloc(NULL, 0);
1184 if (unlikely(ptr == NULL)) return NULL;
1187 name = talloc_set_name_v(ptr, fmt, ap);
1190 if (unlikely(name == NULL)) {
1191 _talloc_free_internal(ptr, __location__);
1199 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1200 should probably not be used in new code. It's in here to keep the talloc
1201 code consistent across Samba 3 and 4.
1203 _PUBLIC_ void talloc_free_children(void *ptr)
1205 struct talloc_chunk *tc;
1207 if (unlikely(ptr == NULL)) {
1211 tc = talloc_chunk_from_ptr(ptr);
1214 /* we need to work out who will own an abandoned child
1215 if it cannot be freed. In priority order, the first
1216 choice is owner of any remaining reference to this
1217 pointer, the second choice is our parent, and the
1218 final choice is the null context. */
1219 void *child = TC_PTR_FROM_CHUNK(tc->child);
1220 const void *new_parent = null_context;
1221 if (unlikely(tc->child->refs)) {
1222 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1223 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1225 if (unlikely(talloc_free(child) == -1)) {
1226 if (new_parent == null_context) {
1227 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1228 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1230 _talloc_steal_internal(new_parent, child);
1236 Allocate a bit of memory as a child of an existing pointer
1238 _PUBLIC_ void *_talloc(const void *context, size_t size)
1240 return __talloc(context, size);
1244 externally callable talloc_set_name_const()
1246 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1248 _talloc_set_name_const(ptr, name);
1252 create a named talloc pointer. Any talloc pointer can be named, and
1253 talloc_named() operates just like talloc() except that it allows you
1254 to name the pointer.
1256 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1258 return _talloc_named_const(context, size, name);
1262 free a talloc pointer. This also frees all child pointers of this
1265 return 0 if the memory is actually freed, otherwise -1. The memory
1266 will not be freed if the ref_count is > 1 or the destructor (if
1267 any) returns non-zero
1269 _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1271 struct talloc_chunk *tc;
1273 if (unlikely(ptr == NULL)) {
1277 tc = talloc_chunk_from_ptr(ptr);
1279 if (unlikely(tc->refs != NULL)) {
1280 struct talloc_reference_handle *h;
1282 if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1283 /* in this case we do know which parent should
1284 get this pointer, as there is really only
1286 return talloc_unlink(null_context, ptr);
1289 talloc_log("ERROR: talloc_free with references at %s\n",
1292 for (h=tc->refs; h; h=h->next) {
1293 talloc_log("\treference at %s\n",
1299 return _talloc_free_internal(ptr, location);
1305 A talloc version of realloc. The context argument is only used if
1308 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1310 struct talloc_chunk *tc;
1312 bool malloced = false;
1313 struct talloc_chunk *pool_tc = NULL;
1315 /* size zero is equivalent to free() */
1316 if (unlikely(size == 0)) {
1317 talloc_unlink(context, ptr);
1321 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1325 /* realloc(NULL) is equivalent to malloc() */
1327 return _talloc_named_const(context, size, name);
1330 tc = talloc_chunk_from_ptr(ptr);
1332 /* don't allow realloc on referenced pointers */
1333 if (unlikely(tc->refs)) {
1337 /* don't let anybody try to realloc a talloc_pool */
1338 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1342 /* don't let anybody try to realloc a talloc_pool */
1343 if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
1344 pool_tc = (struct talloc_chunk *)tc->pool;
1347 #if (ALWAYS_REALLOC == 0)
1348 /* don't shrink if we have less than 1k to gain */
1349 if (size < tc->size) {
1351 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1352 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1354 if (next_tc == pool_tc->pool) {
1355 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1358 } else if ((tc->size - size) < 1024) {
1359 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1360 /* do not shrink if we have less than 1k to gain */
1364 } else if (tc->size == size) {
1366 * do not change the pointer if it is exactly
1373 /* by resetting magic we catch users of the old memory */
1374 tc->flags |= TALLOC_FLAG_FREE;
1378 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1379 *talloc_pool_objectcount(pool_tc) -= 1;
1381 if (new_ptr == NULL) {
1382 new_ptr = malloc(TC_HDR_SIZE+size);
1387 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1388 TC_INVALIDATE_FULL_CHUNK(tc);
1391 new_ptr = malloc(size + TC_HDR_SIZE);
1393 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1399 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1400 size_t old_chunk_size = TC_POOLMEM_CHUNK_SIZE(tc);
1401 size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
1402 size_t space_needed;
1405 if (*talloc_pool_objectcount(pool_tc) == 2) {
1407 * optimize for the case where 'tc' is the only
1408 * chunk in the pool.
1410 space_needed = new_chunk_size;
1411 space_left = pool_tc->size - TALLOC_POOL_HDR_SIZE;
1413 if (space_left >= space_needed) {
1414 size_t old_used = TC_HDR_SIZE + tc->size;
1415 size_t new_used = TC_HDR_SIZE + size;
1416 pool_tc->pool = TC_POOL_FIRST_CHUNK(pool_tc);
1417 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
1419 * we need to prepare the memmove into
1420 * the unaccessable area.
1423 size_t diff = PTR_DIFF(tc, pool_tc->pool);
1424 size_t flen = MIN(diff, old_used);
1425 char *fptr = (char *)pool_tc->pool;
1426 VALGRIND_MAKE_MEM_UNDEFINED(fptr, flen);
1429 memmove(pool_tc->pool, tc, old_used);
1430 new_ptr = pool_tc->pool;
1432 TC_UNDEFINE_GROW_CHUNK(tc, size);
1435 * first we do not align the pool pointer
1436 * because we want to invalidate the padding
1439 pool_tc->pool = new_used + (char *)new_ptr;
1440 TC_INVALIDATE_POOL(pool_tc);
1442 /* now the aligned pointer */
1443 pool_tc->pool = new_chunk_size + (char *)new_ptr;
1450 if (new_chunk_size == old_chunk_size) {
1451 TC_UNDEFINE_GROW_CHUNK(tc, size);
1452 tc->flags &= ~TALLOC_FLAG_FREE;
1457 if (next_tc == pool_tc->pool) {
1459 * optimize for the case where 'tc' is the last
1460 * chunk in the pool.
1462 space_needed = new_chunk_size - old_chunk_size;
1463 space_left = TC_POOL_SPACE_LEFT(pool_tc);
1465 if (space_left >= space_needed) {
1466 TC_UNDEFINE_GROW_CHUNK(tc, size);
1467 tc->flags &= ~TALLOC_FLAG_FREE;
1469 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1474 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1475 *talloc_pool_objectcount(pool_tc) -= 1;
1477 if (new_ptr == NULL) {
1478 new_ptr = malloc(TC_HDR_SIZE+size);
1483 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1484 TC_INVALIDATE_FULL_CHUNK(tc);
1486 if (*talloc_pool_objectcount(pool_tc) == 1) {
1488 * If the pool is empty now reclaim everything.
1490 pool_tc->pool = TC_POOL_FIRST_CHUNK(pool_tc);
1491 TC_INVALIDATE_POOL(pool_tc);
1492 } else if (next_tc == pool_tc->pool) {
1494 * If it was reallocated and tc was the last
1495 * chunk, we can reclaim the memory of tc.
1502 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1506 if (unlikely(!new_ptr)) {
1507 tc->flags &= ~TALLOC_FLAG_FREE;
1511 tc = (struct talloc_chunk *)new_ptr;
1512 tc->flags &= ~TALLOC_FLAG_FREE;
1514 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1517 tc->parent->child = tc;
1520 tc->child->parent = tc;
1524 tc->prev->next = tc;
1527 tc->next->prev = tc;
1531 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1533 return TC_PTR_FROM_CHUNK(tc);
1537 a wrapper around talloc_steal() for situations where you are moving a pointer
1538 between two structures, and want the old pointer to be set to NULL
1540 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
1542 const void **pptr = discard_const_p(const void *,_pptr);
1543 void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1549 return the total size of a talloc pool (subtree)
1551 _PUBLIC_ size_t talloc_total_size(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;
1571 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1574 for (c=tc->child;c;c=c->next) {
1575 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1578 tc->flags &= ~TALLOC_FLAG_LOOP;
1584 return the total number of blocks in a talloc pool (subtree)
1586 _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
1589 struct talloc_chunk *c, *tc;
1598 tc = talloc_chunk_from_ptr(ptr);
1600 if (tc->flags & TALLOC_FLAG_LOOP) {
1604 tc->flags |= TALLOC_FLAG_LOOP;
1607 for (c=tc->child;c;c=c->next) {
1608 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1611 tc->flags &= ~TALLOC_FLAG_LOOP;
1617 return the number of external references to a pointer
1619 _PUBLIC_ size_t talloc_reference_count(const void *ptr)
1621 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1622 struct talloc_reference_handle *h;
1625 for (h=tc->refs;h;h=h->next) {
1632 report on memory usage by all children of a pointer, giving a full tree view
1634 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1635 void (*callback)(const void *ptr,
1636 int depth, int max_depth,
1638 void *private_data),
1641 struct talloc_chunk *c, *tc;
1646 if (ptr == NULL) return;
1648 tc = talloc_chunk_from_ptr(ptr);
1650 if (tc->flags & TALLOC_FLAG_LOOP) {
1654 callback(ptr, depth, max_depth, 0, private_data);
1656 if (max_depth >= 0 && depth >= max_depth) {
1660 tc->flags |= TALLOC_FLAG_LOOP;
1661 for (c=tc->child;c;c=c->next) {
1662 if (c->name == TALLOC_MAGIC_REFERENCE) {
1663 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1664 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1666 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1669 tc->flags &= ~TALLOC_FLAG_LOOP;
1672 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1674 const char *name = talloc_get_name(ptr);
1675 FILE *f = (FILE *)_f;
1678 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1683 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1684 (max_depth < 0 ? "full " :""), name,
1685 (unsigned long)talloc_total_size(ptr),
1686 (unsigned long)talloc_total_blocks(ptr));
1690 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1693 (unsigned long)talloc_total_size(ptr),
1694 (unsigned long)talloc_total_blocks(ptr),
1695 (int)talloc_reference_count(ptr), ptr);
1698 fprintf(f, "content: ");
1699 if (talloc_total_size(ptr)) {
1700 int tot = talloc_total_size(ptr);
1703 for (i = 0; i < tot; i++) {
1704 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1705 fprintf(f, "%c", ((char *)ptr)[i]);
1707 fprintf(f, "~%02x", ((char *)ptr)[i]);
1716 report on memory usage by all children of a pointer, giving a full tree view
1718 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1721 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1727 report on memory usage by all children of a pointer, giving a full tree view
1729 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
1731 talloc_report_depth_file(ptr, 0, -1, f);
1735 report on memory usage by all children of a pointer
1737 _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
1739 talloc_report_depth_file(ptr, 0, 1, f);
1743 report on any memory hanging off the null context
1745 static void talloc_report_null(void)
1747 if (talloc_total_size(null_context) != 0) {
1748 talloc_report(null_context, stderr);
1753 report on any memory hanging off the null context
1755 static void talloc_report_null_full(void)
1757 if (talloc_total_size(null_context) != 0) {
1758 talloc_report_full(null_context, stderr);
1763 enable tracking of the NULL context
1765 _PUBLIC_ void talloc_enable_null_tracking(void)
1767 if (null_context == NULL) {
1768 null_context = _talloc_named_const(NULL, 0, "null_context");
1769 if (autofree_context != NULL) {
1770 talloc_reparent(NULL, null_context, autofree_context);
1776 enable tracking of the NULL context, not moving the autofree context
1777 into the NULL context. This is needed for the talloc testsuite
1779 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
1781 if (null_context == NULL) {
1782 null_context = _talloc_named_const(NULL, 0, "null_context");
1787 disable tracking of the NULL context
1789 _PUBLIC_ void talloc_disable_null_tracking(void)
1791 if (null_context != NULL) {
1792 /* we have to move any children onto the real NULL
1794 struct talloc_chunk *tc, *tc2;
1795 tc = talloc_chunk_from_ptr(null_context);
1796 for (tc2 = tc->child; tc2; tc2=tc2->next) {
1797 if (tc2->parent == tc) tc2->parent = NULL;
1798 if (tc2->prev == tc) tc2->prev = NULL;
1800 for (tc2 = tc->next; tc2; tc2=tc2->next) {
1801 if (tc2->parent == tc) tc2->parent = NULL;
1802 if (tc2->prev == tc) tc2->prev = NULL;
1807 talloc_free(null_context);
1808 null_context = NULL;
1812 enable leak reporting on exit
1814 _PUBLIC_ void talloc_enable_leak_report(void)
1816 talloc_enable_null_tracking();
1817 atexit(talloc_report_null);
1821 enable full leak reporting on exit
1823 _PUBLIC_ void talloc_enable_leak_report_full(void)
1825 talloc_enable_null_tracking();
1826 atexit(talloc_report_null_full);
1830 talloc and zero memory.
1832 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
1834 void *p = _talloc_named_const(ctx, size, name);
1837 memset(p, '\0', size);
1844 memdup with a talloc.
1846 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1848 void *newp = _talloc_named_const(t, size, name);
1851 memcpy(newp, p, size);
1857 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1861 ret = (char *)__talloc(t, len + 1);
1862 if (unlikely(!ret)) return NULL;
1864 memcpy(ret, p, len);
1867 _talloc_set_name_const(ret, ret);
1872 strdup with a talloc
1874 _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
1876 if (unlikely(!p)) return NULL;
1877 return __talloc_strlendup(t, p, strlen(p));
1881 strndup with a talloc
1883 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
1885 if (unlikely(!p)) return NULL;
1886 return __talloc_strlendup(t, p, strnlen(p, n));
1889 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1890 const char *a, size_t alen)
1894 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1895 if (unlikely(!ret)) return NULL;
1897 /* append the string and the trailing \0 */
1898 memcpy(&ret[slen], a, alen);
1901 _talloc_set_name_const(ret, ret);
1906 * Appends at the end of the string.
1908 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
1911 return talloc_strdup(NULL, a);
1918 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1922 * Appends at the end of the talloc'ed buffer,
1923 * not the end of the string.
1925 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
1930 return talloc_strdup(NULL, a);
1937 slen = talloc_get_size(s);
1938 if (likely(slen > 0)) {
1942 return __talloc_strlendup_append(s, slen, a, strlen(a));
1946 * Appends at the end of the string.
1948 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
1951 return talloc_strdup(NULL, a);
1958 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1962 * Appends at the end of the talloc'ed buffer,
1963 * not the end of the string.
1965 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1970 return talloc_strdup(NULL, a);
1977 slen = talloc_get_size(s);
1978 if (likely(slen > 0)) {
1982 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1985 #ifndef HAVE_VA_COPY
1986 #ifdef HAVE___VA_COPY
1987 #define va_copy(dest, src) __va_copy(dest, src)
1989 #define va_copy(dest, src) (dest) = (src)
1993 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
2000 /* this call looks strange, but it makes it work on older solaris boxes */
2002 len = vsnprintf(&c, 1, fmt, ap2);
2004 if (unlikely(len < 0)) {
2008 ret = (char *)__talloc(t, len+1);
2009 if (unlikely(!ret)) return NULL;
2012 vsnprintf(ret, len+1, fmt, ap2);
2015 _talloc_set_name_const(ret, ret);
2021 Perform string formatting, and return a pointer to newly allocated
2022 memory holding the result, inside a memory pool.
2024 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
2030 ret = talloc_vasprintf(t, fmt, ap);
2035 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2036 const char *fmt, va_list ap)
2037 PRINTF_ATTRIBUTE(3,0);
2039 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2040 const char *fmt, va_list ap)
2047 alen = vsnprintf(&c, 1, fmt, ap2);
2051 /* Either the vsnprintf failed or the format resulted in
2052 * no characters being formatted. In the former case, we
2053 * ought to return NULL, in the latter we ought to return
2054 * the original string. Most current callers of this
2055 * function expect it to never return NULL.
2060 s = talloc_realloc(NULL, s, char, slen + alen + 1);
2061 if (!s) return NULL;
2064 vsnprintf(s + slen, alen + 1, fmt, ap2);
2067 _talloc_set_name_const(s, s);
2072 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2073 * and return @p s, which may have moved. Good for gradually
2074 * accumulating output into a string buffer. Appends at the end
2077 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
2080 return talloc_vasprintf(NULL, fmt, ap);
2083 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
2087 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2088 * and return @p s, which may have moved. Always appends at the
2089 * end of the talloc'ed buffer, not the end of the string.
2091 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
2096 return talloc_vasprintf(NULL, fmt, ap);
2099 slen = talloc_get_size(s);
2100 if (likely(slen > 0)) {
2104 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
2108 Realloc @p s to append the formatted result of @p fmt and return @p
2109 s, which may have moved. Good for gradually accumulating output
2110 into a string buffer.
2112 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
2117 s = talloc_vasprintf_append(s, fmt, ap);
2123 Realloc @p s to append the formatted result of @p fmt and return @p
2124 s, which may have moved. Good for gradually accumulating output
2127 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
2132 s = talloc_vasprintf_append_buffer(s, fmt, ap);
2138 alloc an array, checking for integer overflow in the array size
2140 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2142 if (count >= MAX_TALLOC_SIZE/el_size) {
2145 return _talloc_named_const(ctx, el_size * count, name);
2149 alloc an zero array, checking for integer overflow in the array size
2151 _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2153 if (count >= MAX_TALLOC_SIZE/el_size) {
2156 return _talloc_zero(ctx, el_size * count, name);
2160 realloc an array, checking for integer overflow in the array size
2162 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
2164 if (count >= MAX_TALLOC_SIZE/el_size) {
2167 return _talloc_realloc(ctx, ptr, el_size * count, name);
2171 a function version of talloc_realloc(), so it can be passed as a function pointer
2172 to libraries that want a realloc function (a realloc function encapsulates
2173 all the basic capabilities of an allocation library, which is why this is useful)
2175 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
2177 return _talloc_realloc(context, ptr, size, NULL);
2181 static int talloc_autofree_destructor(void *ptr)
2183 autofree_context = NULL;
2187 static void talloc_autofree(void)
2189 talloc_free(autofree_context);
2193 return a context which will be auto-freed on exit
2194 this is useful for reducing the noise in leak reports
2196 _PUBLIC_ void *talloc_autofree_context(void)
2198 if (autofree_context == NULL) {
2199 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
2200 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
2201 atexit(talloc_autofree);
2203 return autofree_context;
2206 _PUBLIC_ size_t talloc_get_size(const void *context)
2208 struct talloc_chunk *tc;
2210 if (context == NULL) {
2211 context = null_context;
2213 if (context == NULL) {
2217 tc = talloc_chunk_from_ptr(context);
2223 find a parent of this context that has the given name, if any
2225 _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
2227 struct talloc_chunk *tc;
2229 if (context == NULL) {
2233 tc = talloc_chunk_from_ptr(context);
2235 if (tc->name && strcmp(tc->name, name) == 0) {
2236 return TC_PTR_FROM_CHUNK(tc);
2238 while (tc && tc->prev) tc = tc->prev;
2247 show the parentage of a context
2249 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
2251 struct talloc_chunk *tc;
2253 if (context == NULL) {
2254 fprintf(file, "talloc no parents for NULL\n");
2258 tc = talloc_chunk_from_ptr(context);
2259 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
2261 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
2262 while (tc && tc->prev) tc = tc->prev;
2271 return 1 if ptr is a parent of context
2273 static int _talloc_is_parent(const void *context, const void *ptr, int depth)
2275 struct talloc_chunk *tc;
2277 if (context == NULL) {
2281 tc = talloc_chunk_from_ptr(context);
2282 while (tc && depth > 0) {
2283 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2284 while (tc && tc->prev) tc = tc->prev;
2294 return 1 if ptr is a parent of context
2296 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2298 return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);