talloc: add talloc_set_log_fn() and talloc_set_log_stderr()
[Samba/fernandojvsilva.git] / lib / talloc / talloc.c
blob58776a9160cfbeec089a356256ae93215c64e35d
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 3 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, see <http://www.gnu.org/licenses/>.
30 inspired by http://swapped.cc/halloc/
33 #ifdef _SAMBA_BUILD_
34 #include "version.h"
35 #if (SAMBA_VERSION_MAJOR<4)
36 #include "includes.h"
37 /* This is to circumvent SAMBA3's paranoid malloc checker. Here in this file
38 * we trust ourselves... */
39 #ifdef malloc
40 #undef malloc
41 #endif
42 #ifdef realloc
43 #undef realloc
44 #endif
45 #define _TALLOC_SAMBA3
46 #endif /* (SAMBA_VERSION_MAJOR<4) */
47 #endif /* _SAMBA_BUILD_ */
49 #ifndef _TALLOC_SAMBA3
50 #include "replace.h"
51 #include "talloc.h"
52 #endif /* not _TALLOC_SAMBA3 */
54 /* use this to force every realloc to change the pointer, to stress test
55 code that might not cope */
56 #define ALWAYS_REALLOC 0
59 #define MAX_TALLOC_SIZE 0x10000000
60 #define TALLOC_MAGIC_V1 0xe814ec70
61 #define TALLOC_MAGIC_V2 0xe814ec80
62 #define TALLOC_MAGIC TALLOC_MAGIC_V2
63 #define TALLOC_FLAG_FREE 0x01
64 #define TALLOC_FLAG_LOOP 0x02
65 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
66 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
67 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
69 /* by default we abort when given a bad pointer (such as when talloc_free() is called
70 on a pointer that came from malloc() */
71 #ifndef TALLOC_ABORT
72 #define TALLOC_ABORT(reason) abort()
73 #endif
75 #ifndef discard_const_p
76 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
77 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
78 #else
79 # define discard_const_p(type, ptr) ((type *)(ptr))
80 #endif
81 #endif
83 /* these macros gain us a few percent of speed on gcc */
84 #if (__GNUC__ >= 3)
85 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
86 as its first argument */
87 #ifndef likely
88 #define likely(x) __builtin_expect(!!(x), 1)
89 #endif
90 #ifndef unlikely
91 #define unlikely(x) __builtin_expect(!!(x), 0)
92 #endif
93 #else
94 #ifndef likely
95 #define likely(x) (x)
96 #endif
97 #ifndef unlikely
98 #define unlikely(x) (x)
99 #endif
100 #endif
102 /* this null_context is only used if talloc_enable_leak_report() or
103 talloc_enable_leak_report_full() is called, otherwise it remains
104 NULL
106 static void *null_context;
107 static void *autofree_context;
109 struct talloc_reference_handle {
110 struct talloc_reference_handle *next, *prev;
111 void *ptr;
112 const char *location;
115 typedef int (*talloc_destructor_t)(void *);
117 struct talloc_chunk {
118 struct talloc_chunk *next, *prev;
119 struct talloc_chunk *parent, *child;
120 struct talloc_reference_handle *refs;
121 talloc_destructor_t destructor;
122 const char *name;
123 size_t size;
124 unsigned flags;
127 * "pool" has dual use:
129 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
130 * marks the end of the currently allocated area.
132 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
133 * is a pointer to the struct talloc_chunk of the pool that it was
134 * allocated from. This way children can quickly find the pool to chew
135 * from.
137 void *pool;
140 /* 16 byte alignment seems to keep everyone happy */
141 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
142 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
144 static void (*talloc_log_fn)(const char *message);
146 void talloc_set_log_fn(void (*log_fn)(const char *message))
148 talloc_log_fn = log_fn;
151 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
152 static void talloc_log(const char *fmt, ...)
154 va_list ap;
155 char *message;
157 if (!talloc_log_fn) {
158 return;
161 va_start(ap, fmt);
162 message = talloc_vasprintf(NULL, fmt, ap);
163 va_end(ap);
165 talloc_log_fn(message);
166 talloc_free(message);
169 static void talloc_log_stderr(const char *message)
171 fprintf(stderr, "%s", message);
174 void talloc_set_log_stderr(void)
176 talloc_set_log_fn(talloc_log_stderr);
179 static void (*talloc_abort_fn)(const char *reason);
181 void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
183 talloc_abort_fn = abort_fn;
186 static void talloc_abort(const char *reason)
188 talloc_log("%s\n", reason);
190 if (!talloc_abort_fn) {
191 TALLOC_ABORT(reason);
194 talloc_abort_fn(reason);
197 static void talloc_abort_magic_v1(void)
199 talloc_abort("Bad talloc magic value - old magic v1 used");
202 static void talloc_abort_double_free(void)
204 talloc_abort("Bad talloc magic value - double free");
207 static void talloc_abort_unknown_value(void)
209 talloc_abort("Bad talloc magic value - unknown value");
212 /* panic if we get a bad magic value */
213 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
215 const char *pp = (const char *)ptr;
216 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
217 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
218 if ((tc->flags & (~0xF)) == TALLOC_MAGIC_V1) {
219 talloc_abort_magic_v1();
220 return NULL;
223 if (tc->flags & TALLOC_FLAG_FREE) {
224 talloc_abort_double_free();
225 return NULL;
226 } else {
227 talloc_abort_unknown_value();
228 return NULL;
231 return tc;
234 /* hook into the front of the list */
235 #define _TLIST_ADD(list, p) \
236 do { \
237 if (!(list)) { \
238 (list) = (p); \
239 (p)->next = (p)->prev = NULL; \
240 } else { \
241 (list)->prev = (p); \
242 (p)->next = (list); \
243 (p)->prev = NULL; \
244 (list) = (p); \
246 } while (0)
248 /* remove an element from a list - element doesn't have to be in list. */
249 #define _TLIST_REMOVE(list, p) \
250 do { \
251 if ((p) == (list)) { \
252 (list) = (p)->next; \
253 if (list) (list)->prev = NULL; \
254 } else { \
255 if ((p)->prev) (p)->prev->next = (p)->next; \
256 if ((p)->next) (p)->next->prev = (p)->prev; \
258 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
259 } while (0)
263 return the parent chunk of a pointer
265 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
267 struct talloc_chunk *tc;
269 if (unlikely(ptr == NULL)) {
270 return NULL;
273 tc = talloc_chunk_from_ptr(ptr);
274 while (tc->prev) tc=tc->prev;
276 return tc->parent;
279 void *talloc_parent(const void *ptr)
281 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
282 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
286 find parents name
288 const char *talloc_parent_name(const void *ptr)
290 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
291 return tc? tc->name : NULL;
295 A pool carries an in-pool object count count in the first 16 bytes.
296 bytes. This is done to support talloc_steal() to a parent outside of the
297 pool. The count includes the pool itself, so a talloc_free() on a pool will
298 only destroy the pool if the count has dropped to zero. A talloc_free() of a
299 pool member will reduce the count, and eventually also call free(3) on the
300 pool memory.
302 The object count is not put into "struct talloc_chunk" because it is only
303 relevant for talloc pools and the alignment to 16 bytes would increase the
304 memory footprint of each talloc chunk by those 16 bytes.
307 #define TALLOC_POOL_HDR_SIZE 16
309 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
311 return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
315 Allocate from a pool
318 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
319 size_t size)
321 struct talloc_chunk *pool_ctx = NULL;
322 size_t space_left;
323 struct talloc_chunk *result;
324 size_t chunk_size;
326 if (parent == NULL) {
327 return NULL;
330 if (parent->flags & TALLOC_FLAG_POOL) {
331 pool_ctx = parent;
333 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
334 pool_ctx = (struct talloc_chunk *)parent->pool;
337 if (pool_ctx == NULL) {
338 return NULL;
341 space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
342 - ((char *)pool_ctx->pool);
345 * Align size to 16 bytes
347 chunk_size = ((size + 15) & ~15);
349 if (space_left < chunk_size) {
350 return NULL;
353 result = (struct talloc_chunk *)pool_ctx->pool;
355 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
356 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
357 #endif
359 pool_ctx->pool = (void *)((char *)result + chunk_size);
361 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
362 result->pool = pool_ctx;
364 *talloc_pool_objectcount(pool_ctx) += 1;
366 return result;
370 Allocate a bit of memory as a child of an existing pointer
372 static inline void *__talloc(const void *context, size_t size)
374 struct talloc_chunk *tc = NULL;
376 if (unlikely(context == NULL)) {
377 context = null_context;
380 if (unlikely(size >= MAX_TALLOC_SIZE)) {
381 return NULL;
384 if (context != NULL) {
385 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
386 TC_HDR_SIZE+size);
389 if (tc == NULL) {
390 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
391 if (unlikely(tc == NULL)) return NULL;
392 tc->flags = TALLOC_MAGIC;
393 tc->pool = NULL;
396 tc->size = size;
397 tc->destructor = NULL;
398 tc->child = NULL;
399 tc->name = NULL;
400 tc->refs = NULL;
402 if (likely(context)) {
403 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
405 if (parent->child) {
406 parent->child->parent = NULL;
407 tc->next = parent->child;
408 tc->next->prev = tc;
409 } else {
410 tc->next = NULL;
412 tc->parent = parent;
413 tc->prev = NULL;
414 parent->child = tc;
415 } else {
416 tc->next = tc->prev = tc->parent = NULL;
419 return TC_PTR_FROM_CHUNK(tc);
423 * Create a talloc pool
426 void *talloc_pool(const void *context, size_t size)
428 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
429 struct talloc_chunk *tc;
431 if (unlikely(result == NULL)) {
432 return NULL;
435 tc = talloc_chunk_from_ptr(result);
437 tc->flags |= TALLOC_FLAG_POOL;
438 tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
440 *talloc_pool_objectcount(tc) = 1;
442 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
443 VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
444 #endif
446 return result;
450 setup a destructor to be called on free of a pointer
451 the destructor should return 0 on success, or -1 on failure.
452 if the destructor fails then the free is failed, and the memory can
453 be continued to be used
455 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
457 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
458 tc->destructor = destructor;
462 increase the reference count on a piece of memory.
464 int talloc_increase_ref_count(const void *ptr)
466 if (unlikely(!talloc_reference(null_context, ptr))) {
467 return -1;
469 return 0;
473 helper for talloc_reference()
475 this is referenced by a function pointer and should not be inline
477 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
479 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
480 _TLIST_REMOVE(ptr_tc->refs, handle);
481 return 0;
485 more efficient way to add a name to a pointer - the name must point to a
486 true string constant
488 static inline void _talloc_set_name_const(const void *ptr, const char *name)
490 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
491 tc->name = name;
495 internal talloc_named_const()
497 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
499 void *ptr;
501 ptr = __talloc(context, size);
502 if (unlikely(ptr == NULL)) {
503 return NULL;
506 _talloc_set_name_const(ptr, name);
508 return ptr;
512 make a secondary reference to a pointer, hanging off the given context.
513 the pointer remains valid until both the original caller and this given
514 context are freed.
516 the major use for this is when two different structures need to reference the
517 same underlying data, and you want to be able to free the two instances separately,
518 and in either order
520 void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
522 struct talloc_chunk *tc;
523 struct talloc_reference_handle *handle;
524 if (unlikely(ptr == NULL)) return NULL;
526 tc = talloc_chunk_from_ptr(ptr);
527 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
528 sizeof(struct talloc_reference_handle),
529 TALLOC_MAGIC_REFERENCE);
530 if (unlikely(handle == NULL)) return NULL;
532 /* note that we hang the destructor off the handle, not the
533 main context as that allows the caller to still setup their
534 own destructor on the context if they want to */
535 talloc_set_destructor(handle, talloc_reference_destructor);
536 handle->ptr = discard_const_p(void, ptr);
537 handle->location = location;
538 _TLIST_ADD(tc->refs, handle);
539 return handle->ptr;
542 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
545 internal talloc_free call
547 static inline int _talloc_free_internal(void *ptr)
549 struct talloc_chunk *tc;
551 if (unlikely(ptr == NULL)) {
552 return -1;
555 tc = talloc_chunk_from_ptr(ptr);
557 if (unlikely(tc->refs)) {
558 int is_child;
559 /* check this is a reference from a child or grantchild
560 * back to it's parent or grantparent
562 * in that case we need to remove the reference and
563 * call another instance of talloc_free() on the current
564 * pointer.
566 is_child = talloc_is_parent(tc->refs, ptr);
567 _talloc_free_internal(tc->refs);
568 if (is_child) {
569 return _talloc_free_internal(ptr);
571 return -1;
574 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
575 /* we have a free loop - stop looping */
576 return 0;
579 if (unlikely(tc->destructor)) {
580 talloc_destructor_t d = tc->destructor;
581 if (d == (talloc_destructor_t)-1) {
582 return -1;
584 tc->destructor = (talloc_destructor_t)-1;
585 if (d(ptr) == -1) {
586 tc->destructor = d;
587 return -1;
589 tc->destructor = NULL;
592 if (tc->parent) {
593 _TLIST_REMOVE(tc->parent->child, tc);
594 if (tc->parent->child) {
595 tc->parent->child->parent = tc->parent;
597 } else {
598 if (tc->prev) tc->prev->next = tc->next;
599 if (tc->next) tc->next->prev = tc->prev;
602 tc->flags |= TALLOC_FLAG_LOOP;
604 while (tc->child) {
605 /* we need to work out who will own an abandoned child
606 if it cannot be freed. In priority order, the first
607 choice is owner of any remaining reference to this
608 pointer, the second choice is our parent, and the
609 final choice is the null context. */
610 void *child = TC_PTR_FROM_CHUNK(tc->child);
611 const void *new_parent = null_context;
612 if (unlikely(tc->child->refs)) {
613 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
614 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
616 if (unlikely(_talloc_free_internal(child) == -1)) {
617 if (new_parent == null_context) {
618 struct talloc_chunk *p = talloc_parent_chunk(ptr);
619 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
621 _talloc_steal_internal(new_parent, child);
625 tc->flags |= TALLOC_FLAG_FREE;
627 if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
628 struct talloc_chunk *pool;
629 unsigned int *pool_object_count;
631 pool = (tc->flags & TALLOC_FLAG_POOL)
632 ? tc : (struct talloc_chunk *)tc->pool;
634 pool_object_count = talloc_pool_objectcount(pool);
636 if (*pool_object_count == 0) {
637 talloc_abort("Pool object count zero!");
638 return 0;
641 *pool_object_count -= 1;
643 if (*pool_object_count == 0) {
644 free(pool);
647 else {
648 free(tc);
650 return 0;
654 move a lump of memory from one talloc context to another return the
655 ptr on success, or NULL if it could not be transferred.
656 passing NULL as ptr will always return NULL with no side effects.
658 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
660 struct talloc_chunk *tc, *new_tc;
662 if (unlikely(!ptr)) {
663 return NULL;
666 if (unlikely(new_ctx == NULL)) {
667 new_ctx = null_context;
670 tc = talloc_chunk_from_ptr(ptr);
672 if (unlikely(new_ctx == NULL)) {
673 if (tc->parent) {
674 _TLIST_REMOVE(tc->parent->child, tc);
675 if (tc->parent->child) {
676 tc->parent->child->parent = tc->parent;
678 } else {
679 if (tc->prev) tc->prev->next = tc->next;
680 if (tc->next) tc->next->prev = tc->prev;
683 tc->parent = tc->next = tc->prev = NULL;
684 return discard_const_p(void, ptr);
687 new_tc = talloc_chunk_from_ptr(new_ctx);
689 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
690 return discard_const_p(void, ptr);
693 if (tc->parent) {
694 _TLIST_REMOVE(tc->parent->child, tc);
695 if (tc->parent->child) {
696 tc->parent->child->parent = tc->parent;
698 } else {
699 if (tc->prev) tc->prev->next = tc->next;
700 if (tc->next) tc->next->prev = tc->prev;
703 tc->parent = new_tc;
704 if (new_tc->child) new_tc->child->parent = NULL;
705 _TLIST_ADD(new_tc->child, tc);
707 return discard_const_p(void, ptr);
711 move a lump of memory from one talloc context to another return the
712 ptr on success, or NULL if it could not be transferred.
713 passing NULL as ptr will always return NULL with no side effects.
715 void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
717 struct talloc_chunk *tc;
719 if (unlikely(ptr == NULL)) {
720 return NULL;
723 tc = talloc_chunk_from_ptr(ptr);
725 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
726 struct talloc_reference_handle *h;
728 talloc_log("WARNING: talloc_steal with references at %s\n",
729 location);
731 for (h=tc->refs; h; h=h->next) {
732 talloc_log("\treference at %s\n",
733 h->location);
737 return _talloc_steal_internal(new_ctx, ptr);
741 this is like a talloc_steal(), but you must supply the old
742 parent. This resolves the ambiguity in a talloc_steal() which is
743 called on a context that has more than one parent (via references)
745 The old parent can be either a reference or a parent
747 void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
749 struct talloc_chunk *tc;
750 struct talloc_reference_handle *h;
752 if (unlikely(ptr == NULL)) {
753 return NULL;
756 if (old_parent == talloc_parent(ptr)) {
757 return _talloc_steal_internal(new_parent, ptr);
760 tc = talloc_chunk_from_ptr(ptr);
761 for (h=tc->refs;h;h=h->next) {
762 if (talloc_parent(h) == old_parent) {
763 if (_talloc_steal_internal(new_parent, h) != h) {
764 return NULL;
766 return (void *)ptr;
770 /* it wasn't a parent */
771 return NULL;
775 remove a secondary reference to a pointer. This undo's what
776 talloc_reference() has done. The context and pointer arguments
777 must match those given to a talloc_reference()
779 static inline int talloc_unreference(const void *context, const void *ptr)
781 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
782 struct talloc_reference_handle *h;
784 if (unlikely(context == NULL)) {
785 context = null_context;
788 for (h=tc->refs;h;h=h->next) {
789 struct talloc_chunk *p = talloc_parent_chunk(h);
790 if (p == NULL) {
791 if (context == NULL) break;
792 } else if (TC_PTR_FROM_CHUNK(p) == context) {
793 break;
796 if (h == NULL) {
797 return -1;
800 return _talloc_free_internal(h);
804 remove a specific parent context from a pointer. This is a more
805 controlled varient of talloc_free()
807 int talloc_unlink(const void *context, void *ptr)
809 struct talloc_chunk *tc_p, *new_p;
810 void *new_parent;
812 if (ptr == NULL) {
813 return -1;
816 if (context == NULL) {
817 context = null_context;
820 if (talloc_unreference(context, ptr) == 0) {
821 return 0;
824 if (context == NULL) {
825 if (talloc_parent_chunk(ptr) != NULL) {
826 return -1;
828 } else {
829 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
830 return -1;
834 tc_p = talloc_chunk_from_ptr(ptr);
836 if (tc_p->refs == NULL) {
837 return _talloc_free_internal(ptr);
840 new_p = talloc_parent_chunk(tc_p->refs);
841 if (new_p) {
842 new_parent = TC_PTR_FROM_CHUNK(new_p);
843 } else {
844 new_parent = NULL;
847 if (talloc_unreference(new_parent, ptr) != 0) {
848 return -1;
851 _talloc_steal_internal(new_parent, ptr);
853 return 0;
857 add a name to an existing pointer - va_list version
859 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
861 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
863 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
864 tc->name = talloc_vasprintf(ptr, fmt, ap);
865 if (likely(tc->name)) {
866 _talloc_set_name_const(tc->name, ".name");
868 return tc->name;
872 add a name to an existing pointer
874 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
876 const char *name;
877 va_list ap;
878 va_start(ap, fmt);
879 name = talloc_set_name_v(ptr, fmt, ap);
880 va_end(ap);
881 return name;
886 create a named talloc pointer. Any talloc pointer can be named, and
887 talloc_named() operates just like talloc() except that it allows you
888 to name the pointer.
890 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
892 va_list ap;
893 void *ptr;
894 const char *name;
896 ptr = __talloc(context, size);
897 if (unlikely(ptr == NULL)) return NULL;
899 va_start(ap, fmt);
900 name = talloc_set_name_v(ptr, fmt, ap);
901 va_end(ap);
903 if (unlikely(name == NULL)) {
904 _talloc_free_internal(ptr);
905 return NULL;
908 return ptr;
912 return the name of a talloc ptr, or "UNNAMED"
914 const char *talloc_get_name(const void *ptr)
916 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
917 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
918 return ".reference";
920 if (likely(tc->name)) {
921 return tc->name;
923 return "UNNAMED";
928 check if a pointer has the given name. If it does, return the pointer,
929 otherwise return NULL
931 void *talloc_check_name(const void *ptr, const char *name)
933 const char *pname;
934 if (unlikely(ptr == NULL)) return NULL;
935 pname = talloc_get_name(ptr);
936 if (likely(pname == name || strcmp(pname, name) == 0)) {
937 return discard_const_p(void, ptr);
939 return NULL;
942 static void talloc_abort_type_missmatch(const char *location,
943 const char *name,
944 const char *expected)
946 const char *reason;
948 reason = talloc_asprintf(NULL,
949 "%s: Type mismatch: name[%s] expected[%s]",
950 location,
951 name?name:"NULL",
952 expected);
953 if (!reason) {
954 reason = "Type mismatch";
957 talloc_abort(reason);
960 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
962 const char *pname;
964 if (unlikely(ptr == NULL)) {
965 talloc_abort_type_missmatch(location, NULL, name);
966 return NULL;
969 pname = talloc_get_name(ptr);
970 if (likely(pname == name || strcmp(pname, name) == 0)) {
971 return discard_const_p(void, ptr);
974 talloc_abort_type_missmatch(location, pname, name);
975 return NULL;
979 this is for compatibility with older versions of talloc
981 void *talloc_init(const char *fmt, ...)
983 va_list ap;
984 void *ptr;
985 const char *name;
988 * samba3 expects talloc_report_depth_cb(NULL, ...)
989 * reports all talloc'ed memory, so we need to enable
990 * null_tracking
992 talloc_enable_null_tracking();
994 ptr = __talloc(NULL, 0);
995 if (unlikely(ptr == NULL)) return NULL;
997 va_start(ap, fmt);
998 name = talloc_set_name_v(ptr, fmt, ap);
999 va_end(ap);
1001 if (unlikely(name == NULL)) {
1002 _talloc_free_internal(ptr);
1003 return NULL;
1006 return ptr;
1010 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1011 should probably not be used in new code. It's in here to keep the talloc
1012 code consistent across Samba 3 and 4.
1014 void talloc_free_children(void *ptr)
1016 struct talloc_chunk *tc;
1018 if (unlikely(ptr == NULL)) {
1019 return;
1022 tc = talloc_chunk_from_ptr(ptr);
1024 while (tc->child) {
1025 /* we need to work out who will own an abandoned child
1026 if it cannot be freed. In priority order, the first
1027 choice is owner of any remaining reference to this
1028 pointer, the second choice is our parent, and the
1029 final choice is the null context. */
1030 void *child = TC_PTR_FROM_CHUNK(tc->child);
1031 const void *new_parent = null_context;
1032 if (unlikely(tc->child->refs)) {
1033 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1034 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1036 if (unlikely(talloc_free(child) == -1)) {
1037 if (new_parent == null_context) {
1038 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1039 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1041 _talloc_steal_internal(new_parent, child);
1045 if ((tc->flags & TALLOC_FLAG_POOL)
1046 && (*talloc_pool_objectcount(tc) == 1)) {
1047 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
1048 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
1049 VALGRIND_MAKE_MEM_NOACCESS(
1050 tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
1051 #endif
1056 Allocate a bit of memory as a child of an existing pointer
1058 void *_talloc(const void *context, size_t size)
1060 return __talloc(context, size);
1064 externally callable talloc_set_name_const()
1066 void talloc_set_name_const(const void *ptr, const char *name)
1068 _talloc_set_name_const(ptr, name);
1072 create a named talloc pointer. Any talloc pointer can be named, and
1073 talloc_named() operates just like talloc() except that it allows you
1074 to name the pointer.
1076 void *talloc_named_const(const void *context, size_t size, const char *name)
1078 return _talloc_named_const(context, size, name);
1082 free a talloc pointer. This also frees all child pointers of this
1083 pointer recursively
1085 return 0 if the memory is actually freed, otherwise -1. The memory
1086 will not be freed if the ref_count is > 1 or the destructor (if
1087 any) returns non-zero
1089 int _talloc_free(void *ptr, const char *location)
1091 struct talloc_chunk *tc;
1093 if (unlikely(ptr == NULL)) {
1094 return -1;
1097 tc = talloc_chunk_from_ptr(ptr);
1099 if (unlikely(tc->refs != NULL)) {
1100 struct talloc_reference_handle *h;
1102 talloc_log("ERROR: talloc_free with references at %s\n",
1103 location);
1105 for (h=tc->refs; h; h=h->next) {
1106 talloc_log("\treference at %s\n",
1107 h->location);
1109 return -1;
1112 return _talloc_free_internal(ptr);
1118 A talloc version of realloc. The context argument is only used if
1119 ptr is NULL
1121 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1123 struct talloc_chunk *tc;
1124 void *new_ptr;
1125 bool malloced = false;
1127 /* size zero is equivalent to free() */
1128 if (unlikely(size == 0)) {
1129 talloc_unlink(context, ptr);
1130 return NULL;
1133 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1134 return NULL;
1137 /* realloc(NULL) is equivalent to malloc() */
1138 if (ptr == NULL) {
1139 return _talloc_named_const(context, size, name);
1142 tc = talloc_chunk_from_ptr(ptr);
1144 /* don't allow realloc on referenced pointers */
1145 if (unlikely(tc->refs)) {
1146 return NULL;
1149 /* don't let anybody try to realloc a talloc_pool */
1150 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1151 return NULL;
1154 /* don't shrink if we have less than 1k to gain */
1155 if ((size < tc->size) && ((tc->size - size) < 1024)) {
1156 tc->size = size;
1157 return ptr;
1160 /* by resetting magic we catch users of the old memory */
1161 tc->flags |= TALLOC_FLAG_FREE;
1163 #if ALWAYS_REALLOC
1164 new_ptr = malloc(size + TC_HDR_SIZE);
1165 if (new_ptr) {
1166 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
1167 free(tc);
1169 #else
1170 if (tc->flags & TALLOC_FLAG_POOLMEM) {
1172 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1173 *talloc_pool_objectcount((struct talloc_chunk *)
1174 (tc->pool)) -= 1;
1176 if (new_ptr == NULL) {
1177 new_ptr = malloc(TC_HDR_SIZE+size);
1178 malloced = true;
1181 if (new_ptr) {
1182 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1185 else {
1186 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1188 #endif
1189 if (unlikely(!new_ptr)) {
1190 tc->flags &= ~TALLOC_FLAG_FREE;
1191 return NULL;
1194 tc = (struct talloc_chunk *)new_ptr;
1195 tc->flags &= ~TALLOC_FLAG_FREE;
1196 if (malloced) {
1197 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1199 if (tc->parent) {
1200 tc->parent->child = tc;
1202 if (tc->child) {
1203 tc->child->parent = tc;
1206 if (tc->prev) {
1207 tc->prev->next = tc;
1209 if (tc->next) {
1210 tc->next->prev = tc;
1213 tc->size = size;
1214 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1216 return TC_PTR_FROM_CHUNK(tc);
1220 a wrapper around talloc_steal() for situations where you are moving a pointer
1221 between two structures, and want the old pointer to be set to NULL
1223 void *_talloc_move(const void *new_ctx, const void *_pptr)
1225 const void **pptr = discard_const_p(const void *,_pptr);
1226 void *ret = talloc_steal(new_ctx, (void *)*pptr);
1227 (*pptr) = NULL;
1228 return ret;
1232 return the total size of a talloc pool (subtree)
1234 size_t talloc_total_size(const void *ptr)
1236 size_t total = 0;
1237 struct talloc_chunk *c, *tc;
1239 if (ptr == NULL) {
1240 ptr = null_context;
1242 if (ptr == NULL) {
1243 return 0;
1246 tc = talloc_chunk_from_ptr(ptr);
1248 if (tc->flags & TALLOC_FLAG_LOOP) {
1249 return 0;
1252 tc->flags |= TALLOC_FLAG_LOOP;
1254 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1255 total = tc->size;
1257 for (c=tc->child;c;c=c->next) {
1258 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1261 tc->flags &= ~TALLOC_FLAG_LOOP;
1263 return total;
1267 return the total number of blocks in a talloc pool (subtree)
1269 size_t talloc_total_blocks(const void *ptr)
1271 size_t total = 0;
1272 struct talloc_chunk *c, *tc;
1274 if (ptr == NULL) {
1275 ptr = null_context;
1277 if (ptr == NULL) {
1278 return 0;
1281 tc = talloc_chunk_from_ptr(ptr);
1283 if (tc->flags & TALLOC_FLAG_LOOP) {
1284 return 0;
1287 tc->flags |= TALLOC_FLAG_LOOP;
1289 total++;
1290 for (c=tc->child;c;c=c->next) {
1291 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1294 tc->flags &= ~TALLOC_FLAG_LOOP;
1296 return total;
1300 return the number of external references to a pointer
1302 size_t talloc_reference_count(const void *ptr)
1304 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1305 struct talloc_reference_handle *h;
1306 size_t ret = 0;
1308 for (h=tc->refs;h;h=h->next) {
1309 ret++;
1311 return ret;
1315 report on memory usage by all children of a pointer, giving a full tree view
1317 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1318 void (*callback)(const void *ptr,
1319 int depth, int max_depth,
1320 int is_ref,
1321 void *private_data),
1322 void *private_data)
1324 struct talloc_chunk *c, *tc;
1326 if (ptr == NULL) {
1327 ptr = null_context;
1329 if (ptr == NULL) return;
1331 tc = talloc_chunk_from_ptr(ptr);
1333 if (tc->flags & TALLOC_FLAG_LOOP) {
1334 return;
1337 callback(ptr, depth, max_depth, 0, private_data);
1339 if (max_depth >= 0 && depth >= max_depth) {
1340 return;
1343 tc->flags |= TALLOC_FLAG_LOOP;
1344 for (c=tc->child;c;c=c->next) {
1345 if (c->name == TALLOC_MAGIC_REFERENCE) {
1346 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1347 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1348 } else {
1349 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1352 tc->flags &= ~TALLOC_FLAG_LOOP;
1355 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1357 const char *name = talloc_get_name(ptr);
1358 FILE *f = (FILE *)_f;
1360 if (is_ref) {
1361 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1362 return;
1365 if (depth == 0) {
1366 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1367 (max_depth < 0 ? "full " :""), name,
1368 (unsigned long)talloc_total_size(ptr),
1369 (unsigned long)talloc_total_blocks(ptr));
1370 return;
1373 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1374 depth*4, "",
1375 name,
1376 (unsigned long)talloc_total_size(ptr),
1377 (unsigned long)talloc_total_blocks(ptr),
1378 (int)talloc_reference_count(ptr), ptr);
1380 #if 0
1381 fprintf(f, "content: ");
1382 if (talloc_total_size(ptr)) {
1383 int tot = talloc_total_size(ptr);
1384 int i;
1386 for (i = 0; i < tot; i++) {
1387 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1388 fprintf(f, "%c", ((char *)ptr)[i]);
1389 } else {
1390 fprintf(f, "~%02x", ((char *)ptr)[i]);
1394 fprintf(f, "\n");
1395 #endif
1399 report on memory usage by all children of a pointer, giving a full tree view
1401 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1403 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1404 fflush(f);
1408 report on memory usage by all children of a pointer, giving a full tree view
1410 void talloc_report_full(const void *ptr, FILE *f)
1412 talloc_report_depth_file(ptr, 0, -1, f);
1416 report on memory usage by all children of a pointer
1418 void talloc_report(const void *ptr, FILE *f)
1420 talloc_report_depth_file(ptr, 0, 1, f);
1424 report on any memory hanging off the null context
1426 static void talloc_report_null(void)
1428 if (talloc_total_size(null_context) != 0) {
1429 talloc_report(null_context, stderr);
1434 report on any memory hanging off the null context
1436 static void talloc_report_null_full(void)
1438 if (talloc_total_size(null_context) != 0) {
1439 talloc_report_full(null_context, stderr);
1444 enable tracking of the NULL context
1446 void talloc_enable_null_tracking(void)
1448 if (null_context == NULL) {
1449 null_context = _talloc_named_const(NULL, 0, "null_context");
1454 disable tracking of the NULL context
1456 void talloc_disable_null_tracking(void)
1458 talloc_free(null_context);
1459 null_context = NULL;
1463 enable leak reporting on exit
1465 void talloc_enable_leak_report(void)
1467 talloc_enable_null_tracking();
1468 atexit(talloc_report_null);
1472 enable full leak reporting on exit
1474 void talloc_enable_leak_report_full(void)
1476 talloc_enable_null_tracking();
1477 atexit(talloc_report_null_full);
1481 talloc and zero memory.
1483 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1485 void *p = _talloc_named_const(ctx, size, name);
1487 if (p) {
1488 memset(p, '\0', size);
1491 return p;
1495 memdup with a talloc.
1497 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1499 void *newp = _talloc_named_const(t, size, name);
1501 if (likely(newp)) {
1502 memcpy(newp, p, size);
1505 return newp;
1508 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1510 char *ret;
1512 ret = (char *)__talloc(t, len + 1);
1513 if (unlikely(!ret)) return NULL;
1515 memcpy(ret, p, len);
1516 ret[len] = 0;
1518 _talloc_set_name_const(ret, ret);
1519 return ret;
1523 strdup with a talloc
1525 char *talloc_strdup(const void *t, const char *p)
1527 if (unlikely(!p)) return NULL;
1528 return __talloc_strlendup(t, p, strlen(p));
1532 strndup with a talloc
1534 char *talloc_strndup(const void *t, const char *p, size_t n)
1536 if (unlikely(!p)) return NULL;
1537 return __talloc_strlendup(t, p, strnlen(p, n));
1540 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1541 const char *a, size_t alen)
1543 char *ret;
1545 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1546 if (unlikely(!ret)) return NULL;
1548 /* append the string and the trailing \0 */
1549 memcpy(&ret[slen], a, alen);
1550 ret[slen+alen] = 0;
1552 _talloc_set_name_const(ret, ret);
1553 return ret;
1557 * Appends at the end of the string.
1559 char *talloc_strdup_append(char *s, const char *a)
1561 if (unlikely(!s)) {
1562 return talloc_strdup(NULL, a);
1565 if (unlikely(!a)) {
1566 return s;
1569 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1573 * Appends at the end of the talloc'ed buffer,
1574 * not the end of the string.
1576 char *talloc_strdup_append_buffer(char *s, const char *a)
1578 size_t slen;
1580 if (unlikely(!s)) {
1581 return talloc_strdup(NULL, a);
1584 if (unlikely(!a)) {
1585 return s;
1588 slen = talloc_get_size(s);
1589 if (likely(slen > 0)) {
1590 slen--;
1593 return __talloc_strlendup_append(s, slen, a, strlen(a));
1597 * Appends at the end of the string.
1599 char *talloc_strndup_append(char *s, const char *a, size_t n)
1601 if (unlikely(!s)) {
1602 return talloc_strdup(NULL, a);
1605 if (unlikely(!a)) {
1606 return s;
1609 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1613 * Appends at the end of the talloc'ed buffer,
1614 * not the end of the string.
1616 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1618 size_t slen;
1620 if (unlikely(!s)) {
1621 return talloc_strdup(NULL, a);
1624 if (unlikely(!a)) {
1625 return s;
1628 slen = talloc_get_size(s);
1629 if (likely(slen > 0)) {
1630 slen--;
1633 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1636 #ifndef HAVE_VA_COPY
1637 #ifdef HAVE___VA_COPY
1638 #define va_copy(dest, src) __va_copy(dest, src)
1639 #else
1640 #define va_copy(dest, src) (dest) = (src)
1641 #endif
1642 #endif
1644 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1646 int len;
1647 char *ret;
1648 va_list ap2;
1649 char c;
1651 /* this call looks strange, but it makes it work on older solaris boxes */
1652 va_copy(ap2, ap);
1653 len = vsnprintf(&c, 1, fmt, ap2);
1654 va_end(ap2);
1655 if (unlikely(len < 0)) {
1656 return NULL;
1659 ret = (char *)__talloc(t, len+1);
1660 if (unlikely(!ret)) return NULL;
1662 va_copy(ap2, ap);
1663 vsnprintf(ret, len+1, fmt, ap2);
1664 va_end(ap2);
1666 _talloc_set_name_const(ret, ret);
1667 return ret;
1672 Perform string formatting, and return a pointer to newly allocated
1673 memory holding the result, inside a memory pool.
1675 char *talloc_asprintf(const void *t, const char *fmt, ...)
1677 va_list ap;
1678 char *ret;
1680 va_start(ap, fmt);
1681 ret = talloc_vasprintf(t, fmt, ap);
1682 va_end(ap);
1683 return ret;
1686 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1687 const char *fmt, va_list ap)
1688 PRINTF_ATTRIBUTE(3,0);
1690 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1691 const char *fmt, va_list ap)
1693 ssize_t alen;
1694 va_list ap2;
1695 char c;
1697 va_copy(ap2, ap);
1698 alen = vsnprintf(&c, 1, fmt, ap2);
1699 va_end(ap2);
1701 if (alen <= 0) {
1702 /* Either the vsnprintf failed or the format resulted in
1703 * no characters being formatted. In the former case, we
1704 * ought to return NULL, in the latter we ought to return
1705 * the original string. Most current callers of this
1706 * function expect it to never return NULL.
1708 return s;
1711 s = talloc_realloc(NULL, s, char, slen + alen + 1);
1712 if (!s) return NULL;
1714 va_copy(ap2, ap);
1715 vsnprintf(s + slen, alen + 1, fmt, ap2);
1716 va_end(ap2);
1718 _talloc_set_name_const(s, s);
1719 return s;
1723 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1724 * and return @p s, which may have moved. Good for gradually
1725 * accumulating output into a string buffer. Appends at the end
1726 * of the string.
1728 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1730 if (unlikely(!s)) {
1731 return talloc_vasprintf(NULL, fmt, ap);
1734 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1738 * Realloc @p s to append the formatted result of @p fmt and @p ap,
1739 * and return @p s, which may have moved. Always appends at the
1740 * end of the talloc'ed buffer, not the end of the string.
1742 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1744 size_t slen;
1746 if (unlikely(!s)) {
1747 return talloc_vasprintf(NULL, fmt, ap);
1750 slen = talloc_get_size(s);
1751 if (likely(slen > 0)) {
1752 slen--;
1755 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1759 Realloc @p s to append the formatted result of @p fmt and return @p
1760 s, which may have moved. Good for gradually accumulating output
1761 into a string buffer.
1763 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1765 va_list ap;
1767 va_start(ap, fmt);
1768 s = talloc_vasprintf_append(s, fmt, ap);
1769 va_end(ap);
1770 return s;
1774 Realloc @p s to append the formatted result of @p fmt and return @p
1775 s, which may have moved. Good for gradually accumulating output
1776 into a buffer.
1778 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1780 va_list ap;
1782 va_start(ap, fmt);
1783 s = talloc_vasprintf_append_buffer(s, fmt, ap);
1784 va_end(ap);
1785 return s;
1789 alloc an array, checking for integer overflow in the array size
1791 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1793 if (count >= MAX_TALLOC_SIZE/el_size) {
1794 return NULL;
1796 return _talloc_named_const(ctx, el_size * count, name);
1800 alloc an zero array, checking for integer overflow in the array size
1802 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1804 if (count >= MAX_TALLOC_SIZE/el_size) {
1805 return NULL;
1807 return _talloc_zero(ctx, el_size * count, name);
1811 realloc an array, checking for integer overflow in the array size
1813 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1815 if (count >= MAX_TALLOC_SIZE/el_size) {
1816 return NULL;
1818 return _talloc_realloc(ctx, ptr, el_size * count, name);
1822 a function version of talloc_realloc(), so it can be passed as a function pointer
1823 to libraries that want a realloc function (a realloc function encapsulates
1824 all the basic capabilities of an allocation library, which is why this is useful)
1826 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1828 return _talloc_realloc(context, ptr, size, NULL);
1832 static int talloc_autofree_destructor(void *ptr)
1834 autofree_context = NULL;
1835 return 0;
1838 static void talloc_autofree(void)
1840 talloc_free(autofree_context);
1844 return a context which will be auto-freed on exit
1845 this is useful for reducing the noise in leak reports
1847 void *talloc_autofree_context(void)
1849 if (autofree_context == NULL) {
1850 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1851 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1852 atexit(talloc_autofree);
1854 return autofree_context;
1857 size_t talloc_get_size(const void *context)
1859 struct talloc_chunk *tc;
1861 if (context == NULL) {
1862 context = null_context;
1864 if (context == NULL) {
1865 return 0;
1868 tc = talloc_chunk_from_ptr(context);
1870 return tc->size;
1874 find a parent of this context that has the given name, if any
1876 void *talloc_find_parent_byname(const void *context, const char *name)
1878 struct talloc_chunk *tc;
1880 if (context == NULL) {
1881 return NULL;
1884 tc = talloc_chunk_from_ptr(context);
1885 while (tc) {
1886 if (tc->name && strcmp(tc->name, name) == 0) {
1887 return TC_PTR_FROM_CHUNK(tc);
1889 while (tc && tc->prev) tc = tc->prev;
1890 if (tc) {
1891 tc = tc->parent;
1894 return NULL;
1898 show the parentage of a context
1900 void talloc_show_parents(const void *context, FILE *file)
1902 struct talloc_chunk *tc;
1904 if (context == NULL) {
1905 fprintf(file, "talloc no parents for NULL\n");
1906 return;
1909 tc = talloc_chunk_from_ptr(context);
1910 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1911 while (tc) {
1912 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1913 while (tc && tc->prev) tc = tc->prev;
1914 if (tc) {
1915 tc = tc->parent;
1918 fflush(file);
1922 return 1 if ptr is a parent of context
1924 int talloc_is_parent(const void *context, const void *ptr)
1926 struct talloc_chunk *tc;
1928 if (context == NULL) {
1929 return 0;
1932 tc = talloc_chunk_from_ptr(context);
1933 while (tc) {
1934 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1935 while (tc && tc->prev) tc = tc->prev;
1936 if (tc) {
1937 tc = tc->parent;
1940 return 0;
1946 /* ABI compat functions (do NOT append anything beyond thess functions,
1947 * keep them as the last ones in the file) */
1949 static const char *talloc_ABI_compat_location = "Called from compatibility function";
1951 /* ABI compat function (don't use) */
1952 void *_talloc_reference(const void *context, const void *ptr) {
1953 return _talloc_reference_loc(context, ptr, talloc_ABI_compat_location);
1956 /* ABI compat function (don't use) */
1957 void *_talloc_steal(const void *new_ctx, const void *ptr)
1959 return _talloc_steal_internal(new_ctx, ptr);
1962 #undef talloc_free
1963 int talloc_free(void *ptr);
1964 int talloc_free(void *ptr)
1966 return _talloc_free_internal(ptr);
1969 /* DO NOT APPEND ANYTHING BEYOND THIS POINT */