tdb: Fix a typo
[Samba/gebeck_regimport.git] / lib / talloc / talloc.c
blob18ee548095792a838687b421e5ad96bcb433e7ef
1 /*
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
13 ** under the LGPL
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/
33 #include "replace.h"
34 #include "talloc.h"
36 #ifdef TALLOC_BUILD_VERSION_MAJOR
37 #if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)
38 #error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"
39 #endif
40 #endif
42 #ifdef TALLOC_BUILD_VERSION_MINOR
43 #if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)
44 #error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"
45 #endif
46 #endif
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)
54 #include <valgrind.h>
55 #endif
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 ( \
65 TALLOC_MAGIC_BASE + \
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 */
74 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
76 /* by default we abort when given a bad pointer (such as when talloc_free() is called
77 on a pointer that came from malloc() */
78 #ifndef TALLOC_ABORT
79 #define TALLOC_ABORT(reason) abort()
80 #endif
82 #ifndef discard_const_p
83 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
84 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
85 #else
86 # define discard_const_p(type, ptr) ((type *)(ptr))
87 #endif
88 #endif
90 /* these macros gain us a few percent of speed on gcc */
91 #if (__GNUC__ >= 3)
92 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
93 as its first argument */
94 #ifndef likely
95 #define likely(x) __builtin_expect(!!(x), 1)
96 #endif
97 #ifndef unlikely
98 #define unlikely(x) __builtin_expect(!!(x), 0)
99 #endif
100 #else
101 #ifndef likely
102 #define likely(x) (x)
103 #endif
104 #ifndef unlikely
105 #define unlikely(x) (x)
106 #endif
107 #endif
109 /* this null_context is only used if talloc_enable_leak_report() or
110 talloc_enable_leak_report_full() is called, otherwise it remains
111 NULL
113 static void *null_context;
114 static void *autofree_context;
116 /* used to enable fill of memory on free, which can be useful for
117 * catching use after free errors when valgrind is too slow
119 static struct {
120 bool initialised;
121 bool enabled;
122 uint8_t fill_value;
123 } talloc_fill;
125 #define TALLOC_FILL_ENV "TALLOC_FREE_FILL"
128 * do not wipe the header, to allow the
129 * double-free logic to still work
131 #define TC_INVALIDATE_FULL_FILL_CHUNK(_tc) do { \
132 if (unlikely(talloc_fill.enabled)) { \
133 size_t _flen = (_tc)->size; \
134 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
135 memset(_fptr, talloc_fill.fill_value, _flen); \
137 } while (0)
139 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
140 /* Mark the whole chunk as not accessable */
141 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { \
142 size_t _flen = TC_HDR_SIZE + (_tc)->size; \
143 char *_fptr = (char *)(_tc); \
144 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
145 } while(0)
146 #else
147 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { } while (0)
148 #endif
150 #define TC_INVALIDATE_FULL_CHUNK(_tc) do { \
151 TC_INVALIDATE_FULL_FILL_CHUNK(_tc); \
152 TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc); \
153 } while (0)
155 #define TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
156 if (unlikely(talloc_fill.enabled)) { \
157 size_t _flen = (_tc)->size - (_new_size); \
158 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
159 _fptr += (_new_size); \
160 memset(_fptr, talloc_fill.fill_value, _flen); \
162 } while (0)
164 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
165 /* Mark the unused bytes not accessable */
166 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
167 size_t _flen = (_tc)->size - (_new_size); \
168 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
169 _fptr += (_new_size); \
170 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
171 } while (0)
172 #else
173 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
174 #endif
176 #define TC_INVALIDATE_SHRINK_CHUNK(_tc, _new_size) do { \
177 TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size); \
178 TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
179 } while (0)
181 #define TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
182 if (unlikely(talloc_fill.enabled)) { \
183 size_t _flen = (_tc)->size - (_new_size); \
184 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
185 _fptr += (_new_size); \
186 memset(_fptr, talloc_fill.fill_value, _flen); \
188 } while (0)
190 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
191 /* Mark the unused bytes as undefined */
192 #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
193 size_t _flen = (_tc)->size - (_new_size); \
194 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
195 _fptr += (_new_size); \
196 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
197 } while (0)
198 #else
199 #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
200 #endif
202 #define TC_UNDEFINE_SHRINK_CHUNK(_tc, _new_size) do { \
203 TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size); \
204 TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
205 } while (0)
207 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
208 /* Mark the new bytes as undefined */
209 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { \
210 size_t _old_used = TC_HDR_SIZE + (_tc)->size; \
211 size_t _new_used = TC_HDR_SIZE + (_new_size); \
212 size_t _flen = _new_used - _old_used; \
213 char *_fptr = _old_used + (char *)(_tc); \
214 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
215 } while (0)
216 #else
217 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
218 #endif
220 #define TC_UNDEFINE_GROW_CHUNK(_tc, _new_size) do { \
221 TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
222 } while (0)
224 struct talloc_reference_handle {
225 struct talloc_reference_handle *next, *prev;
226 void *ptr;
227 const char *location;
230 typedef int (*talloc_destructor_t)(void *);
232 struct talloc_chunk {
233 struct talloc_chunk *next, *prev;
234 struct talloc_chunk *parent, *child;
235 struct talloc_reference_handle *refs;
236 talloc_destructor_t destructor;
237 const char *name;
238 size_t size;
239 unsigned flags;
242 * "pool" has dual use:
244 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
245 * marks the end of the currently allocated area.
247 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
248 * is a pointer to the struct talloc_chunk of the pool that it was
249 * allocated from. This way children can quickly find the pool to chew
250 * from.
252 void *pool;
255 /* 16 byte alignment seems to keep everyone happy */
256 #define TC_ALIGN16(s) (((s)+15)&~15)
257 #define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
258 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
260 _PUBLIC_ int talloc_version_major(void)
262 return TALLOC_VERSION_MAJOR;
265 _PUBLIC_ int talloc_version_minor(void)
267 return TALLOC_VERSION_MINOR;
270 static void (*talloc_log_fn)(const char *message);
272 _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
274 talloc_log_fn = log_fn;
277 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
278 static void talloc_log(const char *fmt, ...)
280 va_list ap;
281 char *message;
283 if (!talloc_log_fn) {
284 return;
287 va_start(ap, fmt);
288 message = talloc_vasprintf(NULL, fmt, ap);
289 va_end(ap);
291 talloc_log_fn(message);
292 talloc_free(message);
295 static void talloc_log_stderr(const char *message)
297 fprintf(stderr, "%s", message);
300 _PUBLIC_ void talloc_set_log_stderr(void)
302 talloc_set_log_fn(talloc_log_stderr);
305 static void (*talloc_abort_fn)(const char *reason);
307 _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
309 talloc_abort_fn = abort_fn;
312 static void talloc_abort(const char *reason)
314 talloc_log("%s\n", reason);
316 if (!talloc_abort_fn) {
317 TALLOC_ABORT(reason);
320 talloc_abort_fn(reason);
323 static void talloc_abort_magic(unsigned magic)
325 unsigned striped = magic - TALLOC_MAGIC_BASE;
326 unsigned major = (striped & 0xFFFFF000) >> 12;
327 unsigned minor = (striped & 0x00000FF0) >> 4;
328 talloc_log("Bad talloc magic[0x%08X/%u/%u] expected[0x%08X/%u/%u]\n",
329 magic, major, minor,
330 TALLOC_MAGIC, TALLOC_VERSION_MAJOR, TALLOC_VERSION_MINOR);
331 talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
334 static void talloc_abort_access_after_free(void)
336 talloc_abort("Bad talloc magic value - access after free");
339 static void talloc_abort_unknown_value(void)
341 talloc_abort("Bad talloc magic value - unknown value");
344 /* panic if we get a bad magic value */
345 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
347 const char *pp = (const char *)ptr;
348 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
349 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
350 if ((tc->flags & (~0xFFF)) == TALLOC_MAGIC_BASE) {
351 talloc_abort_magic(tc->flags & (~0xF));
352 return NULL;
355 if (tc->flags & TALLOC_FLAG_FREE) {
356 talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
357 talloc_abort_access_after_free();
358 return NULL;
359 } else {
360 talloc_abort_unknown_value();
361 return NULL;
364 return tc;
367 /* hook into the front of the list */
368 #define _TLIST_ADD(list, p) \
369 do { \
370 if (!(list)) { \
371 (list) = (p); \
372 (p)->next = (p)->prev = NULL; \
373 } else { \
374 (list)->prev = (p); \
375 (p)->next = (list); \
376 (p)->prev = NULL; \
377 (list) = (p); \
379 } while (0)
381 /* remove an element from a list - element doesn't have to be in list. */
382 #define _TLIST_REMOVE(list, p) \
383 do { \
384 if ((p) == (list)) { \
385 (list) = (p)->next; \
386 if (list) (list)->prev = NULL; \
387 } else { \
388 if ((p)->prev) (p)->prev->next = (p)->next; \
389 if ((p)->next) (p)->next->prev = (p)->prev; \
391 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
392 } while (0)
396 return the parent chunk of a pointer
398 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
400 struct talloc_chunk *tc;
402 if (unlikely(ptr == NULL)) {
403 return NULL;
406 tc = talloc_chunk_from_ptr(ptr);
407 while (tc->prev) tc=tc->prev;
409 return tc->parent;
412 _PUBLIC_ void *talloc_parent(const void *ptr)
414 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
415 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
419 find parents name
421 _PUBLIC_ const char *talloc_parent_name(const void *ptr)
423 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
424 return tc? tc->name : NULL;
428 A pool carries an in-pool object count count in the first 16 bytes.
429 bytes. This is done to support talloc_steal() to a parent outside of the
430 pool. The count includes the pool itself, so a talloc_free() on a pool will
431 only destroy the pool if the count has dropped to zero. A talloc_free() of a
432 pool member will reduce the count, and eventually also call free(3) on the
433 pool memory.
435 The object count is not put into "struct talloc_chunk" because it is only
436 relevant for talloc pools and the alignment to 16 bytes would increase the
437 memory footprint of each talloc chunk by those 16 bytes.
440 union talloc_pool_chunk {
441 /* This lets object_count nestle into 16-byte padding of talloc_chunk,
442 * on 32-bit platforms. */
443 struct tc_pool_hdr {
444 struct talloc_chunk c;
445 unsigned int object_count;
446 } hdr;
447 /* This makes it always 16 byte aligned. */
448 char pad[TC_ALIGN16(sizeof(struct tc_pool_hdr))];
451 static void *tc_pool_end(union talloc_pool_chunk *pool_tc)
453 return (char *)pool_tc + TC_HDR_SIZE + pool_tc->hdr.c.size;
456 static size_t tc_pool_space_left(union talloc_pool_chunk *pool_tc)
458 return (char *)tc_pool_end(pool_tc) - (char *)pool_tc->hdr.c.pool;
461 static void *tc_pool_first_chunk(union talloc_pool_chunk *pool_tc)
463 return pool_tc + 1;
466 /* If tc is inside a pool, this gives the next neighbour. */
467 static void *tc_next_chunk(struct talloc_chunk *tc)
469 return (char *)tc + TC_ALIGN16(TC_HDR_SIZE + tc->size);
472 /* Mark the whole remaining pool as not accessable */
473 static void tc_invalidate_pool(union talloc_pool_chunk *pool_tc)
475 size_t flen = tc_pool_space_left(pool_tc);
477 if (unlikely(talloc_fill.enabled)) {
478 memset(pool_tc->hdr.c.pool, talloc_fill.fill_value, flen);
481 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
482 VALGRIND_MAKE_MEM_NOACCESS(pool_tc->hdr.c.pool, flen);
483 #endif
487 Allocate from a pool
490 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
491 size_t size)
493 union talloc_pool_chunk *pool_ctx = NULL;
494 size_t space_left;
495 struct talloc_chunk *result;
496 size_t chunk_size;
498 if (parent == NULL) {
499 return NULL;
502 if (parent->flags & TALLOC_FLAG_POOL) {
503 pool_ctx = (union talloc_pool_chunk *)parent;
505 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
506 pool_ctx = (union talloc_pool_chunk *)parent->pool;
509 if (pool_ctx == NULL) {
510 return NULL;
513 space_left = tc_pool_space_left(pool_ctx);
516 * Align size to 16 bytes
518 chunk_size = TC_ALIGN16(size);
520 if (space_left < chunk_size) {
521 return NULL;
524 result = (struct talloc_chunk *)pool_ctx->hdr.c.pool;
526 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
527 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
528 #endif
530 pool_ctx->hdr.c.pool = (void *)((char *)result + chunk_size);
532 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
533 result->pool = pool_ctx;
535 pool_ctx->hdr.object_count++;
537 return result;
541 Allocate a bit of memory as a child of an existing pointer
543 static inline void *__talloc(const void *context, size_t size)
545 struct talloc_chunk *tc = NULL;
547 if (unlikely(context == NULL)) {
548 context = null_context;
551 if (unlikely(size >= MAX_TALLOC_SIZE)) {
552 return NULL;
555 if (context != NULL) {
556 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
557 TC_HDR_SIZE+size);
560 if (tc == NULL) {
561 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
562 if (unlikely(tc == NULL)) return NULL;
563 tc->flags = TALLOC_MAGIC;
564 tc->pool = NULL;
567 tc->size = size;
568 tc->destructor = NULL;
569 tc->child = NULL;
570 tc->name = NULL;
571 tc->refs = NULL;
573 if (likely(context)) {
574 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
576 if (parent->child) {
577 parent->child->parent = NULL;
578 tc->next = parent->child;
579 tc->next->prev = tc;
580 } else {
581 tc->next = NULL;
583 tc->parent = parent;
584 tc->prev = NULL;
585 parent->child = tc;
586 } else {
587 tc->next = tc->prev = tc->parent = NULL;
590 return TC_PTR_FROM_CHUNK(tc);
594 * Create a talloc pool
597 _PUBLIC_ void *talloc_pool(const void *context, size_t size)
599 union talloc_pool_chunk *pool_tc;
600 void *result = __talloc(context, sizeof(*pool_tc) - TC_HDR_SIZE + size);
602 if (unlikely(result == NULL)) {
603 return NULL;
606 pool_tc = (union talloc_pool_chunk *)talloc_chunk_from_ptr(result);
607 if (unlikely(pool_tc->hdr.c.flags & TALLOC_FLAG_POOLMEM)) {
608 /* We don't handle this correctly, so fail. */
609 talloc_log("talloc: cannot allocate pool off another pool %s\n",
610 talloc_get_name(context));
611 talloc_free(result);
612 return NULL;
614 pool_tc->hdr.c.flags |= TALLOC_FLAG_POOL;
615 pool_tc->hdr.c.pool = tc_pool_first_chunk(pool_tc);
617 pool_tc->hdr.object_count = 1;
619 tc_invalidate_pool(pool_tc);
621 return result;
625 setup a destructor to be called on free of a pointer
626 the destructor should return 0 on success, or -1 on failure.
627 if the destructor fails then the free is failed, and the memory can
628 be continued to be used
630 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
632 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
633 tc->destructor = destructor;
637 increase the reference count on a piece of memory.
639 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
641 if (unlikely(!talloc_reference(null_context, ptr))) {
642 return -1;
644 return 0;
648 helper for talloc_reference()
650 this is referenced by a function pointer and should not be inline
652 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
654 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
655 _TLIST_REMOVE(ptr_tc->refs, handle);
656 return 0;
660 more efficient way to add a name to a pointer - the name must point to a
661 true string constant
663 static inline void _talloc_set_name_const(const void *ptr, const char *name)
665 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
666 tc->name = name;
670 internal talloc_named_const()
672 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
674 void *ptr;
676 ptr = __talloc(context, size);
677 if (unlikely(ptr == NULL)) {
678 return NULL;
681 _talloc_set_name_const(ptr, name);
683 return ptr;
687 make a secondary reference to a pointer, hanging off the given context.
688 the pointer remains valid until both the original caller and this given
689 context are freed.
691 the major use for this is when two different structures need to reference the
692 same underlying data, and you want to be able to free the two instances separately,
693 and in either order
695 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
697 struct talloc_chunk *tc;
698 struct talloc_reference_handle *handle;
699 if (unlikely(ptr == NULL)) return NULL;
701 tc = talloc_chunk_from_ptr(ptr);
702 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
703 sizeof(struct talloc_reference_handle),
704 TALLOC_MAGIC_REFERENCE);
705 if (unlikely(handle == NULL)) return NULL;
707 /* note that we hang the destructor off the handle, not the
708 main context as that allows the caller to still setup their
709 own destructor on the context if they want to */
710 talloc_set_destructor(handle, talloc_reference_destructor);
711 handle->ptr = discard_const_p(void, ptr);
712 handle->location = location;
713 _TLIST_ADD(tc->refs, handle);
714 return handle->ptr;
717 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
719 static inline void _talloc_free_poolmem(struct talloc_chunk *tc,
720 const char *location)
722 union talloc_pool_chunk *pool;
723 void *next_tc;
725 pool = (union talloc_pool_chunk *)tc->pool;
726 next_tc = tc_next_chunk(tc);
728 tc->flags |= TALLOC_FLAG_FREE;
730 /* we mark the freed memory with where we called the free
731 * from. This means on a double free error we can report where
732 * the first free came from
734 tc->name = location;
736 TC_INVALIDATE_FULL_CHUNK(tc);
738 if (unlikely(pool->hdr.object_count == 0)) {
739 talloc_abort("Pool object count zero!");
740 return;
743 pool->hdr.object_count--;
745 if (unlikely(pool->hdr.object_count == 1
746 && !(pool->hdr.c.flags & TALLOC_FLAG_FREE))) {
748 * if there is just one object left in the pool
749 * and pool->flags does not have TALLOC_FLAG_FREE,
750 * it means this is the pool itself and
751 * the rest is available for new objects
752 * again.
754 pool->hdr.c.pool = tc_pool_first_chunk(pool);
755 tc_invalidate_pool(pool);
756 } else if (unlikely(pool->hdr.object_count == 0)) {
758 * we mark the freed memory with where we called the free
759 * from. This means on a double free error we can report where
760 * the first free came from
762 pool->hdr.c.name = location;
764 TC_INVALIDATE_FULL_CHUNK(&pool->hdr.c);
765 free(pool);
766 } else if (pool->hdr.c.pool == next_tc) {
768 * if pool->pool still points to end of
769 * 'tc' (which is stored in the 'next_tc' variable),
770 * we can reclaim the memory of 'tc'.
772 pool->hdr.c.pool = tc;
776 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
777 void *ptr,
778 const char *location);
781 internal talloc_free call
783 static inline int _talloc_free_internal(void *ptr, const char *location)
785 struct talloc_chunk *tc;
787 if (unlikely(ptr == NULL)) {
788 return -1;
791 /* possibly initialised the talloc fill value */
792 if (unlikely(!talloc_fill.initialised)) {
793 const char *fill = getenv(TALLOC_FILL_ENV);
794 if (fill != NULL) {
795 talloc_fill.enabled = true;
796 talloc_fill.fill_value = strtoul(fill, NULL, 0);
798 talloc_fill.initialised = true;
801 tc = talloc_chunk_from_ptr(ptr);
803 if (unlikely(tc->refs)) {
804 int is_child;
805 /* check if this is a reference from a child or
806 * grandchild back to it's parent or grandparent
808 * in that case we need to remove the reference and
809 * call another instance of talloc_free() on the current
810 * pointer.
812 is_child = talloc_is_parent(tc->refs, ptr);
813 _talloc_free_internal(tc->refs, location);
814 if (is_child) {
815 return _talloc_free_internal(ptr, location);
817 return -1;
820 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
821 /* we have a free loop - stop looping */
822 return 0;
825 if (unlikely(tc->destructor)) {
826 talloc_destructor_t d = tc->destructor;
827 if (d == (talloc_destructor_t)-1) {
828 return -1;
830 tc->destructor = (talloc_destructor_t)-1;
831 if (d(ptr) == -1) {
832 tc->destructor = d;
833 return -1;
835 tc->destructor = NULL;
838 if (tc->parent) {
839 _TLIST_REMOVE(tc->parent->child, tc);
840 if (tc->parent->child) {
841 tc->parent->child->parent = tc->parent;
843 } else {
844 if (tc->prev) tc->prev->next = tc->next;
845 if (tc->next) tc->next->prev = tc->prev;
846 tc->prev = tc->next = NULL;
849 tc->flags |= TALLOC_FLAG_LOOP;
851 _talloc_free_children_internal(tc, ptr, location);
853 tc->flags |= TALLOC_FLAG_FREE;
855 /* we mark the freed memory with where we called the free
856 * from. This means on a double free error we can report where
857 * the first free came from
859 tc->name = location;
861 if (tc->flags & TALLOC_FLAG_POOL) {
862 union talloc_pool_chunk *pool = (union talloc_pool_chunk *)tc;
864 if (unlikely(pool->hdr.object_count == 0)) {
865 talloc_abort("Pool object count zero!");
866 return 0;
869 pool->hdr.object_count--;
870 if (unlikely(pool->hdr.object_count == 0)) {
871 TC_INVALIDATE_FULL_CHUNK(tc);
872 free(tc);
874 } else if (tc->flags & TALLOC_FLAG_POOLMEM) {
875 _talloc_free_poolmem(tc, location);
876 } else {
877 TC_INVALIDATE_FULL_CHUNK(tc);
878 free(tc);
880 return 0;
884 move a lump of memory from one talloc context to another return the
885 ptr on success, or NULL if it could not be transferred.
886 passing NULL as ptr will always return NULL with no side effects.
888 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
890 struct talloc_chunk *tc, *new_tc;
892 if (unlikely(!ptr)) {
893 return NULL;
896 if (unlikely(new_ctx == NULL)) {
897 new_ctx = null_context;
900 tc = talloc_chunk_from_ptr(ptr);
902 if (unlikely(new_ctx == NULL)) {
903 if (tc->parent) {
904 _TLIST_REMOVE(tc->parent->child, tc);
905 if (tc->parent->child) {
906 tc->parent->child->parent = tc->parent;
908 } else {
909 if (tc->prev) tc->prev->next = tc->next;
910 if (tc->next) tc->next->prev = tc->prev;
913 tc->parent = tc->next = tc->prev = NULL;
914 return discard_const_p(void, ptr);
917 new_tc = talloc_chunk_from_ptr(new_ctx);
919 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
920 return discard_const_p(void, ptr);
923 if (tc->parent) {
924 _TLIST_REMOVE(tc->parent->child, tc);
925 if (tc->parent->child) {
926 tc->parent->child->parent = tc->parent;
928 } else {
929 if (tc->prev) tc->prev->next = tc->next;
930 if (tc->next) tc->next->prev = tc->prev;
931 tc->prev = tc->next = NULL;
934 tc->parent = new_tc;
935 if (new_tc->child) new_tc->child->parent = NULL;
936 _TLIST_ADD(new_tc->child, tc);
938 return discard_const_p(void, ptr);
942 move a lump of memory from one talloc context to another return the
943 ptr on success, or NULL if it could not be transferred.
944 passing NULL as ptr will always return NULL with no side effects.
946 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
948 struct talloc_chunk *tc;
950 if (unlikely(ptr == NULL)) {
951 return NULL;
954 tc = talloc_chunk_from_ptr(ptr);
956 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
957 struct talloc_reference_handle *h;
959 talloc_log("WARNING: talloc_steal with references at %s\n",
960 location);
962 for (h=tc->refs; h; h=h->next) {
963 talloc_log("\treference at %s\n",
964 h->location);
968 #if 0
969 /* this test is probably too expensive to have on in the
970 normal build, but it useful for debugging */
971 if (talloc_is_parent(new_ctx, ptr)) {
972 talloc_log("WARNING: stealing into talloc child at %s\n", location);
974 #endif
976 return _talloc_steal_internal(new_ctx, ptr);
980 this is like a talloc_steal(), but you must supply the old
981 parent. This resolves the ambiguity in a talloc_steal() which is
982 called on a context that has more than one parent (via references)
984 The old parent can be either a reference or a parent
986 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
988 struct talloc_chunk *tc;
989 struct talloc_reference_handle *h;
991 if (unlikely(ptr == NULL)) {
992 return NULL;
995 if (old_parent == talloc_parent(ptr)) {
996 return _talloc_steal_internal(new_parent, ptr);
999 tc = talloc_chunk_from_ptr(ptr);
1000 for (h=tc->refs;h;h=h->next) {
1001 if (talloc_parent(h) == old_parent) {
1002 if (_talloc_steal_internal(new_parent, h) != h) {
1003 return NULL;
1005 return discard_const_p(void, ptr);
1009 /* it wasn't a parent */
1010 return NULL;
1014 remove a secondary reference to a pointer. This undo's what
1015 talloc_reference() has done. The context and pointer arguments
1016 must match those given to a talloc_reference()
1018 static inline int talloc_unreference(const void *context, const void *ptr)
1020 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1021 struct talloc_reference_handle *h;
1023 if (unlikely(context == NULL)) {
1024 context = null_context;
1027 for (h=tc->refs;h;h=h->next) {
1028 struct talloc_chunk *p = talloc_parent_chunk(h);
1029 if (p == NULL) {
1030 if (context == NULL) break;
1031 } else if (TC_PTR_FROM_CHUNK(p) == context) {
1032 break;
1035 if (h == NULL) {
1036 return -1;
1039 return _talloc_free_internal(h, __location__);
1043 remove a specific parent context from a pointer. This is a more
1044 controlled variant of talloc_free()
1046 _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
1048 struct talloc_chunk *tc_p, *new_p, *tc_c;
1049 void *new_parent;
1051 if (ptr == NULL) {
1052 return -1;
1055 if (context == NULL) {
1056 context = null_context;
1059 if (talloc_unreference(context, ptr) == 0) {
1060 return 0;
1063 if (context != NULL) {
1064 tc_c = talloc_chunk_from_ptr(context);
1065 } else {
1066 tc_c = NULL;
1068 if (tc_c != talloc_parent_chunk(ptr)) {
1069 return -1;
1072 tc_p = talloc_chunk_from_ptr(ptr);
1074 if (tc_p->refs == NULL) {
1075 return _talloc_free_internal(ptr, __location__);
1078 new_p = talloc_parent_chunk(tc_p->refs);
1079 if (new_p) {
1080 new_parent = TC_PTR_FROM_CHUNK(new_p);
1081 } else {
1082 new_parent = NULL;
1085 if (talloc_unreference(new_parent, ptr) != 0) {
1086 return -1;
1089 _talloc_steal_internal(new_parent, ptr);
1091 return 0;
1095 add a name to an existing pointer - va_list version
1097 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1099 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
1101 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1102 tc->name = talloc_vasprintf(ptr, fmt, ap);
1103 if (likely(tc->name)) {
1104 _talloc_set_name_const(tc->name, ".name");
1106 return tc->name;
1110 add a name to an existing pointer
1112 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
1114 const char *name;
1115 va_list ap;
1116 va_start(ap, fmt);
1117 name = talloc_set_name_v(ptr, fmt, ap);
1118 va_end(ap);
1119 return name;
1124 create a named talloc pointer. Any talloc pointer can be named, and
1125 talloc_named() operates just like talloc() except that it allows you
1126 to name the pointer.
1128 _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
1130 va_list ap;
1131 void *ptr;
1132 const char *name;
1134 ptr = __talloc(context, size);
1135 if (unlikely(ptr == NULL)) return NULL;
1137 va_start(ap, fmt);
1138 name = talloc_set_name_v(ptr, fmt, ap);
1139 va_end(ap);
1141 if (unlikely(name == NULL)) {
1142 _talloc_free_internal(ptr, __location__);
1143 return NULL;
1146 return ptr;
1150 return the name of a talloc ptr, or "UNNAMED"
1152 _PUBLIC_ const char *talloc_get_name(const void *ptr)
1154 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1155 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
1156 return ".reference";
1158 if (likely(tc->name)) {
1159 return tc->name;
1161 return "UNNAMED";
1166 check if a pointer has the given name. If it does, return the pointer,
1167 otherwise return NULL
1169 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
1171 const char *pname;
1172 if (unlikely(ptr == NULL)) return NULL;
1173 pname = talloc_get_name(ptr);
1174 if (likely(pname == name || strcmp(pname, name) == 0)) {
1175 return discard_const_p(void, ptr);
1177 return NULL;
1180 static void talloc_abort_type_mismatch(const char *location,
1181 const char *name,
1182 const char *expected)
1184 const char *reason;
1186 reason = talloc_asprintf(NULL,
1187 "%s: Type mismatch: name[%s] expected[%s]",
1188 location,
1189 name?name:"NULL",
1190 expected);
1191 if (!reason) {
1192 reason = "Type mismatch";
1195 talloc_abort(reason);
1198 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
1200 const char *pname;
1202 if (unlikely(ptr == NULL)) {
1203 talloc_abort_type_mismatch(location, NULL, name);
1204 return NULL;
1207 pname = talloc_get_name(ptr);
1208 if (likely(pname == name || strcmp(pname, name) == 0)) {
1209 return discard_const_p(void, ptr);
1212 talloc_abort_type_mismatch(location, pname, name);
1213 return NULL;
1217 this is for compatibility with older versions of talloc
1219 _PUBLIC_ void *talloc_init(const char *fmt, ...)
1221 va_list ap;
1222 void *ptr;
1223 const char *name;
1225 ptr = __talloc(NULL, 0);
1226 if (unlikely(ptr == NULL)) return NULL;
1228 va_start(ap, fmt);
1229 name = talloc_set_name_v(ptr, fmt, ap);
1230 va_end(ap);
1232 if (unlikely(name == NULL)) {
1233 _talloc_free_internal(ptr, __location__);
1234 return NULL;
1237 return ptr;
1240 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
1241 void *ptr,
1242 const char *location)
1244 while (tc->child) {
1245 /* we need to work out who will own an abandoned child
1246 if it cannot be freed. In priority order, the first
1247 choice is owner of any remaining reference to this
1248 pointer, the second choice is our parent, and the
1249 final choice is the null context. */
1250 void *child = TC_PTR_FROM_CHUNK(tc->child);
1251 const void *new_parent = null_context;
1252 if (unlikely(tc->child->refs)) {
1253 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1254 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1256 if (unlikely(_talloc_free_internal(child, location) == -1)) {
1257 if (new_parent == null_context) {
1258 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1259 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1261 _talloc_steal_internal(new_parent, child);
1267 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1268 should probably not be used in new code. It's in here to keep the talloc
1269 code consistent across Samba 3 and 4.
1271 _PUBLIC_ void talloc_free_children(void *ptr)
1273 struct talloc_chunk *tc_name = NULL;
1274 struct talloc_chunk *tc;
1276 if (unlikely(ptr == NULL)) {
1277 return;
1280 tc = talloc_chunk_from_ptr(ptr);
1282 /* we do not want to free the context name if it is a child .. */
1283 if (likely(tc->child)) {
1284 for (tc_name = tc->child; tc_name; tc_name = tc_name->next) {
1285 if (tc->name == TC_PTR_FROM_CHUNK(tc_name)) break;
1287 if (tc_name) {
1288 _TLIST_REMOVE(tc->child, tc_name);
1289 if (tc->child) {
1290 tc->child->parent = tc;
1295 _talloc_free_children_internal(tc, ptr, __location__);
1297 /* .. so we put it back after all other children have been freed */
1298 if (tc_name) {
1299 if (tc->child) {
1300 tc->child->parent = NULL;
1302 tc_name->parent = tc;
1303 _TLIST_ADD(tc->child, tc_name);
1308 Allocate a bit of memory as a child of an existing pointer
1310 _PUBLIC_ void *_talloc(const void *context, size_t size)
1312 return __talloc(context, size);
1316 externally callable talloc_set_name_const()
1318 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1320 _talloc_set_name_const(ptr, name);
1324 create a named talloc pointer. Any talloc pointer can be named, and
1325 talloc_named() operates just like talloc() except that it allows you
1326 to name the pointer.
1328 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1330 return _talloc_named_const(context, size, name);
1334 free a talloc pointer. This also frees all child pointers of this
1335 pointer recursively
1337 return 0 if the memory is actually freed, otherwise -1. The memory
1338 will not be freed if the ref_count is > 1 or the destructor (if
1339 any) returns non-zero
1341 _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1343 struct talloc_chunk *tc;
1345 if (unlikely(ptr == NULL)) {
1346 return -1;
1349 tc = talloc_chunk_from_ptr(ptr);
1351 if (unlikely(tc->refs != NULL)) {
1352 struct talloc_reference_handle *h;
1354 if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1355 /* in this case we do know which parent should
1356 get this pointer, as there is really only
1357 one parent */
1358 return talloc_unlink(null_context, ptr);
1361 talloc_log("ERROR: talloc_free with references at %s\n",
1362 location);
1364 for (h=tc->refs; h; h=h->next) {
1365 talloc_log("\treference at %s\n",
1366 h->location);
1368 return -1;
1371 return _talloc_free_internal(ptr, location);
1377 A talloc version of realloc. The context argument is only used if
1378 ptr is NULL
1380 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1382 struct talloc_chunk *tc;
1383 void *new_ptr;
1384 bool malloced = false;
1385 union talloc_pool_chunk *pool_tc = NULL;
1387 /* size zero is equivalent to free() */
1388 if (unlikely(size == 0)) {
1389 talloc_unlink(context, ptr);
1390 return NULL;
1393 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1394 return NULL;
1397 /* realloc(NULL) is equivalent to malloc() */
1398 if (ptr == NULL) {
1399 return _talloc_named_const(context, size, name);
1402 tc = talloc_chunk_from_ptr(ptr);
1404 /* don't allow realloc on referenced pointers */
1405 if (unlikely(tc->refs)) {
1406 return NULL;
1409 /* don't let anybody try to realloc a talloc_pool */
1410 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1411 return NULL;
1414 /* handle realloc inside a talloc_pool */
1415 if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
1416 pool_tc = (union talloc_pool_chunk *)tc->pool;
1419 #if (ALWAYS_REALLOC == 0)
1420 /* don't shrink if we have less than 1k to gain */
1421 if (size < tc->size) {
1422 if (pool_tc) {
1423 void *next_tc = tc_next_chunk(tc);
1424 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1425 tc->size = size;
1426 if (next_tc == pool_tc->hdr.c.pool) {
1427 /* note: tc->size has changed, so this works */
1428 pool_tc->hdr.c.pool = tc_next_chunk(tc);
1430 return ptr;
1431 } else if ((tc->size - size) < 1024) {
1433 * if we call TC_INVALIDATE_SHRINK_CHUNK() here
1434 * we would need to call TC_UNDEFINE_GROW_CHUNK()
1435 * after each realloc call, which slows down
1436 * testing a lot :-(.
1438 * That is why we only mark memory as undefined here.
1440 TC_UNDEFINE_SHRINK_CHUNK(tc, size);
1442 /* do not shrink if we have less than 1k to gain */
1443 tc->size = size;
1444 return ptr;
1446 } else if (tc->size == size) {
1448 * do not change the pointer if it is exactly
1449 * the same size.
1451 return ptr;
1453 #endif
1455 /* by resetting magic we catch users of the old memory */
1456 tc->flags |= TALLOC_FLAG_FREE;
1458 #if ALWAYS_REALLOC
1459 if (pool_tc) {
1460 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1461 pool_tc->hdr.object_count--;
1463 if (new_ptr == NULL) {
1464 new_ptr = malloc(TC_HDR_SIZE+size);
1465 malloced = true;
1468 if (new_ptr) {
1469 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1470 TC_INVALIDATE_FULL_CHUNK(tc);
1472 } else {
1473 new_ptr = malloc(size + TC_HDR_SIZE);
1474 if (new_ptr) {
1475 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1476 free(tc);
1479 #else
1480 if (pool_tc) {
1481 void *next_tc = tc_next_chunk(tc);
1482 size_t old_chunk_size = TC_ALIGN16(TC_HDR_SIZE + tc->size);
1483 size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
1484 size_t space_needed;
1485 size_t space_left;
1486 unsigned int chunk_count = pool_tc->hdr.object_count;
1488 if (!(pool_tc->hdr.c.flags & TALLOC_FLAG_FREE)) {
1489 chunk_count -= 1;
1492 if (chunk_count == 1) {
1494 * optimize for the case where 'tc' is the only
1495 * chunk in the pool.
1497 char *start = tc_pool_first_chunk(pool_tc);
1498 space_needed = new_chunk_size;
1499 space_left = (char *)tc_pool_end(pool_tc) - start;
1501 if (space_left >= space_needed) {
1502 size_t old_used = TC_HDR_SIZE + tc->size;
1503 size_t new_used = TC_HDR_SIZE + size;
1504 new_ptr = start;
1505 memmove(new_ptr, tc, old_used);
1507 tc = (struct talloc_chunk *)new_ptr;
1508 TC_UNDEFINE_GROW_CHUNK(tc, size);
1511 * first we do not align the pool pointer
1512 * because we want to invalidate the padding
1513 * too.
1515 pool_tc->hdr.c.pool = new_used + (char *)new_ptr;
1516 tc_invalidate_pool(pool_tc);
1518 /* now the aligned pointer */
1519 pool_tc->hdr.c.pool = new_chunk_size + (char *)new_ptr;
1520 goto got_new_ptr;
1523 next_tc = NULL;
1526 if (new_chunk_size == old_chunk_size) {
1527 TC_UNDEFINE_GROW_CHUNK(tc, size);
1528 tc->flags &= ~TALLOC_FLAG_FREE;
1529 tc->size = size;
1530 return ptr;
1533 if (next_tc == pool_tc->hdr.c.pool) {
1535 * optimize for the case where 'tc' is the last
1536 * chunk in the pool.
1538 space_needed = new_chunk_size - old_chunk_size;
1539 space_left = tc_pool_space_left(pool_tc);
1541 if (space_left >= space_needed) {
1542 TC_UNDEFINE_GROW_CHUNK(tc, size);
1543 tc->flags &= ~TALLOC_FLAG_FREE;
1544 tc->size = size;
1545 pool_tc->hdr.c.pool = tc_next_chunk(tc);
1546 return ptr;
1550 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1552 if (new_ptr == NULL) {
1553 new_ptr = malloc(TC_HDR_SIZE+size);
1554 malloced = true;
1557 if (new_ptr) {
1558 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1560 _talloc_free_poolmem(tc, __location__ "_talloc_realloc");
1563 else {
1564 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1566 got_new_ptr:
1567 #endif
1568 if (unlikely(!new_ptr)) {
1569 tc->flags &= ~TALLOC_FLAG_FREE;
1570 return NULL;
1573 tc = (struct talloc_chunk *)new_ptr;
1574 tc->flags &= ~TALLOC_FLAG_FREE;
1575 if (malloced) {
1576 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1578 if (tc->parent) {
1579 tc->parent->child = tc;
1581 if (tc->child) {
1582 tc->child->parent = tc;
1585 if (tc->prev) {
1586 tc->prev->next = tc;
1588 if (tc->next) {
1589 tc->next->prev = tc;
1592 tc->size = size;
1593 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1595 return TC_PTR_FROM_CHUNK(tc);
1599 a wrapper around talloc_steal() for situations where you are moving a pointer
1600 between two structures, and want the old pointer to be set to NULL
1602 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
1604 const void **pptr = discard_const_p(const void *,_pptr);
1605 void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1606 (*pptr) = NULL;
1607 return ret;
1611 return the total size of a talloc pool (subtree)
1613 _PUBLIC_ size_t talloc_total_size(const void *ptr)
1615 size_t total = 0;
1616 struct talloc_chunk *c, *tc;
1618 if (ptr == NULL) {
1619 ptr = null_context;
1621 if (ptr == NULL) {
1622 return 0;
1625 tc = talloc_chunk_from_ptr(ptr);
1627 if (tc->flags & TALLOC_FLAG_LOOP) {
1628 return 0;
1631 tc->flags |= TALLOC_FLAG_LOOP;
1633 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1634 total = tc->size;
1636 for (c=tc->child;c;c=c->next) {
1637 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1640 tc->flags &= ~TALLOC_FLAG_LOOP;
1642 return total;
1646 return the total number of blocks in a talloc pool (subtree)
1648 _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
1650 size_t total = 0;
1651 struct talloc_chunk *c, *tc;
1653 if (ptr == NULL) {
1654 ptr = null_context;
1656 if (ptr == NULL) {
1657 return 0;
1660 tc = talloc_chunk_from_ptr(ptr);
1662 if (tc->flags & TALLOC_FLAG_LOOP) {
1663 return 0;
1666 tc->flags |= TALLOC_FLAG_LOOP;
1668 total++;
1669 for (c=tc->child;c;c=c->next) {
1670 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1673 tc->flags &= ~TALLOC_FLAG_LOOP;
1675 return total;
1679 return the number of external references to a pointer
1681 _PUBLIC_ size_t talloc_reference_count(const void *ptr)
1683 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1684 struct talloc_reference_handle *h;
1685 size_t ret = 0;
1687 for (h=tc->refs;h;h=h->next) {
1688 ret++;
1690 return ret;
1694 report on memory usage by all children of a pointer, giving a full tree view
1696 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1697 void (*callback)(const void *ptr,
1698 int depth, int max_depth,
1699 int is_ref,
1700 void *private_data),
1701 void *private_data)
1703 struct talloc_chunk *c, *tc;
1705 if (ptr == NULL) {
1706 ptr = null_context;
1708 if (ptr == NULL) return;
1710 tc = talloc_chunk_from_ptr(ptr);
1712 if (tc->flags & TALLOC_FLAG_LOOP) {
1713 return;
1716 callback(ptr, depth, max_depth, 0, private_data);
1718 if (max_depth >= 0 && depth >= max_depth) {
1719 return;
1722 tc->flags |= TALLOC_FLAG_LOOP;
1723 for (c=tc->child;c;c=c->next) {
1724 if (c->name == TALLOC_MAGIC_REFERENCE) {
1725 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1726 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1727 } else {
1728 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1731 tc->flags &= ~TALLOC_FLAG_LOOP;
1734 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1736 const char *name = talloc_get_name(ptr);
1737 FILE *f = (FILE *)_f;
1739 if (is_ref) {
1740 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1741 return;
1744 if (depth == 0) {
1745 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1746 (max_depth < 0 ? "full " :""), name,
1747 (unsigned long)talloc_total_size(ptr),
1748 (unsigned long)talloc_total_blocks(ptr));
1749 return;
1752 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1753 depth*4, "",
1754 name,
1755 (unsigned long)talloc_total_size(ptr),
1756 (unsigned long)talloc_total_blocks(ptr),
1757 (int)talloc_reference_count(ptr), ptr);
1759 #if 0
1760 fprintf(f, "content: ");
1761 if (talloc_total_size(ptr)) {
1762 int tot = talloc_total_size(ptr);
1763 int i;
1765 for (i = 0; i < tot; i++) {
1766 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1767 fprintf(f, "%c", ((char *)ptr)[i]);
1768 } else {
1769 fprintf(f, "~%02x", ((char *)ptr)[i]);
1773 fprintf(f, "\n");
1774 #endif
1778 report on memory usage by all children of a pointer, giving a full tree view
1780 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1782 if (f) {
1783 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1784 fflush(f);
1789 report on memory usage by all children of a pointer, giving a full tree view
1791 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
1793 talloc_report_depth_file(ptr, 0, -1, f);
1797 report on memory usage by all children of a pointer
1799 _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
1801 talloc_report_depth_file(ptr, 0, 1, f);
1805 report on any memory hanging off the null context
1807 static void talloc_report_null(void)
1809 if (talloc_total_size(null_context) != 0) {
1810 talloc_report(null_context, stderr);
1815 report on any memory hanging off the null context
1817 static void talloc_report_null_full(void)
1819 if (talloc_total_size(null_context) != 0) {
1820 talloc_report_full(null_context, stderr);
1825 enable tracking of the NULL context
1827 _PUBLIC_ void talloc_enable_null_tracking(void)
1829 if (null_context == NULL) {
1830 null_context = _talloc_named_const(NULL, 0, "null_context");
1831 if (autofree_context != NULL) {
1832 talloc_reparent(NULL, null_context, autofree_context);
1838 enable tracking of the NULL context, not moving the autofree context
1839 into the NULL context. This is needed for the talloc testsuite
1841 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
1843 if (null_context == NULL) {
1844 null_context = _talloc_named_const(NULL, 0, "null_context");
1849 disable tracking of the NULL context
1851 _PUBLIC_ void talloc_disable_null_tracking(void)
1853 if (null_context != NULL) {
1854 /* we have to move any children onto the real NULL
1855 context */
1856 struct talloc_chunk *tc, *tc2;
1857 tc = talloc_chunk_from_ptr(null_context);
1858 for (tc2 = tc->child; tc2; tc2=tc2->next) {
1859 if (tc2->parent == tc) tc2->parent = NULL;
1860 if (tc2->prev == tc) tc2->prev = NULL;
1862 for (tc2 = tc->next; tc2; tc2=tc2->next) {
1863 if (tc2->parent == tc) tc2->parent = NULL;
1864 if (tc2->prev == tc) tc2->prev = NULL;
1866 tc->child = NULL;
1867 tc->next = NULL;
1869 talloc_free(null_context);
1870 null_context = NULL;
1874 enable leak reporting on exit
1876 _PUBLIC_ void talloc_enable_leak_report(void)
1878 talloc_enable_null_tracking();
1879 atexit(talloc_report_null);
1883 enable full leak reporting on exit
1885 _PUBLIC_ void talloc_enable_leak_report_full(void)
1887 talloc_enable_null_tracking();
1888 atexit(talloc_report_null_full);
1892 talloc and zero memory.
1894 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
1896 void *p = _talloc_named_const(ctx, size, name);
1898 if (p) {
1899 memset(p, '\0', size);
1902 return p;
1906 memdup with a talloc.
1908 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1910 void *newp = _talloc_named_const(t, size, name);
1912 if (likely(newp)) {
1913 memcpy(newp, p, size);
1916 return newp;
1919 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1921 char *ret;
1923 ret = (char *)__talloc(t, len + 1);
1924 if (unlikely(!ret)) return NULL;
1926 memcpy(ret, p, len);
1927 ret[len] = 0;
1929 _talloc_set_name_const(ret, ret);
1930 return ret;
1934 strdup with a talloc
1936 _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
1938 if (unlikely(!p)) return NULL;
1939 return __talloc_strlendup(t, p, strlen(p));
1943 strndup with a talloc
1945 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
1947 if (unlikely(!p)) return NULL;
1948 return __talloc_strlendup(t, p, strnlen(p, n));
1951 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1952 const char *a, size_t alen)
1954 char *ret;
1956 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1957 if (unlikely(!ret)) return NULL;
1959 /* append the string and the trailing \0 */
1960 memcpy(&ret[slen], a, alen);
1961 ret[slen+alen] = 0;
1963 _talloc_set_name_const(ret, ret);
1964 return ret;
1968 * Appends at the end of the string.
1970 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
1972 if (unlikely(!s)) {
1973 return talloc_strdup(NULL, a);
1976 if (unlikely(!a)) {
1977 return s;
1980 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1984 * Appends at the end of the talloc'ed buffer,
1985 * not the end of the string.
1987 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
1989 size_t slen;
1991 if (unlikely(!s)) {
1992 return talloc_strdup(NULL, a);
1995 if (unlikely(!a)) {
1996 return s;
1999 slen = talloc_get_size(s);
2000 if (likely(slen > 0)) {
2001 slen--;
2004 return __talloc_strlendup_append(s, slen, a, strlen(a));
2008 * Appends at the end of the string.
2010 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
2012 if (unlikely(!s)) {
2013 return talloc_strndup(NULL, a, n);
2016 if (unlikely(!a)) {
2017 return s;
2020 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
2024 * Appends at the end of the talloc'ed buffer,
2025 * not the end of the string.
2027 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
2029 size_t slen;
2031 if (unlikely(!s)) {
2032 return talloc_strndup(NULL, a, n);
2035 if (unlikely(!a)) {
2036 return s;
2039 slen = talloc_get_size(s);
2040 if (likely(slen > 0)) {
2041 slen--;
2044 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
2047 #ifndef HAVE_VA_COPY
2048 #ifdef HAVE___VA_COPY
2049 #define va_copy(dest, src) __va_copy(dest, src)
2050 #else
2051 #define va_copy(dest, src) (dest) = (src)
2052 #endif
2053 #endif
2055 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
2057 int len;
2058 char *ret;
2059 va_list ap2;
2060 char c;
2062 /* this call looks strange, but it makes it work on older solaris boxes */
2063 va_copy(ap2, ap);
2064 len = vsnprintf(&c, 1, fmt, ap2);
2065 va_end(ap2);
2066 if (unlikely(len < 0)) {
2067 return NULL;
2070 ret = (char *)__talloc(t, len+1);
2071 if (unlikely(!ret)) return NULL;
2073 va_copy(ap2, ap);
2074 vsnprintf(ret, len+1, fmt, ap2);
2075 va_end(ap2);
2077 _talloc_set_name_const(ret, ret);
2078 return ret;
2083 Perform string formatting, and return a pointer to newly allocated
2084 memory holding the result, inside a memory pool.
2086 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
2088 va_list ap;
2089 char *ret;
2091 va_start(ap, fmt);
2092 ret = talloc_vasprintf(t, fmt, ap);
2093 va_end(ap);
2094 return ret;
2097 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2098 const char *fmt, va_list ap)
2099 PRINTF_ATTRIBUTE(3,0);
2101 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2102 const char *fmt, va_list ap)
2104 ssize_t alen;
2105 va_list ap2;
2106 char c;
2108 va_copy(ap2, ap);
2109 alen = vsnprintf(&c, 1, fmt, ap2);
2110 va_end(ap2);
2112 if (alen <= 0) {
2113 /* Either the vsnprintf failed or the format resulted in
2114 * no characters being formatted. In the former case, we
2115 * ought to return NULL, in the latter we ought to return
2116 * the original string. Most current callers of this
2117 * function expect it to never return NULL.
2119 return s;
2122 s = talloc_realloc(NULL, s, char, slen + alen + 1);
2123 if (!s) return NULL;
2125 va_copy(ap2, ap);
2126 vsnprintf(s + slen, alen + 1, fmt, ap2);
2127 va_end(ap2);
2129 _talloc_set_name_const(s, s);
2130 return s;
2134 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2135 * and return @p s, which may have moved. Good for gradually
2136 * accumulating output into a string buffer. Appends at the end
2137 * of the string.
2139 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
2141 if (unlikely(!s)) {
2142 return talloc_vasprintf(NULL, fmt, ap);
2145 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
2149 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2150 * and return @p s, which may have moved. Always appends at the
2151 * end of the talloc'ed buffer, not the end of the string.
2153 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
2155 size_t slen;
2157 if (unlikely(!s)) {
2158 return talloc_vasprintf(NULL, fmt, ap);
2161 slen = talloc_get_size(s);
2162 if (likely(slen > 0)) {
2163 slen--;
2166 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
2170 Realloc @p s to append the formatted result of @p fmt and return @p
2171 s, which may have moved. Good for gradually accumulating output
2172 into a string buffer.
2174 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
2176 va_list ap;
2178 va_start(ap, fmt);
2179 s = talloc_vasprintf_append(s, fmt, ap);
2180 va_end(ap);
2181 return s;
2185 Realloc @p s to append the formatted result of @p fmt and return @p
2186 s, which may have moved. Good for gradually accumulating output
2187 into a buffer.
2189 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
2191 va_list ap;
2193 va_start(ap, fmt);
2194 s = talloc_vasprintf_append_buffer(s, fmt, ap);
2195 va_end(ap);
2196 return s;
2200 alloc an array, checking for integer overflow in the array size
2202 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2204 if (count >= MAX_TALLOC_SIZE/el_size) {
2205 return NULL;
2207 return _talloc_named_const(ctx, el_size * count, name);
2211 alloc an zero array, checking for integer overflow in the array size
2213 _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2215 if (count >= MAX_TALLOC_SIZE/el_size) {
2216 return NULL;
2218 return _talloc_zero(ctx, el_size * count, name);
2222 realloc an array, checking for integer overflow in the array size
2224 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
2226 if (count >= MAX_TALLOC_SIZE/el_size) {
2227 return NULL;
2229 return _talloc_realloc(ctx, ptr, el_size * count, name);
2233 a function version of talloc_realloc(), so it can be passed as a function pointer
2234 to libraries that want a realloc function (a realloc function encapsulates
2235 all the basic capabilities of an allocation library, which is why this is useful)
2237 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
2239 return _talloc_realloc(context, ptr, size, NULL);
2243 static int talloc_autofree_destructor(void *ptr)
2245 autofree_context = NULL;
2246 return 0;
2249 static void talloc_autofree(void)
2251 talloc_free(autofree_context);
2255 return a context which will be auto-freed on exit
2256 this is useful for reducing the noise in leak reports
2258 _PUBLIC_ void *talloc_autofree_context(void)
2260 if (autofree_context == NULL) {
2261 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
2262 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
2263 atexit(talloc_autofree);
2265 return autofree_context;
2268 _PUBLIC_ size_t talloc_get_size(const void *context)
2270 struct talloc_chunk *tc;
2272 if (context == NULL) {
2273 context = null_context;
2275 if (context == NULL) {
2276 return 0;
2279 tc = talloc_chunk_from_ptr(context);
2281 return tc->size;
2285 find a parent of this context that has the given name, if any
2287 _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
2289 struct talloc_chunk *tc;
2291 if (context == NULL) {
2292 return NULL;
2295 tc = talloc_chunk_from_ptr(context);
2296 while (tc) {
2297 if (tc->name && strcmp(tc->name, name) == 0) {
2298 return TC_PTR_FROM_CHUNK(tc);
2300 while (tc && tc->prev) tc = tc->prev;
2301 if (tc) {
2302 tc = tc->parent;
2305 return NULL;
2309 show the parentage of a context
2311 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
2313 struct talloc_chunk *tc;
2315 if (context == NULL) {
2316 fprintf(file, "talloc no parents for NULL\n");
2317 return;
2320 tc = talloc_chunk_from_ptr(context);
2321 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
2322 while (tc) {
2323 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
2324 while (tc && tc->prev) tc = tc->prev;
2325 if (tc) {
2326 tc = tc->parent;
2329 fflush(file);
2333 return 1 if ptr is a parent of context
2335 static int _talloc_is_parent(const void *context, const void *ptr, int depth)
2337 struct talloc_chunk *tc;
2339 if (context == NULL) {
2340 return 0;
2343 tc = talloc_chunk_from_ptr(context);
2344 while (tc && depth > 0) {
2345 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2346 while (tc && tc->prev) tc = tc->prev;
2347 if (tc) {
2348 tc = tc->parent;
2349 depth--;
2352 return 0;
2356 return 1 if ptr is a parent of context
2358 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2360 return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);