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
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 inspired by http://swapped.cc/halloc/
39 /* assume a modern system */
43 /* use this to force every realloc to change the pointer, to stress test
44 code that might not cope */
45 #define ALWAYS_REALLOC 0
48 #define MAX_TALLOC_SIZE 0x10000000
49 #define TALLOC_MAGIC 0xe814ec4f
50 #define TALLOC_MAGIC_FREE 0x7faebef3
51 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
53 /* by default we abort when given a bad pointer (such as when talloc_free() is called
54 on a pointer that came from malloc() */
56 #define TALLOC_ABORT(reason) abort()
59 #ifndef discard_const_p
60 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
61 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
63 # define discard_const_p(type, ptr) ((type *)(ptr))
67 /* this null_context is only used if talloc_enable_leak_report() or
68 talloc_enable_leak_report_full() is called, otherwise it remains
71 static const void *null_context;
72 static void *cleanup_context;
75 struct talloc_reference_handle {
76 struct talloc_reference_handle *next, *prev;
80 typedef int (*talloc_destructor_t)(void *);
83 struct talloc_chunk *next, *prev;
84 struct talloc_chunk *parent, *child;
85 struct talloc_reference_handle *refs;
88 talloc_destructor_t destructor;
92 /* panic if we get a bad magic value */
93 static struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
95 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, ptr)-1;
96 if (tc->magic != TALLOC_MAGIC) {
97 if (tc->magic == TALLOC_MAGIC_FREE) {
98 TALLOC_ABORT("Bad talloc magic value - double free");
100 TALLOC_ABORT("Bad talloc magic value - unknown value");
107 /* hook into the front of the list */
108 #define _TLIST_ADD(list, p) \
112 (p)->next = (p)->prev = NULL; \
114 (list)->prev = (p); \
115 (p)->next = (list); \
121 /* remove an element from a list - element doesn't have to be in list. */
122 #define _TLIST_REMOVE(list, p) \
124 if ((p) == (list)) { \
125 (list) = (p)->next; \
126 if (list) (list)->prev = NULL; \
128 if ((p)->prev) (p)->prev->next = (p)->next; \
129 if ((p)->next) (p)->next->prev = (p)->prev; \
131 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
136 return the parent chunk of a pointer
138 static struct talloc_chunk *talloc_parent_chunk(const void *ptr)
140 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
141 while (tc->prev) tc=tc->prev;
145 void *talloc_parent(const void *ptr)
147 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
148 return (void *)(tc+1);
152 Allocate a bit of memory as a child of an existing pointer
154 void *_talloc(const void *context, size_t size)
156 struct talloc_chunk *tc;
158 if (context == NULL) {
159 context = null_context;
162 if (size >= MAX_TALLOC_SIZE) {
166 tc = malloc(sizeof(*tc)+size);
167 if (tc == NULL) return NULL;
170 tc->magic = TALLOC_MAGIC;
171 tc->destructor = NULL;
177 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
182 parent->child->parent = NULL;
185 _TLIST_ADD(parent->child, tc);
187 tc->next = tc->prev = tc->parent = NULL;
190 return (void *)(tc+1);
195 setup a destructor to be called on free of a pointer
196 the destructor should return 0 on success, or -1 on failure.
197 if the destructor fails then the free is failed, and the memory can
198 be continued to be used
200 void talloc_set_destructor(const void *ptr, int (*destructor)(void *))
202 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
203 tc->destructor = destructor;
207 increase the reference count on a piece of memory.
209 void talloc_increase_ref_count(const void *ptr)
211 talloc_reference(null_context, ptr);
215 helper for talloc_reference()
217 static int talloc_reference_destructor(void *ptr)
219 struct talloc_reference_handle *handle = ptr;
220 struct talloc_chunk *tc1 = talloc_chunk_from_ptr(ptr);
221 struct talloc_chunk *tc2 = talloc_chunk_from_ptr(handle->ptr);
222 if (tc1->destructor != (talloc_destructor_t)-1) {
223 tc1->destructor = NULL;
225 _TLIST_REMOVE(tc2->refs, handle);
231 make a secondary reference to a pointer, hanging off the given context.
232 the pointer remains valid until both the original caller and this given
235 the major use for this is when two different structures need to reference the
236 same underlying data, and you want to be able to free the two instances separately,
239 void *talloc_reference(const void *context, const void *ptr)
241 struct talloc_chunk *tc;
242 struct talloc_reference_handle *handle;
243 if (ptr == NULL) return NULL;
245 tc = talloc_chunk_from_ptr(ptr);
246 handle = talloc_named_const(context, sizeof(*handle), TALLOC_MAGIC_REFERENCE);
248 if (handle == NULL) return NULL;
250 /* note that we hang the destructor off the handle, not the
251 main context as that allows the caller to still setup their
252 own destructor on the context if they want to */
253 talloc_set_destructor(handle, talloc_reference_destructor);
254 handle->ptr = discard_const_p(void, ptr);
255 _TLIST_ADD(tc->refs, handle);
260 remove a secondary reference to a pointer. This undo's what
261 talloc_reference() has done. The context and pointer arguments
262 must match those given to a talloc_reference()
264 static int talloc_unreference(const void *context, const void *ptr)
266 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
267 struct talloc_reference_handle *h;
269 if (context == NULL) {
270 context = null_context;
273 for (h=tc->refs;h;h=h->next) {
274 struct talloc_chunk *p = talloc_parent_chunk(h);
275 if ((p==NULL && context==NULL) || p+1 == context) break;
281 talloc_set_destructor(h, NULL);
282 _TLIST_REMOVE(tc->refs, h);
288 remove a specific parent context from a pointer. This is a more
289 controlled varient of talloc_free()
291 int talloc_unlink(const void *context, void *ptr)
293 struct talloc_chunk *tc_p, *new_p;
300 if (context == NULL) {
301 context = null_context;
304 if (talloc_unreference(context, ptr) == 0) {
308 if (context == NULL) {
309 if (talloc_parent_chunk(ptr) != NULL) {
313 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
318 tc_p = talloc_chunk_from_ptr(ptr);
320 if (tc_p->refs == NULL) {
321 return talloc_free(ptr);
324 new_p = talloc_parent_chunk(tc_p->refs);
326 new_parent = new_p+1;
331 if (talloc_unreference(new_parent, ptr) != 0) {
335 talloc_steal(new_parent, ptr);
341 add a name to an existing pointer - va_list version
343 static void talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
345 static void talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
347 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
348 tc->name = talloc_vasprintf(ptr, fmt, ap);
350 talloc_set_name_const(tc->name, ".name");
355 add a name to an existing pointer
357 void talloc_set_name(const void *ptr, const char *fmt, ...)
361 talloc_set_name_v(ptr, fmt, ap);
366 more efficient way to add a name to a pointer - the name must point to a
369 void talloc_set_name_const(const void *ptr, const char *name)
371 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
376 create a named talloc pointer. Any talloc pointer can be named, and
377 talloc_named() operates just like talloc() except that it allows you
380 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
385 ptr = _talloc(context, size);
386 if (ptr == NULL) return NULL;
389 talloc_set_name_v(ptr, fmt, ap);
396 create a named talloc pointer. Any talloc pointer can be named, and
397 talloc_named() operates just like talloc() except that it allows you
400 void *talloc_named_const(const void *context, size_t size, const char *name)
404 ptr = _talloc(context, size);
409 talloc_set_name_const(ptr, name);
415 return the name of a talloc ptr, or "UNNAMED"
417 const char *talloc_get_name(const void *ptr)
419 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
420 if (tc->name == TALLOC_MAGIC_REFERENCE) {
431 check if a pointer has the given name. If it does, return the pointer,
432 otherwise return NULL
434 void *talloc_check_name(const void *ptr, const char *name)
437 if (ptr == NULL) return NULL;
438 pname = talloc_get_name(ptr);
439 if (pname == name || strcmp(pname, name) == 0) {
440 return discard_const_p(void, ptr);
447 this is for compatibility with older versions of talloc
449 void *talloc_init(const char *fmt, ...)
454 ptr = _talloc(NULL, 0);
455 if (ptr == NULL) return NULL;
458 talloc_set_name_v(ptr, fmt, ap);
465 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
466 should probably not be used in new code. It's in here to keep the talloc
467 code consistent across Samba 3 and 4.
469 void talloc_free_children(void *ptr)
471 struct talloc_chunk *tc;
477 tc = talloc_chunk_from_ptr(ptr);
480 /* we need to work out who will own an abandoned child
481 if it cannot be freed. In priority order, the first
482 choice is owner of any remaining reference to this
483 pointer, the second choice is our parent, and the
484 final choice is the null context. */
485 void *child = tc->child+1;
486 const void *new_parent = null_context;
487 if (tc->child->refs) {
488 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
489 if (p) new_parent = p+1;
491 if (talloc_free(child) == -1) {
492 if (new_parent == null_context) {
493 struct talloc_chunk *p = talloc_parent_chunk(ptr);
494 if (p) new_parent = p+1;
496 talloc_steal(new_parent, child);
502 free a talloc pointer. This also frees all child pointers of this
505 return 0 if the memory is actually freed, otherwise -1. The memory
506 will not be freed if the ref_count is > 1 or the destructor (if
507 any) returns non-zero
509 int talloc_free(void *ptr)
511 struct talloc_chunk *tc;
517 tc = talloc_chunk_from_ptr(ptr);
520 talloc_reference_destructor(tc->refs);
524 if (tc->destructor) {
525 talloc_destructor_t d = tc->destructor;
526 if (d == (talloc_destructor_t)-1) {
529 tc->destructor = (talloc_destructor_t)-1;
534 tc->destructor = NULL;
537 talloc_free_children(ptr);
540 _TLIST_REMOVE(tc->parent->child, tc);
541 if (tc->parent->child) {
542 tc->parent->child->parent = tc->parent;
545 if (tc->prev) tc->prev->next = tc->next;
546 if (tc->next) tc->next->prev = tc->prev;
549 tc->magic = TALLOC_MAGIC_FREE;
558 A talloc version of realloc. The context argument is only used if
561 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
563 struct talloc_chunk *tc;
566 /* size zero is equivalent to free() */
572 if (size >= MAX_TALLOC_SIZE) {
576 /* realloc(NULL) is equavalent to malloc() */
578 return talloc_named_const(context, size, name);
581 tc = talloc_chunk_from_ptr(ptr);
583 /* don't allow realloc on referenced pointers */
588 /* by resetting magic we catch users of the old memory */
589 tc->magic = TALLOC_MAGIC_FREE;
592 new_ptr = malloc(size + sizeof(*tc));
594 memcpy(new_ptr, tc, tc->size + sizeof(*tc));
598 new_ptr = realloc(tc, size + sizeof(*tc));
601 tc->magic = TALLOC_MAGIC;
606 tc->magic = TALLOC_MAGIC;
608 tc->parent->child = new_ptr;
611 tc->child->parent = new_ptr;
622 talloc_set_name_const(tc+1, name);
624 return (void *)(tc+1);
628 move a lump of memory from one talloc context to another return the
629 ptr on success, or NULL if it could not be transferred.
630 passing NULL as ptr will always return NULL with no side effects.
632 void *talloc_steal(const void *new_ctx, const void *ptr)
634 struct talloc_chunk *tc, *new_tc;
640 if (new_ctx == NULL) {
641 new_ctx = null_context;
644 tc = talloc_chunk_from_ptr(ptr);
646 if (new_ctx == NULL) {
648 _TLIST_REMOVE(tc->parent->child, tc);
649 if (tc->parent->child) {
650 tc->parent->child->parent = tc->parent;
653 if (tc->prev) tc->prev->next = tc->next;
654 if (tc->next) tc->next->prev = tc->prev;
657 tc->parent = tc->next = tc->prev = NULL;
658 return discard_const_p(void, ptr);
661 new_tc = talloc_chunk_from_ptr(new_ctx);
664 return discard_const_p(void, ptr);
668 _TLIST_REMOVE(tc->parent->child, tc);
669 if (tc->parent->child) {
670 tc->parent->child->parent = tc->parent;
673 if (tc->prev) tc->prev->next = tc->next;
674 if (tc->next) tc->next->prev = tc->prev;
678 if (new_tc->child) new_tc->child->parent = NULL;
679 _TLIST_ADD(new_tc->child, tc);
681 return discard_const_p(void, ptr);
685 return the total size of a talloc pool (subtree)
687 off_t talloc_total_size(const void *ptr)
690 struct talloc_chunk *c, *tc;
699 tc = talloc_chunk_from_ptr(ptr);
702 for (c=tc->child;c;c=c->next) {
703 total += talloc_total_size(c+1);
709 return the total number of blocks in a talloc pool (subtree)
711 off_t talloc_total_blocks(const void *ptr)
714 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
717 for (c=tc->child;c;c=c->next) {
718 total += talloc_total_blocks(c+1);
724 return the number of external references to a pointer
726 static int talloc_reference_count(const void *ptr)
728 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
729 struct talloc_reference_handle *h;
732 for (h=tc->refs;h;h=h->next) {
739 report on memory usage by all children of a pointer, giving a full tree view
741 void talloc_report_depth(const void *ptr, FILE *f, int depth)
743 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
745 for (c=tc->child;c;c=c->next) {
746 if (c->name == TALLOC_MAGIC_REFERENCE) {
747 struct talloc_reference_handle *handle = (void *)(c+1);
748 const char *name2 = talloc_get_name(handle->ptr);
749 fprintf(f, "%*sreference to: %s\n", depth*4, "", name2);
751 const char *name = talloc_get_name(c+1);
752 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d)\n",
755 (unsigned long)talloc_total_size(c+1),
756 (unsigned long)talloc_total_blocks(c+1),
757 talloc_reference_count(c+1));
758 talloc_report_depth(c+1, f, depth+1);
765 report on memory usage by all children of a pointer, giving a full tree view
767 void talloc_report_full(const void *ptr, FILE *f)
772 if (ptr == NULL) return;
774 fprintf(f,"full talloc report on '%s' (total %lu bytes in %lu blocks)\n",
775 talloc_get_name(ptr),
776 (unsigned long)talloc_total_size(ptr),
777 (unsigned long)talloc_total_blocks(ptr));
779 talloc_report_depth(ptr, f, 1);
784 report on memory usage by all children of a pointer
786 void talloc_report(const void *ptr, FILE *f)
788 struct talloc_chunk *c, *tc;
793 if (ptr == NULL) return;
795 fprintf(f,"talloc report on '%s' (total %lu bytes in %lu blocks)\n",
796 talloc_get_name(ptr),
797 (unsigned long)talloc_total_size(ptr),
798 (unsigned long)talloc_total_blocks(ptr));
800 tc = talloc_chunk_from_ptr(ptr);
802 for (c=tc->child;c;c=c->next) {
803 fprintf(f, "\t%-30s contains %6lu bytes in %3lu blocks\n",
804 talloc_get_name(c+1),
805 (unsigned long)talloc_total_size(c+1),
806 (unsigned long)talloc_total_blocks(c+1));
812 report on any memory hanging off the null context
814 static void talloc_report_null(void)
816 if (talloc_total_size(null_context) != 0) {
817 talloc_report(null_context, stderr);
822 report on any memory hanging off the null context
824 static void talloc_report_null_full(void)
826 if (talloc_total_size(null_context) != 0) {
827 talloc_report_full(null_context, stderr);
832 enable tracking of the NULL context
834 void talloc_enable_null_tracking(void)
836 if (null_context == NULL) {
837 null_context = talloc_named_const(NULL, 0, "null_context");
842 enable leak reporting on exit
844 void talloc_enable_leak_report(void)
846 talloc_enable_null_tracking();
847 atexit(talloc_report_null);
851 enable full leak reporting on exit
853 void talloc_enable_leak_report_full(void)
855 talloc_enable_null_tracking();
856 atexit(talloc_report_null_full);
860 talloc and zero memory.
862 void *_talloc_zero(const void *ctx, size_t size, const char *name)
864 void *p = talloc_named_const(ctx, size, name);
867 memset(p, '\0', size);
875 memdup with a talloc.
877 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
879 void *newp = talloc_named_const(t, size, name);
882 memcpy(newp, p, size);
891 char *talloc_strdup(const void *t, const char *p)
897 ret = talloc_memdup(t, p, strlen(p) + 1);
899 talloc_set_name_const(ret, ret);
905 strndup with a talloc
907 char *talloc_strndup(const void *t, const char *p, size_t n)
912 for (len=0; p[len] && len<n; len++) ;
914 ret = _talloc(t, len + 1);
915 if (!ret) { return NULL; }
918 talloc_set_name_const(ret, ret);
924 #define VA_COPY(dest, src) va_copy(dest, src)
925 #elif defined(HAVE___VA_COPY)
926 #define VA_COPY(dest, src) __va_copy(dest, src)
928 #define VA_COPY(dest, src) (dest) = (src)
932 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
940 len = vsnprintf(NULL, 0, fmt, ap2);
942 ret = _talloc(t, len+1);
945 vsnprintf(ret, len+1, fmt, ap2);
946 talloc_set_name_const(ret, ret);
954 Perform string formatting, and return a pointer to newly allocated
955 memory holding the result, inside a memory pool.
957 char *talloc_asprintf(const void *t, const char *fmt, ...)
963 ret = talloc_vasprintf(t, fmt, ap);
970 * Realloc @p s to append the formatted result of @p fmt and @p ap,
971 * and return @p s, which may have moved. Good for gradually
972 * accumulating output into a string buffer.
975 static char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
977 static char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
979 struct talloc_chunk *tc;
984 return talloc_vasprintf(NULL, fmt, ap);
987 tc = talloc_chunk_from_ptr(s);
991 s_len = tc->size - 1;
992 len = vsnprintf(NULL, 0, fmt, ap2);
994 s = talloc_realloc(NULL, s, char, s_len + len+1);
999 vsnprintf(s+s_len, len+1, fmt, ap2);
1000 talloc_set_name_const(s, s);
1006 Realloc @p s to append the formatted result of @p fmt and return @p
1007 s, which may have moved. Good for gradually accumulating output
1008 into a string buffer.
1010 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1015 s = talloc_vasprintf_append(s, fmt, ap);
1021 alloc an array, checking for integer overflow in the array size
1023 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1025 if (count >= MAX_TALLOC_SIZE/el_size) {
1028 return talloc_named_const(ctx, el_size * count, name);
1032 alloc an zero array, checking for integer overflow in the array size
1034 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1036 if (count >= MAX_TALLOC_SIZE/el_size) {
1039 return _talloc_zero(ctx, el_size * count, name);
1044 realloc an array, checking for integer overflow in the array size
1046 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1048 if (count >= MAX_TALLOC_SIZE/el_size) {
1051 return _talloc_realloc(ctx, ptr, el_size * count, name);
1055 a function version of talloc_realloc(), so it can be passed as a function pointer
1056 to libraries that want a realloc function (a realloc function encapsulates
1057 all the basic capabilities of an allocation library, which is why this is useful)
1059 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1061 return _talloc_realloc(context, ptr, size, NULL);
1065 static void talloc_autofree(void)
1067 talloc_free(cleanup_context);
1068 cleanup_context = NULL;
1072 return a context which will be auto-freed on exit
1073 this is useful for reducing the noise in leak reports
1075 void *talloc_autofree_context(void)
1077 if (cleanup_context == NULL) {
1078 cleanup_context = talloc_named_const(NULL, 0, "autofree_context");
1079 atexit(talloc_autofree);
1081 return cleanup_context;