s4-dsdb: rework the NC detection for the descriptor calculation
[Samba/gbeck.git] / lib / talloc / talloc.c
blob2098a329031866c60184683c883f4edf0b5e45ed
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 #include "replace.h"
34 #include "talloc.h"
36 #ifdef TALLOC_BUILD_VERSION_MAJOR
37 #if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)
38 #error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"
39 #endif
40 #endif
42 #ifdef TALLOC_BUILD_VERSION_MINOR
43 #if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)
44 #error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"
45 #endif
46 #endif
48 /* Special macros that are no-ops except when run under Valgrind on
49 * x86. They've moved a little bit from valgrind 1.0.4 to 1.9.4 */
50 #ifdef HAVE_VALGRIND_MEMCHECK_H
51 /* memcheck.h includes valgrind.h */
52 #include <valgrind/memcheck.h>
53 #elif defined(HAVE_VALGRIND_H)
54 #include <valgrind.h>
55 #endif
57 /* use this to force every realloc to change the pointer, to stress test
58 code that might not cope */
59 #define ALWAYS_REALLOC 0
62 #define MAX_TALLOC_SIZE 0x10000000
63 #define TALLOC_MAGIC_BASE 0xe814ec70
64 #define TALLOC_MAGIC ( \
65 TALLOC_MAGIC_BASE + \
66 (TALLOC_VERSION_MAJOR << 12) + \
67 (TALLOC_VERSION_MINOR << 4) \
70 #define TALLOC_FLAG_FREE 0x01
71 #define TALLOC_FLAG_LOOP 0x02
72 #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
73 #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
74 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
76 /* by default we abort when given a bad pointer (such as when talloc_free() is called
77 on a pointer that came from malloc() */
78 #ifndef TALLOC_ABORT
79 #define TALLOC_ABORT(reason) abort()
80 #endif
82 #ifndef discard_const_p
83 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
84 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
85 #else
86 # define discard_const_p(type, ptr) ((type *)(ptr))
87 #endif
88 #endif
90 /* these macros gain us a few percent of speed on gcc */
91 #if (__GNUC__ >= 3)
92 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
93 as its first argument */
94 #ifndef likely
95 #define likely(x) __builtin_expect(!!(x), 1)
96 #endif
97 #ifndef unlikely
98 #define unlikely(x) __builtin_expect(!!(x), 0)
99 #endif
100 #else
101 #ifndef likely
102 #define likely(x) (x)
103 #endif
104 #ifndef unlikely
105 #define unlikely(x) (x)
106 #endif
107 #endif
109 /* this null_context is only used if talloc_enable_leak_report() or
110 talloc_enable_leak_report_full() is called, otherwise it remains
111 NULL
113 static void *null_context;
114 static void *autofree_context;
116 /* used to enable fill of memory on free, which can be useful for
117 * catching use after free errors when valgrind is too slow
119 static struct {
120 bool initialised;
121 bool enabled;
122 uint8_t fill_value;
123 } talloc_fill;
125 #define TALLOC_FILL_ENV "TALLOC_FREE_FILL"
128 * do not wipe the header, to allow the
129 * double-free logic to still work
131 #define TC_INVALIDATE_FULL_FILL_CHUNK(_tc) do { \
132 if (unlikely(talloc_fill.enabled)) { \
133 size_t _flen = (_tc)->size; \
134 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
135 memset(_fptr, talloc_fill.fill_value, _flen); \
137 } while (0)
139 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
140 /* Mark the whole chunk as not accessable */
141 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { \
142 size_t _flen = TC_HDR_SIZE + (_tc)->size; \
143 char *_fptr = (char *)(_tc); \
144 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
145 } while(0)
146 #else
147 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { } while (0)
148 #endif
150 #define TC_INVALIDATE_FULL_CHUNK(_tc) do { \
151 TC_INVALIDATE_FULL_FILL_CHUNK(_tc); \
152 TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc); \
153 } while (0)
155 #define TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
156 if (unlikely(talloc_fill.enabled)) { \
157 size_t _flen = (_tc)->size - (_new_size); \
158 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
159 _fptr += (_new_size); \
160 memset(_fptr, talloc_fill.fill_value, _flen); \
162 } while (0)
164 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
165 /* Mark the unused bytes not accessable */
166 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
167 size_t _flen = (_tc)->size - (_new_size); \
168 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
169 _fptr += (_new_size); \
170 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
171 } while (0)
172 #else
173 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
174 #endif
176 #define TC_INVALIDATE_SHRINK_CHUNK(_tc, _new_size) do { \
177 TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size); \
178 TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
179 } while (0)
181 #define TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
182 if (unlikely(talloc_fill.enabled)) { \
183 size_t _flen = (_tc)->size - (_new_size); \
184 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
185 _fptr += (_new_size); \
186 memset(_fptr, talloc_fill.fill_value, _flen); \
188 } while (0)
190 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
191 /* Mark the unused bytes as undefined */
192 #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
193 size_t _flen = (_tc)->size - (_new_size); \
194 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
195 _fptr += (_new_size); \
196 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
197 } while (0)
198 #else
199 #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
200 #endif
202 #define TC_UNDEFINE_SHRINK_CHUNK(_tc, _new_size) do { \
203 TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size); \
204 TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
205 } while (0)
207 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
208 /* Mark the new bytes as undefined */
209 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { \
210 size_t _old_used = TC_HDR_SIZE + (_tc)->size; \
211 size_t _new_used = TC_HDR_SIZE + (_new_size); \
212 size_t _flen = _new_used - _old_used; \
213 char *_fptr = _old_used + (char *)(_tc); \
214 VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
215 } while (0)
216 #else
217 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
218 #endif
220 #define TC_UNDEFINE_GROW_CHUNK(_tc, _new_size) do { \
221 TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
222 } while (0)
224 struct talloc_reference_handle {
225 struct talloc_reference_handle *next, *prev;
226 void *ptr;
227 const char *location;
230 typedef int (*talloc_destructor_t)(void *);
232 struct talloc_chunk {
233 struct talloc_chunk *next, *prev;
234 struct talloc_chunk *parent, *child;
235 struct talloc_reference_handle *refs;
236 talloc_destructor_t destructor;
237 const char *name;
238 size_t size;
239 unsigned flags;
242 * "pool" has dual use:
244 * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
245 * marks the end of the currently allocated area.
247 * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
248 * is a pointer to the struct talloc_chunk of the pool that it was
249 * allocated from. This way children can quickly find the pool to chew
250 * from.
252 void *pool;
255 /* 16 byte alignment seems to keep everyone happy */
256 #define TC_ALIGN16(s) (((s)+15)&~15)
257 #define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
258 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
260 _PUBLIC_ int talloc_version_major(void)
262 return TALLOC_VERSION_MAJOR;
265 _PUBLIC_ int talloc_version_minor(void)
267 return TALLOC_VERSION_MINOR;
270 static void (*talloc_log_fn)(const char *message);
272 _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
274 talloc_log_fn = log_fn;
277 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
278 static void talloc_log(const char *fmt, ...)
280 va_list ap;
281 char *message;
283 if (!talloc_log_fn) {
284 return;
287 va_start(ap, fmt);
288 message = talloc_vasprintf(NULL, fmt, ap);
289 va_end(ap);
291 talloc_log_fn(message);
292 talloc_free(message);
295 static void talloc_log_stderr(const char *message)
297 fprintf(stderr, "%s", message);
300 _PUBLIC_ void talloc_set_log_stderr(void)
302 talloc_set_log_fn(talloc_log_stderr);
305 static void (*talloc_abort_fn)(const char *reason);
307 _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
309 talloc_abort_fn = abort_fn;
312 static void talloc_abort(const char *reason)
314 talloc_log("%s\n", reason);
316 if (!talloc_abort_fn) {
317 TALLOC_ABORT(reason);
320 talloc_abort_fn(reason);
323 static void talloc_abort_magic(unsigned magic)
325 unsigned striped = magic - TALLOC_MAGIC_BASE;
326 unsigned major = (striped & 0xFFFFF000) >> 12;
327 unsigned minor = (striped & 0x00000FF0) >> 4;
328 talloc_log("Bad talloc magic[0x%08X/%u/%u] expected[0x%08X/%u/%u]\n",
329 magic, major, minor,
330 TALLOC_MAGIC, TALLOC_VERSION_MAJOR, TALLOC_VERSION_MINOR);
331 talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
334 static void talloc_abort_access_after_free(void)
336 talloc_abort("Bad talloc magic value - access after free");
339 static void talloc_abort_unknown_value(void)
341 talloc_abort("Bad talloc magic value - unknown value");
344 /* panic if we get a bad magic value */
345 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
347 const char *pp = (const char *)ptr;
348 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
349 if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
350 if ((tc->flags & (~0xFFF)) == TALLOC_MAGIC_BASE) {
351 talloc_abort_magic(tc->flags & (~0xF));
352 return NULL;
355 if (tc->flags & TALLOC_FLAG_FREE) {
356 talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
357 talloc_abort_access_after_free();
358 return NULL;
359 } else {
360 talloc_abort_unknown_value();
361 return NULL;
364 return tc;
367 /* hook into the front of the list */
368 #define _TLIST_ADD(list, p) \
369 do { \
370 if (!(list)) { \
371 (list) = (p); \
372 (p)->next = (p)->prev = NULL; \
373 } else { \
374 (list)->prev = (p); \
375 (p)->next = (list); \
376 (p)->prev = NULL; \
377 (list) = (p); \
379 } while (0)
381 /* remove an element from a list - element doesn't have to be in list. */
382 #define _TLIST_REMOVE(list, p) \
383 do { \
384 if ((p) == (list)) { \
385 (list) = (p)->next; \
386 if (list) (list)->prev = NULL; \
387 } else { \
388 if ((p)->prev) (p)->prev->next = (p)->next; \
389 if ((p)->next) (p)->next->prev = (p)->prev; \
391 if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
392 } while (0)
396 return the parent chunk of a pointer
398 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
400 struct talloc_chunk *tc;
402 if (unlikely(ptr == NULL)) {
403 return NULL;
406 tc = talloc_chunk_from_ptr(ptr);
407 while (tc->prev) tc=tc->prev;
409 return tc->parent;
412 _PUBLIC_ void *talloc_parent(const void *ptr)
414 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
415 return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
419 find parents name
421 _PUBLIC_ const char *talloc_parent_name(const void *ptr)
423 struct talloc_chunk *tc = talloc_parent_chunk(ptr);
424 return tc? tc->name : NULL;
428 A pool carries an in-pool object count count in the first 16 bytes.
429 bytes. This is done to support talloc_steal() to a parent outside of the
430 pool. The count includes the pool itself, so a talloc_free() on a pool will
431 only destroy the pool if the count has dropped to zero. A talloc_free() of a
432 pool member will reduce the count, and eventually also call free(3) on the
433 pool memory.
435 The object count is not put into "struct talloc_chunk" because it is only
436 relevant for talloc pools and the alignment to 16 bytes would increase the
437 memory footprint of each talloc chunk by those 16 bytes.
440 #define TALLOC_POOL_HDR_SIZE 16
442 #define TC_POOL_SPACE_LEFT(_pool_tc) \
443 PTR_DIFF(TC_HDR_SIZE + (_pool_tc)->size + (char *)(_pool_tc), \
444 (_pool_tc)->pool)
446 #define TC_POOL_FIRST_CHUNK(_pool_tc) \
447 ((void *)(TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE + (char *)(_pool_tc)))
449 #define TC_POOLMEM_CHUNK_SIZE(_tc) \
450 TC_ALIGN16(TC_HDR_SIZE + (_tc)->size)
452 #define TC_POOLMEM_NEXT_CHUNK(_tc) \
453 ((void *)(TC_POOLMEM_CHUNK_SIZE(tc) + (char*)(_tc)))
455 /* Mark the whole remaining pool as not accessable */
456 #define TC_INVALIDATE_FILL_POOL(_pool_tc) do { \
457 if (unlikely(talloc_fill.enabled)) { \
458 size_t _flen = TC_POOL_SPACE_LEFT(_pool_tc); \
459 char *_fptr = (char *)(_pool_tc)->pool; \
460 memset(_fptr, talloc_fill.fill_value, _flen); \
462 } while(0)
464 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
465 /* Mark the whole remaining pool as not accessable */
466 #define TC_INVALIDATE_VALGRIND_POOL(_pool_tc) do { \
467 size_t _flen = TC_POOL_SPACE_LEFT(_pool_tc); \
468 char *_fptr = (char *)(_pool_tc)->pool; \
469 VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
470 } while(0)
471 #else
472 #define TC_INVALIDATE_VALGRIND_POOL(_pool_tc) do { } while (0)
473 #endif
475 #define TC_INVALIDATE_POOL(_pool_tc) do { \
476 TC_INVALIDATE_FILL_POOL(_pool_tc); \
477 TC_INVALIDATE_VALGRIND_POOL(_pool_tc); \
478 } while (0)
480 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
482 return (unsigned int *)((char *)tc + TC_HDR_SIZE);
486 Allocate from a pool
489 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
490 size_t size)
492 struct talloc_chunk *pool_ctx = NULL;
493 size_t space_left;
494 struct talloc_chunk *result;
495 size_t chunk_size;
497 if (parent == NULL) {
498 return NULL;
501 if (parent->flags & TALLOC_FLAG_POOL) {
502 pool_ctx = parent;
504 else if (parent->flags & TALLOC_FLAG_POOLMEM) {
505 pool_ctx = (struct talloc_chunk *)parent->pool;
508 if (pool_ctx == NULL) {
509 return NULL;
512 space_left = TC_POOL_SPACE_LEFT(pool_ctx);
515 * Align size to 16 bytes
517 chunk_size = TC_ALIGN16(size);
519 if (space_left < chunk_size) {
520 return NULL;
523 result = (struct talloc_chunk *)pool_ctx->pool;
525 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
526 VALGRIND_MAKE_MEM_UNDEFINED(result, size);
527 #endif
529 pool_ctx->pool = (void *)((char *)result + chunk_size);
531 result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
532 result->pool = pool_ctx;
534 *talloc_pool_objectcount(pool_ctx) += 1;
536 return result;
540 Allocate a bit of memory as a child of an existing pointer
542 static inline void *__talloc(const void *context, size_t size)
544 struct talloc_chunk *tc = NULL;
546 if (unlikely(context == NULL)) {
547 context = null_context;
550 if (unlikely(size >= MAX_TALLOC_SIZE)) {
551 return NULL;
554 if (context != NULL) {
555 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
556 TC_HDR_SIZE+size);
559 if (tc == NULL) {
560 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
561 if (unlikely(tc == NULL)) return NULL;
562 tc->flags = TALLOC_MAGIC;
563 tc->pool = NULL;
566 tc->size = size;
567 tc->destructor = NULL;
568 tc->child = NULL;
569 tc->name = NULL;
570 tc->refs = NULL;
572 if (likely(context)) {
573 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
575 if (parent->child) {
576 parent->child->parent = NULL;
577 tc->next = parent->child;
578 tc->next->prev = tc;
579 } else {
580 tc->next = NULL;
582 tc->parent = parent;
583 tc->prev = NULL;
584 parent->child = tc;
585 } else {
586 tc->next = tc->prev = tc->parent = NULL;
589 return TC_PTR_FROM_CHUNK(tc);
593 * Create a talloc pool
596 _PUBLIC_ void *talloc_pool(const void *context, size_t size)
598 void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
599 struct talloc_chunk *tc;
601 if (unlikely(result == NULL)) {
602 return NULL;
605 tc = talloc_chunk_from_ptr(result);
607 tc->flags |= TALLOC_FLAG_POOL;
608 tc->pool = TC_POOL_FIRST_CHUNK(tc);
610 *talloc_pool_objectcount(tc) = 1;
612 TC_INVALIDATE_POOL(tc);
614 return result;
618 setup a destructor to be called on free of a pointer
619 the destructor should return 0 on success, or -1 on failure.
620 if the destructor fails then the free is failed, and the memory can
621 be continued to be used
623 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
625 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
626 tc->destructor = destructor;
630 increase the reference count on a piece of memory.
632 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
634 if (unlikely(!talloc_reference(null_context, ptr))) {
635 return -1;
637 return 0;
641 helper for talloc_reference()
643 this is referenced by a function pointer and should not be inline
645 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
647 struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
648 _TLIST_REMOVE(ptr_tc->refs, handle);
649 return 0;
653 more efficient way to add a name to a pointer - the name must point to a
654 true string constant
656 static inline void _talloc_set_name_const(const void *ptr, const char *name)
658 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
659 tc->name = name;
663 internal talloc_named_const()
665 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
667 void *ptr;
669 ptr = __talloc(context, size);
670 if (unlikely(ptr == NULL)) {
671 return NULL;
674 _talloc_set_name_const(ptr, name);
676 return ptr;
680 make a secondary reference to a pointer, hanging off the given context.
681 the pointer remains valid until both the original caller and this given
682 context are freed.
684 the major use for this is when two different structures need to reference the
685 same underlying data, and you want to be able to free the two instances separately,
686 and in either order
688 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
690 struct talloc_chunk *tc;
691 struct talloc_reference_handle *handle;
692 if (unlikely(ptr == NULL)) return NULL;
694 tc = talloc_chunk_from_ptr(ptr);
695 handle = (struct talloc_reference_handle *)_talloc_named_const(context,
696 sizeof(struct talloc_reference_handle),
697 TALLOC_MAGIC_REFERENCE);
698 if (unlikely(handle == NULL)) return NULL;
700 /* note that we hang the destructor off the handle, not the
701 main context as that allows the caller to still setup their
702 own destructor on the context if they want to */
703 talloc_set_destructor(handle, talloc_reference_destructor);
704 handle->ptr = discard_const_p(void, ptr);
705 handle->location = location;
706 _TLIST_ADD(tc->refs, handle);
707 return handle->ptr;
710 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
712 static inline void _talloc_free_poolmem(struct talloc_chunk *tc,
713 const char *location)
715 struct talloc_chunk *pool;
716 void *next_tc;
717 unsigned int *pool_object_count;
719 pool = (struct talloc_chunk *)tc->pool;
720 next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
722 tc->flags |= TALLOC_FLAG_FREE;
724 /* we mark the freed memory with where we called the free
725 * from. This means on a double free error we can report where
726 * the first free came from
728 tc->name = location;
730 TC_INVALIDATE_FULL_CHUNK(tc);
732 pool_object_count = talloc_pool_objectcount(pool);
734 if (unlikely(*pool_object_count == 0)) {
735 talloc_abort("Pool object count zero!");
736 return;
739 *pool_object_count -= 1;
741 if (unlikely(*pool_object_count == 1 && !(pool->flags & TALLOC_FLAG_FREE))) {
743 * if there is just one object left in the pool
744 * and pool->flags does not have TALLOC_FLAG_FREE,
745 * it means this is the pool itself and
746 * the rest is available for new objects
747 * again.
749 pool->pool = TC_POOL_FIRST_CHUNK(pool);
750 TC_INVALIDATE_POOL(pool);
751 } else if (unlikely(*pool_object_count == 0)) {
753 * we mark the freed memory with where we called the free
754 * from. This means on a double free error we can report where
755 * the first free came from
757 pool->name = location;
759 TC_INVALIDATE_FULL_CHUNK(pool);
760 free(pool);
761 } else if (pool->pool == next_tc) {
763 * if pool->pool still points to end of
764 * 'tc' (which is stored in the 'next_tc' variable),
765 * we can reclaim the memory of 'tc'.
767 pool->pool = tc;
771 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
772 void *ptr,
773 const char *location);
776 internal talloc_free call
778 static inline int _talloc_free_internal(void *ptr, const char *location)
780 struct talloc_chunk *tc;
782 if (unlikely(ptr == NULL)) {
783 return -1;
786 /* possibly initialised the talloc fill value */
787 if (unlikely(!talloc_fill.initialised)) {
788 const char *fill = getenv(TALLOC_FILL_ENV);
789 if (fill != NULL) {
790 talloc_fill.enabled = true;
791 talloc_fill.fill_value = strtoul(fill, NULL, 0);
793 talloc_fill.initialised = true;
796 tc = talloc_chunk_from_ptr(ptr);
798 if (unlikely(tc->refs)) {
799 int is_child;
800 /* check if this is a reference from a child or
801 * grandchild back to it's parent or grandparent
803 * in that case we need to remove the reference and
804 * call another instance of talloc_free() on the current
805 * pointer.
807 is_child = talloc_is_parent(tc->refs, ptr);
808 _talloc_free_internal(tc->refs, location);
809 if (is_child) {
810 return _talloc_free_internal(ptr, location);
812 return -1;
815 if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
816 /* we have a free loop - stop looping */
817 return 0;
820 if (unlikely(tc->destructor)) {
821 talloc_destructor_t d = tc->destructor;
822 if (d == (talloc_destructor_t)-1) {
823 return -1;
825 tc->destructor = (talloc_destructor_t)-1;
826 if (d(ptr) == -1) {
827 tc->destructor = d;
828 return -1;
830 tc->destructor = NULL;
833 if (tc->parent) {
834 _TLIST_REMOVE(tc->parent->child, tc);
835 if (tc->parent->child) {
836 tc->parent->child->parent = tc->parent;
838 } else {
839 if (tc->prev) tc->prev->next = tc->next;
840 if (tc->next) tc->next->prev = tc->prev;
841 tc->prev = tc->next = NULL;
844 tc->flags |= TALLOC_FLAG_LOOP;
846 _talloc_free_children_internal(tc, ptr, location);
848 tc->flags |= TALLOC_FLAG_FREE;
850 /* we mark the freed memory with where we called the free
851 * from. This means on a double free error we can report where
852 * the first free came from
854 tc->name = location;
856 if (tc->flags & TALLOC_FLAG_POOL) {
857 unsigned int *pool_object_count;
859 pool_object_count = talloc_pool_objectcount(tc);
861 if (unlikely(*pool_object_count == 0)) {
862 talloc_abort("Pool object count zero!");
863 return 0;
866 *pool_object_count -= 1;
868 if (unlikely(*pool_object_count == 0)) {
869 TC_INVALIDATE_FULL_CHUNK(tc);
870 free(tc);
872 } else if (tc->flags & TALLOC_FLAG_POOLMEM) {
873 _talloc_free_poolmem(tc, location);
874 } else {
875 TC_INVALIDATE_FULL_CHUNK(tc);
876 free(tc);
878 return 0;
882 move a lump of memory from one talloc context to another return the
883 ptr on success, or NULL if it could not be transferred.
884 passing NULL as ptr will always return NULL with no side effects.
886 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
888 struct talloc_chunk *tc, *new_tc;
890 if (unlikely(!ptr)) {
891 return NULL;
894 if (unlikely(new_ctx == NULL)) {
895 new_ctx = null_context;
898 tc = talloc_chunk_from_ptr(ptr);
900 if (unlikely(new_ctx == NULL)) {
901 if (tc->parent) {
902 _TLIST_REMOVE(tc->parent->child, tc);
903 if (tc->parent->child) {
904 tc->parent->child->parent = tc->parent;
906 } else {
907 if (tc->prev) tc->prev->next = tc->next;
908 if (tc->next) tc->next->prev = tc->prev;
911 tc->parent = tc->next = tc->prev = NULL;
912 return discard_const_p(void, ptr);
915 new_tc = talloc_chunk_from_ptr(new_ctx);
917 if (unlikely(tc == new_tc || tc->parent == new_tc)) {
918 return discard_const_p(void, ptr);
921 if (tc->parent) {
922 _TLIST_REMOVE(tc->parent->child, tc);
923 if (tc->parent->child) {
924 tc->parent->child->parent = tc->parent;
926 } else {
927 if (tc->prev) tc->prev->next = tc->next;
928 if (tc->next) tc->next->prev = tc->prev;
929 tc->prev = tc->next = NULL;
932 tc->parent = new_tc;
933 if (new_tc->child) new_tc->child->parent = NULL;
934 _TLIST_ADD(new_tc->child, tc);
936 return discard_const_p(void, ptr);
940 move a lump of memory from one talloc context to another return the
941 ptr on success, or NULL if it could not be transferred.
942 passing NULL as ptr will always return NULL with no side effects.
944 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
946 struct talloc_chunk *tc;
948 if (unlikely(ptr == NULL)) {
949 return NULL;
952 tc = talloc_chunk_from_ptr(ptr);
954 if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
955 struct talloc_reference_handle *h;
957 talloc_log("WARNING: talloc_steal with references at %s\n",
958 location);
960 for (h=tc->refs; h; h=h->next) {
961 talloc_log("\treference at %s\n",
962 h->location);
966 #if 0
967 /* this test is probably too expensive to have on in the
968 normal build, but it useful for debugging */
969 if (talloc_is_parent(new_ctx, ptr)) {
970 talloc_log("WARNING: stealing into talloc child at %s\n", location);
972 #endif
974 return _talloc_steal_internal(new_ctx, ptr);
978 this is like a talloc_steal(), but you must supply the old
979 parent. This resolves the ambiguity in a talloc_steal() which is
980 called on a context that has more than one parent (via references)
982 The old parent can be either a reference or a parent
984 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
986 struct talloc_chunk *tc;
987 struct talloc_reference_handle *h;
989 if (unlikely(ptr == NULL)) {
990 return NULL;
993 if (old_parent == talloc_parent(ptr)) {
994 return _talloc_steal_internal(new_parent, ptr);
997 tc = talloc_chunk_from_ptr(ptr);
998 for (h=tc->refs;h;h=h->next) {
999 if (talloc_parent(h) == old_parent) {
1000 if (_talloc_steal_internal(new_parent, h) != h) {
1001 return NULL;
1003 return discard_const_p(void, ptr);
1007 /* it wasn't a parent */
1008 return NULL;
1012 remove a secondary reference to a pointer. This undo's what
1013 talloc_reference() has done. The context and pointer arguments
1014 must match those given to a talloc_reference()
1016 static inline int talloc_unreference(const void *context, const void *ptr)
1018 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1019 struct talloc_reference_handle *h;
1021 if (unlikely(context == NULL)) {
1022 context = null_context;
1025 for (h=tc->refs;h;h=h->next) {
1026 struct talloc_chunk *p = talloc_parent_chunk(h);
1027 if (p == NULL) {
1028 if (context == NULL) break;
1029 } else if (TC_PTR_FROM_CHUNK(p) == context) {
1030 break;
1033 if (h == NULL) {
1034 return -1;
1037 return _talloc_free_internal(h, __location__);
1041 remove a specific parent context from a pointer. This is a more
1042 controlled variant of talloc_free()
1044 _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
1046 struct talloc_chunk *tc_p, *new_p;
1047 void *new_parent;
1049 if (ptr == NULL) {
1050 return -1;
1053 if (context == NULL) {
1054 context = null_context;
1057 if (talloc_unreference(context, ptr) == 0) {
1058 return 0;
1061 if (context == NULL) {
1062 if (talloc_parent_chunk(ptr) != NULL) {
1063 return -1;
1065 } else {
1066 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
1067 return -1;
1071 tc_p = talloc_chunk_from_ptr(ptr);
1073 if (tc_p->refs == NULL) {
1074 return _talloc_free_internal(ptr, __location__);
1077 new_p = talloc_parent_chunk(tc_p->refs);
1078 if (new_p) {
1079 new_parent = TC_PTR_FROM_CHUNK(new_p);
1080 } else {
1081 new_parent = NULL;
1084 if (talloc_unreference(new_parent, ptr) != 0) {
1085 return -1;
1088 _talloc_steal_internal(new_parent, ptr);
1090 return 0;
1094 add a name to an existing pointer - va_list version
1096 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1098 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
1100 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1101 tc->name = talloc_vasprintf(ptr, fmt, ap);
1102 if (likely(tc->name)) {
1103 _talloc_set_name_const(tc->name, ".name");
1105 return tc->name;
1109 add a name to an existing pointer
1111 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
1113 const char *name;
1114 va_list ap;
1115 va_start(ap, fmt);
1116 name = talloc_set_name_v(ptr, fmt, ap);
1117 va_end(ap);
1118 return name;
1123 create a named talloc pointer. Any talloc pointer can be named, and
1124 talloc_named() operates just like talloc() except that it allows you
1125 to name the pointer.
1127 _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
1129 va_list ap;
1130 void *ptr;
1131 const char *name;
1133 ptr = __talloc(context, size);
1134 if (unlikely(ptr == NULL)) return NULL;
1136 va_start(ap, fmt);
1137 name = talloc_set_name_v(ptr, fmt, ap);
1138 va_end(ap);
1140 if (unlikely(name == NULL)) {
1141 _talloc_free_internal(ptr, __location__);
1142 return NULL;
1145 return ptr;
1149 return the name of a talloc ptr, or "UNNAMED"
1151 _PUBLIC_ const char *talloc_get_name(const void *ptr)
1153 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1154 if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
1155 return ".reference";
1157 if (likely(tc->name)) {
1158 return tc->name;
1160 return "UNNAMED";
1165 check if a pointer has the given name. If it does, return the pointer,
1166 otherwise return NULL
1168 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
1170 const char *pname;
1171 if (unlikely(ptr == NULL)) return NULL;
1172 pname = talloc_get_name(ptr);
1173 if (likely(pname == name || strcmp(pname, name) == 0)) {
1174 return discard_const_p(void, ptr);
1176 return NULL;
1179 static void talloc_abort_type_missmatch(const char *location,
1180 const char *name,
1181 const char *expected)
1183 const char *reason;
1185 reason = talloc_asprintf(NULL,
1186 "%s: Type mismatch: name[%s] expected[%s]",
1187 location,
1188 name?name:"NULL",
1189 expected);
1190 if (!reason) {
1191 reason = "Type mismatch";
1194 talloc_abort(reason);
1197 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
1199 const char *pname;
1201 if (unlikely(ptr == NULL)) {
1202 talloc_abort_type_missmatch(location, NULL, name);
1203 return NULL;
1206 pname = talloc_get_name(ptr);
1207 if (likely(pname == name || strcmp(pname, name) == 0)) {
1208 return discard_const_p(void, ptr);
1211 talloc_abort_type_missmatch(location, pname, name);
1212 return NULL;
1216 this is for compatibility with older versions of talloc
1218 _PUBLIC_ void *talloc_init(const char *fmt, ...)
1220 va_list ap;
1221 void *ptr;
1222 const char *name;
1224 ptr = __talloc(NULL, 0);
1225 if (unlikely(ptr == NULL)) return NULL;
1227 va_start(ap, fmt);
1228 name = talloc_set_name_v(ptr, fmt, ap);
1229 va_end(ap);
1231 if (unlikely(name == NULL)) {
1232 _talloc_free_internal(ptr, __location__);
1233 return NULL;
1236 return ptr;
1239 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
1240 void *ptr,
1241 const char *location)
1243 while (tc->child) {
1244 /* we need to work out who will own an abandoned child
1245 if it cannot be freed. In priority order, the first
1246 choice is owner of any remaining reference to this
1247 pointer, the second choice is our parent, and the
1248 final choice is the null context. */
1249 void *child = TC_PTR_FROM_CHUNK(tc->child);
1250 const void *new_parent = null_context;
1251 if (unlikely(tc->child->refs)) {
1252 struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1253 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1255 if (unlikely(_talloc_free_internal(child, location) == -1)) {
1256 if (new_parent == null_context) {
1257 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1258 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1260 _talloc_steal_internal(new_parent, child);
1266 this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1267 should probably not be used in new code. It's in here to keep the talloc
1268 code consistent across Samba 3 and 4.
1270 _PUBLIC_ void talloc_free_children(void *ptr)
1272 struct talloc_chunk *tc_name = NULL;
1273 struct talloc_chunk *tc;
1275 if (unlikely(ptr == NULL)) {
1276 return;
1279 tc = talloc_chunk_from_ptr(ptr);
1281 /* we do not want to free the context name if it is a child .. */
1282 if (likely(tc->child)) {
1283 for (tc_name = tc->child; tc_name; tc_name = tc_name->next) {
1284 if (tc->name == TC_PTR_FROM_CHUNK(tc_name)) break;
1286 if (tc_name) {
1287 _TLIST_REMOVE(tc->child, tc_name);
1288 if (tc->child) {
1289 tc->child->parent = tc;
1294 _talloc_free_children_internal(tc, ptr, __location__);
1296 /* .. so we put it back after all other children have been freed */
1297 if (tc_name) {
1298 if (tc->child) {
1299 tc->child->parent = NULL;
1301 tc_name->parent = tc;
1302 _TLIST_ADD(tc->child, tc_name);
1307 Allocate a bit of memory as a child of an existing pointer
1309 _PUBLIC_ void *_talloc(const void *context, size_t size)
1311 return __talloc(context, size);
1315 externally callable talloc_set_name_const()
1317 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1319 _talloc_set_name_const(ptr, name);
1323 create a named talloc pointer. Any talloc pointer can be named, and
1324 talloc_named() operates just like talloc() except that it allows you
1325 to name the pointer.
1327 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1329 return _talloc_named_const(context, size, name);
1333 free a talloc pointer. This also frees all child pointers of this
1334 pointer recursively
1336 return 0 if the memory is actually freed, otherwise -1. The memory
1337 will not be freed if the ref_count is > 1 or the destructor (if
1338 any) returns non-zero
1340 _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1342 struct talloc_chunk *tc;
1344 if (unlikely(ptr == NULL)) {
1345 return -1;
1348 tc = talloc_chunk_from_ptr(ptr);
1350 if (unlikely(tc->refs != NULL)) {
1351 struct talloc_reference_handle *h;
1353 if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1354 /* in this case we do know which parent should
1355 get this pointer, as there is really only
1356 one parent */
1357 return talloc_unlink(null_context, ptr);
1360 talloc_log("ERROR: talloc_free with references at %s\n",
1361 location);
1363 for (h=tc->refs; h; h=h->next) {
1364 talloc_log("\treference at %s\n",
1365 h->location);
1367 return -1;
1370 return _talloc_free_internal(ptr, location);
1376 A talloc version of realloc. The context argument is only used if
1377 ptr is NULL
1379 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1381 struct talloc_chunk *tc;
1382 void *new_ptr;
1383 bool malloced = false;
1384 struct talloc_chunk *pool_tc = NULL;
1386 /* size zero is equivalent to free() */
1387 if (unlikely(size == 0)) {
1388 talloc_unlink(context, ptr);
1389 return NULL;
1392 if (unlikely(size >= MAX_TALLOC_SIZE)) {
1393 return NULL;
1396 /* realloc(NULL) is equivalent to malloc() */
1397 if (ptr == NULL) {
1398 return _talloc_named_const(context, size, name);
1401 tc = talloc_chunk_from_ptr(ptr);
1403 /* don't allow realloc on referenced pointers */
1404 if (unlikely(tc->refs)) {
1405 return NULL;
1408 /* don't let anybody try to realloc a talloc_pool */
1409 if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1410 return NULL;
1413 /* don't let anybody try to realloc a talloc_pool */
1414 if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
1415 pool_tc = (struct talloc_chunk *)tc->pool;
1418 #if (ALWAYS_REALLOC == 0)
1419 /* don't shrink if we have less than 1k to gain */
1420 if (size < tc->size) {
1421 if (pool_tc) {
1422 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1423 TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1424 tc->size = size;
1425 if (next_tc == pool_tc->pool) {
1426 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1428 return ptr;
1429 } else if ((tc->size - size) < 1024) {
1431 * if we call TC_INVALIDATE_SHRINK_CHUNK() here
1432 * we would need to call TC_UNDEFINE_GROW_CHUNK()
1433 * after each realloc call, which slows down
1434 * testing a lot :-(.
1436 * That is why we only mark memory as undefined here.
1438 TC_UNDEFINE_SHRINK_CHUNK(tc, size);
1440 /* do not shrink if we have less than 1k to gain */
1441 tc->size = size;
1442 return ptr;
1444 } else if (tc->size == size) {
1446 * do not change the pointer if it is exactly
1447 * the same size.
1449 return ptr;
1451 #endif
1453 /* by resetting magic we catch users of the old memory */
1454 tc->flags |= TALLOC_FLAG_FREE;
1456 #if ALWAYS_REALLOC
1457 if (pool_tc) {
1458 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1459 *talloc_pool_objectcount(pool_tc) -= 1;
1461 if (new_ptr == NULL) {
1462 new_ptr = malloc(TC_HDR_SIZE+size);
1463 malloced = true;
1466 if (new_ptr) {
1467 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1468 TC_INVALIDATE_FULL_CHUNK(tc);
1470 } else {
1471 new_ptr = malloc(size + TC_HDR_SIZE);
1472 if (new_ptr) {
1473 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1474 free(tc);
1477 #else
1478 if (pool_tc) {
1479 void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
1480 size_t old_chunk_size = TC_POOLMEM_CHUNK_SIZE(tc);
1481 size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
1482 size_t space_needed;
1483 size_t space_left;
1484 unsigned int chunk_count = *talloc_pool_objectcount(pool_tc);
1486 if (!(pool_tc->flags & TALLOC_FLAG_FREE)) {
1487 chunk_count -= 1;
1490 if (chunk_count == 1) {
1492 * optimize for the case where 'tc' is the only
1493 * chunk in the pool.
1495 space_needed = new_chunk_size;
1496 space_left = pool_tc->size - TALLOC_POOL_HDR_SIZE;
1498 if (space_left >= space_needed) {
1499 size_t old_used = TC_HDR_SIZE + tc->size;
1500 size_t new_used = TC_HDR_SIZE + size;
1501 pool_tc->pool = TC_POOL_FIRST_CHUNK(pool_tc);
1502 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
1504 * we need to prepare the memmove into
1505 * the unaccessable area.
1508 size_t diff = PTR_DIFF(tc, pool_tc->pool);
1509 size_t flen = MIN(diff, old_used);
1510 char *fptr = (char *)pool_tc->pool;
1511 VALGRIND_MAKE_MEM_UNDEFINED(fptr, flen);
1513 #endif
1514 memmove(pool_tc->pool, tc, old_used);
1515 new_ptr = pool_tc->pool;
1517 tc = (struct talloc_chunk *)new_ptr;
1518 TC_UNDEFINE_GROW_CHUNK(tc, size);
1521 * first we do not align the pool pointer
1522 * because we want to invalidate the padding
1523 * too.
1525 pool_tc->pool = new_used + (char *)new_ptr;
1526 TC_INVALIDATE_POOL(pool_tc);
1528 /* now the aligned pointer */
1529 pool_tc->pool = new_chunk_size + (char *)new_ptr;
1530 goto got_new_ptr;
1533 next_tc = NULL;
1536 if (new_chunk_size == old_chunk_size) {
1537 TC_UNDEFINE_GROW_CHUNK(tc, size);
1538 tc->flags &= ~TALLOC_FLAG_FREE;
1539 tc->size = size;
1540 return ptr;
1543 if (next_tc == pool_tc->pool) {
1545 * optimize for the case where 'tc' is the last
1546 * chunk in the pool.
1548 space_needed = new_chunk_size - old_chunk_size;
1549 space_left = TC_POOL_SPACE_LEFT(pool_tc);
1551 if (space_left >= space_needed) {
1552 TC_UNDEFINE_GROW_CHUNK(tc, size);
1553 tc->flags &= ~TALLOC_FLAG_FREE;
1554 tc->size = size;
1555 pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
1556 return ptr;
1560 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1562 if (new_ptr == NULL) {
1563 new_ptr = malloc(TC_HDR_SIZE+size);
1564 malloced = true;
1567 if (new_ptr) {
1568 memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1570 _talloc_free_poolmem(tc, __location__ "_talloc_realloc");
1573 else {
1574 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1576 got_new_ptr:
1577 #endif
1578 if (unlikely(!new_ptr)) {
1579 tc->flags &= ~TALLOC_FLAG_FREE;
1580 return NULL;
1583 tc = (struct talloc_chunk *)new_ptr;
1584 tc->flags &= ~TALLOC_FLAG_FREE;
1585 if (malloced) {
1586 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1588 if (tc->parent) {
1589 tc->parent->child = tc;
1591 if (tc->child) {
1592 tc->child->parent = tc;
1595 if (tc->prev) {
1596 tc->prev->next = tc;
1598 if (tc->next) {
1599 tc->next->prev = tc;
1602 tc->size = size;
1603 _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1605 return TC_PTR_FROM_CHUNK(tc);
1609 a wrapper around talloc_steal() for situations where you are moving a pointer
1610 between two structures, and want the old pointer to be set to NULL
1612 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
1614 const void **pptr = discard_const_p(const void *,_pptr);
1615 void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1616 (*pptr) = NULL;
1617 return ret;
1621 return the total size of a talloc pool (subtree)
1623 _PUBLIC_ size_t talloc_total_size(const void *ptr)
1625 size_t total = 0;
1626 struct talloc_chunk *c, *tc;
1628 if (ptr == NULL) {
1629 ptr = null_context;
1631 if (ptr == NULL) {
1632 return 0;
1635 tc = talloc_chunk_from_ptr(ptr);
1637 if (tc->flags & TALLOC_FLAG_LOOP) {
1638 return 0;
1641 tc->flags |= TALLOC_FLAG_LOOP;
1643 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1644 total = tc->size;
1646 for (c=tc->child;c;c=c->next) {
1647 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1650 tc->flags &= ~TALLOC_FLAG_LOOP;
1652 return total;
1656 return the total number of blocks in a talloc pool (subtree)
1658 _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
1660 size_t total = 0;
1661 struct talloc_chunk *c, *tc;
1663 if (ptr == NULL) {
1664 ptr = null_context;
1666 if (ptr == NULL) {
1667 return 0;
1670 tc = talloc_chunk_from_ptr(ptr);
1672 if (tc->flags & TALLOC_FLAG_LOOP) {
1673 return 0;
1676 tc->flags |= TALLOC_FLAG_LOOP;
1678 total++;
1679 for (c=tc->child;c;c=c->next) {
1680 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1683 tc->flags &= ~TALLOC_FLAG_LOOP;
1685 return total;
1689 return the number of external references to a pointer
1691 _PUBLIC_ size_t talloc_reference_count(const void *ptr)
1693 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1694 struct talloc_reference_handle *h;
1695 size_t ret = 0;
1697 for (h=tc->refs;h;h=h->next) {
1698 ret++;
1700 return ret;
1704 report on memory usage by all children of a pointer, giving a full tree view
1706 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1707 void (*callback)(const void *ptr,
1708 int depth, int max_depth,
1709 int is_ref,
1710 void *private_data),
1711 void *private_data)
1713 struct talloc_chunk *c, *tc;
1715 if (ptr == NULL) {
1716 ptr = null_context;
1718 if (ptr == NULL) return;
1720 tc = talloc_chunk_from_ptr(ptr);
1722 if (tc->flags & TALLOC_FLAG_LOOP) {
1723 return;
1726 callback(ptr, depth, max_depth, 0, private_data);
1728 if (max_depth >= 0 && depth >= max_depth) {
1729 return;
1732 tc->flags |= TALLOC_FLAG_LOOP;
1733 for (c=tc->child;c;c=c->next) {
1734 if (c->name == TALLOC_MAGIC_REFERENCE) {
1735 struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1736 callback(h->ptr, depth + 1, max_depth, 1, private_data);
1737 } else {
1738 talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1741 tc->flags &= ~TALLOC_FLAG_LOOP;
1744 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1746 const char *name = talloc_get_name(ptr);
1747 FILE *f = (FILE *)_f;
1749 if (is_ref) {
1750 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1751 return;
1754 if (depth == 0) {
1755 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
1756 (max_depth < 0 ? "full " :""), name,
1757 (unsigned long)talloc_total_size(ptr),
1758 (unsigned long)talloc_total_blocks(ptr));
1759 return;
1762 fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
1763 depth*4, "",
1764 name,
1765 (unsigned long)talloc_total_size(ptr),
1766 (unsigned long)talloc_total_blocks(ptr),
1767 (int)talloc_reference_count(ptr), ptr);
1769 #if 0
1770 fprintf(f, "content: ");
1771 if (talloc_total_size(ptr)) {
1772 int tot = talloc_total_size(ptr);
1773 int i;
1775 for (i = 0; i < tot; i++) {
1776 if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1777 fprintf(f, "%c", ((char *)ptr)[i]);
1778 } else {
1779 fprintf(f, "~%02x", ((char *)ptr)[i]);
1783 fprintf(f, "\n");
1784 #endif
1788 report on memory usage by all children of a pointer, giving a full tree view
1790 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1792 if (f) {
1793 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1794 fflush(f);
1799 report on memory usage by all children of a pointer, giving a full tree view
1801 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
1803 talloc_report_depth_file(ptr, 0, -1, f);
1807 report on memory usage by all children of a pointer
1809 _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
1811 talloc_report_depth_file(ptr, 0, 1, f);
1815 report on any memory hanging off the null context
1817 static void talloc_report_null(void)
1819 if (talloc_total_size(null_context) != 0) {
1820 talloc_report(null_context, stderr);
1825 report on any memory hanging off the null context
1827 static void talloc_report_null_full(void)
1829 if (talloc_total_size(null_context) != 0) {
1830 talloc_report_full(null_context, stderr);
1835 enable tracking of the NULL context
1837 _PUBLIC_ void talloc_enable_null_tracking(void)
1839 if (null_context == NULL) {
1840 null_context = _talloc_named_const(NULL, 0, "null_context");
1841 if (autofree_context != NULL) {
1842 talloc_reparent(NULL, null_context, autofree_context);
1848 enable tracking of the NULL context, not moving the autofree context
1849 into the NULL context. This is needed for the talloc testsuite
1851 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
1853 if (null_context == NULL) {
1854 null_context = _talloc_named_const(NULL, 0, "null_context");
1859 disable tracking of the NULL context
1861 _PUBLIC_ void talloc_disable_null_tracking(void)
1863 if (null_context != NULL) {
1864 /* we have to move any children onto the real NULL
1865 context */
1866 struct talloc_chunk *tc, *tc2;
1867 tc = talloc_chunk_from_ptr(null_context);
1868 for (tc2 = tc->child; tc2; tc2=tc2->next) {
1869 if (tc2->parent == tc) tc2->parent = NULL;
1870 if (tc2->prev == tc) tc2->prev = NULL;
1872 for (tc2 = tc->next; tc2; tc2=tc2->next) {
1873 if (tc2->parent == tc) tc2->parent = NULL;
1874 if (tc2->prev == tc) tc2->prev = NULL;
1876 tc->child = NULL;
1877 tc->next = NULL;
1879 talloc_free(null_context);
1880 null_context = NULL;
1884 enable leak reporting on exit
1886 _PUBLIC_ void talloc_enable_leak_report(void)
1888 talloc_enable_null_tracking();
1889 atexit(talloc_report_null);
1893 enable full leak reporting on exit
1895 _PUBLIC_ void talloc_enable_leak_report_full(void)
1897 talloc_enable_null_tracking();
1898 atexit(talloc_report_null_full);
1902 talloc and zero memory.
1904 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
1906 void *p = _talloc_named_const(ctx, size, name);
1908 if (p) {
1909 memset(p, '\0', size);
1912 return p;
1916 memdup with a talloc.
1918 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1920 void *newp = _talloc_named_const(t, size, name);
1922 if (likely(newp)) {
1923 memcpy(newp, p, size);
1926 return newp;
1929 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1931 char *ret;
1933 ret = (char *)__talloc(t, len + 1);
1934 if (unlikely(!ret)) return NULL;
1936 memcpy(ret, p, len);
1937 ret[len] = 0;
1939 _talloc_set_name_const(ret, ret);
1940 return ret;
1944 strdup with a talloc
1946 _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
1948 if (unlikely(!p)) return NULL;
1949 return __talloc_strlendup(t, p, strlen(p));
1953 strndup with a talloc
1955 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
1957 if (unlikely(!p)) return NULL;
1958 return __talloc_strlendup(t, p, strnlen(p, n));
1961 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1962 const char *a, size_t alen)
1964 char *ret;
1966 ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1967 if (unlikely(!ret)) return NULL;
1969 /* append the string and the trailing \0 */
1970 memcpy(&ret[slen], a, alen);
1971 ret[slen+alen] = 0;
1973 _talloc_set_name_const(ret, ret);
1974 return ret;
1978 * Appends at the end of the string.
1980 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
1982 if (unlikely(!s)) {
1983 return talloc_strdup(NULL, a);
1986 if (unlikely(!a)) {
1987 return s;
1990 return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1994 * Appends at the end of the talloc'ed buffer,
1995 * not the end of the string.
1997 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
1999 size_t slen;
2001 if (unlikely(!s)) {
2002 return talloc_strdup(NULL, a);
2005 if (unlikely(!a)) {
2006 return s;
2009 slen = talloc_get_size(s);
2010 if (likely(slen > 0)) {
2011 slen--;
2014 return __talloc_strlendup_append(s, slen, a, strlen(a));
2018 * Appends at the end of the string.
2020 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
2022 if (unlikely(!s)) {
2023 return talloc_strdup(NULL, a);
2026 if (unlikely(!a)) {
2027 return s;
2030 return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
2034 * Appends at the end of the talloc'ed buffer,
2035 * not the end of the string.
2037 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
2039 size_t slen;
2041 if (unlikely(!s)) {
2042 return talloc_strdup(NULL, a);
2045 if (unlikely(!a)) {
2046 return s;
2049 slen = talloc_get_size(s);
2050 if (likely(slen > 0)) {
2051 slen--;
2054 return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
2057 #ifndef HAVE_VA_COPY
2058 #ifdef HAVE___VA_COPY
2059 #define va_copy(dest, src) __va_copy(dest, src)
2060 #else
2061 #define va_copy(dest, src) (dest) = (src)
2062 #endif
2063 #endif
2065 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
2067 int len;
2068 char *ret;
2069 va_list ap2;
2070 char c;
2072 /* this call looks strange, but it makes it work on older solaris boxes */
2073 va_copy(ap2, ap);
2074 len = vsnprintf(&c, 1, fmt, ap2);
2075 va_end(ap2);
2076 if (unlikely(len < 0)) {
2077 return NULL;
2080 ret = (char *)__talloc(t, len+1);
2081 if (unlikely(!ret)) return NULL;
2083 va_copy(ap2, ap);
2084 vsnprintf(ret, len+1, fmt, ap2);
2085 va_end(ap2);
2087 _talloc_set_name_const(ret, ret);
2088 return ret;
2093 Perform string formatting, and return a pointer to newly allocated
2094 memory holding the result, inside a memory pool.
2096 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
2098 va_list ap;
2099 char *ret;
2101 va_start(ap, fmt);
2102 ret = talloc_vasprintf(t, fmt, ap);
2103 va_end(ap);
2104 return ret;
2107 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2108 const char *fmt, va_list ap)
2109 PRINTF_ATTRIBUTE(3,0);
2111 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2112 const char *fmt, va_list ap)
2114 ssize_t alen;
2115 va_list ap2;
2116 char c;
2118 va_copy(ap2, ap);
2119 alen = vsnprintf(&c, 1, fmt, ap2);
2120 va_end(ap2);
2122 if (alen <= 0) {
2123 /* Either the vsnprintf failed or the format resulted in
2124 * no characters being formatted. In the former case, we
2125 * ought to return NULL, in the latter we ought to return
2126 * the original string. Most current callers of this
2127 * function expect it to never return NULL.
2129 return s;
2132 s = talloc_realloc(NULL, s, char, slen + alen + 1);
2133 if (!s) return NULL;
2135 va_copy(ap2, ap);
2136 vsnprintf(s + slen, alen + 1, fmt, ap2);
2137 va_end(ap2);
2139 _talloc_set_name_const(s, s);
2140 return s;
2144 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2145 * and return @p s, which may have moved. Good for gradually
2146 * accumulating output into a string buffer. Appends at the end
2147 * of the string.
2149 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
2151 if (unlikely(!s)) {
2152 return talloc_vasprintf(NULL, fmt, ap);
2155 return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
2159 * Realloc @p s to append the formatted result of @p fmt and @p ap,
2160 * and return @p s, which may have moved. Always appends at the
2161 * end of the talloc'ed buffer, not the end of the string.
2163 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
2165 size_t slen;
2167 if (unlikely(!s)) {
2168 return talloc_vasprintf(NULL, fmt, ap);
2171 slen = talloc_get_size(s);
2172 if (likely(slen > 0)) {
2173 slen--;
2176 return __talloc_vaslenprintf_append(s, slen, fmt, ap);
2180 Realloc @p s to append the formatted result of @p fmt and return @p
2181 s, which may have moved. Good for gradually accumulating output
2182 into a string buffer.
2184 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
2186 va_list ap;
2188 va_start(ap, fmt);
2189 s = talloc_vasprintf_append(s, fmt, ap);
2190 va_end(ap);
2191 return s;
2195 Realloc @p s to append the formatted result of @p fmt and return @p
2196 s, which may have moved. Good for gradually accumulating output
2197 into a buffer.
2199 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
2201 va_list ap;
2203 va_start(ap, fmt);
2204 s = talloc_vasprintf_append_buffer(s, fmt, ap);
2205 va_end(ap);
2206 return s;
2210 alloc an array, checking for integer overflow in the array size
2212 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2214 if (count >= MAX_TALLOC_SIZE/el_size) {
2215 return NULL;
2217 return _talloc_named_const(ctx, el_size * count, name);
2221 alloc an zero array, checking for integer overflow in the array size
2223 _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2225 if (count >= MAX_TALLOC_SIZE/el_size) {
2226 return NULL;
2228 return _talloc_zero(ctx, el_size * count, name);
2232 realloc an array, checking for integer overflow in the array size
2234 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
2236 if (count >= MAX_TALLOC_SIZE/el_size) {
2237 return NULL;
2239 return _talloc_realloc(ctx, ptr, el_size * count, name);
2243 a function version of talloc_realloc(), so it can be passed as a function pointer
2244 to libraries that want a realloc function (a realloc function encapsulates
2245 all the basic capabilities of an allocation library, which is why this is useful)
2247 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
2249 return _talloc_realloc(context, ptr, size, NULL);
2253 static int talloc_autofree_destructor(void *ptr)
2255 autofree_context = NULL;
2256 return 0;
2259 static void talloc_autofree(void)
2261 talloc_free(autofree_context);
2265 return a context which will be auto-freed on exit
2266 this is useful for reducing the noise in leak reports
2268 _PUBLIC_ void *talloc_autofree_context(void)
2270 if (autofree_context == NULL) {
2271 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
2272 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
2273 atexit(talloc_autofree);
2275 return autofree_context;
2278 _PUBLIC_ size_t talloc_get_size(const void *context)
2280 struct talloc_chunk *tc;
2282 if (context == NULL) {
2283 context = null_context;
2285 if (context == NULL) {
2286 return 0;
2289 tc = talloc_chunk_from_ptr(context);
2291 return tc->size;
2295 find a parent of this context that has the given name, if any
2297 _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
2299 struct talloc_chunk *tc;
2301 if (context == NULL) {
2302 return NULL;
2305 tc = talloc_chunk_from_ptr(context);
2306 while (tc) {
2307 if (tc->name && strcmp(tc->name, name) == 0) {
2308 return TC_PTR_FROM_CHUNK(tc);
2310 while (tc && tc->prev) tc = tc->prev;
2311 if (tc) {
2312 tc = tc->parent;
2315 return NULL;
2319 show the parentage of a context
2321 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
2323 struct talloc_chunk *tc;
2325 if (context == NULL) {
2326 fprintf(file, "talloc no parents for NULL\n");
2327 return;
2330 tc = talloc_chunk_from_ptr(context);
2331 fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
2332 while (tc) {
2333 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
2334 while (tc && tc->prev) tc = tc->prev;
2335 if (tc) {
2336 tc = tc->parent;
2339 fflush(file);
2343 return 1 if ptr is a parent of context
2345 static int _talloc_is_parent(const void *context, const void *ptr, int depth)
2347 struct talloc_chunk *tc;
2349 if (context == NULL) {
2350 return 0;
2353 tc = talloc_chunk_from_ptr(context);
2354 while (tc && depth > 0) {
2355 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2356 while (tc && tc->prev) tc = tc->prev;
2357 if (tc) {
2358 tc = tc->parent;
2359 depth--;
2362 return 0;
2366 return 1 if ptr is a parent of context
2368 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2370 return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);