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/
36 #if (SAMBA_VERSION_MAJOR<4)
38 /* This is to circumvent SAMBA3's paranoid malloc checker. Here in this file
39 * we trust ourselves... */
46 #define _TALLOC_SAMBA3
47 #endif /* (SAMBA_VERSION_MAJOR<4) */
48 #endif /* _SAMBA_BUILD_ */
50 #ifndef _TALLOC_SAMBA3
61 #if defined(HAVE_STDARG_H)
63 #elif defined (HAVE_VARARGS_H)
66 #error "no var arg header"
70 #endif /* not _TALLOC_SAMBA3 */
72 /* use this to force every realloc to change the pointer, to stress test
73 code that might not cope */
74 #define ALWAYS_REALLOC 0
77 #define MAX_TALLOC_SIZE 0x10000000
78 #define TALLOC_MAGIC 0xe814ec70
79 #define TALLOC_FLAG_FREE 0x01
80 #define TALLOC_FLAG_LOOP 0x02
81 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
83 /* by default we abort when given a bad pointer (such as when talloc_free() is called
84 on a pointer that came from malloc() */
86 #define TALLOC_ABORT(reason) abort()
89 #ifndef discard_const_p
90 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
91 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
93 # define discard_const_p(type, ptr) ((type *)(ptr))
97 /* this null_context is only used if talloc_enable_leak_report() or
98 talloc_enable_leak_report_full() is called, otherwise it remains
101 static void *null_context;
102 static void *cleanup_context;
104 struct talloc_reference_handle {
105 struct talloc_reference_handle *next, *prev;
109 typedef int (*talloc_destructor_t)(void *);
111 struct talloc_chunk {
112 struct talloc_chunk *next, *prev;
113 struct talloc_chunk *parent, *child;
114 struct talloc_reference_handle *refs;
115 talloc_destructor_t destructor;
121 /* 16 byte alignment seems to keep everyone happy */
122 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
123 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
125 /* panic if we get a bad magic value */
126 static struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
128 const char *pp = (const char *)ptr;
129 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
130 if ((tc->flags & ~0xF) != TALLOC_MAGIC) {
131 TALLOC_ABORT("Bad talloc magic value - unknown value");
133 if (tc->flags & TALLOC_FLAG_FREE) {
134 TALLOC_ABORT("Bad talloc magic value - double free");
139 /* hook into the front of the list */
140 #define _TLIST_ADD(list, p) \
144 (p)->next = (p)->prev = NULL; \
146 (list)->prev = (p); \
147 (p)->next = (list); \
153 /* remove an element from a list - element doesn't have to be in list. */
154 #define _TLIST_REMOVE(list, p) \
156 if ((p) == (list)) { \
157 (list) = (p)->next; \
158 if (list) (list)->prev = NULL; \
160 if ((p)->prev) (p)->prev->next = (p)->next; \
161 if ((p)->next) (p)->next->prev = (p)->prev; \
163 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
168 return the parent chunk of a pointer
170 static struct talloc_chunk *talloc_parent_chunk(const void *ptr)
172 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
173 while (tc->prev) tc=tc->prev;
177 void *talloc_parent(const void *ptr)
179 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
180 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
184 Allocate a bit of memory as a child of an existing pointer
186 void *_talloc(const void *context, size_t size)
188 struct talloc_chunk *tc;
190 if (context == NULL) {
191 context = null_context;
194 if (size >= MAX_TALLOC_SIZE) {
198 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
199 if (tc == NULL) return NULL;
202 tc->flags = TALLOC_MAGIC;
203 tc->destructor = NULL;
209 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
214 parent->child->parent = NULL;
217 _TLIST_ADD(parent->child, tc);
219 tc->next = tc->prev = tc->parent = NULL;
222 return TC_PTR_FROM_CHUNK(tc);
227 setup a destructor to be called on free of a pointer
228 the destructor should return 0 on success, or -1 on failure.
229 if the destructor fails then the free is failed, and the memory can
230 be continued to be used
232 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
234 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
235 tc->destructor = destructor;
239 increase the reference count on a piece of memory.
241 int talloc_increase_ref_count(const void *ptr)
243 if (!talloc_reference(null_context, ptr)) {
250 helper for talloc_reference()
252 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
254 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
255 _TLIST_REMOVE(ptr_tc->refs, handle);
260 make a secondary reference to a pointer, hanging off the given context.
261 the pointer remains valid until both the original caller and this given
264 the major use for this is when two different structures need to reference the
265 same underlying data, and you want to be able to free the two instances separately,
268 void *_talloc_reference(const void *context, const void *ptr)
270 struct talloc_chunk *tc;
271 struct talloc_reference_handle *handle;
272 if (ptr == NULL) return NULL;
274 tc = talloc_chunk_from_ptr(ptr);
275 handle = (struct talloc_reference_handle *)talloc_named_const(context,
276 sizeof(struct talloc_reference_handle),
277 TALLOC_MAGIC_REFERENCE);
278 if (handle == NULL) return NULL;
280 /* note that we hang the destructor off the handle, not the
281 main context as that allows the caller to still setup their
282 own destructor on the context if they want to */
283 talloc_set_destructor(handle, talloc_reference_destructor);
284 handle->ptr = discard_const_p(void, ptr);
285 _TLIST_ADD(tc->refs, handle);
290 remove a secondary reference to a pointer. This undo's what
291 talloc_reference() has done. The context and pointer arguments
292 must match those given to a talloc_reference()
294 static int talloc_unreference(const void *context, const void *ptr)
296 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
297 struct talloc_reference_handle *h;
299 if (context == NULL) {
300 context = null_context;
303 for (h=tc->refs;h;h=h->next) {
304 struct talloc_chunk *p = talloc_parent_chunk(h);
306 if (context == NULL) break;
307 } else if (TC_PTR_FROM_CHUNK(p) == context) {
315 return talloc_free(h);
319 remove a specific parent context from a pointer. This is a more
320 controlled varient of talloc_free()
322 int talloc_unlink(const void *context, void *ptr)
324 struct talloc_chunk *tc_p, *new_p;
331 if (context == NULL) {
332 context = null_context;
335 if (talloc_unreference(context, ptr) == 0) {
339 if (context == NULL) {
340 if (talloc_parent_chunk(ptr) != NULL) {
344 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
349 tc_p = talloc_chunk_from_ptr(ptr);
351 if (tc_p->refs == NULL) {
352 return talloc_free(ptr);
355 new_p = talloc_parent_chunk(tc_p->refs);
357 new_parent = TC_PTR_FROM_CHUNK(new_p);
362 if (talloc_unreference(new_parent, ptr) != 0) {
366 talloc_steal(new_parent, ptr);
372 add a name to an existing pointer - va_list version
374 static const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
376 static const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
378 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
379 tc->name = talloc_vasprintf(ptr, fmt, ap);
381 talloc_set_name_const(tc->name, ".name");
387 add a name to an existing pointer
389 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
394 name = talloc_set_name_v(ptr, fmt, ap);
400 more efficient way to add a name to a pointer - the name must point to a
403 void talloc_set_name_const(const void *ptr, const char *name)
405 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
410 create a named talloc pointer. Any talloc pointer can be named, and
411 talloc_named() operates just like talloc() except that it allows you
414 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
420 ptr = _talloc(context, size);
421 if (ptr == NULL) return NULL;
424 name = talloc_set_name_v(ptr, fmt, ap);
436 create a named talloc pointer. Any talloc pointer can be named, and
437 talloc_named() operates just like talloc() except that it allows you
440 void *talloc_named_const(const void *context, size_t size, const char *name)
444 ptr = _talloc(context, size);
449 talloc_set_name_const(ptr, name);
455 return the name of a talloc ptr, or "UNNAMED"
457 const char *talloc_get_name(const void *ptr)
459 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
460 if (tc->name == TALLOC_MAGIC_REFERENCE) {
471 check if a pointer has the given name. If it does, return the pointer,
472 otherwise return NULL
474 void *talloc_check_name(const void *ptr, const char *name)
477 if (ptr == NULL) return NULL;
478 pname = talloc_get_name(ptr);
479 if (pname == name || strcmp(pname, name) == 0) {
480 return discard_const_p(void, ptr);
487 this is for compatibility with older versions of talloc
489 void *talloc_init(const char *fmt, ...)
496 * samba3 expects talloc_report_depth_cb(NULL, ...)
497 * reports all talloc'ed memory, so we need to enable
500 talloc_enable_null_tracking();
502 ptr = _talloc(NULL, 0);
503 if (ptr == NULL) return NULL;
506 name = talloc_set_name_v(ptr, fmt, ap);
518 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
519 should probably not be used in new code. It's in here to keep the talloc
520 code consistent across Samba 3 and 4.
522 void talloc_free_children(void *ptr)
524 struct talloc_chunk *tc;
530 tc = talloc_chunk_from_ptr(ptr);
533 /* we need to work out who will own an abandoned child
534 if it cannot be freed. In priority order, the first
535 choice is owner of any remaining reference to this
536 pointer, the second choice is our parent, and the
537 final choice is the null context. */
538 void *child = TC_PTR_FROM_CHUNK(tc->child);
539 const void *new_parent = null_context;
540 if (tc->child->refs) {
541 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
542 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
544 if (talloc_free(child) == -1) {
545 if (new_parent == null_context) {
546 struct talloc_chunk *p = talloc_parent_chunk(ptr);
547 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
549 talloc_steal(new_parent, child);
555 free a talloc pointer. This also frees all child pointers of this
558 return 0 if the memory is actually freed, otherwise -1. The memory
559 will not be freed if the ref_count is > 1 or the destructor (if
560 any) returns non-zero
562 int talloc_free(void *ptr)
564 struct talloc_chunk *tc;
571 tc = talloc_chunk_from_ptr(ptr);
575 /* check this is a reference from a child or grantchild
576 * back to it's parent or grantparent
578 * in that case we need to remove the reference and
579 * call another instance of talloc_free() on the current
582 is_child = talloc_is_parent(tc->refs, ptr);
583 talloc_free(tc->refs);
585 return talloc_free(ptr);
590 if (tc->flags & TALLOC_FLAG_LOOP) {
591 /* we have a free loop - stop looping */
595 if (tc->destructor) {
596 talloc_destructor_t d = tc->destructor;
597 if (d == (talloc_destructor_t)-1) {
600 tc->destructor = (talloc_destructor_t)-1;
605 tc->destructor = NULL;
609 _TLIST_REMOVE(tc->parent->child, tc);
610 if (tc->parent->child) {
611 tc->parent->child->parent = tc->parent;
614 if (tc->prev) tc->prev->next = tc->next;
615 if (tc->next) tc->next->prev = tc->prev;
618 tc->flags |= TALLOC_FLAG_LOOP;
619 talloc_free_children(ptr);
621 tc->flags |= TALLOC_FLAG_FREE;
631 A talloc version of realloc. The context argument is only used if
634 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
636 struct talloc_chunk *tc;
639 /* size zero is equivalent to free() */
645 if (size >= MAX_TALLOC_SIZE) {
649 /* realloc(NULL) is equavalent to malloc() */
651 return talloc_named_const(context, size, name);
654 tc = talloc_chunk_from_ptr(ptr);
656 /* don't allow realloc on referenced pointers */
661 /* by resetting magic we catch users of the old memory */
662 tc->flags |= TALLOC_FLAG_FREE;
665 new_ptr = malloc(size + TC_HDR_SIZE);
667 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
671 new_ptr = realloc(tc, size + TC_HDR_SIZE);
674 tc->flags &= ~TALLOC_FLAG_FREE;
678 tc = (struct talloc_chunk *)new_ptr;
679 tc->flags &= ~TALLOC_FLAG_FREE;
681 tc->parent->child = tc;
684 tc->child->parent = tc;
695 talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
697 return TC_PTR_FROM_CHUNK(tc);
701 move a lump of memory from one talloc context to another return the
702 ptr on success, or NULL if it could not be transferred.
703 passing NULL as ptr will always return NULL with no side effects.
705 void *_talloc_steal(const void *new_ctx, const void *ptr)
707 struct talloc_chunk *tc, *new_tc;
713 if (new_ctx == NULL) {
714 new_ctx = null_context;
717 tc = talloc_chunk_from_ptr(ptr);
719 if (new_ctx == NULL) {
721 _TLIST_REMOVE(tc->parent->child, tc);
722 if (tc->parent->child) {
723 tc->parent->child->parent = tc->parent;
726 if (tc->prev) tc->prev->next = tc->next;
727 if (tc->next) tc->next->prev = tc->prev;
730 tc->parent = tc->next = tc->prev = NULL;
731 return discard_const_p(void, ptr);
734 new_tc = talloc_chunk_from_ptr(new_ctx);
736 if (tc == new_tc || tc->parent == new_tc) {
737 return discard_const_p(void, ptr);
741 _TLIST_REMOVE(tc->parent->child, tc);
742 if (tc->parent->child) {
743 tc->parent->child->parent = tc->parent;
746 if (tc->prev) tc->prev->next = tc->next;
747 if (tc->next) tc->next->prev = tc->prev;
751 if (new_tc->child) new_tc->child->parent = NULL;
752 _TLIST_ADD(new_tc->child, tc);
754 return discard_const_p(void, ptr);
758 return the total size of a talloc pool (subtree)
760 size_t talloc_total_size(const void *ptr)
763 struct talloc_chunk *c, *tc;
772 tc = talloc_chunk_from_ptr(ptr);
774 if (tc->flags & TALLOC_FLAG_LOOP) {
778 tc->flags |= TALLOC_FLAG_LOOP;
781 for (c=tc->child;c;c=c->next) {
782 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
785 tc->flags &= ~TALLOC_FLAG_LOOP;
791 return the total number of blocks in a talloc pool (subtree)
793 size_t talloc_total_blocks(const void *ptr)
796 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
798 if (tc->flags & TALLOC_FLAG_LOOP) {
802 tc->flags |= TALLOC_FLAG_LOOP;
805 for (c=tc->child;c;c=c->next) {
806 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
809 tc->flags &= ~TALLOC_FLAG_LOOP;
815 return the number of external references to a pointer
817 size_t talloc_reference_count(const void *ptr)
819 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
820 struct talloc_reference_handle *h;
823 for (h=tc->refs;h;h=h->next) {
830 report on memory usage by all children of a pointer, giving a full tree view
832 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
833 void (*callback)(const void *ptr,
834 int depth, int max_depth,
839 struct talloc_chunk *c, *tc;
844 if (ptr == NULL) return;
846 tc = talloc_chunk_from_ptr(ptr);
848 if (tc->flags & TALLOC_FLAG_LOOP) {
852 callback(ptr, depth, max_depth, 0, private_data);
854 if (max_depth >= 0 && depth >= max_depth) {
858 tc->flags |= TALLOC_FLAG_LOOP;
859 for (c=tc->child;c;c=c->next) {
860 if (c->name == TALLOC_MAGIC_REFERENCE) {
861 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
862 callback(h->ptr, depth + 1, max_depth, 1, private_data);
864 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
867 tc->flags &= ~TALLOC_FLAG_LOOP;
870 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
872 const char *name = talloc_get_name(ptr);
873 FILE *f = (FILE *)_f;
876 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
881 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
882 (max_depth < 0 ? "full " :""), name,
883 (unsigned long)talloc_total_size(ptr),
884 (unsigned long)talloc_total_blocks(ptr));
888 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d)\n",
891 (unsigned long)talloc_total_size(ptr),
892 (unsigned long)talloc_total_blocks(ptr),
893 (int)talloc_reference_count(ptr));
897 report on memory usage by all children of a pointer, giving a full tree view
899 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
901 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
906 report on memory usage by all children of a pointer, giving a full tree view
908 void talloc_report_full(const void *ptr, FILE *f)
910 talloc_report_depth_file(ptr, 0, -1, f);
914 report on memory usage by all children of a pointer
916 void talloc_report(const void *ptr, FILE *f)
918 talloc_report_depth_file(ptr, 0, 1, f);
922 report on any memory hanging off the null context
924 static void talloc_report_null(void)
926 if (talloc_total_size(null_context) != 0) {
927 talloc_report(null_context, stderr);
932 report on any memory hanging off the null context
934 static void talloc_report_null_full(void)
936 if (talloc_total_size(null_context) != 0) {
937 talloc_report_full(null_context, stderr);
942 enable tracking of the NULL context
944 void talloc_enable_null_tracking(void)
946 if (null_context == NULL) {
947 null_context = talloc_named_const(NULL, 0, "null_context");
952 disable tracking of the NULL context
954 void talloc_disable_null_tracking(void)
956 talloc_free(null_context);
961 enable leak reporting on exit
963 void talloc_enable_leak_report(void)
965 talloc_enable_null_tracking();
966 atexit(talloc_report_null);
970 enable full leak reporting on exit
972 void talloc_enable_leak_report_full(void)
974 talloc_enable_null_tracking();
975 atexit(talloc_report_null_full);
979 talloc and zero memory.
981 void *_talloc_zero(const void *ctx, size_t size, const char *name)
983 void *p = talloc_named_const(ctx, size, name);
986 memset(p, '\0', size);
994 memdup with a talloc.
996 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
998 void *newp = talloc_named_const(t, size, name);
1001 memcpy(newp, p, size);
1008 strdup with a talloc
1010 char *talloc_strdup(const void *t, const char *p)
1016 ret = (char *)talloc_memdup(t, p, strlen(p) + 1);
1018 talloc_set_name_const(ret, ret);
1024 append to a talloced string
1026 char *talloc_append_string(const void *t, char *orig, const char *append)
1029 size_t olen = strlen(orig);
1035 alenz = strlen(append) + 1;
1037 ret = talloc_realloc(t, orig, char, olen + alenz);
1041 /* append the string with the trailing \0 */
1042 memcpy(&ret[olen], append, alenz);
1048 strndup with a talloc
1050 char *talloc_strndup(const void *t, const char *p, size_t n)
1055 for (len=0; len<n && p[len]; len++) ;
1057 ret = (char *)_talloc(t, len + 1);
1058 if (!ret) { return NULL; }
1059 memcpy(ret, p, len);
1061 talloc_set_name_const(ret, ret);
1065 #ifndef HAVE_VA_COPY
1066 #ifdef HAVE___VA_COPY
1067 #define va_copy(dest, src) __va_copy(dest, src)
1069 #define va_copy(dest, src) (dest) = (src)
1073 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1082 /* this call looks strange, but it makes it work on older solaris boxes */
1083 if ((len = vsnprintf(&c, 1, fmt, ap2)) < 0) {
1087 ret = (char *)_talloc(t, len+1);
1090 vsnprintf(ret, len+1, fmt, ap2);
1091 talloc_set_name_const(ret, ret);
1099 Perform string formatting, and return a pointer to newly allocated
1100 memory holding the result, inside a memory pool.
1102 char *talloc_asprintf(const void *t, const char *fmt, ...)
1108 ret = talloc_vasprintf(t, fmt, ap);
1115 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1116 * and return @p s, which may have moved. Good for gradually
1117 * accumulating output into a string buffer.
1119 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1121 struct talloc_chunk *tc;
1127 return talloc_vasprintf(NULL, fmt, ap);
1130 tc = talloc_chunk_from_ptr(s);
1134 s_len = tc->size - 1;
1135 if ((len = vsnprintf(&c, 1, fmt, ap2)) <= 0) {
1136 /* Either the vsnprintf failed or the format resulted in
1137 * no characters being formatted. In the former case, we
1138 * ought to return NULL, in the latter we ought to return
1139 * the original string. Most current callers of this
1140 * function expect it to never return NULL.
1145 s = talloc_realloc(NULL, s, char, s_len + len+1);
1146 if (!s) return NULL;
1150 vsnprintf(s+s_len, len+1, fmt, ap2);
1151 talloc_set_name_const(s, s);
1157 Realloc @p s to append the formatted result of @p fmt and return @p
1158 s, which may have moved. Good for gradually accumulating output
1159 into a string buffer.
1161 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1166 s = talloc_vasprintf_append(s, fmt, ap);
1172 alloc an array, checking for integer overflow in the array size
1174 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1176 if (count >= MAX_TALLOC_SIZE/el_size) {
1179 return talloc_named_const(ctx, el_size * count, name);
1183 alloc an zero array, checking for integer overflow in the array size
1185 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1187 if (count >= MAX_TALLOC_SIZE/el_size) {
1190 return _talloc_zero(ctx, el_size * count, name);
1195 realloc an array, checking for integer overflow in the array size
1197 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1199 if (count >= MAX_TALLOC_SIZE/el_size) {
1202 return _talloc_realloc(ctx, ptr, el_size * count, name);
1206 a function version of talloc_realloc(), so it can be passed as a function pointer
1207 to libraries that want a realloc function (a realloc function encapsulates
1208 all the basic capabilities of an allocation library, which is why this is useful)
1210 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1212 return _talloc_realloc(context, ptr, size, NULL);
1216 static void talloc_autofree(void)
1218 talloc_free(cleanup_context);
1219 cleanup_context = NULL;
1223 return a context which will be auto-freed on exit
1224 this is useful for reducing the noise in leak reports
1226 void *talloc_autofree_context(void)
1228 if (cleanup_context == NULL) {
1229 cleanup_context = talloc_named_const(NULL, 0, "autofree_context");
1230 atexit(talloc_autofree);
1232 return cleanup_context;
1235 size_t talloc_get_size(const void *context)
1237 struct talloc_chunk *tc;
1239 if (context == NULL)
1242 tc = talloc_chunk_from_ptr(context);
1248 find a parent of this context that has the given name, if any
1250 void *talloc_find_parent_byname(const void *context, const char *name)
1252 struct talloc_chunk *tc;
1254 if (context == NULL) {
1258 tc = talloc_chunk_from_ptr(context);
1260 if (tc->name && strcmp(tc->name, name) == 0) {
1261 return TC_PTR_FROM_CHUNK(tc);
1263 while (tc && tc->prev) tc = tc->prev;
1272 show the parentage of a context
1274 void talloc_show_parents(const void *context, FILE *file)
1276 struct talloc_chunk *tc;
1278 if (context == NULL) {
1279 fprintf(file, "talloc no parents for NULL\n");
1283 tc = talloc_chunk_from_ptr(context);
1284 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1286 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1287 while (tc && tc->prev) tc = tc->prev;
1295 return 1 if ptr is a parent of context
1297 int talloc_is_parent(const void *context, const void *ptr)
1299 struct talloc_chunk *tc;
1301 if (context == NULL) {
1305 tc = talloc_chunk_from_ptr(context);
1307 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1308 while (tc && tc->prev) tc = tc->prev;