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_V1 0xe814ec70
61 #define TALLOC_MAGIC_V2 0xe814ec80
62 #define TALLOC_MAGIC TALLOC_MAGIC_V2
63 #define TALLOC_FLAG_FREE 0x01
64 #define TALLOC_FLAG_LOOP 0x02
65 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
66 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
67 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
69 /* by default we abort when given a bad pointer (such as when talloc_free() is called
70 on a pointer that came from malloc() */
72 #define TALLOC_ABORT(reason) abort()
75 #ifndef discard_const_p
76 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
77 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
79 # define discard_const_p(type, ptr) ((type *)(ptr))
83 /* these macros gain us a few percent of speed on gcc */
85 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
86 as its first argument */
88 #define likely(x) __builtin_expect(!!(x), 1)
91 #define unlikely(x) __builtin_expect(!!(x), 0)
98 #define unlikely(x) (x)
102 /* this null_context is only used if talloc_enable_leak_report() or
103 talloc_enable_leak_report_full() is called, otherwise it remains
106 static void *null_context
;
107 static void *autofree_context
;
109 struct talloc_reference_handle
{
110 struct talloc_reference_handle
*next
, *prev
;
112 const char *location
;
115 typedef int (*talloc_destructor_t
)(void *);
117 struct talloc_chunk
{
118 struct talloc_chunk
*next
, *prev
;
119 struct talloc_chunk
*parent
, *child
;
120 struct talloc_reference_handle
*refs
;
121 talloc_destructor_t destructor
;
127 * "pool" has dual use:
129 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
130 * marks the end of the currently allocated area.
132 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
133 * is a pointer to the struct talloc_chunk of the pool that it was
134 * allocated from. This way children can quickly find the pool to chew
140 /* 16 byte alignment seems to keep everyone happy */
141 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
142 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
144 static void (*talloc_log_fn
)(const char *message
);
146 void talloc_set_log_fn(void (*log_fn
)(const char *message
))
148 talloc_log_fn
= log_fn
;
151 static void talloc_log(const char *fmt
, ...) PRINTF_ATTRIBUTE(1,2);
152 static void talloc_log(const char *fmt
, ...)
157 if (!talloc_log_fn
) {
162 message
= talloc_vasprintf(NULL
, fmt
, ap
);
165 talloc_log_fn(message
);
166 talloc_free(message
);
169 static void talloc_log_stderr(const char *message
)
171 fprintf(stderr
, "%s", message
);
174 void talloc_set_log_stderr(void)
176 talloc_set_log_fn(talloc_log_stderr
);
179 static void (*talloc_abort_fn
)(const char *reason
);
181 void talloc_set_abort_fn(void (*abort_fn
)(const char *reason
))
183 talloc_abort_fn
= abort_fn
;
186 static void talloc_abort(const char *reason
)
188 talloc_log("%s\n", reason
);
190 if (!talloc_abort_fn
) {
191 TALLOC_ABORT(reason
);
194 talloc_abort_fn(reason
);
197 static void talloc_abort_magic_v1(void)
199 talloc_abort("Bad talloc magic value - old magic v1 used");
202 static void talloc_abort_double_free(void)
204 talloc_abort("Bad talloc magic value - double free");
207 static void talloc_abort_unknown_value(void)
209 talloc_abort("Bad talloc magic value - unknown value");
212 /* panic if we get a bad magic value */
213 static inline struct talloc_chunk
*talloc_chunk_from_ptr(const void *ptr
)
215 const char *pp
= (const char *)ptr
;
216 struct talloc_chunk
*tc
= discard_const_p(struct talloc_chunk
, pp
- TC_HDR_SIZE
);
217 if (unlikely((tc
->flags
& (TALLOC_FLAG_FREE
| ~0xF)) != TALLOC_MAGIC
)) {
218 if ((tc
->flags
& (~0xF)) == TALLOC_MAGIC_V1
) {
219 talloc_abort_magic_v1();
223 if (tc
->flags
& TALLOC_FLAG_FREE
) {
224 talloc_abort_double_free();
227 talloc_abort_unknown_value();
234 /* hook into the front of the list */
235 #define _TLIST_ADD(list, p) \
239 (p)->next = (p)->prev = NULL; \
241 (list)->prev = (p); \
242 (p)->next = (list); \
248 /* remove an element from a list - element doesn't have to be in list. */
249 #define _TLIST_REMOVE(list, p) \
251 if ((p) == (list)) { \
252 (list) = (p)->next; \
253 if (list) (list)->prev = NULL; \
255 if ((p)->prev) (p)->prev->next = (p)->next; \
256 if ((p)->next) (p)->next->prev = (p)->prev; \
258 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
263 return the parent chunk of a pointer
265 static inline struct talloc_chunk
*talloc_parent_chunk(const void *ptr
)
267 struct talloc_chunk
*tc
;
269 if (unlikely(ptr
== NULL
)) {
273 tc
= talloc_chunk_from_ptr(ptr
);
274 while (tc
->prev
) tc
=tc
->prev
;
279 void *talloc_parent(const void *ptr
)
281 struct talloc_chunk
*tc
= talloc_parent_chunk(ptr
);
282 return tc
? TC_PTR_FROM_CHUNK(tc
) : NULL
;
288 const char *talloc_parent_name(const void *ptr
)
290 struct talloc_chunk
*tc
= talloc_parent_chunk(ptr
);
291 return tc
? tc
->name
: NULL
;
295 A pool carries an in-pool object count count in the first 16 bytes.
296 bytes. This is done to support talloc_steal() to a parent outside of the
297 pool. The count includes the pool itself, so a talloc_free() on a pool will
298 only destroy the pool if the count has dropped to zero. A talloc_free() of a
299 pool member will reduce the count, and eventually also call free(3) on the
302 The object count is not put into "struct talloc_chunk" because it is only
303 relevant for talloc pools and the alignment to 16 bytes would increase the
304 memory footprint of each talloc chunk by those 16 bytes.
307 #define TALLOC_POOL_HDR_SIZE 16
309 static unsigned int *talloc_pool_objectcount(struct talloc_chunk
*tc
)
311 return (unsigned int *)((char *)tc
+ sizeof(struct talloc_chunk
));
318 static struct talloc_chunk
*talloc_alloc_pool(struct talloc_chunk
*parent
,
321 struct talloc_chunk
*pool_ctx
= NULL
;
323 struct talloc_chunk
*result
;
326 if (parent
== NULL
) {
330 if (parent
->flags
& TALLOC_FLAG_POOL
) {
333 else if (parent
->flags
& TALLOC_FLAG_POOLMEM
) {
334 pool_ctx
= (struct talloc_chunk
*)parent
->pool
;
337 if (pool_ctx
== NULL
) {
341 space_left
= ((char *)pool_ctx
+ TC_HDR_SIZE
+ pool_ctx
->size
)
342 - ((char *)pool_ctx
->pool
);
345 * Align size to 16 bytes
347 chunk_size
= ((size
+ 15) & ~15);
349 if (space_left
< chunk_size
) {
353 result
= (struct talloc_chunk
*)pool_ctx
->pool
;
355 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
356 VALGRIND_MAKE_MEM_UNDEFINED(result
, size
);
359 pool_ctx
->pool
= (void *)((char *)result
+ chunk_size
);
361 result
->flags
= TALLOC_MAGIC
| TALLOC_FLAG_POOLMEM
;
362 result
->pool
= pool_ctx
;
364 *talloc_pool_objectcount(pool_ctx
) += 1;
370 Allocate a bit of memory as a child of an existing pointer
372 static inline void *__talloc(const void *context
, size_t size
)
374 struct talloc_chunk
*tc
= NULL
;
376 if (unlikely(context
== NULL
)) {
377 context
= null_context
;
380 if (unlikely(size
>= MAX_TALLOC_SIZE
)) {
384 if (context
!= NULL
) {
385 tc
= talloc_alloc_pool(talloc_chunk_from_ptr(context
),
390 tc
= (struct talloc_chunk
*)malloc(TC_HDR_SIZE
+size
);
391 if (unlikely(tc
== NULL
)) return NULL
;
392 tc
->flags
= TALLOC_MAGIC
;
397 tc
->destructor
= NULL
;
402 if (likely(context
)) {
403 struct talloc_chunk
*parent
= talloc_chunk_from_ptr(context
);
406 parent
->child
->parent
= NULL
;
407 tc
->next
= parent
->child
;
416 tc
->next
= tc
->prev
= tc
->parent
= NULL
;
419 return TC_PTR_FROM_CHUNK(tc
);
423 * Create a talloc pool
426 void *talloc_pool(const void *context
, size_t size
)
428 void *result
= __talloc(context
, size
+ TALLOC_POOL_HDR_SIZE
);
429 struct talloc_chunk
*tc
;
431 if (unlikely(result
== NULL
)) {
435 tc
= talloc_chunk_from_ptr(result
);
437 tc
->flags
|= TALLOC_FLAG_POOL
;
438 tc
->pool
= (char *)result
+ TALLOC_POOL_HDR_SIZE
;
440 *talloc_pool_objectcount(tc
) = 1;
442 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
443 VALGRIND_MAKE_MEM_NOACCESS(tc
->pool
, size
);
450 setup a destructor to be called on free of a pointer
451 the destructor should return 0 on success, or -1 on failure.
452 if the destructor fails then the free is failed, and the memory can
453 be continued to be used
455 void _talloc_set_destructor(const void *ptr
, int (*destructor
)(void *))
457 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
458 tc
->destructor
= destructor
;
462 increase the reference count on a piece of memory.
464 int talloc_increase_ref_count(const void *ptr
)
466 if (unlikely(!talloc_reference(null_context
, ptr
))) {
473 helper for talloc_reference()
475 this is referenced by a function pointer and should not be inline
477 static int talloc_reference_destructor(struct talloc_reference_handle
*handle
)
479 struct talloc_chunk
*ptr_tc
= talloc_chunk_from_ptr(handle
->ptr
);
480 _TLIST_REMOVE(ptr_tc
->refs
, handle
);
485 more efficient way to add a name to a pointer - the name must point to a
488 static inline void _talloc_set_name_const(const void *ptr
, const char *name
)
490 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
495 internal talloc_named_const()
497 static inline void *_talloc_named_const(const void *context
, size_t size
, const char *name
)
501 ptr
= __talloc(context
, size
);
502 if (unlikely(ptr
== NULL
)) {
506 _talloc_set_name_const(ptr
, name
);
512 make a secondary reference to a pointer, hanging off the given context.
513 the pointer remains valid until both the original caller and this given
516 the major use for this is when two different structures need to reference the
517 same underlying data, and you want to be able to free the two instances separately,
520 void *_talloc_reference_loc(const void *context
, const void *ptr
, const char *location
)
522 struct talloc_chunk
*tc
;
523 struct talloc_reference_handle
*handle
;
524 if (unlikely(ptr
== NULL
)) return NULL
;
526 tc
= talloc_chunk_from_ptr(ptr
);
527 handle
= (struct talloc_reference_handle
*)_talloc_named_const(context
,
528 sizeof(struct talloc_reference_handle
),
529 TALLOC_MAGIC_REFERENCE
);
530 if (unlikely(handle
== NULL
)) return NULL
;
532 /* note that we hang the destructor off the handle, not the
533 main context as that allows the caller to still setup their
534 own destructor on the context if they want to */
535 talloc_set_destructor(handle
, talloc_reference_destructor
);
536 handle
->ptr
= discard_const_p(void, ptr
);
537 handle
->location
= location
;
538 _TLIST_ADD(tc
->refs
, handle
);
542 static void *_talloc_steal_internal(const void *new_ctx
, const void *ptr
);
545 internal talloc_free call
547 static inline int _talloc_free_internal(void *ptr
)
549 struct talloc_chunk
*tc
;
551 if (unlikely(ptr
== NULL
)) {
555 tc
= talloc_chunk_from_ptr(ptr
);
557 if (unlikely(tc
->refs
)) {
559 /* check this is a reference from a child or grantchild
560 * back to it's parent or grantparent
562 * in that case we need to remove the reference and
563 * call another instance of talloc_free() on the current
566 is_child
= talloc_is_parent(tc
->refs
, ptr
);
567 _talloc_free_internal(tc
->refs
);
569 return _talloc_free_internal(ptr
);
574 if (unlikely(tc
->flags
& TALLOC_FLAG_LOOP
)) {
575 /* we have a free loop - stop looping */
579 if (unlikely(tc
->destructor
)) {
580 talloc_destructor_t d
= tc
->destructor
;
581 if (d
== (talloc_destructor_t
)-1) {
584 tc
->destructor
= (talloc_destructor_t
)-1;
589 tc
->destructor
= NULL
;
593 _TLIST_REMOVE(tc
->parent
->child
, tc
);
594 if (tc
->parent
->child
) {
595 tc
->parent
->child
->parent
= tc
->parent
;
598 if (tc
->prev
) tc
->prev
->next
= tc
->next
;
599 if (tc
->next
) tc
->next
->prev
= tc
->prev
;
602 tc
->flags
|= TALLOC_FLAG_LOOP
;
605 /* we need to work out who will own an abandoned child
606 if it cannot be freed. In priority order, the first
607 choice is owner of any remaining reference to this
608 pointer, the second choice is our parent, and the
609 final choice is the null context. */
610 void *child
= TC_PTR_FROM_CHUNK(tc
->child
);
611 const void *new_parent
= null_context
;
612 if (unlikely(tc
->child
->refs
)) {
613 struct talloc_chunk
*p
= talloc_parent_chunk(tc
->child
->refs
);
614 if (p
) new_parent
= TC_PTR_FROM_CHUNK(p
);
616 if (unlikely(_talloc_free_internal(child
) == -1)) {
617 if (new_parent
== null_context
) {
618 struct talloc_chunk
*p
= talloc_parent_chunk(ptr
);
619 if (p
) new_parent
= TC_PTR_FROM_CHUNK(p
);
621 _talloc_steal_internal(new_parent
, child
);
625 tc
->flags
|= TALLOC_FLAG_FREE
;
627 if (tc
->flags
& (TALLOC_FLAG_POOL
|TALLOC_FLAG_POOLMEM
)) {
628 struct talloc_chunk
*pool
;
629 unsigned int *pool_object_count
;
631 pool
= (tc
->flags
& TALLOC_FLAG_POOL
)
632 ? tc
: (struct talloc_chunk
*)tc
->pool
;
634 pool_object_count
= talloc_pool_objectcount(pool
);
636 if (*pool_object_count
== 0) {
637 talloc_abort("Pool object count zero!");
641 *pool_object_count
-= 1;
643 if (*pool_object_count
== 0) {
654 move a lump of memory from one talloc context to another return the
655 ptr on success, or NULL if it could not be transferred.
656 passing NULL as ptr will always return NULL with no side effects.
658 static void *_talloc_steal_internal(const void *new_ctx
, const void *ptr
)
660 struct talloc_chunk
*tc
, *new_tc
;
662 if (unlikely(!ptr
)) {
666 if (unlikely(new_ctx
== NULL
)) {
667 new_ctx
= null_context
;
670 tc
= talloc_chunk_from_ptr(ptr
);
672 if (unlikely(new_ctx
== NULL
)) {
674 _TLIST_REMOVE(tc
->parent
->child
, tc
);
675 if (tc
->parent
->child
) {
676 tc
->parent
->child
->parent
= tc
->parent
;
679 if (tc
->prev
) tc
->prev
->next
= tc
->next
;
680 if (tc
->next
) tc
->next
->prev
= tc
->prev
;
683 tc
->parent
= tc
->next
= tc
->prev
= NULL
;
684 return discard_const_p(void, ptr
);
687 new_tc
= talloc_chunk_from_ptr(new_ctx
);
689 if (unlikely(tc
== new_tc
|| tc
->parent
== new_tc
)) {
690 return discard_const_p(void, ptr
);
694 _TLIST_REMOVE(tc
->parent
->child
, tc
);
695 if (tc
->parent
->child
) {
696 tc
->parent
->child
->parent
= tc
->parent
;
699 if (tc
->prev
) tc
->prev
->next
= tc
->next
;
700 if (tc
->next
) tc
->next
->prev
= tc
->prev
;
704 if (new_tc
->child
) new_tc
->child
->parent
= NULL
;
705 _TLIST_ADD(new_tc
->child
, tc
);
707 return discard_const_p(void, ptr
);
711 move a lump of memory from one talloc context to another return the
712 ptr on success, or NULL if it could not be transferred.
713 passing NULL as ptr will always return NULL with no side effects.
715 void *_talloc_steal_loc(const void *new_ctx
, const void *ptr
, const char *location
)
717 struct talloc_chunk
*tc
;
719 if (unlikely(ptr
== NULL
)) {
723 tc
= talloc_chunk_from_ptr(ptr
);
725 if (unlikely(tc
->refs
!= NULL
) && talloc_parent(ptr
) != new_ctx
) {
726 struct talloc_reference_handle
*h
;
728 talloc_log("WARNING: talloc_steal with references at %s\n",
731 for (h
=tc
->refs
; h
; h
=h
->next
) {
732 talloc_log("\treference at %s\n",
737 return _talloc_steal_internal(new_ctx
, ptr
);
741 this is like a talloc_steal(), but you must supply the old
742 parent. This resolves the ambiguity in a talloc_steal() which is
743 called on a context that has more than one parent (via references)
745 The old parent can be either a reference or a parent
747 void *talloc_reparent(const void *old_parent
, const void *new_parent
, const void *ptr
)
749 struct talloc_chunk
*tc
;
750 struct talloc_reference_handle
*h
;
752 if (unlikely(ptr
== NULL
)) {
756 if (old_parent
== talloc_parent(ptr
)) {
757 return _talloc_steal_internal(new_parent
, ptr
);
760 tc
= talloc_chunk_from_ptr(ptr
);
761 for (h
=tc
->refs
;h
;h
=h
->next
) {
762 if (talloc_parent(h
) == old_parent
) {
763 if (_talloc_steal_internal(new_parent
, h
) != h
) {
770 /* it wasn't a parent */
775 remove a secondary reference to a pointer. This undo's what
776 talloc_reference() has done. The context and pointer arguments
777 must match those given to a talloc_reference()
779 static inline int talloc_unreference(const void *context
, const void *ptr
)
781 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
782 struct talloc_reference_handle
*h
;
784 if (unlikely(context
== NULL
)) {
785 context
= null_context
;
788 for (h
=tc
->refs
;h
;h
=h
->next
) {
789 struct talloc_chunk
*p
= talloc_parent_chunk(h
);
791 if (context
== NULL
) break;
792 } else if (TC_PTR_FROM_CHUNK(p
) == context
) {
800 return _talloc_free_internal(h
);
804 remove a specific parent context from a pointer. This is a more
805 controlled varient of talloc_free()
807 int talloc_unlink(const void *context
, void *ptr
)
809 struct talloc_chunk
*tc_p
, *new_p
;
816 if (context
== NULL
) {
817 context
= null_context
;
820 if (talloc_unreference(context
, ptr
) == 0) {
824 if (context
== NULL
) {
825 if (talloc_parent_chunk(ptr
) != NULL
) {
829 if (talloc_chunk_from_ptr(context
) != talloc_parent_chunk(ptr
)) {
834 tc_p
= talloc_chunk_from_ptr(ptr
);
836 if (tc_p
->refs
== NULL
) {
837 return _talloc_free_internal(ptr
);
840 new_p
= talloc_parent_chunk(tc_p
->refs
);
842 new_parent
= TC_PTR_FROM_CHUNK(new_p
);
847 if (talloc_unreference(new_parent
, ptr
) != 0) {
851 _talloc_steal_internal(new_parent
, ptr
);
857 add a name to an existing pointer - va_list version
859 static inline const char *talloc_set_name_v(const void *ptr
, const char *fmt
, va_list ap
) PRINTF_ATTRIBUTE(2,0);
861 static inline const char *talloc_set_name_v(const void *ptr
, const char *fmt
, va_list ap
)
863 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
864 tc
->name
= talloc_vasprintf(ptr
, fmt
, ap
);
865 if (likely(tc
->name
)) {
866 _talloc_set_name_const(tc
->name
, ".name");
872 add a name to an existing pointer
874 const char *talloc_set_name(const void *ptr
, const char *fmt
, ...)
879 name
= talloc_set_name_v(ptr
, fmt
, ap
);
886 create a named talloc pointer. Any talloc pointer can be named, and
887 talloc_named() operates just like talloc() except that it allows you
890 void *talloc_named(const void *context
, size_t size
, const char *fmt
, ...)
896 ptr
= __talloc(context
, size
);
897 if (unlikely(ptr
== NULL
)) return NULL
;
900 name
= talloc_set_name_v(ptr
, fmt
, ap
);
903 if (unlikely(name
== NULL
)) {
904 _talloc_free_internal(ptr
);
912 return the name of a talloc ptr, or "UNNAMED"
914 const char *talloc_get_name(const void *ptr
)
916 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
917 if (unlikely(tc
->name
== TALLOC_MAGIC_REFERENCE
)) {
920 if (likely(tc
->name
)) {
928 check if a pointer has the given name. If it does, return the pointer,
929 otherwise return NULL
931 void *talloc_check_name(const void *ptr
, const char *name
)
934 if (unlikely(ptr
== NULL
)) return NULL
;
935 pname
= talloc_get_name(ptr
);
936 if (likely(pname
== name
|| strcmp(pname
, name
) == 0)) {
937 return discard_const_p(void, ptr
);
942 static void talloc_abort_type_missmatch(const char *location
,
944 const char *expected
)
948 reason
= talloc_asprintf(NULL
,
949 "%s: Type mismatch: name[%s] expected[%s]",
954 reason
= "Type mismatch";
957 talloc_abort(reason
);
960 void *_talloc_get_type_abort(const void *ptr
, const char *name
, const char *location
)
964 if (unlikely(ptr
== NULL
)) {
965 talloc_abort_type_missmatch(location
, NULL
, name
);
969 pname
= talloc_get_name(ptr
);
970 if (likely(pname
== name
|| strcmp(pname
, name
) == 0)) {
971 return discard_const_p(void, ptr
);
974 talloc_abort_type_missmatch(location
, pname
, name
);
979 this is for compatibility with older versions of talloc
981 void *talloc_init(const char *fmt
, ...)
988 * samba3 expects talloc_report_depth_cb(NULL, ...)
989 * reports all talloc'ed memory, so we need to enable
992 talloc_enable_null_tracking();
994 ptr
= __talloc(NULL
, 0);
995 if (unlikely(ptr
== NULL
)) return NULL
;
998 name
= talloc_set_name_v(ptr
, fmt
, ap
);
1001 if (unlikely(name
== NULL
)) {
1002 _talloc_free_internal(ptr
);
1010 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1011 should probably not be used in new code. It's in here to keep the talloc
1012 code consistent across Samba 3 and 4.
1014 void talloc_free_children(void *ptr
)
1016 struct talloc_chunk
*tc
;
1018 if (unlikely(ptr
== NULL
)) {
1022 tc
= talloc_chunk_from_ptr(ptr
);
1025 /* we need to work out who will own an abandoned child
1026 if it cannot be freed. In priority order, the first
1027 choice is owner of any remaining reference to this
1028 pointer, the second choice is our parent, and the
1029 final choice is the null context. */
1030 void *child
= TC_PTR_FROM_CHUNK(tc
->child
);
1031 const void *new_parent
= null_context
;
1032 if (unlikely(tc
->child
->refs
)) {
1033 struct talloc_chunk
*p
= talloc_parent_chunk(tc
->child
->refs
);
1034 if (p
) new_parent
= TC_PTR_FROM_CHUNK(p
);
1036 if (unlikely(talloc_free(child
) == -1)) {
1037 if (new_parent
== null_context
) {
1038 struct talloc_chunk
*p
= talloc_parent_chunk(ptr
);
1039 if (p
) new_parent
= TC_PTR_FROM_CHUNK(p
);
1041 _talloc_steal_internal(new_parent
, child
);
1045 if ((tc
->flags
& TALLOC_FLAG_POOL
)
1046 && (*talloc_pool_objectcount(tc
) == 1)) {
1047 tc
->pool
= ((char *)tc
+ TC_HDR_SIZE
+ TALLOC_POOL_HDR_SIZE
);
1048 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
1049 VALGRIND_MAKE_MEM_NOACCESS(
1050 tc
->pool
, tc
->size
- TALLOC_POOL_HDR_SIZE
);
1056 Allocate a bit of memory as a child of an existing pointer
1058 void *_talloc(const void *context
, size_t size
)
1060 return __talloc(context
, size
);
1064 externally callable talloc_set_name_const()
1066 void talloc_set_name_const(const void *ptr
, const char *name
)
1068 _talloc_set_name_const(ptr
, name
);
1072 create a named talloc pointer. Any talloc pointer can be named, and
1073 talloc_named() operates just like talloc() except that it allows you
1074 to name the pointer.
1076 void *talloc_named_const(const void *context
, size_t size
, const char *name
)
1078 return _talloc_named_const(context
, size
, name
);
1082 free a talloc pointer. This also frees all child pointers of this
1085 return 0 if the memory is actually freed, otherwise -1. The memory
1086 will not be freed if the ref_count is > 1 or the destructor (if
1087 any) returns non-zero
1089 int _talloc_free(void *ptr
, const char *location
)
1091 struct talloc_chunk
*tc
;
1093 if (unlikely(ptr
== NULL
)) {
1097 tc
= talloc_chunk_from_ptr(ptr
);
1099 if (unlikely(tc
->refs
!= NULL
)) {
1100 struct talloc_reference_handle
*h
;
1102 talloc_log("ERROR: talloc_free with references at %s\n",
1105 for (h
=tc
->refs
; h
; h
=h
->next
) {
1106 talloc_log("\treference at %s\n",
1112 return _talloc_free_internal(ptr
);
1118 A talloc version of realloc. The context argument is only used if
1121 void *_talloc_realloc(const void *context
, void *ptr
, size_t size
, const char *name
)
1123 struct talloc_chunk
*tc
;
1125 bool malloced
= false;
1127 /* size zero is equivalent to free() */
1128 if (unlikely(size
== 0)) {
1129 talloc_unlink(context
, ptr
);
1133 if (unlikely(size
>= MAX_TALLOC_SIZE
)) {
1137 /* realloc(NULL) is equivalent to malloc() */
1139 return _talloc_named_const(context
, size
, name
);
1142 tc
= talloc_chunk_from_ptr(ptr
);
1144 /* don't allow realloc on referenced pointers */
1145 if (unlikely(tc
->refs
)) {
1149 /* don't let anybody try to realloc a talloc_pool */
1150 if (unlikely(tc
->flags
& TALLOC_FLAG_POOL
)) {
1154 /* don't shrink if we have less than 1k to gain */
1155 if ((size
< tc
->size
) && ((tc
->size
- size
) < 1024)) {
1160 /* by resetting magic we catch users of the old memory */
1161 tc
->flags
|= TALLOC_FLAG_FREE
;
1164 new_ptr
= malloc(size
+ TC_HDR_SIZE
);
1166 memcpy(new_ptr
, tc
, tc
->size
+ TC_HDR_SIZE
);
1170 if (tc
->flags
& TALLOC_FLAG_POOLMEM
) {
1172 new_ptr
= talloc_alloc_pool(tc
, size
+ TC_HDR_SIZE
);
1173 *talloc_pool_objectcount((struct talloc_chunk
*)
1176 if (new_ptr
== NULL
) {
1177 new_ptr
= malloc(TC_HDR_SIZE
+size
);
1182 memcpy(new_ptr
, tc
, MIN(tc
->size
,size
) + TC_HDR_SIZE
);
1186 new_ptr
= realloc(tc
, size
+ TC_HDR_SIZE
);
1189 if (unlikely(!new_ptr
)) {
1190 tc
->flags
&= ~TALLOC_FLAG_FREE
;
1194 tc
= (struct talloc_chunk
*)new_ptr
;
1195 tc
->flags
&= ~TALLOC_FLAG_FREE
;
1197 tc
->flags
&= ~TALLOC_FLAG_POOLMEM
;
1200 tc
->parent
->child
= tc
;
1203 tc
->child
->parent
= tc
;
1207 tc
->prev
->next
= tc
;
1210 tc
->next
->prev
= tc
;
1214 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc
), name
);
1216 return TC_PTR_FROM_CHUNK(tc
);
1220 a wrapper around talloc_steal() for situations where you are moving a pointer
1221 between two structures, and want the old pointer to be set to NULL
1223 void *_talloc_move(const void *new_ctx
, const void *_pptr
)
1225 const void **pptr
= discard_const_p(const void *,_pptr
);
1226 void *ret
= talloc_steal(new_ctx
, (void *)*pptr
);
1232 return the total size of a talloc pool (subtree)
1234 size_t talloc_total_size(const void *ptr
)
1237 struct talloc_chunk
*c
, *tc
;
1246 tc
= talloc_chunk_from_ptr(ptr
);
1248 if (tc
->flags
& TALLOC_FLAG_LOOP
) {
1252 tc
->flags
|= TALLOC_FLAG_LOOP
;
1254 if (likely(tc
->name
!= TALLOC_MAGIC_REFERENCE
)) {
1257 for (c
=tc
->child
;c
;c
=c
->next
) {
1258 total
+= talloc_total_size(TC_PTR_FROM_CHUNK(c
));
1261 tc
->flags
&= ~TALLOC_FLAG_LOOP
;
1267 return the total number of blocks in a talloc pool (subtree)
1269 size_t talloc_total_blocks(const void *ptr
)
1272 struct talloc_chunk
*c
, *tc
;
1281 tc
= talloc_chunk_from_ptr(ptr
);
1283 if (tc
->flags
& TALLOC_FLAG_LOOP
) {
1287 tc
->flags
|= TALLOC_FLAG_LOOP
;
1290 for (c
=tc
->child
;c
;c
=c
->next
) {
1291 total
+= talloc_total_blocks(TC_PTR_FROM_CHUNK(c
));
1294 tc
->flags
&= ~TALLOC_FLAG_LOOP
;
1300 return the number of external references to a pointer
1302 size_t talloc_reference_count(const void *ptr
)
1304 struct talloc_chunk
*tc
= talloc_chunk_from_ptr(ptr
);
1305 struct talloc_reference_handle
*h
;
1308 for (h
=tc
->refs
;h
;h
=h
->next
) {
1315 report on memory usage by all children of a pointer, giving a full tree view
1317 void talloc_report_depth_cb(const void *ptr
, int depth
, int max_depth
,
1318 void (*callback
)(const void *ptr
,
1319 int depth
, int max_depth
,
1321 void *private_data
),
1324 struct talloc_chunk
*c
, *tc
;
1329 if (ptr
== NULL
) return;
1331 tc
= talloc_chunk_from_ptr(ptr
);
1333 if (tc
->flags
& TALLOC_FLAG_LOOP
) {
1337 callback(ptr
, depth
, max_depth
, 0, private_data
);
1339 if (max_depth
>= 0 && depth
>= max_depth
) {
1343 tc
->flags
|= TALLOC_FLAG_LOOP
;
1344 for (c
=tc
->child
;c
;c
=c
->next
) {
1345 if (c
->name
== TALLOC_MAGIC_REFERENCE
) {
1346 struct talloc_reference_handle
*h
= (struct talloc_reference_handle
*)TC_PTR_FROM_CHUNK(c
);
1347 callback(h
->ptr
, depth
+ 1, max_depth
, 1, private_data
);
1349 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c
), depth
+ 1, max_depth
, callback
, private_data
);
1352 tc
->flags
&= ~TALLOC_FLAG_LOOP
;
1355 static void talloc_report_depth_FILE_helper(const void *ptr
, int depth
, int max_depth
, int is_ref
, void *_f
)
1357 const char *name
= talloc_get_name(ptr
);
1358 FILE *f
= (FILE *)_f
;
1361 fprintf(f
, "%*sreference to: %s\n", depth
*4, "", name
);
1366 fprintf(f
,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1367 (max_depth
< 0 ? "full " :""), name
,
1368 (unsigned long)talloc_total_size(ptr
),
1369 (unsigned long)talloc_total_blocks(ptr
));
1373 fprintf(f
, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1376 (unsigned long)talloc_total_size(ptr
),
1377 (unsigned long)talloc_total_blocks(ptr
),
1378 (int)talloc_reference_count(ptr
), ptr
);
1381 fprintf(f
, "content: ");
1382 if (talloc_total_size(ptr
)) {
1383 int tot
= talloc_total_size(ptr
);
1386 for (i
= 0; i
< tot
; i
++) {
1387 if ((((char *)ptr
)[i
] > 31) && (((char *)ptr
)[i
] < 126)) {
1388 fprintf(f
, "%c", ((char *)ptr
)[i
]);
1390 fprintf(f
, "~%02x", ((char *)ptr
)[i
]);
1399 report on memory usage by all children of a pointer, giving a full tree view
1401 void talloc_report_depth_file(const void *ptr
, int depth
, int max_depth
, FILE *f
)
1403 talloc_report_depth_cb(ptr
, depth
, max_depth
, talloc_report_depth_FILE_helper
, f
);
1408 report on memory usage by all children of a pointer, giving a full tree view
1410 void talloc_report_full(const void *ptr
, FILE *f
)
1412 talloc_report_depth_file(ptr
, 0, -1, f
);
1416 report on memory usage by all children of a pointer
1418 void talloc_report(const void *ptr
, FILE *f
)
1420 talloc_report_depth_file(ptr
, 0, 1, f
);
1424 report on any memory hanging off the null context
1426 static void talloc_report_null(void)
1428 if (talloc_total_size(null_context
) != 0) {
1429 talloc_report(null_context
, stderr
);
1434 report on any memory hanging off the null context
1436 static void talloc_report_null_full(void)
1438 if (talloc_total_size(null_context
) != 0) {
1439 talloc_report_full(null_context
, stderr
);
1444 enable tracking of the NULL context
1446 void talloc_enable_null_tracking(void)
1448 if (null_context
== NULL
) {
1449 null_context
= _talloc_named_const(NULL
, 0, "null_context");
1454 disable tracking of the NULL context
1456 void talloc_disable_null_tracking(void)
1458 talloc_free(null_context
);
1459 null_context
= NULL
;
1463 enable leak reporting on exit
1465 void talloc_enable_leak_report(void)
1467 talloc_enable_null_tracking();
1468 atexit(talloc_report_null
);
1472 enable full leak reporting on exit
1474 void talloc_enable_leak_report_full(void)
1476 talloc_enable_null_tracking();
1477 atexit(talloc_report_null_full
);
1481 talloc and zero memory.
1483 void *_talloc_zero(const void *ctx
, size_t size
, const char *name
)
1485 void *p
= _talloc_named_const(ctx
, size
, name
);
1488 memset(p
, '\0', size
);
1495 memdup with a talloc.
1497 void *_talloc_memdup(const void *t
, const void *p
, size_t size
, const char *name
)
1499 void *newp
= _talloc_named_const(t
, size
, name
);
1502 memcpy(newp
, p
, size
);
1508 static inline char *__talloc_strlendup(const void *t
, const char *p
, size_t len
)
1512 ret
= (char *)__talloc(t
, len
+ 1);
1513 if (unlikely(!ret
)) return NULL
;
1515 memcpy(ret
, p
, len
);
1518 _talloc_set_name_const(ret
, ret
);
1523 strdup with a talloc
1525 char *talloc_strdup(const void *t
, const char *p
)
1527 if (unlikely(!p
)) return NULL
;
1528 return __talloc_strlendup(t
, p
, strlen(p
));
1532 strndup with a talloc
1534 char *talloc_strndup(const void *t
, const char *p
, size_t n
)
1536 if (unlikely(!p
)) return NULL
;
1537 return __talloc_strlendup(t
, p
, strnlen(p
, n
));
1540 static inline char *__talloc_strlendup_append(char *s
, size_t slen
,
1541 const char *a
, size_t alen
)
1545 ret
= talloc_realloc(NULL
, s
, char, slen
+ alen
+ 1);
1546 if (unlikely(!ret
)) return NULL
;
1548 /* append the string and the trailing \0 */
1549 memcpy(&ret
[slen
], a
, alen
);
1552 _talloc_set_name_const(ret
, ret
);
1557 * Appends at the end of the string.
1559 char *talloc_strdup_append(char *s
, const char *a
)
1562 return talloc_strdup(NULL
, a
);
1569 return __talloc_strlendup_append(s
, strlen(s
), a
, strlen(a
));
1573 * Appends at the end of the talloc'ed buffer,
1574 * not the end of the string.
1576 char *talloc_strdup_append_buffer(char *s
, const char *a
)
1581 return talloc_strdup(NULL
, a
);
1588 slen
= talloc_get_size(s
);
1589 if (likely(slen
> 0)) {
1593 return __talloc_strlendup_append(s
, slen
, a
, strlen(a
));
1597 * Appends at the end of the string.
1599 char *talloc_strndup_append(char *s
, const char *a
, size_t n
)
1602 return talloc_strdup(NULL
, a
);
1609 return __talloc_strlendup_append(s
, strlen(s
), a
, strnlen(a
, n
));
1613 * Appends at the end of the talloc'ed buffer,
1614 * not the end of the string.
1616 char *talloc_strndup_append_buffer(char *s
, const char *a
, size_t n
)
1621 return talloc_strdup(NULL
, a
);
1628 slen
= talloc_get_size(s
);
1629 if (likely(slen
> 0)) {
1633 return __talloc_strlendup_append(s
, slen
, a
, strnlen(a
, n
));
1636 #ifndef HAVE_VA_COPY
1637 #ifdef HAVE___VA_COPY
1638 #define va_copy(dest, src) __va_copy(dest, src)
1640 #define va_copy(dest, src) (dest) = (src)
1644 char *talloc_vasprintf(const void *t
, const char *fmt
, va_list ap
)
1651 /* this call looks strange, but it makes it work on older solaris boxes */
1653 len
= vsnprintf(&c
, 1, fmt
, ap2
);
1655 if (unlikely(len
< 0)) {
1659 ret
= (char *)__talloc(t
, len
+1);
1660 if (unlikely(!ret
)) return NULL
;
1663 vsnprintf(ret
, len
+1, fmt
, ap2
);
1666 _talloc_set_name_const(ret
, ret
);
1672 Perform string formatting, and return a pointer to newly allocated
1673 memory holding the result, inside a memory pool.
1675 char *talloc_asprintf(const void *t
, const char *fmt
, ...)
1681 ret
= talloc_vasprintf(t
, fmt
, ap
);
1686 static inline char *__talloc_vaslenprintf_append(char *s
, size_t slen
,
1687 const char *fmt
, va_list ap
)
1688 PRINTF_ATTRIBUTE(3,0);
1690 static inline char *__talloc_vaslenprintf_append(char *s
, size_t slen
,
1691 const char *fmt
, va_list ap
)
1698 alen
= vsnprintf(&c
, 1, fmt
, ap2
);
1702 /* Either the vsnprintf failed or the format resulted in
1703 * no characters being formatted. In the former case, we
1704 * ought to return NULL, in the latter we ought to return
1705 * the original string. Most current callers of this
1706 * function expect it to never return NULL.
1711 s
= talloc_realloc(NULL
, s
, char, slen
+ alen
+ 1);
1712 if (!s
) return NULL
;
1715 vsnprintf(s
+ slen
, alen
+ 1, fmt
, ap2
);
1718 _talloc_set_name_const(s
, s
);
1723 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1724 * and return @p s, which may have moved. Good for gradually
1725 * accumulating output into a string buffer. Appends at the end
1728 char *talloc_vasprintf_append(char *s
, const char *fmt
, va_list ap
)
1731 return talloc_vasprintf(NULL
, fmt
, ap
);
1734 return __talloc_vaslenprintf_append(s
, strlen(s
), fmt
, ap
);
1738 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1739 * and return @p s, which may have moved. Always appends at the
1740 * end of the talloc'ed buffer, not the end of the string.
1742 char *talloc_vasprintf_append_buffer(char *s
, const char *fmt
, va_list ap
)
1747 return talloc_vasprintf(NULL
, fmt
, ap
);
1750 slen
= talloc_get_size(s
);
1751 if (likely(slen
> 0)) {
1755 return __talloc_vaslenprintf_append(s
, slen
, fmt
, ap
);
1759 Realloc @p s to append the formatted result of @p fmt and return @p
1760 s, which may have moved. Good for gradually accumulating output
1761 into a string buffer.
1763 char *talloc_asprintf_append(char *s
, const char *fmt
, ...)
1768 s
= talloc_vasprintf_append(s
, fmt
, ap
);
1774 Realloc @p s to append the formatted result of @p fmt and return @p
1775 s, which may have moved. Good for gradually accumulating output
1778 char *talloc_asprintf_append_buffer(char *s
, const char *fmt
, ...)
1783 s
= talloc_vasprintf_append_buffer(s
, fmt
, ap
);
1789 alloc an array, checking for integer overflow in the array size
1791 void *_talloc_array(const void *ctx
, size_t el_size
, unsigned count
, const char *name
)
1793 if (count
>= MAX_TALLOC_SIZE
/el_size
) {
1796 return _talloc_named_const(ctx
, el_size
* count
, name
);
1800 alloc an zero array, checking for integer overflow in the array size
1802 void *_talloc_zero_array(const void *ctx
, size_t el_size
, unsigned count
, const char *name
)
1804 if (count
>= MAX_TALLOC_SIZE
/el_size
) {
1807 return _talloc_zero(ctx
, el_size
* count
, name
);
1811 realloc an array, checking for integer overflow in the array size
1813 void *_talloc_realloc_array(const void *ctx
, void *ptr
, size_t el_size
, unsigned count
, const char *name
)
1815 if (count
>= MAX_TALLOC_SIZE
/el_size
) {
1818 return _talloc_realloc(ctx
, ptr
, el_size
* count
, name
);
1822 a function version of talloc_realloc(), so it can be passed as a function pointer
1823 to libraries that want a realloc function (a realloc function encapsulates
1824 all the basic capabilities of an allocation library, which is why this is useful)
1826 void *talloc_realloc_fn(const void *context
, void *ptr
, size_t size
)
1828 return _talloc_realloc(context
, ptr
, size
, NULL
);
1832 static int talloc_autofree_destructor(void *ptr
)
1834 autofree_context
= NULL
;
1838 static void talloc_autofree(void)
1840 talloc_free(autofree_context
);
1844 return a context which will be auto-freed on exit
1845 this is useful for reducing the noise in leak reports
1847 void *talloc_autofree_context(void)
1849 if (autofree_context
== NULL
) {
1850 autofree_context
= _talloc_named_const(NULL
, 0, "autofree_context");
1851 talloc_set_destructor(autofree_context
, talloc_autofree_destructor
);
1852 atexit(talloc_autofree
);
1854 return autofree_context
;
1857 size_t talloc_get_size(const void *context
)
1859 struct talloc_chunk
*tc
;
1861 if (context
== NULL
) {
1862 context
= null_context
;
1864 if (context
== NULL
) {
1868 tc
= talloc_chunk_from_ptr(context
);
1874 find a parent of this context that has the given name, if any
1876 void *talloc_find_parent_byname(const void *context
, const char *name
)
1878 struct talloc_chunk
*tc
;
1880 if (context
== NULL
) {
1884 tc
= talloc_chunk_from_ptr(context
);
1886 if (tc
->name
&& strcmp(tc
->name
, name
) == 0) {
1887 return TC_PTR_FROM_CHUNK(tc
);
1889 while (tc
&& tc
->prev
) tc
= tc
->prev
;
1898 show the parentage of a context
1900 void talloc_show_parents(const void *context
, FILE *file
)
1902 struct talloc_chunk
*tc
;
1904 if (context
== NULL
) {
1905 fprintf(file
, "talloc no parents for NULL\n");
1909 tc
= talloc_chunk_from_ptr(context
);
1910 fprintf(file
, "talloc parents of '%s'\n", talloc_get_name(context
));
1912 fprintf(file
, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc
)));
1913 while (tc
&& tc
->prev
) tc
= tc
->prev
;
1922 return 1 if ptr is a parent of context
1924 int talloc_is_parent(const void *context
, const void *ptr
)
1926 struct talloc_chunk
*tc
;
1928 if (context
== NULL
) {
1932 tc
= talloc_chunk_from_ptr(context
);
1934 if (TC_PTR_FROM_CHUNK(tc
) == ptr
) return 1;
1935 while (tc
&& tc
->prev
) tc
= tc
->prev
;
1946 /* ABI compat functions (do NOT append anything beyond thess functions,
1947 * keep them as the last ones in the file) */
1949 static const char *talloc_ABI_compat_location
= "Called from compatibility function";
1951 /* ABI compat function (don't use) */
1952 void *_talloc_reference(const void *context
, const void *ptr
) {
1953 return _talloc_reference_loc(context
, ptr
, talloc_ABI_compat_location
);
1956 /* ABI compat function (don't use) */
1957 void *_talloc_steal(const void *new_ctx
, const void *ptr
)
1959 return _talloc_steal_internal(new_ctx
, ptr
);
1963 int talloc_free(void *ptr
);
1964 int talloc_free(void *ptr
)
1966 return _talloc_free_internal(ptr
);
1969 /* DO NOT APPEND ANYTHING BEYOND THIS POINT */