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"
118 struct talloc_reference_handle
{
119 struct talloc_reference_handle
*next
, *prev
;
121 const char *location
;
124 typedef int (*talloc_destructor_t
)(void *);
126 struct talloc_chunk
{
127 struct talloc_chunk
*next
, *prev
;
128 struct talloc_chunk
*parent
, *child
;
129 struct talloc_reference_handle
*refs
;
130 talloc_destructor_t destructor
;
136 * "pool" has dual use:
138 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
139 * marks the end of the currently allocated area.
141 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
142 * is a pointer to the struct talloc_chunk of the pool that it was
143 * allocated from. This way children can quickly find the pool to chew
149 /* 16 byte alignment seems to keep everyone happy */
150 #define TC_ALIGN16(s) (((s)+15)&~15)
151 #define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
152 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
154 _PUBLIC_
int talloc_version_major(void)
156 return TALLOC_VERSION_MAJOR
;
159 _PUBLIC_
int talloc_version_minor(void)
161 return TALLOC_VERSION_MINOR
;
164 static void (*talloc_log_fn
)(const char *message
);
166 _PUBLIC_
void talloc_set_log_fn(void (*log_fn
)(const char *message
))
168 talloc_log_fn
= log_fn
;
171 static void talloc_log(const char *fmt
, ...) PRINTF_ATTRIBUTE(1,2);
172 static void talloc_log(const char *fmt
, ...)
177 if (!talloc_log_fn
) {
182 message
= talloc_vasprintf(NULL
, fmt
, ap
);
185 talloc_log_fn(message
);
186 talloc_free(message
);
189 static void talloc_log_stderr(const char *message
)
191 fprintf(stderr
, "%s", message
);
194 _PUBLIC_
void talloc_set_log_stderr(void)
196 talloc_set_log_fn(talloc_log_stderr
);
199 static void (*talloc_abort_fn
)(const char *reason
);
201 _PUBLIC_
void talloc_set_abort_fn(void (*abort_fn
)(const char *reason
))
203 talloc_abort_fn
= abort_fn
;
206 static void talloc_abort(const char *reason
)
208 talloc_log("%s\n", reason
);
210 if (!talloc_abort_fn
) {
211 TALLOC_ABORT(reason
);
214 talloc_abort_fn(reason
);
217 static void talloc_abort_magic(unsigned magic
)
219 unsigned striped
= magic
- TALLOC_MAGIC_BASE
;
220 unsigned major
= (striped
& 0xFFFFF000) >> 12;
221 unsigned minor
= (striped
& 0x00000FF0) >> 4;
222 talloc_log("Bad talloc magic[0x%08X/%u/%u] expected[0x%08X/%u/%u]\n",
224 TALLOC_MAGIC
, TALLOC_VERSION_MAJOR
, TALLOC_VERSION_MINOR
);
225 talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
228 static void talloc_abort_access_after_free(void)
230 talloc_abort("Bad talloc magic value - access after free");
233 static void talloc_abort_unknown_value(void)
235 talloc_abort("Bad talloc magic value - unknown value");
238 /* panic if we get a bad magic value */
239 static inline struct talloc_chunk
*talloc_chunk_from_ptr(const void *ptr
)
241 const char *pp
= (const char *)ptr
;
242 struct talloc_chunk
*tc
= discard_const_p(struct talloc_chunk
, pp
- TC_HDR_SIZE
);
243 if (unlikely((tc
->flags
& (TALLOC_FLAG_FREE
| ~0xF)) != TALLOC_MAGIC
)) {
244 if ((tc
->flags
& (~0xFFF)) == TALLOC_MAGIC_BASE
) {
245 talloc_abort_magic(tc
->flags
& (~0xF));
249 if (tc
->flags
& TALLOC_FLAG_FREE
) {
250 talloc_log("talloc: access after free error - first free may be at %s\n", tc
->name
);
251 talloc_abort_access_after_free();
254 talloc_abort_unknown_value();
261 /* hook into the front of the list */
262 #define _TLIST_ADD(list, p) \
266 (p)->next = (p)->prev = NULL; \
268 (list)->prev = (p); \
269 (p)->next = (list); \
275 /* remove an element from a list - element doesn't have to be in list. */
276 #define _TLIST_REMOVE(list, p) \
278 if ((p) == (list)) { \
279 (list) = (p)->next; \
280 if (list) (list)->prev = NULL; \
282 if ((p)->prev) (p)->prev->next = (p)->next; \
283 if ((p)->next) (p)->next->prev = (p)->prev; \
285 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
290 return the parent chunk of a pointer
292 static inline struct talloc_chunk
*talloc_parent_chunk(const void *ptr
)
294 struct talloc_chunk
*tc
;
296 if (unlikely(ptr
== NULL
)) {
300 tc
= talloc_chunk_from_ptr(ptr
);
301 while (tc
->prev
) tc
=tc
->prev
;
306 _PUBLIC_
void *talloc_parent(const void *ptr
)
308 struct talloc_chunk
*tc
= talloc_parent_chunk(ptr
);
309 return tc
? TC_PTR_FROM_CHUNK(tc
) : NULL
;
315 _PUBLIC_
const char *talloc_parent_name(const void *ptr
)
317 struct talloc_chunk
*tc
= talloc_parent_chunk(ptr
);
318 return tc
? tc
->name
: NULL
;
322 A pool carries an in-pool object count count in the first 16 bytes.
323 bytes. This is done to support talloc_steal() to a parent outside of the
324 pool. The count includes the pool itself, so a talloc_free() on a pool will
325 only destroy the pool if the count has dropped to zero. A talloc_free() of a
326 pool member will reduce the count, and eventually also call free(3) on the
329 The object count is not put into "struct talloc_chunk" because it is only
330 relevant for talloc pools and the alignment to 16 bytes would increase the
331 memory footprint of each talloc chunk by those 16 bytes.
334 #define TALLOC_POOL_HDR_SIZE 16
336 #define TC_POOL_SPACE_LEFT(_pool_tc) \
337 PTR_DIFF(TC_HDR_SIZE + (_pool_tc)->size + (char *)(_pool_tc), \
340 #define TC_POOL_FIRST_CHUNK(_pool_tc) \
341 ((void *)(TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE + (char *)(_pool_tc)))
343 static unsigned int *talloc_pool_objectcount(struct talloc_chunk
*tc
)
345 return (unsigned int *)((char *)tc
+ TC_HDR_SIZE
);
352 static struct talloc_chunk
*talloc_alloc_pool(struct talloc_chunk
*parent
,
355 struct talloc_chunk
*pool_ctx
= NULL
;
357 struct talloc_chunk
*result
;
360 if (parent
== NULL
) {
364 if (parent
->flags
& TALLOC_FLAG_POOL
) {
367 else if (parent
->flags
& TALLOC_FLAG_POOLMEM
) {
368 pool_ctx
= (struct talloc_chunk
*)parent
->pool
;
371 if (pool_ctx
== NULL
) {
375 space_left
= TC_POOL_SPACE_LEFT(pool_ctx
);
378 * Align size to 16 bytes
380 chunk_size
= TC_ALIGN16(size
);
382 if (space_left
< chunk_size
) {
386 result
= (struct talloc_chunk
*)pool_ctx
->pool
;
388 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
389 VALGRIND_MAKE_MEM_UNDEFINED(result
, size
);
392 pool_ctx
->pool
= (void *)((char *)result
+ chunk_size
);
394 result
->flags
= TALLOC_MAGIC
| TALLOC_FLAG_POOLMEM
;
395 result
->pool
= pool_ctx
;
397 *talloc_pool_objectcount(pool_ctx
) += 1;
403 Allocate a bit of memory as a child of an existing pointer
405 static inline void *__talloc(const void *context
, size_t size
)
407 struct talloc_chunk
*tc
= NULL
;
409 if (unlikely(context
== NULL
)) {
410 context
= null_context
;
413 if (unlikely(size
>= MAX_TALLOC_SIZE
)) {
417 if (context
!= NULL
) {
418 tc
= talloc_alloc_pool(talloc_chunk_from_ptr(context
),
423 tc
= (struct talloc_chunk
*)malloc(TC_HDR_SIZE
+size
);
424 if (unlikely(tc
== NULL
)) return NULL
;
425 tc
->flags
= TALLOC_MAGIC
;
430 tc
->destructor
= NULL
;
435 if (likely(context
)) {
436 struct talloc_chunk
*parent
= talloc_chunk_from_ptr(context
);
439 parent
->child
->parent
= NULL
;
440 tc
->next
= parent
->child
;
449 tc
->next
= tc
->prev
= tc
->parent
= NULL
;
452 return TC_PTR_FROM_CHUNK(tc
);
456 * Create a talloc pool
459 _PUBLIC_
void *talloc_pool(const void *context
, size_t size
)
461 void *result
= __talloc(context
, size
+ TALLOC_POOL_HDR_SIZE
);
462 struct talloc_chunk
*tc
;
464 if (unlikely(result
== NULL
)) {
468 tc
= talloc_chunk_from_ptr(result
);
470 tc
->flags
|= TALLOC_FLAG_POOL
;
471 tc
->pool
= TC_POOL_FIRST_CHUNK(tc
);
473 *talloc_pool_objectcount(tc
) = 1;
475 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
476 VALGRIND_MAKE_MEM_NOACCESS(tc
->pool
, size
);
483 setup a destructor to be called on free of a pointer
484 the destructor should return 0 on success, or -1 on failure.
485 if the destructor fails then the free is failed, and the memory can
486 be continued to be used
488 _PUBLIC_
void _talloc_set_destructor(const void *ptr
, int (*destructor
)(void *))
490 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
491 tc
->destructor
= destructor
;
495 increase the reference count on a piece of memory.
497 _PUBLIC_
int talloc_increase_ref_count(const void *ptr
)
499 if (unlikely(!talloc_reference(null_context
, ptr
))) {
506 helper for talloc_reference()
508 this is referenced by a function pointer and should not be inline
510 static int talloc_reference_destructor(struct talloc_reference_handle
*handle
)
512 struct talloc_chunk
*ptr_tc
= talloc_chunk_from_ptr(handle
->ptr
);
513 _TLIST_REMOVE(ptr_tc
->refs
, handle
);
518 more efficient way to add a name to a pointer - the name must point to a
521 static inline void _talloc_set_name_const(const void *ptr
, const char *name
)
523 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
528 internal talloc_named_const()
530 static inline void *_talloc_named_const(const void *context
, size_t size
, const char *name
)
534 ptr
= __talloc(context
, size
);
535 if (unlikely(ptr
== NULL
)) {
539 _talloc_set_name_const(ptr
, name
);
545 make a secondary reference to a pointer, hanging off the given context.
546 the pointer remains valid until both the original caller and this given
549 the major use for this is when two different structures need to reference the
550 same underlying data, and you want to be able to free the two instances separately,
553 _PUBLIC_
void *_talloc_reference_loc(const void *context
, const void *ptr
, const char *location
)
555 struct talloc_chunk
*tc
;
556 struct talloc_reference_handle
*handle
;
557 if (unlikely(ptr
== NULL
)) return NULL
;
559 tc
= talloc_chunk_from_ptr(ptr
);
560 handle
= (struct talloc_reference_handle
*)_talloc_named_const(context
,
561 sizeof(struct talloc_reference_handle
),
562 TALLOC_MAGIC_REFERENCE
);
563 if (unlikely(handle
== NULL
)) return NULL
;
565 /* note that we hang the destructor off the handle, not the
566 main context as that allows the caller to still setup their
567 own destructor on the context if they want to */
568 talloc_set_destructor(handle
, talloc_reference_destructor
);
569 handle
->ptr
= discard_const_p(void, ptr
);
570 handle
->location
= location
;
571 _TLIST_ADD(tc
->refs
, handle
);
575 static void *_talloc_steal_internal(const void *new_ctx
, const void *ptr
);
578 internal talloc_free call
580 static inline int _talloc_free_internal(void *ptr
, const char *location
)
582 struct talloc_chunk
*tc
;
584 if (unlikely(ptr
== NULL
)) {
588 /* possibly initialised the talloc fill value */
589 if (!talloc_fill
.initialised
) {
590 const char *fill
= getenv(TALLOC_FILL_ENV
);
592 talloc_fill
.enabled
= true;
593 talloc_fill
.fill_value
= strtoul(fill
, NULL
, 0);
595 talloc_fill
.initialised
= true;
598 tc
= talloc_chunk_from_ptr(ptr
);
600 if (unlikely(tc
->refs
)) {
602 /* check if this is a reference from a child or
603 * grandchild back to it's parent or grandparent
605 * in that case we need to remove the reference and
606 * call another instance of talloc_free() on the current
609 is_child
= talloc_is_parent(tc
->refs
, ptr
);
610 _talloc_free_internal(tc
->refs
, location
);
612 return _talloc_free_internal(ptr
, location
);
617 if (unlikely(tc
->flags
& TALLOC_FLAG_LOOP
)) {
618 /* we have a free loop - stop looping */
622 if (unlikely(tc
->destructor
)) {
623 talloc_destructor_t d
= tc
->destructor
;
624 if (d
== (talloc_destructor_t
)-1) {
627 tc
->destructor
= (talloc_destructor_t
)-1;
632 tc
->destructor
= NULL
;
636 _TLIST_REMOVE(tc
->parent
->child
, tc
);
637 if (tc
->parent
->child
) {
638 tc
->parent
->child
->parent
= tc
->parent
;
641 if (tc
->prev
) tc
->prev
->next
= tc
->next
;
642 if (tc
->next
) tc
->next
->prev
= tc
->prev
;
645 tc
->flags
|= TALLOC_FLAG_LOOP
;
648 /* we need to work out who will own an abandoned child
649 if it cannot be freed. In priority order, the first
650 choice is owner of any remaining reference to this
651 pointer, the second choice is our parent, and the
652 final choice is the null context. */
653 void *child
= TC_PTR_FROM_CHUNK(tc
->child
);
654 const void *new_parent
= null_context
;
655 struct talloc_chunk
*old_parent
= NULL
;
656 if (unlikely(tc
->child
->refs
)) {
657 struct talloc_chunk
*p
= talloc_parent_chunk(tc
->child
->refs
);
658 if (p
) new_parent
= TC_PTR_FROM_CHUNK(p
);
660 /* finding the parent here is potentially quite
661 expensive, but the alternative, which is to change
662 talloc to always have a valid tc->parent pointer,
663 makes realloc more expensive where there are a
664 large number of children.
666 The reason we need the parent pointer here is that
667 if _talloc_free_internal() fails due to references
668 or a failing destructor we need to re-parent, but
669 the free call can invalidate the prev pointer.
671 if (new_parent
== null_context
&& (tc
->child
->refs
|| tc
->child
->destructor
)) {
672 old_parent
= talloc_parent_chunk(ptr
);
674 if (unlikely(_talloc_free_internal(child
, location
) == -1)) {
675 if (new_parent
== null_context
) {
676 struct talloc_chunk
*p
= old_parent
;
677 if (p
) new_parent
= TC_PTR_FROM_CHUNK(p
);
679 _talloc_steal_internal(new_parent
, child
);
683 tc
->flags
|= TALLOC_FLAG_FREE
;
685 /* we mark the freed memory with where we called the free
686 * from. This means on a double free error we can report where
687 * the first free came from
691 if (tc
->flags
& (TALLOC_FLAG_POOL
|TALLOC_FLAG_POOLMEM
)) {
692 struct talloc_chunk
*pool
;
693 unsigned int *pool_object_count
;
695 pool
= (tc
->flags
& TALLOC_FLAG_POOL
)
696 ? tc
: (struct talloc_chunk
*)tc
->pool
;
698 pool_object_count
= talloc_pool_objectcount(pool
);
700 if (*pool_object_count
== 0) {
701 talloc_abort("Pool object count zero!");
705 *pool_object_count
-= 1;
707 if (*pool_object_count
== 0) {
708 if (talloc_fill
.enabled
) {
709 memset(TC_PTR_FROM_CHUNK(pool
), talloc_fill
.fill_value
, pool
->size
);
715 if (talloc_fill
.enabled
) {
716 /* don't wipe the header, to allow the
717 double-free logic to still work
719 memset(TC_PTR_FROM_CHUNK(tc
), talloc_fill
.fill_value
, tc
->size
);
727 move a lump of memory from one talloc context to another return the
728 ptr on success, or NULL if it could not be transferred.
729 passing NULL as ptr will always return NULL with no side effects.
731 static void *_talloc_steal_internal(const void *new_ctx
, const void *ptr
)
733 struct talloc_chunk
*tc
, *new_tc
;
735 if (unlikely(!ptr
)) {
739 if (unlikely(new_ctx
== NULL
)) {
740 new_ctx
= null_context
;
743 tc
= talloc_chunk_from_ptr(ptr
);
745 if (unlikely(new_ctx
== NULL
)) {
747 _TLIST_REMOVE(tc
->parent
->child
, tc
);
748 if (tc
->parent
->child
) {
749 tc
->parent
->child
->parent
= tc
->parent
;
752 if (tc
->prev
) tc
->prev
->next
= tc
->next
;
753 if (tc
->next
) tc
->next
->prev
= tc
->prev
;
756 tc
->parent
= tc
->next
= tc
->prev
= NULL
;
757 return discard_const_p(void, ptr
);
760 new_tc
= talloc_chunk_from_ptr(new_ctx
);
762 if (unlikely(tc
== new_tc
|| tc
->parent
== new_tc
)) {
763 return discard_const_p(void, ptr
);
767 _TLIST_REMOVE(tc
->parent
->child
, tc
);
768 if (tc
->parent
->child
) {
769 tc
->parent
->child
->parent
= tc
->parent
;
772 if (tc
->prev
) tc
->prev
->next
= tc
->next
;
773 if (tc
->next
) tc
->next
->prev
= tc
->prev
;
777 if (new_tc
->child
) new_tc
->child
->parent
= NULL
;
778 _TLIST_ADD(new_tc
->child
, tc
);
780 return discard_const_p(void, ptr
);
784 move a lump of memory from one talloc context to another return the
785 ptr on success, or NULL if it could not be transferred.
786 passing NULL as ptr will always return NULL with no side effects.
788 _PUBLIC_
void *_talloc_steal_loc(const void *new_ctx
, const void *ptr
, const char *location
)
790 struct talloc_chunk
*tc
;
792 if (unlikely(ptr
== NULL
)) {
796 tc
= talloc_chunk_from_ptr(ptr
);
798 if (unlikely(tc
->refs
!= NULL
) && talloc_parent(ptr
) != new_ctx
) {
799 struct talloc_reference_handle
*h
;
801 talloc_log("WARNING: talloc_steal with references at %s\n",
804 for (h
=tc
->refs
; h
; h
=h
->next
) {
805 talloc_log("\treference at %s\n",
811 /* this test is probably too expensive to have on in the
812 normal build, but it useful for debugging */
813 if (talloc_is_parent(new_ctx
, ptr
)) {
814 talloc_log("WARNING: stealing into talloc child at %s\n", location
);
818 return _talloc_steal_internal(new_ctx
, ptr
);
822 this is like a talloc_steal(), but you must supply the old
823 parent. This resolves the ambiguity in a talloc_steal() which is
824 called on a context that has more than one parent (via references)
826 The old parent can be either a reference or a parent
828 _PUBLIC_
void *talloc_reparent(const void *old_parent
, const void *new_parent
, const void *ptr
)
830 struct talloc_chunk
*tc
;
831 struct talloc_reference_handle
*h
;
833 if (unlikely(ptr
== NULL
)) {
837 if (old_parent
== talloc_parent(ptr
)) {
838 return _talloc_steal_internal(new_parent
, ptr
);
841 tc
= talloc_chunk_from_ptr(ptr
);
842 for (h
=tc
->refs
;h
;h
=h
->next
) {
843 if (talloc_parent(h
) == old_parent
) {
844 if (_talloc_steal_internal(new_parent
, h
) != h
) {
847 return discard_const_p(void, ptr
);
851 /* it wasn't a parent */
856 remove a secondary reference to a pointer. This undo's what
857 talloc_reference() has done. The context and pointer arguments
858 must match those given to a talloc_reference()
860 static inline int talloc_unreference(const void *context
, const void *ptr
)
862 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
863 struct talloc_reference_handle
*h
;
865 if (unlikely(context
== NULL
)) {
866 context
= null_context
;
869 for (h
=tc
->refs
;h
;h
=h
->next
) {
870 struct talloc_chunk
*p
= talloc_parent_chunk(h
);
872 if (context
== NULL
) break;
873 } else if (TC_PTR_FROM_CHUNK(p
) == context
) {
881 return _talloc_free_internal(h
, __location__
);
885 remove a specific parent context from a pointer. This is a more
886 controlled varient of talloc_free()
888 _PUBLIC_
int talloc_unlink(const void *context
, void *ptr
)
890 struct talloc_chunk
*tc_p
, *new_p
;
897 if (context
== NULL
) {
898 context
= null_context
;
901 if (talloc_unreference(context
, ptr
) == 0) {
905 if (context
== NULL
) {
906 if (talloc_parent_chunk(ptr
) != NULL
) {
910 if (talloc_chunk_from_ptr(context
) != talloc_parent_chunk(ptr
)) {
915 tc_p
= talloc_chunk_from_ptr(ptr
);
917 if (tc_p
->refs
== NULL
) {
918 return _talloc_free_internal(ptr
, __location__
);
921 new_p
= talloc_parent_chunk(tc_p
->refs
);
923 new_parent
= TC_PTR_FROM_CHUNK(new_p
);
928 if (talloc_unreference(new_parent
, ptr
) != 0) {
932 _talloc_steal_internal(new_parent
, ptr
);
938 add a name to an existing pointer - va_list version
940 static inline const char *talloc_set_name_v(const void *ptr
, const char *fmt
, va_list ap
) PRINTF_ATTRIBUTE(2,0);
942 static inline const char *talloc_set_name_v(const void *ptr
, const char *fmt
, va_list ap
)
944 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
945 tc
->name
= talloc_vasprintf(ptr
, fmt
, ap
);
946 if (likely(tc
->name
)) {
947 _talloc_set_name_const(tc
->name
, ".name");
953 add a name to an existing pointer
955 _PUBLIC_
const char *talloc_set_name(const void *ptr
, const char *fmt
, ...)
960 name
= talloc_set_name_v(ptr
, fmt
, ap
);
967 create a named talloc pointer. Any talloc pointer can be named, and
968 talloc_named() operates just like talloc() except that it allows you
971 _PUBLIC_
void *talloc_named(const void *context
, size_t size
, const char *fmt
, ...)
977 ptr
= __talloc(context
, size
);
978 if (unlikely(ptr
== NULL
)) return NULL
;
981 name
= talloc_set_name_v(ptr
, fmt
, ap
);
984 if (unlikely(name
== NULL
)) {
985 _talloc_free_internal(ptr
, __location__
);
993 return the name of a talloc ptr, or "UNNAMED"
995 _PUBLIC_
const char *talloc_get_name(const void *ptr
)
997 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
998 if (unlikely(tc
->name
== TALLOC_MAGIC_REFERENCE
)) {
1001 if (likely(tc
->name
)) {
1009 check if a pointer has the given name. If it does, return the pointer,
1010 otherwise return NULL
1012 _PUBLIC_
void *talloc_check_name(const void *ptr
, const char *name
)
1015 if (unlikely(ptr
== NULL
)) return NULL
;
1016 pname
= talloc_get_name(ptr
);
1017 if (likely(pname
== name
|| strcmp(pname
, name
) == 0)) {
1018 return discard_const_p(void, ptr
);
1023 static void talloc_abort_type_missmatch(const char *location
,
1025 const char *expected
)
1029 reason
= talloc_asprintf(NULL
,
1030 "%s: Type mismatch: name[%s] expected[%s]",
1035 reason
= "Type mismatch";
1038 talloc_abort(reason
);
1041 _PUBLIC_
void *_talloc_get_type_abort(const void *ptr
, const char *name
, const char *location
)
1045 if (unlikely(ptr
== NULL
)) {
1046 talloc_abort_type_missmatch(location
, NULL
, name
);
1050 pname
= talloc_get_name(ptr
);
1051 if (likely(pname
== name
|| strcmp(pname
, name
) == 0)) {
1052 return discard_const_p(void, ptr
);
1055 talloc_abort_type_missmatch(location
, pname
, name
);
1060 this is for compatibility with older versions of talloc
1062 _PUBLIC_
void *talloc_init(const char *fmt
, ...)
1068 ptr
= __talloc(NULL
, 0);
1069 if (unlikely(ptr
== NULL
)) return NULL
;
1072 name
= talloc_set_name_v(ptr
, fmt
, ap
);
1075 if (unlikely(name
== NULL
)) {
1076 _talloc_free_internal(ptr
, __location__
);
1084 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1085 should probably not be used in new code. It's in here to keep the talloc
1086 code consistent across Samba 3 and 4.
1088 _PUBLIC_
void talloc_free_children(void *ptr
)
1090 struct talloc_chunk
*tc
;
1092 if (unlikely(ptr
== NULL
)) {
1096 tc
= talloc_chunk_from_ptr(ptr
);
1099 /* we need to work out who will own an abandoned child
1100 if it cannot be freed. In priority order, the first
1101 choice is owner of any remaining reference to this
1102 pointer, the second choice is our parent, and the
1103 final choice is the null context. */
1104 void *child
= TC_PTR_FROM_CHUNK(tc
->child
);
1105 const void *new_parent
= null_context
;
1106 if (unlikely(tc
->child
->refs
)) {
1107 struct talloc_chunk
*p
= talloc_parent_chunk(tc
->child
->refs
);
1108 if (p
) new_parent
= TC_PTR_FROM_CHUNK(p
);
1110 if (unlikely(talloc_free(child
) == -1)) {
1111 if (new_parent
== null_context
) {
1112 struct talloc_chunk
*p
= talloc_parent_chunk(ptr
);
1113 if (p
) new_parent
= TC_PTR_FROM_CHUNK(p
);
1115 _talloc_steal_internal(new_parent
, child
);
1119 if ((tc
->flags
& TALLOC_FLAG_POOL
)
1120 && (*talloc_pool_objectcount(tc
) == 1)) {
1121 tc
->pool
= TC_POOL_FIRST_CHUNK(tc
);
1122 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
1123 VALGRIND_MAKE_MEM_NOACCESS(
1124 tc
->pool
, tc
->size
- TALLOC_POOL_HDR_SIZE
);
1130 Allocate a bit of memory as a child of an existing pointer
1132 _PUBLIC_
void *_talloc(const void *context
, size_t size
)
1134 return __talloc(context
, size
);
1138 externally callable talloc_set_name_const()
1140 _PUBLIC_
void talloc_set_name_const(const void *ptr
, const char *name
)
1142 _talloc_set_name_const(ptr
, name
);
1146 create a named talloc pointer. Any talloc pointer can be named, and
1147 talloc_named() operates just like talloc() except that it allows you
1148 to name the pointer.
1150 _PUBLIC_
void *talloc_named_const(const void *context
, size_t size
, const char *name
)
1152 return _talloc_named_const(context
, size
, name
);
1156 free a talloc pointer. This also frees all child pointers of this
1159 return 0 if the memory is actually freed, otherwise -1. The memory
1160 will not be freed if the ref_count is > 1 or the destructor (if
1161 any) returns non-zero
1163 _PUBLIC_
int _talloc_free(void *ptr
, const char *location
)
1165 struct talloc_chunk
*tc
;
1167 if (unlikely(ptr
== NULL
)) {
1171 tc
= talloc_chunk_from_ptr(ptr
);
1173 if (unlikely(tc
->refs
!= NULL
)) {
1174 struct talloc_reference_handle
*h
;
1176 if (talloc_parent(ptr
) == null_context
&& tc
->refs
->next
== NULL
) {
1177 /* in this case we do know which parent should
1178 get this pointer, as there is really only
1180 return talloc_unlink(null_context
, ptr
);
1183 talloc_log("ERROR: talloc_free with references at %s\n",
1186 for (h
=tc
->refs
; h
; h
=h
->next
) {
1187 talloc_log("\treference at %s\n",
1193 return _talloc_free_internal(ptr
, location
);
1199 A talloc version of realloc. The context argument is only used if
1202 _PUBLIC_
void *_talloc_realloc(const void *context
, void *ptr
, size_t size
, const char *name
)
1204 struct talloc_chunk
*tc
;
1206 bool malloced
= false;
1208 /* size zero is equivalent to free() */
1209 if (unlikely(size
== 0)) {
1210 talloc_unlink(context
, ptr
);
1214 if (unlikely(size
>= MAX_TALLOC_SIZE
)) {
1218 /* realloc(NULL) is equivalent to malloc() */
1220 return _talloc_named_const(context
, size
, name
);
1223 tc
= talloc_chunk_from_ptr(ptr
);
1225 /* don't allow realloc on referenced pointers */
1226 if (unlikely(tc
->refs
)) {
1230 /* don't let anybody try to realloc a talloc_pool */
1231 if (unlikely(tc
->flags
& TALLOC_FLAG_POOL
)) {
1235 /* don't shrink if we have less than 1k to gain */
1236 if ((size
< tc
->size
) && ((tc
->size
- size
) < 1024)) {
1241 /* by resetting magic we catch users of the old memory */
1242 tc
->flags
|= TALLOC_FLAG_FREE
;
1245 new_ptr
= malloc(size
+ TC_HDR_SIZE
);
1247 memcpy(new_ptr
, tc
, MIN(tc
->size
, size
) + TC_HDR_SIZE
);
1251 if (tc
->flags
& TALLOC_FLAG_POOLMEM
) {
1253 new_ptr
= talloc_alloc_pool(tc
, size
+ TC_HDR_SIZE
);
1254 *talloc_pool_objectcount((struct talloc_chunk
*)
1257 if (new_ptr
== NULL
) {
1258 new_ptr
= malloc(TC_HDR_SIZE
+size
);
1263 memcpy(new_ptr
, tc
, MIN(tc
->size
,size
) + TC_HDR_SIZE
);
1267 new_ptr
= realloc(tc
, size
+ TC_HDR_SIZE
);
1270 if (unlikely(!new_ptr
)) {
1271 tc
->flags
&= ~TALLOC_FLAG_FREE
;
1275 tc
= (struct talloc_chunk
*)new_ptr
;
1276 tc
->flags
&= ~TALLOC_FLAG_FREE
;
1278 tc
->flags
&= ~TALLOC_FLAG_POOLMEM
;
1281 tc
->parent
->child
= tc
;
1284 tc
->child
->parent
= tc
;
1288 tc
->prev
->next
= tc
;
1291 tc
->next
->prev
= tc
;
1295 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc
), name
);
1297 return TC_PTR_FROM_CHUNK(tc
);
1301 a wrapper around talloc_steal() for situations where you are moving a pointer
1302 between two structures, and want the old pointer to be set to NULL
1304 _PUBLIC_
void *_talloc_move(const void *new_ctx
, const void *_pptr
)
1306 const void **pptr
= discard_const_p(const void *,_pptr
);
1307 void *ret
= talloc_steal(new_ctx
, discard_const_p(void, *pptr
));
1313 return the total size of a talloc pool (subtree)
1315 _PUBLIC_
size_t talloc_total_size(const void *ptr
)
1318 struct talloc_chunk
*c
, *tc
;
1327 tc
= talloc_chunk_from_ptr(ptr
);
1329 if (tc
->flags
& TALLOC_FLAG_LOOP
) {
1333 tc
->flags
|= TALLOC_FLAG_LOOP
;
1335 if (likely(tc
->name
!= TALLOC_MAGIC_REFERENCE
)) {
1338 for (c
=tc
->child
;c
;c
=c
->next
) {
1339 total
+= talloc_total_size(TC_PTR_FROM_CHUNK(c
));
1342 tc
->flags
&= ~TALLOC_FLAG_LOOP
;
1348 return the total number of blocks in a talloc pool (subtree)
1350 _PUBLIC_
size_t talloc_total_blocks(const void *ptr
)
1353 struct talloc_chunk
*c
, *tc
;
1362 tc
= talloc_chunk_from_ptr(ptr
);
1364 if (tc
->flags
& TALLOC_FLAG_LOOP
) {
1368 tc
->flags
|= TALLOC_FLAG_LOOP
;
1371 for (c
=tc
->child
;c
;c
=c
->next
) {
1372 total
+= talloc_total_blocks(TC_PTR_FROM_CHUNK(c
));
1375 tc
->flags
&= ~TALLOC_FLAG_LOOP
;
1381 return the number of external references to a pointer
1383 _PUBLIC_
size_t talloc_reference_count(const void *ptr
)
1385 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
1386 struct talloc_reference_handle
*h
;
1389 for (h
=tc
->refs
;h
;h
=h
->next
) {
1396 report on memory usage by all children of a pointer, giving a full tree view
1398 _PUBLIC_
void talloc_report_depth_cb(const void *ptr
, int depth
, int max_depth
,
1399 void (*callback
)(const void *ptr
,
1400 int depth
, int max_depth
,
1402 void *private_data
),
1405 struct talloc_chunk
*c
, *tc
;
1410 if (ptr
== NULL
) return;
1412 tc
= talloc_chunk_from_ptr(ptr
);
1414 if (tc
->flags
& TALLOC_FLAG_LOOP
) {
1418 callback(ptr
, depth
, max_depth
, 0, private_data
);
1420 if (max_depth
>= 0 && depth
>= max_depth
) {
1424 tc
->flags
|= TALLOC_FLAG_LOOP
;
1425 for (c
=tc
->child
;c
;c
=c
->next
) {
1426 if (c
->name
== TALLOC_MAGIC_REFERENCE
) {
1427 struct talloc_reference_handle
*h
= (struct talloc_reference_handle
*)TC_PTR_FROM_CHUNK(c
);
1428 callback(h
->ptr
, depth
+ 1, max_depth
, 1, private_data
);
1430 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c
), depth
+ 1, max_depth
, callback
, private_data
);
1433 tc
->flags
&= ~TALLOC_FLAG_LOOP
;
1436 static void talloc_report_depth_FILE_helper(const void *ptr
, int depth
, int max_depth
, int is_ref
, void *_f
)
1438 const char *name
= talloc_get_name(ptr
);
1439 FILE *f
= (FILE *)_f
;
1442 fprintf(f
, "%*sreference to: %s\n", depth
*4, "", name
);
1447 fprintf(f
,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1448 (max_depth
< 0 ? "full " :""), name
,
1449 (unsigned long)talloc_total_size(ptr
),
1450 (unsigned long)talloc_total_blocks(ptr
));
1454 fprintf(f
, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1457 (unsigned long)talloc_total_size(ptr
),
1458 (unsigned long)talloc_total_blocks(ptr
),
1459 (int)talloc_reference_count(ptr
), ptr
);
1462 fprintf(f
, "content: ");
1463 if (talloc_total_size(ptr
)) {
1464 int tot
= talloc_total_size(ptr
);
1467 for (i
= 0; i
< tot
; i
++) {
1468 if ((((char *)ptr
)[i
] > 31) && (((char *)ptr
)[i
] < 126)) {
1469 fprintf(f
, "%c", ((char *)ptr
)[i
]);
1471 fprintf(f
, "~%02x", ((char *)ptr
)[i
]);
1480 report on memory usage by all children of a pointer, giving a full tree view
1482 _PUBLIC_
void talloc_report_depth_file(const void *ptr
, int depth
, int max_depth
, FILE *f
)
1485 talloc_report_depth_cb(ptr
, depth
, max_depth
, talloc_report_depth_FILE_helper
, f
);
1491 report on memory usage by all children of a pointer, giving a full tree view
1493 _PUBLIC_
void talloc_report_full(const void *ptr
, FILE *f
)
1495 talloc_report_depth_file(ptr
, 0, -1, f
);
1499 report on memory usage by all children of a pointer
1501 _PUBLIC_
void talloc_report(const void *ptr
, FILE *f
)
1503 talloc_report_depth_file(ptr
, 0, 1, f
);
1507 report on any memory hanging off the null context
1509 static void talloc_report_null(void)
1511 if (talloc_total_size(null_context
) != 0) {
1512 talloc_report(null_context
, stderr
);
1517 report on any memory hanging off the null context
1519 static void talloc_report_null_full(void)
1521 if (talloc_total_size(null_context
) != 0) {
1522 talloc_report_full(null_context
, stderr
);
1527 enable tracking of the NULL context
1529 _PUBLIC_
void talloc_enable_null_tracking(void)
1531 if (null_context
== NULL
) {
1532 null_context
= _talloc_named_const(NULL
, 0, "null_context");
1533 if (autofree_context
!= NULL
) {
1534 talloc_reparent(NULL
, null_context
, autofree_context
);
1540 enable tracking of the NULL context, not moving the autofree context
1541 into the NULL context. This is needed for the talloc testsuite
1543 _PUBLIC_
void talloc_enable_null_tracking_no_autofree(void)
1545 if (null_context
== NULL
) {
1546 null_context
= _talloc_named_const(NULL
, 0, "null_context");
1551 disable tracking of the NULL context
1553 _PUBLIC_
void talloc_disable_null_tracking(void)
1555 if (null_context
!= NULL
) {
1556 /* we have to move any children onto the real NULL
1558 struct talloc_chunk
*tc
, *tc2
;
1559 tc
= talloc_chunk_from_ptr(null_context
);
1560 for (tc2
= tc
->child
; tc2
; tc2
=tc2
->next
) {
1561 if (tc2
->parent
== tc
) tc2
->parent
= NULL
;
1562 if (tc2
->prev
== tc
) tc2
->prev
= NULL
;
1564 for (tc2
= tc
->next
; tc2
; tc2
=tc2
->next
) {
1565 if (tc2
->parent
== tc
) tc2
->parent
= NULL
;
1566 if (tc2
->prev
== tc
) tc2
->prev
= NULL
;
1571 talloc_free(null_context
);
1572 null_context
= NULL
;
1576 enable leak reporting on exit
1578 _PUBLIC_
void talloc_enable_leak_report(void)
1580 talloc_enable_null_tracking();
1581 atexit(talloc_report_null
);
1585 enable full leak reporting on exit
1587 _PUBLIC_
void talloc_enable_leak_report_full(void)
1589 talloc_enable_null_tracking();
1590 atexit(talloc_report_null_full
);
1594 talloc and zero memory.
1596 _PUBLIC_
void *_talloc_zero(const void *ctx
, size_t size
, const char *name
)
1598 void *p
= _talloc_named_const(ctx
, size
, name
);
1601 memset(p
, '\0', size
);
1608 memdup with a talloc.
1610 _PUBLIC_
void *_talloc_memdup(const void *t
, const void *p
, size_t size
, const char *name
)
1612 void *newp
= _talloc_named_const(t
, size
, name
);
1615 memcpy(newp
, p
, size
);
1621 static inline char *__talloc_strlendup(const void *t
, const char *p
, size_t len
)
1625 ret
= (char *)__talloc(t
, len
+ 1);
1626 if (unlikely(!ret
)) return NULL
;
1628 memcpy(ret
, p
, len
);
1631 _talloc_set_name_const(ret
, ret
);
1636 strdup with a talloc
1638 _PUBLIC_
char *talloc_strdup(const void *t
, const char *p
)
1640 if (unlikely(!p
)) return NULL
;
1641 return __talloc_strlendup(t
, p
, strlen(p
));
1645 strndup with a talloc
1647 _PUBLIC_
char *talloc_strndup(const void *t
, const char *p
, size_t n
)
1649 if (unlikely(!p
)) return NULL
;
1650 return __talloc_strlendup(t
, p
, strnlen(p
, n
));
1653 static inline char *__talloc_strlendup_append(char *s
, size_t slen
,
1654 const char *a
, size_t alen
)
1658 ret
= talloc_realloc(NULL
, s
, char, slen
+ alen
+ 1);
1659 if (unlikely(!ret
)) return NULL
;
1661 /* append the string and the trailing \0 */
1662 memcpy(&ret
[slen
], a
, alen
);
1665 _talloc_set_name_const(ret
, ret
);
1670 * Appends at the end of the string.
1672 _PUBLIC_
char *talloc_strdup_append(char *s
, const char *a
)
1675 return talloc_strdup(NULL
, a
);
1682 return __talloc_strlendup_append(s
, strlen(s
), a
, strlen(a
));
1686 * Appends at the end of the talloc'ed buffer,
1687 * not the end of the string.
1689 _PUBLIC_
char *talloc_strdup_append_buffer(char *s
, const char *a
)
1694 return talloc_strdup(NULL
, a
);
1701 slen
= talloc_get_size(s
);
1702 if (likely(slen
> 0)) {
1706 return __talloc_strlendup_append(s
, slen
, a
, strlen(a
));
1710 * Appends at the end of the string.
1712 _PUBLIC_
char *talloc_strndup_append(char *s
, const char *a
, size_t n
)
1715 return talloc_strdup(NULL
, a
);
1722 return __talloc_strlendup_append(s
, strlen(s
), a
, strnlen(a
, n
));
1726 * Appends at the end of the talloc'ed buffer,
1727 * not the end of the string.
1729 _PUBLIC_
char *talloc_strndup_append_buffer(char *s
, const char *a
, size_t n
)
1734 return talloc_strdup(NULL
, a
);
1741 slen
= talloc_get_size(s
);
1742 if (likely(slen
> 0)) {
1746 return __talloc_strlendup_append(s
, slen
, a
, strnlen(a
, n
));
1749 #ifndef HAVE_VA_COPY
1750 #ifdef HAVE___VA_COPY
1751 #define va_copy(dest, src) __va_copy(dest, src)
1753 #define va_copy(dest, src) (dest) = (src)
1757 _PUBLIC_
char *talloc_vasprintf(const void *t
, const char *fmt
, va_list ap
)
1764 /* this call looks strange, but it makes it work on older solaris boxes */
1766 len
= vsnprintf(&c
, 1, fmt
, ap2
);
1768 if (unlikely(len
< 0)) {
1772 ret
= (char *)__talloc(t
, len
+1);
1773 if (unlikely(!ret
)) return NULL
;
1776 vsnprintf(ret
, len
+1, fmt
, ap2
);
1779 _talloc_set_name_const(ret
, ret
);
1785 Perform string formatting, and return a pointer to newly allocated
1786 memory holding the result, inside a memory pool.
1788 _PUBLIC_
char *talloc_asprintf(const void *t
, const char *fmt
, ...)
1794 ret
= talloc_vasprintf(t
, fmt
, ap
);
1799 static inline char *__talloc_vaslenprintf_append(char *s
, size_t slen
,
1800 const char *fmt
, va_list ap
)
1801 PRINTF_ATTRIBUTE(3,0);
1803 static inline char *__talloc_vaslenprintf_append(char *s
, size_t slen
,
1804 const char *fmt
, va_list ap
)
1811 alen
= vsnprintf(&c
, 1, fmt
, ap2
);
1815 /* Either the vsnprintf failed or the format resulted in
1816 * no characters being formatted. In the former case, we
1817 * ought to return NULL, in the latter we ought to return
1818 * the original string. Most current callers of this
1819 * function expect it to never return NULL.
1824 s
= talloc_realloc(NULL
, s
, char, slen
+ alen
+ 1);
1825 if (!s
) return NULL
;
1828 vsnprintf(s
+ slen
, alen
+ 1, fmt
, ap2
);
1831 _talloc_set_name_const(s
, s
);
1836 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1837 * and return @p s, which may have moved. Good for gradually
1838 * accumulating output into a string buffer. Appends at the end
1841 _PUBLIC_
char *talloc_vasprintf_append(char *s
, const char *fmt
, va_list ap
)
1844 return talloc_vasprintf(NULL
, fmt
, ap
);
1847 return __talloc_vaslenprintf_append(s
, strlen(s
), fmt
, ap
);
1851 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1852 * and return @p s, which may have moved. Always appends at the
1853 * end of the talloc'ed buffer, not the end of the string.
1855 _PUBLIC_
char *talloc_vasprintf_append_buffer(char *s
, const char *fmt
, va_list ap
)
1860 return talloc_vasprintf(NULL
, fmt
, ap
);
1863 slen
= talloc_get_size(s
);
1864 if (likely(slen
> 0)) {
1868 return __talloc_vaslenprintf_append(s
, slen
, fmt
, ap
);
1872 Realloc @p s to append the formatted result of @p fmt and return @p
1873 s, which may have moved. Good for gradually accumulating output
1874 into a string buffer.
1876 _PUBLIC_
char *talloc_asprintf_append(char *s
, const char *fmt
, ...)
1881 s
= talloc_vasprintf_append(s
, fmt
, ap
);
1887 Realloc @p s to append the formatted result of @p fmt and return @p
1888 s, which may have moved. Good for gradually accumulating output
1891 _PUBLIC_
char *talloc_asprintf_append_buffer(char *s
, const char *fmt
, ...)
1896 s
= talloc_vasprintf_append_buffer(s
, fmt
, ap
);
1902 alloc an array, checking for integer overflow in the array size
1904 _PUBLIC_
void *_talloc_array(const void *ctx
, size_t el_size
, unsigned count
, const char *name
)
1906 if (count
>= MAX_TALLOC_SIZE
/el_size
) {
1909 return _talloc_named_const(ctx
, el_size
* count
, name
);
1913 alloc an zero array, checking for integer overflow in the array size
1915 _PUBLIC_
void *_talloc_zero_array(const void *ctx
, size_t el_size
, unsigned count
, const char *name
)
1917 if (count
>= MAX_TALLOC_SIZE
/el_size
) {
1920 return _talloc_zero(ctx
, el_size
* count
, name
);
1924 realloc an array, checking for integer overflow in the array size
1926 _PUBLIC_
void *_talloc_realloc_array(const void *ctx
, void *ptr
, size_t el_size
, unsigned count
, const char *name
)
1928 if (count
>= MAX_TALLOC_SIZE
/el_size
) {
1931 return _talloc_realloc(ctx
, ptr
, el_size
* count
, name
);
1935 a function version of talloc_realloc(), so it can be passed as a function pointer
1936 to libraries that want a realloc function (a realloc function encapsulates
1937 all the basic capabilities of an allocation library, which is why this is useful)
1939 _PUBLIC_
void *talloc_realloc_fn(const void *context
, void *ptr
, size_t size
)
1941 return _talloc_realloc(context
, ptr
, size
, NULL
);
1945 static int talloc_autofree_destructor(void *ptr
)
1947 autofree_context
= NULL
;
1951 static void talloc_autofree(void)
1953 talloc_free(autofree_context
);
1957 return a context which will be auto-freed on exit
1958 this is useful for reducing the noise in leak reports
1960 _PUBLIC_
void *talloc_autofree_context(void)
1962 if (autofree_context
== NULL
) {
1963 autofree_context
= _talloc_named_const(NULL
, 0, "autofree_context");
1964 talloc_set_destructor(autofree_context
, talloc_autofree_destructor
);
1965 atexit(talloc_autofree
);
1967 return autofree_context
;
1970 _PUBLIC_
size_t talloc_get_size(const void *context
)
1972 struct talloc_chunk
*tc
;
1974 if (context
== NULL
) {
1975 context
= null_context
;
1977 if (context
== NULL
) {
1981 tc
= talloc_chunk_from_ptr(context
);
1987 find a parent of this context that has the given name, if any
1989 _PUBLIC_
void *talloc_find_parent_byname(const void *context
, const char *name
)
1991 struct talloc_chunk
*tc
;
1993 if (context
== NULL
) {
1997 tc
= talloc_chunk_from_ptr(context
);
1999 if (tc
->name
&& strcmp(tc
->name
, name
) == 0) {
2000 return TC_PTR_FROM_CHUNK(tc
);
2002 while (tc
&& tc
->prev
) tc
= tc
->prev
;
2011 show the parentage of a context
2013 _PUBLIC_
void talloc_show_parents(const void *context
, FILE *file
)
2015 struct talloc_chunk
*tc
;
2017 if (context
== NULL
) {
2018 fprintf(file
, "talloc no parents for NULL\n");
2022 tc
= talloc_chunk_from_ptr(context
);
2023 fprintf(file
, "talloc parents of '%s'\n", talloc_get_name(context
));
2025 fprintf(file
, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc
)));
2026 while (tc
&& tc
->prev
) tc
= tc
->prev
;
2035 return 1 if ptr is a parent of context
2037 static int _talloc_is_parent(const void *context
, const void *ptr
, int depth
)
2039 struct talloc_chunk
*tc
;
2041 if (context
== NULL
) {
2045 tc
= talloc_chunk_from_ptr(context
);
2046 while (tc
&& depth
> 0) {
2047 if (TC_PTR_FROM_CHUNK(tc
) == ptr
) return 1;
2048 while (tc
&& tc
->prev
) tc
= tc
->prev
;
2058 return 1 if ptr is a parent of context
2060 _PUBLIC_
int talloc_is_parent(const void *context
, const void *ptr
)
2062 return _talloc_is_parent(context
, ptr
, TALLOC_MAX_DEPTH
);