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
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 2 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, write to the Free Software
27 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 inspired by http://swapped.cc/halloc/
38 /* use this to force every realloc to change the pointer, to stress test
39 code that might not cope */
40 #define ALWAYS_REALLOC 0
43 #define MAX_TALLOC_SIZE 0x10000000
44 #define TALLOC_MAGIC 0xe814ec70
45 #define TALLOC_FLAG_FREE 0x01
46 #define TALLOC_FLAG_LOOP 0x02
47 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
49 /* by default we abort when given a bad pointer (such as when talloc_free() is called
50 on a pointer that came from malloc() */
52 #define TALLOC_ABORT(reason) abort()
55 #ifndef discard_const_p
56 #if defined(INTPTR_MIN)
57 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
59 # define discard_const_p(type, ptr) ((type *)(ptr))
63 /* these macros gain us a few percent of speed on gcc */
64 #if HAVE_BUILTIN_EXPECT
65 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
66 as its first argument */
67 #define likely(x) __builtin_expect(!!(x), 1)
68 #define unlikely(x) __builtin_expect(!!(x), 0)
74 /* this null_context is only used if talloc_enable_leak_report() or
75 talloc_enable_leak_report_full() is called, otherwise it remains
78 static void *null_context;
79 static void *autofree_context;
81 struct talloc_reference_handle {
82 struct talloc_reference_handle *next, *prev;
86 typedef int (*talloc_destructor_t)(void *);
89 struct talloc_chunk *next, *prev;
90 struct talloc_chunk *parent, *child;
91 struct talloc_reference_handle *refs;
92 talloc_destructor_t destructor;
98 /* 16 byte alignment seems to keep everyone happy */
99 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
100 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
102 /* panic if we get a bad magic value */
103 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
105 const char *pp = (const char *)ptr;
106 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
107 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
108 if (tc->flags & TALLOC_FLAG_FREE) {
109 TALLOC_ABORT("Bad talloc magic value - double free");
111 TALLOC_ABORT("Bad talloc magic value - unknown value");
117 /* hook into the front of the list */
118 #define _TLIST_ADD(list, p) \
122 (p)->next = (p)->prev = NULL; \
124 (list)->prev = (p); \
125 (p)->next = (list); \
131 /* remove an element from a list - element doesn't have to be in list. */
132 #define _TLIST_REMOVE(list, p) \
134 if ((p) == (list)) { \
135 (list) = (p)->next; \
136 if (list) (list)->prev = NULL; \
138 if ((p)->prev) (p)->prev->next = (p)->next; \
139 if ((p)->next) (p)->next->prev = (p)->prev; \
141 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
146 return the parent chunk of a pointer
148 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
150 struct talloc_chunk *tc;
152 if (unlikely(ptr == NULL)) {
156 tc = talloc_chunk_from_ptr(ptr);
157 while (tc->prev) tc=tc->prev;
162 void *talloc_parent(const void *ptr)
164 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
165 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
171 const char *talloc_parent_name(const void *ptr)
173 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
174 return tc? tc->name : NULL;
178 Allocate a bit of memory as a child of an existing pointer
180 static inline void *__talloc(const void *context, size_t size)
182 struct talloc_chunk *tc;
184 if (unlikely(context == NULL)) {
185 context = null_context;
188 if (unlikely(size >= MAX_TALLOC_SIZE)) {
192 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
193 if (unlikely(tc == NULL)) return NULL;
196 tc->flags = TALLOC_MAGIC;
197 tc->destructor = NULL;
202 if (likely(context)) {
203 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
206 parent->child->parent = NULL;
207 tc->next = parent->child;
216 tc->next = tc->prev = tc->parent = NULL;
219 return TC_PTR_FROM_CHUNK(tc);
223 setup a destructor to be called on free of a pointer
224 the destructor should return 0 on success, or -1 on failure.
225 if the destructor fails then the free is failed, and the memory can
226 be continued to be used
228 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
230 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
231 tc->destructor = destructor;
235 increase the reference count on a piece of memory.
237 int talloc_increase_ref_count(const void *ptr)
239 if (unlikely(!talloc_reference(null_context, ptr))) {
246 helper for talloc_reference()
248 this is referenced by a function pointer and should not be inline
250 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
252 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
253 _TLIST_REMOVE(ptr_tc->refs, handle);
258 more efficient way to add a name to a pointer - the name must point to a
261 static inline void _talloc_set_name_const(const void *ptr, const char *name)
263 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
268 internal talloc_named_const()
270 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
274 ptr = __talloc(context, size);
275 if (unlikely(ptr == NULL)) {
279 _talloc_set_name_const(ptr, name);
285 make a secondary reference to a pointer, hanging off the given context.
286 the pointer remains valid until both the original caller and this given
289 the major use for this is when two different structures need to reference the
290 same underlying data, and you want to be able to free the two instances separately,
293 void *_talloc_reference(const void *context, const void *ptr)
295 struct talloc_chunk *tc;
296 struct talloc_reference_handle *handle;
297 if (unlikely(ptr == NULL)) return NULL;
299 tc = talloc_chunk_from_ptr(ptr);
300 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
301 sizeof(struct talloc_reference_handle),
302 TALLOC_MAGIC_REFERENCE);
303 if (unlikely(handle == NULL)) return NULL;
305 /* note that we hang the destructor off the handle, not the
306 main context as that allows the caller to still setup their
307 own destructor on the context if they want to */
308 talloc_set_destructor(handle, talloc_reference_destructor);
309 handle->ptr = discard_const_p(void, ptr);
310 _TLIST_ADD(tc->refs, handle);
316 internal talloc_free call
318 static inline int _talloc_free(void *ptr)
320 struct talloc_chunk *tc;
322 if (unlikely(ptr == NULL)) {
326 tc = talloc_chunk_from_ptr(ptr);
328 if (unlikely(tc->refs)) {
330 /* check this is a reference from a child or grantchild
331 * back to it's parent or grantparent
333 * in that case we need to remove the reference and
334 * call another instance of talloc_free() on the current
337 is_child = talloc_is_parent(tc->refs, ptr);
338 _talloc_free(tc->refs);
340 return _talloc_free(ptr);
345 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
346 /* we have a free loop - stop looping */
350 if (unlikely(tc->destructor)) {
351 talloc_destructor_t d = tc->destructor;
352 if (d == (talloc_destructor_t)-1) {
355 tc->destructor = (talloc_destructor_t)-1;
360 tc->destructor = NULL;
364 _TLIST_REMOVE(tc->parent->child, tc);
365 if (tc->parent->child) {
366 tc->parent->child->parent = tc->parent;
369 if (tc->prev) tc->prev->next = tc->next;
370 if (tc->next) tc->next->prev = tc->prev;
373 tc->flags |= TALLOC_FLAG_LOOP;
376 /* we need to work out who will own an abandoned child
377 if it cannot be freed. In priority order, the first
378 choice is owner of any remaining reference to this
379 pointer, the second choice is our parent, and the
380 final choice is the null context. */
381 void *child = TC_PTR_FROM_CHUNK(tc->child);
382 const void *new_parent = null_context;
383 if (unlikely(tc->child->refs)) {
384 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
385 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
387 if (unlikely(_talloc_free(child) == -1)) {
388 if (new_parent == null_context) {
389 struct talloc_chunk *p = talloc_parent_chunk(ptr);
390 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
392 talloc_steal(new_parent, child);
396 tc->flags |= TALLOC_FLAG_FREE;
402 move a lump of memory from one talloc context to another return the
403 ptr on success, or NULL if it could not be transferred.
404 passing NULL as ptr will always return NULL with no side effects.
406 void *_talloc_steal(const void *new_ctx, const void *ptr)
408 struct talloc_chunk *tc, *new_tc;
410 if (unlikely(!ptr)) {
414 if (unlikely(new_ctx == NULL)) {
415 new_ctx = null_context;
418 tc = talloc_chunk_from_ptr(ptr);
420 if (unlikely(new_ctx == NULL)) {
422 _TLIST_REMOVE(tc->parent->child, tc);
423 if (tc->parent->child) {
424 tc->parent->child->parent = tc->parent;
427 if (tc->prev) tc->prev->next = tc->next;
428 if (tc->next) tc->next->prev = tc->prev;
431 tc->parent = tc->next = tc->prev = NULL;
432 return discard_const_p(void, ptr);
435 new_tc = talloc_chunk_from_ptr(new_ctx);
437 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
438 return discard_const_p(void, ptr);
442 _TLIST_REMOVE(tc->parent->child, tc);
443 if (tc->parent->child) {
444 tc->parent->child->parent = tc->parent;
447 if (tc->prev) tc->prev->next = tc->next;
448 if (tc->next) tc->next->prev = tc->prev;
452 if (new_tc->child) new_tc->child->parent = NULL;
453 _TLIST_ADD(new_tc->child, tc);
455 return discard_const_p(void, ptr);
461 remove a secondary reference to a pointer. This undo's what
462 talloc_reference() has done. The context and pointer arguments
463 must match those given to a talloc_reference()
465 static inline int talloc_unreference(const void *context, const void *ptr)
467 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
468 struct talloc_reference_handle *h;
470 if (unlikely(context == NULL)) {
471 context = null_context;
474 for (h=tc->refs;h;h=h->next) {
475 struct talloc_chunk *p = talloc_parent_chunk(h);
477 if (context == NULL) break;
478 } else if (TC_PTR_FROM_CHUNK(p) == context) {
486 return _talloc_free(h);
490 remove a specific parent context from a pointer. This is a more
491 controlled varient of talloc_free()
493 int talloc_unlink(const void *context, void *ptr)
495 struct talloc_chunk *tc_p, *new_p;
502 if (context == NULL) {
503 context = null_context;
506 if (talloc_unreference(context, ptr) == 0) {
510 if (context == NULL) {
511 if (talloc_parent_chunk(ptr) != NULL) {
515 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
520 tc_p = talloc_chunk_from_ptr(ptr);
522 if (tc_p->refs == NULL) {
523 return _talloc_free(ptr);
526 new_p = talloc_parent_chunk(tc_p->refs);
528 new_parent = TC_PTR_FROM_CHUNK(new_p);
533 if (talloc_unreference(new_parent, ptr) != 0) {
537 talloc_steal(new_parent, ptr);
543 add a name to an existing pointer - va_list version
545 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
547 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
549 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
550 tc->name = talloc_vasprintf(ptr, fmt, ap);
551 if (likely(tc->name)) {
552 _talloc_set_name_const(tc->name, ".name");
558 add a name to an existing pointer
560 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
565 name = talloc_set_name_v(ptr, fmt, ap);
572 create a named talloc pointer. Any talloc pointer can be named, and
573 talloc_named() operates just like talloc() except that it allows you
576 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
582 ptr = __talloc(context, size);
583 if (unlikely(ptr == NULL)) return NULL;
586 name = talloc_set_name_v(ptr, fmt, ap);
589 if (unlikely(name == NULL)) {
598 return the name of a talloc ptr, or "UNNAMED"
600 const char *talloc_get_name(const void *ptr)
602 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
603 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
606 if (likely(tc->name)) {
614 check if a pointer has the given name. If it does, return the pointer,
615 otherwise return NULL
617 void *talloc_check_name(const void *ptr, const char *name)
620 if (unlikely(ptr == NULL)) return NULL;
621 pname = talloc_get_name(ptr);
622 if (likely(pname == name || strcmp(pname, name) == 0)) {
623 return discard_const_p(void, ptr);
630 this is for compatibility with older versions of talloc
632 void *talloc_init(const char *fmt, ...)
639 * samba3 expects talloc_report_depth_cb(NULL, ...)
640 * reports all talloc'ed memory, so we need to enable
643 talloc_enable_null_tracking();
645 ptr = __talloc(NULL, 0);
646 if (unlikely(ptr == NULL)) return NULL;
649 name = talloc_set_name_v(ptr, fmt, ap);
652 if (unlikely(name == NULL)) {
661 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
662 should probably not be used in new code. It's in here to keep the talloc
663 code consistent across Samba 3 and 4.
665 void talloc_free_children(void *ptr)
667 struct talloc_chunk *tc;
669 if (unlikely(ptr == NULL)) {
673 tc = talloc_chunk_from_ptr(ptr);
676 /* we need to work out who will own an abandoned child
677 if it cannot be freed. In priority order, the first
678 choice is owner of any remaining reference to this
679 pointer, the second choice is our parent, and the
680 final choice is the null context. */
681 void *child = TC_PTR_FROM_CHUNK(tc->child);
682 const void *new_parent = null_context;
683 if (unlikely(tc->child->refs)) {
684 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
685 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
687 if (unlikely(_talloc_free(child) == -1)) {
688 if (new_parent == null_context) {
689 struct talloc_chunk *p = talloc_parent_chunk(ptr);
690 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
692 talloc_steal(new_parent, child);
698 Allocate a bit of memory as a child of an existing pointer
700 void *_talloc(const void *context, size_t size)
702 return __talloc(context, size);
706 externally callable talloc_set_name_const()
708 void talloc_set_name_const(const void *ptr, const char *name)
710 _talloc_set_name_const(ptr, name);
714 create a named talloc pointer. Any talloc pointer can be named, and
715 talloc_named() operates just like talloc() except that it allows you
718 void *talloc_named_const(const void *context, size_t size, const char *name)
720 return _talloc_named_const(context, size, name);
724 free a talloc pointer. This also frees all child pointers of this
727 return 0 if the memory is actually freed, otherwise -1. The memory
728 will not be freed if the ref_count is > 1 or the destructor (if
729 any) returns non-zero
731 int talloc_free(void *ptr)
733 int saved_errno = errno, ret;
734 ret = _talloc_free(ptr);
743 A talloc version of realloc. The context argument is only used if
746 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
748 struct talloc_chunk *tc;
751 /* size zero is equivalent to free() */
752 if (unlikely(size == 0)) {
757 if (unlikely(size >= MAX_TALLOC_SIZE)) {
761 /* realloc(NULL) is equivalent to malloc() */
763 return _talloc_named_const(context, size, name);
766 tc = talloc_chunk_from_ptr(ptr);
768 /* don't allow realloc on referenced pointers */
769 if (unlikely(tc->refs)) {
773 /* by resetting magic we catch users of the old memory */
774 tc->flags |= TALLOC_FLAG_FREE;
777 new_ptr = malloc(size + TC_HDR_SIZE);
779 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
783 new_ptr = realloc(tc, size + TC_HDR_SIZE);
785 if (unlikely(!new_ptr)) {
786 tc->flags &= ~TALLOC_FLAG_FREE;
790 tc = (struct talloc_chunk *)new_ptr;
791 tc->flags &= ~TALLOC_FLAG_FREE;
793 tc->parent->child = tc;
796 tc->child->parent = tc;
807 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
809 return TC_PTR_FROM_CHUNK(tc);
813 a wrapper around talloc_steal() for situations where you are moving a pointer
814 between two structures, and want the old pointer to be set to NULL
816 void *_talloc_move(const void *new_ctx, const void *_pptr)
818 const void **pptr = discard_const_p(const void *,_pptr);
819 void *ret = _talloc_steal(new_ctx, *pptr);
825 return the total size of a talloc pool (subtree)
827 size_t talloc_total_size(const void *ptr)
830 struct talloc_chunk *c, *tc;
839 tc = talloc_chunk_from_ptr(ptr);
841 if (tc->flags & TALLOC_FLAG_LOOP) {
845 tc->flags |= TALLOC_FLAG_LOOP;
848 for (c=tc->child;c;c=c->next) {
849 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
852 tc->flags &= ~TALLOC_FLAG_LOOP;
858 return the total number of blocks in a talloc pool (subtree)
860 size_t talloc_total_blocks(const void *ptr)
863 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
865 if (tc->flags & TALLOC_FLAG_LOOP) {
869 tc->flags |= TALLOC_FLAG_LOOP;
872 for (c=tc->child;c;c=c->next) {
873 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
876 tc->flags &= ~TALLOC_FLAG_LOOP;
882 return the number of external references to a pointer
884 size_t talloc_reference_count(const void *ptr)
886 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
887 struct talloc_reference_handle *h;
890 for (h=tc->refs;h;h=h->next) {
897 report on memory usage by all children of a pointer, giving a full tree view
899 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
900 void (*callback)(const void *ptr,
901 int depth, int max_depth,
906 struct talloc_chunk *c, *tc;
911 if (ptr == NULL) return;
913 tc = talloc_chunk_from_ptr(ptr);
915 if (tc->flags & TALLOC_FLAG_LOOP) {
919 callback(ptr, depth, max_depth, 0, private_data);
921 if (max_depth >= 0 && depth >= max_depth) {
925 tc->flags |= TALLOC_FLAG_LOOP;
926 for (c=tc->child;c;c=c->next) {
927 if (c->name == TALLOC_MAGIC_REFERENCE) {
928 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
929 callback(h->ptr, depth + 1, max_depth, 1, private_data);
931 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
934 tc->flags &= ~TALLOC_FLAG_LOOP;
937 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
939 const char *name = talloc_get_name(ptr);
940 FILE *f = (FILE *)_f;
943 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
948 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
949 (max_depth < 0 ? "full " :""), name,
950 (unsigned long)talloc_total_size(ptr),
951 (unsigned long)talloc_total_blocks(ptr));
955 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
958 (unsigned long)talloc_total_size(ptr),
959 (unsigned long)talloc_total_blocks(ptr),
960 (int)talloc_reference_count(ptr), ptr);
963 fprintf(f, "content: ");
964 if (talloc_total_size(ptr)) {
965 int tot = talloc_total_size(ptr);
968 for (i = 0; i < tot; i++) {
969 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
970 fprintf(f, "%c", ((char *)ptr)[i]);
972 fprintf(f, "~%02x", ((char *)ptr)[i]);
981 report on memory usage by all children of a pointer, giving a full tree view
983 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
985 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
990 report on memory usage by all children of a pointer, giving a full tree view
992 void talloc_report_full(const void *ptr, FILE *f)
994 talloc_report_depth_file(ptr, 0, -1, f);
998 report on memory usage by all children of a pointer
1000 void talloc_report(const void *ptr, FILE *f)
1002 talloc_report_depth_file(ptr, 0, 1, f);
1006 report on any memory hanging off the null context
1008 static void talloc_report_null(void)
1010 if (talloc_total_size(null_context) != 0) {
1011 talloc_report(null_context, stderr);
1016 report on any memory hanging off the null context
1018 static void talloc_report_null_full(void)
1020 if (talloc_total_size(null_context) != 0) {
1021 talloc_report_full(null_context, stderr);
1026 enable tracking of the NULL context
1028 void talloc_enable_null_tracking(void)
1030 if (null_context == NULL) {
1031 null_context = _talloc_named_const(NULL, 0, "null_context");
1036 disable tracking of the NULL context
1038 void talloc_disable_null_tracking(void)
1040 _talloc_free(null_context);
1041 null_context = NULL;
1045 enable leak reporting on exit
1047 void talloc_enable_leak_report(void)
1049 talloc_enable_null_tracking();
1050 atexit(talloc_report_null);
1054 enable full leak reporting on exit
1056 void talloc_enable_leak_report_full(void)
1058 talloc_enable_null_tracking();
1059 atexit(talloc_report_null_full);
1063 talloc and zero memory.
1065 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1067 void *p = _talloc_named_const(ctx, size, name);
1070 memset(p, '\0', size);
1077 memdup with a talloc.
1079 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1081 void *newp = _talloc_named_const(t, size, name);
1084 memcpy(newp, p, size);
1091 strdup with a talloc
1093 char *talloc_strdup(const void *t, const char *p)
1099 ret = (char *)talloc_memdup(t, p, strlen(p) + 1);
1101 _talloc_set_name_const(ret, ret);
1107 append to a talloced string
1109 char *talloc_append_string(char *orig, const char *append)
1112 size_t olen = strlen(orig);
1118 alenz = strlen(append) + 1;
1120 ret = talloc_realloc(NULL, orig, char, olen + alenz);
1124 /* append the string with the trailing \0 */
1125 memcpy(&ret[olen], append, alenz);
1127 _talloc_set_name_const(ret, ret);
1133 strndup with a talloc
1135 char *talloc_strndup(const void *t, const char *p, size_t n)
1140 for (len=0; len<n && p[len]; len++) ;
1142 ret = (char *)__talloc(t, len + 1);
1143 if (!ret) { return NULL; }
1144 memcpy(ret, p, len);
1146 _talloc_set_name_const(ret, ret);
1150 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1157 /* this call looks strange, but it makes it work on older solaris boxes */
1159 len = vsnprintf(&c, 1, fmt, ap2);
1165 ret = (char *)__talloc(t, len+1);
1168 vsnprintf(ret, len+1, fmt, ap2);
1170 _talloc_set_name_const(ret, ret);
1178 Perform string formatting, and return a pointer to newly allocated
1179 memory holding the result, inside a memory pool.
1181 char *talloc_asprintf(const void *t, const char *fmt, ...)
1187 ret = talloc_vasprintf(t, fmt, ap);
1194 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1195 * and return @p s, which may have moved. Good for gradually
1196 * accumulating output into a string buffer.
1198 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1200 struct talloc_chunk *tc;
1206 return talloc_vasprintf(NULL, fmt, ap);
1209 tc = talloc_chunk_from_ptr(s);
1211 s_len = tc->size - 1;
1214 len = vsnprintf(&c, 1, fmt, ap2);
1218 /* Either the vsnprintf failed or the format resulted in
1219 * no characters being formatted. In the former case, we
1220 * ought to return NULL, in the latter we ought to return
1221 * the original string. Most current callers of this
1222 * function expect it to never return NULL.
1227 s = talloc_realloc(NULL, s, char, s_len + len+1);
1228 if (!s) return NULL;
1231 vsnprintf(s+s_len, len+1, fmt, ap2);
1233 _talloc_set_name_const(s, s);
1239 Realloc @p s to append the formatted result of @p fmt and return @p
1240 s, which may have moved. Good for gradually accumulating output
1241 into a string buffer.
1243 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1248 s = talloc_vasprintf_append(s, fmt, ap);
1254 alloc an array, checking for integer overflow in the array size
1256 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1258 if (count >= MAX_TALLOC_SIZE/el_size) {
1261 return _talloc_named_const(ctx, el_size * count, name);
1265 alloc an zero array, checking for integer overflow in the array size
1267 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1269 if (count >= MAX_TALLOC_SIZE/el_size) {
1272 return _talloc_zero(ctx, el_size * count, name);
1276 realloc an array, checking for integer overflow in the array size
1278 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1280 if (count >= MAX_TALLOC_SIZE/el_size) {
1283 return _talloc_realloc(ctx, ptr, el_size * count, name);
1287 a function version of talloc_realloc(), so it can be passed as a function pointer
1288 to libraries that want a realloc function (a realloc function encapsulates
1289 all the basic capabilities of an allocation library, which is why this is useful)
1291 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1293 return _talloc_realloc(context, ptr, size, NULL);
1297 static int talloc_autofree_destructor(void *ptr)
1299 autofree_context = NULL;
1303 static void talloc_autofree(void)
1305 _talloc_free(autofree_context);
1309 return a context which will be auto-freed on exit
1310 this is useful for reducing the noise in leak reports
1312 void *talloc_autofree_context(void)
1314 if (autofree_context == NULL) {
1315 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1316 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1317 atexit(talloc_autofree);
1319 return autofree_context;
1322 size_t talloc_get_size(const void *context)
1324 struct talloc_chunk *tc;
1326 if (context == NULL)
1329 tc = talloc_chunk_from_ptr(context);
1335 find a parent of this context that has the given name, if any
1337 void *talloc_find_parent_byname(const void *context, const char *name)
1339 struct talloc_chunk *tc;
1341 if (context == NULL) {
1345 tc = talloc_chunk_from_ptr(context);
1347 if (tc->name && strcmp(tc->name, name) == 0) {
1348 return TC_PTR_FROM_CHUNK(tc);
1350 while (tc && tc->prev) tc = tc->prev;
1359 show the parentage of a context
1361 void talloc_show_parents(const void *context, FILE *file)
1363 struct talloc_chunk *tc;
1365 if (context == NULL) {
1366 fprintf(file, "talloc no parents for NULL\n");
1370 tc = talloc_chunk_from_ptr(context);
1371 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1373 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1374 while (tc && tc->prev) tc = tc->prev;
1383 return 1 if ptr is a parent of context
1385 int talloc_is_parent(const void *context, const void *ptr)
1387 struct talloc_chunk *tc;
1389 if (context == NULL) {
1393 tc = talloc_chunk_from_ptr(context);
1395 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1396 while (tc && tc->prev) tc = tc->prev;