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/
35 #if (SAMBA_VERSION_MAJOR<4)
37 /* This is to circumvent SAMBA3's paranoid malloc checker. Here in this file
38 * we trust ourselves... */
45 #define _TALLOC_SAMBA3
46 #endif /* (SAMBA_VERSION_MAJOR<4) */
47 #endif /* _SAMBA_BUILD_ */
49 #ifndef _TALLOC_SAMBA3
52 #endif /* not _TALLOC_SAMBA3 */
54 /* use this to force every realloc to change the pointer, to stress test
55 code that might not cope */
56 #define ALWAYS_REALLOC 0
59 #define MAX_TALLOC_SIZE 0x10000000
60 #define TALLOC_MAGIC 0xe814ec70
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 struct talloc_reference_handle
{
108 struct talloc_reference_handle
*next
, *prev
;
112 typedef int (*talloc_destructor_t
)(void *);
114 struct talloc_chunk
{
115 struct talloc_chunk
*next
, *prev
;
116 struct talloc_chunk
*parent
, *child
;
117 struct talloc_reference_handle
*refs
;
118 talloc_destructor_t destructor
;
124 * "pool" has dual use:
126 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
127 * marks the end of the currently allocated area.
129 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
130 * is a pointer to the struct talloc_chunk of the pool that it was
131 * allocated from. This way children can quickly find the pool to chew
137 /* 16 byte alignment seems to keep everyone happy */
138 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
139 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
141 static void (*talloc_abort_fn
)(const char *reason
);
143 void talloc_set_abort_fn(void (*abort_fn
)(const char *reason
))
145 talloc_abort_fn
= abort_fn
;
148 static void talloc_abort(const char *reason
)
150 if (!talloc_abort_fn
) {
151 TALLOC_ABORT(reason
);
154 talloc_abort_fn(reason
);
157 static void talloc_abort_double_free(void)
159 talloc_abort("Bad talloc magic value - double free");
162 static void talloc_abort_unknown_value(void)
164 talloc_abort("Bad talloc magic value - unknown value");
167 /* panic if we get a bad magic value */
168 static inline struct talloc_chunk
*talloc_chunk_from_ptr(const void *ptr
)
170 const char *pp
= (const char *)ptr
;
171 struct talloc_chunk
*tc
= discard_const_p(struct talloc_chunk
, pp
- TC_HDR_SIZE
);
172 if (unlikely((tc
->flags
& (TALLOC_FLAG_FREE
| ~0xF)) != TALLOC_MAGIC
)) {
173 if (tc
->flags
& TALLOC_FLAG_FREE
) {
174 talloc_abort_double_free();
176 talloc_abort_unknown_value();
182 /* hook into the front of the list */
183 #define _TLIST_ADD(list, p) \
187 (p)->next = (p)->prev = NULL; \
189 (list)->prev = (p); \
190 (p)->next = (list); \
196 /* remove an element from a list - element doesn't have to be in list. */
197 #define _TLIST_REMOVE(list, p) \
199 if ((p) == (list)) { \
200 (list) = (p)->next; \
201 if (list) (list)->prev = NULL; \
203 if ((p)->prev) (p)->prev->next = (p)->next; \
204 if ((p)->next) (p)->next->prev = (p)->prev; \
206 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
211 return the parent chunk of a pointer
213 static inline struct talloc_chunk
*talloc_parent_chunk(const void *ptr
)
215 struct talloc_chunk
*tc
;
217 if (unlikely(ptr
== NULL
)) {
221 tc
= talloc_chunk_from_ptr(ptr
);
222 while (tc
->prev
) tc
=tc
->prev
;
227 void *talloc_parent(const void *ptr
)
229 struct talloc_chunk
*tc
= talloc_parent_chunk(ptr
);
230 return tc
? TC_PTR_FROM_CHUNK(tc
) : NULL
;
236 const char *talloc_parent_name(const void *ptr
)
238 struct talloc_chunk
*tc
= talloc_parent_chunk(ptr
);
239 return tc
? tc
->name
: NULL
;
243 A pool carries an in-pool object count count in the first 16 bytes.
244 bytes. This is done to support talloc_steal() to a parent outside of the
245 pool. The count includes the pool itself, so a talloc_free() on a pool will
246 only destroy the pool if the count has dropped to zero. A talloc_free() of a
247 pool member will reduce the count, and eventually also call free(3) on the
250 The object count is not put into "struct talloc_chunk" because it is only
251 relevant for talloc pools and the alignment to 16 bytes would increase the
252 memory footprint of each talloc chunk by those 16 bytes.
255 #define TALLOC_POOL_HDR_SIZE 16
257 static unsigned int *talloc_pool_objectcount(struct talloc_chunk
*tc
)
259 return (unsigned int *)((char *)tc
+ sizeof(struct talloc_chunk
));
266 static struct talloc_chunk
*talloc_alloc_pool(struct talloc_chunk
*parent
,
269 struct talloc_chunk
*pool_ctx
= NULL
;
271 struct talloc_chunk
*result
;
274 if (parent
== NULL
) {
278 if (parent
->flags
& TALLOC_FLAG_POOL
) {
281 else if (parent
->flags
& TALLOC_FLAG_POOLMEM
) {
282 pool_ctx
= (struct talloc_chunk
*)parent
->pool
;
285 if (pool_ctx
== NULL
) {
289 space_left
= ((char *)pool_ctx
+ TC_HDR_SIZE
+ pool_ctx
->size
)
290 - ((char *)pool_ctx
->pool
);
293 * Align size to 16 bytes
295 chunk_size
= ((size
+ 15) & ~15);
297 if (space_left
< chunk_size
) {
301 result
= (struct talloc_chunk
*)pool_ctx
->pool
;
303 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
304 VALGRIND_MAKE_MEM_UNDEFINED(result
, size
);
307 pool_ctx
->pool
= (void *)((char *)result
+ chunk_size
);
309 result
->flags
= TALLOC_MAGIC
| TALLOC_FLAG_POOLMEM
;
310 result
->pool
= pool_ctx
;
312 *talloc_pool_objectcount(pool_ctx
) += 1;
318 Allocate a bit of memory as a child of an existing pointer
320 static inline void *__talloc(const void *context
, size_t size
)
322 struct talloc_chunk
*tc
= NULL
;
324 if (unlikely(context
== NULL
)) {
325 context
= null_context
;
328 if (unlikely(size
>= MAX_TALLOC_SIZE
)) {
332 if (context
!= NULL
) {
333 tc
= talloc_alloc_pool(talloc_chunk_from_ptr(context
),
338 tc
= (struct talloc_chunk
*)malloc(TC_HDR_SIZE
+size
);
339 if (unlikely(tc
== NULL
)) return NULL
;
340 tc
->flags
= TALLOC_MAGIC
;
345 tc
->destructor
= NULL
;
350 if (likely(context
)) {
351 struct talloc_chunk
*parent
= talloc_chunk_from_ptr(context
);
354 parent
->child
->parent
= NULL
;
355 tc
->next
= parent
->child
;
364 tc
->next
= tc
->prev
= tc
->parent
= NULL
;
367 return TC_PTR_FROM_CHUNK(tc
);
371 * Create a talloc pool
374 void *talloc_pool(const void *context
, size_t size
)
376 void *result
= __talloc(context
, size
+ TALLOC_POOL_HDR_SIZE
);
377 struct talloc_chunk
*tc
;
379 if (unlikely(result
== NULL
)) {
383 tc
= talloc_chunk_from_ptr(result
);
385 tc
->flags
|= TALLOC_FLAG_POOL
;
386 tc
->pool
= (char *)result
+ TALLOC_POOL_HDR_SIZE
;
388 *talloc_pool_objectcount(tc
) = 1;
390 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
391 VALGRIND_MAKE_MEM_NOACCESS(tc
->pool
, size
);
398 setup a destructor to be called on free of a pointer
399 the destructor should return 0 on success, or -1 on failure.
400 if the destructor fails then the free is failed, and the memory can
401 be continued to be used
403 void _talloc_set_destructor(const void *ptr
, int (*destructor
)(void *))
405 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
406 tc
->destructor
= destructor
;
410 increase the reference count on a piece of memory.
412 int talloc_increase_ref_count(const void *ptr
)
414 if (unlikely(!talloc_reference(null_context
, ptr
))) {
421 helper for talloc_reference()
423 this is referenced by a function pointer and should not be inline
425 static int talloc_reference_destructor(struct talloc_reference_handle
*handle
)
427 struct talloc_chunk
*ptr_tc
= talloc_chunk_from_ptr(handle
->ptr
);
428 _TLIST_REMOVE(ptr_tc
->refs
, handle
);
433 more efficient way to add a name to a pointer - the name must point to a
436 static inline void _talloc_set_name_const(const void *ptr
, const char *name
)
438 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
443 internal talloc_named_const()
445 static inline void *_talloc_named_const(const void *context
, size_t size
, const char *name
)
449 ptr
= __talloc(context
, size
);
450 if (unlikely(ptr
== NULL
)) {
454 _talloc_set_name_const(ptr
, name
);
460 make a secondary reference to a pointer, hanging off the given context.
461 the pointer remains valid until both the original caller and this given
464 the major use for this is when two different structures need to reference the
465 same underlying data, and you want to be able to free the two instances separately,
468 void *_talloc_reference(const void *context
, const void *ptr
)
470 struct talloc_chunk
*tc
;
471 struct talloc_reference_handle
*handle
;
472 if (unlikely(ptr
== NULL
)) return NULL
;
474 tc
= talloc_chunk_from_ptr(ptr
);
475 handle
= (struct talloc_reference_handle
*)_talloc_named_const(context
,
476 sizeof(struct talloc_reference_handle
),
477 TALLOC_MAGIC_REFERENCE
);
478 if (unlikely(handle
== NULL
)) return NULL
;
480 /* note that we hang the destructor off the handle, not the
481 main context as that allows the caller to still setup their
482 own destructor on the context if they want to */
483 talloc_set_destructor(handle
, talloc_reference_destructor
);
484 handle
->ptr
= discard_const_p(void, ptr
);
485 _TLIST_ADD(tc
->refs
, handle
);
491 internal talloc_free call
493 static inline int _talloc_free(void *ptr
)
495 struct talloc_chunk
*tc
;
497 if (unlikely(ptr
== NULL
)) {
501 tc
= talloc_chunk_from_ptr(ptr
);
503 if (unlikely(tc
->refs
)) {
505 /* check this is a reference from a child or grantchild
506 * back to it's parent or grantparent
508 * in that case we need to remove the reference and
509 * call another instance of talloc_free() on the current
512 is_child
= talloc_is_parent(tc
->refs
, ptr
);
513 _talloc_free(tc
->refs
);
515 return _talloc_free(ptr
);
520 if (unlikely(tc
->flags
& TALLOC_FLAG_LOOP
)) {
521 /* we have a free loop - stop looping */
525 if (unlikely(tc
->destructor
)) {
526 talloc_destructor_t d
= tc
->destructor
;
527 if (d
== (talloc_destructor_t
)-1) {
530 tc
->destructor
= (talloc_destructor_t
)-1;
535 tc
->destructor
= NULL
;
539 _TLIST_REMOVE(tc
->parent
->child
, tc
);
540 if (tc
->parent
->child
) {
541 tc
->parent
->child
->parent
= tc
->parent
;
544 if (tc
->prev
) tc
->prev
->next
= tc
->next
;
545 if (tc
->next
) tc
->next
->prev
= tc
->prev
;
548 tc
->flags
|= TALLOC_FLAG_LOOP
;
551 /* we need to work out who will own an abandoned child
552 if it cannot be freed. In priority order, the first
553 choice is owner of any remaining reference to this
554 pointer, the second choice is our parent, and the
555 final choice is the null context. */
556 void *child
= TC_PTR_FROM_CHUNK(tc
->child
);
557 const void *new_parent
= null_context
;
558 if (unlikely(tc
->child
->refs
)) {
559 struct talloc_chunk
*p
= talloc_parent_chunk(tc
->child
->refs
);
560 if (p
) new_parent
= TC_PTR_FROM_CHUNK(p
);
562 if (unlikely(_talloc_free(child
) == -1)) {
563 if (new_parent
== null_context
) {
564 struct talloc_chunk
*p
= talloc_parent_chunk(ptr
);
565 if (p
) new_parent
= TC_PTR_FROM_CHUNK(p
);
567 talloc_steal(new_parent
, child
);
571 tc
->flags
|= TALLOC_FLAG_FREE
;
573 if (tc
->flags
& (TALLOC_FLAG_POOL
|TALLOC_FLAG_POOLMEM
)) {
574 struct talloc_chunk
*pool
;
575 unsigned int *pool_object_count
;
577 pool
= (tc
->flags
& TALLOC_FLAG_POOL
)
578 ? tc
: (struct talloc_chunk
*)tc
->pool
;
580 pool_object_count
= talloc_pool_objectcount(pool
);
582 if (*pool_object_count
== 0) {
583 talloc_abort("Pool object count zero!");
586 *pool_object_count
-= 1;
588 if (*pool_object_count
== 0) {
599 move a lump of memory from one talloc context to another return the
600 ptr on success, or NULL if it could not be transferred.
601 passing NULL as ptr will always return NULL with no side effects.
603 void *_talloc_steal(const void *new_ctx
, const void *ptr
)
605 struct talloc_chunk
*tc
, *new_tc
;
607 if (unlikely(!ptr
)) {
611 if (unlikely(new_ctx
== NULL
)) {
612 new_ctx
= null_context
;
615 tc
= talloc_chunk_from_ptr(ptr
);
617 if (unlikely(new_ctx
== NULL
)) {
619 _TLIST_REMOVE(tc
->parent
->child
, tc
);
620 if (tc
->parent
->child
) {
621 tc
->parent
->child
->parent
= tc
->parent
;
624 if (tc
->prev
) tc
->prev
->next
= tc
->next
;
625 if (tc
->next
) tc
->next
->prev
= tc
->prev
;
628 tc
->parent
= tc
->next
= tc
->prev
= NULL
;
629 return discard_const_p(void, ptr
);
632 new_tc
= talloc_chunk_from_ptr(new_ctx
);
634 if (unlikely(tc
== new_tc
|| tc
->parent
== new_tc
)) {
635 return discard_const_p(void, ptr
);
639 _TLIST_REMOVE(tc
->parent
->child
, tc
);
640 if (tc
->parent
->child
) {
641 tc
->parent
->child
->parent
= tc
->parent
;
644 if (tc
->prev
) tc
->prev
->next
= tc
->next
;
645 if (tc
->next
) tc
->next
->prev
= tc
->prev
;
649 if (new_tc
->child
) new_tc
->child
->parent
= NULL
;
650 _TLIST_ADD(new_tc
->child
, tc
);
652 return discard_const_p(void, ptr
);
658 remove a secondary reference to a pointer. This undo's what
659 talloc_reference() has done. The context and pointer arguments
660 must match those given to a talloc_reference()
662 static inline int talloc_unreference(const void *context
, const void *ptr
)
664 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
665 struct talloc_reference_handle
*h
;
667 if (unlikely(context
== NULL
)) {
668 context
= null_context
;
671 for (h
=tc
->refs
;h
;h
=h
->next
) {
672 struct talloc_chunk
*p
= talloc_parent_chunk(h
);
674 if (context
== NULL
) break;
675 } else if (TC_PTR_FROM_CHUNK(p
) == context
) {
683 return _talloc_free(h
);
687 remove a specific parent context from a pointer. This is a more
688 controlled varient of talloc_free()
690 int talloc_unlink(const void *context
, void *ptr
)
692 struct talloc_chunk
*tc_p
, *new_p
;
699 if (context
== NULL
) {
700 context
= null_context
;
703 if (talloc_unreference(context
, ptr
) == 0) {
707 if (context
== NULL
) {
708 if (talloc_parent_chunk(ptr
) != NULL
) {
712 if (talloc_chunk_from_ptr(context
) != talloc_parent_chunk(ptr
)) {
717 tc_p
= talloc_chunk_from_ptr(ptr
);
719 if (tc_p
->refs
== NULL
) {
720 return _talloc_free(ptr
);
723 new_p
= talloc_parent_chunk(tc_p
->refs
);
725 new_parent
= TC_PTR_FROM_CHUNK(new_p
);
730 if (talloc_unreference(new_parent
, ptr
) != 0) {
734 talloc_steal(new_parent
, ptr
);
740 add a name to an existing pointer - va_list version
742 static inline const char *talloc_set_name_v(const void *ptr
, const char *fmt
, va_list ap
) PRINTF_ATTRIBUTE(2,0);
744 static inline const char *talloc_set_name_v(const void *ptr
, const char *fmt
, va_list ap
)
746 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
747 tc
->name
= talloc_vasprintf(ptr
, fmt
, ap
);
748 if (likely(tc
->name
)) {
749 _talloc_set_name_const(tc
->name
, ".name");
755 add a name to an existing pointer
757 const char *talloc_set_name(const void *ptr
, const char *fmt
, ...)
762 name
= talloc_set_name_v(ptr
, fmt
, ap
);
769 create a named talloc pointer. Any talloc pointer can be named, and
770 talloc_named() operates just like talloc() except that it allows you
773 void *talloc_named(const void *context
, size_t size
, const char *fmt
, ...)
779 ptr
= __talloc(context
, size
);
780 if (unlikely(ptr
== NULL
)) return NULL
;
783 name
= talloc_set_name_v(ptr
, fmt
, ap
);
786 if (unlikely(name
== NULL
)) {
795 return the name of a talloc ptr, or "UNNAMED"
797 const char *talloc_get_name(const void *ptr
)
799 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
800 if (unlikely(tc
->name
== TALLOC_MAGIC_REFERENCE
)) {
803 if (likely(tc
->name
)) {
811 check if a pointer has the given name. If it does, return the pointer,
812 otherwise return NULL
814 void *talloc_check_name(const void *ptr
, const char *name
)
817 if (unlikely(ptr
== NULL
)) return NULL
;
818 pname
= talloc_get_name(ptr
);
819 if (likely(pname
== name
|| strcmp(pname
, name
) == 0)) {
820 return discard_const_p(void, ptr
);
825 static void talloc_abort_type_missmatch(const char *location
,
827 const char *expected
)
831 reason
= talloc_asprintf(NULL
,
832 "%s: Type mismatch: name[%s] expected[%s]",
837 reason
= "Type mismatch";
840 talloc_abort(reason
);
843 void *_talloc_get_type_abort(const void *ptr
, const char *name
, const char *location
)
847 if (unlikely(ptr
== NULL
)) {
848 talloc_abort_type_missmatch(location
, NULL
, name
);
852 pname
= talloc_get_name(ptr
);
853 if (likely(pname
== name
|| strcmp(pname
, name
) == 0)) {
854 return discard_const_p(void, ptr
);
857 talloc_abort_type_missmatch(location
, pname
, name
);
862 this is for compatibility with older versions of talloc
864 void *talloc_init(const char *fmt
, ...)
871 * samba3 expects talloc_report_depth_cb(NULL, ...)
872 * reports all talloc'ed memory, so we need to enable
875 talloc_enable_null_tracking();
877 ptr
= __talloc(NULL
, 0);
878 if (unlikely(ptr
== NULL
)) return NULL
;
881 name
= talloc_set_name_v(ptr
, fmt
, ap
);
884 if (unlikely(name
== NULL
)) {
893 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
894 should probably not be used in new code. It's in here to keep the talloc
895 code consistent across Samba 3 and 4.
897 void talloc_free_children(void *ptr
)
899 struct talloc_chunk
*tc
;
901 if (unlikely(ptr
== NULL
)) {
905 tc
= talloc_chunk_from_ptr(ptr
);
908 /* we need to work out who will own an abandoned child
909 if it cannot be freed. In priority order, the first
910 choice is owner of any remaining reference to this
911 pointer, the second choice is our parent, and the
912 final choice is the null context. */
913 void *child
= TC_PTR_FROM_CHUNK(tc
->child
);
914 const void *new_parent
= null_context
;
915 if (unlikely(tc
->child
->refs
)) {
916 struct talloc_chunk
*p
= talloc_parent_chunk(tc
->child
->refs
);
917 if (p
) new_parent
= TC_PTR_FROM_CHUNK(p
);
919 if (unlikely(_talloc_free(child
) == -1)) {
920 if (new_parent
== null_context
) {
921 struct talloc_chunk
*p
= talloc_parent_chunk(ptr
);
922 if (p
) new_parent
= TC_PTR_FROM_CHUNK(p
);
924 talloc_steal(new_parent
, child
);
928 if ((tc
->flags
& TALLOC_FLAG_POOL
)
929 && (*talloc_pool_objectcount(tc
) == 1)) {
930 tc
->pool
= ((char *)tc
+ TC_HDR_SIZE
+ TALLOC_POOL_HDR_SIZE
);
931 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
932 VALGRIND_MAKE_MEM_NOACCESS(
933 tc
->pool
, tc
->size
- TALLOC_POOL_HDR_SIZE
);
939 Allocate a bit of memory as a child of an existing pointer
941 void *_talloc(const void *context
, size_t size
)
943 return __talloc(context
, size
);
947 externally callable talloc_set_name_const()
949 void talloc_set_name_const(const void *ptr
, const char *name
)
951 _talloc_set_name_const(ptr
, name
);
955 create a named talloc pointer. Any talloc pointer can be named, and
956 talloc_named() operates just like talloc() except that it allows you
959 void *talloc_named_const(const void *context
, size_t size
, const char *name
)
961 return _talloc_named_const(context
, size
, name
);
965 free a talloc pointer. This also frees all child pointers of this
968 return 0 if the memory is actually freed, otherwise -1. The memory
969 will not be freed if the ref_count is > 1 or the destructor (if
970 any) returns non-zero
972 int talloc_free(void *ptr
)
974 return _talloc_free(ptr
);
980 A talloc version of realloc. The context argument is only used if
983 void *_talloc_realloc(const void *context
, void *ptr
, size_t size
, const char *name
)
985 struct talloc_chunk
*tc
;
987 bool malloced
= false;
989 /* size zero is equivalent to free() */
990 if (unlikely(size
== 0)) {
995 if (unlikely(size
>= MAX_TALLOC_SIZE
)) {
999 /* realloc(NULL) is equivalent to malloc() */
1001 return _talloc_named_const(context
, size
, name
);
1004 tc
= talloc_chunk_from_ptr(ptr
);
1006 /* don't allow realloc on referenced pointers */
1007 if (unlikely(tc
->refs
)) {
1011 /* don't shrink if we have less than 1k to gain */
1012 if ((size
< tc
->size
) && ((tc
->size
- size
) < 1024)) {
1017 /* by resetting magic we catch users of the old memory */
1018 tc
->flags
|= TALLOC_FLAG_FREE
;
1021 new_ptr
= malloc(size
+ TC_HDR_SIZE
);
1023 memcpy(new_ptr
, tc
, tc
->size
+ TC_HDR_SIZE
);
1027 if (tc
->flags
& TALLOC_FLAG_POOLMEM
) {
1029 new_ptr
= talloc_alloc_pool(tc
, size
+ TC_HDR_SIZE
);
1030 *talloc_pool_objectcount((struct talloc_chunk
*)
1033 if (new_ptr
== NULL
) {
1034 new_ptr
= malloc(TC_HDR_SIZE
+size
);
1039 memcpy(new_ptr
, tc
, MIN(tc
->size
,size
) + TC_HDR_SIZE
);
1043 new_ptr
= realloc(tc
, size
+ TC_HDR_SIZE
);
1046 if (unlikely(!new_ptr
)) {
1047 tc
->flags
&= ~TALLOC_FLAG_FREE
;
1051 tc
= (struct talloc_chunk
*)new_ptr
;
1052 tc
->flags
&= ~TALLOC_FLAG_FREE
;
1054 tc
->flags
&= ~TALLOC_FLAG_POOLMEM
;
1057 tc
->parent
->child
= tc
;
1060 tc
->child
->parent
= tc
;
1064 tc
->prev
->next
= tc
;
1067 tc
->next
->prev
= tc
;
1071 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc
), name
);
1073 return TC_PTR_FROM_CHUNK(tc
);
1077 a wrapper around talloc_steal() for situations where you are moving a pointer
1078 between two structures, and want the old pointer to be set to NULL
1080 void *_talloc_move(const void *new_ctx
, const void *_pptr
)
1082 const void **pptr
= discard_const_p(const void *,_pptr
);
1083 void *ret
= _talloc_steal(new_ctx
, *pptr
);
1089 return the total size of a talloc pool (subtree)
1091 size_t talloc_total_size(const void *ptr
)
1094 struct talloc_chunk
*c
, *tc
;
1103 tc
= talloc_chunk_from_ptr(ptr
);
1105 if (tc
->flags
& TALLOC_FLAG_LOOP
) {
1109 tc
->flags
|= TALLOC_FLAG_LOOP
;
1112 for (c
=tc
->child
;c
;c
=c
->next
) {
1113 total
+= talloc_total_size(TC_PTR_FROM_CHUNK(c
));
1116 tc
->flags
&= ~TALLOC_FLAG_LOOP
;
1122 return the total number of blocks in a talloc pool (subtree)
1124 size_t talloc_total_blocks(const void *ptr
)
1127 struct talloc_chunk
*c
, *tc
= talloc_chunk_from_ptr(ptr
);
1129 if (tc
->flags
& TALLOC_FLAG_LOOP
) {
1133 tc
->flags
|= TALLOC_FLAG_LOOP
;
1136 for (c
=tc
->child
;c
;c
=c
->next
) {
1137 total
+= talloc_total_blocks(TC_PTR_FROM_CHUNK(c
));
1140 tc
->flags
&= ~TALLOC_FLAG_LOOP
;
1146 return the number of external references to a pointer
1148 size_t talloc_reference_count(const void *ptr
)
1150 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
1151 struct talloc_reference_handle
*h
;
1154 for (h
=tc
->refs
;h
;h
=h
->next
) {
1161 report on memory usage by all children of a pointer, giving a full tree view
1163 void talloc_report_depth_cb(const void *ptr
, int depth
, int max_depth
,
1164 void (*callback
)(const void *ptr
,
1165 int depth
, int max_depth
,
1167 void *private_data
),
1170 struct talloc_chunk
*c
, *tc
;
1175 if (ptr
== NULL
) return;
1177 tc
= talloc_chunk_from_ptr(ptr
);
1179 if (tc
->flags
& TALLOC_FLAG_LOOP
) {
1183 callback(ptr
, depth
, max_depth
, 0, private_data
);
1185 if (max_depth
>= 0 && depth
>= max_depth
) {
1189 tc
->flags
|= TALLOC_FLAG_LOOP
;
1190 for (c
=tc
->child
;c
;c
=c
->next
) {
1191 if (c
->name
== TALLOC_MAGIC_REFERENCE
) {
1192 struct talloc_reference_handle
*h
= (struct talloc_reference_handle
*)TC_PTR_FROM_CHUNK(c
);
1193 callback(h
->ptr
, depth
+ 1, max_depth
, 1, private_data
);
1195 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c
), depth
+ 1, max_depth
, callback
, private_data
);
1198 tc
->flags
&= ~TALLOC_FLAG_LOOP
;
1201 static void talloc_report_depth_FILE_helper(const void *ptr
, int depth
, int max_depth
, int is_ref
, void *_f
)
1203 const char *name
= talloc_get_name(ptr
);
1204 FILE *f
= (FILE *)_f
;
1207 fprintf(f
, "%*sreference to: %s\n", depth
*4, "", name
);
1212 fprintf(f
,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1213 (max_depth
< 0 ? "full " :""), name
,
1214 (unsigned long)talloc_total_size(ptr
),
1215 (unsigned long)talloc_total_blocks(ptr
));
1219 fprintf(f
, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1222 (unsigned long)talloc_total_size(ptr
),
1223 (unsigned long)talloc_total_blocks(ptr
),
1224 (int)talloc_reference_count(ptr
), ptr
);
1227 fprintf(f
, "content: ");
1228 if (talloc_total_size(ptr
)) {
1229 int tot
= talloc_total_size(ptr
);
1232 for (i
= 0; i
< tot
; i
++) {
1233 if ((((char *)ptr
)[i
] > 31) && (((char *)ptr
)[i
] < 126)) {
1234 fprintf(f
, "%c", ((char *)ptr
)[i
]);
1236 fprintf(f
, "~%02x", ((char *)ptr
)[i
]);
1245 report on memory usage by all children of a pointer, giving a full tree view
1247 void talloc_report_depth_file(const void *ptr
, int depth
, int max_depth
, FILE *f
)
1249 talloc_report_depth_cb(ptr
, depth
, max_depth
, talloc_report_depth_FILE_helper
, f
);
1254 report on memory usage by all children of a pointer, giving a full tree view
1256 void talloc_report_full(const void *ptr
, FILE *f
)
1258 talloc_report_depth_file(ptr
, 0, -1, f
);
1262 report on memory usage by all children of a pointer
1264 void talloc_report(const void *ptr
, FILE *f
)
1266 talloc_report_depth_file(ptr
, 0, 1, f
);
1270 report on any memory hanging off the null context
1272 static void talloc_report_null(void)
1274 if (talloc_total_size(null_context
) != 0) {
1275 talloc_report(null_context
, stderr
);
1280 report on any memory hanging off the null context
1282 static void talloc_report_null_full(void)
1284 if (talloc_total_size(null_context
) != 0) {
1285 talloc_report_full(null_context
, stderr
);
1290 enable tracking of the NULL context
1292 void talloc_enable_null_tracking(void)
1294 if (null_context
== NULL
) {
1295 null_context
= _talloc_named_const(NULL
, 0, "null_context");
1300 disable tracking of the NULL context
1302 void talloc_disable_null_tracking(void)
1304 _talloc_free(null_context
);
1305 null_context
= NULL
;
1309 enable leak reporting on exit
1311 void talloc_enable_leak_report(void)
1313 talloc_enable_null_tracking();
1314 atexit(talloc_report_null
);
1318 enable full leak reporting on exit
1320 void talloc_enable_leak_report_full(void)
1322 talloc_enable_null_tracking();
1323 atexit(talloc_report_null_full
);
1327 talloc and zero memory.
1329 void *_talloc_zero(const void *ctx
, size_t size
, const char *name
)
1331 void *p
= _talloc_named_const(ctx
, size
, name
);
1334 memset(p
, '\0', size
);
1341 memdup with a talloc.
1343 void *_talloc_memdup(const void *t
, const void *p
, size_t size
, const char *name
)
1345 void *newp
= _talloc_named_const(t
, size
, name
);
1348 memcpy(newp
, p
, size
);
1354 static inline char *__talloc_strlendup(const void *t
, const char *p
, size_t len
)
1358 ret
= (char *)__talloc(t
, len
+ 1);
1359 if (unlikely(!ret
)) return NULL
;
1361 memcpy(ret
, p
, len
);
1364 _talloc_set_name_const(ret
, ret
);
1369 strdup with a talloc
1371 char *talloc_strdup(const void *t
, const char *p
)
1373 if (unlikely(!p
)) return NULL
;
1374 return __talloc_strlendup(t
, p
, strlen(p
));
1378 strndup with a talloc
1380 char *talloc_strndup(const void *t
, const char *p
, size_t n
)
1382 if (unlikely(!p
)) return NULL
;
1383 return __talloc_strlendup(t
, p
, strnlen(p
, n
));
1386 static inline char *__talloc_strlendup_append(char *s
, size_t slen
,
1387 const char *a
, size_t alen
)
1391 ret
= talloc_realloc(NULL
, s
, char, slen
+ alen
+ 1);
1392 if (unlikely(!ret
)) return NULL
;
1394 /* append the string and the trailing \0 */
1395 memcpy(&ret
[slen
], a
, alen
);
1398 _talloc_set_name_const(ret
, ret
);
1403 * Appends at the end of the string.
1405 char *talloc_strdup_append(char *s
, const char *a
)
1408 return talloc_strdup(NULL
, a
);
1415 return __talloc_strlendup_append(s
, strlen(s
), a
, strlen(a
));
1419 * Appends at the end of the talloc'ed buffer,
1420 * not the end of the string.
1422 char *talloc_strdup_append_buffer(char *s
, const char *a
)
1427 return talloc_strdup(NULL
, a
);
1434 slen
= talloc_get_size(s
);
1435 if (likely(slen
> 0)) {
1439 return __talloc_strlendup_append(s
, slen
, a
, strlen(a
));
1443 * Appends at the end of the string.
1445 char *talloc_strndup_append(char *s
, const char *a
, size_t n
)
1448 return talloc_strdup(NULL
, a
);
1455 return __talloc_strlendup_append(s
, strlen(s
), a
, strnlen(a
, n
));
1459 * Appends at the end of the talloc'ed buffer,
1460 * not the end of the string.
1462 char *talloc_strndup_append_buffer(char *s
, const char *a
, size_t n
)
1467 return talloc_strdup(NULL
, a
);
1474 slen
= talloc_get_size(s
);
1475 if (likely(slen
> 0)) {
1479 return __talloc_strlendup_append(s
, slen
, a
, strnlen(a
, n
));
1482 #ifndef HAVE_VA_COPY
1483 #ifdef HAVE___VA_COPY
1484 #define va_copy(dest, src) __va_copy(dest, src)
1486 #define va_copy(dest, src) (dest) = (src)
1490 char *talloc_vasprintf(const void *t
, const char *fmt
, va_list ap
)
1497 /* this call looks strange, but it makes it work on older solaris boxes */
1499 len
= vsnprintf(&c
, 1, fmt
, ap2
);
1501 if (unlikely(len
< 0)) {
1505 ret
= (char *)__talloc(t
, len
+1);
1506 if (unlikely(!ret
)) return NULL
;
1509 vsnprintf(ret
, len
+1, fmt
, ap2
);
1512 _talloc_set_name_const(ret
, ret
);
1518 Perform string formatting, and return a pointer to newly allocated
1519 memory holding the result, inside a memory pool.
1521 char *talloc_asprintf(const void *t
, const char *fmt
, ...)
1527 ret
= talloc_vasprintf(t
, fmt
, ap
);
1532 static inline char *__talloc_vaslenprintf_append(char *s
, size_t slen
,
1533 const char *fmt
, va_list ap
)
1534 PRINTF_ATTRIBUTE(3,0);
1536 static inline char *__talloc_vaslenprintf_append(char *s
, size_t slen
,
1537 const char *fmt
, va_list ap
)
1544 alen
= vsnprintf(&c
, 1, fmt
, ap2
);
1548 /* Either the vsnprintf failed or the format resulted in
1549 * no characters being formatted. In the former case, we
1550 * ought to return NULL, in the latter we ought to return
1551 * the original string. Most current callers of this
1552 * function expect it to never return NULL.
1557 s
= talloc_realloc(NULL
, s
, char, slen
+ alen
+ 1);
1558 if (!s
) return NULL
;
1561 vsnprintf(s
+ slen
, alen
+ 1, fmt
, ap2
);
1564 _talloc_set_name_const(s
, s
);
1569 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1570 * and return @p s, which may have moved. Good for gradually
1571 * accumulating output into a string buffer. Appends at the end
1574 char *talloc_vasprintf_append(char *s
, const char *fmt
, va_list ap
)
1577 return talloc_vasprintf(NULL
, fmt
, ap
);
1580 return __talloc_vaslenprintf_append(s
, strlen(s
), fmt
, ap
);
1584 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1585 * and return @p s, which may have moved. Always appends at the
1586 * end of the talloc'ed buffer, not the end of the string.
1588 char *talloc_vasprintf_append_buffer(char *s
, const char *fmt
, va_list ap
)
1593 return talloc_vasprintf(NULL
, fmt
, ap
);
1596 slen
= talloc_get_size(s
);
1597 if (likely(slen
> 0)) {
1601 return __talloc_vaslenprintf_append(s
, slen
, fmt
, ap
);
1605 Realloc @p s to append the formatted result of @p fmt and return @p
1606 s, which may have moved. Good for gradually accumulating output
1607 into a string buffer.
1609 char *talloc_asprintf_append(char *s
, const char *fmt
, ...)
1614 s
= talloc_vasprintf_append(s
, fmt
, ap
);
1620 Realloc @p s to append the formatted result of @p fmt and return @p
1621 s, which may have moved. Good for gradually accumulating output
1624 char *talloc_asprintf_append_buffer(char *s
, const char *fmt
, ...)
1629 s
= talloc_vasprintf_append_buffer(s
, fmt
, ap
);
1635 alloc an array, checking for integer overflow in the array size
1637 void *_talloc_array(const void *ctx
, size_t el_size
, unsigned count
, const char *name
)
1639 if (count
>= MAX_TALLOC_SIZE
/el_size
) {
1642 return _talloc_named_const(ctx
, el_size
* count
, name
);
1646 alloc an zero array, checking for integer overflow in the array size
1648 void *_talloc_zero_array(const void *ctx
, size_t el_size
, unsigned count
, const char *name
)
1650 if (count
>= MAX_TALLOC_SIZE
/el_size
) {
1653 return _talloc_zero(ctx
, el_size
* count
, name
);
1657 realloc an array, checking for integer overflow in the array size
1659 void *_talloc_realloc_array(const void *ctx
, void *ptr
, size_t el_size
, unsigned count
, const char *name
)
1661 if (count
>= MAX_TALLOC_SIZE
/el_size
) {
1664 return _talloc_realloc(ctx
, ptr
, el_size
* count
, name
);
1668 a function version of talloc_realloc(), so it can be passed as a function pointer
1669 to libraries that want a realloc function (a realloc function encapsulates
1670 all the basic capabilities of an allocation library, which is why this is useful)
1672 void *talloc_realloc_fn(const void *context
, void *ptr
, size_t size
)
1674 return _talloc_realloc(context
, ptr
, size
, NULL
);
1678 static int talloc_autofree_destructor(void *ptr
)
1680 autofree_context
= NULL
;
1684 static void talloc_autofree(void)
1686 _talloc_free(autofree_context
);
1690 return a context which will be auto-freed on exit
1691 this is useful for reducing the noise in leak reports
1693 void *talloc_autofree_context(void)
1695 if (autofree_context
== NULL
) {
1696 autofree_context
= _talloc_named_const(NULL
, 0, "autofree_context");
1697 talloc_set_destructor(autofree_context
, talloc_autofree_destructor
);
1698 atexit(talloc_autofree
);
1700 return autofree_context
;
1703 size_t talloc_get_size(const void *context
)
1705 struct talloc_chunk
*tc
;
1707 if (context
== NULL
)
1710 tc
= talloc_chunk_from_ptr(context
);
1716 find a parent of this context that has the given name, if any
1718 void *talloc_find_parent_byname(const void *context
, const char *name
)
1720 struct talloc_chunk
*tc
;
1722 if (context
== NULL
) {
1726 tc
= talloc_chunk_from_ptr(context
);
1728 if (tc
->name
&& strcmp(tc
->name
, name
) == 0) {
1729 return TC_PTR_FROM_CHUNK(tc
);
1731 while (tc
&& tc
->prev
) tc
= tc
->prev
;
1740 show the parentage of a context
1742 void talloc_show_parents(const void *context
, FILE *file
)
1744 struct talloc_chunk
*tc
;
1746 if (context
== NULL
) {
1747 fprintf(file
, "talloc no parents for NULL\n");
1751 tc
= talloc_chunk_from_ptr(context
);
1752 fprintf(file
, "talloc parents of '%s'\n", talloc_get_name(context
));
1754 fprintf(file
, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc
)));
1755 while (tc
&& tc
->prev
) tc
= tc
->prev
;
1764 return 1 if ptr is a parent of context
1766 int talloc_is_parent(const void *context
, const void *ptr
)
1768 struct talloc_chunk
*tc
;
1770 if (context
== NULL
) {
1774 tc
= talloc_chunk_from_ptr(context
);
1776 if (TC_PTR_FROM_CHUNK(tc
) == ptr
) return 1;
1777 while (tc
&& tc
->prev
) tc
= tc
->prev
;