lib/talloc: Disrupt buffer overflow attacks on Samba by using random magic
[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 #include "replace.h"
34 #include "talloc.h"
35
36 #ifdef HAVE_SYS_AUXV_H
37 #include <sys/auxv.h>
38 #endif
39
40 #ifdef TALLOC_BUILD_VERSION_MAJOR
41 #if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)
42 #error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"
43 #endif
44 #endif
45
46 #ifdef TALLOC_BUILD_VERSION_MINOR
47 #if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)
48 #error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"
49 #endif
50 #endif
51
52 /* Special macros that are no-ops except when run under Valgrind on
53  * x86.  They've moved a little bit from valgrind 1.0.4 to 1.9.4 */
54 #ifdef HAVE_VALGRIND_MEMCHECK_H
55         /* memcheck.h includes valgrind.h */
56 #include <valgrind/memcheck.h>
57 #elif defined(HAVE_VALGRIND_H)
58 #include <valgrind.h>
59 #endif
60
61 /* use this to force every realloc to change the pointer, to stress test
62    code that might not cope */
63 #define ALWAYS_REALLOC 0
64
65
66 #define MAX_TALLOC_SIZE 0x10000000
67
68 #define TALLOC_FLAG_FREE 0x01
69 #define TALLOC_FLAG_LOOP 0x02
70 #define TALLOC_FLAG_POOL 0x04           /* This is a talloc pool */
71 #define TALLOC_FLAG_POOLMEM 0x08        /* This is allocated in a pool */
72
73 /*
74  * Bits above this are random, used to make it harder to fake talloc
75  * headers during an attack.  Try not to change this without good reason.
76  */
77 #define TALLOC_FLAG_MASK 0x0F
78
79 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
80
81 #define TALLOC_MAGIC_BASE 0xe814ec70
82 static unsigned int talloc_magic = (
83         TALLOC_MAGIC_BASE +
84         (TALLOC_VERSION_MAJOR << 12) +
85         (TALLOC_VERSION_MINOR << 4));
86
87 /* by default we abort when given a bad pointer (such as when talloc_free() is called
88    on a pointer that came from malloc() */
89 #ifndef TALLOC_ABORT
90 #define TALLOC_ABORT(reason) abort()
91 #endif
92
93 #ifndef discard_const_p
94 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
95 # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
96 #else
97 # define discard_const_p(type, ptr) ((type *)(ptr))
98 #endif
99 #endif
100
101 /* these macros gain us a few percent of speed on gcc */
102 #if (__GNUC__ >= 3)
103 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
104    as its first argument */
105 #ifndef likely
106 #define likely(x)   __builtin_expect(!!(x), 1)
107 #endif
108 #ifndef unlikely
109 #define unlikely(x) __builtin_expect(!!(x), 0)
110 #endif
111 #else
112 #ifndef likely
113 #define likely(x) (x)
114 #endif
115 #ifndef unlikely
116 #define unlikely(x) (x)
117 #endif
118 #endif
119
120 /* this null_context is only used if talloc_enable_leak_report() or
121    talloc_enable_leak_report_full() is called, otherwise it remains
122    NULL
123 */
124 static void *null_context;
125 static void *autofree_context;
126
127 /* used to enable fill of memory on free, which can be useful for
128  * catching use after free errors when valgrind is too slow
129  */
130 static struct {
131         bool initialised;
132         bool enabled;
133         uint8_t fill_value;
134 } talloc_fill;
135
136 #define TALLOC_FILL_ENV "TALLOC_FREE_FILL"
137
138 /*
139  * do not wipe the header, to allow the
140  * double-free logic to still work
141  */
142 #define TC_INVALIDATE_FULL_FILL_CHUNK(_tc) do { \
143         if (unlikely(talloc_fill.enabled)) { \
144                 size_t _flen = (_tc)->size; \
145                 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
146                 memset(_fptr, talloc_fill.fill_value, _flen); \
147         } \
148 } while (0)
149
150 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
151 /* Mark the whole chunk as not accessable */
152 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { \
153         size_t _flen = TC_HDR_SIZE + (_tc)->size; \
154         char *_fptr = (char *)(_tc); \
155         VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
156 } while(0)
157 #else
158 #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { } while (0)
159 #endif
160
161 #define TC_INVALIDATE_FULL_CHUNK(_tc) do { \
162         TC_INVALIDATE_FULL_FILL_CHUNK(_tc); \
163         TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc); \
164 } while (0)
165
166 #define TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
167         if (unlikely(talloc_fill.enabled)) { \
168                 size_t _flen = (_tc)->size - (_new_size); \
169                 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
170                 _fptr += (_new_size); \
171                 memset(_fptr, talloc_fill.fill_value, _flen); \
172         } \
173 } while (0)
174
175 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
176 /* Mark the unused bytes not accessable */
177 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
178         size_t _flen = (_tc)->size - (_new_size); \
179         char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
180         _fptr += (_new_size); \
181         VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
182 } while (0)
183 #else
184 #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
185 #endif
186
187 #define TC_INVALIDATE_SHRINK_CHUNK(_tc, _new_size) do { \
188         TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size); \
189         TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
190 } while (0)
191
192 #define TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
193         if (unlikely(talloc_fill.enabled)) { \
194                 size_t _flen = (_tc)->size - (_new_size); \
195                 char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
196                 _fptr += (_new_size); \
197                 memset(_fptr, talloc_fill.fill_value, _flen); \
198         } \
199 } while (0)
200
201 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
202 /* Mark the unused bytes as undefined */
203 #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
204         size_t _flen = (_tc)->size - (_new_size); \
205         char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
206         _fptr += (_new_size); \
207         VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
208 } while (0)
209 #else
210 #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
211 #endif
212
213 #define TC_UNDEFINE_SHRINK_CHUNK(_tc, _new_size) do { \
214         TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size); \
215         TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
216 } while (0)
217
218 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
219 /* Mark the new bytes as undefined */
220 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { \
221         size_t _old_used = TC_HDR_SIZE + (_tc)->size; \
222         size_t _new_used = TC_HDR_SIZE + (_new_size); \
223         size_t _flen = _new_used - _old_used; \
224         char *_fptr = _old_used + (char *)(_tc); \
225         VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
226 } while (0)
227 #else
228 #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
229 #endif
230
231 #define TC_UNDEFINE_GROW_CHUNK(_tc, _new_size) do { \
232         TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
233 } while (0)
234
235 struct talloc_reference_handle {
236         struct talloc_reference_handle *next, *prev;
237         void *ptr;
238         const char *location;
239 };
240
241 struct talloc_memlimit {
242         struct talloc_chunk *parent;
243         struct talloc_memlimit *upper;
244         size_t max_size;
245         size_t cur_size;
246 };
247
248 static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size);
249 static inline void talloc_memlimit_grow(struct talloc_memlimit *limit,
250                                 size_t size);
251 static inline void talloc_memlimit_shrink(struct talloc_memlimit *limit,
252                                 size_t size);
253 static inline void talloc_memlimit_update_on_free(struct talloc_chunk *tc);
254
255 static inline void _talloc_set_name_const(const void *ptr, const char *name);
256
257 typedef int (*talloc_destructor_t)(void *);
258
259 struct talloc_pool_hdr;
260
261 struct talloc_chunk {
262         unsigned flags;
263         struct talloc_chunk *next, *prev;
264         struct talloc_chunk *parent, *child;
265         struct talloc_reference_handle *refs;
266         talloc_destructor_t destructor;
267         const char *name;
268         size_t size;
269
270         /*
271          * limit semantics:
272          * if 'limit' is set it means all *new* children of the context will
273          * be limited to a total aggregate size ox max_size for memory
274          * allocations.
275          * cur_size is used to keep track of the current use
276          */
277         struct talloc_memlimit *limit;
278
279         /*
280          * For members of a pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
281          * is a pointer to the struct talloc_chunk of the pool that it was
282          * allocated from. This way children can quickly find the pool to chew
283          * from.
284          */
285         struct talloc_pool_hdr *pool;
286 };
287
288 /* 16 byte alignment seems to keep everyone happy */
289 #define TC_ALIGN16(s) (((s)+15)&~15)
290 #define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
291 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
292
293 _PUBLIC_ int talloc_version_major(void)
294 {
295         return TALLOC_VERSION_MAJOR;
296 }
297
298 _PUBLIC_ int talloc_version_minor(void)
299 {
300         return TALLOC_VERSION_MINOR;
301 }
302
303 static void (*talloc_log_fn)(const char *message);
304
305 _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
306 {
307         talloc_log_fn = log_fn;
308 }
309
310 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
311 void talloc_lib_init(void) __attribute__((constructor));
312 void talloc_lib_init(void)
313 {
314         uint32_t random_value;
315 #if defined(HAVE_GETAUXVAL) && defined(AT_RANDOM)
316         uint8_t *p;
317         /*
318          * Use the kernel-provided random values used for
319          * ASLR.  This won't change per-exec, which is ideal for us
320          */
321         p = (uint8_t *) getauxval(AT_RANDOM);
322         if (p) {
323                 memcpy(&random_value, p, sizeof(random_value));
324         } else
325 #endif
326         {
327                 /*
328                  * Otherwise, hope the location we are loaded in
329                  * memory is randomised by someone else
330                  */
331                 random_value = ((uintptr_t)talloc_lib_init & 0xFFFFFFFF);
332         }
333         talloc_magic = random_value & ~TALLOC_FLAG_MASK;
334 }
335 #else
336 #warning "No __attribute__((constructor)) support found on this platform, additional talloc security measures not available"
337 #endif
338
339 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
340 static void talloc_log(const char *fmt, ...)
341 {
342         va_list ap;
343         char *message;
344
345         if (!talloc_log_fn) {
346                 return;
347         }
348
349         va_start(ap, fmt);
350         message = talloc_vasprintf(NULL, fmt, ap);
351         va_end(ap);
352
353         talloc_log_fn(message);
354         talloc_free(message);
355 }
356
357 static void talloc_log_stderr(const char *message)
358 {
359         fprintf(stderr, "%s", message);
360 }
361
362 _PUBLIC_ void talloc_set_log_stderr(void)
363 {
364         talloc_set_log_fn(talloc_log_stderr);
365 }
366
367 static void (*talloc_abort_fn)(const char *reason);
368
369 _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
370 {
371         talloc_abort_fn = abort_fn;
372 }
373
374 static void talloc_abort(const char *reason)
375 {
376         talloc_log("%s\n", reason);
377
378         if (!talloc_abort_fn) {
379                 TALLOC_ABORT(reason);
380         }
381
382         talloc_abort_fn(reason);
383 }
384
385 static void talloc_abort_magic(unsigned magic)
386 {
387         talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
388 }
389
390 static void talloc_abort_access_after_free(void)
391 {
392         talloc_abort("Bad talloc magic value - access after free");
393 }
394
395 static void talloc_abort_unknown_value(void)
396 {
397         talloc_abort("Bad talloc magic value - unknown value");
398 }
399
400 /* panic if we get a bad magic value */
401 static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
402 {
403         const char *pp = (const char *)ptr;
404         struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
405         if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~TALLOC_FLAG_MASK)) != talloc_magic)) {
406                 if ((tc->flags & (~0xF)) == talloc_magic) {
407                         talloc_abort_magic(tc->flags & (~TALLOC_FLAG_MASK));
408                         return NULL;
409                 }
410
411                 if (tc->flags & TALLOC_FLAG_FREE) {
412                         talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
413                         talloc_abort_access_after_free();
414                         return NULL;
415                 } else {
416                         talloc_abort_unknown_value();
417                         return NULL;
418                 }
419         }
420         return tc;
421 }
422
423 /* hook into the front of the list */
424 #define _TLIST_ADD(list, p) \
425 do { \
426         if (!(list)) { \
427                 (list) = (p); \
428                 (p)->next = (p)->prev = NULL; \
429         } else { \
430                 (list)->prev = (p); \
431                 (p)->next = (list); \
432                 (p)->prev = NULL; \
433                 (list) = (p); \
434         }\
435 } while (0)
436
437 /* remove an element from a list - element doesn't have to be in list. */
438 #define _TLIST_REMOVE(list, p) \
439 do { \
440         if ((p) == (list)) { \
441                 (list) = (p)->next; \
442                 if (list) (list)->prev = NULL; \
443         } else { \
444                 if ((p)->prev) (p)->prev->next = (p)->next; \
445                 if ((p)->next) (p)->next->prev = (p)->prev; \
446         } \
447         if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
448 } while (0)
449
450
451 /*
452   return the parent chunk of a pointer
453 */
454 static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
455 {
456         struct talloc_chunk *tc;
457
458         if (unlikely(ptr == NULL)) {
459                 return NULL;
460         }
461
462         tc = talloc_chunk_from_ptr(ptr);
463         while (tc->prev) tc=tc->prev;
464
465         return tc->parent;
466 }
467
468 _PUBLIC_ void *talloc_parent(const void *ptr)
469 {
470         struct talloc_chunk *tc = talloc_parent_chunk(ptr);
471         return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
472 }
473
474 /*
475   find parents name
476 */
477 _PUBLIC_ const char *talloc_parent_name(const void *ptr)
478 {
479         struct talloc_chunk *tc = talloc_parent_chunk(ptr);
480         return tc? tc->name : NULL;
481 }
482
483 /*
484   A pool carries an in-pool object count count in the first 16 bytes.
485   bytes. This is done to support talloc_steal() to a parent outside of the
486   pool. The count includes the pool itself, so a talloc_free() on a pool will
487   only destroy the pool if the count has dropped to zero. A talloc_free() of a
488   pool member will reduce the count, and eventually also call free(3) on the
489   pool memory.
490
491   The object count is not put into "struct talloc_chunk" because it is only
492   relevant for talloc pools and the alignment to 16 bytes would increase the
493   memory footprint of each talloc chunk by those 16 bytes.
494 */
495
496 struct talloc_pool_hdr {
497         void *end;
498         unsigned int object_count;
499         size_t poolsize;
500 };
501
502 #define TP_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_pool_hdr))
503
504 static inline struct talloc_pool_hdr *talloc_pool_from_chunk(struct talloc_chunk *c)
505 {
506         return (struct talloc_pool_hdr *)((char *)c - TP_HDR_SIZE);
507 }
508
509 static inline struct talloc_chunk *talloc_chunk_from_pool(struct talloc_pool_hdr *h)
510 {
511         return (struct talloc_chunk *)((char *)h + TP_HDR_SIZE);
512 }
513
514 static inline void *tc_pool_end(struct talloc_pool_hdr *pool_hdr)
515 {
516         struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);
517         return (char *)tc + TC_HDR_SIZE + pool_hdr->poolsize;
518 }
519
520 static inline size_t tc_pool_space_left(struct talloc_pool_hdr *pool_hdr)
521 {
522         return (char *)tc_pool_end(pool_hdr) - (char *)pool_hdr->end;
523 }
524
525 /* If tc is inside a pool, this gives the next neighbour. */
526 static inline void *tc_next_chunk(struct talloc_chunk *tc)
527 {
528         return (char *)tc + TC_ALIGN16(TC_HDR_SIZE + tc->size);
529 }
530
531 static inline void *tc_pool_first_chunk(struct talloc_pool_hdr *pool_hdr)
532 {
533         struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);
534         return tc_next_chunk(tc);
535 }
536
537 /* Mark the whole remaining pool as not accessable */
538 static inline void tc_invalidate_pool(struct talloc_pool_hdr *pool_hdr)
539 {
540         size_t flen = tc_pool_space_left(pool_hdr);
541
542         if (unlikely(talloc_fill.enabled)) {
543                 memset(pool_hdr->end, talloc_fill.fill_value, flen);
544         }
545
546 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
547         VALGRIND_MAKE_MEM_NOACCESS(pool_hdr->end, flen);
548 #endif
549 }
550
551 /*
552   Allocate from a pool
553 */
554
555 static inline struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
556                                                      size_t size, size_t prefix_len)
557 {
558         struct talloc_pool_hdr *pool_hdr = NULL;
559         size_t space_left;
560         struct talloc_chunk *result;
561         size_t chunk_size;
562
563         if (parent == NULL) {
564                 return NULL;
565         }
566
567         if (parent->flags & TALLOC_FLAG_POOL) {
568                 pool_hdr = talloc_pool_from_chunk(parent);
569         }
570         else if (parent->flags & TALLOC_FLAG_POOLMEM) {
571                 pool_hdr = parent->pool;
572         }
573
574         if (pool_hdr == NULL) {
575                 return NULL;
576         }
577
578         space_left = tc_pool_space_left(pool_hdr);
579
580         /*
581          * Align size to 16 bytes
582          */
583         chunk_size = TC_ALIGN16(size + prefix_len);
584
585         if (space_left < chunk_size) {
586                 return NULL;
587         }
588
589         result = (struct talloc_chunk *)((char *)pool_hdr->end + prefix_len);
590
591 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
592         VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, chunk_size);
593 #endif
594
595         pool_hdr->end = (void *)((char *)pool_hdr->end + chunk_size);
596
597         result->flags = talloc_magic | TALLOC_FLAG_POOLMEM;
598         result->pool = pool_hdr;
599
600         pool_hdr->object_count++;
601
602         return result;
603 }
604
605 /*
606    Allocate a bit of memory as a child of an existing pointer
607 */
608 static inline void *__talloc_with_prefix(const void *context, size_t size,
609                                         size_t prefix_len)
610 {
611         struct talloc_chunk *tc = NULL;
612         struct talloc_memlimit *limit = NULL;
613         size_t total_len = TC_HDR_SIZE + size + prefix_len;
614
615         if (unlikely(context == NULL)) {
616                 context = null_context;
617         }
618
619         if (unlikely(size >= MAX_TALLOC_SIZE)) {
620                 return NULL;
621         }
622
623         if (unlikely(total_len < TC_HDR_SIZE)) {
624                 return NULL;
625         }
626
627         if (context != NULL) {
628                 struct talloc_chunk *ptc = talloc_chunk_from_ptr(context);
629
630                 if (ptc->limit != NULL) {
631                         limit = ptc->limit;
632                 }
633
634                 tc = talloc_alloc_pool(ptc, TC_HDR_SIZE+size, prefix_len);
635         }
636
637         if (tc == NULL) {
638                 char *ptr;
639
640                 /*
641                  * Only do the memlimit check/update on actual allocation.
642                  */
643                 if (!talloc_memlimit_check(limit, total_len)) {
644                         errno = ENOMEM;
645                         return NULL;
646                 }
647
648                 ptr = malloc(total_len);
649                 if (unlikely(ptr == NULL)) {
650                         return NULL;
651                 }
652                 tc = (struct talloc_chunk *)(ptr + prefix_len);
653                 tc->flags = talloc_magic;
654                 tc->pool  = NULL;
655
656                 talloc_memlimit_grow(limit, total_len);
657         }
658
659         tc->limit = limit;
660         tc->size = size;
661         tc->destructor = NULL;
662         tc->child = NULL;
663         tc->name = NULL;
664         tc->refs = NULL;
665
666         if (likely(context)) {
667                 struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
668
669                 if (parent->child) {
670                         parent->child->parent = NULL;
671                         tc->next = parent->child;
672                         tc->next->prev = tc;
673                 } else {
674                         tc->next = NULL;
675                 }
676                 tc->parent = parent;
677                 tc->prev = NULL;
678                 parent->child = tc;
679         } else {
680                 tc->next = tc->prev = tc->parent = NULL;
681         }
682
683         return TC_PTR_FROM_CHUNK(tc);
684 }
685
686 static inline void *__talloc(const void *context, size_t size)
687 {
688         return __talloc_with_prefix(context, size, 0);
689 }
690
691 /*
692  * Create a talloc pool
693  */
694
695 static inline void *_talloc_pool(const void *context, size_t size)
696 {
697         struct talloc_chunk *tc;
698         struct talloc_pool_hdr *pool_hdr;
699         void *result;
700
701         result = __talloc_with_prefix(context, size, TP_HDR_SIZE);
702
703         if (unlikely(result == NULL)) {
704                 return NULL;
705         }
706
707         tc = talloc_chunk_from_ptr(result);
708         pool_hdr = talloc_pool_from_chunk(tc);
709
710         tc->flags |= TALLOC_FLAG_POOL;
711         tc->size = 0;
712
713         pool_hdr->object_count = 1;
714         pool_hdr->end = result;
715         pool_hdr->poolsize = size;
716
717         tc_invalidate_pool(pool_hdr);
718
719         return result;
720 }
721
722 _PUBLIC_ void *talloc_pool(const void *context, size_t size)
723 {
724         return _talloc_pool(context, size);
725 }
726
727 /*
728  * Create a talloc pool correctly sized for a basic size plus
729  * a number of subobjects whose total size is given. Essentially
730  * a custom allocator for talloc to reduce fragmentation.
731  */
732
733 _PUBLIC_ void *_talloc_pooled_object(const void *ctx,
734                                      size_t type_size,
735                                      const char *type_name,
736                                      unsigned num_subobjects,
737                                      size_t total_subobjects_size)
738 {
739         size_t poolsize, subobjects_slack, tmp;
740         struct talloc_chunk *tc;
741         struct talloc_pool_hdr *pool_hdr;
742         void *ret;
743
744         poolsize = type_size + total_subobjects_size;
745
746         if ((poolsize < type_size) || (poolsize < total_subobjects_size)) {
747                 goto overflow;
748         }
749
750         if (num_subobjects == UINT_MAX) {
751                 goto overflow;
752         }
753         num_subobjects += 1;       /* the object body itself */
754
755         /*
756          * Alignment can increase the pool size by at most 15 bytes per object
757          * plus alignment for the object itself
758          */
759         subobjects_slack = (TC_HDR_SIZE + TP_HDR_SIZE + 15) * num_subobjects;
760         if (subobjects_slack < num_subobjects) {
761                 goto overflow;
762         }
763
764         tmp = poolsize + subobjects_slack;
765         if ((tmp < poolsize) || (tmp < subobjects_slack)) {
766                 goto overflow;
767         }
768         poolsize = tmp;
769
770         ret = _talloc_pool(ctx, poolsize);
771         if (ret == NULL) {
772                 return NULL;
773         }
774
775         tc = talloc_chunk_from_ptr(ret);
776         tc->size = type_size;
777
778         pool_hdr = talloc_pool_from_chunk(tc);
779
780 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
781         VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, type_size);
782 #endif
783
784         pool_hdr->end = ((char *)pool_hdr->end + TC_ALIGN16(type_size));
785
786         _talloc_set_name_const(ret, type_name);
787         return ret;
788
789 overflow:
790         return NULL;
791 }
792
793 /*
794   setup a destructor to be called on free of a pointer
795   the destructor should return 0 on success, or -1 on failure.
796   if the destructor fails then the free is failed, and the memory can
797   be continued to be used
798 */
799 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
800 {
801         struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
802         tc->destructor = destructor;
803 }
804
805 /*
806   increase the reference count on a piece of memory.
807 */
808 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
809 {
810         if (unlikely(!talloc_reference(null_context, ptr))) {
811                 return -1;
812         }
813         return 0;
814 }
815
816 /*
817   helper for talloc_reference()
818
819   this is referenced by a function pointer and should not be inline
820 */
821 static int talloc_reference_destructor(struct talloc_reference_handle *handle)
822 {
823         struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
824         _TLIST_REMOVE(ptr_tc->refs, handle);
825         return 0;
826 }
827
828 /*
829    more efficient way to add a name to a pointer - the name must point to a
830    true string constant
831 */
832 static inline void _talloc_set_name_const(const void *ptr, const char *name)
833 {
834         struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
835         tc->name = name;
836 }
837
838 /*
839   internal talloc_named_const()
840 */
841 static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
842 {
843         void *ptr;
844
845         ptr = __talloc(context, size);
846         if (unlikely(ptr == NULL)) {
847                 return NULL;
848         }
849
850         _talloc_set_name_const(ptr, name);
851
852         return ptr;
853 }
854
855 /*
856   make a secondary reference to a pointer, hanging off the given context.
857   the pointer remains valid until both the original caller and this given
858   context are freed.
859
860   the major use for this is when two different structures need to reference the
861   same underlying data, and you want to be able to free the two instances separately,
862   and in either order
863 */
864 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
865 {
866         struct talloc_chunk *tc;
867         struct talloc_reference_handle *handle;
868         if (unlikely(ptr == NULL)) return NULL;
869
870         tc = talloc_chunk_from_ptr(ptr);
871         handle = (struct talloc_reference_handle *)_talloc_named_const(context,
872                                                    sizeof(struct talloc_reference_handle),
873                                                    TALLOC_MAGIC_REFERENCE);
874         if (unlikely(handle == NULL)) return NULL;
875
876         /* note that we hang the destructor off the handle, not the
877            main context as that allows the caller to still setup their
878            own destructor on the context if they want to */
879         talloc_set_destructor(handle, talloc_reference_destructor);
880         handle->ptr = discard_const_p(void, ptr);
881         handle->location = location;
882         _TLIST_ADD(tc->refs, handle);
883         return handle->ptr;
884 }
885
886 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
887
888 static inline void _talloc_free_poolmem(struct talloc_chunk *tc,
889                                         const char *location)
890 {
891         struct talloc_pool_hdr *pool;
892         struct talloc_chunk *pool_tc;
893         void *next_tc;
894
895         pool = tc->pool;
896         pool_tc = talloc_chunk_from_pool(pool);
897         next_tc = tc_next_chunk(tc);
898
899         tc->flags |= TALLOC_FLAG_FREE;
900
901         /* we mark the freed memory with where we called the free
902          * from. This means on a double free error we can report where
903          * the first free came from
904          */
905         tc->name = location;
906
907         TC_INVALIDATE_FULL_CHUNK(tc);
908
909         if (unlikely(pool->object_count == 0)) {
910                 talloc_abort("Pool object count zero!");
911                 return;
912         }
913
914         pool->object_count--;
915
916         if (unlikely(pool->object_count == 1
917                      && !(pool_tc->flags & TALLOC_FLAG_FREE))) {
918                 /*
919                  * if there is just one object left in the pool
920                  * and pool->flags does not have TALLOC_FLAG_FREE,
921                  * it means this is the pool itself and
922                  * the rest is available for new objects
923                  * again.
924                  */
925                 pool->end = tc_pool_first_chunk(pool);
926                 tc_invalidate_pool(pool);
927                 return;
928         }
929
930         if (unlikely(pool->object_count == 0)) {
931                 /*
932                  * we mark the freed memory with where we called the free
933                  * from. This means on a double free error we can report where
934                  * the first free came from
935                  */
936                 pool_tc->name = location;
937
938                 if (pool_tc->flags & TALLOC_FLAG_POOLMEM) {
939                         _talloc_free_poolmem(pool_tc, location);
940                 } else {
941                         /*
942                          * The talloc_memlimit_update_on_free()
943                          * call takes into account the
944                          * prefix TP_HDR_SIZE allocated before
945                          * the pool talloc_chunk.
946                          */
947                         talloc_memlimit_update_on_free(pool_tc);
948                         TC_INVALIDATE_FULL_CHUNK(pool_tc);
949                         free(pool);
950                 }
951                 return;
952         }
953
954         if (pool->end == next_tc) {
955                 /*
956                  * if pool->pool still points to end of
957                  * 'tc' (which is stored in the 'next_tc' variable),
958                  * we can reclaim the memory of 'tc'.
959                  */
960                 pool->end = tc;
961                 return;
962         }
963
964         /*
965          * Do nothing. The memory is just "wasted", waiting for the pool
966          * itself to be freed.
967          */
968 }
969
970 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
971                                                   void *ptr,
972                                                   const char *location);
973
974 /*
975    internal talloc_free call
976 */
977 static inline int _talloc_free_internal(void *ptr, const char *location)
978 {
979         struct talloc_chunk *tc;
980         void *ptr_to_free;
981
982         if (unlikely(ptr == NULL)) {
983                 return -1;
984         }
985
986         /* possibly initialised the talloc fill value */
987         if (unlikely(!talloc_fill.initialised)) {
988                 const char *fill = getenv(TALLOC_FILL_ENV);
989                 if (fill != NULL) {
990                         talloc_fill.enabled = true;
991                         talloc_fill.fill_value = strtoul(fill, NULL, 0);
992                 }
993                 talloc_fill.initialised = true;
994         }
995
996         tc = talloc_chunk_from_ptr(ptr);
997
998         if (unlikely(tc->refs)) {
999                 int is_child;
1000                 /* check if this is a reference from a child or
1001                  * grandchild back to it's parent or grandparent
1002                  *
1003                  * in that case we need to remove the reference and
1004                  * call another instance of talloc_free() on the current
1005                  * pointer.
1006                  */
1007                 is_child = talloc_is_parent(tc->refs, ptr);
1008                 _talloc_free_internal(tc->refs, location);
1009                 if (is_child) {
1010                         return _talloc_free_internal(ptr, location);
1011                 }
1012                 return -1;
1013         }
1014
1015         if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
1016                 /* we have a free loop - stop looping */
1017                 return 0;
1018         }
1019
1020         if (unlikely(tc->destructor)) {
1021                 talloc_destructor_t d = tc->destructor;
1022                 if (d == (talloc_destructor_t)-1) {
1023                         return -1;
1024                 }
1025                 tc->destructor = (talloc_destructor_t)-1;
1026                 if (d(ptr) == -1) {
1027                         /*
1028                          * Only replace the destructor pointer if
1029                          * calling the destructor didn't modify it.
1030                          */
1031                         if (tc->destructor == (talloc_destructor_t)-1) {
1032                                 tc->destructor = d;
1033                         }
1034                         return -1;
1035                 }
1036                 tc->destructor = NULL;
1037         }
1038
1039         if (tc->parent) {
1040                 _TLIST_REMOVE(tc->parent->child, tc);
1041                 if (tc->parent->child) {
1042                         tc->parent->child->parent = tc->parent;
1043                 }
1044         } else {
1045                 if (tc->prev) tc->prev->next = tc->next;
1046                 if (tc->next) tc->next->prev = tc->prev;
1047                 tc->prev = tc->next = NULL;
1048         }
1049
1050         tc->flags |= TALLOC_FLAG_LOOP;
1051
1052         _talloc_free_children_internal(tc, ptr, location);
1053
1054         tc->flags |= TALLOC_FLAG_FREE;
1055
1056         /* we mark the freed memory with where we called the free
1057          * from. This means on a double free error we can report where
1058          * the first free came from
1059          */
1060         tc->name = location;
1061
1062         if (tc->flags & TALLOC_FLAG_POOL) {
1063                 struct talloc_pool_hdr *pool;
1064
1065                 pool = talloc_pool_from_chunk(tc);
1066
1067                 if (unlikely(pool->object_count == 0)) {
1068                         talloc_abort("Pool object count zero!");
1069                         return 0;
1070                 }
1071
1072                 pool->object_count--;
1073
1074                 if (likely(pool->object_count != 0)) {
1075                         return 0;
1076                 }
1077
1078                 /*
1079                  * With object_count==0, a pool becomes a normal piece of
1080                  * memory to free. If it's allocated inside a pool, it needs
1081                  * to be freed as poolmem, else it needs to be just freed.
1082                 */
1083                 ptr_to_free = pool;
1084         } else {
1085                 ptr_to_free = tc;
1086         }
1087
1088         if (tc->flags & TALLOC_FLAG_POOLMEM) {
1089                 _talloc_free_poolmem(tc, location);
1090                 return 0;
1091         }
1092
1093         talloc_memlimit_update_on_free(tc);
1094
1095         TC_INVALIDATE_FULL_CHUNK(tc);
1096         free(ptr_to_free);
1097         return 0;
1098 }
1099
1100 static inline size_t _talloc_total_limit_size(const void *ptr,
1101                                         struct talloc_memlimit *old_limit,
1102                                         struct talloc_memlimit *new_limit);
1103
1104 /*
1105    move a lump of memory from one talloc context to another return the
1106    ptr on success, or NULL if it could not be transferred.
1107    passing NULL as ptr will always return NULL with no side effects.
1108 */
1109 static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
1110 {
1111         struct talloc_chunk *tc, *new_tc;
1112         size_t ctx_size = 0;
1113
1114         if (unlikely(!ptr)) {
1115                 return NULL;
1116         }
1117
1118         if (unlikely(new_ctx == NULL)) {
1119                 new_ctx = null_context;
1120         }
1121
1122         tc = talloc_chunk_from_ptr(ptr);
1123
1124         if (tc->limit != NULL) {
1125
1126                 ctx_size = _talloc_total_limit_size(ptr, NULL, NULL);
1127
1128                 /* Decrement the memory limit from the source .. */
1129                 talloc_memlimit_shrink(tc->limit->upper, ctx_size);
1130
1131                 if (tc->limit->parent == tc) {
1132                         tc->limit->upper = NULL;
1133                 } else {
1134                         tc->limit = NULL;
1135                 }
1136         }
1137
1138         if (unlikely(new_ctx == NULL)) {
1139                 if (tc->parent) {
1140                         _TLIST_REMOVE(tc->parent->child, tc);
1141                         if (tc->parent->child) {
1142                                 tc->parent->child->parent = tc->parent;
1143                         }
1144                 } else {
1145                         if (tc->prev) tc->prev->next = tc->next;
1146                         if (tc->next) tc->next->prev = tc->prev;
1147                 }
1148
1149                 tc->parent = tc->next = tc->prev = NULL;
1150                 return discard_const_p(void, ptr);
1151         }
1152
1153         new_tc = talloc_chunk_from_ptr(new_ctx);
1154
1155         if (unlikely(tc == new_tc || tc->parent == new_tc)) {
1156                 return discard_const_p(void, ptr);
1157         }
1158
1159         if (tc->parent) {
1160                 _TLIST_REMOVE(tc->parent->child, tc);
1161                 if (tc->parent->child) {
1162                         tc->parent->child->parent = tc->parent;
1163                 }
1164         } else {
1165                 if (tc->prev) tc->prev->next = tc->next;
1166                 if (tc->next) tc->next->prev = tc->prev;
1167                 tc->prev = tc->next = NULL;
1168         }
1169
1170         tc->parent = new_tc;
1171         if (new_tc->child) new_tc->child->parent = NULL;
1172         _TLIST_ADD(new_tc->child, tc);
1173
1174         if (tc->limit || new_tc->limit) {
1175                 ctx_size = _talloc_total_limit_size(ptr, tc->limit,
1176                                                     new_tc->limit);
1177                 /* .. and increment it in the destination. */
1178                 if (new_tc->limit) {
1179                         talloc_memlimit_grow(new_tc->limit, ctx_size);
1180                 }
1181         }
1182
1183         return discard_const_p(void, ptr);
1184 }
1185
1186 /*
1187    move a lump of memory from one talloc context to another return the
1188    ptr on success, or NULL if it could not be transferred.
1189    passing NULL as ptr will always return NULL with no side effects.
1190 */
1191 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
1192 {
1193         struct talloc_chunk *tc;
1194
1195         if (unlikely(ptr == NULL)) {
1196                 return NULL;
1197         }
1198
1199         tc = talloc_chunk_from_ptr(ptr);
1200
1201         if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
1202                 struct talloc_reference_handle *h;
1203
1204                 talloc_log("WARNING: talloc_steal with references at %s\n",
1205                            location);
1206
1207                 for (h=tc->refs; h; h=h->next) {
1208                         talloc_log("\treference at %s\n",
1209                                    h->location);
1210                 }
1211         }
1212
1213 #if 0
1214         /* this test is probably too expensive to have on in the
1215            normal build, but it useful for debugging */
1216         if (talloc_is_parent(new_ctx, ptr)) {
1217                 talloc_log("WARNING: stealing into talloc child at %s\n", location);
1218         }
1219 #endif
1220
1221         return _talloc_steal_internal(new_ctx, ptr);
1222 }
1223
1224 /*
1225    this is like a talloc_steal(), but you must supply the old
1226    parent. This resolves the ambiguity in a talloc_steal() which is
1227    called on a context that has more than one parent (via references)
1228
1229    The old parent can be either a reference or a parent
1230 */
1231 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
1232 {
1233         struct talloc_chunk *tc;
1234         struct talloc_reference_handle *h;
1235
1236         if (unlikely(ptr == NULL)) {
1237                 return NULL;
1238         }
1239
1240         if (old_parent == talloc_parent(ptr)) {
1241                 return _talloc_steal_internal(new_parent, ptr);
1242         }
1243
1244         tc = talloc_chunk_from_ptr(ptr);
1245         for (h=tc->refs;h;h=h->next) {
1246                 if (talloc_parent(h) == old_parent) {
1247                         if (_talloc_steal_internal(new_parent, h) != h) {
1248                                 return NULL;
1249                         }
1250                         return discard_const_p(void, ptr);
1251                 }
1252         }
1253
1254         /* it wasn't a parent */
1255         return NULL;
1256 }
1257
1258 /*
1259   remove a secondary reference to a pointer. This undo's what
1260   talloc_reference() has done. The context and pointer arguments
1261   must match those given to a talloc_reference()
1262 */
1263 static inline int talloc_unreference(const void *context, const void *ptr)
1264 {
1265         struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1266         struct talloc_reference_handle *h;
1267
1268         if (unlikely(context == NULL)) {
1269                 context = null_context;
1270         }
1271
1272         for (h=tc->refs;h;h=h->next) {
1273                 struct talloc_chunk *p = talloc_parent_chunk(h);
1274                 if (p == NULL) {
1275                         if (context == NULL) break;
1276                 } else if (TC_PTR_FROM_CHUNK(p) == context) {
1277                         break;
1278                 }
1279         }
1280         if (h == NULL) {
1281                 return -1;
1282         }
1283
1284         return _talloc_free_internal(h, __location__);
1285 }
1286
1287 /*
1288   remove a specific parent context from a pointer. This is a more
1289   controlled variant of talloc_free()
1290 */
1291 _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
1292 {
1293         struct talloc_chunk *tc_p, *new_p, *tc_c;
1294         void *new_parent;
1295
1296         if (ptr == NULL) {
1297                 return -1;
1298         }
1299
1300         if (context == NULL) {
1301                 context = null_context;
1302         }
1303
1304         if (talloc_unreference(context, ptr) == 0) {
1305                 return 0;
1306         }
1307
1308         if (context != NULL) {
1309                 tc_c = talloc_chunk_from_ptr(context);
1310         } else {
1311                 tc_c = NULL;
1312         }
1313         if (tc_c != talloc_parent_chunk(ptr)) {
1314                 return -1;
1315         }
1316
1317         tc_p = talloc_chunk_from_ptr(ptr);
1318
1319         if (tc_p->refs == NULL) {
1320                 return _talloc_free_internal(ptr, __location__);
1321         }
1322
1323         new_p = talloc_parent_chunk(tc_p->refs);
1324         if (new_p) {
1325                 new_parent = TC_PTR_FROM_CHUNK(new_p);
1326         } else {
1327                 new_parent = NULL;
1328         }
1329
1330         if (talloc_unreference(new_parent, ptr) != 0) {
1331                 return -1;
1332         }
1333
1334         _talloc_steal_internal(new_parent, ptr);
1335
1336         return 0;
1337 }
1338
1339 /*
1340   add a name to an existing pointer - va_list version
1341 */
1342 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1343
1344 static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
1345 {
1346         struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1347         tc->name = talloc_vasprintf(ptr, fmt, ap);
1348         if (likely(tc->name)) {
1349                 _talloc_set_name_const(tc->name, ".name");
1350         }
1351         return tc->name;
1352 }
1353
1354 /*
1355   add a name to an existing pointer
1356 */
1357 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
1358 {
1359         const char *name;
1360         va_list ap;
1361         va_start(ap, fmt);
1362         name = talloc_set_name_v(ptr, fmt, ap);
1363         va_end(ap);
1364         return name;
1365 }
1366
1367
1368 /*
1369   create a named talloc pointer. Any talloc pointer can be named, and
1370   talloc_named() operates just like talloc() except that it allows you
1371   to name the pointer.
1372 */
1373 _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
1374 {
1375         va_list ap;
1376         void *ptr;
1377         const char *name;
1378
1379         ptr = __talloc(context, size);
1380         if (unlikely(ptr == NULL)) return NULL;
1381
1382         va_start(ap, fmt);
1383         name = talloc_set_name_v(ptr, fmt, ap);
1384         va_end(ap);
1385
1386         if (unlikely(name == NULL)) {
1387                 _talloc_free_internal(ptr, __location__);
1388                 return NULL;
1389         }
1390
1391         return ptr;
1392 }
1393
1394 /*
1395   return the name of a talloc ptr, or "UNNAMED"
1396 */
1397 static inline const char *__talloc_get_name(const void *ptr)
1398 {
1399         struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1400         if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
1401                 return ".reference";
1402         }
1403         if (likely(tc->name)) {
1404                 return tc->name;
1405         }
1406         return "UNNAMED";
1407 }
1408
1409 _PUBLIC_ const char *talloc_get_name(const void *ptr)
1410 {
1411         return __talloc_get_name(ptr);
1412 }
1413
1414 /*
1415   check if a pointer has the given name. If it does, return the pointer,
1416   otherwise return NULL
1417 */
1418 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
1419 {
1420         const char *pname;
1421         if (unlikely(ptr == NULL)) return NULL;
1422         pname = __talloc_get_name(ptr);
1423         if (likely(pname == name || strcmp(pname, name) == 0)) {
1424                 return discard_const_p(void, ptr);
1425         }
1426         return NULL;
1427 }
1428
1429 static void talloc_abort_type_mismatch(const char *location,
1430                                         const char *name,
1431                                         const char *expected)
1432 {
1433         const char *reason;
1434
1435         reason = talloc_asprintf(NULL,
1436                                  "%s: Type mismatch: name[%s] expected[%s]",
1437                                  location,
1438                                  name?name:"NULL",
1439                                  expected);
1440         if (!reason) {
1441                 reason = "Type mismatch";
1442         }
1443
1444         talloc_abort(reason);
1445 }
1446
1447 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
1448 {
1449         const char *pname;
1450
1451         if (unlikely(ptr == NULL)) {
1452                 talloc_abort_type_mismatch(location, NULL, name);
1453                 return NULL;
1454         }
1455
1456         pname = __talloc_get_name(ptr);
1457         if (likely(pname == name || strcmp(pname, name) == 0)) {
1458                 return discard_const_p(void, ptr);
1459         }
1460
1461         talloc_abort_type_mismatch(location, pname, name);
1462         return NULL;
1463 }
1464
1465 /*
1466   this is for compatibility with older versions of talloc
1467 */
1468 _PUBLIC_ void *talloc_init(const char *fmt, ...)
1469 {
1470         va_list ap;
1471         void *ptr;
1472         const char *name;
1473
1474         ptr = __talloc(NULL, 0);
1475         if (unlikely(ptr == NULL)) return NULL;
1476
1477         va_start(ap, fmt);
1478         name = talloc_set_name_v(ptr, fmt, ap);
1479         va_end(ap);
1480
1481         if (unlikely(name == NULL)) {
1482                 _talloc_free_internal(ptr, __location__);
1483                 return NULL;
1484         }
1485
1486         return ptr;
1487 }
1488
1489 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
1490                                                   void *ptr,
1491                                                   const char *location)
1492 {
1493         while (tc->child) {
1494                 /* we need to work out who will own an abandoned child
1495                    if it cannot be freed. In priority order, the first
1496                    choice is owner of any remaining reference to this
1497                    pointer, the second choice is our parent, and the
1498                    final choice is the null context. */
1499                 void *child = TC_PTR_FROM_CHUNK(tc->child);
1500                 const void *new_parent = null_context;
1501                 if (unlikely(tc->child->refs)) {
1502                         struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1503                         if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1504                 }
1505                 if (unlikely(_talloc_free_internal(child, location) == -1)) {
1506                         if (talloc_parent_chunk(child) != tc) {
1507                                 /*
1508                                  * Destructor already reparented this child.
1509                                  * No further reparenting needed.
1510                                  */
1511                                 return;
1512                         }
1513                         if (new_parent == null_context) {
1514                                 struct talloc_chunk *p = talloc_parent_chunk(ptr);
1515                                 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1516                         }
1517                         _talloc_steal_internal(new_parent, child);
1518                 }
1519         }
1520 }
1521
1522 /*
1523   this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1524   should probably not be used in new code. It's in here to keep the talloc
1525   code consistent across Samba 3 and 4.
1526 */
1527 _PUBLIC_ void talloc_free_children(void *ptr)
1528 {
1529         struct talloc_chunk *tc_name = NULL;
1530         struct talloc_chunk *tc;
1531
1532         if (unlikely(ptr == NULL)) {
1533                 return;
1534         }
1535
1536         tc = talloc_chunk_from_ptr(ptr);
1537
1538         /* we do not want to free the context name if it is a child .. */
1539         if (likely(tc->child)) {
1540                 for (tc_name = tc->child; tc_name; tc_name = tc_name->next) {
1541                         if (tc->name == TC_PTR_FROM_CHUNK(tc_name)) break;
1542                 }
1543                 if (tc_name) {
1544                         _TLIST_REMOVE(tc->child, tc_name);
1545                         if (tc->child) {
1546                                 tc->child->parent = tc;
1547                         }
1548                 }
1549         }
1550
1551         _talloc_free_children_internal(tc, ptr, __location__);
1552
1553         /* .. so we put it back after all other children have been freed */
1554         if (tc_name) {
1555                 if (tc->child) {
1556                         tc->child->parent = NULL;
1557                 }
1558                 tc_name->parent = tc;
1559                 _TLIST_ADD(tc->child, tc_name);
1560         }
1561 }
1562
1563 /*
1564    Allocate a bit of memory as a child of an existing pointer
1565 */
1566 _PUBLIC_ void *_talloc(const void *context, size_t size)
1567 {
1568         return __talloc(context, size);
1569 }
1570
1571 /*
1572   externally callable talloc_set_name_const()
1573 */
1574 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1575 {
1576         _talloc_set_name_const(ptr, name);
1577 }
1578
1579 /*
1580   create a named talloc pointer. Any talloc pointer can be named, and
1581   talloc_named() operates just like talloc() except that it allows you
1582   to name the pointer.
1583 */
1584 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1585 {
1586         return _talloc_named_const(context, size, name);
1587 }
1588
1589 /*
1590    free a talloc pointer. This also frees all child pointers of this
1591    pointer recursively
1592
1593    return 0 if the memory is actually freed, otherwise -1. The memory
1594    will not be freed if the ref_count is > 1 or the destructor (if
1595    any) returns non-zero
1596 */
1597 _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1598 {
1599         struct talloc_chunk *tc;
1600
1601         if (unlikely(ptr == NULL)) {
1602                 return -1;
1603         }
1604
1605         tc = talloc_chunk_from_ptr(ptr);
1606
1607         if (unlikely(tc->refs != NULL)) {
1608                 struct talloc_reference_handle *h;
1609
1610                 if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1611                         /* in this case we do know which parent should
1612                            get this pointer, as there is really only
1613                            one parent */
1614                         return talloc_unlink(null_context, ptr);
1615                 }
1616
1617                 talloc_log("ERROR: talloc_free with references at %s\n",
1618                            location);
1619
1620                 for (h=tc->refs; h; h=h->next) {
1621                         talloc_log("\treference at %s\n",
1622                                    h->location);
1623                 }
1624                 return -1;
1625         }
1626
1627         return _talloc_free_internal(ptr, location);
1628 }
1629
1630
1631
1632 /*
1633   A talloc version of realloc. The context argument is only used if
1634   ptr is NULL
1635 */
1636 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1637 {
1638         struct talloc_chunk *tc;
1639         void *new_ptr;
1640         bool malloced = false;
1641         struct talloc_pool_hdr *pool_hdr = NULL;
1642         size_t old_size = 0;
1643         size_t new_size = 0;
1644
1645         /* size zero is equivalent to free() */
1646         if (unlikely(size == 0)) {
1647                 talloc_unlink(context, ptr);
1648                 return NULL;
1649         }
1650
1651         if (unlikely(size >= MAX_TALLOC_SIZE)) {
1652                 return NULL;
1653         }
1654
1655         /* realloc(NULL) is equivalent to malloc() */
1656         if (ptr == NULL) {
1657                 return _talloc_named_const(context, size, name);
1658         }
1659
1660         tc = talloc_chunk_from_ptr(ptr);
1661
1662         /* don't allow realloc on referenced pointers */
1663         if (unlikely(tc->refs)) {
1664                 return NULL;
1665         }
1666
1667         /* don't let anybody try to realloc a talloc_pool */
1668         if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1669                 return NULL;
1670         }
1671
1672         if (tc->limit && (size > tc->size)) {
1673                 if (!talloc_memlimit_check(tc->limit, (size - tc->size))) {
1674                         errno = ENOMEM;
1675                         return NULL;
1676                 }
1677         }
1678
1679         /* handle realloc inside a talloc_pool */
1680         if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
1681                 pool_hdr = tc->pool;
1682         }
1683
1684 #if (ALWAYS_REALLOC == 0)
1685         /* don't shrink if we have less than 1k to gain */
1686         if (size < tc->size && tc->limit == NULL) {
1687                 if (pool_hdr) {
1688                         void *next_tc = tc_next_chunk(tc);
1689                         TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1690                         tc->size = size;
1691                         if (next_tc == pool_hdr->end) {
1692                                 /* note: tc->size has changed, so this works */
1693                                 pool_hdr->end = tc_next_chunk(tc);
1694                         }
1695                         return ptr;
1696                 } else if ((tc->size - size) < 1024) {
1697                         /*
1698                          * if we call TC_INVALIDATE_SHRINK_CHUNK() here
1699                          * we would need to call TC_UNDEFINE_GROW_CHUNK()
1700                          * after each realloc call, which slows down
1701                          * testing a lot :-(.
1702                          *
1703                          * That is why we only mark memory as undefined here.
1704                          */
1705                         TC_UNDEFINE_SHRINK_CHUNK(tc, size);
1706
1707                         /* do not shrink if we have less than 1k to gain */
1708                         tc->size = size;
1709                         return ptr;
1710                 }
1711         } else if (tc->size == size) {
1712                 /*
1713                  * do not change the pointer if it is exactly
1714                  * the same size.
1715                  */
1716                 return ptr;
1717         }
1718 #endif
1719
1720         /* by resetting magic we catch users of the old memory */
1721         tc->flags |= TALLOC_FLAG_FREE;
1722
1723 #if ALWAYS_REALLOC
1724         if (pool_hdr) {
1725                 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE, 0);
1726                 pool_hdr->object_count--;
1727
1728                 if (new_ptr == NULL) {
1729                         new_ptr = malloc(TC_HDR_SIZE+size);
1730                         malloced = true;
1731                         new_size = size;
1732                 }
1733
1734                 if (new_ptr) {
1735                         memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1736                         TC_INVALIDATE_FULL_CHUNK(tc);
1737                 }
1738         } else {
1739                 /* We're doing malloc then free here, so record the difference. */
1740                 old_size = tc->size;
1741                 new_size = size;
1742                 new_ptr = malloc(size + TC_HDR_SIZE);
1743                 if (new_ptr) {
1744                         memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
1745                         free(tc);
1746                 }
1747         }
1748 #else
1749         if (pool_hdr) {
1750                 struct talloc_chunk *pool_tc;
1751                 void *next_tc = tc_next_chunk(tc);
1752                 size_t old_chunk_size = TC_ALIGN16(TC_HDR_SIZE + tc->size);
1753                 size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
1754                 size_t space_needed;
1755                 size_t space_left;
1756                 unsigned int chunk_count = pool_hdr->object_count;
1757
1758                 pool_tc = talloc_chunk_from_pool(pool_hdr);
1759                 if (!(pool_tc->flags & TALLOC_FLAG_FREE)) {
1760                         chunk_count -= 1;
1761                 }
1762
1763                 if (chunk_count == 1) {
1764                         /*
1765                          * optimize for the case where 'tc' is the only
1766                          * chunk in the pool.
1767                          */
1768                         char *start = tc_pool_first_chunk(pool_hdr);
1769                         space_needed = new_chunk_size;
1770                         space_left = (char *)tc_pool_end(pool_hdr) - start;
1771
1772                         if (space_left >= space_needed) {
1773                                 size_t old_used = TC_HDR_SIZE + tc->size;
1774                                 size_t new_used = TC_HDR_SIZE + size;
1775                                 new_ptr = start;
1776
1777 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
1778                                 {
1779                                         /*
1780                                          * The area from
1781                                          * start -> tc may have
1782                                          * been freed and thus been marked as
1783                                          * VALGRIND_MEM_NOACCESS. Set it to
1784                                          * VALGRIND_MEM_UNDEFINED so we can
1785                                          * copy into it without valgrind errors.
1786                                          * We can't just mark
1787                                          * new_ptr -> new_ptr + old_used
1788                                          * as this may overlap on top of tc,
1789                                          * (which is why we use memmove, not
1790                                          * memcpy below) hence the MIN.
1791                                          */
1792                                         size_t undef_len = MIN((((char *)tc) - ((char *)new_ptr)),old_used);
1793                                         VALGRIND_MAKE_MEM_UNDEFINED(new_ptr, undef_len);
1794                                 }
1795 #endif
1796
1797                                 memmove(new_ptr, tc, old_used);
1798
1799                                 tc = (struct talloc_chunk *)new_ptr;
1800                                 TC_UNDEFINE_GROW_CHUNK(tc, size);
1801
1802                                 /*
1803                                  * first we do not align the pool pointer
1804                                  * because we want to invalidate the padding
1805                                  * too.
1806                                  */
1807                                 pool_hdr->end = new_used + (char *)new_ptr;
1808                                 tc_invalidate_pool(pool_hdr);
1809
1810                                 /* now the aligned pointer */
1811                                 pool_hdr->end = new_chunk_size + (char *)new_ptr;
1812                                 goto got_new_ptr;
1813                         }
1814
1815                         next_tc = NULL;
1816                 }
1817
1818                 if (new_chunk_size == old_chunk_size) {
1819                         TC_UNDEFINE_GROW_CHUNK(tc, size);
1820                         tc->flags &= ~TALLOC_FLAG_FREE;
1821                         tc->size = size;
1822                         return ptr;
1823                 }
1824
1825                 if (next_tc == pool_hdr->end) {
1826                         /*
1827                          * optimize for the case where 'tc' is the last
1828                          * chunk in the pool.
1829                          */
1830                         space_needed = new_chunk_size - old_chunk_size;
1831                         space_left = tc_pool_space_left(pool_hdr);
1832
1833                         if (space_left >= space_needed) {
1834                                 TC_UNDEFINE_GROW_CHUNK(tc, size);
1835                                 tc->flags &= ~TALLOC_FLAG_FREE;
1836                                 tc->size = size;
1837                                 pool_hdr->end = tc_next_chunk(tc);
1838                                 return ptr;
1839                         }
1840                 }
1841
1842                 new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE, 0);
1843
1844                 if (new_ptr == NULL) {
1845                         new_ptr = malloc(TC_HDR_SIZE+size);
1846                         malloced = true;
1847                         new_size = size;
1848                 }
1849
1850                 if (new_ptr) {
1851                         memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
1852
1853                         _talloc_free_poolmem(tc, __location__ "_talloc_realloc");
1854                 }
1855         }
1856         else {
1857                 /* We're doing realloc here, so record the difference. */
1858                 old_size = tc->size;
1859                 new_size = size;
1860                 new_ptr = realloc(tc, size + TC_HDR_SIZE);
1861         }
1862 got_new_ptr:
1863 #endif
1864         if (unlikely(!new_ptr)) {
1865                 tc->flags &= ~TALLOC_FLAG_FREE;
1866                 return NULL;
1867         }
1868
1869         tc = (struct talloc_chunk *)new_ptr;
1870         tc->flags &= ~TALLOC_FLAG_FREE;
1871         if (malloced) {
1872                 tc->flags &= ~TALLOC_FLAG_POOLMEM;
1873         }
1874         if (tc->parent) {
1875                 tc->parent->child = tc;
1876         }
1877         if (tc->child) {
1878                 tc->child->parent = tc;
1879         }
1880
1881         if (tc->prev) {
1882                 tc->prev->next = tc;
1883         }
1884         if (tc->next) {
1885                 tc->next->prev = tc;
1886         }
1887
1888         if (new_size > old_size) {
1889                 talloc_memlimit_grow(tc->limit, new_size - old_size);
1890         } else if (new_size < old_size) {
1891                 talloc_memlimit_shrink(tc->limit, old_size - new_size);
1892         }
1893
1894         tc->size = size;
1895         _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
1896
1897         return TC_PTR_FROM_CHUNK(tc);
1898 }
1899
1900 /*
1901   a wrapper around talloc_steal() for situations where you are moving a pointer
1902   between two structures, and want the old pointer to be set to NULL
1903 */
1904 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
1905 {
1906         const void **pptr = discard_const_p(const void *,_pptr);
1907         void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
1908         (*pptr) = NULL;
1909         return ret;
1910 }
1911
1912 enum talloc_mem_count_type {
1913         TOTAL_MEM_SIZE,
1914         TOTAL_MEM_BLOCKS,
1915         TOTAL_MEM_LIMIT,
1916 };
1917
1918 static inline size_t _talloc_total_mem_internal(const void *ptr,
1919                                          enum talloc_mem_count_type type,
1920                                          struct talloc_memlimit *old_limit,
1921                                          struct talloc_memlimit *new_limit)
1922 {
1923         size_t total = 0;
1924         struct talloc_chunk *c, *tc;
1925
1926         if (ptr == NULL) {
1927                 ptr = null_context;
1928         }
1929         if (ptr == NULL) {
1930                 return 0;
1931         }
1932
1933         tc = talloc_chunk_from_ptr(ptr);
1934
1935         if (old_limit || new_limit) {
1936                 if (tc->limit && tc->limit->upper == old_limit) {
1937                         tc->limit->upper = new_limit;
1938                 }
1939         }
1940
1941         /* optimize in the memlimits case */
1942         if (type == TOTAL_MEM_LIMIT &&
1943             tc->limit != NULL &&
1944             tc->limit != old_limit &&
1945             tc->limit->parent == tc) {
1946                 return tc->limit->cur_size;
1947         }
1948
1949         if (tc->flags & TALLOC_FLAG_LOOP) {
1950                 return 0;
1951         }
1952
1953         tc->flags |= TALLOC_FLAG_LOOP;
1954
1955         if (old_limit || new_limit) {
1956                 if (old_limit == tc->limit) {
1957                         tc->limit = new_limit;
1958                 }
1959         }
1960
1961         switch (type) {
1962         case TOTAL_MEM_SIZE:
1963                 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1964                         total = tc->size;
1965                 }
1966                 break;
1967         case TOTAL_MEM_BLOCKS:
1968                 total++;
1969                 break;
1970         case TOTAL_MEM_LIMIT:
1971                 if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
1972                         /*
1973                          * Don't count memory allocated from a pool
1974                          * when calculating limits. Only count the
1975                          * pool itself.
1976                          */
1977                         if (!(tc->flags & TALLOC_FLAG_POOLMEM)) {
1978                                 if (tc->flags & TALLOC_FLAG_POOL) {
1979                                         /*
1980                                          * If this is a pool, the allocated
1981                                          * size is in the pool header, and
1982                                          * remember to add in the prefix
1983                                          * length.
1984                                          */
1985                                         struct talloc_pool_hdr *pool_hdr
1986                                                         = talloc_pool_from_chunk(tc);
1987                                         total = pool_hdr->poolsize +
1988                                                         TC_HDR_SIZE +
1989                                                         TP_HDR_SIZE;
1990                                 } else {
1991                                         total = tc->size + TC_HDR_SIZE;
1992                                 }
1993                         }
1994                 }
1995                 break;
1996         }
1997         for (c = tc->child; c; c = c->next) {
1998                 total += _talloc_total_mem_internal(TC_PTR_FROM_CHUNK(c), type,
1999                                                     old_limit, new_limit);
2000         }
2001
2002         tc->flags &= ~TALLOC_FLAG_LOOP;
2003
2004         return total;
2005 }
2006
2007 /*
2008   return the total size of a talloc pool (subtree)
2009 */
2010 _PUBLIC_ size_t talloc_total_size(const void *ptr)
2011 {
2012         return _talloc_total_mem_internal(ptr, TOTAL_MEM_SIZE, NULL, NULL);
2013 }
2014
2015 /*
2016   return the total number of blocks in a talloc pool (subtree)
2017 */
2018 _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
2019 {
2020         return _talloc_total_mem_internal(ptr, TOTAL_MEM_BLOCKS, NULL, NULL);
2021 }
2022
2023 /*
2024   return the number of external references to a pointer
2025 */
2026 _PUBLIC_ size_t talloc_reference_count(const void *ptr)
2027 {
2028         struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
2029         struct talloc_reference_handle *h;
2030         size_t ret = 0;
2031
2032         for (h=tc->refs;h;h=h->next) {
2033                 ret++;
2034         }
2035         return ret;
2036 }
2037
2038 /*
2039   report on memory usage by all children of a pointer, giving a full tree view
2040 */
2041 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
2042                             void (*callback)(const void *ptr,
2043                                              int depth, int max_depth,
2044                                              int is_ref,
2045                                              void *private_data),
2046                             void *private_data)
2047 {
2048         struct talloc_chunk *c, *tc;
2049
2050         if (ptr == NULL) {
2051                 ptr = null_context;
2052         }
2053         if (ptr == NULL) return;
2054
2055         tc = talloc_chunk_from_ptr(ptr);
2056
2057         if (tc->flags & TALLOC_FLAG_LOOP) {
2058                 return;
2059         }
2060
2061         callback(ptr, depth, max_depth, 0, private_data);
2062
2063         if (max_depth >= 0 && depth >= max_depth) {
2064                 return;
2065         }
2066
2067         tc->flags |= TALLOC_FLAG_LOOP;
2068         for (c=tc->child;c;c=c->next) {
2069                 if (c->name == TALLOC_MAGIC_REFERENCE) {
2070                         struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
2071                         callback(h->ptr, depth + 1, max_depth, 1, private_data);
2072                 } else {
2073                         talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
2074                 }
2075         }
2076         tc->flags &= ~TALLOC_FLAG_LOOP;
2077 }
2078
2079 static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
2080 {
2081         const char *name = __talloc_get_name(ptr);
2082         struct talloc_chunk *tc;
2083         FILE *f = (FILE *)_f;
2084
2085         if (is_ref) {
2086                 fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
2087                 return;
2088         }
2089
2090         tc = talloc_chunk_from_ptr(ptr);
2091         if (tc->limit && tc->limit->parent == tc) {
2092                 fprintf(f, "%*s%-30s is a memlimit context"
2093                         " (max_size = %lu bytes, cur_size = %lu bytes)\n",
2094                         depth*4, "",
2095                         name,
2096                         (unsigned long)tc->limit->max_size,
2097                         (unsigned long)tc->limit->cur_size);
2098         }
2099
2100         if (depth == 0) {
2101                 fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
2102                         (max_depth < 0 ? "full " :""), name,
2103                         (unsigned long)talloc_total_size(ptr),
2104                         (unsigned long)talloc_total_blocks(ptr));
2105                 return;
2106         }
2107
2108         fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
2109                 depth*4, "",
2110                 name,
2111                 (unsigned long)talloc_total_size(ptr),
2112                 (unsigned long)talloc_total_blocks(ptr),
2113                 (int)talloc_reference_count(ptr), ptr);
2114
2115 #if 0
2116         fprintf(f, "content: ");
2117         if (talloc_total_size(ptr)) {
2118                 int tot = talloc_total_size(ptr);
2119                 int i;
2120
2121                 for (i = 0; i < tot; i++) {
2122                         if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
2123                                 fprintf(f, "%c", ((char *)ptr)[i]);
2124                         } else {
2125                                 fprintf(f, "~%02x", ((char *)ptr)[i]);
2126                         }
2127                 }
2128         }
2129         fprintf(f, "\n");
2130 #endif
2131 }
2132
2133 /*
2134   report on memory usage by all children of a pointer, giving a full tree view
2135 */
2136 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
2137 {
2138         if (f) {
2139                 talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
2140                 fflush(f);
2141         }
2142 }
2143
2144 /*
2145   report on memory usage by all children of a pointer, giving a full tree view
2146 */
2147 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
2148 {
2149         talloc_report_depth_file(ptr, 0, -1, f);
2150 }
2151
2152 /*
2153   report on memory usage by all children of a pointer
2154 */
2155 _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
2156 {
2157         talloc_report_depth_file(ptr, 0, 1, f);
2158 }
2159
2160 /*
2161   report on any memory hanging off the null context
2162 */
2163 static void talloc_report_null(void)
2164 {
2165         if (talloc_total_size(null_context) != 0) {
2166                 talloc_report(null_context, stderr);
2167         }
2168 }
2169
2170 /*
2171   report on any memory hanging off the null context
2172 */
2173 static void talloc_report_null_full(void)
2174 {
2175         if (talloc_total_size(null_context) != 0) {
2176                 talloc_report_full(null_context, stderr);
2177         }
2178 }
2179
2180 /*
2181   enable tracking of the NULL context
2182 */
2183 _PUBLIC_ void talloc_enable_null_tracking(void)
2184 {
2185         if (null_context == NULL) {
2186                 null_context = _talloc_named_const(NULL, 0, "null_context");
2187                 if (autofree_context != NULL) {
2188                         talloc_reparent(NULL, null_context, autofree_context);
2189                 }
2190         }
2191 }
2192
2193 /*
2194   enable tracking of the NULL context, not moving the autofree context
2195   into the NULL context. This is needed for the talloc testsuite
2196 */
2197 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
2198 {
2199         if (null_context == NULL) {
2200                 null_context = _talloc_named_const(NULL, 0, "null_context");
2201         }
2202 }
2203
2204 /*
2205   disable tracking of the NULL context
2206 */
2207 _PUBLIC_ void talloc_disable_null_tracking(void)
2208 {
2209         if (null_context != NULL) {
2210                 /* we have to move any children onto the real NULL
2211                    context */
2212                 struct talloc_chunk *tc, *tc2;
2213                 tc = talloc_chunk_from_ptr(null_context);
2214                 for (tc2 = tc->child; tc2; tc2=tc2->next) {
2215                         if (tc2->parent == tc) tc2->parent = NULL;
2216                         if (tc2->prev == tc) tc2->prev = NULL;
2217                 }
2218                 for (tc2 = tc->next; tc2; tc2=tc2->next) {
2219                         if (tc2->parent == tc) tc2->parent = NULL;
2220                         if (tc2->prev == tc) tc2->prev = NULL;
2221                 }
2222                 tc->child = NULL;
2223                 tc->next = NULL;
2224         }
2225         talloc_free(null_context);
2226         null_context = NULL;
2227 }
2228
2229 /*
2230   enable leak reporting on exit
2231 */
2232 _PUBLIC_ void talloc_enable_leak_report(void)
2233 {
2234         talloc_enable_null_tracking();
2235         atexit(talloc_report_null);
2236 }
2237
2238 /*
2239   enable full leak reporting on exit
2240 */
2241 _PUBLIC_ void talloc_enable_leak_report_full(void)
2242 {
2243         talloc_enable_null_tracking();
2244         atexit(talloc_report_null_full);
2245 }
2246
2247 /*
2248    talloc and zero memory.
2249 */
2250 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
2251 {
2252         void *p = _talloc_named_const(ctx, size, name);
2253
2254         if (p) {
2255                 memset(p, '\0', size);
2256         }
2257
2258         return p;
2259 }
2260
2261 /*
2262   memdup with a talloc.
2263 */
2264 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
2265 {
2266         void *newp = _talloc_named_const(t, size, name);
2267
2268         if (likely(newp)) {
2269                 memcpy(newp, p, size);
2270         }
2271
2272         return newp;
2273 }
2274
2275 static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
2276 {
2277         char *ret;
2278
2279         ret = (char *)__talloc(t, len + 1);
2280         if (unlikely(!ret)) return NULL;
2281
2282         memcpy(ret, p, len);
2283         ret[len] = 0;
2284
2285         _talloc_set_name_const(ret, ret);
2286         return ret;
2287 }
2288
2289 /*
2290   strdup with a talloc
2291 */
2292 _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
2293 {
2294         if (unlikely(!p)) return NULL;
2295         return __talloc_strlendup(t, p, strlen(p));
2296 }
2297
2298 /*
2299   strndup with a talloc
2300 */
2301 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
2302 {
2303         if (unlikely(!p)) return NULL;
2304         return __talloc_strlendup(t, p, strnlen(p, n));
2305 }
2306
2307 static inline char *__talloc_strlendup_append(char *s, size_t slen,
2308                                               const char *a, size_t alen)
2309 {
2310         char *ret;
2311
2312         ret = talloc_realloc(NULL, s, char, slen + alen + 1);
2313         if (unlikely(!ret)) return NULL;
2314
2315         /* append the string and the trailing \0 */
2316         memcpy(&ret[slen], a, alen);
2317         ret[slen+alen] = 0;
2318
2319         _talloc_set_name_const(ret, ret);
2320         return ret;
2321 }
2322
2323 /*
2324  * Appends at the end of the string.
2325  */
2326 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
2327 {
2328         if (unlikely(!s)) {
2329                 return talloc_strdup(NULL, a);
2330         }
2331
2332         if (unlikely(!a)) {
2333                 return s;
2334         }
2335
2336         return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
2337 }
2338
2339 /*
2340  * Appends at the end of the talloc'ed buffer,
2341  * not the end of the string.
2342  */
2343 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
2344 {
2345         size_t slen;
2346
2347         if (unlikely(!s)) {
2348                 return talloc_strdup(NULL, a);
2349         }
2350
2351         if (unlikely(!a)) {
2352                 return s;
2353         }
2354
2355         slen = talloc_get_size(s);
2356         if (likely(slen > 0)) {
2357                 slen--;
2358         }
2359
2360         return __talloc_strlendup_append(s, slen, a, strlen(a));
2361 }
2362
2363 /*
2364  * Appends at the end of the string.
2365  */
2366 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
2367 {
2368         if (unlikely(!s)) {
2369                 return talloc_strndup(NULL, a, n);
2370         }
2371
2372         if (unlikely(!a)) {
2373                 return s;
2374         }
2375
2376         return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
2377 }
2378
2379 /*
2380  * Appends at the end of the talloc'ed buffer,
2381  * not the end of the string.
2382  */
2383 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
2384 {
2385         size_t slen;
2386
2387         if (unlikely(!s)) {
2388                 return talloc_strndup(NULL, a, n);
2389         }
2390
2391         if (unlikely(!a)) {
2392                 return s;
2393         }
2394
2395         slen = talloc_get_size(s);
2396         if (likely(slen > 0)) {
2397                 slen--;
2398         }
2399
2400         return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
2401 }
2402
2403 #ifndef HAVE_VA_COPY
2404 #ifdef HAVE___VA_COPY
2405 #define va_copy(dest, src) __va_copy(dest, src)
2406 #else
2407 #define va_copy(dest, src) (dest) = (src)
2408 #endif
2409 #endif
2410
2411 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
2412 {
2413         int len;
2414         char *ret;
2415         va_list ap2;
2416         char buf[1024];
2417
2418         /* this call looks strange, but it makes it work on older solaris boxes */
2419         va_copy(ap2, ap);
2420         len = vsnprintf(buf, sizeof(buf), fmt, ap2);
2421         va_end(ap2);
2422         if (unlikely(len < 0)) {
2423                 return NULL;
2424         }
2425
2426         ret = (char *)__talloc(t, len+1);
2427         if (unlikely(!ret)) return NULL;
2428
2429         if (len < sizeof(buf)) {
2430                 memcpy(ret, buf, len+1);
2431         } else {
2432                 va_copy(ap2, ap);
2433                 vsnprintf(ret, len+1, fmt, ap2);
2434                 va_end(ap2);
2435         }
2436
2437         _talloc_set_name_const(ret, ret);
2438         return ret;
2439 }
2440
2441
2442 /*
2443   Perform string formatting, and return a pointer to newly allocated
2444   memory holding the result, inside a memory pool.
2445  */
2446 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
2447 {
2448         va_list ap;
2449         char *ret;
2450
2451         va_start(ap, fmt);
2452         ret = talloc_vasprintf(t, fmt, ap);
2453         va_end(ap);
2454         return ret;
2455 }
2456
2457 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2458                                                  const char *fmt, va_list ap)
2459                                                  PRINTF_ATTRIBUTE(3,0);
2460
2461 static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2462                                                  const char *fmt, va_list ap)
2463 {
2464         ssize_t alen;
2465         va_list ap2;
2466         char c;
2467
2468         va_copy(ap2, ap);
2469         alen = vsnprintf(&c, 1, fmt, ap2);
2470         va_end(ap2);
2471
2472         if (alen <= 0) {
2473                 /* Either the vsnprintf failed or the format resulted in
2474                  * no characters being formatted. In the former case, we
2475                  * ought to return NULL, in the latter we ought to return
2476                  * the original string. Most current callers of this
2477                  * function expect it to never return NULL.
2478                  */
2479                 return s;
2480         }
2481
2482         s = talloc_realloc(NULL, s, char, slen + alen + 1);
2483         if (!s) return NULL;
2484
2485         va_copy(ap2, ap);
2486         vsnprintf(s + slen, alen + 1, fmt, ap2);
2487         va_end(ap2);
2488
2489         _talloc_set_name_const(s, s);
2490         return s;
2491 }
2492
2493 /**
2494  * Realloc @p s to append the formatted result of @p fmt and @p ap,
2495  * and return @p s, which may have moved.  Good for gradually
2496  * accumulating output into a string buffer. Appends at the end
2497  * of the string.
2498  **/
2499 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
2500 {
2501         if (unlikely(!s)) {
2502                 return talloc_vasprintf(NULL, fmt, ap);
2503         }
2504
2505         return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
2506 }
2507
2508 /**
2509  * Realloc @p s to append the formatted result of @p fmt and @p ap,
2510  * and return @p s, which may have moved. Always appends at the
2511  * end of the talloc'ed buffer, not the end of the string.
2512  **/
2513 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
2514 {
2515         size_t slen;
2516
2517         if (unlikely(!s)) {
2518                 return talloc_vasprintf(NULL, fmt, ap);
2519         }
2520
2521         slen = talloc_get_size(s);
2522         if (likely(slen > 0)) {
2523                 slen--;
2524         }
2525
2526         return __talloc_vaslenprintf_append(s, slen, fmt, ap);
2527 }
2528
2529 /*
2530   Realloc @p s to append the formatted result of @p fmt and return @p
2531   s, which may have moved.  Good for gradually accumulating output
2532   into a string buffer.
2533  */
2534 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
2535 {
2536         va_list ap;
2537
2538         va_start(ap, fmt);
2539         s = talloc_vasprintf_append(s, fmt, ap);
2540         va_end(ap);
2541         return s;
2542 }
2543
2544 /*
2545   Realloc @p s to append the formatted result of @p fmt and return @p
2546   s, which may have moved.  Good for gradually accumulating output
2547   into a buffer.
2548  */
2549 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
2550 {
2551         va_list ap;
2552
2553         va_start(ap, fmt);
2554         s = talloc_vasprintf_append_buffer(s, fmt, ap);
2555         va_end(ap);
2556         return s;
2557 }
2558
2559 /*
2560   alloc an array, checking for integer overflow in the array size
2561 */
2562 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2563 {
2564         if (count >= MAX_TALLOC_SIZE/el_size) {
2565                 return NULL;
2566         }
2567         return _talloc_named_const(ctx, el_size * count, name);
2568 }
2569
2570 /*
2571   alloc an zero array, checking for integer overflow in the array size
2572 */
2573 _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2574 {
2575         if (count >= MAX_TALLOC_SIZE/el_size) {
2576                 return NULL;
2577         }
2578         return _talloc_zero(ctx, el_size * count, name);
2579 }
2580
2581 /*
2582   realloc an array, checking for integer overflow in the array size
2583 */
2584 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
2585 {
2586         if (count >= MAX_TALLOC_SIZE/el_size) {
2587                 return NULL;
2588         }
2589         return _talloc_realloc(ctx, ptr, el_size * count, name);
2590 }
2591
2592 /*
2593   a function version of talloc_realloc(), so it can be passed as a function pointer
2594   to libraries that want a realloc function (a realloc function encapsulates
2595   all the basic capabilities of an allocation library, which is why this is useful)
2596 */
2597 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
2598 {
2599         return _talloc_realloc(context, ptr, size, NULL);
2600 }
2601
2602
2603 static int talloc_autofree_destructor(void *ptr)
2604 {
2605         autofree_context = NULL;
2606         return 0;
2607 }
2608
2609 static void talloc_autofree(void)
2610 {
2611         talloc_free(autofree_context);
2612 }
2613
2614 /*
2615   return a context which will be auto-freed on exit
2616   this is useful for reducing the noise in leak reports
2617 */
2618 _PUBLIC_ void *talloc_autofree_context(void)
2619 {
2620         if (autofree_context == NULL) {
2621                 autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
2622                 talloc_set_destructor(autofree_context, talloc_autofree_destructor);
2623                 atexit(talloc_autofree);
2624         }
2625         return autofree_context;
2626 }
2627
2628 _PUBLIC_ size_t talloc_get_size(const void *context)
2629 {
2630         struct talloc_chunk *tc;
2631
2632         if (context == NULL) {
2633                 context = null_context;
2634         }
2635         if (context == NULL) {
2636                 return 0;
2637         }
2638
2639         tc = talloc_chunk_from_ptr(context);
2640
2641         return tc->size;
2642 }
2643
2644 /*
2645   find a parent of this context that has the given name, if any
2646 */
2647 _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
2648 {
2649         struct talloc_chunk *tc;
2650
2651         if (context == NULL) {
2652                 return NULL;
2653         }
2654
2655         tc = talloc_chunk_from_ptr(context);
2656         while (tc) {
2657                 if (tc->name && strcmp(tc->name, name) == 0) {
2658                         return TC_PTR_FROM_CHUNK(tc);
2659                 }
2660                 while (tc && tc->prev) tc = tc->prev;
2661                 if (tc) {
2662                         tc = tc->parent;
2663                 }
2664         }
2665         return NULL;
2666 }
2667
2668 /*
2669   show the parentage of a context
2670 */
2671 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
2672 {
2673         struct talloc_chunk *tc;
2674
2675         if (context == NULL) {
2676                 fprintf(file, "talloc no parents for NULL\n");
2677                 return;
2678         }
2679
2680         tc = talloc_chunk_from_ptr(context);
2681         fprintf(file, "talloc parents of '%s'\n", __talloc_get_name(context));
2682         while (tc) {
2683                 fprintf(file, "\t'%s'\n", __talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
2684                 while (tc && tc->prev) tc = tc->prev;
2685                 if (tc) {
2686                         tc = tc->parent;
2687                 }
2688         }
2689         fflush(file);
2690 }
2691
2692 /*
2693   return 1 if ptr is a parent of context
2694 */
2695 static int _talloc_is_parent(const void *context, const void *ptr, int depth)
2696 {
2697         struct talloc_chunk *tc;
2698
2699         if (context == NULL) {
2700                 return 0;
2701         }
2702
2703         tc = talloc_chunk_from_ptr(context);
2704         while (tc) {
2705                 if (depth <= 0) {
2706                         return 0;
2707                 }
2708                 if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2709                 while (tc && tc->prev) tc = tc->prev;
2710                 if (tc) {
2711                         tc = tc->parent;
2712                         depth--;
2713                 }
2714         }
2715         return 0;
2716 }
2717
2718 /*
2719   return 1 if ptr is a parent of context
2720 */
2721 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2722 {
2723         return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);
2724 }
2725
2726 /*
2727   return the total size of memory used by this context and all children
2728 */
2729 static inline size_t _talloc_total_limit_size(const void *ptr,
2730                                         struct talloc_memlimit *old_limit,
2731                                         struct talloc_memlimit *new_limit)
2732 {
2733         return _talloc_total_mem_internal(ptr, TOTAL_MEM_LIMIT,
2734                                           old_limit, new_limit);
2735 }
2736
2737 static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size)
2738 {
2739         struct talloc_memlimit *l;
2740
2741         for (l = limit; l != NULL; l = l->upper) {
2742                 if (l->max_size != 0 &&
2743                     ((l->max_size <= l->cur_size) ||
2744                      (l->max_size - l->cur_size < size))) {
2745                         return false;
2746                 }
2747         }
2748
2749         return true;
2750 }
2751
2752 /*
2753   Update memory limits when freeing a talloc_chunk.
2754 */
2755 static void talloc_memlimit_update_on_free(struct talloc_chunk *tc)
2756 {
2757         size_t limit_shrink_size;
2758
2759         if (!tc->limit) {
2760                 return;
2761         }
2762
2763         /*
2764          * Pool entries don't count. Only the pools
2765          * themselves are counted as part of the memory
2766          * limits. Note that this also takes care of
2767          * nested pools which have both flags
2768          * TALLOC_FLAG_POOLMEM|TALLOC_FLAG_POOL set.
2769          */
2770         if (tc->flags & TALLOC_FLAG_POOLMEM) {
2771                 return;
2772         }
2773
2774         /*
2775          * If we are part of a memory limited context hierarchy
2776          * we need to subtract the memory used from the counters
2777          */
2778
2779         limit_shrink_size = tc->size+TC_HDR_SIZE;
2780
2781         /*
2782          * If we're deallocating a pool, take into
2783          * account the prefix size added for the pool.
2784          */
2785
2786         if (tc->flags & TALLOC_FLAG_POOL) {
2787                 limit_shrink_size += TP_HDR_SIZE;
2788         }
2789
2790         talloc_memlimit_shrink(tc->limit, limit_shrink_size);
2791
2792         if (tc->limit->parent == tc) {
2793                 free(tc->limit);
2794         }
2795
2796         tc->limit = NULL;
2797 }
2798
2799 /*
2800   Increase memory limit accounting after a malloc/realloc.
2801 */
2802 static void talloc_memlimit_grow(struct talloc_memlimit *limit,
2803                                 size_t size)
2804 {
2805         struct talloc_memlimit *l;
2806
2807         for (l = limit; l != NULL; l = l->upper) {
2808                 size_t new_cur_size = l->cur_size + size;
2809                 if (new_cur_size < l->cur_size) {
2810                         talloc_abort("logic error in talloc_memlimit_grow\n");
2811                         return;
2812                 }
2813                 l->cur_size = new_cur_size;
2814         }
2815 }
2816
2817 /*
2818   Decrease memory limit accounting after a free/realloc.
2819 */
2820 static void talloc_memlimit_shrink(struct talloc_memlimit *limit,
2821                                 size_t size)
2822 {
2823         struct talloc_memlimit *l;
2824
2825         for (l = limit; l != NULL; l = l->upper) {
2826                 if (l->cur_size < size) {
2827                         talloc_abort("logic error in talloc_memlimit_shrink\n");
2828                         return;
2829                 }
2830                 l->cur_size = l->cur_size - size;
2831         }
2832 }
2833
2834 _PUBLIC_ int talloc_set_memlimit(const void *ctx, size_t max_size)
2835 {
2836         struct talloc_chunk *tc = talloc_chunk_from_ptr(ctx);
2837         struct talloc_memlimit *orig_limit;
2838         struct talloc_memlimit *limit = NULL;
2839
2840         if (tc->limit && tc->limit->parent == tc) {
2841                 tc->limit->max_size = max_size;
2842                 return 0;
2843         }
2844         orig_limit = tc->limit;
2845
2846         limit = malloc(sizeof(struct talloc_memlimit));
2847         if (limit == NULL) {
2848                 return 1;
2849         }
2850         limit->parent = tc;
2851         limit->max_size = max_size;
2852         limit->cur_size = _talloc_total_limit_size(ctx, tc->limit, limit);
2853
2854         if (orig_limit) {
2855                 limit->upper = orig_limit;
2856         } else {
2857                 limit->upper = NULL;
2858         }
2859
2860         return 0;
2861 }