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