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