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/
37 #ifdef TALLOC_BUILD_VERSION_MAJOR
38 #if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)
39 #error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"
43 #ifdef TALLOC_BUILD_VERSION_MINOR
44 #if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)
45 #error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"
49 /* use this to force every realloc to change the pointer, to stress test
50 code that might not cope */
51 #define ALWAYS_REALLOC 0
54 #define MAX_TALLOC_SIZE 0x10000000
55 #define TALLOC_MAGIC_BASE 0xe814ec70
56 #define TALLOC_MAGIC ( \
58 (TALLOC_VERSION_MAJOR << 12) + \
59 (TALLOC_VERSION_MINOR << 4) \
62 #define TALLOC_FLAG_FREE 0x01
63 #define TALLOC_FLAG_LOOP 0x02
64 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
65 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
66 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
68 /* by default we abort when given a bad pointer (such as when talloc_free() is called
69 on a pointer that came from malloc() */
71 #define TALLOC_ABORT(reason) abort()
74 #ifndef discard_const_p
75 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
76 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
78 # define discard_const_p(type, ptr) ((type *)(ptr))
82 /* these macros gain us a few percent of speed on gcc */
84 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
85 as its first argument */
87 #define likely(x) __builtin_expect(!!(x), 1)
90 #define unlikely(x) __builtin_expect(!!(x), 0)
97 #define unlikely(x) (x)
101 /* this null_context is only used if talloc_enable_leak_report() or
102 talloc_enable_leak_report_full() is called, otherwise it remains
105 static void *null_context;
106 static void *autofree_context;
108 struct talloc_reference_handle {
109 struct talloc_reference_handle *next, *prev;
111 const char *location;
114 typedef int (*talloc_destructor_t)(void *);
116 struct talloc_chunk {
117 struct talloc_chunk *next, *prev;
118 struct talloc_chunk *parent, *child;
119 struct talloc_reference_handle *refs;
120 talloc_destructor_t destructor;
126 * "pool" has dual use:
128 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
129 * marks the end of the currently allocated area.
131 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
132 * is a pointer to the struct talloc_chunk of the pool that it was
133 * allocated from. This way children can quickly find the pool to chew
139 /* 16 byte alignment seems to keep everyone happy */
140 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
141 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
143 _PUBLIC_ int talloc_version_major(void)
145 return TALLOC_VERSION_MAJOR;
148 _PUBLIC_ int talloc_version_minor(void)
150 return TALLOC_VERSION_MINOR;
153 static void (*talloc_log_fn)(const char *message);
155 _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
157 talloc_log_fn = log_fn;
160 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
161 static void talloc_log(const char *fmt, ...)
166 if (!talloc_log_fn) {
171 message = talloc_vasprintf(NULL, fmt, ap);
174 talloc_log_fn(message);
175 talloc_free(message);
178 static void talloc_log_stderr(const char *message)
180 fprintf(stderr, "%s", message);
183 _PUBLIC_ void talloc_set_log_stderr(void)
185 talloc_set_log_fn(talloc_log_stderr);
188 static void (*talloc_abort_fn)(const char *reason);
190 _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
192 talloc_abort_fn = abort_fn;
195 static void talloc_abort(const char *reason)
197 talloc_log("%s\n", reason);
199 if (!talloc_abort_fn) {
200 TALLOC_ABORT(reason);
203 talloc_abort_fn(reason);
206 static void talloc_abort_magic(unsigned magic)
208 unsigned striped = magic - TALLOC_MAGIC_BASE;
209 unsigned major = (striped & 0xFFFFF000) >> 12;
210 unsigned minor = (striped & 0x00000FF0) >> 4;
211 talloc_log("Bad talloc magic[0x%08X/%u/%u] expected[0x%08X/%u/%u]\n",
213 TALLOC_MAGIC, TALLOC_VERSION_MAJOR, TALLOC_VERSION_MINOR);
214 talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
217 static void talloc_abort_double_free(void)
219 talloc_abort("Bad talloc magic value - double free");
222 static void talloc_abort_unknown_value(void)
224 talloc_abort("Bad talloc magic value - unknown value");
227 /* panic if we get a bad magic value */
228 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
230 const char *pp = (const char *)ptr;
231 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
232 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
233 if ((tc->flags & (~0xFFF)) == TALLOC_MAGIC_BASE) {
234 talloc_abort_magic(tc->flags & (~0xF));
238 if (tc->flags & TALLOC_FLAG_FREE) {
239 talloc_log("talloc: double free error - first free may be at %s\n", tc->name);
240 talloc_abort_double_free();
243 talloc_abort_unknown_value();
250 /* hook into the front of the list */
251 #define _TLIST_ADD(list, p) \
255 (p)->next = (p)->prev = NULL; \
257 (list)->prev = (p); \
258 (p)->next = (list); \
264 /* remove an element from a list - element doesn't have to be in list. */
265 #define _TLIST_REMOVE(list, p) \
267 if ((p) == (list)) { \
268 (list) = (p)->next; \
269 if (list) (list)->prev = NULL; \
271 if ((p)->prev) (p)->prev->next = (p)->next; \
272 if ((p)->next) (p)->next->prev = (p)->prev; \
274 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
279 return the parent chunk of a pointer
281 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
283 struct talloc_chunk *tc;
285 if (unlikely(ptr == NULL)) {
289 tc = talloc_chunk_from_ptr(ptr);
290 while (tc->prev) tc=tc->prev;
295 _PUBLIC_ void *talloc_parent(const void *ptr)
297 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
298 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
304 _PUBLIC_ const char *talloc_parent_name(const void *ptr)
306 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
307 return tc? tc->name : NULL;
311 A pool carries an in-pool object count count in the first 16 bytes.
312 bytes. This is done to support talloc_steal() to a parent outside of the
313 pool. The count includes the pool itself, so a talloc_free() on a pool will
314 only destroy the pool if the count has dropped to zero. A talloc_free() of a
315 pool member will reduce the count, and eventually also call free(3) on the
318 The object count is not put into "struct talloc_chunk" because it is only
319 relevant for talloc pools and the alignment to 16 bytes would increase the
320 memory footprint of each talloc chunk by those 16 bytes.
323 #define TALLOC_POOL_HDR_SIZE 16
325 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
327 return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
334 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
337 struct talloc_chunk *pool_ctx = NULL;
339 struct talloc_chunk *result;
342 if (parent == NULL) {
346 if (parent->flags & TALLOC_FLAG_POOL) {
349 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
350 pool_ctx = (struct talloc_chunk *)parent->pool;
353 if (pool_ctx == NULL) {
357 space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
358 - ((char *)pool_ctx->pool);
361 * Align size to 16 bytes
363 chunk_size = ((size + 15) & ~15);
365 if (space_left < chunk_size) {
369 result = (struct talloc_chunk *)pool_ctx->pool;
371 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
372 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
375 pool_ctx->pool = (void *)((char *)result + chunk_size);
377 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
378 result->pool = pool_ctx;
380 *talloc_pool_objectcount(pool_ctx) += 1;
386 Allocate a bit of memory as a child of an existing pointer
388 static inline void *__talloc(const void *context, size_t size)
390 struct talloc_chunk *tc = NULL;
392 if (unlikely(context == NULL)) {
393 context = null_context;
396 if (unlikely(size >= MAX_TALLOC_SIZE)) {
400 if (context != NULL) {
401 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
406 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
407 if (unlikely(tc == NULL)) return NULL;
408 tc->flags = TALLOC_MAGIC;
413 tc->destructor = NULL;
418 if (likely(context)) {
419 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
422 parent->child->parent = NULL;
423 tc->next = parent->child;
432 tc->next = tc->prev = tc->parent = NULL;
435 return TC_PTR_FROM_CHUNK(tc);
439 * Create a talloc pool
442 _PUBLIC_ void *talloc_pool(const void *context, size_t size)
444 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
445 struct talloc_chunk *tc;
447 if (unlikely(result == NULL)) {
451 tc = talloc_chunk_from_ptr(result);
453 tc->flags |= TALLOC_FLAG_POOL;
454 tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
456 *talloc_pool_objectcount(tc) = 1;
458 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
459 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
466 setup a destructor to be called on free of a pointer
467 the destructor should return 0 on success, or -1 on failure.
468 if the destructor fails then the free is failed, and the memory can
469 be continued to be used
471 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
473 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
474 tc->destructor = destructor;
478 increase the reference count on a piece of memory.
480 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
482 if (unlikely(!talloc_reference(null_context, ptr))) {
489 helper for talloc_reference()
491 this is referenced by a function pointer and should not be inline
493 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
495 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
496 _TLIST_REMOVE(ptr_tc->refs, handle);
501 more efficient way to add a name to a pointer - the name must point to a
504 static inline void _talloc_set_name_const(const void *ptr, const char *name)
506 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
511 internal talloc_named_const()
513 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
517 ptr = __talloc(context, size);
518 if (unlikely(ptr == NULL)) {
522 _talloc_set_name_const(ptr, name);
528 make a secondary reference to a pointer, hanging off the given context.
529 the pointer remains valid until both the original caller and this given
532 the major use for this is when two different structures need to reference the
533 same underlying data, and you want to be able to free the two instances separately,
536 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
538 struct talloc_chunk *tc;
539 struct talloc_reference_handle *handle;
540 if (unlikely(ptr == NULL)) return NULL;
542 tc = talloc_chunk_from_ptr(ptr);
543 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
544 sizeof(struct talloc_reference_handle),
545 TALLOC_MAGIC_REFERENCE);
546 if (unlikely(handle == NULL)) return NULL;
548 /* note that we hang the destructor off the handle, not the
549 main context as that allows the caller to still setup their
550 own destructor on the context if they want to */
551 talloc_set_destructor(handle, talloc_reference_destructor);
552 handle->ptr = discard_const_p(void, ptr);
553 handle->location = location;
554 _TLIST_ADD(tc->refs, handle);
558 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
561 internal talloc_free call
563 static inline int _talloc_free_internal(void *ptr, const char *location)
565 struct talloc_chunk *tc;
567 if (unlikely(ptr == NULL)) {
571 tc = talloc_chunk_from_ptr(ptr);
573 if (unlikely(tc->refs)) {
575 /* check this is a reference from a child or grantchild
576 * back to it's parent or grantparent
578 * in that case we need to remove the reference and
579 * call another instance of talloc_free() on the current
582 is_child = talloc_is_parent(tc->refs, ptr);
583 _talloc_free_internal(tc->refs, location);
585 return _talloc_free_internal(ptr, location);
590 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
591 /* we have a free loop - stop looping */
595 if (unlikely(tc->destructor)) {
596 talloc_destructor_t d = tc->destructor;
597 if (d == (talloc_destructor_t)-1) {
600 tc->destructor = (talloc_destructor_t)-1;
605 tc->destructor = NULL;
609 _TLIST_REMOVE(tc->parent->child, tc);
610 if (tc->parent->child) {
611 tc->parent->child->parent = tc->parent;
614 if (tc->prev) tc->prev->next = tc->next;
615 if (tc->next) tc->next->prev = tc->prev;
618 tc->flags |= TALLOC_FLAG_LOOP;
621 /* we need to work out who will own an abandoned child
622 if it cannot be freed. In priority order, the first
623 choice is owner of any remaining reference to this
624 pointer, the second choice is our parent, and the
625 final choice is the null context. */
626 void *child = TC_PTR_FROM_CHUNK(tc->child);
627 const void *new_parent = null_context;
628 if (unlikely(tc->child->refs)) {
629 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
630 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
632 if (unlikely(_talloc_free_internal(child, location) == -1)) {
633 if (new_parent == null_context) {
634 struct talloc_chunk *p = talloc_parent_chunk(ptr);
635 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
637 _talloc_steal_internal(new_parent, child);
641 tc->flags |= TALLOC_FLAG_FREE;
643 /* we mark the freed memory with where we called the free
644 * from. This means on a double free error we can report where
645 * the first free came from
649 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
650 struct talloc_chunk *pool;
651 unsigned int *pool_object_count;
653 pool = (tc->flags & TALLOC_FLAG_POOL)
654 ? tc : (struct talloc_chunk *)tc->pool;
656 pool_object_count = talloc_pool_objectcount(pool);
658 if (*pool_object_count == 0) {
659 talloc_abort("Pool object count zero!");
663 *pool_object_count -= 1;
665 if (*pool_object_count == 0) {
676 move a lump of memory from one talloc context to another return the
677 ptr on success, or NULL if it could not be transferred.
678 passing NULL as ptr will always return NULL with no side effects.
680 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
682 struct talloc_chunk *tc, *new_tc;
684 if (unlikely(!ptr)) {
688 if (unlikely(new_ctx == NULL)) {
689 new_ctx = null_context;
692 tc = talloc_chunk_from_ptr(ptr);
694 if (unlikely(new_ctx == NULL)) {
696 _TLIST_REMOVE(tc->parent->child, tc);
697 if (tc->parent->child) {
698 tc->parent->child->parent = tc->parent;
701 if (tc->prev) tc->prev->next = tc->next;
702 if (tc->next) tc->next->prev = tc->prev;
705 tc->parent = tc->next = tc->prev = NULL;
706 return discard_const_p(void, ptr);
709 new_tc = talloc_chunk_from_ptr(new_ctx);
711 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
712 return discard_const_p(void, ptr);
716 _TLIST_REMOVE(tc->parent->child, tc);
717 if (tc->parent->child) {
718 tc->parent->child->parent = tc->parent;
721 if (tc->prev) tc->prev->next = tc->next;
722 if (tc->next) tc->next->prev = tc->prev;
726 if (new_tc->child) new_tc->child->parent = NULL;
727 _TLIST_ADD(new_tc->child, tc);
729 return discard_const_p(void, ptr);
733 move a lump of memory from one talloc context to another return the
734 ptr on success, or NULL if it could not be transferred.
735 passing NULL as ptr will always return NULL with no side effects.
737 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
739 struct talloc_chunk *tc;
741 if (unlikely(ptr == NULL)) {
745 tc = talloc_chunk_from_ptr(ptr);
747 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
748 struct talloc_reference_handle *h;
750 talloc_log("WARNING: talloc_steal with references at %s\n",
753 for (h=tc->refs; h; h=h->next) {
754 talloc_log("\treference at %s\n",
760 /* this test is probably too expensive to have on in the
761 normal build, but it useful for debugging */
762 if (talloc_is_parent(new_ctx, ptr)) {
763 talloc_log("WARNING: stealing into talloc child at %s\n", location);
767 return _talloc_steal_internal(new_ctx, ptr);
771 this is like a talloc_steal(), but you must supply the old
772 parent. This resolves the ambiguity in a talloc_steal() which is
773 called on a context that has more than one parent (via references)
775 The old parent can be either a reference or a parent
777 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
779 struct talloc_chunk *tc;
780 struct talloc_reference_handle *h;
782 if (unlikely(ptr == NULL)) {
786 if (old_parent == talloc_parent(ptr)) {
787 return _talloc_steal_internal(new_parent, ptr);
790 tc = talloc_chunk_from_ptr(ptr);
791 for (h=tc->refs;h;h=h->next) {
792 if (talloc_parent(h) == old_parent) {
793 if (_talloc_steal_internal(new_parent, h) != h) {
796 return discard_const_p(void, ptr);
800 /* it wasn't a parent */
805 remove a secondary reference to a pointer. This undo's what
806 talloc_reference() has done. The context and pointer arguments
807 must match those given to a talloc_reference()
809 static inline int talloc_unreference(const void *context, const void *ptr)
811 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
812 struct talloc_reference_handle *h;
814 if (unlikely(context == NULL)) {
815 context = null_context;
818 for (h=tc->refs;h;h=h->next) {
819 struct talloc_chunk *p = talloc_parent_chunk(h);
821 if (context == NULL) break;
822 } else if (TC_PTR_FROM_CHUNK(p) == context) {
830 return _talloc_free_internal(h, __location__);
834 remove a specific parent context from a pointer. This is a more
835 controlled varient of talloc_free()
837 _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
839 struct talloc_chunk *tc_p, *new_p;
846 if (context == NULL) {
847 context = null_context;
850 if (talloc_unreference(context, ptr) == 0) {
854 if (context == NULL) {
855 if (talloc_parent_chunk(ptr) != NULL) {
859 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
864 tc_p = talloc_chunk_from_ptr(ptr);
866 if (tc_p->refs == NULL) {
867 return _talloc_free_internal(ptr, __location__);
870 new_p = talloc_parent_chunk(tc_p->refs);
872 new_parent = TC_PTR_FROM_CHUNK(new_p);
877 if (talloc_unreference(new_parent, ptr) != 0) {
881 _talloc_steal_internal(new_parent, ptr);
887 add a name to an existing pointer - va_list version
889 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
891 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
893 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
894 tc->name = talloc_vasprintf(ptr, fmt, ap);
895 if (likely(tc->name)) {
896 _talloc_set_name_const(tc->name, ".name");
902 add a name to an existing pointer
904 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
909 name = talloc_set_name_v(ptr, fmt, ap);
916 create a named talloc pointer. Any talloc pointer can be named, and
917 talloc_named() operates just like talloc() except that it allows you
920 _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
926 ptr = __talloc(context, size);
927 if (unlikely(ptr == NULL)) return NULL;
930 name = talloc_set_name_v(ptr, fmt, ap);
933 if (unlikely(name == NULL)) {
934 _talloc_free_internal(ptr, __location__);
942 return the name of a talloc ptr, or "UNNAMED"
944 _PUBLIC_ const char *talloc_get_name(const void *ptr)
946 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
947 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
950 if (likely(tc->name)) {
958 check if a pointer has the given name. If it does, return the pointer,
959 otherwise return NULL
961 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
964 if (unlikely(ptr == NULL)) return NULL;
965 pname = talloc_get_name(ptr);
966 if (likely(pname == name || strcmp(pname, name) == 0)) {
967 return discard_const_p(void, ptr);
972 static void talloc_abort_type_missmatch(const char *location,
974 const char *expected)
978 reason = talloc_asprintf(NULL,
979 "%s: Type mismatch: name[%s] expected[%s]",
984 reason = "Type mismatch";
987 talloc_abort(reason);
990 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
994 if (unlikely(ptr == NULL)) {
995 talloc_abort_type_missmatch(location, NULL, name);
999 pname = talloc_get_name(ptr);
1000 if (likely(pname == name || strcmp(pname, name) == 0)) {
1001 return discard_const_p(void, ptr);
1004 talloc_abort_type_missmatch(location, pname, name);
1009 this is for compatibility with older versions of talloc
1011 _PUBLIC_ void *talloc_init(const char *fmt, ...)
1018 * samba3 expects talloc_report_depth_cb(NULL, ...)
1019 * reports all talloc'ed memory, so we need to enable
1022 talloc_enable_null_tracking();
1024 ptr = __talloc(NULL, 0);
1025 if (unlikely(ptr == NULL)) return NULL;
1028 name = talloc_set_name_v(ptr, fmt, ap);
1031 if (unlikely(name == NULL)) {
1032 _talloc_free_internal(ptr, __location__);
1040 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1041 should probably not be used in new code. It's in here to keep the talloc
1042 code consistent across Samba 3 and 4.
1044 _PUBLIC_ void talloc_free_children(void *ptr)
1046 struct talloc_chunk *tc;
1048 if (unlikely(ptr == NULL)) {
1052 tc = talloc_chunk_from_ptr(ptr);
1055 /* we need to work out who will own an abandoned child
1056 if it cannot be freed. In priority order, the first
1057 choice is owner of any remaining reference to this
1058 pointer, the second choice is our parent, and the
1059 final choice is the null context. */
1060 void *child = TC_PTR_FROM_CHUNK(tc->child);
1061 const void *new_parent = null_context;
1062 if (unlikely(tc->child->refs)) {
1063 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1064 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1066 if (unlikely(talloc_free(child) == -1)) {
1067 if (new_parent == null_context) {
1068 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1069 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1071 _talloc_steal_internal(new_parent, child);
1075 if ((tc->flags & TALLOC_FLAG_POOL)
1076 && (*talloc_pool_objectcount(tc) == 1)) {
1077 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
1078 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
1079 VALGRIND_MAKE_MEM_NOACCESS(
1080 tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
1086 Allocate a bit of memory as a child of an existing pointer
1088 _PUBLIC_ void *_talloc(const void *context, size_t size)
1090 return __talloc(context, size);
1094 externally callable talloc_set_name_const()
1096 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1098 _talloc_set_name_const(ptr, name);
1102 create a named talloc pointer. Any talloc pointer can be named, and
1103 talloc_named() operates just like talloc() except that it allows you
1104 to name the pointer.
1106 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1108 return _talloc_named_const(context, size, name);
1112 free a talloc pointer. This also frees all child pointers of this
1115 return 0 if the memory is actually freed, otherwise -1. The memory
1116 will not be freed if the ref_count is > 1 or the destructor (if
1117 any) returns non-zero
1119 _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1121 struct talloc_chunk *tc;
1123 if (unlikely(ptr == NULL)) {
1127 tc = talloc_chunk_from_ptr(ptr);
1129 if (unlikely(tc->refs != NULL)) {
1130 struct talloc_reference_handle *h;
1132 if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1133 /* in this case we do know which parent should
1134 get this pointer, as there is really only
1136 return talloc_unlink(null_context, ptr);
1139 talloc_log("ERROR: talloc_free with references at %s\n",
1142 for (h=tc->refs; h; h=h->next) {
1143 talloc_log("\treference at %s\n",
1149 return _talloc_free_internal(ptr, location);
1155 A talloc version of realloc. The context argument is only used if
1158 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1160 struct talloc_chunk *tc;
1162 bool malloced = false;
1164 /* size zero is equivalent to free() */
1165 if (unlikely(size == 0)) {
1166 talloc_unlink(context, ptr);
1170 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1174 /* realloc(NULL) is equivalent to malloc() */
1176 return _talloc_named_const(context, size, name);
1179 tc = talloc_chunk_from_ptr(ptr);
1181 /* don't allow realloc on referenced pointers */
1182 if (unlikely(tc->refs)) {
1186 /* don't let anybody try to realloc a talloc_pool */
1187 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1191 /* don't shrink if we have less than 1k to gain */
1192 if ((size < tc->size) && ((tc->size - size) < 1024)) {
1197 /* by resetting magic we catch users of the old memory */
1198 tc->flags |= TALLOC_FLAG_FREE;
1201 new_ptr = malloc(size + TC_HDR_SIZE);
1203 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1207 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1209 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1210 *talloc_pool_objectcount((struct talloc_chunk *)
1213 if (new_ptr == NULL) {
1214 new_ptr = malloc(TC_HDR_SIZE+size);
1219 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1223 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1226 if (unlikely(!new_ptr)) {
1227 tc->flags &= ~TALLOC_FLAG_FREE;
1231 tc = (struct talloc_chunk *)new_ptr;
1232 tc->flags &= ~TALLOC_FLAG_FREE;
1234 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1237 tc->parent->child = tc;
1240 tc->child->parent = tc;
1244 tc->prev->next = tc;
1247 tc->next->prev = tc;
1251 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1253 return TC_PTR_FROM_CHUNK(tc);
1257 a wrapper around talloc_steal() for situations where you are moving a pointer
1258 between two structures, and want the old pointer to be set to NULL
1260 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
1262 const void **pptr = discard_const_p(const void *,_pptr);
1263 void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1269 return the total size of a talloc pool (subtree)
1271 _PUBLIC_ size_t talloc_total_size(const void *ptr)
1274 struct talloc_chunk *c, *tc;
1283 tc = talloc_chunk_from_ptr(ptr);
1285 if (tc->flags & TALLOC_FLAG_LOOP) {
1289 tc->flags |= TALLOC_FLAG_LOOP;
1291 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1294 for (c=tc->child;c;c=c->next) {
1295 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1298 tc->flags &= ~TALLOC_FLAG_LOOP;
1304 return the total number of blocks in a talloc pool (subtree)
1306 _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
1309 struct talloc_chunk *c, *tc;
1318 tc = talloc_chunk_from_ptr(ptr);
1320 if (tc->flags & TALLOC_FLAG_LOOP) {
1324 tc->flags |= TALLOC_FLAG_LOOP;
1327 for (c=tc->child;c;c=c->next) {
1328 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1331 tc->flags &= ~TALLOC_FLAG_LOOP;
1337 return the number of external references to a pointer
1339 _PUBLIC_ size_t talloc_reference_count(const void *ptr)
1341 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1342 struct talloc_reference_handle *h;
1345 for (h=tc->refs;h;h=h->next) {
1352 report on memory usage by all children of a pointer, giving a full tree view
1354 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1355 void (*callback)(const void *ptr,
1356 int depth, int max_depth,
1358 void *private_data),
1361 struct talloc_chunk *c, *tc;
1366 if (ptr == NULL) return;
1368 tc = talloc_chunk_from_ptr(ptr);
1370 if (tc->flags & TALLOC_FLAG_LOOP) {
1374 callback(ptr, depth, max_depth, 0, private_data);
1376 if (max_depth >= 0 && depth >= max_depth) {
1380 tc->flags |= TALLOC_FLAG_LOOP;
1381 for (c=tc->child;c;c=c->next) {
1382 if (c->name == TALLOC_MAGIC_REFERENCE) {
1383 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1384 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1386 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1389 tc->flags &= ~TALLOC_FLAG_LOOP;
1392 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1394 const char *name = talloc_get_name(ptr);
1395 FILE *f = (FILE *)_f;
1398 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1403 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1404 (max_depth < 0 ? "full " :""), name,
1405 (unsigned long)talloc_total_size(ptr),
1406 (unsigned long)talloc_total_blocks(ptr));
1410 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1413 (unsigned long)talloc_total_size(ptr),
1414 (unsigned long)talloc_total_blocks(ptr),
1415 (int)talloc_reference_count(ptr), ptr);
1418 fprintf(f, "content: ");
1419 if (talloc_total_size(ptr)) {
1420 int tot = talloc_total_size(ptr);
1423 for (i = 0; i < tot; i++) {
1424 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1425 fprintf(f, "%c", ((char *)ptr)[i]);
1427 fprintf(f, "~%02x", ((char *)ptr)[i]);
1436 report on memory usage by all children of a pointer, giving a full tree view
1438 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1441 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1447 report on memory usage by all children of a pointer, giving a full tree view
1449 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
1451 talloc_report_depth_file(ptr, 0, -1, f);
1455 report on memory usage by all children of a pointer
1457 _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
1459 talloc_report_depth_file(ptr, 0, 1, f);
1463 report on any memory hanging off the null context
1465 static void talloc_report_null(void)
1467 if (talloc_total_size(null_context) != 0) {
1468 talloc_report(null_context, stderr);
1473 report on any memory hanging off the null context
1475 static void talloc_report_null_full(void)
1477 if (talloc_total_size(null_context) != 0) {
1478 talloc_report_full(null_context, stderr);
1483 enable tracking of the NULL context
1485 _PUBLIC_ void talloc_enable_null_tracking(void)
1487 if (null_context == NULL) {
1488 null_context = _talloc_named_const(NULL, 0, "null_context");
1489 if (autofree_context != NULL) {
1490 talloc_reparent(NULL, null_context, autofree_context);
1496 enable tracking of the NULL context, not moving the autofree context
1497 into the NULL context. This is needed for the talloc testsuite
1499 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
1501 if (null_context == NULL) {
1502 null_context = _talloc_named_const(NULL, 0, "null_context");
1507 disable tracking of the NULL context
1509 _PUBLIC_ void talloc_disable_null_tracking(void)
1511 if (null_context != NULL) {
1512 /* we have to move any children onto the real NULL
1514 struct talloc_chunk *tc, *tc2;
1515 tc = talloc_chunk_from_ptr(null_context);
1516 for (tc2 = tc->child; tc2; tc2=tc2->next) {
1517 if (tc2->parent == tc) tc2->parent = NULL;
1518 if (tc2->prev == tc) tc2->prev = NULL;
1520 for (tc2 = tc->next; tc2; tc2=tc2->next) {
1521 if (tc2->parent == tc) tc2->parent = NULL;
1522 if (tc2->prev == tc) tc2->prev = NULL;
1527 talloc_free(null_context);
1528 null_context = NULL;
1532 enable leak reporting on exit
1534 _PUBLIC_ void talloc_enable_leak_report(void)
1536 talloc_enable_null_tracking();
1537 atexit(talloc_report_null);
1541 enable full leak reporting on exit
1543 _PUBLIC_ void talloc_enable_leak_report_full(void)
1545 talloc_enable_null_tracking();
1546 atexit(talloc_report_null_full);
1550 talloc and zero memory.
1552 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
1554 void *p = _talloc_named_const(ctx, size, name);
1557 memset(p, '\0', size);
1564 memdup with a talloc.
1566 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1568 void *newp = _talloc_named_const(t, size, name);
1571 memcpy(newp, p, size);
1577 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1581 ret = (char *)__talloc(t, len + 1);
1582 if (unlikely(!ret)) return NULL;
1584 memcpy(ret, p, len);
1587 _talloc_set_name_const(ret, ret);
1592 strdup with a talloc
1594 _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
1596 if (unlikely(!p)) return NULL;
1597 return __talloc_strlendup(t, p, strlen(p));
1601 strndup with a talloc
1603 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
1605 if (unlikely(!p)) return NULL;
1606 return __talloc_strlendup(t, p, strnlen(p, n));
1609 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1610 const char *a, size_t alen)
1614 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1615 if (unlikely(!ret)) return NULL;
1617 /* append the string and the trailing \0 */
1618 memcpy(&ret[slen], a, alen);
1621 _talloc_set_name_const(ret, ret);
1626 * Appends at the end of the string.
1628 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
1631 return talloc_strdup(NULL, a);
1638 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1642 * Appends at the end of the talloc'ed buffer,
1643 * not the end of the string.
1645 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
1650 return talloc_strdup(NULL, a);
1657 slen = talloc_get_size(s);
1658 if (likely(slen > 0)) {
1662 return __talloc_strlendup_append(s, slen, a, strlen(a));
1666 * Appends at the end of the string.
1668 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
1671 return talloc_strdup(NULL, a);
1678 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1682 * Appends at the end of the talloc'ed buffer,
1683 * not the end of the string.
1685 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1690 return talloc_strdup(NULL, a);
1697 slen = talloc_get_size(s);
1698 if (likely(slen > 0)) {
1702 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1705 #ifndef HAVE_VA_COPY
1706 #ifdef HAVE___VA_COPY
1707 #define va_copy(dest, src) __va_copy(dest, src)
1709 #define va_copy(dest, src) (dest) = (src)
1713 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1720 /* this call looks strange, but it makes it work on older solaris boxes */
1722 len = vsnprintf(&c, 1, fmt, ap2);
1724 if (unlikely(len < 0)) {
1728 ret = (char *)__talloc(t, len+1);
1729 if (unlikely(!ret)) return NULL;
1732 vsnprintf(ret, len+1, fmt, ap2);
1735 _talloc_set_name_const(ret, ret);
1741 Perform string formatting, and return a pointer to newly allocated
1742 memory holding the result, inside a memory pool.
1744 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
1750 ret = talloc_vasprintf(t, fmt, ap);
1755 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1756 const char *fmt, va_list ap)
1757 PRINTF_ATTRIBUTE(3,0);
1759 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1760 const char *fmt, va_list ap)
1767 alen = vsnprintf(&c, 1, fmt, ap2);
1771 /* Either the vsnprintf failed or the format resulted in
1772 * no characters being formatted. In the former case, we
1773 * ought to return NULL, in the latter we ought to return
1774 * the original string. Most current callers of this
1775 * function expect it to never return NULL.
1780 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1781 if (!s) return NULL;
1784 vsnprintf(s + slen, alen + 1, fmt, ap2);
1787 _talloc_set_name_const(s, s);
1792 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1793 * and return @p s, which may have moved. Good for gradually
1794 * accumulating output into a string buffer. Appends at the end
1797 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1800 return talloc_vasprintf(NULL, fmt, ap);
1803 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1807 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1808 * and return @p s, which may have moved. Always appends at the
1809 * end of the talloc'ed buffer, not the end of the string.
1811 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1816 return talloc_vasprintf(NULL, fmt, ap);
1819 slen = talloc_get_size(s);
1820 if (likely(slen > 0)) {
1824 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1828 Realloc @p s to append the formatted result of @p fmt and return @p
1829 s, which may have moved. Good for gradually accumulating output
1830 into a string buffer.
1832 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
1837 s = talloc_vasprintf_append(s, fmt, ap);
1843 Realloc @p s to append the formatted result of @p fmt and return @p
1844 s, which may have moved. Good for gradually accumulating output
1847 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1852 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1858 alloc an array, checking for integer overflow in the array size
1860 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1862 if (count >= MAX_TALLOC_SIZE/el_size) {
1865 return _talloc_named_const(ctx, el_size * count, name);
1869 alloc an zero array, checking for integer overflow in the array size
1871 _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1873 if (count >= MAX_TALLOC_SIZE/el_size) {
1876 return _talloc_zero(ctx, el_size * count, name);
1880 realloc an array, checking for integer overflow in the array size
1882 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1884 if (count >= MAX_TALLOC_SIZE/el_size) {
1887 return _talloc_realloc(ctx, ptr, el_size * count, name);
1891 a function version of talloc_realloc(), so it can be passed as a function pointer
1892 to libraries that want a realloc function (a realloc function encapsulates
1893 all the basic capabilities of an allocation library, which is why this is useful)
1895 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1897 return _talloc_realloc(context, ptr, size, NULL);
1901 static int talloc_autofree_destructor(void *ptr)
1903 autofree_context = NULL;
1907 static void talloc_autofree(void)
1909 talloc_free(autofree_context);
1913 return a context which will be auto-freed on exit
1914 this is useful for reducing the noise in leak reports
1916 _PUBLIC_ void *talloc_autofree_context(void)
1918 if (autofree_context == NULL) {
1919 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1920 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1921 atexit(talloc_autofree);
1923 return autofree_context;
1926 _PUBLIC_ size_t talloc_get_size(const void *context)
1928 struct talloc_chunk *tc;
1930 if (context == NULL) {
1931 context = null_context;
1933 if (context == NULL) {
1937 tc = talloc_chunk_from_ptr(context);
1943 find a parent of this context that has the given name, if any
1945 _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
1947 struct talloc_chunk *tc;
1949 if (context == NULL) {
1953 tc = talloc_chunk_from_ptr(context);
1955 if (tc->name && strcmp(tc->name, name) == 0) {
1956 return TC_PTR_FROM_CHUNK(tc);
1958 while (tc && tc->prev) tc = tc->prev;
1967 show the parentage of a context
1969 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
1971 struct talloc_chunk *tc;
1973 if (context == NULL) {
1974 fprintf(file, "talloc no parents for NULL\n");
1978 tc = talloc_chunk_from_ptr(context);
1979 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1981 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1982 while (tc && tc->prev) tc = tc->prev;
1991 return 1 if ptr is a parent of context
1993 static int _talloc_is_parent(const void *context, const void *ptr, int depth)
1995 struct talloc_chunk *tc;
1997 if (context == NULL) {
2001 tc = talloc_chunk_from_ptr(context);
2002 while (tc && depth > 0) {
2003 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2004 while (tc && tc->prev) tc = tc->prev;
2014 return 1 if ptr is a parent of context
2016 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2018 return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);