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/
39 /* use this to force every realloc to change the pointer, to stress test
40 code that might not cope */
41 #define ALWAYS_REALLOC 0
44 #define MAX_TALLOC_SIZE 0x10000000
45 #define TALLOC_MAGIC 0xe814ec70
46 #define TALLOC_FLAG_FREE 0x01
47 #define TALLOC_FLAG_LOOP 0x02
48 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
50 /* by default we abort when given a bad pointer (such as when talloc_free() is called
51 on a pointer that came from malloc() */
53 #define TALLOC_ABORT(reason) abort()
56 #ifndef discard_const_p
57 #if defined(INTPTR_MIN)
58 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
60 # define discard_const_p(type, ptr) ((type *)(ptr))
64 /* these macros gain us a few percent of speed on gcc */
65 #if HAVE_BUILTIN_EXPECT
66 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
67 as its first argument */
68 #define likely(x) __builtin_expect(!!(x), 1)
69 #define unlikely(x) __builtin_expect(!!(x), 0)
75 /* this null_context is only used if talloc_enable_leak_report() or
76 talloc_enable_leak_report_full() is called, otherwise it remains
79 static void *null_context;
80 static void *autofree_context;
82 struct talloc_reference_handle {
83 struct talloc_reference_handle *next, *prev;
87 typedef int (*talloc_destructor_t)(void *);
90 struct talloc_chunk *next, *prev;
91 struct talloc_chunk *parent, *child;
92 struct talloc_reference_handle *refs;
93 talloc_destructor_t destructor;
99 /* 16 byte alignment seems to keep everyone happy */
100 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
101 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
103 /* panic if we get a bad magic value */
104 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
106 const char *pp = (const char *)ptr;
107 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
108 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
109 if (tc->flags & TALLOC_FLAG_FREE) {
110 TALLOC_ABORT("Bad talloc magic value - double free");
112 TALLOC_ABORT("Bad talloc magic value - unknown value");
118 /* hook into the front of the list */
119 #define _TLIST_ADD(list, p) \
123 (p)->next = (p)->prev = NULL; \
125 (list)->prev = (p); \
126 (p)->next = (list); \
132 /* remove an element from a list - element doesn't have to be in list. */
133 #define _TLIST_REMOVE(list, p) \
135 if ((p) == (list)) { \
136 (list) = (p)->next; \
137 if (list) (list)->prev = NULL; \
139 if ((p)->prev) (p)->prev->next = (p)->next; \
140 if ((p)->next) (p)->next->prev = (p)->prev; \
142 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
147 return the parent chunk of a pointer
149 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
151 struct talloc_chunk *tc;
153 if (unlikely(ptr == NULL)) {
157 tc = talloc_chunk_from_ptr(ptr);
158 while (tc->prev) tc=tc->prev;
163 void *talloc_parent(const void *ptr)
165 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
166 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
172 const char *talloc_parent_name(const void *ptr)
174 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
175 return tc? tc->name : NULL;
179 Allocate a bit of memory as a child of an existing pointer
181 static inline void *__talloc(const void *context, size_t size)
183 struct talloc_chunk *tc;
185 if (unlikely(context == NULL)) {
186 context = null_context;
189 if (unlikely(size >= MAX_TALLOC_SIZE)) {
193 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
194 if (unlikely(tc == NULL)) return NULL;
197 tc->flags = TALLOC_MAGIC;
198 tc->destructor = NULL;
203 if (likely(context)) {
204 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
207 parent->child->parent = NULL;
208 tc->next = parent->child;
217 tc->next = tc->prev = tc->parent = NULL;
220 return TC_PTR_FROM_CHUNK(tc);
224 setup a destructor to be called on free of a pointer
225 the destructor should return 0 on success, or -1 on failure.
226 if the destructor fails then the free is failed, and the memory can
227 be continued to be used
229 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
231 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
232 tc->destructor = destructor;
236 increase the reference count on a piece of memory.
238 int talloc_increase_ref_count(const void *ptr)
240 if (unlikely(!talloc_reference(null_context, ptr))) {
247 helper for talloc_reference()
249 this is referenced by a function pointer and should not be inline
251 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
253 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
254 _TLIST_REMOVE(ptr_tc->refs, handle);
259 more efficient way to add a name to a pointer - the name must point to a
262 static inline void _talloc_set_name_const(const void *ptr, const char *name)
264 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
269 internal talloc_named_const()
271 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
275 ptr = __talloc(context, size);
276 if (unlikely(ptr == NULL)) {
280 _talloc_set_name_const(ptr, name);
286 make a secondary reference to a pointer, hanging off the given context.
287 the pointer remains valid until both the original caller and this given
290 the major use for this is when two different structures need to reference the
291 same underlying data, and you want to be able to free the two instances separately,
294 void *_talloc_reference(const void *context, const void *ptr)
296 struct talloc_chunk *tc;
297 struct talloc_reference_handle *handle;
298 if (unlikely(ptr == NULL)) return NULL;
300 tc = talloc_chunk_from_ptr(ptr);
301 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
302 sizeof(struct talloc_reference_handle),
303 TALLOC_MAGIC_REFERENCE);
304 if (unlikely(handle == NULL)) return NULL;
306 /* note that we hang the destructor off the handle, not the
307 main context as that allows the caller to still setup their
308 own destructor on the context if they want to */
309 talloc_set_destructor(handle, talloc_reference_destructor);
310 handle->ptr = discard_const_p(void, ptr);
311 _TLIST_ADD(tc->refs, handle);
317 internal talloc_free call
319 static inline int _talloc_free(void *ptr)
321 struct talloc_chunk *tc;
323 if (unlikely(ptr == NULL)) {
327 tc = talloc_chunk_from_ptr(ptr);
329 if (unlikely(tc->refs)) {
331 /* check this is a reference from a child or grantchild
332 * back to it's parent or grantparent
334 * in that case we need to remove the reference and
335 * call another instance of talloc_free() on the current
338 is_child = talloc_is_parent(tc->refs, ptr);
339 _talloc_free(tc->refs);
341 return _talloc_free(ptr);
346 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
347 /* we have a free loop - stop looping */
351 if (unlikely(tc->destructor)) {
352 talloc_destructor_t d = tc->destructor;
353 if (d == (talloc_destructor_t)-1) {
356 tc->destructor = (talloc_destructor_t)-1;
361 tc->destructor = NULL;
365 _TLIST_REMOVE(tc->parent->child, tc);
366 if (tc->parent->child) {
367 tc->parent->child->parent = tc->parent;
370 if (tc->prev) tc->prev->next = tc->next;
371 if (tc->next) tc->next->prev = tc->prev;
374 tc->flags |= TALLOC_FLAG_LOOP;
377 /* we need to work out who will own an abandoned child
378 if it cannot be freed. In priority order, the first
379 choice is owner of any remaining reference to this
380 pointer, the second choice is our parent, and the
381 final choice is the null context. */
382 void *child = TC_PTR_FROM_CHUNK(tc->child);
383 const void *new_parent = null_context;
384 if (unlikely(tc->child->refs)) {
385 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
386 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
388 if (unlikely(_talloc_free(child) == -1)) {
389 if (new_parent == null_context) {
390 struct talloc_chunk *p = talloc_parent_chunk(ptr);
391 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
393 talloc_steal(new_parent, child);
397 tc->flags |= TALLOC_FLAG_FREE;
403 move a lump of memory from one talloc context to another return the
404 ptr on success, or NULL if it could not be transferred.
405 passing NULL as ptr will always return NULL with no side effects.
407 void *_talloc_steal(const void *new_ctx, const void *ptr)
409 struct talloc_chunk *tc, *new_tc;
411 if (unlikely(!ptr)) {
415 if (unlikely(new_ctx == NULL)) {
416 new_ctx = null_context;
419 tc = talloc_chunk_from_ptr(ptr);
421 if (unlikely(new_ctx == NULL)) {
423 _TLIST_REMOVE(tc->parent->child, tc);
424 if (tc->parent->child) {
425 tc->parent->child->parent = tc->parent;
428 if (tc->prev) tc->prev->next = tc->next;
429 if (tc->next) tc->next->prev = tc->prev;
432 tc->parent = tc->next = tc->prev = NULL;
433 return discard_const_p(void, ptr);
436 new_tc = talloc_chunk_from_ptr(new_ctx);
438 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
439 return discard_const_p(void, ptr);
443 _TLIST_REMOVE(tc->parent->child, tc);
444 if (tc->parent->child) {
445 tc->parent->child->parent = tc->parent;
448 if (tc->prev) tc->prev->next = tc->next;
449 if (tc->next) tc->next->prev = tc->prev;
453 if (new_tc->child) new_tc->child->parent = NULL;
454 _TLIST_ADD(new_tc->child, tc);
456 return discard_const_p(void, ptr);
462 remove a secondary reference to a pointer. This undo's what
463 talloc_reference() has done. The context and pointer arguments
464 must match those given to a talloc_reference()
466 static inline int talloc_unreference(const void *context, const void *ptr)
468 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
469 struct talloc_reference_handle *h;
471 if (unlikely(context == NULL)) {
472 context = null_context;
475 for (h=tc->refs;h;h=h->next) {
476 struct talloc_chunk *p = talloc_parent_chunk(h);
478 if (context == NULL) break;
479 } else if (TC_PTR_FROM_CHUNK(p) == context) {
487 return _talloc_free(h);
491 remove a specific parent context from a pointer. This is a more
492 controlled varient of talloc_free()
494 int talloc_unlink(const void *context, void *ptr)
496 struct talloc_chunk *tc_p, *new_p;
503 if (context == NULL) {
504 context = null_context;
507 if (talloc_unreference(context, ptr) == 0) {
511 if (context == NULL) {
512 if (talloc_parent_chunk(ptr) != NULL) {
516 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
521 tc_p = talloc_chunk_from_ptr(ptr);
523 if (tc_p->refs == NULL) {
524 return _talloc_free(ptr);
527 new_p = talloc_parent_chunk(tc_p->refs);
529 new_parent = TC_PTR_FROM_CHUNK(new_p);
534 if (talloc_unreference(new_parent, ptr) != 0) {
538 talloc_steal(new_parent, ptr);
544 add a name to an existing pointer - va_list version
546 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
548 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
550 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
551 tc->name = talloc_vasprintf(ptr, fmt, ap);
552 if (likely(tc->name)) {
553 _talloc_set_name_const(tc->name, ".name");
559 add a name to an existing pointer
561 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
566 name = talloc_set_name_v(ptr, fmt, ap);
573 create a named talloc pointer. Any talloc pointer can be named, and
574 talloc_named() operates just like talloc() except that it allows you
577 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
583 ptr = __talloc(context, size);
584 if (unlikely(ptr == NULL)) return NULL;
587 name = talloc_set_name_v(ptr, fmt, ap);
590 if (unlikely(name == NULL)) {
599 return the name of a talloc ptr, or "UNNAMED"
601 const char *talloc_get_name(const void *ptr)
603 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
604 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
607 if (likely(tc->name)) {
615 check if a pointer has the given name. If it does, return the pointer,
616 otherwise return NULL
618 void *talloc_check_name(const void *ptr, const char *name)
621 if (unlikely(ptr == NULL)) return NULL;
622 pname = talloc_get_name(ptr);
623 if (likely(pname == name || strcmp(pname, name) == 0)) {
624 return discard_const_p(void, ptr);
631 this is for compatibility with older versions of talloc
633 void *talloc_init(const char *fmt, ...)
640 * samba3 expects talloc_report_depth_cb(NULL, ...)
641 * reports all talloc'ed memory, so we need to enable
644 talloc_enable_null_tracking();
646 ptr = __talloc(NULL, 0);
647 if (unlikely(ptr == NULL)) return NULL;
650 name = talloc_set_name_v(ptr, fmt, ap);
653 if (unlikely(name == NULL)) {
662 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
663 should probably not be used in new code. It's in here to keep the talloc
664 code consistent across Samba 3 and 4.
666 void talloc_free_children(void *ptr)
668 struct talloc_chunk *tc;
670 if (unlikely(ptr == NULL)) {
674 tc = talloc_chunk_from_ptr(ptr);
677 /* we need to work out who will own an abandoned child
678 if it cannot be freed. In priority order, the first
679 choice is owner of any remaining reference to this
680 pointer, the second choice is our parent, and the
681 final choice is the null context. */
682 void *child = TC_PTR_FROM_CHUNK(tc->child);
683 const void *new_parent = null_context;
684 if (unlikely(tc->child->refs)) {
685 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
686 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
688 if (unlikely(_talloc_free(child) == -1)) {
689 if (new_parent == null_context) {
690 struct talloc_chunk *p = talloc_parent_chunk(ptr);
691 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
693 talloc_steal(new_parent, child);
699 Allocate a bit of memory as a child of an existing pointer
701 void *_talloc(const void *context, size_t size)
703 return __talloc(context, size);
707 externally callable talloc_set_name_const()
709 void talloc_set_name_const(const void *ptr, const char *name)
711 _talloc_set_name_const(ptr, name);
715 create a named talloc pointer. Any talloc pointer can be named, and
716 talloc_named() operates just like talloc() except that it allows you
719 void *talloc_named_const(const void *context, size_t size, const char *name)
721 return _talloc_named_const(context, size, name);
725 free a talloc pointer. This also frees all child pointers of this
728 return 0 if the memory is actually freed, otherwise -1. The memory
729 will not be freed if the ref_count is > 1 or the destructor (if
730 any) returns non-zero
732 int talloc_free(void *ptr)
734 int saved_errno = errno, ret;
735 ret = _talloc_free(ptr);
744 A talloc version of realloc. The context argument is only used if
747 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
749 struct talloc_chunk *tc;
752 /* size zero is equivalent to free() */
753 if (unlikely(size == 0)) {
758 if (unlikely(size >= MAX_TALLOC_SIZE)) {
762 /* realloc(NULL) is equivalent to malloc() */
764 return _talloc_named_const(context, size, name);
767 tc = talloc_chunk_from_ptr(ptr);
769 /* don't allow realloc on referenced pointers */
770 if (unlikely(tc->refs)) {
774 /* by resetting magic we catch users of the old memory */
775 tc->flags |= TALLOC_FLAG_FREE;
778 new_ptr = malloc(size + TC_HDR_SIZE);
780 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
784 new_ptr = realloc(tc, size + TC_HDR_SIZE);
786 if (unlikely(!new_ptr)) {
787 tc->flags &= ~TALLOC_FLAG_FREE;
791 tc = (struct talloc_chunk *)new_ptr;
792 tc->flags &= ~TALLOC_FLAG_FREE;
794 tc->parent->child = tc;
797 tc->child->parent = tc;
808 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
810 return TC_PTR_FROM_CHUNK(tc);
814 a wrapper around talloc_steal() for situations where you are moving a pointer
815 between two structures, and want the old pointer to be set to NULL
817 void *_talloc_move(const void *new_ctx, const void *_pptr)
819 const void **pptr = discard_const_p(const void *,_pptr);
820 void *ret = _talloc_steal(new_ctx, *pptr);
826 return the total size of a talloc pool (subtree)
828 size_t talloc_total_size(const void *ptr)
831 struct talloc_chunk *c, *tc;
840 tc = talloc_chunk_from_ptr(ptr);
842 if (tc->flags & TALLOC_FLAG_LOOP) {
846 tc->flags |= TALLOC_FLAG_LOOP;
849 for (c=tc->child;c;c=c->next) {
850 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
853 tc->flags &= ~TALLOC_FLAG_LOOP;
859 return the total number of blocks in a talloc pool (subtree)
861 size_t talloc_total_blocks(const void *ptr)
864 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
866 if (tc->flags & TALLOC_FLAG_LOOP) {
870 tc->flags |= TALLOC_FLAG_LOOP;
873 for (c=tc->child;c;c=c->next) {
874 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
877 tc->flags &= ~TALLOC_FLAG_LOOP;
883 return the number of external references to a pointer
885 size_t talloc_reference_count(const void *ptr)
887 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
888 struct talloc_reference_handle *h;
891 for (h=tc->refs;h;h=h->next) {
898 report on memory usage by all children of a pointer, giving a full tree view
900 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
901 void (*callback)(const void *ptr,
902 int depth, int max_depth,
907 struct talloc_chunk *c, *tc;
912 if (ptr == NULL) return;
914 tc = talloc_chunk_from_ptr(ptr);
916 if (tc->flags & TALLOC_FLAG_LOOP) {
920 callback(ptr, depth, max_depth, 0, private_data);
922 if (max_depth >= 0 && depth >= max_depth) {
926 tc->flags |= TALLOC_FLAG_LOOP;
927 for (c=tc->child;c;c=c->next) {
928 if (c->name == TALLOC_MAGIC_REFERENCE) {
929 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
930 callback(h->ptr, depth + 1, max_depth, 1, private_data);
932 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
935 tc->flags &= ~TALLOC_FLAG_LOOP;
938 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
940 const char *name = talloc_get_name(ptr);
941 FILE *f = (FILE *)_f;
944 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
949 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
950 (max_depth < 0 ? "full " :""), name,
951 (unsigned long)talloc_total_size(ptr),
952 (unsigned long)talloc_total_blocks(ptr));
956 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
959 (unsigned long)talloc_total_size(ptr),
960 (unsigned long)talloc_total_blocks(ptr),
961 (int)talloc_reference_count(ptr), ptr);
964 fprintf(f, "content: ");
965 if (talloc_total_size(ptr)) {
966 int tot = talloc_total_size(ptr);
969 for (i = 0; i < tot; i++) {
970 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
971 fprintf(f, "%c", ((char *)ptr)[i]);
973 fprintf(f, "~%02x", ((char *)ptr)[i]);
982 report on memory usage by all children of a pointer, giving a full tree view
984 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
986 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
991 report on memory usage by all children of a pointer, giving a full tree view
993 void talloc_report_full(const void *ptr, FILE *f)
995 talloc_report_depth_file(ptr, 0, -1, f);
999 report on memory usage by all children of a pointer
1001 void talloc_report(const void *ptr, FILE *f)
1003 talloc_report_depth_file(ptr, 0, 1, f);
1007 report on any memory hanging off the null context
1009 static void talloc_report_null(void)
1011 if (talloc_total_size(null_context) != 0) {
1012 talloc_report(null_context, stderr);
1017 report on any memory hanging off the null context
1019 static void talloc_report_null_full(void)
1021 if (talloc_total_size(null_context) != 0) {
1022 talloc_report_full(null_context, stderr);
1027 enable tracking of the NULL context
1029 void talloc_enable_null_tracking(void)
1031 if (null_context == NULL) {
1032 null_context = _talloc_named_const(NULL, 0, "null_context");
1037 disable tracking of the NULL context
1039 void talloc_disable_null_tracking(void)
1041 _talloc_free(null_context);
1042 null_context = NULL;
1046 enable leak reporting on exit
1048 void talloc_enable_leak_report(void)
1050 talloc_enable_null_tracking();
1051 atexit(talloc_report_null);
1055 enable full leak reporting on exit
1057 void talloc_enable_leak_report_full(void)
1059 talloc_enable_null_tracking();
1060 atexit(talloc_report_null_full);
1064 talloc and zero memory.
1066 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1068 void *p = _talloc_named_const(ctx, size, name);
1071 memset(p, '\0', size);
1078 memdup with a talloc.
1080 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1082 void *newp = _talloc_named_const(t, size, name);
1085 memcpy(newp, p, size);
1092 strdup with a talloc
1094 char *talloc_strdup(const void *t, const char *p)
1100 ret = (char *)talloc_memdup(t, p, strlen(p) + 1);
1102 _talloc_set_name_const(ret, ret);
1108 append to a talloced string
1110 char *talloc_append_string(char *orig, const char *append)
1113 size_t olen = strlen(orig);
1119 alenz = strlen(append) + 1;
1121 ret = talloc_realloc(NULL, orig, char, olen + alenz);
1125 /* append the string with the trailing \0 */
1126 memcpy(&ret[olen], append, alenz);
1128 _talloc_set_name_const(ret, ret);
1134 strndup with a talloc
1136 char *talloc_strndup(const void *t, const char *p, size_t n)
1141 for (len=0; len<n && p[len]; len++) ;
1143 ret = (char *)__talloc(t, len + 1);
1144 if (!ret) { return NULL; }
1145 memcpy(ret, p, len);
1147 _talloc_set_name_const(ret, ret);
1151 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1158 /* this call looks strange, but it makes it work on older solaris boxes */
1160 len = vsnprintf(&c, 1, fmt, ap2);
1166 ret = (char *)__talloc(t, len+1);
1169 vsnprintf(ret, len+1, fmt, ap2);
1171 _talloc_set_name_const(ret, ret);
1179 Perform string formatting, and return a pointer to newly allocated
1180 memory holding the result, inside a memory pool.
1182 char *talloc_asprintf(const void *t, const char *fmt, ...)
1188 ret = talloc_vasprintf(t, fmt, ap);
1195 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1196 * and return @p s, which may have moved. Good for gradually
1197 * accumulating output into a string buffer.
1199 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1201 struct talloc_chunk *tc;
1207 return talloc_vasprintf(NULL, fmt, ap);
1210 tc = talloc_chunk_from_ptr(s);
1212 s_len = tc->size - 1;
1215 len = vsnprintf(&c, 1, fmt, ap2);
1219 /* Either the vsnprintf failed or the format resulted in
1220 * no characters being formatted. In the former case, we
1221 * ought to return NULL, in the latter we ought to return
1222 * the original string. Most current callers of this
1223 * function expect it to never return NULL.
1228 s = talloc_realloc(NULL, s, char, s_len + len+1);
1229 if (!s) return NULL;
1232 vsnprintf(s+s_len, len+1, fmt, ap2);
1234 _talloc_set_name_const(s, s);
1240 Realloc @p s to append the formatted result of @p fmt and return @p
1241 s, which may have moved. Good for gradually accumulating output
1242 into a string buffer.
1244 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1249 s = talloc_vasprintf_append(s, fmt, ap);
1255 alloc an array, checking for integer overflow in the array size
1257 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1259 if (count >= MAX_TALLOC_SIZE/el_size) {
1262 return _talloc_named_const(ctx, el_size * count, name);
1266 alloc an zero array, checking for integer overflow in the array size
1268 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1270 if (count >= MAX_TALLOC_SIZE/el_size) {
1273 return _talloc_zero(ctx, el_size * count, name);
1277 realloc an array, checking for integer overflow in the array size
1279 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1281 if (count >= MAX_TALLOC_SIZE/el_size) {
1284 return _talloc_realloc(ctx, ptr, el_size * count, name);
1288 a function version of talloc_realloc(), so it can be passed as a function pointer
1289 to libraries that want a realloc function (a realloc function encapsulates
1290 all the basic capabilities of an allocation library, which is why this is useful)
1292 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1294 return _talloc_realloc(context, ptr, size, NULL);
1298 static int talloc_autofree_destructor(void *ptr)
1300 autofree_context = NULL;
1304 static void talloc_autofree(void)
1306 _talloc_free(autofree_context);
1310 return a context which will be auto-freed on exit
1311 this is useful for reducing the noise in leak reports
1313 void *talloc_autofree_context(void)
1315 if (autofree_context == NULL) {
1316 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1317 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1318 atexit(talloc_autofree);
1320 return autofree_context;
1323 size_t talloc_get_size(const void *context)
1325 struct talloc_chunk *tc;
1327 if (context == NULL)
1330 tc = talloc_chunk_from_ptr(context);
1336 find a parent of this context that has the given name, if any
1338 void *talloc_find_parent_byname(const void *context, const char *name)
1340 struct talloc_chunk *tc;
1342 if (context == NULL) {
1346 tc = talloc_chunk_from_ptr(context);
1348 if (tc->name && strcmp(tc->name, name) == 0) {
1349 return TC_PTR_FROM_CHUNK(tc);
1351 while (tc && tc->prev) tc = tc->prev;
1360 show the parentage of a context
1362 void talloc_show_parents(const void *context, FILE *file)
1364 struct talloc_chunk *tc;
1366 if (context == NULL) {
1367 fprintf(file, "talloc no parents for NULL\n");
1371 tc = talloc_chunk_from_ptr(context);
1372 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1374 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1375 while (tc && tc->prev) tc = tc->prev;
1384 return 1 if ptr is a parent of context
1386 int talloc_is_parent(const void *context, const void *ptr)
1388 struct talloc_chunk *tc;
1390 if (context == NULL) {
1394 tc = talloc_chunk_from_ptr(context);
1396 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1397 while (tc && tc->prev) tc = tc->prev;