Fix crash during early exit
[mplayer.git] / talloc.c
blob3509523dc6c718fe15ffffde6b0f6a01b907e67b
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 // Hardcode these for MPlayer assuming a working system.
34 // Original used autoconf detection with workarounds for broken systems.
35 #define HAVE_VA_COPY
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
39 #include <stdint.h>
40 #include <string.h>
41 #include <stdbool.h>
42 #define MIN(a,b) ((a)<(b)?(a):(b))
43 #define strnlen rep_strnlen
44 static size_t rep_strnlen(const char *s, size_t max)
46 size_t len;
48 for (len = 0; len < max; len++) {
49 if (s[len] == '\0') {
50 break;
53 return len;
58 #ifdef _SAMBA_BUILD_
59 #include "version.h"
60 #if (SAMBA_VERSION_MAJOR<4)
61 #include "includes.h"
62 /* This is to circumvent SAMBA3's paranoid malloc checker. Here in this file
63 * we trust ourselves... */
64 #ifdef malloc
65 #undef malloc
66 #endif
67 #ifdef realloc
68 #undef realloc
69 #endif
70 #define _TALLOC_SAMBA3
71 #endif /* (SAMBA_VERSION_MAJOR<4) */
72 #endif /* _SAMBA_BUILD_ */
74 #ifndef _TALLOC_SAMBA3
75 // Workarounds for missing standard features, not used in MPlayer
76 // #include "replace.h"
77 #include "talloc.h"
78 #endif /* not _TALLOC_SAMBA3 */
80 /* use this to force every realloc to change the pointer, to stress test
81 code that might not cope */
82 #define ALWAYS_REALLOC 0
85 #define MAX_TALLOC_SIZE 0x10000000
86 #define TALLOC_MAGIC 0xe814ec70
87 #define TALLOC_FLAG_FREE 0x01
88 #define TALLOC_FLAG_LOOP 0x02
89 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
90 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
91 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
93 /* by default we abort when given a bad pointer (such as when talloc_free() is called
94 on a pointer that came from malloc() */
95 #ifndef TALLOC_ABORT
96 #define TALLOC_ABORT(reason) abort()
97 #endif
99 #ifndef discard_const_p
100 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
101 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
102 #else
103 # define discard_const_p(type, ptr) ((type *)(ptr))
104 #endif
105 #endif
107 /* these macros gain us a few percent of speed on gcc */
108 #if (__GNUC__ >= 3)
109 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
110 as its first argument */
111 #define likely(x) __builtin_expect(!!(x), 1)
112 #define unlikely(x) __builtin_expect(!!(x), 0)
113 #else
114 #define likely(x) x
115 #define unlikely(x) x
116 #endif
118 /* this null_context is only used if talloc_enable_leak_report() or
119 talloc_enable_leak_report_full() is called, otherwise it remains
120 NULL
122 static void *null_context;
123 static void *autofree_context;
125 struct talloc_reference_handle {
126 struct talloc_reference_handle *next, *prev;
127 void *ptr;
130 typedef int (*talloc_destructor_t)(void *);
132 struct talloc_chunk {
133 struct talloc_chunk *next, *prev;
134 struct talloc_chunk *parent, *child;
135 struct talloc_reference_handle *refs;
136 talloc_destructor_t destructor;
137 const char *name;
138 size_t size;
139 unsigned flags;
142 * "pool" has dual use:
144 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
145 * marks the end of the currently allocated area.
147 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
148 * is a pointer to the struct talloc_chunk of the pool that it was
149 * allocated from. This way children can quickly find the pool to chew
150 * from.
152 void *pool;
155 /* 16 byte alignment seems to keep everyone happy */
156 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
157 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
159 static void talloc_abort_double_free(void)
161 TALLOC_ABORT("Bad talloc magic value - double free");
164 static void talloc_abort_unknown_value(void)
166 TALLOC_ABORT("Bad talloc magic value - unknown value");
169 /* panic if we get a bad magic value */
170 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
172 const char *pp = (const char *)ptr;
173 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
174 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
175 if (tc->flags & TALLOC_FLAG_FREE) {
176 talloc_abort_double_free();
177 } else {
178 talloc_abort_unknown_value();
181 return tc;
184 /* hook into the front of the list */
185 #define _TLIST_ADD(list, p) \
186 do { \
187 if (!(list)) { \
188 (list) = (p); \
189 (p)->next = (p)->prev = NULL; \
190 } else { \
191 (list)->prev = (p); \
192 (p)->next = (list); \
193 (p)->prev = NULL; \
194 (list) = (p); \
196 } while (0)
198 /* remove an element from a list - element doesn't have to be in list. */
199 #define _TLIST_REMOVE(list, p) \
200 do { \
201 if ((p) == (list)) { \
202 (list) = (p)->next; \
203 if (list) (list)->prev = NULL; \
204 } else { \
205 if ((p)->prev) (p)->prev->next = (p)->next; \
206 if ((p)->next) (p)->next->prev = (p)->prev; \
208 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
209 } while (0)
213 return the parent chunk of a pointer
215 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
217 struct talloc_chunk *tc;
219 if (unlikely(ptr == NULL)) {
220 return NULL;
223 tc = talloc_chunk_from_ptr(ptr);
224 while (tc->prev) tc=tc->prev;
226 return tc->parent;
229 void *talloc_parent(const void *ptr)
231 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
232 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
236 find parents name
238 const char *talloc_parent_name(const void *ptr)
240 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
241 return tc? tc->name : NULL;
245 A pool carries an in-pool object count count in the first 16 bytes.
246 bytes. This is done to support talloc_steal() to a parent outside of the
247 pool. The count includes the pool itself, so a talloc_free() on a pool will
248 only destroy the pool if the count has dropped to zero. A talloc_free() of a
249 pool member will reduce the count, and eventually also call free(3) on the
250 pool memory.
252 The object count is not put into "struct talloc_chunk" because it is only
253 relevant for talloc pools and the alignment to 16 bytes would increase the
254 memory footprint of each talloc chunk by those 16 bytes.
257 #define TALLOC_POOL_HDR_SIZE 16
259 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
261 return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
265 Allocate from a pool
268 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
269 size_t size)
271 struct talloc_chunk *pool_ctx = NULL;
272 size_t space_left;
273 struct talloc_chunk *result;
274 size_t chunk_size;
276 if (parent == NULL) {
277 return NULL;
280 if (parent->flags & TALLOC_FLAG_POOL) {
281 pool_ctx = parent;
283 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
284 pool_ctx = (struct talloc_chunk *)parent->pool;
287 if (pool_ctx == NULL) {
288 return NULL;
291 space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
292 - ((char *)pool_ctx->pool);
295 * Align size to 16 bytes
297 chunk_size = ((size + 15) & ~15);
299 if (space_left < chunk_size) {
300 return NULL;
303 result = (struct talloc_chunk *)pool_ctx->pool;
305 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
306 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
307 #endif
309 pool_ctx->pool = (void *)((char *)result + chunk_size);
311 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
312 result->pool = pool_ctx;
314 *talloc_pool_objectcount(pool_ctx) += 1;
316 return result;
320 Allocate a bit of memory as a child of an existing pointer
322 static inline void *__talloc(const void *context, size_t size)
324 struct talloc_chunk *tc = NULL;
326 if (unlikely(context == NULL)) {
327 context = null_context;
330 if (unlikely(size >= MAX_TALLOC_SIZE)) {
331 abort(); // return NULL;
334 if (context != NULL) {
335 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
336 TC_HDR_SIZE+size);
339 if (tc == NULL) {
340 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
341 if (unlikely(tc == NULL)) abort(); // return NULL;
342 tc->flags = TALLOC_MAGIC;
343 tc->pool = NULL;
346 tc->size = size;
347 tc->destructor = NULL;
348 tc->child = NULL;
349 tc->name = NULL;
350 tc->refs = NULL;
352 if (likely(context)) {
353 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
355 if (parent->child) {
356 parent->child->parent = NULL;
357 tc->next = parent->child;
358 tc->next->prev = tc;
359 } else {
360 tc->next = NULL;
362 tc->parent = parent;
363 tc->prev = NULL;
364 parent->child = tc;
365 } else {
366 tc->next = tc->prev = tc->parent = NULL;
369 return TC_PTR_FROM_CHUNK(tc);
373 * Create a talloc pool
376 void *talloc_pool(const void *context, size_t size)
378 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
379 struct talloc_chunk *tc;
381 if (unlikely(result == NULL)) {
382 return NULL;
385 tc = talloc_chunk_from_ptr(result);
387 tc->flags |= TALLOC_FLAG_POOL;
388 tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
390 *talloc_pool_objectcount(tc) = 1;
392 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
393 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
394 #endif
396 return result;
400 setup a destructor to be called on free of a pointer
401 the destructor should return 0 on success, or -1 on failure.
402 if the destructor fails then the free is failed, and the memory can
403 be continued to be used
405 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
407 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
408 tc->destructor = destructor;
412 increase the reference count on a piece of memory.
414 int talloc_increase_ref_count(const void *ptr)
416 if (unlikely(!talloc_reference(null_context, ptr))) {
417 return -1;
419 return 0;
423 helper for talloc_reference()
425 this is referenced by a function pointer and should not be inline
427 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
429 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
430 _TLIST_REMOVE(ptr_tc->refs, handle);
431 return 0;
435 more efficient way to add a name to a pointer - the name must point to a
436 true string constant
438 static inline void _talloc_set_name_const(const void *ptr, const char *name)
440 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
441 tc->name = name;
445 internal talloc_named_const()
447 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
449 void *ptr;
451 ptr = __talloc(context, size);
452 if (unlikely(ptr == NULL)) {
453 return NULL;
456 _talloc_set_name_const(ptr, name);
458 return ptr;
462 make a secondary reference to a pointer, hanging off the given context.
463 the pointer remains valid until both the original caller and this given
464 context are freed.
466 the major use for this is when two different structures need to reference the
467 same underlying data, and you want to be able to free the two instances separately,
468 and in either order
470 void *_talloc_reference(const void *context, const void *ptr)
472 struct talloc_chunk *tc;
473 struct talloc_reference_handle *handle;
474 if (unlikely(ptr == NULL)) return NULL;
476 tc = talloc_chunk_from_ptr(ptr);
477 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
478 sizeof(struct talloc_reference_handle),
479 TALLOC_MAGIC_REFERENCE);
480 if (unlikely(handle == NULL)) return NULL;
482 /* note that we hang the destructor off the handle, not the
483 main context as that allows the caller to still setup their
484 own destructor on the context if they want to */
485 talloc_set_destructor(handle, talloc_reference_destructor);
486 handle->ptr = discard_const_p(void, ptr);
487 _TLIST_ADD(tc->refs, handle);
488 return handle->ptr;
493 internal talloc_free call
495 static inline int _talloc_free(void *ptr)
497 struct talloc_chunk *tc;
499 if (unlikely(ptr == NULL)) {
500 return -1;
503 tc = talloc_chunk_from_ptr(ptr);
505 if (unlikely(tc->refs)) {
506 int is_child;
507 /* check this is a reference from a child or grantchild
508 * back to it's parent or grantparent
510 * in that case we need to remove the reference and
511 * call another instance of talloc_free() on the current
512 * pointer.
514 is_child = talloc_is_parent(tc->refs, ptr);
515 _talloc_free(tc->refs);
516 if (is_child) {
517 return _talloc_free(ptr);
519 return -1;
522 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
523 /* we have a free loop - stop looping */
524 return 0;
527 if (unlikely(tc->destructor)) {
528 talloc_destructor_t d = tc->destructor;
529 if (d == (talloc_destructor_t)-1) {
530 return -1;
532 tc->destructor = (talloc_destructor_t)-1;
533 if (d(ptr) == -1) {
534 tc->destructor = d;
535 return -1;
537 tc->destructor = NULL;
540 if (tc->parent) {
541 _TLIST_REMOVE(tc->parent->child, tc);
542 if (tc->parent->child) {
543 tc->parent->child->parent = tc->parent;
545 } else {
546 if (tc->prev) tc->prev->next = tc->next;
547 if (tc->next) tc->next->prev = tc->prev;
550 tc->flags |= TALLOC_FLAG_LOOP;
552 while (tc->child) {
553 /* we need to work out who will own an abandoned child
554 if it cannot be freed. In priority order, the first
555 choice is owner of any remaining reference to this
556 pointer, the second choice is our parent, and the
557 final choice is the null context. */
558 void *child = TC_PTR_FROM_CHUNK(tc->child);
559 const void *new_parent = null_context;
560 if (unlikely(tc->child->refs)) {
561 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
562 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
564 if (unlikely(_talloc_free(child) == -1)) {
565 if (new_parent == null_context) {
566 struct talloc_chunk *p = talloc_parent_chunk(ptr);
567 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
569 talloc_steal(new_parent, child);
573 tc->flags |= TALLOC_FLAG_FREE;
575 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
576 struct talloc_chunk *pool;
577 unsigned int *pool_object_count;
579 pool = (tc->flags & TALLOC_FLAG_POOL)
580 ? tc : (struct talloc_chunk *)tc->pool;
582 pool_object_count = talloc_pool_objectcount(pool);
584 if (*pool_object_count == 0) {
585 TALLOC_ABORT("Pool object count zero!");
588 *pool_object_count -= 1;
590 if (*pool_object_count == 0) {
591 free(pool);
594 else {
595 free(tc);
597 return 0;
601 move a lump of memory from one talloc context to another return the
602 ptr on success, or NULL if it could not be transferred.
603 passing NULL as ptr will always return NULL with no side effects.
605 void *_talloc_steal(const void *new_ctx, const void *ptr)
607 struct talloc_chunk *tc, *new_tc;
609 if (unlikely(!ptr)) {
610 return NULL;
613 if (unlikely(new_ctx == NULL)) {
614 new_ctx = null_context;
617 tc = talloc_chunk_from_ptr(ptr);
619 if (unlikely(new_ctx == NULL)) {
620 if (tc->parent) {
621 _TLIST_REMOVE(tc->parent->child, tc);
622 if (tc->parent->child) {
623 tc->parent->child->parent = tc->parent;
625 } else {
626 if (tc->prev) tc->prev->next = tc->next;
627 if (tc->next) tc->next->prev = tc->prev;
630 tc->parent = tc->next = tc->prev = NULL;
631 return discard_const_p(void, ptr);
634 new_tc = talloc_chunk_from_ptr(new_ctx);
636 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
637 return discard_const_p(void, ptr);
640 if (tc->parent) {
641 _TLIST_REMOVE(tc->parent->child, tc);
642 if (tc->parent->child) {
643 tc->parent->child->parent = tc->parent;
645 } else {
646 if (tc->prev) tc->prev->next = tc->next;
647 if (tc->next) tc->next->prev = tc->prev;
650 tc->parent = new_tc;
651 if (new_tc->child) new_tc->child->parent = NULL;
652 _TLIST_ADD(new_tc->child, tc);
654 return discard_const_p(void, ptr);
660 remove a secondary reference to a pointer. This undo's what
661 talloc_reference() has done. The context and pointer arguments
662 must match those given to a talloc_reference()
664 static inline int talloc_unreference(const void *context, const void *ptr)
666 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
667 struct talloc_reference_handle *h;
669 if (unlikely(context == NULL)) {
670 context = null_context;
673 for (h=tc->refs;h;h=h->next) {
674 struct talloc_chunk *p = talloc_parent_chunk(h);
675 if (p == NULL) {
676 if (context == NULL) break;
677 } else if (TC_PTR_FROM_CHUNK(p) == context) {
678 break;
681 if (h == NULL) {
682 return -1;
685 return _talloc_free(h);
689 remove a specific parent context from a pointer. This is a more
690 controlled varient of talloc_free()
692 int talloc_unlink(const void *context, void *ptr)
694 struct talloc_chunk *tc_p, *new_p;
695 void *new_parent;
697 if (ptr == NULL) {
698 return -1;
701 if (context == NULL) {
702 context = null_context;
705 if (talloc_unreference(context, ptr) == 0) {
706 return 0;
709 if (context == NULL) {
710 if (talloc_parent_chunk(ptr) != NULL) {
711 return -1;
713 } else {
714 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
715 return -1;
719 tc_p = talloc_chunk_from_ptr(ptr);
721 if (tc_p->refs == NULL) {
722 return _talloc_free(ptr);
725 new_p = talloc_parent_chunk(tc_p->refs);
726 if (new_p) {
727 new_parent = TC_PTR_FROM_CHUNK(new_p);
728 } else {
729 new_parent = NULL;
732 if (talloc_unreference(new_parent, ptr) != 0) {
733 return -1;
736 talloc_steal(new_parent, ptr);
738 return 0;
742 add a name to an existing pointer - va_list version
744 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
746 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
748 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
749 tc->name = talloc_vasprintf(ptr, fmt, ap);
750 if (likely(tc->name)) {
751 _talloc_set_name_const(tc->name, ".name");
753 return tc->name;
757 add a name to an existing pointer
759 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
761 const char *name;
762 va_list ap;
763 va_start(ap, fmt);
764 name = talloc_set_name_v(ptr, fmt, ap);
765 va_end(ap);
766 return name;
771 create a named talloc pointer. Any talloc pointer can be named, and
772 talloc_named() operates just like talloc() except that it allows you
773 to name the pointer.
775 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
777 va_list ap;
778 void *ptr;
779 const char *name;
781 ptr = __talloc(context, size);
782 if (unlikely(ptr == NULL)) return NULL;
784 va_start(ap, fmt);
785 name = talloc_set_name_v(ptr, fmt, ap);
786 va_end(ap);
788 if (unlikely(name == NULL)) {
789 _talloc_free(ptr);
790 return NULL;
793 return ptr;
797 return the name of a talloc ptr, or "UNNAMED"
799 const char *talloc_get_name(const void *ptr)
801 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
802 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
803 return ".reference";
805 if (likely(tc->name)) {
806 return tc->name;
808 return "UNNAMED";
813 check if a pointer has the given name. If it does, return the pointer,
814 otherwise return NULL
816 void *talloc_check_name(const void *ptr, const char *name)
818 const char *pname;
819 if (unlikely(ptr == NULL)) return NULL;
820 pname = talloc_get_name(ptr);
821 if (likely(pname == name || strcmp(pname, name) == 0)) {
822 return discard_const_p(void, ptr);
824 return NULL;
829 this is for compatibility with older versions of talloc
831 void *talloc_init(const char *fmt, ...)
833 va_list ap;
834 void *ptr;
835 const char *name;
838 * samba3 expects talloc_report_depth_cb(NULL, ...)
839 * reports all talloc'ed memory, so we need to enable
840 * null_tracking
842 talloc_enable_null_tracking();
844 ptr = __talloc(NULL, 0);
845 if (unlikely(ptr == NULL)) return NULL;
847 va_start(ap, fmt);
848 name = talloc_set_name_v(ptr, fmt, ap);
849 va_end(ap);
851 if (unlikely(name == NULL)) {
852 _talloc_free(ptr);
853 return NULL;
856 return ptr;
860 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
861 should probably not be used in new code. It's in here to keep the talloc
862 code consistent across Samba 3 and 4.
864 void talloc_free_children(void *ptr)
866 struct talloc_chunk *tc;
868 if (unlikely(ptr == NULL)) {
869 return;
872 tc = talloc_chunk_from_ptr(ptr);
874 while (tc->child) {
875 /* we need to work out who will own an abandoned child
876 if it cannot be freed. In priority order, the first
877 choice is owner of any remaining reference to this
878 pointer, the second choice is our parent, and the
879 final choice is the null context. */
880 void *child = TC_PTR_FROM_CHUNK(tc->child);
881 const void *new_parent = null_context;
882 if (unlikely(tc->child->refs)) {
883 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
884 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
886 if (unlikely(_talloc_free(child) == -1)) {
887 if (new_parent == null_context) {
888 struct talloc_chunk *p = talloc_parent_chunk(ptr);
889 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
891 talloc_steal(new_parent, child);
895 if ((tc->flags & TALLOC_FLAG_POOL)
896 && (*talloc_pool_objectcount(tc) == 1)) {
897 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
898 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
899 VALGRIND_MAKE_MEM_NOACCESS(
900 tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
901 #endif
906 Allocate a bit of memory as a child of an existing pointer
908 void *_talloc(const void *context, size_t size)
910 return __talloc(context, size);
914 externally callable talloc_set_name_const()
916 void talloc_set_name_const(const void *ptr, const char *name)
918 _talloc_set_name_const(ptr, name);
922 create a named talloc pointer. Any talloc pointer can be named, and
923 talloc_named() operates just like talloc() except that it allows you
924 to name the pointer.
926 void *talloc_named_const(const void *context, size_t size, const char *name)
928 return _talloc_named_const(context, size, name);
932 free a talloc pointer. This also frees all child pointers of this
933 pointer recursively
935 return 0 if the memory is actually freed, otherwise -1. The memory
936 will not be freed if the ref_count is > 1 or the destructor (if
937 any) returns non-zero
939 int talloc_free(void *ptr)
941 return _talloc_free(ptr);
947 A talloc version of realloc. The context argument is only used if
948 ptr is NULL
950 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
952 struct talloc_chunk *tc;
953 void *new_ptr;
954 bool malloced = false;
956 /* size zero is equivalent to free() */
957 if (unlikely(size == 0)) {
958 _talloc_free(ptr);
959 return NULL;
962 if (unlikely(size >= MAX_TALLOC_SIZE)) {
963 abort(); // return NULL;
966 /* realloc(NULL) is equivalent to malloc() */
967 if (ptr == NULL) {
968 return _talloc_named_const(context, size, name);
971 tc = talloc_chunk_from_ptr(ptr);
973 /* don't allow realloc on referenced pointers */
974 if (unlikely(tc->refs)) {
975 abort(); // return NULL;
978 /* don't shrink if we have less than 1k to gain */
979 if ((size < tc->size) && ((tc->size - size) < 1024)) {
980 tc->size = size;
981 return ptr;
984 /* by resetting magic we catch users of the old memory */
985 tc->flags |= TALLOC_FLAG_FREE;
987 #if ALWAYS_REALLOC
988 new_ptr = malloc(size + TC_HDR_SIZE);
989 if (new_ptr) {
990 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
991 free(tc);
993 #else
994 if (tc->flags & TALLOC_FLAG_POOLMEM) {
996 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
997 *talloc_pool_objectcount((struct talloc_chunk *)
998 (tc->pool)) -= 1;
1000 if (new_ptr == NULL) {
1001 new_ptr = malloc(TC_HDR_SIZE+size);
1002 malloced = true;
1005 if (new_ptr) {
1006 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1009 else {
1010 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1012 #endif
1013 if (unlikely(!new_ptr)) {
1014 tc->flags &= ~TALLOC_FLAG_FREE;
1015 abort(); // return NULL;
1018 tc = (struct talloc_chunk *)new_ptr;
1019 tc->flags &= ~TALLOC_FLAG_FREE;
1020 if (malloced) {
1021 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1023 if (tc->parent) {
1024 tc->parent->child = tc;
1026 if (tc->child) {
1027 tc->child->parent = tc;
1030 if (tc->prev) {
1031 tc->prev->next = tc;
1033 if (tc->next) {
1034 tc->next->prev = tc;
1037 tc->size = size;
1038 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1040 return TC_PTR_FROM_CHUNK(tc);
1044 a wrapper around talloc_steal() for situations where you are moving a pointer
1045 between two structures, and want the old pointer to be set to NULL
1047 void *_talloc_move(const void *new_ctx, const void *_pptr)
1049 const void **pptr = discard_const_p(const void *,_pptr);
1050 void *ret = _talloc_steal(new_ctx, *pptr);
1051 (*pptr) = NULL;
1052 return ret;
1056 return the total size of a talloc pool (subtree)
1058 size_t talloc_total_size(const void *ptr)
1060 size_t total = 0;
1061 struct talloc_chunk *c, *tc;
1063 if (ptr == NULL) {
1064 ptr = null_context;
1066 if (ptr == NULL) {
1067 return 0;
1070 tc = talloc_chunk_from_ptr(ptr);
1072 if (tc->flags & TALLOC_FLAG_LOOP) {
1073 return 0;
1076 tc->flags |= TALLOC_FLAG_LOOP;
1078 total = tc->size;
1079 for (c=tc->child;c;c=c->next) {
1080 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1083 tc->flags &= ~TALLOC_FLAG_LOOP;
1085 return total;
1089 return the total number of blocks in a talloc pool (subtree)
1091 size_t talloc_total_blocks(const void *ptr)
1093 size_t total = 0;
1094 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
1096 if (tc->flags & TALLOC_FLAG_LOOP) {
1097 return 0;
1100 tc->flags |= TALLOC_FLAG_LOOP;
1102 total++;
1103 for (c=tc->child;c;c=c->next) {
1104 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1107 tc->flags &= ~TALLOC_FLAG_LOOP;
1109 return total;
1113 return the number of external references to a pointer
1115 size_t talloc_reference_count(const void *ptr)
1117 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1118 struct talloc_reference_handle *h;
1119 size_t ret = 0;
1121 for (h=tc->refs;h;h=h->next) {
1122 ret++;
1124 return ret;
1128 report on memory usage by all children of a pointer, giving a full tree view
1130 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1131 void (*callback)(const void *ptr,
1132 int depth, int max_depth,
1133 int is_ref,
1134 void *private_data),
1135 void *private_data)
1137 struct talloc_chunk *c, *tc;
1139 if (ptr == NULL) {
1140 ptr = null_context;
1142 if (ptr == NULL) return;
1144 tc = talloc_chunk_from_ptr(ptr);
1146 if (tc->flags & TALLOC_FLAG_LOOP) {
1147 return;
1150 callback(ptr, depth, max_depth, 0, private_data);
1152 if (max_depth >= 0 && depth >= max_depth) {
1153 return;
1156 tc->flags |= TALLOC_FLAG_LOOP;
1157 for (c=tc->child;c;c=c->next) {
1158 if (c->name == TALLOC_MAGIC_REFERENCE) {
1159 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1160 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1161 } else {
1162 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1165 tc->flags &= ~TALLOC_FLAG_LOOP;
1168 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1170 const char *name = talloc_get_name(ptr);
1171 FILE *f = (FILE *)_f;
1173 if (is_ref) {
1174 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1175 return;
1178 if (depth == 0) {
1179 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1180 (max_depth < 0 ? "full " :""), name,
1181 (unsigned long)talloc_total_size(ptr),
1182 (unsigned long)talloc_total_blocks(ptr));
1183 return;
1186 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1187 depth*4, "",
1188 name,
1189 (unsigned long)talloc_total_size(ptr),
1190 (unsigned long)talloc_total_blocks(ptr),
1191 (int)talloc_reference_count(ptr), ptr);
1193 #if 0
1194 fprintf(f, "content: ");
1195 if (talloc_total_size(ptr)) {
1196 int tot = talloc_total_size(ptr);
1197 int i;
1199 for (i = 0; i < tot; i++) {
1200 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1201 fprintf(f, "%c", ((char *)ptr)[i]);
1202 } else {
1203 fprintf(f, "~%02x", ((char *)ptr)[i]);
1207 fprintf(f, "\n");
1208 #endif
1212 report on memory usage by all children of a pointer, giving a full tree view
1214 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1216 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1217 fflush(f);
1221 report on memory usage by all children of a pointer, giving a full tree view
1223 void talloc_report_full(const void *ptr, FILE *f)
1225 talloc_report_depth_file(ptr, 0, -1, f);
1229 report on memory usage by all children of a pointer
1231 void talloc_report(const void *ptr, FILE *f)
1233 talloc_report_depth_file(ptr, 0, 1, f);
1237 report on any memory hanging off the null context
1239 static void talloc_report_null(void)
1241 if (talloc_total_size(null_context) != 0) {
1242 talloc_report(null_context, stderr);
1247 report on any memory hanging off the null context
1249 static void talloc_report_null_full(void)
1251 if (talloc_total_size(null_context) != 0) {
1252 talloc_report_full(null_context, stderr);
1257 enable tracking of the NULL context
1259 void talloc_enable_null_tracking(void)
1261 if (null_context == NULL) {
1262 null_context = _talloc_named_const(NULL, 0, "null_context");
1267 disable tracking of the NULL context
1269 void talloc_disable_null_tracking(void)
1271 _talloc_free(null_context);
1272 null_context = NULL;
1276 enable leak reporting on exit
1278 void talloc_enable_leak_report(void)
1280 talloc_enable_null_tracking();
1281 atexit(talloc_report_null);
1285 enable full leak reporting on exit
1287 void talloc_enable_leak_report_full(void)
1289 talloc_enable_null_tracking();
1290 atexit(talloc_report_null_full);
1294 talloc and zero memory.
1296 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1298 void *p = _talloc_named_const(ctx, size, name);
1300 if (p) {
1301 memset(p, '\0', size);
1304 return p;
1308 memdup with a talloc.
1310 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1312 void *newp = _talloc_named_const(t, size, name);
1314 if (likely(newp)) {
1315 memcpy(newp, p, size);
1318 return newp;
1321 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1323 char *ret;
1325 ret = (char *)__talloc(t, len + 1);
1326 if (unlikely(!ret)) return NULL;
1328 memcpy(ret, p, len);
1329 ret[len] = 0;
1331 _talloc_set_name_const(ret, ret);
1332 return ret;
1336 strdup with a talloc
1338 char *talloc_strdup(const void *t, const char *p)
1340 if (unlikely(!p)) return NULL;
1341 return __talloc_strlendup(t, p, strlen(p));
1345 strndup with a talloc
1347 char *talloc_strndup(const void *t, const char *p, size_t n)
1349 if (unlikely(!p)) return NULL;
1350 return __talloc_strlendup(t, p, strnlen(p, n));
1353 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1354 const char *a, size_t alen)
1356 char *ret;
1358 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1359 if (unlikely(!ret)) return NULL;
1361 /* append the string and the trailing \0 */
1362 memcpy(&ret[slen], a, alen);
1363 ret[slen+alen] = 0;
1365 _talloc_set_name_const(ret, ret);
1366 return ret;
1370 * Appends at the end of the string.
1372 char *talloc_strdup_append(char *s, const char *a)
1374 if (unlikely(!s)) {
1375 return talloc_strdup(NULL, a);
1378 if (unlikely(!a)) {
1379 return s;
1382 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1386 * Appends at the end of the talloc'ed buffer,
1387 * not the end of the string.
1389 char *talloc_strdup_append_buffer(char *s, const char *a)
1391 size_t slen;
1393 if (unlikely(!s)) {
1394 return talloc_strdup(NULL, a);
1397 if (unlikely(!a)) {
1398 return s;
1401 slen = talloc_get_size(s);
1402 if (likely(slen > 0)) {
1403 slen--;
1406 return __talloc_strlendup_append(s, slen, a, strlen(a));
1410 * Appends at the end of the string.
1412 char *talloc_strndup_append(char *s, const char *a, size_t n)
1414 if (unlikely(!s)) {
1415 return talloc_strdup(NULL, a);
1418 if (unlikely(!a)) {
1419 return s;
1422 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1426 * Appends at the end of the talloc'ed buffer,
1427 * not the end of the string.
1429 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1431 size_t slen;
1433 if (unlikely(!s)) {
1434 return talloc_strdup(NULL, a);
1437 if (unlikely(!a)) {
1438 return s;
1441 slen = talloc_get_size(s);
1442 if (likely(slen > 0)) {
1443 slen--;
1446 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1449 #ifndef HAVE_VA_COPY
1450 #ifdef HAVE___VA_COPY
1451 #define va_copy(dest, src) __va_copy(dest, src)
1452 #else
1453 #define va_copy(dest, src) (dest) = (src)
1454 #endif
1455 #endif
1457 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1459 int len;
1460 char *ret;
1461 va_list ap2;
1462 char c;
1464 /* this call looks strange, but it makes it work on older solaris boxes */
1465 va_copy(ap2, ap);
1466 len = vsnprintf(&c, 1, fmt, ap2);
1467 va_end(ap2);
1468 if (unlikely(len < 0)) {
1469 abort(); // return NULL;
1472 ret = (char *)__talloc(t, len+1);
1473 if (unlikely(!ret)) return NULL;
1475 va_copy(ap2, ap);
1476 vsnprintf(ret, len+1, fmt, ap2);
1477 va_end(ap2);
1479 _talloc_set_name_const(ret, ret);
1480 return ret;
1485 Perform string formatting, and return a pointer to newly allocated
1486 memory holding the result, inside a memory pool.
1488 char *talloc_asprintf(const void *t, const char *fmt, ...)
1490 va_list ap;
1491 char *ret;
1493 va_start(ap, fmt);
1494 ret = talloc_vasprintf(t, fmt, ap);
1495 va_end(ap);
1496 return ret;
1499 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1500 const char *fmt, va_list ap)
1501 PRINTF_ATTRIBUTE(3,0);
1503 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1504 const char *fmt, va_list ap)
1506 ssize_t alen;
1507 va_list ap2;
1508 char c;
1510 va_copy(ap2, ap);
1511 alen = vsnprintf(&c, 1, fmt, ap2);
1512 va_end(ap2);
1514 if (alen <= 0) {
1515 /* Either the vsnprintf failed or the format resulted in
1516 * no characters being formatted. In the former case, we
1517 * ought to return NULL, in the latter we ought to return
1518 * the original string. Most current callers of this
1519 * function expect it to never return NULL.
1521 return s;
1524 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1525 if (!s) return NULL;
1527 va_copy(ap2, ap);
1528 vsnprintf(s + slen, alen + 1, fmt, ap2);
1529 va_end(ap2);
1531 _talloc_set_name_const(s, s);
1532 return s;
1536 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1537 * and return @p s, which may have moved. Good for gradually
1538 * accumulating output into a string buffer. Appends at the end
1539 * of the string.
1541 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1543 if (unlikely(!s)) {
1544 return talloc_vasprintf(NULL, fmt, ap);
1547 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1551 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1552 * and return @p s, which may have moved. Always appends at the
1553 * end of the talloc'ed buffer, not the end of the string.
1555 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1557 size_t slen;
1559 if (unlikely(!s)) {
1560 return talloc_vasprintf(NULL, fmt, ap);
1563 slen = talloc_get_size(s);
1564 if (likely(slen > 0)) {
1565 slen--;
1568 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1572 Realloc @p s to append the formatted result of @p fmt and return @p
1573 s, which may have moved. Good for gradually accumulating output
1574 into a string buffer.
1576 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1578 va_list ap;
1580 va_start(ap, fmt);
1581 s = talloc_vasprintf_append(s, fmt, ap);
1582 va_end(ap);
1583 return s;
1587 Realloc @p s to append the formatted result of @p fmt and return @p
1588 s, which may have moved. Good for gradually accumulating output
1589 into a buffer.
1591 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1593 va_list ap;
1595 va_start(ap, fmt);
1596 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1597 va_end(ap);
1598 return s;
1602 alloc an array, checking for integer overflow in the array size
1604 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1606 if (count >= MAX_TALLOC_SIZE/el_size) {
1607 abort(); // return NULL;
1609 return _talloc_named_const(ctx, el_size * count, name);
1613 alloc an zero array, checking for integer overflow in the array size
1615 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1617 if (count >= MAX_TALLOC_SIZE/el_size) {
1618 abort(); // return NULL;
1620 return _talloc_zero(ctx, el_size * count, name);
1624 realloc an array, checking for integer overflow in the array size
1626 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1628 if (count >= MAX_TALLOC_SIZE/el_size) {
1629 abort(); // return NULL;
1631 return _talloc_realloc(ctx, ptr, el_size * count, name);
1635 a function version of talloc_realloc(), so it can be passed as a function pointer
1636 to libraries that want a realloc function (a realloc function encapsulates
1637 all the basic capabilities of an allocation library, which is why this is useful)
1639 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1641 return _talloc_realloc(context, ptr, size, NULL);
1645 static int talloc_autofree_destructor(void *ptr)
1647 autofree_context = NULL;
1648 return 0;
1651 static void talloc_autofree(void)
1653 _talloc_free(autofree_context);
1657 return a context which will be auto-freed on exit
1658 this is useful for reducing the noise in leak reports
1660 void *talloc_autofree_context(void)
1662 if (autofree_context == NULL) {
1663 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1664 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1665 atexit(talloc_autofree);
1667 return autofree_context;
1670 size_t talloc_get_size(const void *context)
1672 struct talloc_chunk *tc;
1674 if (context == NULL)
1675 return 0;
1677 tc = talloc_chunk_from_ptr(context);
1679 return tc->size;
1683 find a parent of this context that has the given name, if any
1685 void *talloc_find_parent_byname(const void *context, const char *name)
1687 struct talloc_chunk *tc;
1689 if (context == NULL) {
1690 return NULL;
1693 tc = talloc_chunk_from_ptr(context);
1694 while (tc) {
1695 if (tc->name && strcmp(tc->name, name) == 0) {
1696 return TC_PTR_FROM_CHUNK(tc);
1698 while (tc && tc->prev) tc = tc->prev;
1699 if (tc) {
1700 tc = tc->parent;
1703 return NULL;
1707 show the parentage of a context
1709 void talloc_show_parents(const void *context, FILE *file)
1711 struct talloc_chunk *tc;
1713 if (context == NULL) {
1714 fprintf(file, "talloc no parents for NULL\n");
1715 return;
1718 tc = talloc_chunk_from_ptr(context);
1719 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1720 while (tc) {
1721 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1722 while (tc && tc->prev) tc = tc->prev;
1723 if (tc) {
1724 tc = tc->parent;
1727 fflush(file);
1731 return 1 if ptr is a parent of context
1733 int talloc_is_parent(const void *context, const void *ptr)
1735 struct talloc_chunk *tc;
1737 if (context == NULL) {
1738 return 0;
1741 tc = talloc_chunk_from_ptr(context);
1742 while (tc) {
1743 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1744 while (tc && tc->prev) tc = tc->prev;
1745 if (tc) {
1746 tc = tc->parent;
1749 return 0;