2 Samba Unix SMB/CIFS implementation.
4 Samba trivial allocation library - new interface
6 NOTE: Please read talloc_guide.txt for full documentation
8 Copyright (C) Andrew Tridgell 2004
9 Copyright (C) Stefan Metzmacher 2006
11 ** NOTE! The following LGPL license applies to the talloc
12 ** library. This does NOT imply that all of Samba is released
15 This library is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Lesser General Public
17 License as published by the Free Software Foundation; either
18 version 3 of the License, or (at your option) any later version.
20 This library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Lesser General Public License for more details.
25 You should have received a copy of the GNU Lesser General Public
26 License along with this library; if not, see <http://www.gnu.org/licenses/>.
30 inspired by http://swapped.cc/halloc/
36 #ifdef TALLOC_BUILD_VERSION_MAJOR
37 #if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)
38 #error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"
42 #ifdef TALLOC_BUILD_VERSION_MINOR
43 #if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)
44 #error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"
48 /* Special macros that are no-ops except when run under Valgrind on
49 * x86. They've moved a little bit from valgrind 1.0.4 to 1.9.4 */
50 #ifdef HAVE_VALGRIND_MEMCHECK_H
51 /* memcheck.h includes valgrind.h */
52 #include <valgrind/memcheck.h>
53 #elif defined(HAVE_VALGRIND_H)
57 /* use this to force every realloc to change the pointer, to stress test
58 code that might not cope */
59 #define ALWAYS_REALLOC 0
62 #define MAX_TALLOC_SIZE 0x10000000
63 #define TALLOC_MAGIC_BASE 0xe814ec70
64 #define TALLOC_MAGIC ( \
66 (TALLOC_VERSION_MAJOR << 12) + \
67 (TALLOC_VERSION_MINOR << 4) \
70 #define TALLOC_FLAG_FREE 0x01
71 #define TALLOC_FLAG_LOOP 0x02
72 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
73 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
75 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
77 /* by default we abort when given a bad pointer (such as when talloc_free() is called
78 on a pointer that came from malloc() */
80 #define TALLOC_ABORT(reason) abort()
83 #ifndef discard_const_p
84 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
85 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
87 # define discard_const_p(type, ptr) ((type *)(ptr))
91 /* these macros gain us a few percent of speed on gcc */
93 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
94 as its first argument */
96 #define likely(x) __builtin_expect(!!(x), 1)
99 #define unlikely(x) __builtin_expect(!!(x), 0)
103 #define likely(x) (x)
106 #define unlikely(x) (x)
110 /* this null_context is only used if talloc_enable_leak_report() or
111 talloc_enable_leak_report_full() is called, otherwise it remains
114 static void *null_context;
115 static void *autofree_context;
117 /* used to enable fill of memory on free, which can be useful for
118 * catching use after free errors when valgrind is too slow
126 #define TALLOC_FILL_ENV "TALLOC_FREE_FILL"
129 * do not wipe the header, to allow the
130 * double-free logic to still work
132 #define TC_INVALIDATE_FULL_FILL_CHUNK(_tc) do { \
133 if (unlikely(talloc_fill.enabled)) { \
134 size_t _flen = (_tc)->size; \
135 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
136 memset(_fptr, talloc_fill.fill_value, _flen); \
140 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
141 /* Mark the whole chunk as not accessable */
142 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { \
143 size_t _flen = TC_HDR_SIZE + (_tc)->size; \
144 char *_fptr = (char *)(_tc); \
145 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
148 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { } while (0)
151 #define TC_INVALIDATE_FULL_CHUNK(_tc) do { \
152 TC_INVALIDATE_FULL_FILL_CHUNK(_tc); \
153 TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc); \
156 #define TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
157 if (unlikely(talloc_fill.enabled)) { \
158 size_t _flen = (_tc)->size - (_new_size); \
159 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
160 _fptr += (_new_size); \
161 memset(_fptr, talloc_fill.fill_value, _flen); \
165 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
166 /* Mark the unused bytes not accessable */
167 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
168 size_t _flen = (_tc)->size - (_new_size); \
169 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
170 _fptr += (_new_size); \
171 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
174 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
177 #define TC_INVALIDATE_SHRINK_CHUNK(_tc, _new_size) do { \
178 TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size); \
179 TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
182 #define TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
183 if (unlikely(talloc_fill.enabled)) { \
184 size_t _flen = (_tc)->size - (_new_size); \
185 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
186 _fptr += (_new_size); \
187 memset(_fptr, talloc_fill.fill_value, _flen); \
191 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
192 /* Mark the unused bytes as undefined */
193 #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
194 size_t _flen = (_tc)->size - (_new_size); \
195 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
196 _fptr += (_new_size); \
197 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
200 #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
203 #define TC_UNDEFINE_SHRINK_CHUNK(_tc, _new_size) do { \
204 TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size); \
205 TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
208 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
209 /* Mark the new bytes as undefined */
210 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { \
211 size_t _old_used = TC_HDR_SIZE + (_tc)->size; \
212 size_t _new_used = TC_HDR_SIZE + (_new_size); \
213 size_t _flen = _new_used - _old_used; \
214 char *_fptr = _old_used + (char *)(_tc); \
215 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
218 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
221 #define TC_UNDEFINE_GROW_CHUNK(_tc, _new_size) do { \
222 TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
225 struct talloc_reference_handle {
226 struct talloc_reference_handle *next, *prev;
228 const char *location;
231 struct talloc_memlimit {
232 struct talloc_chunk *parent;
233 struct talloc_memlimit *upper;
238 static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size);
239 static inline void talloc_memlimit_grow(struct talloc_memlimit *limit,
241 static inline void talloc_memlimit_shrink(struct talloc_memlimit *limit,
243 static inline void talloc_memlimit_update_on_free(struct talloc_chunk *tc);
245 static inline void _talloc_set_name_const(const void *ptr, const char *name);
247 typedef int (*talloc_destructor_t)(void *);
249 struct talloc_pool_hdr;
251 struct talloc_chunk {
252 struct talloc_chunk *next, *prev;
253 struct talloc_chunk *parent, *child;
254 struct talloc_reference_handle *refs;
255 talloc_destructor_t destructor;
262 * if 'limit' is set it means all *new* children of the context will
263 * be limited to a total aggregate size ox max_size for memory
265 * cur_size is used to keep track of the current use
267 struct talloc_memlimit *limit;
270 * For members of a pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
271 * is a pointer to the struct talloc_chunk of the pool that it was
272 * allocated from. This way children can quickly find the pool to chew
275 struct talloc_pool_hdr *pool;
278 /* 16 byte alignment seems to keep everyone happy */
279 #define TC_ALIGN16(s) (((s)+15)&~15)
280 #define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
281 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
283 _PUBLIC_ int talloc_version_major(void)
285 return TALLOC_VERSION_MAJOR;
288 _PUBLIC_ int talloc_version_minor(void)
290 return TALLOC_VERSION_MINOR;
293 static void (*talloc_log_fn)(const char *message);
295 _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
297 talloc_log_fn = log_fn;
300 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
301 static void talloc_log(const char *fmt, ...)
306 if (!talloc_log_fn) {
311 message = talloc_vasprintf(NULL, fmt, ap);
314 talloc_log_fn(message);
315 talloc_free(message);
318 static void talloc_log_stderr(const char *message)
320 fprintf(stderr, "%s", message);
323 _PUBLIC_ void talloc_set_log_stderr(void)
325 talloc_set_log_fn(talloc_log_stderr);
328 static void (*talloc_abort_fn)(const char *reason);
330 _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
332 talloc_abort_fn = abort_fn;
335 static void talloc_abort(const char *reason)
337 talloc_log("%s\n", reason);
339 if (!talloc_abort_fn) {
340 TALLOC_ABORT(reason);
343 talloc_abort_fn(reason);
346 static void talloc_abort_magic(unsigned magic)
348 unsigned striped = magic - TALLOC_MAGIC_BASE;
349 unsigned major = (striped & 0xFFFFF000) >> 12;
350 unsigned minor = (striped & 0x00000FF0) >> 4;
351 talloc_log("Bad talloc magic[0x%08X/%u/%u] expected[0x%08X/%u/%u]\n",
353 TALLOC_MAGIC, TALLOC_VERSION_MAJOR, TALLOC_VERSION_MINOR);
354 talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
357 static void talloc_abort_access_after_free(void)
359 talloc_abort("Bad talloc magic value - access after free");
362 static void talloc_abort_unknown_value(void)
364 talloc_abort("Bad talloc magic value - unknown value");
367 /* panic if we get a bad magic value */
368 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
370 const char *pp = (const char *)ptr;
371 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
372 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
373 if ((tc->flags & (~0xFFF)) == TALLOC_MAGIC_BASE) {
374 talloc_abort_magic(tc->flags & (~0xF));
378 if (tc->flags & TALLOC_FLAG_FREE) {
379 talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
380 talloc_abort_access_after_free();
383 talloc_abort_unknown_value();
390 /* hook into the front of the list */
391 #define _TLIST_ADD(list, p) \
395 (p)->next = (p)->prev = NULL; \
397 (list)->prev = (p); \
398 (p)->next = (list); \
404 /* remove an element from a list - element doesn't have to be in list. */
405 #define _TLIST_REMOVE(list, p) \
407 if ((p) == (list)) { \
408 (list) = (p)->next; \
409 if (list) (list)->prev = NULL; \
411 if ((p)->prev) (p)->prev->next = (p)->next; \
412 if ((p)->next) (p)->next->prev = (p)->prev; \
414 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
419 return the parent chunk of a pointer
421 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
423 struct talloc_chunk *tc;
425 if (unlikely(ptr == NULL)) {
429 tc = talloc_chunk_from_ptr(ptr);
430 while (tc->prev) tc=tc->prev;
435 _PUBLIC_ void *talloc_parent(const void *ptr)
437 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
438 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
444 _PUBLIC_ const char *talloc_parent_name(const void *ptr)
446 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
447 return tc? tc->name : NULL;
451 A pool carries an in-pool object count count in the first 16 bytes.
452 bytes. This is done to support talloc_steal() to a parent outside of the
453 pool. The count includes the pool itself, so a talloc_free() on a pool will
454 only destroy the pool if the count has dropped to zero. A talloc_free() of a
455 pool member will reduce the count, and eventually also call free(3) on the
458 The object count is not put into "struct talloc_chunk" because it is only
459 relevant for talloc pools and the alignment to 16 bytes would increase the
460 memory footprint of each talloc chunk by those 16 bytes.
463 struct talloc_pool_hdr {
465 unsigned int object_count;
469 #define TP_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_pool_hdr))
471 static inline struct talloc_pool_hdr *talloc_pool_from_chunk(struct talloc_chunk *c)
473 return (struct talloc_pool_hdr *)((char *)c - TP_HDR_SIZE);
476 static inline struct talloc_chunk *talloc_chunk_from_pool(struct talloc_pool_hdr *h)
478 return (struct talloc_chunk *)((char *)h + TP_HDR_SIZE);
481 static inline void *tc_pool_end(struct talloc_pool_hdr *pool_hdr)
483 struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);
484 return (char *)tc + TC_HDR_SIZE + pool_hdr->poolsize;
487 static inline size_t tc_pool_space_left(struct talloc_pool_hdr *pool_hdr)
489 return (char *)tc_pool_end(pool_hdr) - (char *)pool_hdr->end;
492 /* If tc is inside a pool, this gives the next neighbour. */
493 static inline void *tc_next_chunk(struct talloc_chunk *tc)
495 return (char *)tc + TC_ALIGN16(TC_HDR_SIZE + tc->size);
498 static inline void *tc_pool_first_chunk(struct talloc_pool_hdr *pool_hdr)
500 struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);
501 return tc_next_chunk(tc);
504 /* Mark the whole remaining pool as not accessable */
505 static inline void tc_invalidate_pool(struct talloc_pool_hdr *pool_hdr)
507 size_t flen = tc_pool_space_left(pool_hdr);
509 if (unlikely(talloc_fill.enabled)) {
510 memset(pool_hdr->end, talloc_fill.fill_value, flen);
513 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
514 VALGRIND_MAKE_MEM_NOACCESS(pool_hdr->end, flen);
522 static inline struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
523 size_t size, size_t prefix_len)
525 struct talloc_pool_hdr *pool_hdr = NULL;
527 struct talloc_chunk *result;
530 if (parent == NULL) {
534 if (parent->flags & TALLOC_FLAG_POOL) {
535 pool_hdr = talloc_pool_from_chunk(parent);
537 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
538 pool_hdr = parent->pool;
541 if (pool_hdr == NULL) {
545 space_left = tc_pool_space_left(pool_hdr);
548 * Align size to 16 bytes
550 chunk_size = TC_ALIGN16(size + prefix_len);
552 if (space_left < chunk_size) {
556 result = (struct talloc_chunk *)((char *)pool_hdr->end + prefix_len);
558 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
559 VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, chunk_size);
562 pool_hdr->end = (void *)((char *)pool_hdr->end + chunk_size);
564 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
565 result->pool = pool_hdr;
567 pool_hdr->object_count++;
573 Allocate a bit of memory as a child of an existing pointer
575 static inline void *__talloc_with_prefix(const void *context, size_t size,
578 struct talloc_chunk *tc = NULL;
579 struct talloc_memlimit *limit = NULL;
580 size_t total_len = TC_HDR_SIZE + size + prefix_len;
582 if (unlikely(context == NULL)) {
583 context = null_context;
586 if (unlikely(size >= MAX_TALLOC_SIZE)) {
590 if (unlikely(total_len < TC_HDR_SIZE)) {
594 if (context != NULL) {
595 struct talloc_chunk *ptc = talloc_chunk_from_ptr(context);
597 if (ptc->limit != NULL) {
601 tc = talloc_alloc_pool(ptc, TC_HDR_SIZE+size, prefix_len);
608 * Only do the memlimit check/update on actual allocation.
610 if (!talloc_memlimit_check(limit, total_len)) {
615 ptr = malloc(total_len);
616 if (unlikely(ptr == NULL)) {
619 tc = (struct talloc_chunk *)(ptr + prefix_len);
620 tc->flags = TALLOC_MAGIC;
623 talloc_memlimit_grow(limit, total_len);
628 tc->destructor = NULL;
633 if (likely(context)) {
634 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
637 parent->child->parent = NULL;
638 tc->next = parent->child;
647 tc->next = tc->prev = tc->parent = NULL;
650 return TC_PTR_FROM_CHUNK(tc);
653 static inline void *__talloc(const void *context, size_t size)
655 return __talloc_with_prefix(context, size, 0);
659 * Create a talloc pool
662 static inline void *_talloc_pool(const void *context, size_t size)
664 struct talloc_chunk *tc;
665 struct talloc_pool_hdr *pool_hdr;
668 result = __talloc_with_prefix(context, size, TP_HDR_SIZE);
670 if (unlikely(result == NULL)) {
674 tc = talloc_chunk_from_ptr(result);
675 pool_hdr = talloc_pool_from_chunk(tc);
677 tc->flags |= TALLOC_FLAG_POOL;
680 pool_hdr->object_count = 1;
681 pool_hdr->end = result;
682 pool_hdr->poolsize = size;
684 tc_invalidate_pool(pool_hdr);
689 _PUBLIC_ void *talloc_pool(const void *context, size_t size)
691 return _talloc_pool(context, size);
695 * Create a talloc pool correctly sized for a basic size plus
696 * a number of subobjects whose total size is given. Essentially
697 * a custom allocator for talloc to reduce fragmentation.
700 _PUBLIC_ void *_talloc_pooled_object(const void *ctx,
702 const char *type_name,
703 unsigned num_subobjects,
704 size_t total_subobjects_size)
706 size_t poolsize, subobjects_slack, tmp;
707 struct talloc_chunk *tc;
708 struct talloc_pool_hdr *pool_hdr;
711 poolsize = type_size + total_subobjects_size;
713 if ((poolsize < type_size) || (poolsize < total_subobjects_size)) {
717 if (num_subobjects == UINT_MAX) {
720 num_subobjects += 1; /* the object body itself */
723 * Alignment can increase the pool size by at most 15 bytes per object
724 * plus alignment for the object itself
726 subobjects_slack = (TC_HDR_SIZE + TP_HDR_SIZE + 15) * num_subobjects;
727 if (subobjects_slack < num_subobjects) {
731 tmp = poolsize + subobjects_slack;
732 if ((tmp < poolsize) || (tmp < subobjects_slack)) {
737 ret = _talloc_pool(ctx, poolsize);
742 tc = talloc_chunk_from_ptr(ret);
743 tc->size = type_size;
745 pool_hdr = talloc_pool_from_chunk(tc);
747 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
748 VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, type_size);
751 pool_hdr->end = ((char *)pool_hdr->end + TC_ALIGN16(type_size));
753 _talloc_set_name_const(ret, type_name);
761 setup a destructor to be called on free of a pointer
762 the destructor should return 0 on success, or -1 on failure.
763 if the destructor fails then the free is failed, and the memory can
764 be continued to be used
766 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
768 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
769 tc->destructor = destructor;
773 increase the reference count on a piece of memory.
775 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
777 if (unlikely(!talloc_reference(null_context, ptr))) {
784 helper for talloc_reference()
786 this is referenced by a function pointer and should not be inline
788 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
790 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
791 _TLIST_REMOVE(ptr_tc->refs, handle);
796 more efficient way to add a name to a pointer - the name must point to a
799 static inline void _talloc_set_name_const(const void *ptr, const char *name)
801 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
806 internal talloc_named_const()
808 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
812 ptr = __talloc(context, size);
813 if (unlikely(ptr == NULL)) {
817 _talloc_set_name_const(ptr, name);
823 make a secondary reference to a pointer, hanging off the given context.
824 the pointer remains valid until both the original caller and this given
827 the major use for this is when two different structures need to reference the
828 same underlying data, and you want to be able to free the two instances separately,
831 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
833 struct talloc_chunk *tc;
834 struct talloc_reference_handle *handle;
835 if (unlikely(ptr == NULL)) return NULL;
837 tc = talloc_chunk_from_ptr(ptr);
838 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
839 sizeof(struct talloc_reference_handle),
840 TALLOC_MAGIC_REFERENCE);
841 if (unlikely(handle == NULL)) return NULL;
843 /* note that we hang the destructor off the handle, not the
844 main context as that allows the caller to still setup their
845 own destructor on the context if they want to */
846 talloc_set_destructor(handle, talloc_reference_destructor);
847 handle->ptr = discard_const_p(void, ptr);
848 handle->location = location;
849 _TLIST_ADD(tc->refs, handle);
853 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
855 static inline void _talloc_free_poolmem(struct talloc_chunk *tc,
856 const char *location)
858 struct talloc_pool_hdr *pool;
859 struct talloc_chunk *pool_tc;
863 pool_tc = talloc_chunk_from_pool(pool);
864 next_tc = tc_next_chunk(tc);
866 tc->flags |= TALLOC_FLAG_FREE;
868 /* we mark the freed memory with where we called the free
869 * from. This means on a double free error we can report where
870 * the first free came from
874 TC_INVALIDATE_FULL_CHUNK(tc);
876 if (unlikely(pool->object_count == 0)) {
877 talloc_abort("Pool object count zero!");
881 pool->object_count--;
883 if (unlikely(pool->object_count == 1
884 && !(pool_tc->flags & TALLOC_FLAG_FREE))) {
886 * if there is just one object left in the pool
887 * and pool->flags does not have TALLOC_FLAG_FREE,
888 * it means this is the pool itself and
889 * the rest is available for new objects
892 pool->end = tc_pool_first_chunk(pool);
893 tc_invalidate_pool(pool);
897 if (unlikely(pool->object_count == 0)) {
899 * we mark the freed memory with where we called the free
900 * from. This means on a double free error we can report where
901 * the first free came from
903 pool_tc->name = location;
905 if (pool_tc->flags & TALLOC_FLAG_POOLMEM) {
906 _talloc_free_poolmem(pool_tc, location);
909 * The talloc_memlimit_update_on_free()
910 * call takes into account the
911 * prefix TP_HDR_SIZE allocated before
912 * the pool talloc_chunk.
914 talloc_memlimit_update_on_free(pool_tc);
915 TC_INVALIDATE_FULL_CHUNK(pool_tc);
921 if (pool->end == next_tc) {
923 * if pool->pool still points to end of
924 * 'tc' (which is stored in the 'next_tc' variable),
925 * we can reclaim the memory of 'tc'.
932 * Do nothing. The memory is just "wasted", waiting for the pool
933 * itself to be freed.
937 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
939 const char *location);
942 internal talloc_free call
944 static inline int _talloc_free_internal(void *ptr, const char *location)
946 struct talloc_chunk *tc;
949 if (unlikely(ptr == NULL)) {
953 /* possibly initialised the talloc fill value */
954 if (unlikely(!talloc_fill.initialised)) {
955 const char *fill = getenv(TALLOC_FILL_ENV);
957 talloc_fill.enabled = true;
958 talloc_fill.fill_value = strtoul(fill, NULL, 0);
960 talloc_fill.initialised = true;
963 tc = talloc_chunk_from_ptr(ptr);
965 if (unlikely(tc->refs)) {
967 /* check if this is a reference from a child or
968 * grandchild back to it's parent or grandparent
970 * in that case we need to remove the reference and
971 * call another instance of talloc_free() on the current
974 is_child = talloc_is_parent(tc->refs, ptr);
975 _talloc_free_internal(tc->refs, location);
977 return _talloc_free_internal(ptr, location);
982 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
983 /* we have a free loop - stop looping */
987 if (unlikely(tc->destructor)) {
988 talloc_destructor_t d = tc->destructor;
989 if (d == (talloc_destructor_t)-1) {
992 tc->destructor = (talloc_destructor_t)-1;
995 * Only replace the destructor pointer if
996 * calling the destructor didn't modify it.
998 if (tc->destructor == (talloc_destructor_t)-1) {
1003 tc->destructor = NULL;
1007 _TLIST_REMOVE(tc->parent->child, tc);
1008 if (tc->parent->child) {
1009 tc->parent->child->parent = tc->parent;
1012 if (tc->prev) tc->prev->next = tc->next;
1013 if (tc->next) tc->next->prev = tc->prev;
1014 tc->prev = tc->next = NULL;
1017 tc->flags |= TALLOC_FLAG_LOOP;
1019 _talloc_free_children_internal(tc, ptr, location);
1021 tc->flags |= TALLOC_FLAG_FREE;
1023 /* we mark the freed memory with where we called the free
1024 * from. This means on a double free error we can report where
1025 * the first free came from
1027 tc->name = location;
1029 if (tc->flags & TALLOC_FLAG_POOL) {
1030 struct talloc_pool_hdr *pool;
1032 pool = talloc_pool_from_chunk(tc);
1034 if (unlikely(pool->object_count == 0)) {
1035 talloc_abort("Pool object count zero!");
1039 pool->object_count--;
1041 if (likely(pool->object_count != 0)) {
1046 * With object_count==0, a pool becomes a normal piece of
1047 * memory to free. If it's allocated inside a pool, it needs
1048 * to be freed as poolmem, else it needs to be just freed.
1055 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1056 _talloc_free_poolmem(tc, location);
1060 talloc_memlimit_update_on_free(tc);
1062 TC_INVALIDATE_FULL_CHUNK(tc);
1067 static size_t _talloc_total_limit_size(const void *ptr,
1068 struct talloc_memlimit *old_limit,
1069 struct talloc_memlimit *new_limit);
1072 move a lump of memory from one talloc context to another return the
1073 ptr on success, or NULL if it could not be transferred.
1074 passing NULL as ptr will always return NULL with no side effects.
1076 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
1078 struct talloc_chunk *tc, *new_tc;
1079 size_t ctx_size = 0;
1081 if (unlikely(!ptr)) {
1085 if (unlikely(new_ctx == NULL)) {
1086 new_ctx = null_context;
1089 tc = talloc_chunk_from_ptr(ptr);
1091 if (tc->limit != NULL) {
1093 ctx_size = _talloc_total_limit_size(ptr, NULL, NULL);
1095 /* Decrement the memory limit from the source .. */
1096 talloc_memlimit_shrink(tc->limit->upper, ctx_size);
1098 if (tc->limit->parent == tc) {
1099 tc->limit->upper = NULL;
1105 if (unlikely(new_ctx == NULL)) {
1107 _TLIST_REMOVE(tc->parent->child, tc);
1108 if (tc->parent->child) {
1109 tc->parent->child->parent = tc->parent;
1112 if (tc->prev) tc->prev->next = tc->next;
1113 if (tc->next) tc->next->prev = tc->prev;
1116 tc->parent = tc->next = tc->prev = NULL;
1117 return discard_const_p(void, ptr);
1120 new_tc = talloc_chunk_from_ptr(new_ctx);
1122 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
1123 return discard_const_p(void, ptr);
1127 _TLIST_REMOVE(tc->parent->child, tc);
1128 if (tc->parent->child) {
1129 tc->parent->child->parent = tc->parent;
1132 if (tc->prev) tc->prev->next = tc->next;
1133 if (tc->next) tc->next->prev = tc->prev;
1134 tc->prev = tc->next = NULL;
1137 tc->parent = new_tc;
1138 if (new_tc->child) new_tc->child->parent = NULL;
1139 _TLIST_ADD(new_tc->child, tc);
1141 if (tc->limit || new_tc->limit) {
1142 ctx_size = _talloc_total_limit_size(ptr, tc->limit,
1144 /* .. and increment it in the destination. */
1145 if (new_tc->limit) {
1146 talloc_memlimit_grow(new_tc->limit, ctx_size);
1150 return discard_const_p(void, ptr);
1154 move a lump of memory from one talloc context to another return the
1155 ptr on success, or NULL if it could not be transferred.
1156 passing NULL as ptr will always return NULL with no side effects.
1158 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
1160 struct talloc_chunk *tc;
1162 if (unlikely(ptr == NULL)) {
1166 tc = talloc_chunk_from_ptr(ptr);
1168 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
1169 struct talloc_reference_handle *h;
1171 talloc_log("WARNING: talloc_steal with references at %s\n",
1174 for (h=tc->refs; h; h=h->next) {
1175 talloc_log("\treference at %s\n",
1181 /* this test is probably too expensive to have on in the
1182 normal build, but it useful for debugging */
1183 if (talloc_is_parent(new_ctx, ptr)) {
1184 talloc_log("WARNING: stealing into talloc child at %s\n", location);
1188 return _talloc_steal_internal(new_ctx, ptr);
1192 this is like a talloc_steal(), but you must supply the old
1193 parent. This resolves the ambiguity in a talloc_steal() which is
1194 called on a context that has more than one parent (via references)
1196 The old parent can be either a reference or a parent
1198 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
1200 struct talloc_chunk *tc;
1201 struct talloc_reference_handle *h;
1203 if (unlikely(ptr == NULL)) {
1207 if (old_parent == talloc_parent(ptr)) {
1208 return _talloc_steal_internal(new_parent, ptr);
1211 tc = talloc_chunk_from_ptr(ptr);
1212 for (h=tc->refs;h;h=h->next) {
1213 if (talloc_parent(h) == old_parent) {
1214 if (_talloc_steal_internal(new_parent, h) != h) {
1217 return discard_const_p(void, ptr);
1221 /* it wasn't a parent */
1226 remove a secondary reference to a pointer. This undo's what
1227 talloc_reference() has done. The context and pointer arguments
1228 must match those given to a talloc_reference()
1230 static inline int talloc_unreference(const void *context, const void *ptr)
1232 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1233 struct talloc_reference_handle *h;
1235 if (unlikely(context == NULL)) {
1236 context = null_context;
1239 for (h=tc->refs;h;h=h->next) {
1240 struct talloc_chunk *p = talloc_parent_chunk(h);
1242 if (context == NULL) break;
1243 } else if (TC_PTR_FROM_CHUNK(p) == context) {
1251 return _talloc_free_internal(h, __location__);
1255 remove a specific parent context from a pointer. This is a more
1256 controlled variant of talloc_free()
1258 _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
1260 struct talloc_chunk *tc_p, *new_p, *tc_c;
1267 if (context == NULL) {
1268 context = null_context;
1271 if (talloc_unreference(context, ptr) == 0) {
1275 if (context != NULL) {
1276 tc_c = talloc_chunk_from_ptr(context);
1280 if (tc_c != talloc_parent_chunk(ptr)) {
1284 tc_p = talloc_chunk_from_ptr(ptr);
1286 if (tc_p->refs == NULL) {
1287 return _talloc_free_internal(ptr, __location__);
1290 new_p = talloc_parent_chunk(tc_p->refs);
1292 new_parent = TC_PTR_FROM_CHUNK(new_p);
1297 if (talloc_unreference(new_parent, ptr) != 0) {
1301 _talloc_steal_internal(new_parent, ptr);
1307 add a name to an existing pointer - va_list version
1309 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1311 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
1313 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1314 tc->name = talloc_vasprintf(ptr, fmt, ap);
1315 if (likely(tc->name)) {
1316 _talloc_set_name_const(tc->name, ".name");
1322 add a name to an existing pointer
1324 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
1329 name = talloc_set_name_v(ptr, fmt, ap);
1336 create a named talloc pointer. Any talloc pointer can be named, and
1337 talloc_named() operates just like talloc() except that it allows you
1338 to name the pointer.
1340 _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
1346 ptr = __talloc(context, size);
1347 if (unlikely(ptr == NULL)) return NULL;
1350 name = talloc_set_name_v(ptr, fmt, ap);
1353 if (unlikely(name == NULL)) {
1354 _talloc_free_internal(ptr, __location__);
1362 return the name of a talloc ptr, or "UNNAMED"
1364 static inline const char *__talloc_get_name(const void *ptr)
1366 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1367 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
1368 return ".reference";
1370 if (likely(tc->name)) {
1376 _PUBLIC_ const char *talloc_get_name(const void *ptr)
1378 return __talloc_get_name(ptr);
1382 check if a pointer has the given name. If it does, return the pointer,
1383 otherwise return NULL
1385 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
1388 if (unlikely(ptr == NULL)) return NULL;
1389 pname = __talloc_get_name(ptr);
1390 if (likely(pname == name || strcmp(pname, name) == 0)) {
1391 return discard_const_p(void, ptr);
1396 static void talloc_abort_type_mismatch(const char *location,
1398 const char *expected)
1402 reason = talloc_asprintf(NULL,
1403 "%s: Type mismatch: name[%s] expected[%s]",
1408 reason = "Type mismatch";
1411 talloc_abort(reason);
1414 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
1418 if (unlikely(ptr == NULL)) {
1419 talloc_abort_type_mismatch(location, NULL, name);
1423 pname = __talloc_get_name(ptr);
1424 if (likely(pname == name || strcmp(pname, name) == 0)) {
1425 return discard_const_p(void, ptr);
1428 talloc_abort_type_mismatch(location, pname, name);
1433 this is for compatibility with older versions of talloc
1435 _PUBLIC_ void *talloc_init(const char *fmt, ...)
1441 ptr = __talloc(NULL, 0);
1442 if (unlikely(ptr == NULL)) return NULL;
1445 name = talloc_set_name_v(ptr, fmt, ap);
1448 if (unlikely(name == NULL)) {
1449 _talloc_free_internal(ptr, __location__);
1456 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
1458 const char *location)
1461 /* we need to work out who will own an abandoned child
1462 if it cannot be freed. In priority order, the first
1463 choice is owner of any remaining reference to this
1464 pointer, the second choice is our parent, and the
1465 final choice is the null context. */
1466 void *child = TC_PTR_FROM_CHUNK(tc->child);
1467 const void *new_parent = null_context;
1468 if (unlikely(tc->child->refs)) {
1469 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1470 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1472 if (unlikely(_talloc_free_internal(child, location) == -1)) {
1473 if (new_parent == null_context) {
1474 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1475 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1477 _talloc_steal_internal(new_parent, child);
1483 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1484 should probably not be used in new code. It's in here to keep the talloc
1485 code consistent across Samba 3 and 4.
1487 _PUBLIC_ void talloc_free_children(void *ptr)
1489 struct talloc_chunk *tc_name = NULL;
1490 struct talloc_chunk *tc;
1492 if (unlikely(ptr == NULL)) {
1496 tc = talloc_chunk_from_ptr(ptr);
1498 /* we do not want to free the context name if it is a child .. */
1499 if (likely(tc->child)) {
1500 for (tc_name = tc->child; tc_name; tc_name = tc_name->next) {
1501 if (tc->name == TC_PTR_FROM_CHUNK(tc_name)) break;
1504 _TLIST_REMOVE(tc->child, tc_name);
1506 tc->child->parent = tc;
1511 _talloc_free_children_internal(tc, ptr, __location__);
1513 /* .. so we put it back after all other children have been freed */
1516 tc->child->parent = NULL;
1518 tc_name->parent = tc;
1519 _TLIST_ADD(tc->child, tc_name);
1524 Allocate a bit of memory as a child of an existing pointer
1526 _PUBLIC_ void *_talloc(const void *context, size_t size)
1528 return __talloc(context, size);
1532 externally callable talloc_set_name_const()
1534 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1536 _talloc_set_name_const(ptr, name);
1540 create a named talloc pointer. Any talloc pointer can be named, and
1541 talloc_named() operates just like talloc() except that it allows you
1542 to name the pointer.
1544 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1546 return _talloc_named_const(context, size, name);
1550 free a talloc pointer. This also frees all child pointers of this
1553 return 0 if the memory is actually freed, otherwise -1. The memory
1554 will not be freed if the ref_count is > 1 or the destructor (if
1555 any) returns non-zero
1557 _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1559 struct talloc_chunk *tc;
1561 if (unlikely(ptr == NULL)) {
1565 tc = talloc_chunk_from_ptr(ptr);
1567 if (unlikely(tc->refs != NULL)) {
1568 struct talloc_reference_handle *h;
1570 if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1571 /* in this case we do know which parent should
1572 get this pointer, as there is really only
1574 return talloc_unlink(null_context, ptr);
1577 talloc_log("ERROR: talloc_free with references at %s\n",
1580 for (h=tc->refs; h; h=h->next) {
1581 talloc_log("\treference at %s\n",
1587 return _talloc_free_internal(ptr, location);
1593 A talloc version of realloc. The context argument is only used if
1596 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1598 struct talloc_chunk *tc;
1600 bool malloced = false;
1601 struct talloc_pool_hdr *pool_hdr = NULL;
1602 size_t old_size = 0;
1603 size_t new_size = 0;
1605 /* size zero is equivalent to free() */
1606 if (unlikely(size == 0)) {
1607 talloc_unlink(context, ptr);
1611 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1615 /* realloc(NULL) is equivalent to malloc() */
1617 return _talloc_named_const(context, size, name);
1620 tc = talloc_chunk_from_ptr(ptr);
1622 /* don't allow realloc on referenced pointers */
1623 if (unlikely(tc->refs)) {
1627 /* don't let anybody try to realloc a talloc_pool */
1628 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1632 if (tc->limit && (size > tc->size)) {
1633 if (!talloc_memlimit_check(tc->limit, (size - tc->size))) {
1639 /* handle realloc inside a talloc_pool */
1640 if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
1641 pool_hdr = tc->pool;
1644 #if (ALWAYS_REALLOC == 0)
1645 /* don't shrink if we have less than 1k to gain */
1646 if (size < tc->size && tc->limit == NULL) {
1648 void *next_tc = tc_next_chunk(tc);
1649 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1651 if (next_tc == pool_hdr->end) {
1652 /* note: tc->size has changed, so this works */
1653 pool_hdr->end = tc_next_chunk(tc);
1656 } else if ((tc->size - size) < 1024) {
1658 * if we call TC_INVALIDATE_SHRINK_CHUNK() here
1659 * we would need to call TC_UNDEFINE_GROW_CHUNK()
1660 * after each realloc call, which slows down
1661 * testing a lot :-(.
1663 * That is why we only mark memory as undefined here.
1665 TC_UNDEFINE_SHRINK_CHUNK(tc, size);
1667 /* do not shrink if we have less than 1k to gain */
1671 } else if (tc->size == size) {
1673 * do not change the pointer if it is exactly
1680 /* by resetting magic we catch users of the old memory */
1681 tc->flags |= TALLOC_FLAG_FREE;
1685 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE, 0);
1686 pool_hdr->object_count--;
1688 if (new_ptr == NULL) {
1689 new_ptr = malloc(TC_HDR_SIZE+size);
1695 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1696 TC_INVALIDATE_FULL_CHUNK(tc);
1699 /* We're doing malloc then free here, so record the difference. */
1700 old_size = tc->size;
1702 new_ptr = malloc(size + TC_HDR_SIZE);
1704 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1710 struct talloc_chunk *pool_tc;
1711 void *next_tc = tc_next_chunk(tc);
1712 size_t old_chunk_size = TC_ALIGN16(TC_HDR_SIZE + tc->size);
1713 size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
1714 size_t space_needed;
1716 unsigned int chunk_count = pool_hdr->object_count;
1718 pool_tc = talloc_chunk_from_pool(pool_hdr);
1719 if (!(pool_tc->flags & TALLOC_FLAG_FREE)) {
1723 if (chunk_count == 1) {
1725 * optimize for the case where 'tc' is the only
1726 * chunk in the pool.
1728 char *start = tc_pool_first_chunk(pool_hdr);
1729 space_needed = new_chunk_size;
1730 space_left = (char *)tc_pool_end(pool_hdr) - start;
1732 if (space_left >= space_needed) {
1733 size_t old_used = TC_HDR_SIZE + tc->size;
1734 size_t new_used = TC_HDR_SIZE + size;
1737 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
1741 * start -> tc may have
1742 * been freed and thus been marked as
1743 * VALGRIND_MEM_NOACCESS. Set it to
1744 * VALGRIND_MEM_UNDEFINED so we can
1745 * copy into it without valgrind errors.
1746 * We can't just mark
1747 * new_ptr -> new_ptr + old_used
1748 * as this may overlap on top of tc,
1749 * (which is why we use memmove, not
1750 * memcpy below) hence the MIN.
1752 size_t undef_len = MIN((((char *)tc) - ((char *)new_ptr)),old_used);
1753 VALGRIND_MAKE_MEM_UNDEFINED(new_ptr, undef_len);
1757 memmove(new_ptr, tc, old_used);
1759 tc = (struct talloc_chunk *)new_ptr;
1760 TC_UNDEFINE_GROW_CHUNK(tc, size);
1763 * first we do not align the pool pointer
1764 * because we want to invalidate the padding
1767 pool_hdr->end = new_used + (char *)new_ptr;
1768 tc_invalidate_pool(pool_hdr);
1770 /* now the aligned pointer */
1771 pool_hdr->end = new_chunk_size + (char *)new_ptr;
1778 if (new_chunk_size == old_chunk_size) {
1779 TC_UNDEFINE_GROW_CHUNK(tc, size);
1780 tc->flags &= ~TALLOC_FLAG_FREE;
1785 if (next_tc == pool_hdr->end) {
1787 * optimize for the case where 'tc' is the last
1788 * chunk in the pool.
1790 space_needed = new_chunk_size - old_chunk_size;
1791 space_left = tc_pool_space_left(pool_hdr);
1793 if (space_left >= space_needed) {
1794 TC_UNDEFINE_GROW_CHUNK(tc, size);
1795 tc->flags &= ~TALLOC_FLAG_FREE;
1797 pool_hdr->end = tc_next_chunk(tc);
1802 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE, 0);
1804 if (new_ptr == NULL) {
1805 new_ptr = malloc(TC_HDR_SIZE+size);
1811 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1813 _talloc_free_poolmem(tc, __location__ "_talloc_realloc");
1817 /* We're doing realloc here, so record the difference. */
1818 old_size = tc->size;
1820 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1824 if (unlikely(!new_ptr)) {
1825 tc->flags &= ~TALLOC_FLAG_FREE;
1829 tc = (struct talloc_chunk *)new_ptr;
1830 tc->flags &= ~TALLOC_FLAG_FREE;
1832 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1835 tc->parent->child = tc;
1838 tc->child->parent = tc;
1842 tc->prev->next = tc;
1845 tc->next->prev = tc;
1848 if (new_size > old_size) {
1849 talloc_memlimit_grow(tc->limit, new_size - old_size);
1850 } else if (new_size < old_size) {
1851 talloc_memlimit_shrink(tc->limit, old_size - new_size);
1855 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1857 return TC_PTR_FROM_CHUNK(tc);
1861 a wrapper around talloc_steal() for situations where you are moving a pointer
1862 between two structures, and want the old pointer to be set to NULL
1864 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
1866 const void **pptr = discard_const_p(const void *,_pptr);
1867 void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1872 enum talloc_mem_count_type {
1878 static inline size_t _talloc_total_mem_internal(const void *ptr,
1879 enum talloc_mem_count_type type,
1880 struct talloc_memlimit *old_limit,
1881 struct talloc_memlimit *new_limit)
1884 struct talloc_chunk *c, *tc;
1893 tc = talloc_chunk_from_ptr(ptr);
1895 if (old_limit || new_limit) {
1896 if (tc->limit && tc->limit->upper == old_limit) {
1897 tc->limit->upper = new_limit;
1901 /* optimize in the memlimits case */
1902 if (type == TOTAL_MEM_LIMIT &&
1903 tc->limit != NULL &&
1904 tc->limit != old_limit &&
1905 tc->limit->parent == tc) {
1906 return tc->limit->cur_size;
1909 if (tc->flags & TALLOC_FLAG_LOOP) {
1913 tc->flags |= TALLOC_FLAG_LOOP;
1915 if (old_limit || new_limit) {
1916 if (old_limit == tc->limit) {
1917 tc->limit = new_limit;
1922 case TOTAL_MEM_SIZE:
1923 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1927 case TOTAL_MEM_BLOCKS:
1930 case TOTAL_MEM_LIMIT:
1931 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1933 * Don't count memory allocated from a pool
1934 * when calculating limits. Only count the
1937 if (!(tc->flags & TALLOC_FLAG_POOLMEM)) {
1938 if (tc->flags & TALLOC_FLAG_POOL) {
1940 * If this is a pool, the allocated
1941 * size is in the pool header, and
1942 * remember to add in the prefix
1945 struct talloc_pool_hdr *pool_hdr
1946 = talloc_pool_from_chunk(tc);
1947 total = pool_hdr->poolsize +
1951 total = tc->size + TC_HDR_SIZE;
1957 for (c = tc->child; c; c = c->next) {
1958 total += _talloc_total_mem_internal(TC_PTR_FROM_CHUNK(c), type,
1959 old_limit, new_limit);
1962 tc->flags &= ~TALLOC_FLAG_LOOP;
1968 return the total size of a talloc pool (subtree)
1970 _PUBLIC_ size_t talloc_total_size(const void *ptr)
1972 return _talloc_total_mem_internal(ptr, TOTAL_MEM_SIZE, NULL, NULL);
1976 return the total number of blocks in a talloc pool (subtree)
1978 _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
1980 return _talloc_total_mem_internal(ptr, TOTAL_MEM_BLOCKS, NULL, NULL);
1984 return the number of external references to a pointer
1986 _PUBLIC_ size_t talloc_reference_count(const void *ptr)
1988 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1989 struct talloc_reference_handle *h;
1992 for (h=tc->refs;h;h=h->next) {
1999 report on memory usage by all children of a pointer, giving a full tree view
2001 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
2002 void (*callback)(const void *ptr,
2003 int depth, int max_depth,
2005 void *private_data),
2008 struct talloc_chunk *c, *tc;
2013 if (ptr == NULL) return;
2015 tc = talloc_chunk_from_ptr(ptr);
2017 if (tc->flags & TALLOC_FLAG_LOOP) {
2021 callback(ptr, depth, max_depth, 0, private_data);
2023 if (max_depth >= 0 && depth >= max_depth) {
2027 tc->flags |= TALLOC_FLAG_LOOP;
2028 for (c=tc->child;c;c=c->next) {
2029 if (c->name == TALLOC_MAGIC_REFERENCE) {
2030 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
2031 callback(h->ptr, depth + 1, max_depth, 1, private_data);
2033 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
2036 tc->flags &= ~TALLOC_FLAG_LOOP;
2039 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
2041 const char *name = __talloc_get_name(ptr);
2042 struct talloc_chunk *tc;
2043 FILE *f = (FILE *)_f;
2046 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
2050 tc = talloc_chunk_from_ptr(ptr);
2051 if (tc->limit && tc->limit->parent == tc) {
2052 fprintf(f, "%*s%-30s is a memlimit context"
2053 " (max_size = %lu bytes, cur_size = %lu bytes)\n",
2056 (unsigned long)tc->limit->max_size,
2057 (unsigned long)tc->limit->cur_size);
2061 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
2062 (max_depth < 0 ? "full " :""), name,
2063 (unsigned long)talloc_total_size(ptr),
2064 (unsigned long)talloc_total_blocks(ptr));
2068 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
2071 (unsigned long)talloc_total_size(ptr),
2072 (unsigned long)talloc_total_blocks(ptr),
2073 (int)talloc_reference_count(ptr), ptr);
2076 fprintf(f, "content: ");
2077 if (talloc_total_size(ptr)) {
2078 int tot = talloc_total_size(ptr);
2081 for (i = 0; i < tot; i++) {
2082 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
2083 fprintf(f, "%c", ((char *)ptr)[i]);
2085 fprintf(f, "~%02x", ((char *)ptr)[i]);
2094 report on memory usage by all children of a pointer, giving a full tree view
2096 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
2099 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
2105 report on memory usage by all children of a pointer, giving a full tree view
2107 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
2109 talloc_report_depth_file(ptr, 0, -1, f);
2113 report on memory usage by all children of a pointer
2115 _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
2117 talloc_report_depth_file(ptr, 0, 1, f);
2121 report on any memory hanging off the null context
2123 static void talloc_report_null(void)
2125 if (talloc_total_size(null_context) != 0) {
2126 talloc_report(null_context, stderr);
2131 report on any memory hanging off the null context
2133 static void talloc_report_null_full(void)
2135 if (talloc_total_size(null_context) != 0) {
2136 talloc_report_full(null_context, stderr);
2141 enable tracking of the NULL context
2143 _PUBLIC_ void talloc_enable_null_tracking(void)
2145 if (null_context == NULL) {
2146 null_context = _talloc_named_const(NULL, 0, "null_context");
2147 if (autofree_context != NULL) {
2148 talloc_reparent(NULL, null_context, autofree_context);
2154 enable tracking of the NULL context, not moving the autofree context
2155 into the NULL context. This is needed for the talloc testsuite
2157 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
2159 if (null_context == NULL) {
2160 null_context = _talloc_named_const(NULL, 0, "null_context");
2165 disable tracking of the NULL context
2167 _PUBLIC_ void talloc_disable_null_tracking(void)
2169 if (null_context != NULL) {
2170 /* we have to move any children onto the real NULL
2172 struct talloc_chunk *tc, *tc2;
2173 tc = talloc_chunk_from_ptr(null_context);
2174 for (tc2 = tc->child; tc2; tc2=tc2->next) {
2175 if (tc2->parent == tc) tc2->parent = NULL;
2176 if (tc2->prev == tc) tc2->prev = NULL;
2178 for (tc2 = tc->next; tc2; tc2=tc2->next) {
2179 if (tc2->parent == tc) tc2->parent = NULL;
2180 if (tc2->prev == tc) tc2->prev = NULL;
2185 talloc_free(null_context);
2186 null_context = NULL;
2190 enable leak reporting on exit
2192 _PUBLIC_ void talloc_enable_leak_report(void)
2194 talloc_enable_null_tracking();
2195 atexit(talloc_report_null);
2199 enable full leak reporting on exit
2201 _PUBLIC_ void talloc_enable_leak_report_full(void)
2203 talloc_enable_null_tracking();
2204 atexit(talloc_report_null_full);
2208 talloc and zero memory.
2210 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
2212 void *p = _talloc_named_const(ctx, size, name);
2215 memset(p, '\0', size);
2222 memdup with a talloc.
2224 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
2226 void *newp = _talloc_named_const(t, size, name);
2229 memcpy(newp, p, size);
2235 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
2239 ret = (char *)__talloc(t, len + 1);
2240 if (unlikely(!ret)) return NULL;
2242 memcpy(ret, p, len);
2245 _talloc_set_name_const(ret, ret);
2250 strdup with a talloc
2252 _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
2254 if (unlikely(!p)) return NULL;
2255 return __talloc_strlendup(t, p, strlen(p));
2259 strndup with a talloc
2261 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
2263 if (unlikely(!p)) return NULL;
2264 return __talloc_strlendup(t, p, strnlen(p, n));
2267 static inline char *__talloc_strlendup_append(char *s, size_t slen,
2268 const char *a, size_t alen)
2272 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
2273 if (unlikely(!ret)) return NULL;
2275 /* append the string and the trailing \0 */
2276 memcpy(&ret[slen], a, alen);
2279 _talloc_set_name_const(ret, ret);
2284 * Appends at the end of the string.
2286 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
2289 return talloc_strdup(NULL, a);
2296 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
2300 * Appends at the end of the talloc'ed buffer,
2301 * not the end of the string.
2303 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
2308 return talloc_strdup(NULL, a);
2315 slen = talloc_get_size(s);
2316 if (likely(slen > 0)) {
2320 return __talloc_strlendup_append(s, slen, a, strlen(a));
2324 * Appends at the end of the string.
2326 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
2329 return talloc_strndup(NULL, a, n);
2336 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
2340 * Appends at the end of the talloc'ed buffer,
2341 * not the end of the string.
2343 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
2348 return talloc_strndup(NULL, a, n);
2355 slen = talloc_get_size(s);
2356 if (likely(slen > 0)) {
2360 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
2363 #ifndef HAVE_VA_COPY
2364 #ifdef HAVE___VA_COPY
2365 #define va_copy(dest, src) __va_copy(dest, src)
2367 #define va_copy(dest, src) (dest) = (src)
2371 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
2378 /* this call looks strange, but it makes it work on older solaris boxes */
2380 len = vsnprintf(buf, sizeof(buf), fmt, ap2);
2382 if (unlikely(len < 0)) {
2386 ret = (char *)__talloc(t, len+1);
2387 if (unlikely(!ret)) return NULL;
2389 if (len < sizeof(buf)) {
2390 memcpy(ret, buf, len+1);
2393 vsnprintf(ret, len+1, fmt, ap2);
2397 _talloc_set_name_const(ret, ret);
2403 Perform string formatting, and return a pointer to newly allocated
2404 memory holding the result, inside a memory pool.
2406 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
2412 ret = talloc_vasprintf(t, fmt, ap);
2417 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2418 const char *fmt, va_list ap)
2419 PRINTF_ATTRIBUTE(3,0);
2421 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2422 const char *fmt, va_list ap)
2429 alen = vsnprintf(&c, 1, fmt, ap2);
2433 /* Either the vsnprintf failed or the format resulted in
2434 * no characters being formatted. In the former case, we
2435 * ought to return NULL, in the latter we ought to return
2436 * the original string. Most current callers of this
2437 * function expect it to never return NULL.
2442 s = talloc_realloc(NULL, s, char, slen + alen + 1);
2443 if (!s) return NULL;
2446 vsnprintf(s + slen, alen + 1, fmt, ap2);
2449 _talloc_set_name_const(s, s);
2454 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2455 * and return @p s, which may have moved. Good for gradually
2456 * accumulating output into a string buffer. Appends at the end
2459 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
2462 return talloc_vasprintf(NULL, fmt, ap);
2465 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
2469 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2470 * and return @p s, which may have moved. Always appends at the
2471 * end of the talloc'ed buffer, not the end of the string.
2473 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
2478 return talloc_vasprintf(NULL, fmt, ap);
2481 slen = talloc_get_size(s);
2482 if (likely(slen > 0)) {
2486 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
2490 Realloc @p s to append the formatted result of @p fmt and return @p
2491 s, which may have moved. Good for gradually accumulating output
2492 into a string buffer.
2494 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
2499 s = talloc_vasprintf_append(s, fmt, ap);
2505 Realloc @p s to append the formatted result of @p fmt and return @p
2506 s, which may have moved. Good for gradually accumulating output
2509 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
2514 s = talloc_vasprintf_append_buffer(s, fmt, ap);
2520 alloc an array, checking for integer overflow in the array size
2522 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2524 if (count >= MAX_TALLOC_SIZE/el_size) {
2527 return _talloc_named_const(ctx, el_size * count, name);
2531 alloc an zero array, checking for integer overflow in the array size
2533 _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2535 if (count >= MAX_TALLOC_SIZE/el_size) {
2538 return _talloc_zero(ctx, el_size * count, name);
2542 realloc an array, checking for integer overflow in the array size
2544 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
2546 if (count >= MAX_TALLOC_SIZE/el_size) {
2549 return _talloc_realloc(ctx, ptr, el_size * count, name);
2553 a function version of talloc_realloc(), so it can be passed as a function pointer
2554 to libraries that want a realloc function (a realloc function encapsulates
2555 all the basic capabilities of an allocation library, which is why this is useful)
2557 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
2559 return _talloc_realloc(context, ptr, size, NULL);
2563 static int talloc_autofree_destructor(void *ptr)
2565 autofree_context = NULL;
2569 static void talloc_autofree(void)
2571 talloc_free(autofree_context);
2575 return a context which will be auto-freed on exit
2576 this is useful for reducing the noise in leak reports
2578 _PUBLIC_ void *talloc_autofree_context(void)
2580 if (autofree_context == NULL) {
2581 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
2582 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
2583 atexit(talloc_autofree);
2585 return autofree_context;
2588 _PUBLIC_ size_t talloc_get_size(const void *context)
2590 struct talloc_chunk *tc;
2592 if (context == NULL) {
2593 context = null_context;
2595 if (context == NULL) {
2599 tc = talloc_chunk_from_ptr(context);
2605 find a parent of this context that has the given name, if any
2607 _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
2609 struct talloc_chunk *tc;
2611 if (context == NULL) {
2615 tc = talloc_chunk_from_ptr(context);
2617 if (tc->name && strcmp(tc->name, name) == 0) {
2618 return TC_PTR_FROM_CHUNK(tc);
2620 while (tc && tc->prev) tc = tc->prev;
2629 show the parentage of a context
2631 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
2633 struct talloc_chunk *tc;
2635 if (context == NULL) {
2636 fprintf(file, "talloc no parents for NULL\n");
2640 tc = talloc_chunk_from_ptr(context);
2641 fprintf(file, "talloc parents of '%s'\n", __talloc_get_name(context));
2643 fprintf(file, "\t'%s'\n", __talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
2644 while (tc && tc->prev) tc = tc->prev;
2653 return 1 if ptr is a parent of context
2655 static int _talloc_is_parent(const void *context, const void *ptr, int depth)
2657 struct talloc_chunk *tc;
2659 if (context == NULL) {
2663 tc = talloc_chunk_from_ptr(context);
2668 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2669 while (tc && tc->prev) tc = tc->prev;
2679 return 1 if ptr is a parent of context
2681 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2683 return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);
2687 return the total size of memory used by this context and all children
2689 static inline size_t _talloc_total_limit_size(const void *ptr,
2690 struct talloc_memlimit *old_limit,
2691 struct talloc_memlimit *new_limit)
2693 return _talloc_total_mem_internal(ptr, TOTAL_MEM_LIMIT,
2694 old_limit, new_limit);
2697 static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size)
2699 struct talloc_memlimit *l;
2701 for (l = limit; l != NULL; l = l->upper) {
2702 if (l->max_size != 0 &&
2703 ((l->max_size <= l->cur_size) ||
2704 (l->max_size - l->cur_size < size))) {
2713 Update memory limits when freeing a talloc_chunk.
2715 static void talloc_memlimit_update_on_free(struct talloc_chunk *tc)
2717 size_t limit_shrink_size;
2724 * Pool entries don't count. Only the pools
2725 * themselves are counted as part of the memory
2726 * limits. Note that this also takes care of
2727 * nested pools which have both flags
2728 * TALLOC_FLAG_POOLMEM|TALLOC_FLAG_POOL set.
2730 if (tc->flags & TALLOC_FLAG_POOLMEM) {
2735 * If we are part of a memory limited context hierarchy
2736 * we need to subtract the memory used from the counters
2739 limit_shrink_size = tc->size+TC_HDR_SIZE;
2742 * If we're deallocating a pool, take into
2743 * account the prefix size added for the pool.
2746 if (tc->flags & TALLOC_FLAG_POOL) {
2747 limit_shrink_size += TP_HDR_SIZE;
2750 talloc_memlimit_shrink(tc->limit, limit_shrink_size);
2752 if (tc->limit->parent == tc) {
2760 Increase memory limit accounting after a malloc/realloc.
2762 static void talloc_memlimit_grow(struct talloc_memlimit *limit,
2765 struct talloc_memlimit *l;
2767 for (l = limit; l != NULL; l = l->upper) {
2768 size_t new_cur_size = l->cur_size + size;
2769 if (new_cur_size < l->cur_size) {
2770 talloc_abort("logic error in talloc_memlimit_grow\n");
2773 l->cur_size = new_cur_size;
2778 Decrease memory limit accounting after a free/realloc.
2780 static void talloc_memlimit_shrink(struct talloc_memlimit *limit,
2783 struct talloc_memlimit *l;
2785 for (l = limit; l != NULL; l = l->upper) {
2786 if (l->cur_size < size) {
2787 talloc_abort("logic error in talloc_memlimit_shrink\n");
2790 l->cur_size = l->cur_size - size;
2794 _PUBLIC_ int talloc_set_memlimit(const void *ctx, size_t max_size)
2796 struct talloc_chunk *tc = talloc_chunk_from_ptr(ctx);
2797 struct talloc_memlimit *orig_limit;
2798 struct talloc_memlimit *limit = NULL;
2800 if (tc->limit && tc->limit->parent == tc) {
2801 tc->limit->max_size = max_size;
2804 orig_limit = tc->limit;
2806 limit = malloc(sizeof(struct talloc_memlimit));
2807 if (limit == NULL) {
2811 limit->max_size = max_size;
2812 limit->cur_size = _talloc_total_limit_size(ctx, tc->limit, limit);
2815 limit->upper = orig_limit;
2817 limit->upper = NULL;