r17972: revert accidental commit to ads_verify_ticket()
[Samba.git] / source / lib / talloc.c
blobb7284e9317bc52be702aab1331c2931d284a5ab2
1 /*
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
13 ** under the LGPL
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/
34 #ifdef _SAMBA_BUILD_
35 #include "version.h"
36 #if (SAMBA_VERSION_MAJOR<4)
37 #include "includes.h"
38 /* This is to circumvent SAMBA3's paranoid malloc checker. Here in this file
39 * we trust ourselves... */
40 #ifdef malloc
41 #undef malloc
42 #endif
43 #ifdef realloc
44 #undef realloc
45 #endif
46 #define _TALLOC_SAMBA3
47 #endif /* (SAMBA_VERSION_MAJOR<4) */
48 #endif /* _SAMBA_BUILD_ */
50 #ifndef _TALLOC_SAMBA3
51 #include "config.h"
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <errno.h>
57 #ifdef HAVE_STDINT_H
58 #include <stdint.h>
59 #endif
61 #if defined(HAVE_STDARG_H)
62 #include <stdarg.h>
63 #elif defined (HAVE_VARARGS_H)
64 #include <varargs.h>
65 #else
66 #error "no var arg header"
67 #endif
69 #include "talloc.h"
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() */
85 #ifndef TALLOC_ABORT
86 #define TALLOC_ABORT(reason) abort()
87 #endif
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)))
92 #else
93 # define discard_const_p(type, ptr) ((type *)(ptr))
94 #endif
95 #endif
97 /* this null_context is only used if talloc_enable_leak_report() or
98 talloc_enable_leak_report_full() is called, otherwise it remains
99 NULL
101 static void *null_context;
102 static void *cleanup_context;
104 struct talloc_reference_handle {
105 struct talloc_reference_handle *next, *prev;
106 void *ptr;
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;
116 const char *name;
117 size_t size;
118 unsigned flags;
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");
136 return tc;
139 /* hook into the front of the list */
140 #define _TLIST_ADD(list, p) \
141 do { \
142 if (!(list)) { \
143 (list) = (p); \
144 (p)->next = (p)->prev = NULL; \
145 } else { \
146 (list)->prev = (p); \
147 (p)->next = (list); \
148 (p)->prev = NULL; \
149 (list) = (p); \
151 } while (0)
153 /* remove an element from a list - element doesn't have to be in list. */
154 #define _TLIST_REMOVE(list, p) \
155 do { \
156 if ((p) == (list)) { \
157 (list) = (p)->next; \
158 if (list) (list)->prev = NULL; \
159 } else { \
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; \
164 } while (0)
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;
174 return tc->parent;
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) {
195 return NULL;
198 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
199 if (tc == NULL) return NULL;
201 tc->size = size;
202 tc->flags = TALLOC_MAGIC;
203 tc->destructor = NULL;
204 tc->child = NULL;
205 tc->name = NULL;
206 tc->refs = NULL;
208 if (context) {
209 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
211 tc->parent = parent;
213 if (parent->child) {
214 parent->child->parent = NULL;
217 _TLIST_ADD(parent->child, tc);
218 } else {
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)) {
244 return -1;
246 return 0;
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);
256 return 0;
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
262 context are freed.
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,
266 and in either order
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);
286 return handle->ptr;
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);
305 if (p == NULL) {
306 if (context == NULL) break;
307 } else if (TC_PTR_FROM_CHUNK(p) == context) {
308 break;
311 if (h == NULL) {
312 return -1;
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;
325 void *new_parent;
327 if (ptr == NULL) {
328 return -1;
331 if (context == NULL) {
332 context = null_context;
335 if (talloc_unreference(context, ptr) == 0) {
336 return 0;
339 if (context == NULL) {
340 if (talloc_parent_chunk(ptr) != NULL) {
341 return -1;
343 } else {
344 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
345 return -1;
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);
356 if (new_p) {
357 new_parent = TC_PTR_FROM_CHUNK(new_p);
358 } else {
359 new_parent = NULL;
362 if (talloc_unreference(new_parent, ptr) != 0) {
363 return -1;
366 talloc_steal(new_parent, ptr);
368 return 0;
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);
380 if (tc->name) {
381 talloc_set_name_const(tc->name, ".name");
383 return tc->name;
387 add a name to an existing pointer
389 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
391 const char *name;
392 va_list ap;
393 va_start(ap, fmt);
394 name = talloc_set_name_v(ptr, fmt, ap);
395 va_end(ap);
396 return name;
400 more efficient way to add a name to a pointer - the name must point to a
401 true string constant
403 void talloc_set_name_const(const void *ptr, const char *name)
405 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
406 tc->name = name;
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
412 to name the pointer.
414 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
416 va_list ap;
417 void *ptr;
418 const char *name;
420 ptr = _talloc(context, size);
421 if (ptr == NULL) return NULL;
423 va_start(ap, fmt);
424 name = talloc_set_name_v(ptr, fmt, ap);
425 va_end(ap);
427 if (name == NULL) {
428 talloc_free(ptr);
429 return NULL;
432 return ptr;
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
438 to name the pointer.
440 void *talloc_named_const(const void *context, size_t size, const char *name)
442 void *ptr;
444 ptr = _talloc(context, size);
445 if (ptr == NULL) {
446 return NULL;
449 talloc_set_name_const(ptr, name);
451 return ptr;
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) {
461 return ".reference";
463 if (tc->name) {
464 return tc->name;
466 return "UNNAMED";
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)
476 const char *pname;
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);
482 return NULL;
487 this is for compatibility with older versions of talloc
489 void *talloc_init(const char *fmt, ...)
491 va_list ap;
492 void *ptr;
493 const char *name;
496 * samba3 expects talloc_report_depth_cb(NULL, ...)
497 * reports all talloc'ed memory, so we need to enable
498 * null_tracking
500 talloc_enable_null_tracking();
502 ptr = _talloc(NULL, 0);
503 if (ptr == NULL) return NULL;
505 va_start(ap, fmt);
506 name = talloc_set_name_v(ptr, fmt, ap);
507 va_end(ap);
509 if (name == NULL) {
510 talloc_free(ptr);
511 return NULL;
514 return ptr;
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;
526 if (ptr == NULL) {
527 return;
530 tc = talloc_chunk_from_ptr(ptr);
532 while (tc->child) {
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
556 pointer recursively
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;
565 int old_errno;
567 if (ptr == NULL) {
568 return -1;
571 tc = talloc_chunk_from_ptr(ptr);
573 if (tc->refs) {
574 int is_child;
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
580 * pointer.
582 is_child = talloc_is_parent(tc->refs, ptr);
583 talloc_free(tc->refs);
584 if (is_child) {
585 return talloc_free(ptr);
587 return -1;
590 if (tc->flags & TALLOC_FLAG_LOOP) {
591 /* we have a free loop - stop looping */
592 return 0;
595 if (tc->destructor) {
596 talloc_destructor_t d = tc->destructor;
597 if (d == (talloc_destructor_t)-1) {
598 return -1;
600 tc->destructor = (talloc_destructor_t)-1;
601 if (d(ptr) == -1) {
602 tc->destructor = d;
603 return -1;
605 tc->destructor = NULL;
608 if (tc->parent) {
609 _TLIST_REMOVE(tc->parent->child, tc);
610 if (tc->parent->child) {
611 tc->parent->child->parent = tc->parent;
613 } else {
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;
622 old_errno = errno;
623 free(tc);
624 errno = old_errno;
625 return 0;
631 A talloc version of realloc. The context argument is only used if
632 ptr is NULL
634 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
636 struct talloc_chunk *tc;
637 void *new_ptr;
639 /* size zero is equivalent to free() */
640 if (size == 0) {
641 talloc_free(ptr);
642 return NULL;
645 if (size >= MAX_TALLOC_SIZE) {
646 return NULL;
649 /* realloc(NULL) is equavalent to malloc() */
650 if (ptr == NULL) {
651 return talloc_named_const(context, size, name);
654 tc = talloc_chunk_from_ptr(ptr);
656 /* don't allow realloc on referenced pointers */
657 if (tc->refs) {
658 return NULL;
661 /* by resetting magic we catch users of the old memory */
662 tc->flags |= TALLOC_FLAG_FREE;
664 #if ALWAYS_REALLOC
665 new_ptr = malloc(size + TC_HDR_SIZE);
666 if (new_ptr) {
667 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
668 free(tc);
670 #else
671 new_ptr = realloc(tc, size + TC_HDR_SIZE);
672 #endif
673 if (!new_ptr) {
674 tc->flags &= ~TALLOC_FLAG_FREE;
675 return NULL;
678 tc = (struct talloc_chunk *)new_ptr;
679 tc->flags &= ~TALLOC_FLAG_FREE;
680 if (tc->parent) {
681 tc->parent->child = tc;
683 if (tc->child) {
684 tc->child->parent = tc;
687 if (tc->prev) {
688 tc->prev->next = tc;
690 if (tc->next) {
691 tc->next->prev = tc;
694 tc->size = size;
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;
709 if (!ptr) {
710 return NULL;
713 if (new_ctx == NULL) {
714 new_ctx = null_context;
717 tc = talloc_chunk_from_ptr(ptr);
719 if (new_ctx == NULL) {
720 if (tc->parent) {
721 _TLIST_REMOVE(tc->parent->child, tc);
722 if (tc->parent->child) {
723 tc->parent->child->parent = tc->parent;
725 } else {
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);
740 if (tc->parent) {
741 _TLIST_REMOVE(tc->parent->child, tc);
742 if (tc->parent->child) {
743 tc->parent->child->parent = tc->parent;
745 } else {
746 if (tc->prev) tc->prev->next = tc->next;
747 if (tc->next) tc->next->prev = tc->prev;
750 tc->parent = new_tc;
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)
762 size_t total = 0;
763 struct talloc_chunk *c, *tc;
765 if (ptr == NULL) {
766 ptr = null_context;
768 if (ptr == NULL) {
769 return 0;
772 tc = talloc_chunk_from_ptr(ptr);
774 if (tc->flags & TALLOC_FLAG_LOOP) {
775 return 0;
778 tc->flags |= TALLOC_FLAG_LOOP;
780 total = tc->size;
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;
787 return total;
791 return the total number of blocks in a talloc pool (subtree)
793 size_t talloc_total_blocks(const void *ptr)
795 size_t total = 0;
796 struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
798 if (tc->flags & TALLOC_FLAG_LOOP) {
799 return 0;
802 tc->flags |= TALLOC_FLAG_LOOP;
804 total++;
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;
811 return total;
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;
821 size_t ret = 0;
823 for (h=tc->refs;h;h=h->next) {
824 ret++;
826 return ret;
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,
835 int is_ref,
836 void *private_data),
837 void *private_data)
839 struct talloc_chunk *c, *tc;
841 if (ptr == NULL) {
842 ptr = null_context;
844 if (ptr == NULL) return;
846 tc = talloc_chunk_from_ptr(ptr);
848 if (tc->flags & TALLOC_FLAG_LOOP) {
849 return;
852 callback(ptr, depth, max_depth, 0, private_data);
854 if (max_depth >= 0 && depth >= max_depth) {
855 return;
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);
863 } else {
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;
875 if (is_ref) {
876 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
877 return;
880 if (depth == 0) {
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));
885 return;
888 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d)\n",
889 depth*4, "",
890 name,
891 (unsigned long)talloc_total_size(ptr),
892 (unsigned long)talloc_total_blocks(ptr),
893 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);
902 fflush(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);
957 null_context = NULL;
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);
985 if (p) {
986 memset(p, '\0', size);
989 return p;
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);
1000 if (newp) {
1001 memcpy(newp, p, size);
1004 return newp;
1008 strdup with a talloc
1010 char *talloc_strdup(const void *t, const char *p)
1012 char *ret;
1013 if (!p) {
1014 return NULL;
1016 ret = (char *)talloc_memdup(t, p, strlen(p) + 1);
1017 if (ret) {
1018 talloc_set_name_const(ret, ret);
1020 return ret;
1024 append to a talloced string
1026 char *talloc_append_string(const void *t, char *orig, const char *append)
1028 char *ret;
1029 size_t olen = strlen(orig);
1030 size_t alenz;
1032 if (!append)
1033 return orig;
1035 alenz = strlen(append) + 1;
1037 ret = talloc_realloc(t, orig, char, olen + alenz);
1038 if (!ret)
1039 return NULL;
1041 /* append the string with the trailing \0 */
1042 memcpy(&ret[olen], append, alenz);
1044 return ret;
1048 strndup with a talloc
1050 char *talloc_strndup(const void *t, const char *p, size_t n)
1052 size_t len;
1053 char *ret;
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);
1060 ret[len] = 0;
1061 talloc_set_name_const(ret, ret);
1062 return ret;
1065 #ifndef HAVE_VA_COPY
1066 #ifdef HAVE___VA_COPY
1067 #define va_copy(dest, src) __va_copy(dest, src)
1068 #else
1069 #define va_copy(dest, src) (dest) = (src)
1070 #endif
1071 #endif
1073 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1075 int len;
1076 char *ret;
1077 va_list ap2;
1078 char c;
1080 va_copy(ap2, ap);
1082 /* this call looks strange, but it makes it work on older solaris boxes */
1083 if ((len = vsnprintf(&c, 1, fmt, ap2)) < 0) {
1084 return NULL;
1087 ret = (char *)_talloc(t, len+1);
1088 if (ret) {
1089 va_copy(ap2, ap);
1090 vsnprintf(ret, len+1, fmt, ap2);
1091 talloc_set_name_const(ret, ret);
1094 return 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, ...)
1104 va_list ap;
1105 char *ret;
1107 va_start(ap, fmt);
1108 ret = talloc_vasprintf(t, fmt, ap);
1109 va_end(ap);
1110 return ret;
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;
1122 int len, s_len;
1123 va_list ap2;
1124 char c;
1126 if (s == NULL) {
1127 return talloc_vasprintf(NULL, fmt, ap);
1130 tc = talloc_chunk_from_ptr(s);
1132 va_copy(ap2, ap);
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.
1142 return s;
1145 s = talloc_realloc(NULL, s, char, s_len + len+1);
1146 if (!s) return NULL;
1148 va_copy(ap2, ap);
1150 vsnprintf(s+s_len, len+1, fmt, ap2);
1151 talloc_set_name_const(s, s);
1153 return 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, ...)
1163 va_list ap;
1165 va_start(ap, fmt);
1166 s = talloc_vasprintf_append(s, fmt, ap);
1167 va_end(ap);
1168 return s;
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) {
1177 return NULL;
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) {
1188 return NULL;
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) {
1200 return NULL;
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)
1240 return 0;
1242 tc = talloc_chunk_from_ptr(context);
1244 return tc->size;
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) {
1255 return NULL;
1258 tc = talloc_chunk_from_ptr(context);
1259 while (tc) {
1260 if (tc->name && strcmp(tc->name, name) == 0) {
1261 return TC_PTR_FROM_CHUNK(tc);
1263 while (tc && tc->prev) tc = tc->prev;
1264 if (tc) {
1265 tc = tc->parent;
1268 return NULL;
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");
1280 return;
1283 tc = talloc_chunk_from_ptr(context);
1284 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1285 while (tc) {
1286 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1287 while (tc && tc->prev) tc = tc->prev;
1288 if (tc) {
1289 tc = tc->parent;
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) {
1302 return 0;
1305 tc = talloc_chunk_from_ptr(context);
1306 while (tc) {
1307 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1308 while (tc && tc->prev) tc = tc->prev;
1309 if (tc) {
1310 tc = tc->parent;
1313 return 0;