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 HAVE_SYS_AUXV_H
40 #ifdef TALLOC_BUILD_VERSION_MAJOR
41 #if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)
42 #error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"
46 #ifdef TALLOC_BUILD_VERSION_MINOR
47 #if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)
48 #error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"
52 /* Special macros that are no-ops except when run under Valgrind on
53 * x86. They've moved a little bit from valgrind 1.0.4 to 1.9.4 */
54 #ifdef HAVE_VALGRIND_MEMCHECK_H
55 /* memcheck.h includes valgrind.h */
56 #include <valgrind/memcheck.h>
57 #elif defined(HAVE_VALGRIND_H)
61 /* use this to force every realloc to change the pointer, to stress test
62 code that might not cope */
63 #define ALWAYS_REALLOC 0
66 #define MAX_TALLOC_SIZE 0x10000000
68 #define TALLOC_FLAG_FREE 0x01
69 #define TALLOC_FLAG_LOOP 0x02
70 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
71 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
74 * Bits above this are random, used to make it harder to fake talloc
75 * headers during an attack. Try not to change this without good reason.
77 #define TALLOC_FLAG_MASK 0x0F
79 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
81 #define TALLOC_MAGIC_BASE 0xe814ec70
82 static unsigned int talloc_magic = (
84 (TALLOC_VERSION_MAJOR << 12) +
85 (TALLOC_VERSION_MINOR << 4));
87 /* by default we abort when given a bad pointer (such as when talloc_free() is called
88 on a pointer that came from malloc() */
90 #define TALLOC_ABORT(reason) abort()
93 #ifndef discard_const_p
94 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
95 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
97 # define discard_const_p(type, ptr) ((type *)(ptr))
101 /* these macros gain us a few percent of speed on gcc */
103 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
104 as its first argument */
106 #define likely(x) __builtin_expect(!!(x), 1)
109 #define unlikely(x) __builtin_expect(!!(x), 0)
113 #define likely(x) (x)
116 #define unlikely(x) (x)
120 /* this null_context is only used if talloc_enable_leak_report() or
121 talloc_enable_leak_report_full() is called, otherwise it remains
124 static void *null_context;
125 static void *autofree_context;
127 /* used to enable fill of memory on free, which can be useful for
128 * catching use after free errors when valgrind is too slow
136 #define TALLOC_FILL_ENV "TALLOC_FREE_FILL"
139 * do not wipe the header, to allow the
140 * double-free logic to still work
142 #define TC_INVALIDATE_FULL_FILL_CHUNK(_tc) do { \
143 if (unlikely(talloc_fill.enabled)) { \
144 size_t _flen = (_tc)->size; \
145 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
146 memset(_fptr, talloc_fill.fill_value, _flen); \
150 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
151 /* Mark the whole chunk as not accessable */
152 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { \
153 size_t _flen = TC_HDR_SIZE + (_tc)->size; \
154 char *_fptr = (char *)(_tc); \
155 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
158 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { } while (0)
161 #define TC_INVALIDATE_FULL_CHUNK(_tc) do { \
162 TC_INVALIDATE_FULL_FILL_CHUNK(_tc); \
163 TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc); \
166 #define TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
167 if (unlikely(talloc_fill.enabled)) { \
168 size_t _flen = (_tc)->size - (_new_size); \
169 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
170 _fptr += (_new_size); \
171 memset(_fptr, talloc_fill.fill_value, _flen); \
175 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
176 /* Mark the unused bytes not accessable */
177 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
178 size_t _flen = (_tc)->size - (_new_size); \
179 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
180 _fptr += (_new_size); \
181 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
184 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
187 #define TC_INVALIDATE_SHRINK_CHUNK(_tc, _new_size) do { \
188 TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size); \
189 TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
192 #define TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
193 if (unlikely(talloc_fill.enabled)) { \
194 size_t _flen = (_tc)->size - (_new_size); \
195 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
196 _fptr += (_new_size); \
197 memset(_fptr, talloc_fill.fill_value, _flen); \
201 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
202 /* Mark the unused bytes as undefined */
203 #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
204 size_t _flen = (_tc)->size - (_new_size); \
205 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
206 _fptr += (_new_size); \
207 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
210 #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
213 #define TC_UNDEFINE_SHRINK_CHUNK(_tc, _new_size) do { \
214 TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size); \
215 TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
218 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
219 /* Mark the new bytes as undefined */
220 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { \
221 size_t _old_used = TC_HDR_SIZE + (_tc)->size; \
222 size_t _new_used = TC_HDR_SIZE + (_new_size); \
223 size_t _flen = _new_used - _old_used; \
224 char *_fptr = _old_used + (char *)(_tc); \
225 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
228 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
231 #define TC_UNDEFINE_GROW_CHUNK(_tc, _new_size) do { \
232 TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
235 struct talloc_reference_handle {
236 struct talloc_reference_handle *next, *prev;
238 const char *location;
241 struct talloc_memlimit {
242 struct talloc_chunk *parent;
243 struct talloc_memlimit *upper;
248 static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size);
249 static inline void talloc_memlimit_grow(struct talloc_memlimit *limit,
251 static inline void talloc_memlimit_shrink(struct talloc_memlimit *limit,
253 static inline void talloc_memlimit_update_on_free(struct talloc_chunk *tc);
255 static inline void _talloc_set_name_const(const void *ptr, const char *name);
257 typedef int (*talloc_destructor_t)(void *);
259 struct talloc_pool_hdr;
261 struct talloc_chunk {
263 struct talloc_chunk *next, *prev;
264 struct talloc_chunk *parent, *child;
265 struct talloc_reference_handle *refs;
266 talloc_destructor_t destructor;
272 * if 'limit' is set it means all *new* children of the context will
273 * be limited to a total aggregate size ox max_size for memory
275 * cur_size is used to keep track of the current use
277 struct talloc_memlimit *limit;
280 * For members of a pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
281 * is a pointer to the struct talloc_chunk of the pool that it was
282 * allocated from. This way children can quickly find the pool to chew
285 struct talloc_pool_hdr *pool;
288 /* 16 byte alignment seems to keep everyone happy */
289 #define TC_ALIGN16(s) (((s)+15)&~15)
290 #define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
291 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
293 _PUBLIC_ int talloc_version_major(void)
295 return TALLOC_VERSION_MAJOR;
298 _PUBLIC_ int talloc_version_minor(void)
300 return TALLOC_VERSION_MINOR;
303 static void (*talloc_log_fn)(const char *message);
305 _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
307 talloc_log_fn = log_fn;
310 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
311 void talloc_lib_init(void) __attribute__((constructor));
312 void talloc_lib_init(void)
314 uint32_t random_value;
315 #if defined(HAVE_GETAUXVAL) && defined(AT_RANDOM)
318 * Use the kernel-provided random values used for
319 * ASLR. This won't change per-exec, which is ideal for us
321 p = (uint8_t *) getauxval(AT_RANDOM);
323 memcpy(&random_value, p, sizeof(random_value));
328 * Otherwise, hope the location we are loaded in
329 * memory is randomised by someone else
331 random_value = ((uintptr_t)talloc_lib_init & 0xFFFFFFFF);
333 talloc_magic = random_value & ~TALLOC_FLAG_MASK;
336 #warning "No __attribute__((constructor)) support found on this platform, additional talloc security measures not available"
339 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
340 static void talloc_log(const char *fmt, ...)
345 if (!talloc_log_fn) {
350 message = talloc_vasprintf(NULL, fmt, ap);
353 talloc_log_fn(message);
354 talloc_free(message);
357 static void talloc_log_stderr(const char *message)
359 fprintf(stderr, "%s", message);
362 _PUBLIC_ void talloc_set_log_stderr(void)
364 talloc_set_log_fn(talloc_log_stderr);
367 static void (*talloc_abort_fn)(const char *reason);
369 _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
371 talloc_abort_fn = abort_fn;
374 static void talloc_abort(const char *reason)
376 talloc_log("%s\n", reason);
378 if (!talloc_abort_fn) {
379 TALLOC_ABORT(reason);
382 talloc_abort_fn(reason);
385 static void talloc_abort_magic(unsigned magic)
387 talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
390 static void talloc_abort_access_after_free(void)
392 talloc_abort("Bad talloc magic value - access after free");
395 static void talloc_abort_unknown_value(void)
397 talloc_abort("Bad talloc magic value - unknown value");
400 /* panic if we get a bad magic value */
401 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
403 const char *pp = (const char *)ptr;
404 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
405 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~TALLOC_FLAG_MASK)) != talloc_magic)) {
406 if ((tc->flags & (~0xF)) == talloc_magic) {
407 talloc_abort_magic(tc->flags & (~TALLOC_FLAG_MASK));
411 if (tc->flags & TALLOC_FLAG_FREE) {
412 talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
413 talloc_abort_access_after_free();
416 talloc_abort_unknown_value();
423 /* hook into the front of the list */
424 #define _TLIST_ADD(list, p) \
428 (p)->next = (p)->prev = NULL; \
430 (list)->prev = (p); \
431 (p)->next = (list); \
437 /* remove an element from a list - element doesn't have to be in list. */
438 #define _TLIST_REMOVE(list, p) \
440 if ((p) == (list)) { \
441 (list) = (p)->next; \
442 if (list) (list)->prev = NULL; \
444 if ((p)->prev) (p)->prev->next = (p)->next; \
445 if ((p)->next) (p)->next->prev = (p)->prev; \
447 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
452 return the parent chunk of a pointer
454 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
456 struct talloc_chunk *tc;
458 if (unlikely(ptr == NULL)) {
462 tc = talloc_chunk_from_ptr(ptr);
463 while (tc->prev) tc=tc->prev;
468 _PUBLIC_ void *talloc_parent(const void *ptr)
470 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
471 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
477 _PUBLIC_ const char *talloc_parent_name(const void *ptr)
479 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
480 return tc? tc->name : NULL;
484 A pool carries an in-pool object count count in the first 16 bytes.
485 bytes. This is done to support talloc_steal() to a parent outside of the
486 pool. The count includes the pool itself, so a talloc_free() on a pool will
487 only destroy the pool if the count has dropped to zero. A talloc_free() of a
488 pool member will reduce the count, and eventually also call free(3) on the
491 The object count is not put into "struct talloc_chunk" because it is only
492 relevant for talloc pools and the alignment to 16 bytes would increase the
493 memory footprint of each talloc chunk by those 16 bytes.
496 struct talloc_pool_hdr {
498 unsigned int object_count;
502 #define TP_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_pool_hdr))
504 static inline struct talloc_pool_hdr *talloc_pool_from_chunk(struct talloc_chunk *c)
506 return (struct talloc_pool_hdr *)((char *)c - TP_HDR_SIZE);
509 static inline struct talloc_chunk *talloc_chunk_from_pool(struct talloc_pool_hdr *h)
511 return (struct talloc_chunk *)((char *)h + TP_HDR_SIZE);
514 static inline void *tc_pool_end(struct talloc_pool_hdr *pool_hdr)
516 struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);
517 return (char *)tc + TC_HDR_SIZE + pool_hdr->poolsize;
520 static inline size_t tc_pool_space_left(struct talloc_pool_hdr *pool_hdr)
522 return (char *)tc_pool_end(pool_hdr) - (char *)pool_hdr->end;
525 /* If tc is inside a pool, this gives the next neighbour. */
526 static inline void *tc_next_chunk(struct talloc_chunk *tc)
528 return (char *)tc + TC_ALIGN16(TC_HDR_SIZE + tc->size);
531 static inline void *tc_pool_first_chunk(struct talloc_pool_hdr *pool_hdr)
533 struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);
534 return tc_next_chunk(tc);
537 /* Mark the whole remaining pool as not accessable */
538 static inline void tc_invalidate_pool(struct talloc_pool_hdr *pool_hdr)
540 size_t flen = tc_pool_space_left(pool_hdr);
542 if (unlikely(talloc_fill.enabled)) {
543 memset(pool_hdr->end, talloc_fill.fill_value, flen);
546 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
547 VALGRIND_MAKE_MEM_NOACCESS(pool_hdr->end, flen);
555 static inline struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
556 size_t size, size_t prefix_len)
558 struct talloc_pool_hdr *pool_hdr = NULL;
560 struct talloc_chunk *result;
563 if (parent == NULL) {
567 if (parent->flags & TALLOC_FLAG_POOL) {
568 pool_hdr = talloc_pool_from_chunk(parent);
570 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
571 pool_hdr = parent->pool;
574 if (pool_hdr == NULL) {
578 space_left = tc_pool_space_left(pool_hdr);
581 * Align size to 16 bytes
583 chunk_size = TC_ALIGN16(size + prefix_len);
585 if (space_left < chunk_size) {
589 result = (struct talloc_chunk *)((char *)pool_hdr->end + prefix_len);
591 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
592 VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, chunk_size);
595 pool_hdr->end = (void *)((char *)pool_hdr->end + chunk_size);
597 result->flags = talloc_magic | TALLOC_FLAG_POOLMEM;
598 result->pool = pool_hdr;
600 pool_hdr->object_count++;
606 Allocate a bit of memory as a child of an existing pointer
608 static inline void *__talloc_with_prefix(const void *context, size_t size,
611 struct talloc_chunk *tc = NULL;
612 struct talloc_memlimit *limit = NULL;
613 size_t total_len = TC_HDR_SIZE + size + prefix_len;
615 if (unlikely(context == NULL)) {
616 context = null_context;
619 if (unlikely(size >= MAX_TALLOC_SIZE)) {
623 if (unlikely(total_len < TC_HDR_SIZE)) {
627 if (context != NULL) {
628 struct talloc_chunk *ptc = talloc_chunk_from_ptr(context);
630 if (ptc->limit != NULL) {
634 tc = talloc_alloc_pool(ptc, TC_HDR_SIZE+size, prefix_len);
641 * Only do the memlimit check/update on actual allocation.
643 if (!talloc_memlimit_check(limit, total_len)) {
648 ptr = malloc(total_len);
649 if (unlikely(ptr == NULL)) {
652 tc = (struct talloc_chunk *)(ptr + prefix_len);
653 tc->flags = talloc_magic;
656 talloc_memlimit_grow(limit, total_len);
661 tc->destructor = NULL;
666 if (likely(context)) {
667 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
670 parent->child->parent = NULL;
671 tc->next = parent->child;
680 tc->next = tc->prev = tc->parent = NULL;
683 return TC_PTR_FROM_CHUNK(tc);
686 static inline void *__talloc(const void *context, size_t size)
688 return __talloc_with_prefix(context, size, 0);
692 * Create a talloc pool
695 static inline void *_talloc_pool(const void *context, size_t size)
697 struct talloc_chunk *tc;
698 struct talloc_pool_hdr *pool_hdr;
701 result = __talloc_with_prefix(context, size, TP_HDR_SIZE);
703 if (unlikely(result == NULL)) {
707 tc = talloc_chunk_from_ptr(result);
708 pool_hdr = talloc_pool_from_chunk(tc);
710 tc->flags |= TALLOC_FLAG_POOL;
713 pool_hdr->object_count = 1;
714 pool_hdr->end = result;
715 pool_hdr->poolsize = size;
717 tc_invalidate_pool(pool_hdr);
722 _PUBLIC_ void *talloc_pool(const void *context, size_t size)
724 return _talloc_pool(context, size);
728 * Create a talloc pool correctly sized for a basic size plus
729 * a number of subobjects whose total size is given. Essentially
730 * a custom allocator for talloc to reduce fragmentation.
733 _PUBLIC_ void *_talloc_pooled_object(const void *ctx,
735 const char *type_name,
736 unsigned num_subobjects,
737 size_t total_subobjects_size)
739 size_t poolsize, subobjects_slack, tmp;
740 struct talloc_chunk *tc;
741 struct talloc_pool_hdr *pool_hdr;
744 poolsize = type_size + total_subobjects_size;
746 if ((poolsize < type_size) || (poolsize < total_subobjects_size)) {
750 if (num_subobjects == UINT_MAX) {
753 num_subobjects += 1; /* the object body itself */
756 * Alignment can increase the pool size by at most 15 bytes per object
757 * plus alignment for the object itself
759 subobjects_slack = (TC_HDR_SIZE + TP_HDR_SIZE + 15) * num_subobjects;
760 if (subobjects_slack < num_subobjects) {
764 tmp = poolsize + subobjects_slack;
765 if ((tmp < poolsize) || (tmp < subobjects_slack)) {
770 ret = _talloc_pool(ctx, poolsize);
775 tc = talloc_chunk_from_ptr(ret);
776 tc->size = type_size;
778 pool_hdr = talloc_pool_from_chunk(tc);
780 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
781 VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, type_size);
784 pool_hdr->end = ((char *)pool_hdr->end + TC_ALIGN16(type_size));
786 _talloc_set_name_const(ret, type_name);
794 setup a destructor to be called on free of a pointer
795 the destructor should return 0 on success, or -1 on failure.
796 if the destructor fails then the free is failed, and the memory can
797 be continued to be used
799 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
801 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
802 tc->destructor = destructor;
806 increase the reference count on a piece of memory.
808 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
810 if (unlikely(!talloc_reference(null_context, ptr))) {
817 helper for talloc_reference()
819 this is referenced by a function pointer and should not be inline
821 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
823 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
824 _TLIST_REMOVE(ptr_tc->refs, handle);
829 more efficient way to add a name to a pointer - the name must point to a
832 static inline void _talloc_set_name_const(const void *ptr, const char *name)
834 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
839 internal talloc_named_const()
841 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
845 ptr = __talloc(context, size);
846 if (unlikely(ptr == NULL)) {
850 _talloc_set_name_const(ptr, name);
856 make a secondary reference to a pointer, hanging off the given context.
857 the pointer remains valid until both the original caller and this given
860 the major use for this is when two different structures need to reference the
861 same underlying data, and you want to be able to free the two instances separately,
864 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
866 struct talloc_chunk *tc;
867 struct talloc_reference_handle *handle;
868 if (unlikely(ptr == NULL)) return NULL;
870 tc = talloc_chunk_from_ptr(ptr);
871 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
872 sizeof(struct talloc_reference_handle),
873 TALLOC_MAGIC_REFERENCE);
874 if (unlikely(handle == NULL)) return NULL;
876 /* note that we hang the destructor off the handle, not the
877 main context as that allows the caller to still setup their
878 own destructor on the context if they want to */
879 talloc_set_destructor(handle, talloc_reference_destructor);
880 handle->ptr = discard_const_p(void, ptr);
881 handle->location = location;
882 _TLIST_ADD(tc->refs, handle);
886 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
888 static inline void _talloc_free_poolmem(struct talloc_chunk *tc,
889 const char *location)
891 struct talloc_pool_hdr *pool;
892 struct talloc_chunk *pool_tc;
896 pool_tc = talloc_chunk_from_pool(pool);
897 next_tc = tc_next_chunk(tc);
899 tc->flags |= TALLOC_FLAG_FREE;
901 /* we mark the freed memory with where we called the free
902 * from. This means on a double free error we can report where
903 * the first free came from
907 TC_INVALIDATE_FULL_CHUNK(tc);
909 if (unlikely(pool->object_count == 0)) {
910 talloc_abort("Pool object count zero!");
914 pool->object_count--;
916 if (unlikely(pool->object_count == 1
917 && !(pool_tc->flags & TALLOC_FLAG_FREE))) {
919 * if there is just one object left in the pool
920 * and pool->flags does not have TALLOC_FLAG_FREE,
921 * it means this is the pool itself and
922 * the rest is available for new objects
925 pool->end = tc_pool_first_chunk(pool);
926 tc_invalidate_pool(pool);
930 if (unlikely(pool->object_count == 0)) {
932 * we mark the freed memory with where we called the free
933 * from. This means on a double free error we can report where
934 * the first free came from
936 pool_tc->name = location;
938 if (pool_tc->flags & TALLOC_FLAG_POOLMEM) {
939 _talloc_free_poolmem(pool_tc, location);
942 * The talloc_memlimit_update_on_free()
943 * call takes into account the
944 * prefix TP_HDR_SIZE allocated before
945 * the pool talloc_chunk.
947 talloc_memlimit_update_on_free(pool_tc);
948 TC_INVALIDATE_FULL_CHUNK(pool_tc);
954 if (pool->end == next_tc) {
956 * if pool->pool still points to end of
957 * 'tc' (which is stored in the 'next_tc' variable),
958 * we can reclaim the memory of 'tc'.
965 * Do nothing. The memory is just "wasted", waiting for the pool
966 * itself to be freed.
970 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
972 const char *location);
975 internal talloc_free call
977 static inline int _talloc_free_internal(void *ptr, const char *location)
979 struct talloc_chunk *tc;
982 if (unlikely(ptr == NULL)) {
986 /* possibly initialised the talloc fill value */
987 if (unlikely(!talloc_fill.initialised)) {
988 const char *fill = getenv(TALLOC_FILL_ENV);
990 talloc_fill.enabled = true;
991 talloc_fill.fill_value = strtoul(fill, NULL, 0);
993 talloc_fill.initialised = true;
996 tc = talloc_chunk_from_ptr(ptr);
998 if (unlikely(tc->refs)) {
1000 /* check if this is a reference from a child or
1001 * grandchild back to it's parent or grandparent
1003 * in that case we need to remove the reference and
1004 * call another instance of talloc_free() on the current
1007 is_child = talloc_is_parent(tc->refs, ptr);
1008 _talloc_free_internal(tc->refs, location);
1010 return _talloc_free_internal(ptr, location);
1015 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
1016 /* we have a free loop - stop looping */
1020 if (unlikely(tc->destructor)) {
1021 talloc_destructor_t d = tc->destructor;
1022 if (d == (talloc_destructor_t)-1) {
1025 tc->destructor = (talloc_destructor_t)-1;
1028 * Only replace the destructor pointer if
1029 * calling the destructor didn't modify it.
1031 if (tc->destructor == (talloc_destructor_t)-1) {
1036 tc->destructor = NULL;
1040 _TLIST_REMOVE(tc->parent->child, tc);
1041 if (tc->parent->child) {
1042 tc->parent->child->parent = tc->parent;
1045 if (tc->prev) tc->prev->next = tc->next;
1046 if (tc->next) tc->next->prev = tc->prev;
1047 tc->prev = tc->next = NULL;
1050 tc->flags |= TALLOC_FLAG_LOOP;
1052 _talloc_free_children_internal(tc, ptr, location);
1054 tc->flags |= TALLOC_FLAG_FREE;
1056 /* we mark the freed memory with where we called the free
1057 * from. This means on a double free error we can report where
1058 * the first free came from
1060 tc->name = location;
1062 if (tc->flags & TALLOC_FLAG_POOL) {
1063 struct talloc_pool_hdr *pool;
1065 pool = talloc_pool_from_chunk(tc);
1067 if (unlikely(pool->object_count == 0)) {
1068 talloc_abort("Pool object count zero!");
1072 pool->object_count--;
1074 if (likely(pool->object_count != 0)) {
1079 * With object_count==0, a pool becomes a normal piece of
1080 * memory to free. If it's allocated inside a pool, it needs
1081 * to be freed as poolmem, else it needs to be just freed.
1088 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1089 _talloc_free_poolmem(tc, location);
1093 talloc_memlimit_update_on_free(tc);
1095 TC_INVALIDATE_FULL_CHUNK(tc);
1100 static inline size_t _talloc_total_limit_size(const void *ptr,
1101 struct talloc_memlimit *old_limit,
1102 struct talloc_memlimit *new_limit);
1105 move a lump of memory from one talloc context to another return the
1106 ptr on success, or NULL if it could not be transferred.
1107 passing NULL as ptr will always return NULL with no side effects.
1109 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
1111 struct talloc_chunk *tc, *new_tc;
1112 size_t ctx_size = 0;
1114 if (unlikely(!ptr)) {
1118 if (unlikely(new_ctx == NULL)) {
1119 new_ctx = null_context;
1122 tc = talloc_chunk_from_ptr(ptr);
1124 if (tc->limit != NULL) {
1126 ctx_size = _talloc_total_limit_size(ptr, NULL, NULL);
1128 /* Decrement the memory limit from the source .. */
1129 talloc_memlimit_shrink(tc->limit->upper, ctx_size);
1131 if (tc->limit->parent == tc) {
1132 tc->limit->upper = NULL;
1138 if (unlikely(new_ctx == NULL)) {
1140 _TLIST_REMOVE(tc->parent->child, tc);
1141 if (tc->parent->child) {
1142 tc->parent->child->parent = tc->parent;
1145 if (tc->prev) tc->prev->next = tc->next;
1146 if (tc->next) tc->next->prev = tc->prev;
1149 tc->parent = tc->next = tc->prev = NULL;
1150 return discard_const_p(void, ptr);
1153 new_tc = talloc_chunk_from_ptr(new_ctx);
1155 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
1156 return discard_const_p(void, ptr);
1160 _TLIST_REMOVE(tc->parent->child, tc);
1161 if (tc->parent->child) {
1162 tc->parent->child->parent = tc->parent;
1165 if (tc->prev) tc->prev->next = tc->next;
1166 if (tc->next) tc->next->prev = tc->prev;
1167 tc->prev = tc->next = NULL;
1170 tc->parent = new_tc;
1171 if (new_tc->child) new_tc->child->parent = NULL;
1172 _TLIST_ADD(new_tc->child, tc);
1174 if (tc->limit || new_tc->limit) {
1175 ctx_size = _talloc_total_limit_size(ptr, tc->limit,
1177 /* .. and increment it in the destination. */
1178 if (new_tc->limit) {
1179 talloc_memlimit_grow(new_tc->limit, ctx_size);
1183 return discard_const_p(void, ptr);
1187 move a lump of memory from one talloc context to another return the
1188 ptr on success, or NULL if it could not be transferred.
1189 passing NULL as ptr will always return NULL with no side effects.
1191 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
1193 struct talloc_chunk *tc;
1195 if (unlikely(ptr == NULL)) {
1199 tc = talloc_chunk_from_ptr(ptr);
1201 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
1202 struct talloc_reference_handle *h;
1204 talloc_log("WARNING: talloc_steal with references at %s\n",
1207 for (h=tc->refs; h; h=h->next) {
1208 talloc_log("\treference at %s\n",
1214 /* this test is probably too expensive to have on in the
1215 normal build, but it useful for debugging */
1216 if (talloc_is_parent(new_ctx, ptr)) {
1217 talloc_log("WARNING: stealing into talloc child at %s\n", location);
1221 return _talloc_steal_internal(new_ctx, ptr);
1225 this is like a talloc_steal(), but you must supply the old
1226 parent. This resolves the ambiguity in a talloc_steal() which is
1227 called on a context that has more than one parent (via references)
1229 The old parent can be either a reference or a parent
1231 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
1233 struct talloc_chunk *tc;
1234 struct talloc_reference_handle *h;
1236 if (unlikely(ptr == NULL)) {
1240 if (old_parent == talloc_parent(ptr)) {
1241 return _talloc_steal_internal(new_parent, ptr);
1244 tc = talloc_chunk_from_ptr(ptr);
1245 for (h=tc->refs;h;h=h->next) {
1246 if (talloc_parent(h) == old_parent) {
1247 if (_talloc_steal_internal(new_parent, h) != h) {
1250 return discard_const_p(void, ptr);
1254 /* it wasn't a parent */
1259 remove a secondary reference to a pointer. This undo's what
1260 talloc_reference() has done. The context and pointer arguments
1261 must match those given to a talloc_reference()
1263 static inline int talloc_unreference(const void *context, const void *ptr)
1265 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1266 struct talloc_reference_handle *h;
1268 if (unlikely(context == NULL)) {
1269 context = null_context;
1272 for (h=tc->refs;h;h=h->next) {
1273 struct talloc_chunk *p = talloc_parent_chunk(h);
1275 if (context == NULL) break;
1276 } else if (TC_PTR_FROM_CHUNK(p) == context) {
1284 return _talloc_free_internal(h, __location__);
1288 remove a specific parent context from a pointer. This is a more
1289 controlled variant of talloc_free()
1291 _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
1293 struct talloc_chunk *tc_p, *new_p, *tc_c;
1300 if (context == NULL) {
1301 context = null_context;
1304 if (talloc_unreference(context, ptr) == 0) {
1308 if (context != NULL) {
1309 tc_c = talloc_chunk_from_ptr(context);
1313 if (tc_c != talloc_parent_chunk(ptr)) {
1317 tc_p = talloc_chunk_from_ptr(ptr);
1319 if (tc_p->refs == NULL) {
1320 return _talloc_free_internal(ptr, __location__);
1323 new_p = talloc_parent_chunk(tc_p->refs);
1325 new_parent = TC_PTR_FROM_CHUNK(new_p);
1330 if (talloc_unreference(new_parent, ptr) != 0) {
1334 _talloc_steal_internal(new_parent, ptr);
1340 add a name to an existing pointer - va_list version
1342 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1344 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
1346 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1347 tc->name = talloc_vasprintf(ptr, fmt, ap);
1348 if (likely(tc->name)) {
1349 _talloc_set_name_const(tc->name, ".name");
1355 add a name to an existing pointer
1357 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
1362 name = talloc_set_name_v(ptr, fmt, ap);
1369 create a named talloc pointer. Any talloc pointer can be named, and
1370 talloc_named() operates just like talloc() except that it allows you
1371 to name the pointer.
1373 _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
1379 ptr = __talloc(context, size);
1380 if (unlikely(ptr == NULL)) return NULL;
1383 name = talloc_set_name_v(ptr, fmt, ap);
1386 if (unlikely(name == NULL)) {
1387 _talloc_free_internal(ptr, __location__);
1395 return the name of a talloc ptr, or "UNNAMED"
1397 static inline const char *__talloc_get_name(const void *ptr)
1399 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1400 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
1401 return ".reference";
1403 if (likely(tc->name)) {
1409 _PUBLIC_ const char *talloc_get_name(const void *ptr)
1411 return __talloc_get_name(ptr);
1415 check if a pointer has the given name. If it does, return the pointer,
1416 otherwise return NULL
1418 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
1421 if (unlikely(ptr == NULL)) return NULL;
1422 pname = __talloc_get_name(ptr);
1423 if (likely(pname == name || strcmp(pname, name) == 0)) {
1424 return discard_const_p(void, ptr);
1429 static void talloc_abort_type_mismatch(const char *location,
1431 const char *expected)
1435 reason = talloc_asprintf(NULL,
1436 "%s: Type mismatch: name[%s] expected[%s]",
1441 reason = "Type mismatch";
1444 talloc_abort(reason);
1447 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
1451 if (unlikely(ptr == NULL)) {
1452 talloc_abort_type_mismatch(location, NULL, name);
1456 pname = __talloc_get_name(ptr);
1457 if (likely(pname == name || strcmp(pname, name) == 0)) {
1458 return discard_const_p(void, ptr);
1461 talloc_abort_type_mismatch(location, pname, name);
1466 this is for compatibility with older versions of talloc
1468 _PUBLIC_ void *talloc_init(const char *fmt, ...)
1474 ptr = __talloc(NULL, 0);
1475 if (unlikely(ptr == NULL)) return NULL;
1478 name = talloc_set_name_v(ptr, fmt, ap);
1481 if (unlikely(name == NULL)) {
1482 _talloc_free_internal(ptr, __location__);
1489 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
1491 const char *location)
1494 /* we need to work out who will own an abandoned child
1495 if it cannot be freed. In priority order, the first
1496 choice is owner of any remaining reference to this
1497 pointer, the second choice is our parent, and the
1498 final choice is the null context. */
1499 void *child = TC_PTR_FROM_CHUNK(tc->child);
1500 const void *new_parent = null_context;
1501 if (unlikely(tc->child->refs)) {
1502 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1503 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1505 if (unlikely(_talloc_free_internal(child, location) == -1)) {
1506 if (talloc_parent_chunk(child) != tc) {
1508 * Destructor already reparented this child.
1509 * No further reparenting needed.
1513 if (new_parent == null_context) {
1514 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1515 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1517 _talloc_steal_internal(new_parent, child);
1523 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1524 should probably not be used in new code. It's in here to keep the talloc
1525 code consistent across Samba 3 and 4.
1527 _PUBLIC_ void talloc_free_children(void *ptr)
1529 struct talloc_chunk *tc_name = NULL;
1530 struct talloc_chunk *tc;
1532 if (unlikely(ptr == NULL)) {
1536 tc = talloc_chunk_from_ptr(ptr);
1538 /* we do not want to free the context name if it is a child .. */
1539 if (likely(tc->child)) {
1540 for (tc_name = tc->child; tc_name; tc_name = tc_name->next) {
1541 if (tc->name == TC_PTR_FROM_CHUNK(tc_name)) break;
1544 _TLIST_REMOVE(tc->child, tc_name);
1546 tc->child->parent = tc;
1551 _talloc_free_children_internal(tc, ptr, __location__);
1553 /* .. so we put it back after all other children have been freed */
1556 tc->child->parent = NULL;
1558 tc_name->parent = tc;
1559 _TLIST_ADD(tc->child, tc_name);
1564 Allocate a bit of memory as a child of an existing pointer
1566 _PUBLIC_ void *_talloc(const void *context, size_t size)
1568 return __talloc(context, size);
1572 externally callable talloc_set_name_const()
1574 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1576 _talloc_set_name_const(ptr, name);
1580 create a named talloc pointer. Any talloc pointer can be named, and
1581 talloc_named() operates just like talloc() except that it allows you
1582 to name the pointer.
1584 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1586 return _talloc_named_const(context, size, name);
1590 free a talloc pointer. This also frees all child pointers of this
1593 return 0 if the memory is actually freed, otherwise -1. The memory
1594 will not be freed if the ref_count is > 1 or the destructor (if
1595 any) returns non-zero
1597 _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1599 struct talloc_chunk *tc;
1601 if (unlikely(ptr == NULL)) {
1605 tc = talloc_chunk_from_ptr(ptr);
1607 if (unlikely(tc->refs != NULL)) {
1608 struct talloc_reference_handle *h;
1610 if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1611 /* in this case we do know which parent should
1612 get this pointer, as there is really only
1614 return talloc_unlink(null_context, ptr);
1617 talloc_log("ERROR: talloc_free with references at %s\n",
1620 for (h=tc->refs; h; h=h->next) {
1621 talloc_log("\treference at %s\n",
1627 return _talloc_free_internal(ptr, location);
1633 A talloc version of realloc. The context argument is only used if
1636 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1638 struct talloc_chunk *tc;
1640 bool malloced = false;
1641 struct talloc_pool_hdr *pool_hdr = NULL;
1642 size_t old_size = 0;
1643 size_t new_size = 0;
1645 /* size zero is equivalent to free() */
1646 if (unlikely(size == 0)) {
1647 talloc_unlink(context, ptr);
1651 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1655 /* realloc(NULL) is equivalent to malloc() */
1657 return _talloc_named_const(context, size, name);
1660 tc = talloc_chunk_from_ptr(ptr);
1662 /* don't allow realloc on referenced pointers */
1663 if (unlikely(tc->refs)) {
1667 /* don't let anybody try to realloc a talloc_pool */
1668 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1672 if (tc->limit && (size > tc->size)) {
1673 if (!talloc_memlimit_check(tc->limit, (size - tc->size))) {
1679 /* handle realloc inside a talloc_pool */
1680 if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
1681 pool_hdr = tc->pool;
1684 #if (ALWAYS_REALLOC == 0)
1685 /* don't shrink if we have less than 1k to gain */
1686 if (size < tc->size && tc->limit == NULL) {
1688 void *next_tc = tc_next_chunk(tc);
1689 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1691 if (next_tc == pool_hdr->end) {
1692 /* note: tc->size has changed, so this works */
1693 pool_hdr->end = tc_next_chunk(tc);
1696 } else if ((tc->size - size) < 1024) {
1698 * if we call TC_INVALIDATE_SHRINK_CHUNK() here
1699 * we would need to call TC_UNDEFINE_GROW_CHUNK()
1700 * after each realloc call, which slows down
1701 * testing a lot :-(.
1703 * That is why we only mark memory as undefined here.
1705 TC_UNDEFINE_SHRINK_CHUNK(tc, size);
1707 /* do not shrink if we have less than 1k to gain */
1711 } else if (tc->size == size) {
1713 * do not change the pointer if it is exactly
1720 /* by resetting magic we catch users of the old memory */
1721 tc->flags |= TALLOC_FLAG_FREE;
1725 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE, 0);
1726 pool_hdr->object_count--;
1728 if (new_ptr == NULL) {
1729 new_ptr = malloc(TC_HDR_SIZE+size);
1735 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1736 TC_INVALIDATE_FULL_CHUNK(tc);
1739 /* We're doing malloc then free here, so record the difference. */
1740 old_size = tc->size;
1742 new_ptr = malloc(size + TC_HDR_SIZE);
1744 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1750 struct talloc_chunk *pool_tc;
1751 void *next_tc = tc_next_chunk(tc);
1752 size_t old_chunk_size = TC_ALIGN16(TC_HDR_SIZE + tc->size);
1753 size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
1754 size_t space_needed;
1756 unsigned int chunk_count = pool_hdr->object_count;
1758 pool_tc = talloc_chunk_from_pool(pool_hdr);
1759 if (!(pool_tc->flags & TALLOC_FLAG_FREE)) {
1763 if (chunk_count == 1) {
1765 * optimize for the case where 'tc' is the only
1766 * chunk in the pool.
1768 char *start = tc_pool_first_chunk(pool_hdr);
1769 space_needed = new_chunk_size;
1770 space_left = (char *)tc_pool_end(pool_hdr) - start;
1772 if (space_left >= space_needed) {
1773 size_t old_used = TC_HDR_SIZE + tc->size;
1774 size_t new_used = TC_HDR_SIZE + size;
1777 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
1781 * start -> tc may have
1782 * been freed and thus been marked as
1783 * VALGRIND_MEM_NOACCESS. Set it to
1784 * VALGRIND_MEM_UNDEFINED so we can
1785 * copy into it without valgrind errors.
1786 * We can't just mark
1787 * new_ptr -> new_ptr + old_used
1788 * as this may overlap on top of tc,
1789 * (which is why we use memmove, not
1790 * memcpy below) hence the MIN.
1792 size_t undef_len = MIN((((char *)tc) - ((char *)new_ptr)),old_used);
1793 VALGRIND_MAKE_MEM_UNDEFINED(new_ptr, undef_len);
1797 memmove(new_ptr, tc, old_used);
1799 tc = (struct talloc_chunk *)new_ptr;
1800 TC_UNDEFINE_GROW_CHUNK(tc, size);
1803 * first we do not align the pool pointer
1804 * because we want to invalidate the padding
1807 pool_hdr->end = new_used + (char *)new_ptr;
1808 tc_invalidate_pool(pool_hdr);
1810 /* now the aligned pointer */
1811 pool_hdr->end = new_chunk_size + (char *)new_ptr;
1818 if (new_chunk_size == old_chunk_size) {
1819 TC_UNDEFINE_GROW_CHUNK(tc, size);
1820 tc->flags &= ~TALLOC_FLAG_FREE;
1825 if (next_tc == pool_hdr->end) {
1827 * optimize for the case where 'tc' is the last
1828 * chunk in the pool.
1830 space_needed = new_chunk_size - old_chunk_size;
1831 space_left = tc_pool_space_left(pool_hdr);
1833 if (space_left >= space_needed) {
1834 TC_UNDEFINE_GROW_CHUNK(tc, size);
1835 tc->flags &= ~TALLOC_FLAG_FREE;
1837 pool_hdr->end = tc_next_chunk(tc);
1842 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE, 0);
1844 if (new_ptr == NULL) {
1845 new_ptr = malloc(TC_HDR_SIZE+size);
1851 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1853 _talloc_free_poolmem(tc, __location__ "_talloc_realloc");
1857 /* We're doing realloc here, so record the difference. */
1858 old_size = tc->size;
1860 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1864 if (unlikely(!new_ptr)) {
1865 tc->flags &= ~TALLOC_FLAG_FREE;
1869 tc = (struct talloc_chunk *)new_ptr;
1870 tc->flags &= ~TALLOC_FLAG_FREE;
1872 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1875 tc->parent->child = tc;
1878 tc->child->parent = tc;
1882 tc->prev->next = tc;
1885 tc->next->prev = tc;
1888 if (new_size > old_size) {
1889 talloc_memlimit_grow(tc->limit, new_size - old_size);
1890 } else if (new_size < old_size) {
1891 talloc_memlimit_shrink(tc->limit, old_size - new_size);
1895 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1897 return TC_PTR_FROM_CHUNK(tc);
1901 a wrapper around talloc_steal() for situations where you are moving a pointer
1902 between two structures, and want the old pointer to be set to NULL
1904 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
1906 const void **pptr = discard_const_p(const void *,_pptr);
1907 void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1912 enum talloc_mem_count_type {
1918 static inline size_t _talloc_total_mem_internal(const void *ptr,
1919 enum talloc_mem_count_type type,
1920 struct talloc_memlimit *old_limit,
1921 struct talloc_memlimit *new_limit)
1924 struct talloc_chunk *c, *tc;
1933 tc = talloc_chunk_from_ptr(ptr);
1935 if (old_limit || new_limit) {
1936 if (tc->limit && tc->limit->upper == old_limit) {
1937 tc->limit->upper = new_limit;
1941 /* optimize in the memlimits case */
1942 if (type == TOTAL_MEM_LIMIT &&
1943 tc->limit != NULL &&
1944 tc->limit != old_limit &&
1945 tc->limit->parent == tc) {
1946 return tc->limit->cur_size;
1949 if (tc->flags & TALLOC_FLAG_LOOP) {
1953 tc->flags |= TALLOC_FLAG_LOOP;
1955 if (old_limit || new_limit) {
1956 if (old_limit == tc->limit) {
1957 tc->limit = new_limit;
1962 case TOTAL_MEM_SIZE:
1963 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1967 case TOTAL_MEM_BLOCKS:
1970 case TOTAL_MEM_LIMIT:
1971 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1973 * Don't count memory allocated from a pool
1974 * when calculating limits. Only count the
1977 if (!(tc->flags & TALLOC_FLAG_POOLMEM)) {
1978 if (tc->flags & TALLOC_FLAG_POOL) {
1980 * If this is a pool, the allocated
1981 * size is in the pool header, and
1982 * remember to add in the prefix
1985 struct talloc_pool_hdr *pool_hdr
1986 = talloc_pool_from_chunk(tc);
1987 total = pool_hdr->poolsize +
1991 total = tc->size + TC_HDR_SIZE;
1997 for (c = tc->child; c; c = c->next) {
1998 total += _talloc_total_mem_internal(TC_PTR_FROM_CHUNK(c), type,
1999 old_limit, new_limit);
2002 tc->flags &= ~TALLOC_FLAG_LOOP;
2008 return the total size of a talloc pool (subtree)
2010 _PUBLIC_ size_t talloc_total_size(const void *ptr)
2012 return _talloc_total_mem_internal(ptr, TOTAL_MEM_SIZE, NULL, NULL);
2016 return the total number of blocks in a talloc pool (subtree)
2018 _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
2020 return _talloc_total_mem_internal(ptr, TOTAL_MEM_BLOCKS, NULL, NULL);
2024 return the number of external references to a pointer
2026 _PUBLIC_ size_t talloc_reference_count(const void *ptr)
2028 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
2029 struct talloc_reference_handle *h;
2032 for (h=tc->refs;h;h=h->next) {
2039 report on memory usage by all children of a pointer, giving a full tree view
2041 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
2042 void (*callback)(const void *ptr,
2043 int depth, int max_depth,
2045 void *private_data),
2048 struct talloc_chunk *c, *tc;
2053 if (ptr == NULL) return;
2055 tc = talloc_chunk_from_ptr(ptr);
2057 if (tc->flags & TALLOC_FLAG_LOOP) {
2061 callback(ptr, depth, max_depth, 0, private_data);
2063 if (max_depth >= 0 && depth >= max_depth) {
2067 tc->flags |= TALLOC_FLAG_LOOP;
2068 for (c=tc->child;c;c=c->next) {
2069 if (c->name == TALLOC_MAGIC_REFERENCE) {
2070 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
2071 callback(h->ptr, depth + 1, max_depth, 1, private_data);
2073 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
2076 tc->flags &= ~TALLOC_FLAG_LOOP;
2079 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
2081 const char *name = __talloc_get_name(ptr);
2082 struct talloc_chunk *tc;
2083 FILE *f = (FILE *)_f;
2086 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
2090 tc = talloc_chunk_from_ptr(ptr);
2091 if (tc->limit && tc->limit->parent == tc) {
2092 fprintf(f, "%*s%-30s is a memlimit context"
2093 " (max_size = %lu bytes, cur_size = %lu bytes)\n",
2096 (unsigned long)tc->limit->max_size,
2097 (unsigned long)tc->limit->cur_size);
2101 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
2102 (max_depth < 0 ? "full " :""), name,
2103 (unsigned long)talloc_total_size(ptr),
2104 (unsigned long)talloc_total_blocks(ptr));
2108 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
2111 (unsigned long)talloc_total_size(ptr),
2112 (unsigned long)talloc_total_blocks(ptr),
2113 (int)talloc_reference_count(ptr), ptr);
2116 fprintf(f, "content: ");
2117 if (talloc_total_size(ptr)) {
2118 int tot = talloc_total_size(ptr);
2121 for (i = 0; i < tot; i++) {
2122 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
2123 fprintf(f, "%c", ((char *)ptr)[i]);
2125 fprintf(f, "~%02x", ((char *)ptr)[i]);
2134 report on memory usage by all children of a pointer, giving a full tree view
2136 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
2139 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
2145 report on memory usage by all children of a pointer, giving a full tree view
2147 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
2149 talloc_report_depth_file(ptr, 0, -1, f);
2153 report on memory usage by all children of a pointer
2155 _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
2157 talloc_report_depth_file(ptr, 0, 1, f);
2161 report on any memory hanging off the null context
2163 static void talloc_report_null(void)
2165 if (talloc_total_size(null_context) != 0) {
2166 talloc_report(null_context, stderr);
2171 report on any memory hanging off the null context
2173 static void talloc_report_null_full(void)
2175 if (talloc_total_size(null_context) != 0) {
2176 talloc_report_full(null_context, stderr);
2181 enable tracking of the NULL context
2183 _PUBLIC_ void talloc_enable_null_tracking(void)
2185 if (null_context == NULL) {
2186 null_context = _talloc_named_const(NULL, 0, "null_context");
2187 if (autofree_context != NULL) {
2188 talloc_reparent(NULL, null_context, autofree_context);
2194 enable tracking of the NULL context, not moving the autofree context
2195 into the NULL context. This is needed for the talloc testsuite
2197 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
2199 if (null_context == NULL) {
2200 null_context = _talloc_named_const(NULL, 0, "null_context");
2205 disable tracking of the NULL context
2207 _PUBLIC_ void talloc_disable_null_tracking(void)
2209 if (null_context != NULL) {
2210 /* we have to move any children onto the real NULL
2212 struct talloc_chunk *tc, *tc2;
2213 tc = talloc_chunk_from_ptr(null_context);
2214 for (tc2 = tc->child; tc2; tc2=tc2->next) {
2215 if (tc2->parent == tc) tc2->parent = NULL;
2216 if (tc2->prev == tc) tc2->prev = NULL;
2218 for (tc2 = tc->next; tc2; tc2=tc2->next) {
2219 if (tc2->parent == tc) tc2->parent = NULL;
2220 if (tc2->prev == tc) tc2->prev = NULL;
2225 talloc_free(null_context);
2226 null_context = NULL;
2230 enable leak reporting on exit
2232 _PUBLIC_ void talloc_enable_leak_report(void)
2234 talloc_enable_null_tracking();
2235 atexit(talloc_report_null);
2239 enable full leak reporting on exit
2241 _PUBLIC_ void talloc_enable_leak_report_full(void)
2243 talloc_enable_null_tracking();
2244 atexit(talloc_report_null_full);
2248 talloc and zero memory.
2250 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
2252 void *p = _talloc_named_const(ctx, size, name);
2255 memset(p, '\0', size);
2262 memdup with a talloc.
2264 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
2266 void *newp = _talloc_named_const(t, size, name);
2269 memcpy(newp, p, size);
2275 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
2279 ret = (char *)__talloc(t, len + 1);
2280 if (unlikely(!ret)) return NULL;
2282 memcpy(ret, p, len);
2285 _talloc_set_name_const(ret, ret);
2290 strdup with a talloc
2292 _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
2294 if (unlikely(!p)) return NULL;
2295 return __talloc_strlendup(t, p, strlen(p));
2299 strndup with a talloc
2301 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
2303 if (unlikely(!p)) return NULL;
2304 return __talloc_strlendup(t, p, strnlen(p, n));
2307 static inline char *__talloc_strlendup_append(char *s, size_t slen,
2308 const char *a, size_t alen)
2312 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
2313 if (unlikely(!ret)) return NULL;
2315 /* append the string and the trailing \0 */
2316 memcpy(&ret[slen], a, alen);
2319 _talloc_set_name_const(ret, ret);
2324 * Appends at the end of the string.
2326 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
2329 return talloc_strdup(NULL, a);
2336 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
2340 * Appends at the end of the talloc'ed buffer,
2341 * not the end of the string.
2343 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
2348 return talloc_strdup(NULL, a);
2355 slen = talloc_get_size(s);
2356 if (likely(slen > 0)) {
2360 return __talloc_strlendup_append(s, slen, a, strlen(a));
2364 * Appends at the end of the string.
2366 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
2369 return talloc_strndup(NULL, a, n);
2376 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
2380 * Appends at the end of the talloc'ed buffer,
2381 * not the end of the string.
2383 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
2388 return talloc_strndup(NULL, a, n);
2395 slen = talloc_get_size(s);
2396 if (likely(slen > 0)) {
2400 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
2403 #ifndef HAVE_VA_COPY
2404 #ifdef HAVE___VA_COPY
2405 #define va_copy(dest, src) __va_copy(dest, src)
2407 #define va_copy(dest, src) (dest) = (src)
2411 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
2418 /* this call looks strange, but it makes it work on older solaris boxes */
2420 len = vsnprintf(buf, sizeof(buf), fmt, ap2);
2422 if (unlikely(len < 0)) {
2426 ret = (char *)__talloc(t, len+1);
2427 if (unlikely(!ret)) return NULL;
2429 if (len < sizeof(buf)) {
2430 memcpy(ret, buf, len+1);
2433 vsnprintf(ret, len+1, fmt, ap2);
2437 _talloc_set_name_const(ret, ret);
2443 Perform string formatting, and return a pointer to newly allocated
2444 memory holding the result, inside a memory pool.
2446 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
2452 ret = talloc_vasprintf(t, fmt, ap);
2457 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2458 const char *fmt, va_list ap)
2459 PRINTF_ATTRIBUTE(3,0);
2461 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2462 const char *fmt, va_list ap)
2469 alen = vsnprintf(&c, 1, fmt, ap2);
2473 /* Either the vsnprintf failed or the format resulted in
2474 * no characters being formatted. In the former case, we
2475 * ought to return NULL, in the latter we ought to return
2476 * the original string. Most current callers of this
2477 * function expect it to never return NULL.
2482 s = talloc_realloc(NULL, s, char, slen + alen + 1);
2483 if (!s) return NULL;
2486 vsnprintf(s + slen, alen + 1, fmt, ap2);
2489 _talloc_set_name_const(s, s);
2494 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2495 * and return @p s, which may have moved. Good for gradually
2496 * accumulating output into a string buffer. Appends at the end
2499 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
2502 return talloc_vasprintf(NULL, fmt, ap);
2505 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
2509 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2510 * and return @p s, which may have moved. Always appends at the
2511 * end of the talloc'ed buffer, not the end of the string.
2513 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
2518 return talloc_vasprintf(NULL, fmt, ap);
2521 slen = talloc_get_size(s);
2522 if (likely(slen > 0)) {
2526 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
2530 Realloc @p s to append the formatted result of @p fmt and return @p
2531 s, which may have moved. Good for gradually accumulating output
2532 into a string buffer.
2534 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
2539 s = talloc_vasprintf_append(s, fmt, ap);
2545 Realloc @p s to append the formatted result of @p fmt and return @p
2546 s, which may have moved. Good for gradually accumulating output
2549 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
2554 s = talloc_vasprintf_append_buffer(s, fmt, ap);
2560 alloc an array, checking for integer overflow in the array size
2562 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2564 if (count >= MAX_TALLOC_SIZE/el_size) {
2567 return _talloc_named_const(ctx, el_size * count, name);
2571 alloc an zero array, checking for integer overflow in the array size
2573 _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2575 if (count >= MAX_TALLOC_SIZE/el_size) {
2578 return _talloc_zero(ctx, el_size * count, name);
2582 realloc an array, checking for integer overflow in the array size
2584 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
2586 if (count >= MAX_TALLOC_SIZE/el_size) {
2589 return _talloc_realloc(ctx, ptr, el_size * count, name);
2593 a function version of talloc_realloc(), so it can be passed as a function pointer
2594 to libraries that want a realloc function (a realloc function encapsulates
2595 all the basic capabilities of an allocation library, which is why this is useful)
2597 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
2599 return _talloc_realloc(context, ptr, size, NULL);
2603 static int talloc_autofree_destructor(void *ptr)
2605 autofree_context = NULL;
2609 static void talloc_autofree(void)
2611 talloc_free(autofree_context);
2615 return a context which will be auto-freed on exit
2616 this is useful for reducing the noise in leak reports
2618 _PUBLIC_ void *talloc_autofree_context(void)
2620 if (autofree_context == NULL) {
2621 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
2622 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
2623 atexit(talloc_autofree);
2625 return autofree_context;
2628 _PUBLIC_ size_t talloc_get_size(const void *context)
2630 struct talloc_chunk *tc;
2632 if (context == NULL) {
2633 context = null_context;
2635 if (context == NULL) {
2639 tc = talloc_chunk_from_ptr(context);
2645 find a parent of this context that has the given name, if any
2647 _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
2649 struct talloc_chunk *tc;
2651 if (context == NULL) {
2655 tc = talloc_chunk_from_ptr(context);
2657 if (tc->name && strcmp(tc->name, name) == 0) {
2658 return TC_PTR_FROM_CHUNK(tc);
2660 while (tc && tc->prev) tc = tc->prev;
2669 show the parentage of a context
2671 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
2673 struct talloc_chunk *tc;
2675 if (context == NULL) {
2676 fprintf(file, "talloc no parents for NULL\n");
2680 tc = talloc_chunk_from_ptr(context);
2681 fprintf(file, "talloc parents of '%s'\n", __talloc_get_name(context));
2683 fprintf(file, "\t'%s'\n", __talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
2684 while (tc && tc->prev) tc = tc->prev;
2693 return 1 if ptr is a parent of context
2695 static int _talloc_is_parent(const void *context, const void *ptr, int depth)
2697 struct talloc_chunk *tc;
2699 if (context == NULL) {
2703 tc = talloc_chunk_from_ptr(context);
2708 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2709 while (tc && tc->prev) tc = tc->prev;
2719 return 1 if ptr is a parent of context
2721 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2723 return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);
2727 return the total size of memory used by this context and all children
2729 static inline size_t _talloc_total_limit_size(const void *ptr,
2730 struct talloc_memlimit *old_limit,
2731 struct talloc_memlimit *new_limit)
2733 return _talloc_total_mem_internal(ptr, TOTAL_MEM_LIMIT,
2734 old_limit, new_limit);
2737 static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size)
2739 struct talloc_memlimit *l;
2741 for (l = limit; l != NULL; l = l->upper) {
2742 if (l->max_size != 0 &&
2743 ((l->max_size <= l->cur_size) ||
2744 (l->max_size - l->cur_size < size))) {
2753 Update memory limits when freeing a talloc_chunk.
2755 static void talloc_memlimit_update_on_free(struct talloc_chunk *tc)
2757 size_t limit_shrink_size;
2764 * Pool entries don't count. Only the pools
2765 * themselves are counted as part of the memory
2766 * limits. Note that this also takes care of
2767 * nested pools which have both flags
2768 * TALLOC_FLAG_POOLMEM|TALLOC_FLAG_POOL set.
2770 if (tc->flags & TALLOC_FLAG_POOLMEM) {
2775 * If we are part of a memory limited context hierarchy
2776 * we need to subtract the memory used from the counters
2779 limit_shrink_size = tc->size+TC_HDR_SIZE;
2782 * If we're deallocating a pool, take into
2783 * account the prefix size added for the pool.
2786 if (tc->flags & TALLOC_FLAG_POOL) {
2787 limit_shrink_size += TP_HDR_SIZE;
2790 talloc_memlimit_shrink(tc->limit, limit_shrink_size);
2792 if (tc->limit->parent == tc) {
2800 Increase memory limit accounting after a malloc/realloc.
2802 static void talloc_memlimit_grow(struct talloc_memlimit *limit,
2805 struct talloc_memlimit *l;
2807 for (l = limit; l != NULL; l = l->upper) {
2808 size_t new_cur_size = l->cur_size + size;
2809 if (new_cur_size < l->cur_size) {
2810 talloc_abort("logic error in talloc_memlimit_grow\n");
2813 l->cur_size = new_cur_size;
2818 Decrease memory limit accounting after a free/realloc.
2820 static void talloc_memlimit_shrink(struct talloc_memlimit *limit,
2823 struct talloc_memlimit *l;
2825 for (l = limit; l != NULL; l = l->upper) {
2826 if (l->cur_size < size) {
2827 talloc_abort("logic error in talloc_memlimit_shrink\n");
2830 l->cur_size = l->cur_size - size;
2834 _PUBLIC_ int talloc_set_memlimit(const void *ctx, size_t max_size)
2836 struct talloc_chunk *tc = talloc_chunk_from_ptr(ctx);
2837 struct talloc_memlimit *orig_limit;
2838 struct talloc_memlimit *limit = NULL;
2840 if (tc->limit && tc->limit->parent == tc) {
2841 tc->limit->max_size = max_size;
2844 orig_limit = tc->limit;
2846 limit = malloc(sizeof(struct talloc_memlimit));
2847 if (limit == NULL) {
2851 limit->max_size = max_size;
2852 limit->cur_size = _talloc_total_limit_size(ctx, tc->limit, limit);
2855 limit->upper = orig_limit;
2857 limit->upper = NULL;