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/
36 #ifdef HAVE_SYS_TYPES_H
37 #include <sys/types.h>
56 /* use this to force every realloc to change the pointer, to stress test
57 code that might not cope */
58 #define ALWAYS_REALLOC 0
61 #define MAX_TALLOC_SIZE 0x10000000
62 #define TALLOC_MAGIC 0xe814ec4f
63 #define TALLOC_MAGIC_FREE 0x7faebef3
64 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
66 /* by default we abort when given a bad pointer (such as when talloc_free() is called
67 on a pointer that came from malloc() */
69 #define TALLOC_ABORT(reason) abort()
72 #ifndef discard_const_p
73 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
74 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
76 # define discard_const_p(type, ptr) ((type *)(ptr))
80 /* this null_context is only used if talloc_enable_leak_report() or
81 talloc_enable_leak_report_full() is called, otherwise it remains
84 static const void *null_context;
85 static void *cleanup_context;
88 struct talloc_reference_handle {
89 struct talloc_reference_handle *next, *prev;
93 typedef int (*talloc_destructor_t)(void *);
96 struct talloc_chunk *next, *prev;
97 struct talloc_chunk *parent, *child;
98 struct talloc_reference_handle *refs;
100 talloc_destructor_t destructor;
108 /* panic if we get a bad magic value */
109 static struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
111 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, ptr)-1;
112 if (tc->u.magic != TALLOC_MAGIC) {
113 if (tc->u.magic == TALLOC_MAGIC_FREE) {
114 TALLOC_ABORT("Bad talloc magic value - double free");
116 TALLOC_ABORT("Bad talloc magic value - unknown value");
123 /* hook into the front of the list */
124 #define _TLIST_ADD(list, p) \
128 (p)->next = (p)->prev = NULL; \
130 (list)->prev = (p); \
131 (p)->next = (list); \
137 /* remove an element from a list - element doesn't have to be in list. */
138 #define _TLIST_REMOVE(list, p) \
140 if ((p) == (list)) { \
141 (list) = (p)->next; \
142 if (list) (list)->prev = NULL; \
144 if ((p)->prev) (p)->prev->next = (p)->next; \
145 if ((p)->next) (p)->next->prev = (p)->prev; \
147 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
152 return the parent chunk of a pointer
154 static struct talloc_chunk *talloc_parent_chunk(const void *ptr)
156 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
157 while (tc->prev) tc=tc->prev;
161 void *talloc_parent(const void *ptr)
163 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
164 return (void *)(tc+1);
168 Allocate a bit of memory as a child of an existing pointer
170 void *_talloc(const void *context, size_t size)
172 struct talloc_chunk *tc;
174 if (context == NULL) {
175 context = null_context;
178 if (size >= MAX_TALLOC_SIZE) {
182 tc = malloc(sizeof(*tc)+size);
183 if (tc == NULL) return NULL;
186 tc->u.magic = TALLOC_MAGIC;
187 tc->destructor = NULL;
193 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
198 parent->child->parent = NULL;
201 _TLIST_ADD(parent->child, tc);
203 tc->next = tc->prev = tc->parent = NULL;
206 return (void *)(tc+1);
211 setup a destructor to be called on free of a pointer
212 the destructor should return 0 on success, or -1 on failure.
213 if the destructor fails then the free is failed, and the memory can
214 be continued to be used
216 void talloc_set_destructor(const void *ptr, int (*destructor)(void *))
218 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
219 tc->destructor = destructor;
223 increase the reference count on a piece of memory.
225 void talloc_increase_ref_count(const void *ptr)
227 talloc_reference(null_context, ptr);
231 helper for talloc_reference()
233 static int talloc_reference_destructor(void *ptr)
235 struct talloc_reference_handle *handle = ptr;
236 struct talloc_chunk *tc1 = talloc_chunk_from_ptr(ptr);
237 struct talloc_chunk *tc2 = talloc_chunk_from_ptr(handle->ptr);
238 if (tc1->destructor != (talloc_destructor_t)-1) {
239 tc1->destructor = NULL;
241 _TLIST_REMOVE(tc2->refs, handle);
247 make a secondary reference to a pointer, hanging off the given context.
248 the pointer remains valid until both the original caller and this given
251 the major use for this is when two different structures need to reference the
252 same underlying data, and you want to be able to free the two instances separately,
255 void *talloc_reference(const void *context, const void *ptr)
257 struct talloc_chunk *tc;
258 struct talloc_reference_handle *handle;
259 if (ptr == NULL) return NULL;
261 tc = talloc_chunk_from_ptr(ptr);
262 handle = talloc_named_const(context, sizeof(*handle), TALLOC_MAGIC_REFERENCE);
264 if (handle == NULL) return NULL;
266 /* note that we hang the destructor off the handle, not the
267 main context as that allows the caller to still setup their
268 own destructor on the context if they want to */
269 talloc_set_destructor(handle, talloc_reference_destructor);
270 handle->ptr = discard_const_p(void, ptr);
271 _TLIST_ADD(tc->refs, handle);
276 remove a secondary reference to a pointer. This undo's what
277 talloc_reference() has done. The context and pointer arguments
278 must match those given to a talloc_reference()
280 static int talloc_unreference(const void *context, const void *ptr)
282 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
283 struct talloc_reference_handle *h;
285 if (context == NULL) {
286 context = null_context;
289 for (h=tc->refs;h;h=h->next) {
290 struct talloc_chunk *p = talloc_parent_chunk(h);
291 if ((p==NULL && context==NULL) || p+1 == context) break;
297 talloc_set_destructor(h, NULL);
298 _TLIST_REMOVE(tc->refs, h);
304 remove a specific parent context from a pointer. This is a more
305 controlled varient of talloc_free()
307 int talloc_unlink(const void *context, void *ptr)
309 struct talloc_chunk *tc_p, *new_p;
316 if (context == NULL) {
317 context = null_context;
320 if (talloc_unreference(context, ptr) == 0) {
324 if (context == NULL) {
325 if (talloc_parent_chunk(ptr) != NULL) {
329 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
334 tc_p = talloc_chunk_from_ptr(ptr);
336 if (tc_p->refs == NULL) {
337 return talloc_free(ptr);
340 new_p = talloc_parent_chunk(tc_p->refs);
342 new_parent = new_p+1;
347 if (talloc_unreference(new_parent, ptr) != 0) {
351 talloc_steal(new_parent, ptr);
357 add a name to an existing pointer - va_list version
359 static void talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
361 static void talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
363 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
364 tc->name = talloc_vasprintf(ptr, fmt, ap);
366 talloc_set_name_const(tc->name, ".name");
371 add a name to an existing pointer
373 void talloc_set_name(const void *ptr, const char *fmt, ...)
377 talloc_set_name_v(ptr, fmt, ap);
382 more efficient way to add a name to a pointer - the name must point to a
385 void talloc_set_name_const(const void *ptr, const char *name)
387 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
392 create a named talloc pointer. Any talloc pointer can be named, and
393 talloc_named() operates just like talloc() except that it allows you
396 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
401 ptr = _talloc(context, size);
402 if (ptr == NULL) return NULL;
405 talloc_set_name_v(ptr, fmt, ap);
412 create a named talloc pointer. Any talloc pointer can be named, and
413 talloc_named() operates just like talloc() except that it allows you
416 void *talloc_named_const(const void *context, size_t size, const char *name)
420 ptr = _talloc(context, size);
425 talloc_set_name_const(ptr, name);
431 return the name of a talloc ptr, or "UNNAMED"
433 const char *talloc_get_name(const void *ptr)
435 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
436 if (tc->name == TALLOC_MAGIC_REFERENCE) {
447 check if a pointer has the given name. If it does, return the pointer,
448 otherwise return NULL
450 void *talloc_check_name(const void *ptr, const char *name)
453 if (ptr == NULL) return NULL;
454 pname = talloc_get_name(ptr);
455 if (pname == name || strcmp(pname, name) == 0) {
456 return discard_const_p(void, ptr);
463 this is for compatibility with older versions of talloc
465 void *talloc_init(const char *fmt, ...)
470 ptr = _talloc(NULL, 0);
471 if (ptr == NULL) return NULL;
474 talloc_set_name_v(ptr, fmt, ap);
481 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
482 should probably not be used in new code. It's in here to keep the talloc
483 code consistent across Samba 3 and 4.
485 void talloc_free_children(void *ptr)
487 struct talloc_chunk *tc;
493 tc = talloc_chunk_from_ptr(ptr);
496 /* we need to work out who will own an abandoned child
497 if it cannot be freed. In priority order, the first
498 choice is owner of any remaining reference to this
499 pointer, the second choice is our parent, and the
500 final choice is the null context. */
501 void *child = tc->child+1;
502 const void *new_parent = null_context;
503 if (tc->child->refs) {
504 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
505 if (p) new_parent = p+1;
507 if (talloc_free(child) == -1) {
508 if (new_parent == null_context) {
509 struct talloc_chunk *p = talloc_parent_chunk(ptr);
510 if (p) new_parent = p+1;
512 talloc_steal(new_parent, child);
518 free a talloc pointer. This also frees all child pointers of this
521 return 0 if the memory is actually freed, otherwise -1. The memory
522 will not be freed if the ref_count is > 1 or the destructor (if
523 any) returns non-zero
525 int talloc_free(void *ptr)
527 struct talloc_chunk *tc;
533 tc = talloc_chunk_from_ptr(ptr);
536 talloc_reference_destructor(tc->refs);
540 if (tc->destructor) {
541 talloc_destructor_t d = tc->destructor;
542 if (d == (talloc_destructor_t)-1) {
545 tc->destructor = (talloc_destructor_t)-1;
550 tc->destructor = NULL;
553 talloc_free_children(ptr);
556 _TLIST_REMOVE(tc->parent->child, tc);
557 if (tc->parent->child) {
558 tc->parent->child->parent = tc->parent;
561 if (tc->prev) tc->prev->next = tc->next;
562 if (tc->next) tc->next->prev = tc->prev;
565 tc->u.magic = TALLOC_MAGIC_FREE;
574 A talloc version of realloc. The context argument is only used if
577 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
579 struct talloc_chunk *tc;
582 /* size zero is equivalent to free() */
588 if (size >= MAX_TALLOC_SIZE) {
592 /* realloc(NULL) is equavalent to malloc() */
594 return talloc_named_const(context, size, name);
597 tc = talloc_chunk_from_ptr(ptr);
599 /* don't allow realloc on referenced pointers */
604 /* by resetting magic we catch users of the old memory */
605 tc->u.magic = TALLOC_MAGIC_FREE;
608 new_ptr = malloc(size + sizeof(*tc));
610 memcpy(new_ptr, tc, tc->size + sizeof(*tc));
614 new_ptr = realloc(tc, size + sizeof(*tc));
617 tc->u.magic = TALLOC_MAGIC;
622 tc->u.magic = TALLOC_MAGIC;
624 tc->parent->child = new_ptr;
627 tc->child->parent = new_ptr;
638 talloc_set_name_const(tc+1, name);
640 return (void *)(tc+1);
644 move a lump of memory from one talloc context to another return the
645 ptr on success, or NULL if it could not be transferred.
646 passing NULL as ptr will always return NULL with no side effects.
648 void *talloc_steal(const void *new_ctx, const void *ptr)
650 struct talloc_chunk *tc, *new_tc;
656 if (new_ctx == NULL) {
657 new_ctx = null_context;
660 tc = talloc_chunk_from_ptr(ptr);
662 if (new_ctx == NULL) {
664 _TLIST_REMOVE(tc->parent->child, tc);
665 if (tc->parent->child) {
666 tc->parent->child->parent = tc->parent;
669 if (tc->prev) tc->prev->next = tc->next;
670 if (tc->next) tc->next->prev = tc->prev;
673 tc->parent = tc->next = tc->prev = NULL;
674 return discard_const_p(void, ptr);
677 new_tc = talloc_chunk_from_ptr(new_ctx);
680 return discard_const_p(void, ptr);
684 _TLIST_REMOVE(tc->parent->child, tc);
685 if (tc->parent->child) {
686 tc->parent->child->parent = tc->parent;
689 if (tc->prev) tc->prev->next = tc->next;
690 if (tc->next) tc->next->prev = tc->prev;
694 if (new_tc->child) new_tc->child->parent = NULL;
695 _TLIST_ADD(new_tc->child, tc);
697 return discard_const_p(void, ptr);
701 return the total size of a talloc pool (subtree)
703 off_t talloc_total_size(const void *ptr)
706 struct talloc_chunk *c, *tc;
715 tc = talloc_chunk_from_ptr(ptr);
718 for (c=tc->child;c;c=c->next) {
719 total += talloc_total_size(c+1);
725 return the total number of blocks in a talloc pool (subtree)
727 off_t talloc_total_blocks(const void *ptr)
730 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
733 for (c=tc->child;c;c=c->next) {
734 total += talloc_total_blocks(c+1);
740 return the number of external references to a pointer
742 static int talloc_reference_count(const void *ptr)
744 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
745 struct talloc_reference_handle *h;
748 for (h=tc->refs;h;h=h->next) {
755 report on memory usage by all children of a pointer, giving a full tree view
757 void talloc_report_depth(const void *ptr, FILE *f, int depth)
759 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
761 for (c=tc->child;c;c=c->next) {
762 if (c->name == TALLOC_MAGIC_REFERENCE) {
763 struct talloc_reference_handle *handle = (void *)(c+1);
764 const char *name2 = talloc_get_name(handle->ptr);
765 fprintf(f, "%*sreference to: %s\n", depth*4, "", name2);
767 const char *name = talloc_get_name(c+1);
768 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d)\n",
771 (unsigned long)talloc_total_size(c+1),
772 (unsigned long)talloc_total_blocks(c+1),
773 talloc_reference_count(c+1));
774 talloc_report_depth(c+1, f, depth+1);
781 report on memory usage by all children of a pointer, giving a full tree view
783 void talloc_report_full(const void *ptr, FILE *f)
788 if (ptr == NULL) return;
790 fprintf(f,"full talloc report on '%s' (total %lu bytes in %lu blocks)\n",
791 talloc_get_name(ptr),
792 (unsigned long)talloc_total_size(ptr),
793 (unsigned long)talloc_total_blocks(ptr));
795 talloc_report_depth(ptr, f, 1);
800 report on memory usage by all children of a pointer
802 void talloc_report(const void *ptr, FILE *f)
804 struct talloc_chunk *c, *tc;
809 if (ptr == NULL) return;
811 fprintf(f,"talloc report on '%s' (total %lu bytes in %lu blocks)\n",
812 talloc_get_name(ptr),
813 (unsigned long)talloc_total_size(ptr),
814 (unsigned long)talloc_total_blocks(ptr));
816 tc = talloc_chunk_from_ptr(ptr);
818 for (c=tc->child;c;c=c->next) {
819 fprintf(f, "\t%-30s contains %6lu bytes in %3lu blocks\n",
820 talloc_get_name(c+1),
821 (unsigned long)talloc_total_size(c+1),
822 (unsigned long)talloc_total_blocks(c+1));
828 report on any memory hanging off the null context
830 static void talloc_report_null(void)
832 if (talloc_total_size(null_context) != 0) {
833 talloc_report(null_context, stderr);
838 report on any memory hanging off the null context
840 static void talloc_report_null_full(void)
842 if (talloc_total_size(null_context) != 0) {
843 talloc_report_full(null_context, stderr);
848 enable tracking of the NULL context
850 void talloc_enable_null_tracking(void)
852 if (null_context == NULL) {
853 null_context = talloc_named_const(NULL, 0, "null_context");
858 enable leak reporting on exit
860 void talloc_enable_leak_report(void)
862 talloc_enable_null_tracking();
863 atexit(talloc_report_null);
867 enable full leak reporting on exit
869 void talloc_enable_leak_report_full(void)
871 talloc_enable_null_tracking();
872 atexit(talloc_report_null_full);
876 talloc and zero memory.
878 void *_talloc_zero(const void *ctx, size_t size, const char *name)
880 void *p = talloc_named_const(ctx, size, name);
883 memset(p, '\0', size);
891 memdup with a talloc.
893 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
895 void *newp = talloc_named_const(t, size, name);
898 memcpy(newp, p, size);
907 char *talloc_strdup(const void *t, const char *p)
913 ret = talloc_memdup(t, p, strlen(p) + 1);
915 talloc_set_name_const(ret, ret);
921 strndup with a talloc
923 char *talloc_strndup(const void *t, const char *p, size_t n)
928 for (len=0; len<n && p[len]; len++) ;
930 ret = _talloc(t, len + 1);
931 if (!ret) { return NULL; }
934 talloc_set_name_const(ret, ret);
940 #define VA_COPY(dest, src) va_copy(dest, src)
941 #elif defined(HAVE___VA_COPY)
942 #define VA_COPY(dest, src) __va_copy(dest, src)
944 #define VA_COPY(dest, src) (dest) = (src)
948 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
956 len = vsnprintf(NULL, 0, fmt, ap2);
958 ret = _talloc(t, len+1);
961 vsnprintf(ret, len+1, fmt, ap2);
962 talloc_set_name_const(ret, ret);
970 Perform string formatting, and return a pointer to newly allocated
971 memory holding the result, inside a memory pool.
973 char *talloc_asprintf(const void *t, const char *fmt, ...)
979 ret = talloc_vasprintf(t, fmt, ap);
986 * Realloc @p s to append the formatted result of @p fmt and @p ap,
987 * and return @p s, which may have moved. Good for gradually
988 * accumulating output into a string buffer.
991 static char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
993 static char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
995 struct talloc_chunk *tc;
1000 return talloc_vasprintf(NULL, fmt, ap);
1003 tc = talloc_chunk_from_ptr(s);
1007 s_len = tc->size - 1;
1008 len = vsnprintf(NULL, 0, fmt, ap2);
1010 s = talloc_realloc(NULL, s, char, s_len + len+1);
1011 if (!s) return NULL;
1015 vsnprintf(s+s_len, len+1, fmt, ap2);
1016 talloc_set_name_const(s, s);
1022 Realloc @p s to append the formatted result of @p fmt and return @p
1023 s, which may have moved. Good for gradually accumulating output
1024 into a string buffer.
1026 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1031 s = talloc_vasprintf_append(s, fmt, ap);
1037 alloc an array, checking for integer overflow in the array size
1039 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1041 if (count >= MAX_TALLOC_SIZE/el_size) {
1044 return talloc_named_const(ctx, el_size * count, name);
1048 alloc an zero array, checking for integer overflow in the array size
1050 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1052 if (count >= MAX_TALLOC_SIZE/el_size) {
1055 return _talloc_zero(ctx, el_size * count, name);
1060 realloc an array, checking for integer overflow in the array size
1062 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1064 if (count >= MAX_TALLOC_SIZE/el_size) {
1067 return _talloc_realloc(ctx, ptr, el_size * count, name);
1071 a function version of talloc_realloc(), so it can be passed as a function pointer
1072 to libraries that want a realloc function (a realloc function encapsulates
1073 all the basic capabilities of an allocation library, which is why this is useful)
1075 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1077 return _talloc_realloc(context, ptr, size, NULL);
1081 static void talloc_autofree(void)
1083 talloc_free(cleanup_context);
1084 cleanup_context = NULL;
1088 return a context which will be auto-freed on exit
1089 this is useful for reducing the noise in leak reports
1091 void *talloc_autofree_context(void)
1093 if (cleanup_context == NULL) {
1094 cleanup_context = talloc_named_const(NULL, 0, "autofree_context");
1095 atexit(talloc_autofree);
1097 return cleanup_context;
1100 size_t talloc_get_size(const void *context)
1102 struct talloc_chunk *tc;
1104 if (context == NULL)
1107 tc = talloc_chunk_from_ptr(context);
1113 find a parent of this context that has the given name, if any
1115 void *talloc_find_parent_byname(const void *context, const char *name)
1117 struct talloc_chunk *tc;
1119 if (context == NULL) {
1123 tc = talloc_chunk_from_ptr(context);
1127 while (tc->parent && (!tc->name || strcmp(tc->name, name))) {
1133 return (void *)(tc+1);