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);
324 * We get 16 bytes from getauxval. By calling rand(),
325 * a totally insecure PRNG, but one that will
326 * deterministically have a different value when called
327 * twice, we ensure that if two talloc-like libraries
328 * are somehow loaded in the same address space, that
329 * because we choose different bytes, we will keep the
330 * protection against collision of multiple talloc
333 * This protection is important because the effects of
334 * passing a talloc pointer from one to the other may
335 * be very hard to determine.
337 int offset = rand() % (16 - sizeof(random_value));
338 memcpy(&random_value, p + offset, sizeof(random_value));
343 * Otherwise, hope the location we are loaded in
344 * memory is randomised by someone else
346 random_value = ((uintptr_t)talloc_lib_init & 0xFFFFFFFF);
348 talloc_magic = random_value & ~TALLOC_FLAG_MASK;
351 #warning "No __attribute__((constructor)) support found on this platform, additional talloc security measures not available"
354 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
355 static void talloc_log(const char *fmt, ...)
360 if (!talloc_log_fn) {
365 message = talloc_vasprintf(NULL, fmt, ap);
368 talloc_log_fn(message);
369 talloc_free(message);
372 static void talloc_log_stderr(const char *message)
374 fprintf(stderr, "%s", message);
377 _PUBLIC_ void talloc_set_log_stderr(void)
379 talloc_set_log_fn(talloc_log_stderr);
382 static void (*talloc_abort_fn)(const char *reason);
384 _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
386 talloc_abort_fn = abort_fn;
389 static void talloc_abort(const char *reason)
391 talloc_log("%s\n", reason);
393 if (!talloc_abort_fn) {
394 TALLOC_ABORT(reason);
397 talloc_abort_fn(reason);
400 static void talloc_abort_magic(unsigned magic)
402 talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
405 static void talloc_abort_access_after_free(void)
407 talloc_abort("Bad talloc magic value - access after free");
410 static void talloc_abort_unknown_value(void)
412 talloc_abort("Bad talloc magic value - unknown value");
415 /* panic if we get a bad magic value */
416 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
418 const char *pp = (const char *)ptr;
419 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
420 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~TALLOC_FLAG_MASK)) != talloc_magic)) {
421 if ((tc->flags & (~0xF)) == talloc_magic) {
422 talloc_abort_magic(tc->flags & (~TALLOC_FLAG_MASK));
426 if (tc->flags & TALLOC_FLAG_FREE) {
427 talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
428 talloc_abort_access_after_free();
431 talloc_abort_unknown_value();
438 /* hook into the front of the list */
439 #define _TLIST_ADD(list, p) \
443 (p)->next = (p)->prev = NULL; \
445 (list)->prev = (p); \
446 (p)->next = (list); \
452 /* remove an element from a list - element doesn't have to be in list. */
453 #define _TLIST_REMOVE(list, p) \
455 if ((p) == (list)) { \
456 (list) = (p)->next; \
457 if (list) (list)->prev = NULL; \
459 if ((p)->prev) (p)->prev->next = (p)->next; \
460 if ((p)->next) (p)->next->prev = (p)->prev; \
462 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
467 return the parent chunk of a pointer
469 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
471 struct talloc_chunk *tc;
473 if (unlikely(ptr == NULL)) {
477 tc = talloc_chunk_from_ptr(ptr);
478 while (tc->prev) tc=tc->prev;
483 _PUBLIC_ void *talloc_parent(const void *ptr)
485 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
486 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
492 _PUBLIC_ const char *talloc_parent_name(const void *ptr)
494 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
495 return tc? tc->name : NULL;
499 A pool carries an in-pool object count count in the first 16 bytes.
500 bytes. This is done to support talloc_steal() to a parent outside of the
501 pool. The count includes the pool itself, so a talloc_free() on a pool will
502 only destroy the pool if the count has dropped to zero. A talloc_free() of a
503 pool member will reduce the count, and eventually also call free(3) on the
506 The object count is not put into "struct talloc_chunk" because it is only
507 relevant for talloc pools and the alignment to 16 bytes would increase the
508 memory footprint of each talloc chunk by those 16 bytes.
511 struct talloc_pool_hdr {
513 unsigned int object_count;
517 #define TP_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_pool_hdr))
519 static inline struct talloc_pool_hdr *talloc_pool_from_chunk(struct talloc_chunk *c)
521 return (struct talloc_pool_hdr *)((char *)c - TP_HDR_SIZE);
524 static inline struct talloc_chunk *talloc_chunk_from_pool(struct talloc_pool_hdr *h)
526 return (struct talloc_chunk *)((char *)h + TP_HDR_SIZE);
529 static inline void *tc_pool_end(struct talloc_pool_hdr *pool_hdr)
531 struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);
532 return (char *)tc + TC_HDR_SIZE + pool_hdr->poolsize;
535 static inline size_t tc_pool_space_left(struct talloc_pool_hdr *pool_hdr)
537 return (char *)tc_pool_end(pool_hdr) - (char *)pool_hdr->end;
540 /* If tc is inside a pool, this gives the next neighbour. */
541 static inline void *tc_next_chunk(struct talloc_chunk *tc)
543 return (char *)tc + TC_ALIGN16(TC_HDR_SIZE + tc->size);
546 static inline void *tc_pool_first_chunk(struct talloc_pool_hdr *pool_hdr)
548 struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);
549 return tc_next_chunk(tc);
552 /* Mark the whole remaining pool as not accessable */
553 static inline void tc_invalidate_pool(struct talloc_pool_hdr *pool_hdr)
555 size_t flen = tc_pool_space_left(pool_hdr);
557 if (unlikely(talloc_fill.enabled)) {
558 memset(pool_hdr->end, talloc_fill.fill_value, flen);
561 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
562 VALGRIND_MAKE_MEM_NOACCESS(pool_hdr->end, flen);
570 static inline struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
571 size_t size, size_t prefix_len)
573 struct talloc_pool_hdr *pool_hdr = NULL;
575 struct talloc_chunk *result;
578 if (parent == NULL) {
582 if (parent->flags & TALLOC_FLAG_POOL) {
583 pool_hdr = talloc_pool_from_chunk(parent);
585 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
586 pool_hdr = parent->pool;
589 if (pool_hdr == NULL) {
593 space_left = tc_pool_space_left(pool_hdr);
596 * Align size to 16 bytes
598 chunk_size = TC_ALIGN16(size + prefix_len);
600 if (space_left < chunk_size) {
604 result = (struct talloc_chunk *)((char *)pool_hdr->end + prefix_len);
606 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
607 VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, chunk_size);
610 pool_hdr->end = (void *)((char *)pool_hdr->end + chunk_size);
612 result->flags = talloc_magic | TALLOC_FLAG_POOLMEM;
613 result->pool = pool_hdr;
615 pool_hdr->object_count++;
621 Allocate a bit of memory as a child of an existing pointer
623 static inline void *__talloc_with_prefix(const void *context, size_t size,
626 struct talloc_chunk *tc = NULL;
627 struct talloc_memlimit *limit = NULL;
628 size_t total_len = TC_HDR_SIZE + size + prefix_len;
630 if (unlikely(context == NULL)) {
631 context = null_context;
634 if (unlikely(size >= MAX_TALLOC_SIZE)) {
638 if (unlikely(total_len < TC_HDR_SIZE)) {
642 if (context != NULL) {
643 struct talloc_chunk *ptc = talloc_chunk_from_ptr(context);
645 if (ptc->limit != NULL) {
649 tc = talloc_alloc_pool(ptc, TC_HDR_SIZE+size, prefix_len);
656 * Only do the memlimit check/update on actual allocation.
658 if (!talloc_memlimit_check(limit, total_len)) {
663 ptr = malloc(total_len);
664 if (unlikely(ptr == NULL)) {
667 tc = (struct talloc_chunk *)(ptr + prefix_len);
668 tc->flags = talloc_magic;
671 talloc_memlimit_grow(limit, total_len);
676 tc->destructor = NULL;
681 if (likely(context)) {
682 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
685 parent->child->parent = NULL;
686 tc->next = parent->child;
695 tc->next = tc->prev = tc->parent = NULL;
698 return TC_PTR_FROM_CHUNK(tc);
701 static inline void *__talloc(const void *context, size_t size)
703 return __talloc_with_prefix(context, size, 0);
707 * Create a talloc pool
710 static inline void *_talloc_pool(const void *context, size_t size)
712 struct talloc_chunk *tc;
713 struct talloc_pool_hdr *pool_hdr;
716 result = __talloc_with_prefix(context, size, TP_HDR_SIZE);
718 if (unlikely(result == NULL)) {
722 tc = talloc_chunk_from_ptr(result);
723 pool_hdr = talloc_pool_from_chunk(tc);
725 tc->flags |= TALLOC_FLAG_POOL;
728 pool_hdr->object_count = 1;
729 pool_hdr->end = result;
730 pool_hdr->poolsize = size;
732 tc_invalidate_pool(pool_hdr);
737 _PUBLIC_ void *talloc_pool(const void *context, size_t size)
739 return _talloc_pool(context, size);
743 * Create a talloc pool correctly sized for a basic size plus
744 * a number of subobjects whose total size is given. Essentially
745 * a custom allocator for talloc to reduce fragmentation.
748 _PUBLIC_ void *_talloc_pooled_object(const void *ctx,
750 const char *type_name,
751 unsigned num_subobjects,
752 size_t total_subobjects_size)
754 size_t poolsize, subobjects_slack, tmp;
755 struct talloc_chunk *tc;
756 struct talloc_pool_hdr *pool_hdr;
759 poolsize = type_size + total_subobjects_size;
761 if ((poolsize < type_size) || (poolsize < total_subobjects_size)) {
765 if (num_subobjects == UINT_MAX) {
768 num_subobjects += 1; /* the object body itself */
771 * Alignment can increase the pool size by at most 15 bytes per object
772 * plus alignment for the object itself
774 subobjects_slack = (TC_HDR_SIZE + TP_HDR_SIZE + 15) * num_subobjects;
775 if (subobjects_slack < num_subobjects) {
779 tmp = poolsize + subobjects_slack;
780 if ((tmp < poolsize) || (tmp < subobjects_slack)) {
785 ret = _talloc_pool(ctx, poolsize);
790 tc = talloc_chunk_from_ptr(ret);
791 tc->size = type_size;
793 pool_hdr = talloc_pool_from_chunk(tc);
795 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
796 VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, type_size);
799 pool_hdr->end = ((char *)pool_hdr->end + TC_ALIGN16(type_size));
801 _talloc_set_name_const(ret, type_name);
809 setup a destructor to be called on free of a pointer
810 the destructor should return 0 on success, or -1 on failure.
811 if the destructor fails then the free is failed, and the memory can
812 be continued to be used
814 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
816 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
817 tc->destructor = destructor;
821 increase the reference count on a piece of memory.
823 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
825 if (unlikely(!talloc_reference(null_context, ptr))) {
832 helper for talloc_reference()
834 this is referenced by a function pointer and should not be inline
836 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
838 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
839 _TLIST_REMOVE(ptr_tc->refs, handle);
844 more efficient way to add a name to a pointer - the name must point to a
847 static inline void _talloc_set_name_const(const void *ptr, const char *name)
849 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
854 internal talloc_named_const()
856 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
860 ptr = __talloc(context, size);
861 if (unlikely(ptr == NULL)) {
865 _talloc_set_name_const(ptr, name);
871 make a secondary reference to a pointer, hanging off the given context.
872 the pointer remains valid until both the original caller and this given
875 the major use for this is when two different structures need to reference the
876 same underlying data, and you want to be able to free the two instances separately,
879 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
881 struct talloc_chunk *tc;
882 struct talloc_reference_handle *handle;
883 if (unlikely(ptr == NULL)) return NULL;
885 tc = talloc_chunk_from_ptr(ptr);
886 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
887 sizeof(struct talloc_reference_handle),
888 TALLOC_MAGIC_REFERENCE);
889 if (unlikely(handle == NULL)) return NULL;
891 /* note that we hang the destructor off the handle, not the
892 main context as that allows the caller to still setup their
893 own destructor on the context if they want to */
894 talloc_set_destructor(handle, talloc_reference_destructor);
895 handle->ptr = discard_const_p(void, ptr);
896 handle->location = location;
897 _TLIST_ADD(tc->refs, handle);
901 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
903 static inline void _talloc_free_poolmem(struct talloc_chunk *tc,
904 const char *location)
906 struct talloc_pool_hdr *pool;
907 struct talloc_chunk *pool_tc;
911 pool_tc = talloc_chunk_from_pool(pool);
912 next_tc = tc_next_chunk(tc);
914 tc->flags |= TALLOC_FLAG_FREE;
916 /* we mark the freed memory with where we called the free
917 * from. This means on a double free error we can report where
918 * the first free came from
922 TC_INVALIDATE_FULL_CHUNK(tc);
924 if (unlikely(pool->object_count == 0)) {
925 talloc_abort("Pool object count zero!");
929 pool->object_count--;
931 if (unlikely(pool->object_count == 1
932 && !(pool_tc->flags & TALLOC_FLAG_FREE))) {
934 * if there is just one object left in the pool
935 * and pool->flags does not have TALLOC_FLAG_FREE,
936 * it means this is the pool itself and
937 * the rest is available for new objects
940 pool->end = tc_pool_first_chunk(pool);
941 tc_invalidate_pool(pool);
945 if (unlikely(pool->object_count == 0)) {
947 * we mark the freed memory with where we called the free
948 * from. This means on a double free error we can report where
949 * the first free came from
951 pool_tc->name = location;
953 if (pool_tc->flags & TALLOC_FLAG_POOLMEM) {
954 _talloc_free_poolmem(pool_tc, location);
957 * The talloc_memlimit_update_on_free()
958 * call takes into account the
959 * prefix TP_HDR_SIZE allocated before
960 * the pool talloc_chunk.
962 talloc_memlimit_update_on_free(pool_tc);
963 TC_INVALIDATE_FULL_CHUNK(pool_tc);
969 if (pool->end == next_tc) {
971 * if pool->pool still points to end of
972 * 'tc' (which is stored in the 'next_tc' variable),
973 * we can reclaim the memory of 'tc'.
980 * Do nothing. The memory is just "wasted", waiting for the pool
981 * itself to be freed.
985 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
987 const char *location);
990 internal talloc_free call
992 static inline int _talloc_free_internal(void *ptr, const char *location)
994 struct talloc_chunk *tc;
997 if (unlikely(ptr == NULL)) {
1001 /* possibly initialised the talloc fill value */
1002 if (unlikely(!talloc_fill.initialised)) {
1003 const char *fill = getenv(TALLOC_FILL_ENV);
1005 talloc_fill.enabled = true;
1006 talloc_fill.fill_value = strtoul(fill, NULL, 0);
1008 talloc_fill.initialised = true;
1011 tc = talloc_chunk_from_ptr(ptr);
1013 if (unlikely(tc->refs)) {
1015 /* check if this is a reference from a child or
1016 * grandchild back to it's parent or grandparent
1018 * in that case we need to remove the reference and
1019 * call another instance of talloc_free() on the current
1022 is_child = talloc_is_parent(tc->refs, ptr);
1023 _talloc_free_internal(tc->refs, location);
1025 return _talloc_free_internal(ptr, location);
1030 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
1031 /* we have a free loop - stop looping */
1035 if (unlikely(tc->destructor)) {
1036 talloc_destructor_t d = tc->destructor;
1037 if (d == (talloc_destructor_t)-1) {
1040 tc->destructor = (talloc_destructor_t)-1;
1043 * Only replace the destructor pointer if
1044 * calling the destructor didn't modify it.
1046 if (tc->destructor == (talloc_destructor_t)-1) {
1051 tc->destructor = NULL;
1055 _TLIST_REMOVE(tc->parent->child, tc);
1056 if (tc->parent->child) {
1057 tc->parent->child->parent = tc->parent;
1060 if (tc->prev) tc->prev->next = tc->next;
1061 if (tc->next) tc->next->prev = tc->prev;
1062 tc->prev = tc->next = NULL;
1065 tc->flags |= TALLOC_FLAG_LOOP;
1067 _talloc_free_children_internal(tc, ptr, location);
1069 tc->flags |= TALLOC_FLAG_FREE;
1071 /* we mark the freed memory with where we called the free
1072 * from. This means on a double free error we can report where
1073 * the first free came from
1075 tc->name = location;
1077 if (tc->flags & TALLOC_FLAG_POOL) {
1078 struct talloc_pool_hdr *pool;
1080 pool = talloc_pool_from_chunk(tc);
1082 if (unlikely(pool->object_count == 0)) {
1083 talloc_abort("Pool object count zero!");
1087 pool->object_count--;
1089 if (likely(pool->object_count != 0)) {
1094 * With object_count==0, a pool becomes a normal piece of
1095 * memory to free. If it's allocated inside a pool, it needs
1096 * to be freed as poolmem, else it needs to be just freed.
1103 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1104 _talloc_free_poolmem(tc, location);
1108 talloc_memlimit_update_on_free(tc);
1110 TC_INVALIDATE_FULL_CHUNK(tc);
1115 static inline size_t _talloc_total_limit_size(const void *ptr,
1116 struct talloc_memlimit *old_limit,
1117 struct talloc_memlimit *new_limit);
1120 move a lump of memory from one talloc context to another return the
1121 ptr on success, or NULL if it could not be transferred.
1122 passing NULL as ptr will always return NULL with no side effects.
1124 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
1126 struct talloc_chunk *tc, *new_tc;
1127 size_t ctx_size = 0;
1129 if (unlikely(!ptr)) {
1133 if (unlikely(new_ctx == NULL)) {
1134 new_ctx = null_context;
1137 tc = talloc_chunk_from_ptr(ptr);
1139 if (tc->limit != NULL) {
1141 ctx_size = _talloc_total_limit_size(ptr, NULL, NULL);
1143 /* Decrement the memory limit from the source .. */
1144 talloc_memlimit_shrink(tc->limit->upper, ctx_size);
1146 if (tc->limit->parent == tc) {
1147 tc->limit->upper = NULL;
1153 if (unlikely(new_ctx == NULL)) {
1155 _TLIST_REMOVE(tc->parent->child, tc);
1156 if (tc->parent->child) {
1157 tc->parent->child->parent = tc->parent;
1160 if (tc->prev) tc->prev->next = tc->next;
1161 if (tc->next) tc->next->prev = tc->prev;
1164 tc->parent = tc->next = tc->prev = NULL;
1165 return discard_const_p(void, ptr);
1168 new_tc = talloc_chunk_from_ptr(new_ctx);
1170 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
1171 return discard_const_p(void, ptr);
1175 _TLIST_REMOVE(tc->parent->child, tc);
1176 if (tc->parent->child) {
1177 tc->parent->child->parent = tc->parent;
1180 if (tc->prev) tc->prev->next = tc->next;
1181 if (tc->next) tc->next->prev = tc->prev;
1182 tc->prev = tc->next = NULL;
1185 tc->parent = new_tc;
1186 if (new_tc->child) new_tc->child->parent = NULL;
1187 _TLIST_ADD(new_tc->child, tc);
1189 if (tc->limit || new_tc->limit) {
1190 ctx_size = _talloc_total_limit_size(ptr, tc->limit,
1192 /* .. and increment it in the destination. */
1193 if (new_tc->limit) {
1194 talloc_memlimit_grow(new_tc->limit, ctx_size);
1198 return discard_const_p(void, ptr);
1202 move a lump of memory from one talloc context to another return the
1203 ptr on success, or NULL if it could not be transferred.
1204 passing NULL as ptr will always return NULL with no side effects.
1206 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
1208 struct talloc_chunk *tc;
1210 if (unlikely(ptr == NULL)) {
1214 tc = talloc_chunk_from_ptr(ptr);
1216 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
1217 struct talloc_reference_handle *h;
1219 talloc_log("WARNING: talloc_steal with references at %s\n",
1222 for (h=tc->refs; h; h=h->next) {
1223 talloc_log("\treference at %s\n",
1229 /* this test is probably too expensive to have on in the
1230 normal build, but it useful for debugging */
1231 if (talloc_is_parent(new_ctx, ptr)) {
1232 talloc_log("WARNING: stealing into talloc child at %s\n", location);
1236 return _talloc_steal_internal(new_ctx, ptr);
1240 this is like a talloc_steal(), but you must supply the old
1241 parent. This resolves the ambiguity in a talloc_steal() which is
1242 called on a context that has more than one parent (via references)
1244 The old parent can be either a reference or a parent
1246 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
1248 struct talloc_chunk *tc;
1249 struct talloc_reference_handle *h;
1251 if (unlikely(ptr == NULL)) {
1255 if (old_parent == talloc_parent(ptr)) {
1256 return _talloc_steal_internal(new_parent, ptr);
1259 tc = talloc_chunk_from_ptr(ptr);
1260 for (h=tc->refs;h;h=h->next) {
1261 if (talloc_parent(h) == old_parent) {
1262 if (_talloc_steal_internal(new_parent, h) != h) {
1265 return discard_const_p(void, ptr);
1269 /* it wasn't a parent */
1274 remove a secondary reference to a pointer. This undo's what
1275 talloc_reference() has done. The context and pointer arguments
1276 must match those given to a talloc_reference()
1278 static inline int talloc_unreference(const void *context, const void *ptr)
1280 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1281 struct talloc_reference_handle *h;
1283 if (unlikely(context == NULL)) {
1284 context = null_context;
1287 for (h=tc->refs;h;h=h->next) {
1288 struct talloc_chunk *p = talloc_parent_chunk(h);
1290 if (context == NULL) break;
1291 } else if (TC_PTR_FROM_CHUNK(p) == context) {
1299 return _talloc_free_internal(h, __location__);
1303 remove a specific parent context from a pointer. This is a more
1304 controlled variant of talloc_free()
1306 _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
1308 struct talloc_chunk *tc_p, *new_p, *tc_c;
1315 if (context == NULL) {
1316 context = null_context;
1319 if (talloc_unreference(context, ptr) == 0) {
1323 if (context != NULL) {
1324 tc_c = talloc_chunk_from_ptr(context);
1328 if (tc_c != talloc_parent_chunk(ptr)) {
1332 tc_p = talloc_chunk_from_ptr(ptr);
1334 if (tc_p->refs == NULL) {
1335 return _talloc_free_internal(ptr, __location__);
1338 new_p = talloc_parent_chunk(tc_p->refs);
1340 new_parent = TC_PTR_FROM_CHUNK(new_p);
1345 if (talloc_unreference(new_parent, ptr) != 0) {
1349 _talloc_steal_internal(new_parent, ptr);
1355 add a name to an existing pointer - va_list version
1357 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1359 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
1361 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1362 tc->name = talloc_vasprintf(ptr, fmt, ap);
1363 if (likely(tc->name)) {
1364 _talloc_set_name_const(tc->name, ".name");
1370 add a name to an existing pointer
1372 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
1377 name = talloc_set_name_v(ptr, fmt, ap);
1384 create a named talloc pointer. Any talloc pointer can be named, and
1385 talloc_named() operates just like talloc() except that it allows you
1386 to name the pointer.
1388 _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
1394 ptr = __talloc(context, size);
1395 if (unlikely(ptr == NULL)) return NULL;
1398 name = talloc_set_name_v(ptr, fmt, ap);
1401 if (unlikely(name == NULL)) {
1402 _talloc_free_internal(ptr, __location__);
1410 return the name of a talloc ptr, or "UNNAMED"
1412 static inline const char *__talloc_get_name(const void *ptr)
1414 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1415 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
1416 return ".reference";
1418 if (likely(tc->name)) {
1424 _PUBLIC_ const char *talloc_get_name(const void *ptr)
1426 return __talloc_get_name(ptr);
1430 check if a pointer has the given name. If it does, return the pointer,
1431 otherwise return NULL
1433 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
1436 if (unlikely(ptr == NULL)) return NULL;
1437 pname = __talloc_get_name(ptr);
1438 if (likely(pname == name || strcmp(pname, name) == 0)) {
1439 return discard_const_p(void, ptr);
1444 static void talloc_abort_type_mismatch(const char *location,
1446 const char *expected)
1450 reason = talloc_asprintf(NULL,
1451 "%s: Type mismatch: name[%s] expected[%s]",
1456 reason = "Type mismatch";
1459 talloc_abort(reason);
1462 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
1466 if (unlikely(ptr == NULL)) {
1467 talloc_abort_type_mismatch(location, NULL, name);
1471 pname = __talloc_get_name(ptr);
1472 if (likely(pname == name || strcmp(pname, name) == 0)) {
1473 return discard_const_p(void, ptr);
1476 talloc_abort_type_mismatch(location, pname, name);
1481 this is for compatibility with older versions of talloc
1483 _PUBLIC_ void *talloc_init(const char *fmt, ...)
1489 ptr = __talloc(NULL, 0);
1490 if (unlikely(ptr == NULL)) return NULL;
1493 name = talloc_set_name_v(ptr, fmt, ap);
1496 if (unlikely(name == NULL)) {
1497 _talloc_free_internal(ptr, __location__);
1504 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
1506 const char *location)
1509 /* we need to work out who will own an abandoned child
1510 if it cannot be freed. In priority order, the first
1511 choice is owner of any remaining reference to this
1512 pointer, the second choice is our parent, and the
1513 final choice is the null context. */
1514 void *child = TC_PTR_FROM_CHUNK(tc->child);
1515 const void *new_parent = null_context;
1516 if (unlikely(tc->child->refs)) {
1517 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1518 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1520 if (unlikely(_talloc_free_internal(child, location) == -1)) {
1521 if (talloc_parent_chunk(child) != tc) {
1523 * Destructor already reparented this child.
1524 * No further reparenting needed.
1528 if (new_parent == null_context) {
1529 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1530 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1532 _talloc_steal_internal(new_parent, child);
1538 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1539 should probably not be used in new code. It's in here to keep the talloc
1540 code consistent across Samba 3 and 4.
1542 _PUBLIC_ void talloc_free_children(void *ptr)
1544 struct talloc_chunk *tc_name = NULL;
1545 struct talloc_chunk *tc;
1547 if (unlikely(ptr == NULL)) {
1551 tc = talloc_chunk_from_ptr(ptr);
1553 /* we do not want to free the context name if it is a child .. */
1554 if (likely(tc->child)) {
1555 for (tc_name = tc->child; tc_name; tc_name = tc_name->next) {
1556 if (tc->name == TC_PTR_FROM_CHUNK(tc_name)) break;
1559 _TLIST_REMOVE(tc->child, tc_name);
1561 tc->child->parent = tc;
1566 _talloc_free_children_internal(tc, ptr, __location__);
1568 /* .. so we put it back after all other children have been freed */
1571 tc->child->parent = NULL;
1573 tc_name->parent = tc;
1574 _TLIST_ADD(tc->child, tc_name);
1579 Allocate a bit of memory as a child of an existing pointer
1581 _PUBLIC_ void *_talloc(const void *context, size_t size)
1583 return __talloc(context, size);
1587 externally callable talloc_set_name_const()
1589 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1591 _talloc_set_name_const(ptr, name);
1595 create a named talloc pointer. Any talloc pointer can be named, and
1596 talloc_named() operates just like talloc() except that it allows you
1597 to name the pointer.
1599 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1601 return _talloc_named_const(context, size, name);
1605 free a talloc pointer. This also frees all child pointers of this
1608 return 0 if the memory is actually freed, otherwise -1. The memory
1609 will not be freed if the ref_count is > 1 or the destructor (if
1610 any) returns non-zero
1612 _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1614 struct talloc_chunk *tc;
1616 if (unlikely(ptr == NULL)) {
1620 tc = talloc_chunk_from_ptr(ptr);
1622 if (unlikely(tc->refs != NULL)) {
1623 struct talloc_reference_handle *h;
1625 if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1626 /* in this case we do know which parent should
1627 get this pointer, as there is really only
1629 return talloc_unlink(null_context, ptr);
1632 talloc_log("ERROR: talloc_free with references at %s\n",
1635 for (h=tc->refs; h; h=h->next) {
1636 talloc_log("\treference at %s\n",
1642 return _talloc_free_internal(ptr, location);
1648 A talloc version of realloc. The context argument is only used if
1651 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1653 struct talloc_chunk *tc;
1655 bool malloced = false;
1656 struct talloc_pool_hdr *pool_hdr = NULL;
1657 size_t old_size = 0;
1658 size_t new_size = 0;
1660 /* size zero is equivalent to free() */
1661 if (unlikely(size == 0)) {
1662 talloc_unlink(context, ptr);
1666 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1670 /* realloc(NULL) is equivalent to malloc() */
1672 return _talloc_named_const(context, size, name);
1675 tc = talloc_chunk_from_ptr(ptr);
1677 /* don't allow realloc on referenced pointers */
1678 if (unlikely(tc->refs)) {
1682 /* don't let anybody try to realloc a talloc_pool */
1683 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1687 if (tc->limit && (size > tc->size)) {
1688 if (!talloc_memlimit_check(tc->limit, (size - tc->size))) {
1694 /* handle realloc inside a talloc_pool */
1695 if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
1696 pool_hdr = tc->pool;
1699 #if (ALWAYS_REALLOC == 0)
1700 /* don't shrink if we have less than 1k to gain */
1701 if (size < tc->size && tc->limit == NULL) {
1703 void *next_tc = tc_next_chunk(tc);
1704 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1706 if (next_tc == pool_hdr->end) {
1707 /* note: tc->size has changed, so this works */
1708 pool_hdr->end = tc_next_chunk(tc);
1711 } else if ((tc->size - size) < 1024) {
1713 * if we call TC_INVALIDATE_SHRINK_CHUNK() here
1714 * we would need to call TC_UNDEFINE_GROW_CHUNK()
1715 * after each realloc call, which slows down
1716 * testing a lot :-(.
1718 * That is why we only mark memory as undefined here.
1720 TC_UNDEFINE_SHRINK_CHUNK(tc, size);
1722 /* do not shrink if we have less than 1k to gain */
1726 } else if (tc->size == size) {
1728 * do not change the pointer if it is exactly
1735 /* by resetting magic we catch users of the old memory */
1736 tc->flags |= TALLOC_FLAG_FREE;
1740 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE, 0);
1741 pool_hdr->object_count--;
1743 if (new_ptr == NULL) {
1744 new_ptr = malloc(TC_HDR_SIZE+size);
1750 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1751 TC_INVALIDATE_FULL_CHUNK(tc);
1754 /* We're doing malloc then free here, so record the difference. */
1755 old_size = tc->size;
1757 new_ptr = malloc(size + TC_HDR_SIZE);
1759 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1765 struct talloc_chunk *pool_tc;
1766 void *next_tc = tc_next_chunk(tc);
1767 size_t old_chunk_size = TC_ALIGN16(TC_HDR_SIZE + tc->size);
1768 size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
1769 size_t space_needed;
1771 unsigned int chunk_count = pool_hdr->object_count;
1773 pool_tc = talloc_chunk_from_pool(pool_hdr);
1774 if (!(pool_tc->flags & TALLOC_FLAG_FREE)) {
1778 if (chunk_count == 1) {
1780 * optimize for the case where 'tc' is the only
1781 * chunk in the pool.
1783 char *start = tc_pool_first_chunk(pool_hdr);
1784 space_needed = new_chunk_size;
1785 space_left = (char *)tc_pool_end(pool_hdr) - start;
1787 if (space_left >= space_needed) {
1788 size_t old_used = TC_HDR_SIZE + tc->size;
1789 size_t new_used = TC_HDR_SIZE + size;
1792 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
1796 * start -> tc may have
1797 * been freed and thus been marked as
1798 * VALGRIND_MEM_NOACCESS. Set it to
1799 * VALGRIND_MEM_UNDEFINED so we can
1800 * copy into it without valgrind errors.
1801 * We can't just mark
1802 * new_ptr -> new_ptr + old_used
1803 * as this may overlap on top of tc,
1804 * (which is why we use memmove, not
1805 * memcpy below) hence the MIN.
1807 size_t undef_len = MIN((((char *)tc) - ((char *)new_ptr)),old_used);
1808 VALGRIND_MAKE_MEM_UNDEFINED(new_ptr, undef_len);
1812 memmove(new_ptr, tc, old_used);
1814 tc = (struct talloc_chunk *)new_ptr;
1815 TC_UNDEFINE_GROW_CHUNK(tc, size);
1818 * first we do not align the pool pointer
1819 * because we want to invalidate the padding
1822 pool_hdr->end = new_used + (char *)new_ptr;
1823 tc_invalidate_pool(pool_hdr);
1825 /* now the aligned pointer */
1826 pool_hdr->end = new_chunk_size + (char *)new_ptr;
1833 if (new_chunk_size == old_chunk_size) {
1834 TC_UNDEFINE_GROW_CHUNK(tc, size);
1835 tc->flags &= ~TALLOC_FLAG_FREE;
1840 if (next_tc == pool_hdr->end) {
1842 * optimize for the case where 'tc' is the last
1843 * chunk in the pool.
1845 space_needed = new_chunk_size - old_chunk_size;
1846 space_left = tc_pool_space_left(pool_hdr);
1848 if (space_left >= space_needed) {
1849 TC_UNDEFINE_GROW_CHUNK(tc, size);
1850 tc->flags &= ~TALLOC_FLAG_FREE;
1852 pool_hdr->end = tc_next_chunk(tc);
1857 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE, 0);
1859 if (new_ptr == NULL) {
1860 new_ptr = malloc(TC_HDR_SIZE+size);
1866 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1868 _talloc_free_poolmem(tc, __location__ "_talloc_realloc");
1872 /* We're doing realloc here, so record the difference. */
1873 old_size = tc->size;
1875 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1879 if (unlikely(!new_ptr)) {
1880 tc->flags &= ~TALLOC_FLAG_FREE;
1884 tc = (struct talloc_chunk *)new_ptr;
1885 tc->flags &= ~TALLOC_FLAG_FREE;
1887 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1890 tc->parent->child = tc;
1893 tc->child->parent = tc;
1897 tc->prev->next = tc;
1900 tc->next->prev = tc;
1903 if (new_size > old_size) {
1904 talloc_memlimit_grow(tc->limit, new_size - old_size);
1905 } else if (new_size < old_size) {
1906 talloc_memlimit_shrink(tc->limit, old_size - new_size);
1910 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1912 return TC_PTR_FROM_CHUNK(tc);
1916 a wrapper around talloc_steal() for situations where you are moving a pointer
1917 between two structures, and want the old pointer to be set to NULL
1919 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
1921 const void **pptr = discard_const_p(const void *,_pptr);
1922 void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1927 enum talloc_mem_count_type {
1933 static inline size_t _talloc_total_mem_internal(const void *ptr,
1934 enum talloc_mem_count_type type,
1935 struct talloc_memlimit *old_limit,
1936 struct talloc_memlimit *new_limit)
1939 struct talloc_chunk *c, *tc;
1948 tc = talloc_chunk_from_ptr(ptr);
1950 if (old_limit || new_limit) {
1951 if (tc->limit && tc->limit->upper == old_limit) {
1952 tc->limit->upper = new_limit;
1956 /* optimize in the memlimits case */
1957 if (type == TOTAL_MEM_LIMIT &&
1958 tc->limit != NULL &&
1959 tc->limit != old_limit &&
1960 tc->limit->parent == tc) {
1961 return tc->limit->cur_size;
1964 if (tc->flags & TALLOC_FLAG_LOOP) {
1968 tc->flags |= TALLOC_FLAG_LOOP;
1970 if (old_limit || new_limit) {
1971 if (old_limit == tc->limit) {
1972 tc->limit = new_limit;
1977 case TOTAL_MEM_SIZE:
1978 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1982 case TOTAL_MEM_BLOCKS:
1985 case TOTAL_MEM_LIMIT:
1986 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1988 * Don't count memory allocated from a pool
1989 * when calculating limits. Only count the
1992 if (!(tc->flags & TALLOC_FLAG_POOLMEM)) {
1993 if (tc->flags & TALLOC_FLAG_POOL) {
1995 * If this is a pool, the allocated
1996 * size is in the pool header, and
1997 * remember to add in the prefix
2000 struct talloc_pool_hdr *pool_hdr
2001 = talloc_pool_from_chunk(tc);
2002 total = pool_hdr->poolsize +
2006 total = tc->size + TC_HDR_SIZE;
2012 for (c = tc->child; c; c = c->next) {
2013 total += _talloc_total_mem_internal(TC_PTR_FROM_CHUNK(c), type,
2014 old_limit, new_limit);
2017 tc->flags &= ~TALLOC_FLAG_LOOP;
2023 return the total size of a talloc pool (subtree)
2025 _PUBLIC_ size_t talloc_total_size(const void *ptr)
2027 return _talloc_total_mem_internal(ptr, TOTAL_MEM_SIZE, NULL, NULL);
2031 return the total number of blocks in a talloc pool (subtree)
2033 _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
2035 return _talloc_total_mem_internal(ptr, TOTAL_MEM_BLOCKS, NULL, NULL);
2039 return the number of external references to a pointer
2041 _PUBLIC_ size_t talloc_reference_count(const void *ptr)
2043 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
2044 struct talloc_reference_handle *h;
2047 for (h=tc->refs;h;h=h->next) {
2054 report on memory usage by all children of a pointer, giving a full tree view
2056 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
2057 void (*callback)(const void *ptr,
2058 int depth, int max_depth,
2060 void *private_data),
2063 struct talloc_chunk *c, *tc;
2068 if (ptr == NULL) return;
2070 tc = talloc_chunk_from_ptr(ptr);
2072 if (tc->flags & TALLOC_FLAG_LOOP) {
2076 callback(ptr, depth, max_depth, 0, private_data);
2078 if (max_depth >= 0 && depth >= max_depth) {
2082 tc->flags |= TALLOC_FLAG_LOOP;
2083 for (c=tc->child;c;c=c->next) {
2084 if (c->name == TALLOC_MAGIC_REFERENCE) {
2085 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
2086 callback(h->ptr, depth + 1, max_depth, 1, private_data);
2088 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
2091 tc->flags &= ~TALLOC_FLAG_LOOP;
2094 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
2096 const char *name = __talloc_get_name(ptr);
2097 struct talloc_chunk *tc;
2098 FILE *f = (FILE *)_f;
2101 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
2105 tc = talloc_chunk_from_ptr(ptr);
2106 if (tc->limit && tc->limit->parent == tc) {
2107 fprintf(f, "%*s%-30s is a memlimit context"
2108 " (max_size = %lu bytes, cur_size = %lu bytes)\n",
2111 (unsigned long)tc->limit->max_size,
2112 (unsigned long)tc->limit->cur_size);
2116 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
2117 (max_depth < 0 ? "full " :""), name,
2118 (unsigned long)talloc_total_size(ptr),
2119 (unsigned long)talloc_total_blocks(ptr));
2123 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
2126 (unsigned long)talloc_total_size(ptr),
2127 (unsigned long)talloc_total_blocks(ptr),
2128 (int)talloc_reference_count(ptr), ptr);
2131 fprintf(f, "content: ");
2132 if (talloc_total_size(ptr)) {
2133 int tot = talloc_total_size(ptr);
2136 for (i = 0; i < tot; i++) {
2137 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
2138 fprintf(f, "%c", ((char *)ptr)[i]);
2140 fprintf(f, "~%02x", ((char *)ptr)[i]);
2149 report on memory usage by all children of a pointer, giving a full tree view
2151 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
2154 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
2160 report on memory usage by all children of a pointer, giving a full tree view
2162 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
2164 talloc_report_depth_file(ptr, 0, -1, f);
2168 report on memory usage by all children of a pointer
2170 _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
2172 talloc_report_depth_file(ptr, 0, 1, f);
2176 report on any memory hanging off the null context
2178 static void talloc_report_null(void)
2180 if (talloc_total_size(null_context) != 0) {
2181 talloc_report(null_context, stderr);
2186 report on any memory hanging off the null context
2188 static void talloc_report_null_full(void)
2190 if (talloc_total_size(null_context) != 0) {
2191 talloc_report_full(null_context, stderr);
2196 enable tracking of the NULL context
2198 _PUBLIC_ void talloc_enable_null_tracking(void)
2200 if (null_context == NULL) {
2201 null_context = _talloc_named_const(NULL, 0, "null_context");
2202 if (autofree_context != NULL) {
2203 talloc_reparent(NULL, null_context, autofree_context);
2209 enable tracking of the NULL context, not moving the autofree context
2210 into the NULL context. This is needed for the talloc testsuite
2212 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
2214 if (null_context == NULL) {
2215 null_context = _talloc_named_const(NULL, 0, "null_context");
2220 disable tracking of the NULL context
2222 _PUBLIC_ void talloc_disable_null_tracking(void)
2224 if (null_context != NULL) {
2225 /* we have to move any children onto the real NULL
2227 struct talloc_chunk *tc, *tc2;
2228 tc = talloc_chunk_from_ptr(null_context);
2229 for (tc2 = tc->child; tc2; tc2=tc2->next) {
2230 if (tc2->parent == tc) tc2->parent = NULL;
2231 if (tc2->prev == tc) tc2->prev = NULL;
2233 for (tc2 = tc->next; tc2; tc2=tc2->next) {
2234 if (tc2->parent == tc) tc2->parent = NULL;
2235 if (tc2->prev == tc) tc2->prev = NULL;
2240 talloc_free(null_context);
2241 null_context = NULL;
2245 enable leak reporting on exit
2247 _PUBLIC_ void talloc_enable_leak_report(void)
2249 talloc_enable_null_tracking();
2250 atexit(talloc_report_null);
2254 enable full leak reporting on exit
2256 _PUBLIC_ void talloc_enable_leak_report_full(void)
2258 talloc_enable_null_tracking();
2259 atexit(talloc_report_null_full);
2263 talloc and zero memory.
2265 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
2267 void *p = _talloc_named_const(ctx, size, name);
2270 memset(p, '\0', size);
2277 memdup with a talloc.
2279 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
2281 void *newp = _talloc_named_const(t, size, name);
2284 memcpy(newp, p, size);
2290 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
2294 ret = (char *)__talloc(t, len + 1);
2295 if (unlikely(!ret)) return NULL;
2297 memcpy(ret, p, len);
2300 _talloc_set_name_const(ret, ret);
2305 strdup with a talloc
2307 _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
2309 if (unlikely(!p)) return NULL;
2310 return __talloc_strlendup(t, p, strlen(p));
2314 strndup with a talloc
2316 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
2318 if (unlikely(!p)) return NULL;
2319 return __talloc_strlendup(t, p, strnlen(p, n));
2322 static inline char *__talloc_strlendup_append(char *s, size_t slen,
2323 const char *a, size_t alen)
2327 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
2328 if (unlikely(!ret)) return NULL;
2330 /* append the string and the trailing \0 */
2331 memcpy(&ret[slen], a, alen);
2334 _talloc_set_name_const(ret, ret);
2339 * Appends at the end of the string.
2341 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
2344 return talloc_strdup(NULL, a);
2351 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
2355 * Appends at the end of the talloc'ed buffer,
2356 * not the end of the string.
2358 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
2363 return talloc_strdup(NULL, a);
2370 slen = talloc_get_size(s);
2371 if (likely(slen > 0)) {
2375 return __talloc_strlendup_append(s, slen, a, strlen(a));
2379 * Appends at the end of the string.
2381 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
2384 return talloc_strndup(NULL, a, n);
2391 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
2395 * Appends at the end of the talloc'ed buffer,
2396 * not the end of the string.
2398 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
2403 return talloc_strndup(NULL, a, n);
2410 slen = talloc_get_size(s);
2411 if (likely(slen > 0)) {
2415 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
2418 #ifndef HAVE_VA_COPY
2419 #ifdef HAVE___VA_COPY
2420 #define va_copy(dest, src) __va_copy(dest, src)
2422 #define va_copy(dest, src) (dest) = (src)
2426 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
2433 /* this call looks strange, but it makes it work on older solaris boxes */
2435 len = vsnprintf(buf, sizeof(buf), fmt, ap2);
2437 if (unlikely(len < 0)) {
2441 ret = (char *)__talloc(t, len+1);
2442 if (unlikely(!ret)) return NULL;
2444 if (len < sizeof(buf)) {
2445 memcpy(ret, buf, len+1);
2448 vsnprintf(ret, len+1, fmt, ap2);
2452 _talloc_set_name_const(ret, ret);
2458 Perform string formatting, and return a pointer to newly allocated
2459 memory holding the result, inside a memory pool.
2461 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
2467 ret = talloc_vasprintf(t, fmt, ap);
2472 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2473 const char *fmt, va_list ap)
2474 PRINTF_ATTRIBUTE(3,0);
2476 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2477 const char *fmt, va_list ap)
2484 alen = vsnprintf(&c, 1, fmt, ap2);
2488 /* Either the vsnprintf failed or the format resulted in
2489 * no characters being formatted. In the former case, we
2490 * ought to return NULL, in the latter we ought to return
2491 * the original string. Most current callers of this
2492 * function expect it to never return NULL.
2497 s = talloc_realloc(NULL, s, char, slen + alen + 1);
2498 if (!s) return NULL;
2501 vsnprintf(s + slen, alen + 1, fmt, ap2);
2504 _talloc_set_name_const(s, s);
2509 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2510 * and return @p s, which may have moved. Good for gradually
2511 * accumulating output into a string buffer. Appends at the end
2514 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
2517 return talloc_vasprintf(NULL, fmt, ap);
2520 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
2524 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2525 * and return @p s, which may have moved. Always appends at the
2526 * end of the talloc'ed buffer, not the end of the string.
2528 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
2533 return talloc_vasprintf(NULL, fmt, ap);
2536 slen = talloc_get_size(s);
2537 if (likely(slen > 0)) {
2541 return __talloc_vaslenprintf_append(s, slen, 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
2547 into a string buffer.
2549 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
2554 s = talloc_vasprintf_append(s, fmt, ap);
2560 Realloc @p s to append the formatted result of @p fmt and return @p
2561 s, which may have moved. Good for gradually accumulating output
2564 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
2569 s = talloc_vasprintf_append_buffer(s, fmt, ap);
2575 alloc an array, checking for integer overflow in the array size
2577 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2579 if (count >= MAX_TALLOC_SIZE/el_size) {
2582 return _talloc_named_const(ctx, el_size * count, name);
2586 alloc an zero array, checking for integer overflow in the array size
2588 _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2590 if (count >= MAX_TALLOC_SIZE/el_size) {
2593 return _talloc_zero(ctx, el_size * count, name);
2597 realloc an array, checking for integer overflow in the array size
2599 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
2601 if (count >= MAX_TALLOC_SIZE/el_size) {
2604 return _talloc_realloc(ctx, ptr, el_size * count, name);
2608 a function version of talloc_realloc(), so it can be passed as a function pointer
2609 to libraries that want a realloc function (a realloc function encapsulates
2610 all the basic capabilities of an allocation library, which is why this is useful)
2612 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
2614 return _talloc_realloc(context, ptr, size, NULL);
2618 static int talloc_autofree_destructor(void *ptr)
2620 autofree_context = NULL;
2624 static void talloc_autofree(void)
2626 talloc_free(autofree_context);
2630 return a context which will be auto-freed on exit
2631 this is useful for reducing the noise in leak reports
2633 _PUBLIC_ void *talloc_autofree_context(void)
2635 if (autofree_context == NULL) {
2636 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
2637 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
2638 atexit(talloc_autofree);
2640 return autofree_context;
2643 _PUBLIC_ size_t talloc_get_size(const void *context)
2645 struct talloc_chunk *tc;
2647 if (context == NULL) {
2648 context = null_context;
2650 if (context == NULL) {
2654 tc = talloc_chunk_from_ptr(context);
2660 find a parent of this context that has the given name, if any
2662 _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
2664 struct talloc_chunk *tc;
2666 if (context == NULL) {
2670 tc = talloc_chunk_from_ptr(context);
2672 if (tc->name && strcmp(tc->name, name) == 0) {
2673 return TC_PTR_FROM_CHUNK(tc);
2675 while (tc && tc->prev) tc = tc->prev;
2684 show the parentage of a context
2686 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
2688 struct talloc_chunk *tc;
2690 if (context == NULL) {
2691 fprintf(file, "talloc no parents for NULL\n");
2695 tc = talloc_chunk_from_ptr(context);
2696 fprintf(file, "talloc parents of '%s'\n", __talloc_get_name(context));
2698 fprintf(file, "\t'%s'\n", __talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
2699 while (tc && tc->prev) tc = tc->prev;
2708 return 1 if ptr is a parent of context
2710 static int _talloc_is_parent(const void *context, const void *ptr, int depth)
2712 struct talloc_chunk *tc;
2714 if (context == NULL) {
2718 tc = talloc_chunk_from_ptr(context);
2723 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2724 while (tc && tc->prev) tc = tc->prev;
2734 return 1 if ptr is a parent of context
2736 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2738 return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);
2742 return the total size of memory used by this context and all children
2744 static inline size_t _talloc_total_limit_size(const void *ptr,
2745 struct talloc_memlimit *old_limit,
2746 struct talloc_memlimit *new_limit)
2748 return _talloc_total_mem_internal(ptr, TOTAL_MEM_LIMIT,
2749 old_limit, new_limit);
2752 static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size)
2754 struct talloc_memlimit *l;
2756 for (l = limit; l != NULL; l = l->upper) {
2757 if (l->max_size != 0 &&
2758 ((l->max_size <= l->cur_size) ||
2759 (l->max_size - l->cur_size < size))) {
2768 Update memory limits when freeing a talloc_chunk.
2770 static void talloc_memlimit_update_on_free(struct talloc_chunk *tc)
2772 size_t limit_shrink_size;
2779 * Pool entries don't count. Only the pools
2780 * themselves are counted as part of the memory
2781 * limits. Note that this also takes care of
2782 * nested pools which have both flags
2783 * TALLOC_FLAG_POOLMEM|TALLOC_FLAG_POOL set.
2785 if (tc->flags & TALLOC_FLAG_POOLMEM) {
2790 * If we are part of a memory limited context hierarchy
2791 * we need to subtract the memory used from the counters
2794 limit_shrink_size = tc->size+TC_HDR_SIZE;
2797 * If we're deallocating a pool, take into
2798 * account the prefix size added for the pool.
2801 if (tc->flags & TALLOC_FLAG_POOL) {
2802 limit_shrink_size += TP_HDR_SIZE;
2805 talloc_memlimit_shrink(tc->limit, limit_shrink_size);
2807 if (tc->limit->parent == tc) {
2815 Increase memory limit accounting after a malloc/realloc.
2817 static void talloc_memlimit_grow(struct talloc_memlimit *limit,
2820 struct talloc_memlimit *l;
2822 for (l = limit; l != NULL; l = l->upper) {
2823 size_t new_cur_size = l->cur_size + size;
2824 if (new_cur_size < l->cur_size) {
2825 talloc_abort("logic error in talloc_memlimit_grow\n");
2828 l->cur_size = new_cur_size;
2833 Decrease memory limit accounting after a free/realloc.
2835 static void talloc_memlimit_shrink(struct talloc_memlimit *limit,
2838 struct talloc_memlimit *l;
2840 for (l = limit; l != NULL; l = l->upper) {
2841 if (l->cur_size < size) {
2842 talloc_abort("logic error in talloc_memlimit_shrink\n");
2845 l->cur_size = l->cur_size - size;
2849 _PUBLIC_ int talloc_set_memlimit(const void *ctx, size_t max_size)
2851 struct talloc_chunk *tc = talloc_chunk_from_ptr(ctx);
2852 struct talloc_memlimit *orig_limit;
2853 struct talloc_memlimit *limit = NULL;
2855 if (tc->limit && tc->limit->parent == tc) {
2856 tc->limit->max_size = max_size;
2859 orig_limit = tc->limit;
2861 limit = malloc(sizeof(struct talloc_memlimit));
2862 if (limit == NULL) {
2866 limit->max_size = max_size;
2867 limit->cur_size = _talloc_total_limit_size(ctx, tc->limit, limit);
2870 limit->upper = orig_limit;
2872 limit->upper = NULL;