s3: In winbind, close parent/child sockets
[Samba.git] / lib / talloc / talloc.c
blob91452bfadac1ecd4a30e1cccd21dacb0f7566f65
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 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
182 /* Mark the new bytes as undefined */
183 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { \
184 size_t _old_used = TC_HDR_SIZE + (_tc)->size; \
185 size_t _new_used = TC_HDR_SIZE + (_new_size); \
186 size_t _flen = _new_used - _old_used; \
187 char *_fptr = _old_used + (char *)(_tc); \
188 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
189 } while (0)
190 #else
191 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
192 #endif
194 #define TC_UNDEFINE_GROW_CHUNK(_tc, _new_size) do { \
195 TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
196 } while (0)
198 struct talloc_reference_handle {
199 struct talloc_reference_handle *next, *prev;
200 void *ptr;
201 const char *location;
204 typedef int (*talloc_destructor_t)(void *);
206 struct talloc_chunk {
207 struct talloc_chunk *next, *prev;
208 struct talloc_chunk *parent, *child;
209 struct talloc_reference_handle *refs;
210 talloc_destructor_t destructor;
211 const char *name;
212 size_t size;
213 unsigned flags;
216 * "pool" has dual use:
218 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
219 * marks the end of the currently allocated area.
221 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
222 * is a pointer to the struct talloc_chunk of the pool that it was
223 * allocated from. This way children can quickly find the pool to chew
224 * from.
226 void *pool;
229 /* 16 byte alignment seems to keep everyone happy */
230 #define TC_ALIGN16(s) (((s)+15)&~15)
231 #define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
232 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
234 _PUBLIC_ int talloc_version_major(void)
236 return TALLOC_VERSION_MAJOR;
239 _PUBLIC_ int talloc_version_minor(void)
241 return TALLOC_VERSION_MINOR;
244 static void (*talloc_log_fn)(const char *message);
246 _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
248 talloc_log_fn = log_fn;
251 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
252 static void talloc_log(const char *fmt, ...)
254 va_list ap;
255 char *message;
257 if (!talloc_log_fn) {
258 return;
261 va_start(ap, fmt);
262 message = talloc_vasprintf(NULL, fmt, ap);
263 va_end(ap);
265 talloc_log_fn(message);
266 talloc_free(message);
269 static void talloc_log_stderr(const char *message)
271 fprintf(stderr, "%s", message);
274 _PUBLIC_ void talloc_set_log_stderr(void)
276 talloc_set_log_fn(talloc_log_stderr);
279 static void (*talloc_abort_fn)(const char *reason);
281 _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
283 talloc_abort_fn = abort_fn;
286 static void talloc_abort(const char *reason)
288 talloc_log("%s\n", reason);
290 if (!talloc_abort_fn) {
291 TALLOC_ABORT(reason);
294 talloc_abort_fn(reason);
297 static void talloc_abort_magic(unsigned magic)
299 unsigned striped = magic - TALLOC_MAGIC_BASE;
300 unsigned major = (striped & 0xFFFFF000) >> 12;
301 unsigned minor = (striped & 0x00000FF0) >> 4;
302 talloc_log("Bad talloc magic[0x%08X/%u/%u] expected[0x%08X/%u/%u]\n",
303 magic, major, minor,
304 TALLOC_MAGIC, TALLOC_VERSION_MAJOR, TALLOC_VERSION_MINOR);
305 talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
308 static void talloc_abort_access_after_free(void)
310 talloc_abort("Bad talloc magic value - access after free");
313 static void talloc_abort_unknown_value(void)
315 talloc_abort("Bad talloc magic value - unknown value");
318 /* panic if we get a bad magic value */
319 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
321 const char *pp = (const char *)ptr;
322 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
323 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
324 if ((tc->flags & (~0xFFF)) == TALLOC_MAGIC_BASE) {
325 talloc_abort_magic(tc->flags & (~0xF));
326 return NULL;
329 if (tc->flags & TALLOC_FLAG_FREE) {
330 talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
331 talloc_abort_access_after_free();
332 return NULL;
333 } else {
334 talloc_abort_unknown_value();
335 return NULL;
338 return tc;
341 /* hook into the front of the list */
342 #define _TLIST_ADD(list, p) \
343 do { \
344 if (!(list)) { \
345 (list) = (p); \
346 (p)->next = (p)->prev = NULL; \
347 } else { \
348 (list)->prev = (p); \
349 (p)->next = (list); \
350 (p)->prev = NULL; \
351 (list) = (p); \
353 } while (0)
355 /* remove an element from a list - element doesn't have to be in list. */
356 #define _TLIST_REMOVE(list, p) \
357 do { \
358 if ((p) == (list)) { \
359 (list) = (p)->next; \
360 if (list) (list)->prev = NULL; \
361 } else { \
362 if ((p)->prev) (p)->prev->next = (p)->next; \
363 if ((p)->next) (p)->next->prev = (p)->prev; \
365 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
366 } while (0)
370 return the parent chunk of a pointer
372 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
374 struct talloc_chunk *tc;
376 if (unlikely(ptr == NULL)) {
377 return NULL;
380 tc = talloc_chunk_from_ptr(ptr);
381 while (tc->prev) tc=tc->prev;
383 return tc->parent;
386 _PUBLIC_ void *talloc_parent(const void *ptr)
388 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
389 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
393 find parents name
395 _PUBLIC_ const char *talloc_parent_name(const void *ptr)
397 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
398 return tc? tc->name : NULL;
402 A pool carries an in-pool object count count in the first 16 bytes.
403 bytes. This is done to support talloc_steal() to a parent outside of the
404 pool. The count includes the pool itself, so a talloc_free() on a pool will
405 only destroy the pool if the count has dropped to zero. A talloc_free() of a
406 pool member will reduce the count, and eventually also call free(3) on the
407 pool memory.
409 The object count is not put into "struct talloc_chunk" because it is only
410 relevant for talloc pools and the alignment to 16 bytes would increase the
411 memory footprint of each talloc chunk by those 16 bytes.
414 #define TALLOC_POOL_HDR_SIZE 16
416 #define TC_POOL_SPACE_LEFT(_pool_tc) \
417 PTR_DIFF(TC_HDR_SIZE + (_pool_tc)->size + (char *)(_pool_tc), \
418 (_pool_tc)->pool)
420 #define TC_POOL_FIRST_CHUNK(_pool_tc) \
421 ((void *)(TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE + (char *)(_pool_tc)))
423 #define TC_POOLMEM_CHUNK_SIZE(_tc) \
424 TC_ALIGN16(TC_HDR_SIZE + (_tc)->size)
426 #define TC_POOLMEM_NEXT_CHUNK(_tc) \
427 ((void *)(TC_POOLMEM_CHUNK_SIZE(tc) + (char*)(_tc)))
429 /* Mark the whole remaining pool as not accessable */
430 #define TC_INVALIDATE_FILL_POOL(_pool_tc) do { \
431 if (unlikely(talloc_fill.enabled)) { \
432 size_t _flen = TC_POOL_SPACE_LEFT(_pool_tc); \
433 char *_fptr = (char *)(_pool_tc)->pool; \
434 memset(_fptr, talloc_fill.fill_value, _flen); \
436 } while(0)
438 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
439 /* Mark the whole remaining pool as not accessable */
440 #define TC_INVALIDATE_VALGRIND_POOL(_pool_tc) do { \
441 size_t _flen = TC_POOL_SPACE_LEFT(_pool_tc); \
442 char *_fptr = (char *)(_pool_tc)->pool; \
443 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
444 } while(0)
445 #else
446 #define TC_INVALIDATE_VALGRIND_POOL(_pool_tc) do { } while (0)
447 #endif
449 #define TC_INVALIDATE_POOL(_pool_tc) do { \
450 TC_INVALIDATE_FILL_POOL(_pool_tc); \
451 TC_INVALIDATE_VALGRIND_POOL(_pool_tc); \
452 } while (0)
454 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
456 return (unsigned int *)((char *)tc + TC_HDR_SIZE);
460 Allocate from a pool
463 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
464 size_t size)
466 struct talloc_chunk *pool_ctx = NULL;
467 size_t space_left;
468 struct talloc_chunk *result;
469 size_t chunk_size;
471 if (parent == NULL) {
472 return NULL;
475 if (parent->flags & TALLOC_FLAG_POOL) {
476 pool_ctx = parent;
478 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
479 pool_ctx = (struct talloc_chunk *)parent->pool;
482 if (pool_ctx == NULL) {
483 return NULL;
486 space_left = TC_POOL_SPACE_LEFT(pool_ctx);
489 * Align size to 16 bytes
491 chunk_size = TC_ALIGN16(size);
493 if (space_left < chunk_size) {
494 return NULL;
497 result = (struct talloc_chunk *)pool_ctx->pool;
499 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
500 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
501 #endif
503 pool_ctx->pool = (void *)((char *)result + chunk_size);
505 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
506 result->pool = pool_ctx;
508 *talloc_pool_objectcount(pool_ctx) += 1;
510 return result;
514 Allocate a bit of memory as a child of an existing pointer
516 static inline void *__talloc(const void *context, size_t size)
518 struct talloc_chunk *tc = NULL;
520 if (unlikely(context == NULL)) {
521 context = null_context;
524 if (unlikely(size >= MAX_TALLOC_SIZE)) {
525 return NULL;
528 if (context != NULL) {
529 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
530 TC_HDR_SIZE+size);
533 if (tc == NULL) {
534 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
535 if (unlikely(tc == NULL)) return NULL;
536 tc->flags = TALLOC_MAGIC;
537 tc->pool = NULL;
540 tc->size = size;
541 tc->destructor = NULL;
542 tc->child = NULL;
543 tc->name = NULL;
544 tc->refs = NULL;
546 if (likely(context)) {
547 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
549 if (parent->child) {
550 parent->child->parent = NULL;
551 tc->next = parent->child;
552 tc->next->prev = tc;
553 } else {
554 tc->next = NULL;
556 tc->parent = parent;
557 tc->prev = NULL;
558 parent->child = tc;
559 } else {
560 tc->next = tc->prev = tc->parent = NULL;
563 return TC_PTR_FROM_CHUNK(tc);
567 * Create a talloc pool
570 _PUBLIC_ void *talloc_pool(const void *context, size_t size)
572 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
573 struct talloc_chunk *tc;
575 if (unlikely(result == NULL)) {
576 return NULL;
579 tc = talloc_chunk_from_ptr(result);
581 tc->flags |= TALLOC_FLAG_POOL;
582 tc->pool = TC_POOL_FIRST_CHUNK(tc);
584 *talloc_pool_objectcount(tc) = 1;
586 TC_INVALIDATE_POOL(tc);
588 return result;
592 setup a destructor to be called on free of a pointer
593 the destructor should return 0 on success, or -1 on failure.
594 if the destructor fails then the free is failed, and the memory can
595 be continued to be used
597 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
599 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
600 tc->destructor = destructor;
604 increase the reference count on a piece of memory.
606 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
608 if (unlikely(!talloc_reference(null_context, ptr))) {
609 return -1;
611 return 0;
615 helper for talloc_reference()
617 this is referenced by a function pointer and should not be inline
619 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
621 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
622 _TLIST_REMOVE(ptr_tc->refs, handle);
623 return 0;
627 more efficient way to add a name to a pointer - the name must point to a
628 true string constant
630 static inline void _talloc_set_name_const(const void *ptr, const char *name)
632 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
633 tc->name = name;
637 internal talloc_named_const()
639 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
641 void *ptr;
643 ptr = __talloc(context, size);
644 if (unlikely(ptr == NULL)) {
645 return NULL;
648 _talloc_set_name_const(ptr, name);
650 return ptr;
654 make a secondary reference to a pointer, hanging off the given context.
655 the pointer remains valid until both the original caller and this given
656 context are freed.
658 the major use for this is when two different structures need to reference the
659 same underlying data, and you want to be able to free the two instances separately,
660 and in either order
662 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
664 struct talloc_chunk *tc;
665 struct talloc_reference_handle *handle;
666 if (unlikely(ptr == NULL)) return NULL;
668 tc = talloc_chunk_from_ptr(ptr);
669 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
670 sizeof(struct talloc_reference_handle),
671 TALLOC_MAGIC_REFERENCE);
672 if (unlikely(handle == NULL)) return NULL;
674 /* note that we hang the destructor off the handle, not the
675 main context as that allows the caller to still setup their
676 own destructor on the context if they want to */
677 talloc_set_destructor(handle, talloc_reference_destructor);
678 handle->ptr = discard_const_p(void, ptr);
679 handle->location = location;
680 _TLIST_ADD(tc->refs, handle);
681 return handle->ptr;
684 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
687 internal talloc_free call
689 static inline int _talloc_free_internal(void *ptr, const char *location)
691 struct talloc_chunk *tc;
693 if (unlikely(ptr == NULL)) {
694 return -1;
697 /* possibly initialised the talloc fill value */
698 if (unlikely(!talloc_fill.initialised)) {
699 const char *fill = getenv(TALLOC_FILL_ENV);
700 if (fill != NULL) {
701 talloc_fill.enabled = true;
702 talloc_fill.fill_value = strtoul(fill, NULL, 0);
704 talloc_fill.initialised = true;
707 tc = talloc_chunk_from_ptr(ptr);
709 if (unlikely(tc->refs)) {
710 int is_child;
711 /* check if this is a reference from a child or
712 * grandchild back to it's parent or grandparent
714 * in that case we need to remove the reference and
715 * call another instance of talloc_free() on the current
716 * pointer.
718 is_child = talloc_is_parent(tc->refs, ptr);
719 _talloc_free_internal(tc->refs, location);
720 if (is_child) {
721 return _talloc_free_internal(ptr, location);
723 return -1;
726 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
727 /* we have a free loop - stop looping */
728 return 0;
731 if (unlikely(tc->destructor)) {
732 talloc_destructor_t d = tc->destructor;
733 if (d == (talloc_destructor_t)-1) {
734 return -1;
736 tc->destructor = (talloc_destructor_t)-1;
737 if (d(ptr) == -1) {
738 tc->destructor = d;
739 return -1;
741 tc->destructor = NULL;
744 if (tc->parent) {
745 _TLIST_REMOVE(tc->parent->child, tc);
746 if (tc->parent->child) {
747 tc->parent->child->parent = tc->parent;
749 } else {
750 if (tc->prev) tc->prev->next = tc->next;
751 if (tc->next) tc->next->prev = tc->prev;
754 tc->flags |= TALLOC_FLAG_LOOP;
756 while (tc->child) {
757 /* we need to work out who will own an abandoned child
758 if it cannot be freed. In priority order, the first
759 choice is owner of any remaining reference to this
760 pointer, the second choice is our parent, and the
761 final choice is the null context. */
762 void *child = TC_PTR_FROM_CHUNK(tc->child);
763 const void *new_parent = null_context;
764 struct talloc_chunk *old_parent = NULL;
765 if (unlikely(tc->child->refs)) {
766 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
767 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
769 /* finding the parent here is potentially quite
770 expensive, but the alternative, which is to change
771 talloc to always have a valid tc->parent pointer,
772 makes realloc more expensive where there are a
773 large number of children.
775 The reason we need the parent pointer here is that
776 if _talloc_free_internal() fails due to references
777 or a failing destructor we need to re-parent, but
778 the free call can invalidate the prev pointer.
780 if (new_parent == null_context && (tc->child->refs || tc->child->destructor)) {
781 old_parent = talloc_parent_chunk(ptr);
783 if (unlikely(_talloc_free_internal(child, location) == -1)) {
784 if (new_parent == null_context) {
785 struct talloc_chunk *p = old_parent;
786 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
788 _talloc_steal_internal(new_parent, child);
792 tc->flags |= TALLOC_FLAG_FREE;
794 /* we mark the freed memory with where we called the free
795 * from. This means on a double free error we can report where
796 * the first free came from
798 tc->name = location;
800 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
801 struct talloc_chunk *pool;
802 void *next_tc = NULL;
803 unsigned int *pool_object_count;
805 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
806 pool = tc;
807 } else {
808 pool = (struct talloc_chunk *)tc->pool;
809 next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
811 TC_INVALIDATE_FULL_CHUNK(tc);
814 pool_object_count = talloc_pool_objectcount(pool);
816 if (unlikely(*pool_object_count == 0)) {
817 talloc_abort("Pool object count zero!");
818 return 0;
821 *pool_object_count -= 1;
823 if (unlikely(*pool_object_count == 1)) {
825 * if there is just object left in the pool
826 * it means this is the pool itself and
827 * the rest is available for new objects
828 * again.
830 pool->pool = TC_POOL_FIRST_CHUNK(pool);
831 TC_INVALIDATE_POOL(pool);
832 } else if (unlikely(*pool_object_count == 0)) {
833 TC_INVALIDATE_FULL_CHUNK(pool);
834 free(pool);
835 } else if (pool->pool == next_tc) {
837 * if pool->pool still points to end of
838 * 'tc' (which is stored in the 'next_tc' variable),
839 * we can reclaim the memory of 'tc'.
841 pool->pool = tc;
843 } else {
844 TC_INVALIDATE_FULL_CHUNK(tc);
845 free(tc);
847 return 0;
851 move a lump of memory from one talloc context to another return the
852 ptr on success, or NULL if it could not be transferred.
853 passing NULL as ptr will always return NULL with no side effects.
855 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
857 struct talloc_chunk *tc, *new_tc;
859 if (unlikely(!ptr)) {
860 return NULL;
863 if (unlikely(new_ctx == NULL)) {
864 new_ctx = null_context;
867 tc = talloc_chunk_from_ptr(ptr);
869 if (unlikely(new_ctx == NULL)) {
870 if (tc->parent) {
871 _TLIST_REMOVE(tc->parent->child, tc);
872 if (tc->parent->child) {
873 tc->parent->child->parent = tc->parent;
875 } else {
876 if (tc->prev) tc->prev->next = tc->next;
877 if (tc->next) tc->next->prev = tc->prev;
880 tc->parent = tc->next = tc->prev = NULL;
881 return discard_const_p(void, ptr);
884 new_tc = talloc_chunk_from_ptr(new_ctx);
886 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
887 return discard_const_p(void, ptr);
890 if (tc->parent) {
891 _TLIST_REMOVE(tc->parent->child, tc);
892 if (tc->parent->child) {
893 tc->parent->child->parent = tc->parent;
895 } else {
896 if (tc->prev) tc->prev->next = tc->next;
897 if (tc->next) tc->next->prev = tc->prev;
900 tc->parent = new_tc;
901 if (new_tc->child) new_tc->child->parent = NULL;
902 _TLIST_ADD(new_tc->child, tc);
904 return discard_const_p(void, ptr);
908 move a lump of memory from one talloc context to another return the
909 ptr on success, or NULL if it could not be transferred.
910 passing NULL as ptr will always return NULL with no side effects.
912 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
914 struct talloc_chunk *tc;
916 if (unlikely(ptr == NULL)) {
917 return NULL;
920 tc = talloc_chunk_from_ptr(ptr);
922 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
923 struct talloc_reference_handle *h;
925 talloc_log("WARNING: talloc_steal with references at %s\n",
926 location);
928 for (h=tc->refs; h; h=h->next) {
929 talloc_log("\treference at %s\n",
930 h->location);
934 #if 0
935 /* this test is probably too expensive to have on in the
936 normal build, but it useful for debugging */
937 if (talloc_is_parent(new_ctx, ptr)) {
938 talloc_log("WARNING: stealing into talloc child at %s\n", location);
940 #endif
942 return _talloc_steal_internal(new_ctx, ptr);
946 this is like a talloc_steal(), but you must supply the old
947 parent. This resolves the ambiguity in a talloc_steal() which is
948 called on a context that has more than one parent (via references)
950 The old parent can be either a reference or a parent
952 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
954 struct talloc_chunk *tc;
955 struct talloc_reference_handle *h;
957 if (unlikely(ptr == NULL)) {
958 return NULL;
961 if (old_parent == talloc_parent(ptr)) {
962 return _talloc_steal_internal(new_parent, ptr);
965 tc = talloc_chunk_from_ptr(ptr);
966 for (h=tc->refs;h;h=h->next) {
967 if (talloc_parent(h) == old_parent) {
968 if (_talloc_steal_internal(new_parent, h) != h) {
969 return NULL;
971 return discard_const_p(void, ptr);
975 /* it wasn't a parent */
976 return NULL;
980 remove a secondary reference to a pointer. This undo's what
981 talloc_reference() has done. The context and pointer arguments
982 must match those given to a talloc_reference()
984 static inline int talloc_unreference(const void *context, const void *ptr)
986 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
987 struct talloc_reference_handle *h;
989 if (unlikely(context == NULL)) {
990 context = null_context;
993 for (h=tc->refs;h;h=h->next) {
994 struct talloc_chunk *p = talloc_parent_chunk(h);
995 if (p == NULL) {
996 if (context == NULL) break;
997 } else if (TC_PTR_FROM_CHUNK(p) == context) {
998 break;
1001 if (h == NULL) {
1002 return -1;
1005 return _talloc_free_internal(h, __location__);
1009 remove a specific parent context from a pointer. This is a more
1010 controlled varient of talloc_free()
1012 _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
1014 struct talloc_chunk *tc_p, *new_p;
1015 void *new_parent;
1017 if (ptr == NULL) {
1018 return -1;
1021 if (context == NULL) {
1022 context = null_context;
1025 if (talloc_unreference(context, ptr) == 0) {
1026 return 0;
1029 if (context == NULL) {
1030 if (talloc_parent_chunk(ptr) != NULL) {
1031 return -1;
1033 } else {
1034 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
1035 return -1;
1039 tc_p = talloc_chunk_from_ptr(ptr);
1041 if (tc_p->refs == NULL) {
1042 return _talloc_free_internal(ptr, __location__);
1045 new_p = talloc_parent_chunk(tc_p->refs);
1046 if (new_p) {
1047 new_parent = TC_PTR_FROM_CHUNK(new_p);
1048 } else {
1049 new_parent = NULL;
1052 if (talloc_unreference(new_parent, ptr) != 0) {
1053 return -1;
1056 _talloc_steal_internal(new_parent, ptr);
1058 return 0;
1062 add a name to an existing pointer - va_list version
1064 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1066 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
1068 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1069 tc->name = talloc_vasprintf(ptr, fmt, ap);
1070 if (likely(tc->name)) {
1071 _talloc_set_name_const(tc->name, ".name");
1073 return tc->name;
1077 add a name to an existing pointer
1079 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
1081 const char *name;
1082 va_list ap;
1083 va_start(ap, fmt);
1084 name = talloc_set_name_v(ptr, fmt, ap);
1085 va_end(ap);
1086 return name;
1091 create a named talloc pointer. Any talloc pointer can be named, and
1092 talloc_named() operates just like talloc() except that it allows you
1093 to name the pointer.
1095 _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
1097 va_list ap;
1098 void *ptr;
1099 const char *name;
1101 ptr = __talloc(context, size);
1102 if (unlikely(ptr == NULL)) return NULL;
1104 va_start(ap, fmt);
1105 name = talloc_set_name_v(ptr, fmt, ap);
1106 va_end(ap);
1108 if (unlikely(name == NULL)) {
1109 _talloc_free_internal(ptr, __location__);
1110 return NULL;
1113 return ptr;
1117 return the name of a talloc ptr, or "UNNAMED"
1119 _PUBLIC_ const char *talloc_get_name(const void *ptr)
1121 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1122 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
1123 return ".reference";
1125 if (likely(tc->name)) {
1126 return tc->name;
1128 return "UNNAMED";
1133 check if a pointer has the given name. If it does, return the pointer,
1134 otherwise return NULL
1136 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
1138 const char *pname;
1139 if (unlikely(ptr == NULL)) return NULL;
1140 pname = talloc_get_name(ptr);
1141 if (likely(pname == name || strcmp(pname, name) == 0)) {
1142 return discard_const_p(void, ptr);
1144 return NULL;
1147 static void talloc_abort_type_missmatch(const char *location,
1148 const char *name,
1149 const char *expected)
1151 const char *reason;
1153 reason = talloc_asprintf(NULL,
1154 "%s: Type mismatch: name[%s] expected[%s]",
1155 location,
1156 name?name:"NULL",
1157 expected);
1158 if (!reason) {
1159 reason = "Type mismatch";
1162 talloc_abort(reason);
1165 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
1167 const char *pname;
1169 if (unlikely(ptr == NULL)) {
1170 talloc_abort_type_missmatch(location, NULL, name);
1171 return NULL;
1174 pname = talloc_get_name(ptr);
1175 if (likely(pname == name || strcmp(pname, name) == 0)) {
1176 return discard_const_p(void, ptr);
1179 talloc_abort_type_missmatch(location, pname, name);
1180 return NULL;
1184 this is for compatibility with older versions of talloc
1186 _PUBLIC_ void *talloc_init(const char *fmt, ...)
1188 va_list ap;
1189 void *ptr;
1190 const char *name;
1192 ptr = __talloc(NULL, 0);
1193 if (unlikely(ptr == NULL)) return NULL;
1195 va_start(ap, fmt);
1196 name = talloc_set_name_v(ptr, fmt, ap);
1197 va_end(ap);
1199 if (unlikely(name == NULL)) {
1200 _talloc_free_internal(ptr, __location__);
1201 return NULL;
1204 return ptr;
1208 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1209 should probably not be used in new code. It's in here to keep the talloc
1210 code consistent across Samba 3 and 4.
1212 _PUBLIC_ void talloc_free_children(void *ptr)
1214 struct talloc_chunk *tc;
1216 if (unlikely(ptr == NULL)) {
1217 return;
1220 tc = talloc_chunk_from_ptr(ptr);
1222 while (tc->child) {
1223 /* we need to work out who will own an abandoned child
1224 if it cannot be freed. In priority order, the first
1225 choice is owner of any remaining reference to this
1226 pointer, the second choice is our parent, and the
1227 final choice is the null context. */
1228 void *child = TC_PTR_FROM_CHUNK(tc->child);
1229 const void *new_parent = null_context;
1230 if (unlikely(tc->child->refs)) {
1231 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1232 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1234 if (unlikely(talloc_free(child) == -1)) {
1235 if (new_parent == null_context) {
1236 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1237 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1239 _talloc_steal_internal(new_parent, child);
1245 Allocate a bit of memory as a child of an existing pointer
1247 _PUBLIC_ void *_talloc(const void *context, size_t size)
1249 return __talloc(context, size);
1253 externally callable talloc_set_name_const()
1255 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1257 _talloc_set_name_const(ptr, name);
1261 create a named talloc pointer. Any talloc pointer can be named, and
1262 talloc_named() operates just like talloc() except that it allows you
1263 to name the pointer.
1265 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1267 return _talloc_named_const(context, size, name);
1271 free a talloc pointer. This also frees all child pointers of this
1272 pointer recursively
1274 return 0 if the memory is actually freed, otherwise -1. The memory
1275 will not be freed if the ref_count is > 1 or the destructor (if
1276 any) returns non-zero
1278 _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1280 struct talloc_chunk *tc;
1282 if (unlikely(ptr == NULL)) {
1283 return -1;
1286 tc = talloc_chunk_from_ptr(ptr);
1288 if (unlikely(tc->refs != NULL)) {
1289 struct talloc_reference_handle *h;
1291 if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1292 /* in this case we do know which parent should
1293 get this pointer, as there is really only
1294 one parent */
1295 return talloc_unlink(null_context, ptr);
1298 talloc_log("ERROR: talloc_free with references at %s\n",
1299 location);
1301 for (h=tc->refs; h; h=h->next) {
1302 talloc_log("\treference at %s\n",
1303 h->location);
1305 return -1;
1308 return _talloc_free_internal(ptr, location);
1314 A talloc version of realloc. The context argument is only used if
1315 ptr is NULL
1317 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1319 struct talloc_chunk *tc;
1320 void *new_ptr;
1321 bool malloced = false;
1322 struct talloc_chunk *pool_tc = NULL;
1324 /* size zero is equivalent to free() */
1325 if (unlikely(size == 0)) {
1326 talloc_unlink(context, ptr);
1327 return NULL;
1330 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1331 return NULL;
1334 /* realloc(NULL) is equivalent to malloc() */
1335 if (ptr == NULL) {
1336 return _talloc_named_const(context, size, name);
1339 tc = talloc_chunk_from_ptr(ptr);
1341 /* don't allow realloc on referenced pointers */
1342 if (unlikely(tc->refs)) {
1343 return NULL;
1346 /* don't let anybody try to realloc a talloc_pool */
1347 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1348 return NULL;
1351 /* don't let anybody try to realloc a talloc_pool */
1352 if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
1353 pool_tc = (struct talloc_chunk *)tc->pool;
1356 #if (ALWAYS_REALLOC == 0)
1357 /* don't shrink if we have less than 1k to gain */
1358 if (size < tc->size) {
1359 if (pool_tc) {
1360 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1361 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1362 tc->size = size;
1363 if (next_tc == pool_tc->pool) {
1364 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1366 return ptr;
1367 } else if ((tc->size - size) < 1024) {
1368 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1369 /* do not shrink if we have less than 1k to gain */
1370 tc->size = size;
1371 return ptr;
1373 } else if (tc->size == size) {
1375 * do not change the pointer if it is exactly
1376 * the same size.
1378 return ptr;
1380 #endif
1382 /* by resetting magic we catch users of the old memory */
1383 tc->flags |= TALLOC_FLAG_FREE;
1385 #if ALWAYS_REALLOC
1386 if (pool_tc) {
1387 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1388 *talloc_pool_objectcount(pool_tc) -= 1;
1390 if (new_ptr == NULL) {
1391 new_ptr = malloc(TC_HDR_SIZE+size);
1392 malloced = true;
1395 if (new_ptr) {
1396 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1397 TC_INVALIDATE_FULL_CHUNK(tc);
1399 } else {
1400 new_ptr = malloc(size + TC_HDR_SIZE);
1401 if (new_ptr) {
1402 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1403 free(tc);
1406 #else
1407 if (pool_tc) {
1408 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1409 size_t old_chunk_size = TC_POOLMEM_CHUNK_SIZE(tc);
1410 size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
1411 size_t space_needed;
1412 size_t space_left;
1414 if (*talloc_pool_objectcount(pool_tc) == 2) {
1416 * optimize for the case where 'tc' is the only
1417 * chunk in the pool.
1419 space_needed = new_chunk_size;
1420 space_left = pool_tc->size - TALLOC_POOL_HDR_SIZE;
1422 if (space_left >= space_needed) {
1423 size_t old_used = TC_HDR_SIZE + tc->size;
1424 size_t new_used = TC_HDR_SIZE + size;
1425 pool_tc->pool = TC_POOL_FIRST_CHUNK(pool_tc);
1426 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
1428 * we need to prepare the memmove into
1429 * the unaccessable area.
1432 size_t diff = PTR_DIFF(tc, pool_tc->pool);
1433 size_t flen = MIN(diff, old_used);
1434 char *fptr = (char *)pool_tc->pool;
1435 VALGRIND_MAKE_MEM_UNDEFINED(fptr, flen);
1437 #endif
1438 memmove(pool_tc->pool, tc, old_used);
1439 new_ptr = pool_tc->pool;
1441 TC_UNDEFINE_GROW_CHUNK(tc, size);
1444 * first we do not align the pool pointer
1445 * because we want to invalidate the padding
1446 * too.
1448 pool_tc->pool = new_used + (char *)new_ptr;
1449 TC_INVALIDATE_POOL(pool_tc);
1451 /* now the aligned pointer */
1452 pool_tc->pool = new_chunk_size + (char *)new_ptr;
1453 goto got_new_ptr;
1456 next_tc = NULL;
1459 if (new_chunk_size == old_chunk_size) {
1460 TC_UNDEFINE_GROW_CHUNK(tc, size);
1461 tc->flags &= ~TALLOC_FLAG_FREE;
1462 tc->size = size;
1463 return ptr;
1466 if (next_tc == pool_tc->pool) {
1468 * optimize for the case where 'tc' is the last
1469 * chunk in the pool.
1471 space_needed = new_chunk_size - old_chunk_size;
1472 space_left = TC_POOL_SPACE_LEFT(pool_tc);
1474 if (space_left >= space_needed) {
1475 TC_UNDEFINE_GROW_CHUNK(tc, size);
1476 tc->flags &= ~TALLOC_FLAG_FREE;
1477 tc->size = size;
1478 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1479 return ptr;
1483 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1484 *talloc_pool_objectcount(pool_tc) -= 1;
1486 if (new_ptr == NULL) {
1487 new_ptr = malloc(TC_HDR_SIZE+size);
1488 malloced = true;
1491 if (new_ptr) {
1492 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1493 TC_INVALIDATE_FULL_CHUNK(tc);
1495 if (*talloc_pool_objectcount(pool_tc) == 1) {
1497 * If the pool is empty now reclaim everything.
1499 pool_tc->pool = TC_POOL_FIRST_CHUNK(pool_tc);
1500 TC_INVALIDATE_POOL(pool_tc);
1501 } else if (next_tc == pool_tc->pool) {
1503 * If it was reallocated and tc was the last
1504 * chunk, we can reclaim the memory of tc.
1506 pool_tc->pool = tc;
1510 else {
1511 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1513 got_new_ptr:
1514 #endif
1515 if (unlikely(!new_ptr)) {
1516 tc->flags &= ~TALLOC_FLAG_FREE;
1517 return NULL;
1520 tc = (struct talloc_chunk *)new_ptr;
1521 tc->flags &= ~TALLOC_FLAG_FREE;
1522 if (malloced) {
1523 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1525 if (tc->parent) {
1526 tc->parent->child = tc;
1528 if (tc->child) {
1529 tc->child->parent = tc;
1532 if (tc->prev) {
1533 tc->prev->next = tc;
1535 if (tc->next) {
1536 tc->next->prev = tc;
1539 tc->size = size;
1540 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1542 return TC_PTR_FROM_CHUNK(tc);
1546 a wrapper around talloc_steal() for situations where you are moving a pointer
1547 between two structures, and want the old pointer to be set to NULL
1549 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
1551 const void **pptr = discard_const_p(const void *,_pptr);
1552 void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1553 (*pptr) = NULL;
1554 return ret;
1558 return the total size of a talloc pool (subtree)
1560 _PUBLIC_ size_t talloc_total_size(const void *ptr)
1562 size_t total = 0;
1563 struct talloc_chunk *c, *tc;
1565 if (ptr == NULL) {
1566 ptr = null_context;
1568 if (ptr == NULL) {
1569 return 0;
1572 tc = talloc_chunk_from_ptr(ptr);
1574 if (tc->flags & TALLOC_FLAG_LOOP) {
1575 return 0;
1578 tc->flags |= TALLOC_FLAG_LOOP;
1580 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1581 total = tc->size;
1583 for (c=tc->child;c;c=c->next) {
1584 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1587 tc->flags &= ~TALLOC_FLAG_LOOP;
1589 return total;
1593 return the total number of blocks in a talloc pool (subtree)
1595 _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
1597 size_t total = 0;
1598 struct talloc_chunk *c, *tc;
1600 if (ptr == NULL) {
1601 ptr = null_context;
1603 if (ptr == NULL) {
1604 return 0;
1607 tc = talloc_chunk_from_ptr(ptr);
1609 if (tc->flags & TALLOC_FLAG_LOOP) {
1610 return 0;
1613 tc->flags |= TALLOC_FLAG_LOOP;
1615 total++;
1616 for (c=tc->child;c;c=c->next) {
1617 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1620 tc->flags &= ~TALLOC_FLAG_LOOP;
1622 return total;
1626 return the number of external references to a pointer
1628 _PUBLIC_ size_t talloc_reference_count(const void *ptr)
1630 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1631 struct talloc_reference_handle *h;
1632 size_t ret = 0;
1634 for (h=tc->refs;h;h=h->next) {
1635 ret++;
1637 return ret;
1641 report on memory usage by all children of a pointer, giving a full tree view
1643 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1644 void (*callback)(const void *ptr,
1645 int depth, int max_depth,
1646 int is_ref,
1647 void *private_data),
1648 void *private_data)
1650 struct talloc_chunk *c, *tc;
1652 if (ptr == NULL) {
1653 ptr = null_context;
1655 if (ptr == NULL) return;
1657 tc = talloc_chunk_from_ptr(ptr);
1659 if (tc->flags & TALLOC_FLAG_LOOP) {
1660 return;
1663 callback(ptr, depth, max_depth, 0, private_data);
1665 if (max_depth >= 0 && depth >= max_depth) {
1666 return;
1669 tc->flags |= TALLOC_FLAG_LOOP;
1670 for (c=tc->child;c;c=c->next) {
1671 if (c->name == TALLOC_MAGIC_REFERENCE) {
1672 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1673 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1674 } else {
1675 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1678 tc->flags &= ~TALLOC_FLAG_LOOP;
1681 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1683 const char *name = talloc_get_name(ptr);
1684 FILE *f = (FILE *)_f;
1686 if (is_ref) {
1687 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1688 return;
1691 if (depth == 0) {
1692 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1693 (max_depth < 0 ? "full " :""), name,
1694 (unsigned long)talloc_total_size(ptr),
1695 (unsigned long)talloc_total_blocks(ptr));
1696 return;
1699 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1700 depth*4, "",
1701 name,
1702 (unsigned long)talloc_total_size(ptr),
1703 (unsigned long)talloc_total_blocks(ptr),
1704 (int)talloc_reference_count(ptr), ptr);
1706 #if 0
1707 fprintf(f, "content: ");
1708 if (talloc_total_size(ptr)) {
1709 int tot = talloc_total_size(ptr);
1710 int i;
1712 for (i = 0; i < tot; i++) {
1713 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1714 fprintf(f, "%c", ((char *)ptr)[i]);
1715 } else {
1716 fprintf(f, "~%02x", ((char *)ptr)[i]);
1720 fprintf(f, "\n");
1721 #endif
1725 report on memory usage by all children of a pointer, giving a full tree view
1727 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1729 if (f) {
1730 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1731 fflush(f);
1736 report on memory usage by all children of a pointer, giving a full tree view
1738 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
1740 talloc_report_depth_file(ptr, 0, -1, f);
1744 report on memory usage by all children of a pointer
1746 _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
1748 talloc_report_depth_file(ptr, 0, 1, f);
1752 report on any memory hanging off the null context
1754 static void talloc_report_null(void)
1756 if (talloc_total_size(null_context) != 0) {
1757 talloc_report(null_context, stderr);
1762 report on any memory hanging off the null context
1764 static void talloc_report_null_full(void)
1766 if (talloc_total_size(null_context) != 0) {
1767 talloc_report_full(null_context, stderr);
1772 enable tracking of the NULL context
1774 _PUBLIC_ void talloc_enable_null_tracking(void)
1776 if (null_context == NULL) {
1777 null_context = _talloc_named_const(NULL, 0, "null_context");
1778 if (autofree_context != NULL) {
1779 talloc_reparent(NULL, null_context, autofree_context);
1785 enable tracking of the NULL context, not moving the autofree context
1786 into the NULL context. This is needed for the talloc testsuite
1788 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
1790 if (null_context == NULL) {
1791 null_context = _talloc_named_const(NULL, 0, "null_context");
1796 disable tracking of the NULL context
1798 _PUBLIC_ void talloc_disable_null_tracking(void)
1800 if (null_context != NULL) {
1801 /* we have to move any children onto the real NULL
1802 context */
1803 struct talloc_chunk *tc, *tc2;
1804 tc = talloc_chunk_from_ptr(null_context);
1805 for (tc2 = tc->child; tc2; tc2=tc2->next) {
1806 if (tc2->parent == tc) tc2->parent = NULL;
1807 if (tc2->prev == tc) tc2->prev = NULL;
1809 for (tc2 = tc->next; tc2; tc2=tc2->next) {
1810 if (tc2->parent == tc) tc2->parent = NULL;
1811 if (tc2->prev == tc) tc2->prev = NULL;
1813 tc->child = NULL;
1814 tc->next = NULL;
1816 talloc_free(null_context);
1817 null_context = NULL;
1821 enable leak reporting on exit
1823 _PUBLIC_ void talloc_enable_leak_report(void)
1825 talloc_enable_null_tracking();
1826 atexit(talloc_report_null);
1830 enable full leak reporting on exit
1832 _PUBLIC_ void talloc_enable_leak_report_full(void)
1834 talloc_enable_null_tracking();
1835 atexit(talloc_report_null_full);
1839 talloc and zero memory.
1841 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
1843 void *p = _talloc_named_const(ctx, size, name);
1845 if (p) {
1846 memset(p, '\0', size);
1849 return p;
1853 memdup with a talloc.
1855 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1857 void *newp = _talloc_named_const(t, size, name);
1859 if (likely(newp)) {
1860 memcpy(newp, p, size);
1863 return newp;
1866 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1868 char *ret;
1870 ret = (char *)__talloc(t, len + 1);
1871 if (unlikely(!ret)) return NULL;
1873 memcpy(ret, p, len);
1874 ret[len] = 0;
1876 _talloc_set_name_const(ret, ret);
1877 return ret;
1881 strdup with a talloc
1883 _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
1885 if (unlikely(!p)) return NULL;
1886 return __talloc_strlendup(t, p, strlen(p));
1890 strndup with a talloc
1892 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
1894 if (unlikely(!p)) return NULL;
1895 return __talloc_strlendup(t, p, strnlen(p, n));
1898 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1899 const char *a, size_t alen)
1901 char *ret;
1903 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1904 if (unlikely(!ret)) return NULL;
1906 /* append the string and the trailing \0 */
1907 memcpy(&ret[slen], a, alen);
1908 ret[slen+alen] = 0;
1910 _talloc_set_name_const(ret, ret);
1911 return ret;
1915 * Appends at the end of the string.
1917 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
1919 if (unlikely(!s)) {
1920 return talloc_strdup(NULL, a);
1923 if (unlikely(!a)) {
1924 return s;
1927 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1931 * Appends at the end of the talloc'ed buffer,
1932 * not the end of the string.
1934 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
1936 size_t slen;
1938 if (unlikely(!s)) {
1939 return talloc_strdup(NULL, a);
1942 if (unlikely(!a)) {
1943 return s;
1946 slen = talloc_get_size(s);
1947 if (likely(slen > 0)) {
1948 slen--;
1951 return __talloc_strlendup_append(s, slen, a, strlen(a));
1955 * Appends at the end of the string.
1957 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
1959 if (unlikely(!s)) {
1960 return talloc_strdup(NULL, a);
1963 if (unlikely(!a)) {
1964 return s;
1967 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1971 * Appends at the end of the talloc'ed buffer,
1972 * not the end of the string.
1974 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1976 size_t slen;
1978 if (unlikely(!s)) {
1979 return talloc_strdup(NULL, a);
1982 if (unlikely(!a)) {
1983 return s;
1986 slen = talloc_get_size(s);
1987 if (likely(slen > 0)) {
1988 slen--;
1991 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1994 #ifndef HAVE_VA_COPY
1995 #ifdef HAVE___VA_COPY
1996 #define va_copy(dest, src) __va_copy(dest, src)
1997 #else
1998 #define va_copy(dest, src) (dest) = (src)
1999 #endif
2000 #endif
2002 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
2004 int len;
2005 char *ret;
2006 va_list ap2;
2007 char c;
2009 /* this call looks strange, but it makes it work on older solaris boxes */
2010 va_copy(ap2, ap);
2011 len = vsnprintf(&c, 1, fmt, ap2);
2012 va_end(ap2);
2013 if (unlikely(len < 0)) {
2014 return NULL;
2017 ret = (char *)__talloc(t, len+1);
2018 if (unlikely(!ret)) return NULL;
2020 va_copy(ap2, ap);
2021 vsnprintf(ret, len+1, fmt, ap2);
2022 va_end(ap2);
2024 _talloc_set_name_const(ret, ret);
2025 return ret;
2030 Perform string formatting, and return a pointer to newly allocated
2031 memory holding the result, inside a memory pool.
2033 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
2035 va_list ap;
2036 char *ret;
2038 va_start(ap, fmt);
2039 ret = talloc_vasprintf(t, fmt, ap);
2040 va_end(ap);
2041 return ret;
2044 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2045 const char *fmt, va_list ap)
2046 PRINTF_ATTRIBUTE(3,0);
2048 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2049 const char *fmt, va_list ap)
2051 ssize_t alen;
2052 va_list ap2;
2053 char c;
2055 va_copy(ap2, ap);
2056 alen = vsnprintf(&c, 1, fmt, ap2);
2057 va_end(ap2);
2059 if (alen <= 0) {
2060 /* Either the vsnprintf failed or the format resulted in
2061 * no characters being formatted. In the former case, we
2062 * ought to return NULL, in the latter we ought to return
2063 * the original string. Most current callers of this
2064 * function expect it to never return NULL.
2066 return s;
2069 s = talloc_realloc(NULL, s, char, slen + alen + 1);
2070 if (!s) return NULL;
2072 va_copy(ap2, ap);
2073 vsnprintf(s + slen, alen + 1, fmt, ap2);
2074 va_end(ap2);
2076 _talloc_set_name_const(s, s);
2077 return s;
2081 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2082 * and return @p s, which may have moved. Good for gradually
2083 * accumulating output into a string buffer. Appends at the end
2084 * of the string.
2086 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
2088 if (unlikely(!s)) {
2089 return talloc_vasprintf(NULL, fmt, ap);
2092 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
2096 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2097 * and return @p s, which may have moved. Always appends at the
2098 * end of the talloc'ed buffer, not the end of the string.
2100 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
2102 size_t slen;
2104 if (unlikely(!s)) {
2105 return talloc_vasprintf(NULL, fmt, ap);
2108 slen = talloc_get_size(s);
2109 if (likely(slen > 0)) {
2110 slen--;
2113 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
2117 Realloc @p s to append the formatted result of @p fmt and return @p
2118 s, which may have moved. Good for gradually accumulating output
2119 into a string buffer.
2121 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
2123 va_list ap;
2125 va_start(ap, fmt);
2126 s = talloc_vasprintf_append(s, fmt, ap);
2127 va_end(ap);
2128 return s;
2132 Realloc @p s to append the formatted result of @p fmt and return @p
2133 s, which may have moved. Good for gradually accumulating output
2134 into a buffer.
2136 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
2138 va_list ap;
2140 va_start(ap, fmt);
2141 s = talloc_vasprintf_append_buffer(s, fmt, ap);
2142 va_end(ap);
2143 return s;
2147 alloc an array, checking for integer overflow in the array size
2149 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2151 if (count >= MAX_TALLOC_SIZE/el_size) {
2152 return NULL;
2154 return _talloc_named_const(ctx, el_size * count, name);
2158 alloc an zero array, checking for integer overflow in the array size
2160 _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2162 if (count >= MAX_TALLOC_SIZE/el_size) {
2163 return NULL;
2165 return _talloc_zero(ctx, el_size * count, name);
2169 realloc an array, checking for integer overflow in the array size
2171 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
2173 if (count >= MAX_TALLOC_SIZE/el_size) {
2174 return NULL;
2176 return _talloc_realloc(ctx, ptr, el_size * count, name);
2180 a function version of talloc_realloc(), so it can be passed as a function pointer
2181 to libraries that want a realloc function (a realloc function encapsulates
2182 all the basic capabilities of an allocation library, which is why this is useful)
2184 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
2186 return _talloc_realloc(context, ptr, size, NULL);
2190 static int talloc_autofree_destructor(void *ptr)
2192 autofree_context = NULL;
2193 return 0;
2196 static void talloc_autofree(void)
2198 talloc_free(autofree_context);
2202 return a context which will be auto-freed on exit
2203 this is useful for reducing the noise in leak reports
2205 _PUBLIC_ void *talloc_autofree_context(void)
2207 if (autofree_context == NULL) {
2208 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
2209 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
2210 atexit(talloc_autofree);
2212 return autofree_context;
2215 _PUBLIC_ size_t talloc_get_size(const void *context)
2217 struct talloc_chunk *tc;
2219 if (context == NULL) {
2220 context = null_context;
2222 if (context == NULL) {
2223 return 0;
2226 tc = talloc_chunk_from_ptr(context);
2228 return tc->size;
2232 find a parent of this context that has the given name, if any
2234 _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
2236 struct talloc_chunk *tc;
2238 if (context == NULL) {
2239 return NULL;
2242 tc = talloc_chunk_from_ptr(context);
2243 while (tc) {
2244 if (tc->name && strcmp(tc->name, name) == 0) {
2245 return TC_PTR_FROM_CHUNK(tc);
2247 while (tc && tc->prev) tc = tc->prev;
2248 if (tc) {
2249 tc = tc->parent;
2252 return NULL;
2256 show the parentage of a context
2258 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
2260 struct talloc_chunk *tc;
2262 if (context == NULL) {
2263 fprintf(file, "talloc no parents for NULL\n");
2264 return;
2267 tc = talloc_chunk_from_ptr(context);
2268 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
2269 while (tc) {
2270 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
2271 while (tc && tc->prev) tc = tc->prev;
2272 if (tc) {
2273 tc = tc->parent;
2276 fflush(file);
2280 return 1 if ptr is a parent of context
2282 static int _talloc_is_parent(const void *context, const void *ptr, int depth)
2284 struct talloc_chunk *tc;
2286 if (context == NULL) {
2287 return 0;
2290 tc = talloc_chunk_from_ptr(context);
2291 while (tc && depth > 0) {
2292 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2293 while (tc && tc->prev) tc = tc->prev;
2294 if (tc) {
2295 tc = tc->parent;
2296 depth--;
2299 return 0;
2303 return 1 if ptr is a parent of context
2305 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2307 return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);