s4:schema Don't rely on objectCategory 'magic' when loading the schema
[kai/samba.git] / lib / talloc / talloc.c
1 /* 
2    Samba Unix SMB/CIFS implementation.
3
4    Samba trivial allocation library - new interface
5
6    NOTE: Please read talloc_guide.txt for full documentation
7
8    Copyright (C) Andrew Tridgell 2004
9    Copyright (C) Stefan Metzmacher 2006
10    
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
14    
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.
19
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.
24
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/>.
27 */
28
29 /*
30   inspired by http://swapped.cc/halloc/
31 */
32
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_ */
48
49 #ifndef _TALLOC_SAMBA3
50 #include "replace.h"
51 #include "talloc.h"
52 #endif /* not _TALLOC_SAMBA3 */
53
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
57
58
59 #define MAX_TALLOC_SIZE 0x10000000
60 #define TALLOC_MAGIC 0xe814ec70
61 #define TALLOC_FLAG_FREE 0x01
62 #define TALLOC_FLAG_LOOP 0x02
63 #define TALLOC_FLAG_POOL 0x04           /* This is a talloc pool */
64 #define TALLOC_FLAG_POOLMEM 0x08        /* This is allocated in a pool */
65 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
66
67 /* by default we abort when given a bad pointer (such as when talloc_free() is called 
68    on a pointer that came from malloc() */
69 #ifndef TALLOC_ABORT
70 #define TALLOC_ABORT(reason) abort()
71 #endif
72
73 #ifndef discard_const_p
74 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
75 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
76 #else
77 # define discard_const_p(type, ptr) ((type *)(ptr))
78 #endif
79 #endif
80
81 /* these macros gain us a few percent of speed on gcc */
82 #if (__GNUC__ >= 3)
83 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
84    as its first argument */
85 #ifndef likely
86 #define likely(x)   __builtin_expect(!!(x), 1)
87 #endif
88 #ifndef unlikely
89 #define unlikely(x) __builtin_expect(!!(x), 0)
90 #endif
91 #else
92 #ifndef likely
93 #define likely(x) (x)
94 #endif
95 #ifndef unlikely
96 #define unlikely(x) (x)
97 #endif
98 #endif
99
100 /* this null_context is only used if talloc_enable_leak_report() or
101    talloc_enable_leak_report_full() is called, otherwise it remains
102    NULL
103 */
104 static void *null_context;
105 static void *autofree_context;
106
107 struct talloc_reference_handle {
108         struct talloc_reference_handle *next, *prev;
109         void *ptr;
110 };
111
112 typedef int (*talloc_destructor_t)(void *);
113
114 struct talloc_chunk {
115         struct talloc_chunk *next, *prev;
116         struct talloc_chunk *parent, *child;
117         struct talloc_reference_handle *refs;
118         talloc_destructor_t destructor;
119         const char *name;
120         size_t size;
121         unsigned flags;
122
123         /*
124          * "pool" has dual use:
125          *
126          * For the talloc pool itself (i.e. TALLOC_FLAG_POOL is set), "pool"
127          * marks the end of the currently allocated area.
128          *
129          * For members of the pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
130          * is a pointer to the struct talloc_chunk of the pool that it was
131          * allocated from. This way children can quickly find the pool to chew
132          * from.
133          */
134         void *pool;
135 };
136
137 /* 16 byte alignment seems to keep everyone happy */
138 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
139 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
140
141 static void talloc_abort_double_free(void)
142 {
143         TALLOC_ABORT("Bad talloc magic value - double free"); 
144 }
145
146 static void talloc_abort_unknown_value(void)
147 {
148         TALLOC_ABORT("Bad talloc magic value - unknown value"); 
149 }
150
151 /* panic if we get a bad magic value */
152 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
153 {
154         const char *pp = (const char *)ptr;
155         struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
156         if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) { 
157                 if (tc->flags & TALLOC_FLAG_FREE) {
158                         talloc_abort_double_free();
159                 } else {
160                         talloc_abort_unknown_value();
161                 }
162         }
163         return tc;
164 }
165
166 /* hook into the front of the list */
167 #define _TLIST_ADD(list, p) \
168 do { \
169         if (!(list)) { \
170                 (list) = (p); \
171                 (p)->next = (p)->prev = NULL; \
172         } else { \
173                 (list)->prev = (p); \
174                 (p)->next = (list); \
175                 (p)->prev = NULL; \
176                 (list) = (p); \
177         }\
178 } while (0)
179
180 /* remove an element from a list - element doesn't have to be in list. */
181 #define _TLIST_REMOVE(list, p) \
182 do { \
183         if ((p) == (list)) { \
184                 (list) = (p)->next; \
185                 if (list) (list)->prev = NULL; \
186         } else { \
187                 if ((p)->prev) (p)->prev->next = (p)->next; \
188                 if ((p)->next) (p)->next->prev = (p)->prev; \
189         } \
190         if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
191 } while (0)
192
193
194 /*
195   return the parent chunk of a pointer
196 */
197 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
198 {
199         struct talloc_chunk *tc;
200
201         if (unlikely(ptr == NULL)) {
202                 return NULL;
203         }
204
205         tc = talloc_chunk_from_ptr(ptr);
206         while (tc->prev) tc=tc->prev;
207
208         return tc->parent;
209 }
210
211 void *talloc_parent(const void *ptr)
212 {
213         struct talloc_chunk *tc = talloc_parent_chunk(ptr);
214         return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
215 }
216
217 /*
218   find parents name
219 */
220 const char *talloc_parent_name(const void *ptr)
221 {
222         struct talloc_chunk *tc = talloc_parent_chunk(ptr);
223         return tc? tc->name : NULL;
224 }
225
226 /*
227   A pool carries an in-pool object count count in the first 16 bytes.
228   bytes. This is done to support talloc_steal() to a parent outside of the
229   pool. The count includes the pool itself, so a talloc_free() on a pool will
230   only destroy the pool if the count has dropped to zero. A talloc_free() of a
231   pool member will reduce the count, and eventually also call free(3) on the
232   pool memory.
233
234   The object count is not put into "struct talloc_chunk" because it is only
235   relevant for talloc pools and the alignment to 16 bytes would increase the
236   memory footprint of each talloc chunk by those 16 bytes.
237 */
238
239 #define TALLOC_POOL_HDR_SIZE 16
240
241 static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
242 {
243         return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
244 }
245
246 /*
247   Allocate from a pool
248 */
249
250 static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
251                                               size_t size)
252 {
253         struct talloc_chunk *pool_ctx = NULL;
254         size_t space_left;
255         struct talloc_chunk *result;
256         size_t chunk_size;
257
258         if (parent == NULL) {
259                 return NULL;
260         }
261
262         if (parent->flags & TALLOC_FLAG_POOL) {
263                 pool_ctx = parent;
264         }
265         else if (parent->flags & TALLOC_FLAG_POOLMEM) {
266                 pool_ctx = (struct talloc_chunk *)parent->pool;
267         }
268
269         if (pool_ctx == NULL) {
270                 return NULL;
271         }
272
273         space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
274                 - ((char *)pool_ctx->pool);
275
276         /*
277          * Align size to 16 bytes
278          */
279         chunk_size = ((size + 15) & ~15);
280
281         if (space_left < chunk_size) {
282                 return NULL;
283         }
284
285         result = (struct talloc_chunk *)pool_ctx->pool;
286
287 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
288         VALGRIND_MAKE_MEM_UNDEFINED(result, size);
289 #endif
290
291         pool_ctx->pool = (void *)((char *)result + chunk_size);
292
293         result->flags = TALLOC_MAGIC | TALLOC_FLAG_POOLMEM;
294         result->pool = pool_ctx;
295
296         *talloc_pool_objectcount(pool_ctx) += 1;
297
298         return result;
299 }
300
301 /* 
302    Allocate a bit of memory as a child of an existing pointer
303 */
304 static inline void *__talloc(const void *context, size_t size)
305 {
306         struct talloc_chunk *tc = NULL;
307
308         if (unlikely(context == NULL)) {
309                 context = null_context;
310         }
311
312         if (unlikely(size >= MAX_TALLOC_SIZE)) {
313                 return NULL;
314         }
315
316         if (context != NULL) {
317                 tc = talloc_alloc_pool(talloc_chunk_from_ptr(context),
318                                        TC_HDR_SIZE+size);
319         }
320
321         if (tc == NULL) {
322                 tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
323                 if (unlikely(tc == NULL)) return NULL;
324                 tc->flags = TALLOC_MAGIC;
325                 tc->pool  = NULL;
326         }
327
328         tc->size = size;
329         tc->destructor = NULL;
330         tc->child = NULL;
331         tc->name = NULL;
332         tc->refs = NULL;
333
334         if (likely(context)) {
335                 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
336
337                 if (parent->child) {
338                         parent->child->parent = NULL;
339                         tc->next = parent->child;
340                         tc->next->prev = tc;
341                 } else {
342                         tc->next = NULL;
343                 }
344                 tc->parent = parent;
345                 tc->prev = NULL;
346                 parent->child = tc;
347         } else {
348                 tc->next = tc->prev = tc->parent = NULL;
349         }
350
351         return TC_PTR_FROM_CHUNK(tc);
352 }
353
354 /*
355  * Create a talloc pool
356  */
357
358 void *talloc_pool(const void *context, size_t size)
359 {
360         void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
361         struct talloc_chunk *tc;
362
363         if (unlikely(result == NULL)) {
364                 return NULL;
365         }
366
367         tc = talloc_chunk_from_ptr(result);
368
369         tc->flags |= TALLOC_FLAG_POOL;
370         tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
371
372         *talloc_pool_objectcount(tc) = 1;
373
374 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
375         VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
376 #endif
377
378         return result;
379 }
380
381 /*
382   setup a destructor to be called on free of a pointer
383   the destructor should return 0 on success, or -1 on failure.
384   if the destructor fails then the free is failed, and the memory can
385   be continued to be used
386 */
387 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
388 {
389         struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
390         tc->destructor = destructor;
391 }
392
393 /*
394   increase the reference count on a piece of memory. 
395 */
396 int talloc_increase_ref_count(const void *ptr)
397 {
398         if (unlikely(!talloc_reference(null_context, ptr))) {
399                 return -1;
400         }
401         return 0;
402 }
403
404 /*
405   helper for talloc_reference()
406
407   this is referenced by a function pointer and should not be inline
408 */
409 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
410 {
411         struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
412         _TLIST_REMOVE(ptr_tc->refs, handle);
413         return 0;
414 }
415
416 /*
417    more efficient way to add a name to a pointer - the name must point to a 
418    true string constant
419 */
420 static inline void _talloc_set_name_const(const void *ptr, const char *name)
421 {
422         struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
423         tc->name = name;
424 }
425
426 /*
427   internal talloc_named_const()
428 */
429 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
430 {
431         void *ptr;
432
433         ptr = __talloc(context, size);
434         if (unlikely(ptr == NULL)) {
435                 return NULL;
436         }
437
438         _talloc_set_name_const(ptr, name);
439
440         return ptr;
441 }
442
443 /*
444   make a secondary reference to a pointer, hanging off the given context.
445   the pointer remains valid until both the original caller and this given
446   context are freed.
447   
448   the major use for this is when two different structures need to reference the 
449   same underlying data, and you want to be able to free the two instances separately,
450   and in either order
451 */
452 void *_talloc_reference(const void *context, const void *ptr)
453 {
454         struct talloc_chunk *tc;
455         struct talloc_reference_handle *handle;
456         if (unlikely(ptr == NULL)) return NULL;
457
458         tc = talloc_chunk_from_ptr(ptr);
459         handle = (struct talloc_reference_handle *)_talloc_named_const(context,
460                                                    sizeof(struct talloc_reference_handle),
461                                                    TALLOC_MAGIC_REFERENCE);
462         if (unlikely(handle == NULL)) return NULL;
463
464         /* note that we hang the destructor off the handle, not the
465            main context as that allows the caller to still setup their
466            own destructor on the context if they want to */
467         talloc_set_destructor(handle, talloc_reference_destructor);
468         handle->ptr = discard_const_p(void, ptr);
469         _TLIST_ADD(tc->refs, handle);
470         return handle->ptr;
471 }
472
473
474 /* 
475    internal talloc_free call
476 */
477 static inline int _talloc_free(void *ptr)
478 {
479         struct talloc_chunk *tc;
480
481         if (unlikely(ptr == NULL)) {
482                 return -1;
483         }
484
485         tc = talloc_chunk_from_ptr(ptr);
486
487         if (unlikely(tc->refs)) {
488                 int is_child;
489                 /* check this is a reference from a child or grantchild
490                  * back to it's parent or grantparent
491                  *
492                  * in that case we need to remove the reference and
493                  * call another instance of talloc_free() on the current
494                  * pointer.
495                  */
496                 is_child = talloc_is_parent(tc->refs, ptr);
497                 _talloc_free(tc->refs);
498                 if (is_child) {
499                         return _talloc_free(ptr);
500                 }
501                 return -1;
502         }
503
504         if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
505                 /* we have a free loop - stop looping */
506                 return 0;
507         }
508
509         if (unlikely(tc->destructor)) {
510                 talloc_destructor_t d = tc->destructor;
511                 if (d == (talloc_destructor_t)-1) {
512                         return -1;
513                 }
514                 tc->destructor = (talloc_destructor_t)-1;
515                 if (d(ptr) == -1) {
516                         tc->destructor = d;
517                         return -1;
518                 }
519                 tc->destructor = NULL;
520         }
521
522         if (tc->parent) {
523                 _TLIST_REMOVE(tc->parent->child, tc);
524                 if (tc->parent->child) {
525                         tc->parent->child->parent = tc->parent;
526                 }
527         } else {
528                 if (tc->prev) tc->prev->next = tc->next;
529                 if (tc->next) tc->next->prev = tc->prev;
530         }
531
532         tc->flags |= TALLOC_FLAG_LOOP;
533
534         while (tc->child) {
535                 /* we need to work out who will own an abandoned child
536                    if it cannot be freed. In priority order, the first
537                    choice is owner of any remaining reference to this
538                    pointer, the second choice is our parent, and the
539                    final choice is the null context. */
540                 void *child = TC_PTR_FROM_CHUNK(tc->child);
541                 const void *new_parent = null_context;
542                 if (unlikely(tc->child->refs)) {
543                         struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
544                         if (p) new_parent = TC_PTR_FROM_CHUNK(p);
545                 }
546                 if (unlikely(_talloc_free(child) == -1)) {
547                         if (new_parent == null_context) {
548                                 struct talloc_chunk *p = talloc_parent_chunk(ptr);
549                                 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
550                         }
551                         talloc_steal(new_parent, child);
552                 }
553         }
554
555         tc->flags |= TALLOC_FLAG_FREE;
556
557         if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
558                 struct talloc_chunk *pool;
559                 unsigned int *pool_object_count;
560
561                 pool = (tc->flags & TALLOC_FLAG_POOL)
562                         ? tc : (struct talloc_chunk *)tc->pool;
563
564                 pool_object_count = talloc_pool_objectcount(pool);
565
566                 if (*pool_object_count == 0) {
567                         TALLOC_ABORT("Pool object count zero!");
568                 }
569
570                 *pool_object_count -= 1;
571
572                 if (*pool_object_count == 0) {
573                         free(pool);
574                 }
575         }
576         else {
577                 free(tc);
578         }
579         return 0;
580 }
581
582 /* 
583    move a lump of memory from one talloc context to another return the
584    ptr on success, or NULL if it could not be transferred.
585    passing NULL as ptr will always return NULL with no side effects.
586 */
587 void *_talloc_steal(const void *new_ctx, const void *ptr)
588 {
589         struct talloc_chunk *tc, *new_tc;
590
591         if (unlikely(!ptr)) {
592                 return NULL;
593         }
594
595         if (unlikely(new_ctx == NULL)) {
596                 new_ctx = null_context;
597         }
598
599         tc = talloc_chunk_from_ptr(ptr);
600
601         if (unlikely(new_ctx == NULL)) {
602                 if (tc->parent) {
603                         _TLIST_REMOVE(tc->parent->child, tc);
604                         if (tc->parent->child) {
605                                 tc->parent->child->parent = tc->parent;
606                         }
607                 } else {
608                         if (tc->prev) tc->prev->next = tc->next;
609                         if (tc->next) tc->next->prev = tc->prev;
610                 }
611                 
612                 tc->parent = tc->next = tc->prev = NULL;
613                 return discard_const_p(void, ptr);
614         }
615
616         new_tc = talloc_chunk_from_ptr(new_ctx);
617
618         if (unlikely(tc == new_tc || tc->parent == new_tc)) {
619                 return discard_const_p(void, ptr);
620         }
621
622         if (tc->parent) {
623                 _TLIST_REMOVE(tc->parent->child, tc);
624                 if (tc->parent->child) {
625                         tc->parent->child->parent = tc->parent;
626                 }
627         } else {
628                 if (tc->prev) tc->prev->next = tc->next;
629                 if (tc->next) tc->next->prev = tc->prev;
630         }
631
632         tc->parent = new_tc;
633         if (new_tc->child) new_tc->child->parent = NULL;
634         _TLIST_ADD(new_tc->child, tc);
635
636         return discard_const_p(void, ptr);
637 }
638
639
640
641 /*
642   remove a secondary reference to a pointer. This undo's what
643   talloc_reference() has done. The context and pointer arguments
644   must match those given to a talloc_reference()
645 */
646 static inline int talloc_unreference(const void *context, const void *ptr)
647 {
648         struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
649         struct talloc_reference_handle *h;
650
651         if (unlikely(context == NULL)) {
652                 context = null_context;
653         }
654
655         for (h=tc->refs;h;h=h->next) {
656                 struct talloc_chunk *p = talloc_parent_chunk(h);
657                 if (p == NULL) {
658                         if (context == NULL) break;
659                 } else if (TC_PTR_FROM_CHUNK(p) == context) {
660                         break;
661                 }
662         }
663         if (h == NULL) {
664                 return -1;
665         }
666
667         return _talloc_free(h);
668 }
669
670 /*
671   remove a specific parent context from a pointer. This is a more
672   controlled varient of talloc_free()
673 */
674 int talloc_unlink(const void *context, void *ptr)
675 {
676         struct talloc_chunk *tc_p, *new_p;
677         void *new_parent;
678
679         if (ptr == NULL) {
680                 return -1;
681         }
682
683         if (context == NULL) {
684                 context = null_context;
685         }
686
687         if (talloc_unreference(context, ptr) == 0) {
688                 return 0;
689         }
690
691         if (context == NULL) {
692                 if (talloc_parent_chunk(ptr) != NULL) {
693                         return -1;
694                 }
695         } else {
696                 if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
697                         return -1;
698                 }
699         }
700         
701         tc_p = talloc_chunk_from_ptr(ptr);
702
703         if (tc_p->refs == NULL) {
704                 return _talloc_free(ptr);
705         }
706
707         new_p = talloc_parent_chunk(tc_p->refs);
708         if (new_p) {
709                 new_parent = TC_PTR_FROM_CHUNK(new_p);
710         } else {
711                 new_parent = NULL;
712         }
713
714         if (talloc_unreference(new_parent, ptr) != 0) {
715                 return -1;
716         }
717
718         talloc_steal(new_parent, ptr);
719
720         return 0;
721 }
722
723 /*
724   add a name to an existing pointer - va_list version
725 */
726 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
727
728 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
729 {
730         struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
731         tc->name = talloc_vasprintf(ptr, fmt, ap);
732         if (likely(tc->name)) {
733                 _talloc_set_name_const(tc->name, ".name");
734         }
735         return tc->name;
736 }
737
738 /*
739   add a name to an existing pointer
740 */
741 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
742 {
743         const char *name;
744         va_list ap;
745         va_start(ap, fmt);
746         name = talloc_set_name_v(ptr, fmt, ap);
747         va_end(ap);
748         return name;
749 }
750
751
752 /*
753   create a named talloc pointer. Any talloc pointer can be named, and
754   talloc_named() operates just like talloc() except that it allows you
755   to name the pointer.
756 */
757 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
758 {
759         va_list ap;
760         void *ptr;
761         const char *name;
762
763         ptr = __talloc(context, size);
764         if (unlikely(ptr == NULL)) return NULL;
765
766         va_start(ap, fmt);
767         name = talloc_set_name_v(ptr, fmt, ap);
768         va_end(ap);
769
770         if (unlikely(name == NULL)) {
771                 _talloc_free(ptr);
772                 return NULL;
773         }
774
775         return ptr;
776 }
777
778 /*
779   return the name of a talloc ptr, or "UNNAMED"
780 */
781 const char *talloc_get_name(const void *ptr)
782 {
783         struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
784         if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
785                 return ".reference";
786         }
787         if (likely(tc->name)) {
788                 return tc->name;
789         }
790         return "UNNAMED";
791 }
792
793
794 /*
795   check if a pointer has the given name. If it does, return the pointer,
796   otherwise return NULL
797 */
798 void *talloc_check_name(const void *ptr, const char *name)
799 {
800         const char *pname;
801         if (unlikely(ptr == NULL)) return NULL;
802         pname = talloc_get_name(ptr);
803         if (likely(pname == name || strcmp(pname, name) == 0)) {
804                 return discard_const_p(void, ptr);
805         }
806         return NULL;
807 }
808
809 static void talloc_abort_type_missmatch(const char *location,
810                                         const char *name,
811                                         const char *expected)
812 {
813         TALLOC_ABORT("Type missmatch");
814 }
815
816 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
817 {
818         const char *pname;
819
820         if (unlikely(ptr == NULL)) {
821                 talloc_abort_type_missmatch(location, NULL, name);
822                 return NULL;
823         }
824
825         pname = talloc_get_name(ptr);
826         if (likely(pname == name || strcmp(pname, name) == 0)) {
827                 return discard_const_p(void, ptr);
828         }
829
830         talloc_abort_type_missmatch(location, pname, name);
831         return NULL;
832 }
833
834 /*
835   this is for compatibility with older versions of talloc
836 */
837 void *talloc_init(const char *fmt, ...)
838 {
839         va_list ap;
840         void *ptr;
841         const char *name;
842
843         /*
844          * samba3 expects talloc_report_depth_cb(NULL, ...)
845          * reports all talloc'ed memory, so we need to enable
846          * null_tracking
847          */
848         talloc_enable_null_tracking();
849
850         ptr = __talloc(NULL, 0);
851         if (unlikely(ptr == NULL)) return NULL;
852
853         va_start(ap, fmt);
854         name = talloc_set_name_v(ptr, fmt, ap);
855         va_end(ap);
856
857         if (unlikely(name == NULL)) {
858                 _talloc_free(ptr);
859                 return NULL;
860         }
861
862         return ptr;
863 }
864
865 /*
866   this is a replacement for the Samba3 talloc_destroy_pool functionality. It
867   should probably not be used in new code. It's in here to keep the talloc
868   code consistent across Samba 3 and 4.
869 */
870 void talloc_free_children(void *ptr)
871 {
872         struct talloc_chunk *tc;
873
874         if (unlikely(ptr == NULL)) {
875                 return;
876         }
877
878         tc = talloc_chunk_from_ptr(ptr);
879
880         while (tc->child) {
881                 /* we need to work out who will own an abandoned child
882                    if it cannot be freed. In priority order, the first
883                    choice is owner of any remaining reference to this
884                    pointer, the second choice is our parent, and the
885                    final choice is the null context. */
886                 void *child = TC_PTR_FROM_CHUNK(tc->child);
887                 const void *new_parent = null_context;
888                 if (unlikely(tc->child->refs)) {
889                         struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
890                         if (p) new_parent = TC_PTR_FROM_CHUNK(p);
891                 }
892                 if (unlikely(_talloc_free(child) == -1)) {
893                         if (new_parent == null_context) {
894                                 struct talloc_chunk *p = talloc_parent_chunk(ptr);
895                                 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
896                         }
897                         talloc_steal(new_parent, child);
898                 }
899         }
900
901         if ((tc->flags & TALLOC_FLAG_POOL)
902             && (*talloc_pool_objectcount(tc) == 1)) {
903                 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
904 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
905                 VALGRIND_MAKE_MEM_NOACCESS(
906                         tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
907 #endif
908         }
909 }
910
911 /* 
912    Allocate a bit of memory as a child of an existing pointer
913 */
914 void *_talloc(const void *context, size_t size)
915 {
916         return __talloc(context, size);
917 }
918
919 /*
920   externally callable talloc_set_name_const()
921 */
922 void talloc_set_name_const(const void *ptr, const char *name)
923 {
924         _talloc_set_name_const(ptr, name);
925 }
926
927 /*
928   create a named talloc pointer. Any talloc pointer can be named, and
929   talloc_named() operates just like talloc() except that it allows you
930   to name the pointer.
931 */
932 void *talloc_named_const(const void *context, size_t size, const char *name)
933 {
934         return _talloc_named_const(context, size, name);
935 }
936
937 /* 
938    free a talloc pointer. This also frees all child pointers of this 
939    pointer recursively
940
941    return 0 if the memory is actually freed, otherwise -1. The memory
942    will not be freed if the ref_count is > 1 or the destructor (if
943    any) returns non-zero
944 */
945 int talloc_free(void *ptr)
946 {
947         return _talloc_free(ptr);
948 }
949
950
951
952 /*
953   A talloc version of realloc. The context argument is only used if
954   ptr is NULL
955 */
956 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
957 {
958         struct talloc_chunk *tc;
959         void *new_ptr;
960         bool malloced = false;
961
962         /* size zero is equivalent to free() */
963         if (unlikely(size == 0)) {
964                 _talloc_free(ptr);
965                 return NULL;
966         }
967
968         if (unlikely(size >= MAX_TALLOC_SIZE)) {
969                 return NULL;
970         }
971
972         /* realloc(NULL) is equivalent to malloc() */
973         if (ptr == NULL) {
974                 return _talloc_named_const(context, size, name);
975         }
976
977         tc = talloc_chunk_from_ptr(ptr);
978
979         /* don't allow realloc on referenced pointers */
980         if (unlikely(tc->refs)) {
981                 return NULL;
982         }
983
984         /* don't shrink if we have less than 1k to gain */
985         if ((size < tc->size) && ((tc->size - size) < 1024)) {
986                 tc->size = size;
987                 return ptr;
988         }
989
990         /* by resetting magic we catch users of the old memory */
991         tc->flags |= TALLOC_FLAG_FREE;
992
993 #if ALWAYS_REALLOC
994         new_ptr = malloc(size + TC_HDR_SIZE);
995         if (new_ptr) {
996                 memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
997                 free(tc);
998         }
999 #else
1000         if (tc->flags & TALLOC_FLAG_POOLMEM) {
1001
1002                 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
1003                 *talloc_pool_objectcount((struct talloc_chunk *)
1004                                          (tc->pool)) -= 1;
1005
1006                 if (new_ptr == NULL) {
1007                         new_ptr = malloc(TC_HDR_SIZE+size);
1008                         malloced = true;
1009                 }
1010
1011                 if (new_ptr) {
1012                         memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1013                 }
1014         }
1015         else {
1016                 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1017         }
1018 #endif
1019         if (unlikely(!new_ptr)) {       
1020                 tc->flags &= ~TALLOC_FLAG_FREE; 
1021                 return NULL; 
1022         }
1023
1024         tc = (struct talloc_chunk *)new_ptr;
1025         tc->flags &= ~TALLOC_FLAG_FREE;
1026         if (malloced) {
1027                 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1028         }
1029         if (tc->parent) {
1030                 tc->parent->child = tc;
1031         }
1032         if (tc->child) {
1033                 tc->child->parent = tc;
1034         }
1035
1036         if (tc->prev) {
1037                 tc->prev->next = tc;
1038         }
1039         if (tc->next) {
1040                 tc->next->prev = tc;
1041         }
1042
1043         tc->size = size;
1044         _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1045
1046         return TC_PTR_FROM_CHUNK(tc);
1047 }
1048
1049 /*
1050   a wrapper around talloc_steal() for situations where you are moving a pointer
1051   between two structures, and want the old pointer to be set to NULL
1052 */
1053 void *_talloc_move(const void *new_ctx, const void *_pptr)
1054 {
1055         const void **pptr = discard_const_p(const void *,_pptr);
1056         void *ret = _talloc_steal(new_ctx, *pptr);
1057         (*pptr) = NULL;
1058         return ret;
1059 }
1060
1061 /*
1062   return the total size of a talloc pool (subtree)
1063 */
1064 size_t talloc_total_size(const void *ptr)
1065 {
1066         size_t total = 0;
1067         struct talloc_chunk *c, *tc;
1068
1069         if (ptr == NULL) {
1070                 ptr = null_context;
1071         }
1072         if (ptr == NULL) {
1073                 return 0;
1074         }
1075
1076         tc = talloc_chunk_from_ptr(ptr);
1077
1078         if (tc->flags & TALLOC_FLAG_LOOP) {
1079                 return 0;
1080         }
1081
1082         tc->flags |= TALLOC_FLAG_LOOP;
1083
1084         total = tc->size;
1085         for (c=tc->child;c;c=c->next) {
1086                 total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
1087         }
1088
1089         tc->flags &= ~TALLOC_FLAG_LOOP;
1090
1091         return total;
1092 }
1093
1094 /*
1095   return the total number of blocks in a talloc pool (subtree)
1096 */
1097 size_t talloc_total_blocks(const void *ptr)
1098 {
1099         size_t total = 0;
1100         struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
1101
1102         if (tc->flags & TALLOC_FLAG_LOOP) {
1103                 return 0;
1104         }
1105
1106         tc->flags |= TALLOC_FLAG_LOOP;
1107
1108         total++;
1109         for (c=tc->child;c;c=c->next) {
1110                 total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
1111         }
1112
1113         tc->flags &= ~TALLOC_FLAG_LOOP;
1114
1115         return total;
1116 }
1117
1118 /*
1119   return the number of external references to a pointer
1120 */
1121 size_t talloc_reference_count(const void *ptr)
1122 {
1123         struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1124         struct talloc_reference_handle *h;
1125         size_t ret = 0;
1126
1127         for (h=tc->refs;h;h=h->next) {
1128                 ret++;
1129         }
1130         return ret;
1131 }
1132
1133 /*
1134   report on memory usage by all children of a pointer, giving a full tree view
1135 */
1136 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1137                             void (*callback)(const void *ptr,
1138                                              int depth, int max_depth,
1139                                              int is_ref,
1140                                              void *private_data),
1141                             void *private_data)
1142 {
1143         struct talloc_chunk *c, *tc;
1144
1145         if (ptr == NULL) {
1146                 ptr = null_context;
1147         }
1148         if (ptr == NULL) return;
1149
1150         tc = talloc_chunk_from_ptr(ptr);
1151
1152         if (tc->flags & TALLOC_FLAG_LOOP) {
1153                 return;
1154         }
1155
1156         callback(ptr, depth, max_depth, 0, private_data);
1157
1158         if (max_depth >= 0 && depth >= max_depth) {
1159                 return;
1160         }
1161
1162         tc->flags |= TALLOC_FLAG_LOOP;
1163         for (c=tc->child;c;c=c->next) {
1164                 if (c->name == TALLOC_MAGIC_REFERENCE) {
1165                         struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
1166                         callback(h->ptr, depth + 1, max_depth, 1, private_data);
1167                 } else {
1168                         talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
1169                 }
1170         }
1171         tc->flags &= ~TALLOC_FLAG_LOOP;
1172 }
1173
1174 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
1175 {
1176         const char *name = talloc_get_name(ptr);
1177         FILE *f = (FILE *)_f;
1178
1179         if (is_ref) {
1180                 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
1181                 return;
1182         }
1183
1184         if (depth == 0) {
1185                 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n", 
1186                         (max_depth < 0 ? "full " :""), name,
1187                         (unsigned long)talloc_total_size(ptr),
1188                         (unsigned long)talloc_total_blocks(ptr));
1189                 return;
1190         }
1191
1192         fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n", 
1193                 depth*4, "",
1194                 name,
1195                 (unsigned long)talloc_total_size(ptr),
1196                 (unsigned long)talloc_total_blocks(ptr),
1197                 (int)talloc_reference_count(ptr), ptr);
1198
1199 #if 0
1200         fprintf(f, "content: ");
1201         if (talloc_total_size(ptr)) {
1202                 int tot = talloc_total_size(ptr);
1203                 int i;
1204
1205                 for (i = 0; i < tot; i++) {
1206                         if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
1207                                 fprintf(f, "%c", ((char *)ptr)[i]);
1208                         } else {
1209                                 fprintf(f, "~%02x", ((char *)ptr)[i]);
1210                         }
1211                 }
1212         }
1213         fprintf(f, "\n");
1214 #endif
1215 }
1216
1217 /*
1218   report on memory usage by all children of a pointer, giving a full tree view
1219 */
1220 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
1221 {
1222         talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
1223         fflush(f);
1224 }
1225
1226 /*
1227   report on memory usage by all children of a pointer, giving a full tree view
1228 */
1229 void talloc_report_full(const void *ptr, FILE *f)
1230 {
1231         talloc_report_depth_file(ptr, 0, -1, f);
1232 }
1233
1234 /*
1235   report on memory usage by all children of a pointer
1236 */
1237 void talloc_report(const void *ptr, FILE *f)
1238 {
1239         talloc_report_depth_file(ptr, 0, 1, f);
1240 }
1241
1242 /*
1243   report on any memory hanging off the null context
1244 */
1245 static void talloc_report_null(void)
1246 {
1247         if (talloc_total_size(null_context) != 0) {
1248                 talloc_report(null_context, stderr);
1249         }
1250 }
1251
1252 /*
1253   report on any memory hanging off the null context
1254 */
1255 static void talloc_report_null_full(void)
1256 {
1257         if (talloc_total_size(null_context) != 0) {
1258                 talloc_report_full(null_context, stderr);
1259         }
1260 }
1261
1262 /*
1263   enable tracking of the NULL context
1264 */
1265 void talloc_enable_null_tracking(void)
1266 {
1267         if (null_context == NULL) {
1268                 null_context = _talloc_named_const(NULL, 0, "null_context");
1269         }
1270 }
1271
1272 /*
1273   disable tracking of the NULL context
1274 */
1275 void talloc_disable_null_tracking(void)
1276 {
1277         _talloc_free(null_context);
1278         null_context = NULL;
1279 }
1280
1281 /*
1282   enable leak reporting on exit
1283 */
1284 void talloc_enable_leak_report(void)
1285 {
1286         talloc_enable_null_tracking();
1287         atexit(talloc_report_null);
1288 }
1289
1290 /*
1291   enable full leak reporting on exit
1292 */
1293 void talloc_enable_leak_report_full(void)
1294 {
1295         talloc_enable_null_tracking();
1296         atexit(talloc_report_null_full);
1297 }
1298
1299 /* 
1300    talloc and zero memory. 
1301 */
1302 void *_talloc_zero(const void *ctx, size_t size, const char *name)
1303 {
1304         void *p = _talloc_named_const(ctx, size, name);
1305
1306         if (p) {
1307                 memset(p, '\0', size);
1308         }
1309
1310         return p;
1311 }
1312
1313 /*
1314   memdup with a talloc. 
1315 */
1316 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
1317 {
1318         void *newp = _talloc_named_const(t, size, name);
1319
1320         if (likely(newp)) {
1321                 memcpy(newp, p, size);
1322         }
1323
1324         return newp;
1325 }
1326
1327 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
1328 {
1329         char *ret;
1330
1331         ret = (char *)__talloc(t, len + 1);
1332         if (unlikely(!ret)) return NULL;
1333
1334         memcpy(ret, p, len);
1335         ret[len] = 0;
1336
1337         _talloc_set_name_const(ret, ret);
1338         return ret;
1339 }
1340
1341 /*
1342   strdup with a talloc
1343 */
1344 char *talloc_strdup(const void *t, const char *p)
1345 {
1346         if (unlikely(!p)) return NULL;
1347         return __talloc_strlendup(t, p, strlen(p));
1348 }
1349
1350 /*
1351   strndup with a talloc
1352 */
1353 char *talloc_strndup(const void *t, const char *p, size_t n)
1354 {
1355         if (unlikely(!p)) return NULL;
1356         return __talloc_strlendup(t, p, strnlen(p, n));
1357 }
1358
1359 static inline char *__talloc_strlendup_append(char *s, size_t slen,
1360                                               const char *a, size_t alen)
1361 {
1362         char *ret;
1363
1364         ret = talloc_realloc(NULL, s, char, slen + alen + 1);
1365         if (unlikely(!ret)) return NULL;
1366
1367         /* append the string and the trailing \0 */
1368         memcpy(&ret[slen], a, alen);
1369         ret[slen+alen] = 0;
1370
1371         _talloc_set_name_const(ret, ret);
1372         return ret;
1373 }
1374
1375 /*
1376  * Appends at the end of the string.
1377  */
1378 char *talloc_strdup_append(char *s, const char *a)
1379 {
1380         if (unlikely(!s)) {
1381                 return talloc_strdup(NULL, a);
1382         }
1383
1384         if (unlikely(!a)) {
1385                 return s;
1386         }
1387
1388         return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
1389 }
1390
1391 /*
1392  * Appends at the end of the talloc'ed buffer,
1393  * not the end of the string.
1394  */
1395 char *talloc_strdup_append_buffer(char *s, const char *a)
1396 {
1397         size_t slen;
1398
1399         if (unlikely(!s)) {
1400                 return talloc_strdup(NULL, a);
1401         }
1402
1403         if (unlikely(!a)) {
1404                 return s;
1405         }
1406
1407         slen = talloc_get_size(s);
1408         if (likely(slen > 0)) {
1409                 slen--;
1410         }
1411
1412         return __talloc_strlendup_append(s, slen, a, strlen(a));
1413 }
1414
1415 /*
1416  * Appends at the end of the string.
1417  */
1418 char *talloc_strndup_append(char *s, const char *a, size_t n)
1419 {
1420         if (unlikely(!s)) {
1421                 return talloc_strdup(NULL, a);
1422         }
1423
1424         if (unlikely(!a)) {
1425                 return s;
1426         }
1427
1428         return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
1429 }
1430
1431 /*
1432  * Appends at the end of the talloc'ed buffer,
1433  * not the end of the string.
1434  */
1435 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
1436 {
1437         size_t slen;
1438
1439         if (unlikely(!s)) {
1440                 return talloc_strdup(NULL, a);
1441         }
1442
1443         if (unlikely(!a)) {
1444                 return s;
1445         }
1446
1447         slen = talloc_get_size(s);
1448         if (likely(slen > 0)) {
1449                 slen--;
1450         }
1451
1452         return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
1453 }
1454
1455 #ifndef HAVE_VA_COPY
1456 #ifdef HAVE___VA_COPY
1457 #define va_copy(dest, src) __va_copy(dest, src)
1458 #else
1459 #define va_copy(dest, src) (dest) = (src)
1460 #endif
1461 #endif
1462
1463 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
1464 {
1465         int len;
1466         char *ret;
1467         va_list ap2;
1468         char c;
1469
1470         /* this call looks strange, but it makes it work on older solaris boxes */
1471         va_copy(ap2, ap);
1472         len = vsnprintf(&c, 1, fmt, ap2);
1473         va_end(ap2);
1474         if (unlikely(len < 0)) {
1475                 return NULL;
1476         }
1477
1478         ret = (char *)__talloc(t, len+1);
1479         if (unlikely(!ret)) return NULL;
1480
1481         va_copy(ap2, ap);
1482         vsnprintf(ret, len+1, fmt, ap2);
1483         va_end(ap2);
1484
1485         _talloc_set_name_const(ret, ret);
1486         return ret;
1487 }
1488
1489
1490 /*
1491   Perform string formatting, and return a pointer to newly allocated
1492   memory holding the result, inside a memory pool.
1493  */
1494 char *talloc_asprintf(const void *t, const char *fmt, ...)
1495 {
1496         va_list ap;
1497         char *ret;
1498
1499         va_start(ap, fmt);
1500         ret = talloc_vasprintf(t, fmt, ap);
1501         va_end(ap);
1502         return ret;
1503 }
1504
1505 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1506                                                  const char *fmt, va_list ap)
1507                                                  PRINTF_ATTRIBUTE(3,0);
1508
1509 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
1510                                                  const char *fmt, va_list ap)
1511 {
1512         ssize_t alen;
1513         va_list ap2;
1514         char c;
1515
1516         va_copy(ap2, ap);
1517         alen = vsnprintf(&c, 1, fmt, ap2);
1518         va_end(ap2);
1519
1520         if (alen <= 0) {
1521                 /* Either the vsnprintf failed or the format resulted in
1522                  * no characters being formatted. In the former case, we
1523                  * ought to return NULL, in the latter we ought to return
1524                  * the original string. Most current callers of this
1525                  * function expect it to never return NULL.
1526                  */
1527                 return s;
1528         }
1529
1530         s = talloc_realloc(NULL, s, char, slen + alen + 1);
1531         if (!s) return NULL;
1532
1533         va_copy(ap2, ap);
1534         vsnprintf(s + slen, alen + 1, fmt, ap2);
1535         va_end(ap2);
1536
1537         _talloc_set_name_const(s, s);
1538         return s;
1539 }
1540
1541 /**
1542  * Realloc @p s to append the formatted result of @p fmt and @p ap,
1543  * and return @p s, which may have moved.  Good for gradually
1544  * accumulating output into a string buffer. Appends at the end
1545  * of the string.
1546  **/
1547 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
1548 {
1549         if (unlikely(!s)) {
1550                 return talloc_vasprintf(NULL, fmt, ap);
1551         }
1552
1553         return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
1554 }
1555
1556 /**
1557  * Realloc @p s to append the formatted result of @p fmt and @p ap,
1558  * and return @p s, which may have moved. Always appends at the
1559  * end of the talloc'ed buffer, not the end of the string.
1560  **/
1561 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
1562 {
1563         size_t slen;
1564
1565         if (unlikely(!s)) {
1566                 return talloc_vasprintf(NULL, fmt, ap);
1567         }
1568
1569         slen = talloc_get_size(s);
1570         if (likely(slen > 0)) {
1571                 slen--;
1572         }
1573
1574         return __talloc_vaslenprintf_append(s, slen, fmt, ap);
1575 }
1576
1577 /*
1578   Realloc @p s to append the formatted result of @p fmt and return @p
1579   s, which may have moved.  Good for gradually accumulating output
1580   into a string buffer.
1581  */
1582 char *talloc_asprintf_append(char *s, const char *fmt, ...)
1583 {
1584         va_list ap;
1585
1586         va_start(ap, fmt);
1587         s = talloc_vasprintf_append(s, fmt, ap);
1588         va_end(ap);
1589         return s;
1590 }
1591
1592 /*
1593   Realloc @p s to append the formatted result of @p fmt and return @p
1594   s, which may have moved.  Good for gradually accumulating output
1595   into a buffer.
1596  */
1597 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
1598 {
1599         va_list ap;
1600
1601         va_start(ap, fmt);
1602         s = talloc_vasprintf_append_buffer(s, fmt, ap);
1603         va_end(ap);
1604         return s;
1605 }
1606
1607 /*
1608   alloc an array, checking for integer overflow in the array size
1609 */
1610 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1611 {
1612         if (count >= MAX_TALLOC_SIZE/el_size) {
1613                 return NULL;
1614         }
1615         return _talloc_named_const(ctx, el_size * count, name);
1616 }
1617
1618 /*
1619   alloc an zero array, checking for integer overflow in the array size
1620 */
1621 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
1622 {
1623         if (count >= MAX_TALLOC_SIZE/el_size) {
1624                 return NULL;
1625         }
1626         return _talloc_zero(ctx, el_size * count, name);
1627 }
1628
1629 /*
1630   realloc an array, checking for integer overflow in the array size
1631 */
1632 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
1633 {
1634         if (count >= MAX_TALLOC_SIZE/el_size) {
1635                 return NULL;
1636         }
1637         return _talloc_realloc(ctx, ptr, el_size * count, name);
1638 }
1639
1640 /*
1641   a function version of talloc_realloc(), so it can be passed as a function pointer
1642   to libraries that want a realloc function (a realloc function encapsulates
1643   all the basic capabilities of an allocation library, which is why this is useful)
1644 */
1645 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
1646 {
1647         return _talloc_realloc(context, ptr, size, NULL);
1648 }
1649
1650
1651 static int talloc_autofree_destructor(void *ptr)
1652 {
1653         autofree_context = NULL;
1654         return 0;
1655 }
1656
1657 static void talloc_autofree(void)
1658 {
1659         _talloc_free(autofree_context);
1660 }
1661
1662 /*
1663   return a context which will be auto-freed on exit
1664   this is useful for reducing the noise in leak reports
1665 */
1666 void *talloc_autofree_context(void)
1667 {
1668         if (autofree_context == NULL) {
1669                 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
1670                 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
1671                 atexit(talloc_autofree);
1672         }
1673         return autofree_context;
1674 }
1675
1676 size_t talloc_get_size(const void *context)
1677 {
1678         struct talloc_chunk *tc;
1679
1680         if (context == NULL)
1681                 return 0;
1682
1683         tc = talloc_chunk_from_ptr(context);
1684
1685         return tc->size;
1686 }
1687
1688 /*
1689   find a parent of this context that has the given name, if any
1690 */
1691 void *talloc_find_parent_byname(const void *context, const char *name)
1692 {
1693         struct talloc_chunk *tc;
1694
1695         if (context == NULL) {
1696                 return NULL;
1697         }
1698
1699         tc = talloc_chunk_from_ptr(context);
1700         while (tc) {
1701                 if (tc->name && strcmp(tc->name, name) == 0) {
1702                         return TC_PTR_FROM_CHUNK(tc);
1703                 }
1704                 while (tc && tc->prev) tc = tc->prev;
1705                 if (tc) {
1706                         tc = tc->parent;
1707                 }
1708         }
1709         return NULL;
1710 }
1711
1712 /*
1713   show the parentage of a context
1714 */
1715 void talloc_show_parents(const void *context, FILE *file)
1716 {
1717         struct talloc_chunk *tc;
1718
1719         if (context == NULL) {
1720                 fprintf(file, "talloc no parents for NULL\n");
1721                 return;
1722         }
1723
1724         tc = talloc_chunk_from_ptr(context);
1725         fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
1726         while (tc) {
1727                 fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
1728                 while (tc && tc->prev) tc = tc->prev;
1729                 if (tc) {
1730                         tc = tc->parent;
1731                 }
1732         }
1733         fflush(file);
1734 }
1735
1736 /*
1737   return 1 if ptr is a parent of context
1738 */
1739 int talloc_is_parent(const void *context, const void *ptr)
1740 {
1741         struct talloc_chunk *tc;
1742
1743         if (context == NULL) {
1744                 return 0;
1745         }
1746
1747         tc = talloc_chunk_from_ptr(context);
1748         while (tc) {
1749                 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
1750                 while (tc && tc->prev) tc = tc->prev;
1751                 if (tc) {
1752                         tc = tc->parent;
1753                 }
1754         }
1755         return 0;
1756 }