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 return _talloc_free(ptr);
739 A talloc version of realloc. The context argument is only used if
742 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
744 struct talloc_chunk *tc;
747 /* size zero is equivalent to free() */
748 if (unlikely(size == 0)) {
753 if (unlikely(size >= MAX_TALLOC_SIZE)) {
757 /* realloc(NULL) is equivalent to malloc() */
759 return _talloc_named_const(context, size, name);
762 tc = talloc_chunk_from_ptr(ptr);
764 /* don't allow realloc on referenced pointers */
765 if (unlikely(tc->refs)) {
769 /* by resetting magic we catch users of the old memory */
770 tc->flags |= TALLOC_FLAG_FREE;
773 new_ptr = malloc(size + TC_HDR_SIZE);
775 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
779 new_ptr = realloc(tc, size + TC_HDR_SIZE);
781 if (unlikely(!new_ptr)) {
782 tc->flags &= ~TALLOC_FLAG_FREE;
786 tc = (struct talloc_chunk *)new_ptr;
787 tc->flags &= ~TALLOC_FLAG_FREE;
789 tc->parent->child = tc;
792 tc->child->parent = tc;
803 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
805 return TC_PTR_FROM_CHUNK(tc);
809 a wrapper around talloc_steal() for situations where you are moving a pointer
810 between two structures, and want the old pointer to be set to NULL
812 void *_talloc_move(const void *new_ctx, const void *_pptr)
814 const void **pptr = discard_const_p(const void *,_pptr);
815 void *ret = _talloc_steal(new_ctx, *pptr);
821 return the total size of a talloc pool (subtree)
823 size_t talloc_total_size(const void *ptr)
826 struct talloc_chunk *c, *tc;
835 tc = talloc_chunk_from_ptr(ptr);
837 if (tc->flags & TALLOC_FLAG_LOOP) {
841 tc->flags |= TALLOC_FLAG_LOOP;
844 for (c=tc->child;c;c=c->next) {
845 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
848 tc->flags &= ~TALLOC_FLAG_LOOP;
854 return the total number of blocks in a talloc pool (subtree)
856 size_t talloc_total_blocks(const void *ptr)
859 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
861 if (tc->flags & TALLOC_FLAG_LOOP) {
865 tc->flags |= TALLOC_FLAG_LOOP;
868 for (c=tc->child;c;c=c->next) {
869 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
872 tc->flags &= ~TALLOC_FLAG_LOOP;
878 return the number of external references to a pointer
880 size_t talloc_reference_count(const void *ptr)
882 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
883 struct talloc_reference_handle *h;
886 for (h=tc->refs;h;h=h->next) {
893 report on memory usage by all children of a pointer, giving a full tree view
895 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
896 void (*callback)(const void *ptr,
897 int depth, int max_depth,
902 struct talloc_chunk *c, *tc;
907 if (ptr == NULL) return;
909 tc = talloc_chunk_from_ptr(ptr);
911 if (tc->flags & TALLOC_FLAG_LOOP) {
915 callback(ptr, depth, max_depth, 0, private_data);
917 if (max_depth >= 0 && depth >= max_depth) {
921 tc->flags |= TALLOC_FLAG_LOOP;
922 for (c=tc->child;c;c=c->next) {
923 if (c->name == TALLOC_MAGIC_REFERENCE) {
924 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
925 callback(h->ptr, depth + 1, max_depth, 1, private_data);
927 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
930 tc->flags &= ~TALLOC_FLAG_LOOP;
933 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
935 const char *name = talloc_get_name(ptr);
936 FILE *f = (FILE *)_f;
939 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
944 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
945 (max_depth < 0 ? "full " :""), name,
946 (unsigned long)talloc_total_size(ptr),
947 (unsigned long)talloc_total_blocks(ptr));
951 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
954 (unsigned long)talloc_total_size(ptr),
955 (unsigned long)talloc_total_blocks(ptr),
956 (int)talloc_reference_count(ptr), ptr);
959 fprintf(f, "content: ");
960 if (talloc_total_size(ptr)) {
961 int tot = talloc_total_size(ptr);
964 for (i = 0; i < tot; i++) {
965 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
966 fprintf(f, "%c", ((char *)ptr)[i]);
968 fprintf(f, "~%02x", ((char *)ptr)[i]);
977 report on memory usage by all children of a pointer, giving a full tree view
979 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
981 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
986 report on memory usage by all children of a pointer, giving a full tree view
988 void talloc_report_full(const void *ptr, FILE *f)
990 talloc_report_depth_file(ptr, 0, -1, f);
994 report on memory usage by all children of a pointer
996 void talloc_report(const void *ptr, FILE *f)
998 talloc_report_depth_file(ptr, 0, 1, f);
1002 report on any memory hanging off the null context
1004 static void talloc_report_null(void)
1006 if (talloc_total_size(null_context) != 0) {
1007 talloc_report(null_context, stderr);
1012 report on any memory hanging off the null context
1014 static void talloc_report_null_full(void)
1016 if (talloc_total_size(null_context) != 0) {
1017 talloc_report_full(null_context, stderr);
1022 enable tracking of the NULL context
1024 void talloc_enable_null_tracking(void)
1026 if (null_context == NULL) {
1027 null_context = _talloc_named_const(NULL, 0, "null_context");
1032 disable tracking of the NULL context
1034 void talloc_disable_null_tracking(void)
1036 _talloc_free(null_context);
1037 null_context = NULL;
1041 enable leak reporting on exit
1043 void talloc_enable_leak_report(void)
1045 talloc_enable_null_tracking();
1046 atexit(talloc_report_null);
1050 enable full leak reporting on exit
1052 void talloc_enable_leak_report_full(void)
1054 talloc_enable_null_tracking();
1055 atexit(talloc_report_null_full);
1059 talloc and zero memory.
1061 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1063 void *p = _talloc_named_const(ctx, size, name);
1066 memset(p, '\0', size);
1073 memdup with a talloc.
1075 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1077 void *newp = _talloc_named_const(t, size, name);
1080 memcpy(newp, p, size);
1087 strdup with a talloc
1089 char *talloc_strdup(const void *t, const char *p)
1095 ret = (char *)talloc_memdup(t, p, strlen(p) + 1);
1097 _talloc_set_name_const(ret, ret);
1103 append to a talloced string
1105 char *talloc_append_string(const void *t, char *orig, const char *append)
1108 size_t olen = strlen(orig);
1114 alenz = strlen(append) + 1;
1116 ret = talloc_realloc(t, orig, char, olen + alenz);
1120 /* append the string with the trailing \0 */
1121 memcpy(&ret[olen], append, alenz);
1123 _talloc_set_name_const(ret, ret);
1129 strndup with a talloc
1131 char *talloc_strndup(const void *t, const char *p, size_t n)
1136 for (len=0; len<n && p[len]; len++) ;
1138 ret = (char *)__talloc(t, len + 1);
1139 if (!ret) { return NULL; }
1140 memcpy(ret, p, len);
1142 _talloc_set_name_const(ret, ret);
1146 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1153 /* this call looks strange, but it makes it work on older solaris boxes */
1155 len = vsnprintf(&c, 1, fmt, ap2);
1161 ret = (char *)__talloc(t, len+1);
1164 vsnprintf(ret, len+1, fmt, ap2);
1166 _talloc_set_name_const(ret, ret);
1174 Perform string formatting, and return a pointer to newly allocated
1175 memory holding the result, inside a memory pool.
1177 char *talloc_asprintf(const void *t, const char *fmt, ...)
1183 ret = talloc_vasprintf(t, fmt, ap);
1190 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1191 * and return @p s, which may have moved. Good for gradually
1192 * accumulating output into a string buffer.
1194 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1196 struct talloc_chunk *tc;
1202 return talloc_vasprintf(NULL, fmt, ap);
1205 tc = talloc_chunk_from_ptr(s);
1207 s_len = tc->size - 1;
1210 len = vsnprintf(&c, 1, fmt, ap2);
1214 /* Either the vsnprintf failed or the format resulted in
1215 * no characters being formatted. In the former case, we
1216 * ought to return NULL, in the latter we ought to return
1217 * the original string. Most current callers of this
1218 * function expect it to never return NULL.
1223 s = talloc_realloc(NULL, s, char, s_len + len+1);
1224 if (!s) return NULL;
1227 vsnprintf(s+s_len, len+1, fmt, ap2);
1229 _talloc_set_name_const(s, s);
1235 Realloc @p s to append the formatted result of @p fmt and return @p
1236 s, which may have moved. Good for gradually accumulating output
1237 into a string buffer.
1239 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1244 s = talloc_vasprintf_append(s, fmt, ap);
1250 alloc an array, checking for integer overflow in the array size
1252 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1254 if (count >= MAX_TALLOC_SIZE/el_size) {
1257 return _talloc_named_const(ctx, el_size * count, name);
1261 alloc an zero array, checking for integer overflow in the array size
1263 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1265 if (count >= MAX_TALLOC_SIZE/el_size) {
1268 return _talloc_zero(ctx, el_size * count, name);
1272 realloc an array, checking for integer overflow in the array size
1274 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1276 if (count >= MAX_TALLOC_SIZE/el_size) {
1279 return _talloc_realloc(ctx, ptr, el_size * count, name);
1283 a function version of talloc_realloc(), so it can be passed as a function pointer
1284 to libraries that want a realloc function (a realloc function encapsulates
1285 all the basic capabilities of an allocation library, which is why this is useful)
1287 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1289 return _talloc_realloc(context, ptr, size, NULL);
1293 static int talloc_autofree_destructor(void *ptr)
1295 autofree_context = NULL;
1299 static void talloc_autofree(void)
1301 _talloc_free(autofree_context);
1305 return a context which will be auto-freed on exit
1306 this is useful for reducing the noise in leak reports
1308 void *talloc_autofree_context(void)
1310 if (autofree_context == NULL) {
1311 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1312 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1313 atexit(talloc_autofree);
1315 return autofree_context;
1318 size_t talloc_get_size(const void *context)
1320 struct talloc_chunk *tc;
1322 if (context == NULL)
1325 tc = talloc_chunk_from_ptr(context);
1331 find a parent of this context that has the given name, if any
1333 void *talloc_find_parent_byname(const void *context, const char *name)
1335 struct talloc_chunk *tc;
1337 if (context == NULL) {
1341 tc = talloc_chunk_from_ptr(context);
1343 if (tc->name && strcmp(tc->name, name) == 0) {
1344 return TC_PTR_FROM_CHUNK(tc);
1346 while (tc && tc->prev) tc = tc->prev;
1355 show the parentage of a context
1357 void talloc_show_parents(const void *context, FILE *file)
1359 struct talloc_chunk *tc;
1361 if (context == NULL) {
1362 fprintf(file, "talloc no parents for NULL\n");
1366 tc = talloc_chunk_from_ptr(context);
1367 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1369 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1370 while (tc && tc->prev) tc = tc->prev;
1379 return 1 if ptr is a parent of context
1381 int talloc_is_parent(const void *context, const void *ptr)
1383 struct talloc_chunk *tc;
1385 if (context == NULL) {
1389 tc = talloc_chunk_from_ptr(context);
1391 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1392 while (tc && tc->prev) tc = tc->prev;