libndr: Avoid assigning duplicate versions to symbols
[amitay/samba.git] / lib / talloc / talloc.h
1 #ifndef _TALLOC_H_
2 #define _TALLOC_H_
3 /*
4    Unix SMB/CIFS implementation.
5    Samba temporary memory allocation functions
6
7    Copyright (C) Andrew Tridgell 2004-2005
8    Copyright (C) Stefan Metzmacher 2006
9
10      ** NOTE! The following LGPL license applies to the talloc
11      ** library. This does NOT imply that all of Samba is released
12      ** under the LGPL
13
14    This library is free software; you can redistribute it and/or
15    modify it under the terms of the GNU Lesser General Public
16    License as published by the Free Software Foundation; either
17    version 3 of the License, or (at your option) any later version.
18
19    This library is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    Lesser General Public License for more details.
23
24    You should have received a copy of the GNU Lesser General Public
25    License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 */
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /**
37  * @defgroup talloc The talloc API
38  *
39  * talloc is a hierarchical, reference counted memory pool system with
40  * destructors. It is the core memory allocator used in Samba.
41  *
42  * @{
43  */
44
45 #define TALLOC_VERSION_MAJOR 2
46 #define TALLOC_VERSION_MINOR 3
47
48 int talloc_version_major(void);
49 int talloc_version_minor(void);
50 /* This is mostly useful only for testing */
51 int talloc_test_get_magic(void);
52
53 /**
54  * @brief Define a talloc parent type
55  *
56  * As talloc is a hierarchial memory allocator, every talloc chunk is a
57  * potential parent to other talloc chunks. So defining a separate type for a
58  * talloc chunk is not strictly necessary. TALLOC_CTX is defined nevertheless,
59  * as it provides an indicator for function arguments. You will frequently
60  * write code like
61  *
62  * @code
63  *      struct foo *foo_create(TALLOC_CTX *mem_ctx)
64  *      {
65  *              struct foo *result;
66  *              result = talloc(mem_ctx, struct foo);
67  *              if (result == NULL) return NULL;
68  *                      ... initialize foo ...
69  *              return result;
70  *      }
71  * @endcode
72  *
73  * In this type of allocating functions it is handy to have a general
74  * TALLOC_CTX type to indicate which parent to put allocated structures on.
75  */
76 typedef void TALLOC_CTX;
77
78 /*
79   this uses a little trick to allow __LINE__ to be stringified
80 */
81 #ifndef __location__
82 #define __TALLOC_STRING_LINE1__(s)    #s
83 #define __TALLOC_STRING_LINE2__(s)   __TALLOC_STRING_LINE1__(s)
84 #define __TALLOC_STRING_LINE3__  __TALLOC_STRING_LINE2__(__LINE__)
85 #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__
86 #endif
87
88 #ifndef TALLOC_DEPRECATED
89 #define TALLOC_DEPRECATED 0
90 #endif
91
92 /* for old gcc releases that don't have the feature test macro __has_attribute */
93 #ifndef __has_attribute
94 #define __has_attribute(x) 0
95 #endif
96
97 #ifndef PRINTF_ATTRIBUTE
98 #if __has_attribute(format) || (__GNUC__ >= 3)
99 /** Use gcc attribute to check printf fns.  a1 is the 1-based index of
100  * the parameter containing the format, and a2 the index of the first
101  * argument. Note that some gcc 2.x versions don't handle this
102  * properly **/
103 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
104 #else
105 #define PRINTF_ATTRIBUTE(a1, a2)
106 #endif
107 #endif
108
109 #ifndef _DEPRECATED_
110 #if __has_attribute(deprecated) || (__GNUC__ >= 3)
111 #define _DEPRECATED_ __attribute__ ((deprecated))
112 #else
113 #define _DEPRECATED_
114 #endif
115 #endif
116 #ifdef DOXYGEN
117
118 /**
119  * @brief Create a new talloc context.
120  *
121  * The talloc() macro is the core of the talloc library. It takes a memory
122  * context and a type, and returns a pointer to a new area of memory of the
123  * given type.
124  *
125  * The returned pointer is itself a talloc context, so you can use it as the
126  * context argument to more calls to talloc if you wish.
127  *
128  * The returned pointer is a "child" of the supplied context. This means that if
129  * you talloc_free() the context then the new child disappears as well.
130  * Alternatively you can free just the child.
131  *
132  * @param[in]  ctx      A talloc context to create a new reference on or NULL to
133  *                      create a new top level context.
134  *
135  * @param[in]  type     The type of memory to allocate.
136  *
137  * @return              A type casted talloc context or NULL on error.
138  *
139  * @code
140  *      unsigned int *a, *b;
141  *
142  *      a = talloc(NULL, unsigned int);
143  *      b = talloc(a, unsigned int);
144  * @endcode
145  *
146  * @see talloc_zero
147  * @see talloc_array
148  * @see talloc_steal
149  * @see talloc_free
150  */
151 void *talloc(const void *ctx, #type);
152 #else
153 #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
154 void *_talloc(const void *context, size_t size);
155 #endif
156
157 /**
158  * @brief Create a new top level talloc context.
159  *
160  * This function creates a zero length named talloc context as a top level
161  * context. It is equivalent to:
162  *
163  * @code
164  *      talloc_named(NULL, 0, fmt, ...);
165  * @endcode
166  * @param[in]  fmt      Format string for the name.
167  *
168  * @param[in]  ...      Additional printf-style arguments.
169  *
170  * @return              The allocated memory chunk, NULL on error.
171  *
172  * @see talloc_named()
173  */
174 void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
175
176 #ifdef DOXYGEN
177 /**
178  * @brief Free a chunk of talloc memory.
179  *
180  * The talloc_free() function frees a piece of talloc memory, and all its
181  * children. You can call talloc_free() on any pointer returned by
182  * talloc().
183  *
184  * The return value of talloc_free() indicates success or failure, with 0
185  * returned for success and -1 for failure. A possible failure condition
186  * is if the pointer had a destructor attached to it and the destructor
187  * returned -1. See talloc_set_destructor() for details on
188  * destructors. Likewise, if "ptr" is NULL, then the function will make
189  * no modifications and return -1.
190  *
191  * From version 2.0 and onwards, as a special case, talloc_free() is
192  * refused on pointers that have more than one parent associated, as talloc
193  * would have no way of knowing which parent should be removed. This is
194  * different from older versions in the sense that always the reference to
195  * the most recently established parent has been destroyed. Hence to free a
196  * pointer that has more than one parent please use talloc_unlink().
197  *
198  * To help you find problems in your code caused by this behaviour, if
199  * you do try and free a pointer with more than one parent then the
200  * talloc logging function will be called to give output like this:
201  *
202  * @code
203  *   ERROR: talloc_free with references at some_dir/source/foo.c:123
204  *     reference at some_dir/source/other.c:325
205  *     reference at some_dir/source/third.c:121
206  * @endcode
207  *
208  * Please see the documentation for talloc_set_log_fn() and
209  * talloc_set_log_stderr() for more information on talloc logging
210  * functions.
211  *
212  * If <code>TALLOC_FREE_FILL</code> environment variable is set,
213  * the memory occupied by the context is filled with the value of this variable.
214  * The value should be a numeric representation of the character you want to
215  * use.
216  *
217  * talloc_free() operates recursively on its children.
218  *
219  * @param[in]  ptr      The chunk to be freed.
220  *
221  * @return              Returns 0 on success and -1 on error. A possible
222  *                      failure condition is if the pointer had a destructor
223  *                      attached to it and the destructor returned -1. Likewise,
224  *                      if "ptr" is NULL, then the function will make no
225  *                      modifications and returns -1.
226  *
227  * Example:
228  * @code
229  *      unsigned int *a, *b;
230  *      a = talloc(NULL, unsigned int);
231  *      b = talloc(a, unsigned int);
232  *
233  *      talloc_free(a); // Frees a and b
234  * @endcode
235  *
236  * @see talloc_set_destructor()
237  * @see talloc_unlink()
238  */
239 int talloc_free(void *ptr);
240 #else
241 #define talloc_free(ctx) _talloc_free(ctx, __location__)
242 int _talloc_free(void *ptr, const char *location);
243 #endif
244
245 /**
246  * @brief Free a talloc chunk's children.
247  *
248  * The function walks along the list of all children of a talloc context and
249  * talloc_free()s only the children, not the context itself.
250  *
251  * A NULL argument is handled as no-op.
252  *
253  * @param[in]  ptr      The chunk that you want to free the children of
254  *                      (NULL is allowed too)
255  */
256 void talloc_free_children(void *ptr);
257
258 #ifdef DOXYGEN
259 /**
260  * @brief Assign a destructor function to be called when a chunk is freed.
261  *
262  * The function talloc_set_destructor() sets the "destructor" for the pointer
263  * "ptr". A destructor is a function that is called when the memory used by a
264  * pointer is about to be released. The destructor receives the pointer as an
265  * argument, and should return 0 for success and -1 for failure.
266  *
267  * The destructor can do anything it wants to, including freeing other pieces
268  * of memory. A common use for destructors is to clean up operating system
269  * resources (such as open file descriptors) contained in the structure the
270  * destructor is placed on.
271  *
272  * You can only place one destructor on a pointer. If you need more than one
273  * destructor then you can create a zero-length child of the pointer and place
274  * an additional destructor on that.
275  *
276  * To remove a destructor call talloc_set_destructor() with NULL for the
277  * destructor.
278  *
279  * If your destructor attempts to talloc_free() the pointer that it is the
280  * destructor for then talloc_free() will return -1 and the free will be
281  * ignored. This would be a pointless operation anyway, as the destructor is
282  * only called when the memory is just about to go away.
283  *
284  * @param[in]  ptr      The talloc chunk to add a destructor to.
285  *
286  * @param[in]  destructor  The destructor function to be called. NULL to remove
287  *                         it.
288  *
289  * Example:
290  * @code
291  *      static int destroy_fd(int *fd) {
292  *              close(*fd);
293  *              return 0;
294  *      }
295  *
296  *      int *open_file(const char *filename) {
297  *              int *fd = talloc(NULL, int);
298  *              *fd = open(filename, O_RDONLY);
299  *              if (*fd < 0) {
300  *                      talloc_free(fd);
301  *                      return NULL;
302  *              }
303  *              // Whenever they free this, we close the file.
304  *              talloc_set_destructor(fd, destroy_fd);
305  *              return fd;
306  *      }
307  * @endcode
308  *
309  * @see talloc()
310  * @see talloc_free()
311  */
312 void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
313
314 /**
315  * @brief Change a talloc chunk's parent.
316  *
317  * The talloc_steal() function changes the parent context of a talloc
318  * pointer. It is typically used when the context that the pointer is
319  * currently a child of is going to be freed and you wish to keep the
320  * memory for a longer time.
321  *
322  * To make the changed hierarchy less error-prone, you might consider to use
323  * talloc_move().
324  *
325  * If you try and call talloc_steal() on a pointer that has more than one
326  * parent then the result is ambiguous. Talloc will choose to remove the
327  * parent that is currently indicated by talloc_parent() and replace it with
328  * the chosen parent. You will also get a message like this via the talloc
329  * logging functions:
330  *
331  * @code
332  *   WARNING: talloc_steal with references at some_dir/source/foo.c:123
333  *     reference at some_dir/source/other.c:325
334  *     reference at some_dir/source/third.c:121
335  * @endcode
336  *
337  * To unambiguously change the parent of a pointer please see the function
338  * talloc_reparent(). See the talloc_set_log_fn() documentation for more
339  * information on talloc logging.
340  *
341  * @param[in]  new_ctx  The new parent context.
342  *
343  * @param[in]  ptr      The talloc chunk to move.
344  *
345  * @return              Returns the pointer that you pass it. It does not have
346  *                      any failure modes.
347  *
348  * @note It is possible to produce loops in the parent/child relationship
349  * if you are not careful with talloc_steal(). No guarantees are provided
350  * as to your sanity or the safety of your data if you do this.
351  */
352 void *talloc_steal(const void *new_ctx, const void *ptr);
353 #else /* DOXYGEN */
354 /* try to make talloc_set_destructor() and talloc_steal() type safe,
355    if we have a recent gcc */
356 #if (__GNUC__ >= 3)
357 #define _TALLOC_TYPEOF(ptr) __typeof__(ptr)
358 #define talloc_set_destructor(ptr, function)                                  \
359         do {                                                                  \
360                 int (*_talloc_destructor_fn)(_TALLOC_TYPEOF(ptr)) = (function);       \
361                 _talloc_set_destructor((ptr), (int (*)(void *))_talloc_destructor_fn); \
362         } while(0)
363 /* this extremely strange macro is to avoid some braindamaged warning
364    stupidity in gcc 4.1.x */
365 #define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__); __talloc_steal_ret; })
366 #else /* __GNUC__ >= 3 */
367 #define talloc_set_destructor(ptr, function) \
368         _talloc_set_destructor((ptr), (int (*)(void *))(function))
369 #define _TALLOC_TYPEOF(ptr) void *
370 #define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__)
371 #endif /* __GNUC__ >= 3 */
372 void _talloc_set_destructor(const void *ptr, int (*_destructor)(void *));
373 void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location);
374 #endif /* DOXYGEN */
375
376 /**
377  * @brief Assign a name to a talloc chunk.
378  *
379  * Each talloc pointer has a "name". The name is used principally for
380  * debugging purposes, although it is also possible to set and get the name on
381  * a pointer in as a way of "marking" pointers in your code.
382  *
383  * The main use for names on pointer is for "talloc reports". See
384  * talloc_report() and talloc_report_full() for details. Also see
385  * talloc_enable_leak_report() and talloc_enable_leak_report_full().
386  *
387  * The talloc_set_name() function allocates memory as a child of the
388  * pointer. It is logically equivalent to:
389  *
390  * @code
391  *      talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
392  * @endcode
393  *
394  * @param[in]  ptr      The talloc chunk to assign a name to.
395  *
396  * @param[in]  fmt      Format string for the name.
397  *
398  * @param[in]  ...      Add printf-style additional arguments.
399  *
400  * @return              The assigned name, NULL on error.
401  *
402  * @note Multiple calls to talloc_set_name() will allocate more memory without
403  * releasing the name. All of the memory is released when the ptr is freed
404  * using talloc_free().
405  */
406 const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
407
408 #ifdef DOXYGEN
409 /**
410  * @brief Change a talloc chunk's parent.
411  *
412  * This function has the same effect as talloc_steal(), and additionally sets
413  * the source pointer to NULL. You would use it like this:
414  *
415  * @code
416  *      struct foo *X = talloc(tmp_ctx, struct foo);
417  *      struct foo *Y;
418  *      Y = talloc_move(new_ctx, &X);
419  * @endcode
420  *
421  * @param[in]  new_ctx  The new parent context.
422  *
423  * @param[in]  pptr     Pointer to a pointer to the talloc chunk to move.
424  *
425  * @return              The pointer to the talloc chunk that moved.
426  *                      It does not have any failure modes.
427  *
428  */
429 void *talloc_move(const void *new_ctx, void **pptr);
430 #else
431 #define talloc_move(ctx, pptr) (_TALLOC_TYPEOF(*(pptr)))_talloc_move((ctx),(void *)(pptr))
432 void *_talloc_move(const void *new_ctx, const void *pptr);
433 #endif
434
435 /**
436  * @brief Assign a name to a talloc chunk.
437  *
438  * The function is just like talloc_set_name(), but it takes a string constant,
439  * and is much faster. It is extensively used by the "auto naming" macros, such
440  * as talloc_p().
441  *
442  * This function does not allocate any memory. It just copies the supplied
443  * pointer into the internal representation of the talloc ptr. This means you
444  * must not pass a name pointer to memory that will disappear before the ptr
445  * is freed with talloc_free().
446  *
447  * @param[in]  ptr      The talloc chunk to assign a name to.
448  *
449  * @param[in]  name     Format string for the name.
450  */
451 void talloc_set_name_const(const void *ptr, const char *name);
452
453 /**
454  * @brief Create a named talloc chunk.
455  *
456  * The talloc_named() function creates a named talloc pointer. It is
457  * equivalent to:
458  *
459  * @code
460  *      ptr = talloc_size(context, size);
461  *      talloc_set_name(ptr, fmt, ....);
462  * @endcode
463  *
464  * @param[in]  context  The talloc context to hang the result off.
465  *
466  * @param[in]  size     Number of char's that you want to allocate.
467  *
468  * @param[in]  fmt      Format string for the name.
469  *
470  * @param[in]  ...      Additional printf-style arguments.
471  *
472  * @return              The allocated memory chunk, NULL on error.
473  *
474  * @see talloc_set_name()
475  */
476 void *talloc_named(const void *context, size_t size,
477                    const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
478
479 /**
480  * @brief Basic routine to allocate a chunk of memory.
481  *
482  * This is equivalent to:
483  *
484  * @code
485  *      ptr = talloc_size(context, size);
486  *      talloc_set_name_const(ptr, name);
487  * @endcode
488  *
489  * @param[in]  context  The parent context.
490  *
491  * @param[in]  size     The number of char's that we want to allocate.
492  *
493  * @param[in]  name     The name the talloc block has.
494  *
495  * @return             The allocated memory chunk, NULL on error.
496  */
497 void *talloc_named_const(const void *context, size_t size, const char *name);
498
499 #ifdef DOXYGEN
500 /**
501  * @brief Untyped allocation.
502  *
503  * The function should be used when you don't have a convenient type to pass to
504  * talloc(). Unlike talloc(), it is not type safe (as it returns a void *), so
505  * you are on your own for type checking.
506  *
507  * Best to use talloc() or talloc_array() instead.
508  *
509  * @param[in]  ctx     The talloc context to hang the result off.
510  *
511  * @param[in]  size    Number of char's that you want to allocate.
512  *
513  * @return             The allocated memory chunk, NULL on error.
514  *
515  * Example:
516  * @code
517  *      void *mem = talloc_size(NULL, 100);
518  * @endcode
519  */
520 void *talloc_size(const void *ctx, size_t size);
521 #else
522 #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
523 #endif
524
525 #ifdef DOXYGEN
526 /**
527  * @brief Allocate into a typed pointer.
528  *
529  * The talloc_ptrtype() macro should be used when you have a pointer and want
530  * to allocate memory to point at with this pointer. When compiling with
531  * gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size() and
532  * talloc_get_name() will return the current location in the source file and
533  * not the type.
534  *
535  * @param[in]  ctx      The talloc context to hang the result off.
536  *
537  * @param[in]  type     The pointer you want to assign the result to.
538  *
539  * @return              The properly casted allocated memory chunk, NULL on
540  *                      error.
541  *
542  * Example:
543  * @code
544  *       unsigned int *a = talloc_ptrtype(NULL, a);
545  * @endcode
546  */
547 void *talloc_ptrtype(const void *ctx, #type);
548 #else
549 #define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
550 #endif
551
552 #ifdef DOXYGEN
553 /**
554  * @brief Allocate a new 0-sized talloc chunk.
555  *
556  * This is a utility macro that creates a new memory context hanging off an
557  * existing context, automatically naming it "talloc_new: __location__" where
558  * __location__ is the source line it is called from. It is particularly
559  * useful for creating a new temporary working context.
560  *
561  * @param[in]  ctx      The talloc parent context.
562  *
563  * @return              A new talloc chunk, NULL on error.
564  */
565 void *talloc_new(const void *ctx);
566 #else
567 #define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
568 #endif
569
570 #ifdef DOXYGEN
571 /**
572  * @brief Allocate a 0-initizialized structure.
573  *
574  * The macro is equivalent to:
575  *
576  * @code
577  *      ptr = talloc(ctx, type);
578  *      if (ptr) memset(ptr, 0, sizeof(type));
579  * @endcode
580  *
581  * @param[in]  ctx      The talloc context to hang the result off.
582  *
583  * @param[in]  type     The type that we want to allocate.
584  *
585  * @return              Pointer to a piece of memory, properly cast to 'type *',
586  *                      NULL on error.
587  *
588  * Example:
589  * @code
590  *      unsigned int *a, *b;
591  *      a = talloc_zero(NULL, unsigned int);
592  *      b = talloc_zero(a, unsigned int);
593  * @endcode
594  *
595  * @see talloc()
596  * @see talloc_zero_size()
597  * @see talloc_zero_array()
598  */
599 void *talloc_zero(const void *ctx, #type);
600
601 /**
602  * @brief Allocate untyped, 0-initialized memory.
603  *
604  * @param[in]  ctx      The talloc context to hang the result off.
605  *
606  * @param[in]  size     Number of char's that you want to allocate.
607  *
608  * @return              The allocated memory chunk.
609  */
610 void *talloc_zero_size(const void *ctx, size_t size);
611 #else
612 #define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
613 #define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
614 void *_talloc_zero(const void *ctx, size_t size, const char *name);
615 #endif
616
617 /**
618  * @brief Return the name of a talloc chunk.
619  *
620  * @param[in]  ptr      The talloc chunk.
621  *
622  * @return              The current name for the given talloc pointer.
623  *
624  * @see talloc_set_name()
625  */
626 const char *talloc_get_name(const void *ptr);
627
628 /**
629  * @brief Verify that a talloc chunk carries a specified name.
630  *
631  * This function checks if a pointer has the specified name. If it does
632  * then the pointer is returned.
633  *
634  * @param[in]  ptr       The talloc chunk to check.
635  *
636  * @param[in]  name      The name to check against.
637  *
638  * @return               The pointer if the name matches, NULL if it doesn't.
639  */
640 void *talloc_check_name(const void *ptr, const char *name);
641
642 /**
643  * @brief Get the parent chunk of a pointer.
644  *
645  * @param[in]  ptr      The talloc pointer to inspect.
646  *
647  * @return              The talloc parent of ptr, NULL on error.
648  */
649 void *talloc_parent(const void *ptr);
650
651 /**
652  * @brief Get a talloc chunk's parent name.
653  *
654  * @param[in]  ptr      The talloc pointer to inspect.
655  *
656  * @return              The name of ptr's parent chunk.
657  */
658 const char *talloc_parent_name(const void *ptr);
659
660 /**
661  * @brief Get the total size of a talloc chunk including its children.
662  *
663  * The function returns the total size in bytes used by this pointer and all
664  * child pointers. Mostly useful for debugging.
665  *
666  * Passing NULL is allowed, but it will only give a meaningful result if
667  * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
668  * been called.
669  *
670  * @param[in]  ptr      The talloc chunk.
671  *
672  * @return              The total size.
673  */
674 size_t talloc_total_size(const void *ptr);
675
676 /**
677  * @brief Get the number of talloc chunks hanging off a chunk.
678  *
679  * The talloc_total_blocks() function returns the total memory block
680  * count used by this pointer and all child pointers. Mostly useful for
681  * debugging.
682  *
683  * Passing NULL is allowed, but it will only give a meaningful result if
684  * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
685  * been called.
686  *
687  * @param[in]  ptr      The talloc chunk.
688  *
689  * @return              The total size.
690  */
691 size_t talloc_total_blocks(const void *ptr);
692
693 #ifdef DOXYGEN
694 /**
695  * @brief Duplicate a memory area into a talloc chunk.
696  *
697  * The function is equivalent to:
698  *
699  * @code
700  *      ptr = talloc_size(ctx, size);
701  *      if (ptr) memcpy(ptr, p, size);
702  * @endcode
703  *
704  * @param[in]  t        The talloc context to hang the result off.
705  *
706  * @param[in]  p        The memory chunk you want to duplicate.
707  *
708  * @param[in]  size     Number of char's that you want copy.
709  *
710  * @return              The allocated memory chunk.
711  *
712  * @see talloc_size()
713  */
714 void *talloc_memdup(const void *t, const void *p, size_t size);
715 #else
716 #define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
717 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
718 #endif
719
720 #ifdef DOXYGEN
721 /**
722  * @brief Assign a type to a talloc chunk.
723  *
724  * This macro allows you to force the name of a pointer to be of a particular
725  * type. This can be used in conjunction with talloc_get_type() to do type
726  * checking on void* pointers.
727  *
728  * It is equivalent to this:
729  *
730  * @code
731  *      talloc_set_name_const(ptr, #type)
732  * @endcode
733  *
734  * @param[in]  ptr      The talloc chunk to assign the type to.
735  *
736  * @param[in]  type     The type to assign.
737  */
738 void talloc_set_type(const char *ptr, #type);
739
740 /**
741  * @brief Get a typed pointer out of a talloc pointer.
742  *
743  * This macro allows you to do type checking on talloc pointers. It is
744  * particularly useful for void* private pointers. It is equivalent to
745  * this:
746  *
747  * @code
748  *      (type *)talloc_check_name(ptr, #type)
749  * @endcode
750  *
751  * @param[in]  ptr      The talloc pointer to check.
752  *
753  * @param[in]  type     The type to check against.
754  *
755  * @return              The properly casted pointer given by ptr, NULL on error.
756  */
757 type *talloc_get_type(const void *ptr, #type);
758 #else
759 #define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
760 #define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
761 #endif
762
763 #ifdef DOXYGEN
764 /**
765  * @brief Safely turn a void pointer into a typed pointer.
766  *
767  * This macro is used together with talloc(mem_ctx, struct foo). If you had to
768  * assign the talloc chunk pointer to some void pointer variable,
769  * talloc_get_type_abort() is the recommended way to get the convert the void
770  * pointer back to a typed pointer.
771  *
772  * @param[in]  ptr      The void pointer to convert.
773  *
774  * @param[in]  type     The type that this chunk contains
775  *
776  * @return              The same value as ptr, type-checked and properly cast.
777  */
778 void *talloc_get_type_abort(const void *ptr, #type);
779 #else
780 #ifdef TALLOC_GET_TYPE_ABORT_NOOP
781 #define talloc_get_type_abort(ptr, type) (type *)(ptr)
782 #else
783 #define talloc_get_type_abort(ptr, type) (type *)_talloc_get_type_abort(ptr, #type, __location__)
784 #endif
785 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location);
786 #endif
787
788 /**
789  * @brief Find a parent context by name.
790  *
791  * Find a parent memory context of the current context that has the given
792  * name. This can be very useful in complex programs where it may be
793  * difficult to pass all information down to the level you need, but you
794  * know the structure you want is a parent of another context.
795  *
796  * @param[in]  ctx      The talloc chunk to start from.
797  *
798  * @param[in]  name     The name of the parent we look for.
799  *
800  * @return              The memory context we are looking for, NULL if not
801  *                      found.
802  */
803 void *talloc_find_parent_byname(const void *ctx, const char *name);
804
805 #ifdef DOXYGEN
806 /**
807  * @brief Find a parent context by type.
808  *
809  * Find a parent memory context of the current context that has the given
810  * name. This can be very useful in complex programs where it may be
811  * difficult to pass all information down to the level you need, but you
812  * know the structure you want is a parent of another context.
813  *
814  * Like talloc_find_parent_byname() but takes a type, making it typesafe.
815  *
816  * @param[in]  ptr      The talloc chunk to start from.
817  *
818  * @param[in]  type     The type of the parent to look for.
819  *
820  * @return              The memory context we are looking for, NULL if not
821  *                      found.
822  */
823 void *talloc_find_parent_bytype(const void *ptr, #type);
824 #else
825 #define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
826 #endif
827
828 /**
829  * @brief Allocate a talloc pool.
830  *
831  * A talloc pool is a pure optimization for specific situations. In the
832  * release process for Samba 3.2 we found out that we had become considerably
833  * slower than Samba 3.0 was. Profiling showed that malloc(3) was a large CPU
834  * consumer in benchmarks. For Samba 3.2 we have internally converted many
835  * static buffers to dynamically allocated ones, so malloc(3) being beaten
836  * more was no surprise. But it made us slower.
837  *
838  * talloc_pool() is an optimization to call malloc(3) a lot less for the use
839  * pattern Samba has: The SMB protocol is mainly a request/response protocol
840  * where we have to allocate a certain amount of memory per request and free
841  * that after the SMB reply is sent to the client.
842  *
843  * talloc_pool() creates a talloc chunk that you can use as a talloc parent
844  * exactly as you would use any other ::TALLOC_CTX. The difference is that
845  * when you talloc a child of this pool, no malloc(3) is done. Instead, talloc
846  * just increments a pointer inside the talloc_pool. This also works
847  * recursively. If you use the child of the talloc pool as a parent for
848  * grand-children, their memory is also taken from the talloc pool.
849  *
850  * If there is not enough memory in the pool to allocate the new child,
851  * it will create a new talloc chunk as if the parent was a normal talloc
852  * context.
853  *
854  * If you talloc_free() children of a talloc pool, the memory is not given
855  * back to the system. Instead, free(3) is only called if the talloc_pool()
856  * itself is released with talloc_free().
857  *
858  * The downside of a talloc pool is that if you talloc_move() a child of a
859  * talloc pool to a talloc parent outside the pool, the whole pool memory is
860  * not free(3)'ed until that moved chunk is also talloc_free()ed.
861  *
862  * @param[in]  context  The talloc context to hang the result off.
863  *
864  * @param[in]  size     Size of the talloc pool.
865  *
866  * @return              The allocated talloc pool, NULL on error.
867  */
868 void *talloc_pool(const void *context, size_t size);
869
870 #ifdef DOXYGEN
871 /**
872  * @brief Allocate a talloc object as/with an additional pool.
873  *
874  * This is like talloc_pool(), but's it's more flexible
875  * and allows an object to be a pool for its children.
876  *
877  * @param[in] ctx                   The talloc context to hang the result off.
878  *
879  * @param[in] type                  The type that we want to allocate.
880  *
881  * @param[in] num_subobjects        The expected number of subobjects, which will
882  *                                  be allocated within the pool. This allocates
883  *                                  space for talloc_chunk headers.
884  *
885  * @param[in] total_subobjects_size The size that all subobjects can use in total.
886  *
887  *
888  * @return              The allocated talloc object, NULL on error.
889  */
890 void *talloc_pooled_object(const void *ctx, #type,
891                            unsigned num_subobjects,
892                            size_t total_subobjects_size);
893 #else
894 #define talloc_pooled_object(_ctx, _type, \
895                              _num_subobjects, \
896                              _total_subobjects_size) \
897         (_type *)_talloc_pooled_object((_ctx), sizeof(_type), #_type, \
898                                         (_num_subobjects), \
899                                         (_total_subobjects_size))
900 void *_talloc_pooled_object(const void *ctx,
901                             size_t type_size,
902                             const char *type_name,
903                             unsigned num_subobjects,
904                             size_t total_subobjects_size);
905 #endif
906
907 /**
908  * @brief Free a talloc chunk and NULL out the pointer.
909  *
910  * TALLOC_FREE() frees a pointer and sets it to NULL. Use this if you want
911  * immediate feedback (i.e. crash) if you use a pointer after having free'ed
912  * it.
913  *
914  * @param[in]  ctx      The chunk to be freed.
915  */
916 #define TALLOC_FREE(ctx) do { if (ctx != NULL) { talloc_free(ctx); ctx=NULL; } } while(0)
917
918 /* @} ******************************************************************/
919
920 /**
921  * \defgroup talloc_ref The talloc reference function.
922  * @ingroup talloc
923  *
924  * This module contains the definitions around talloc references
925  *
926  * @{
927  */
928
929 /**
930  * @brief Increase the reference count of a talloc chunk.
931  *
932  * The talloc_increase_ref_count(ptr) function is exactly equivalent to:
933  *
934  * @code
935  *      talloc_reference(NULL, ptr);
936  * @endcode
937  *
938  * You can use either syntax, depending on which you think is clearer in
939  * your code.
940  *
941  * @param[in]  ptr      The pointer to increase the reference count.
942  *
943  * @return              0 on success, -1 on error.
944  */
945 int talloc_increase_ref_count(const void *ptr);
946
947 /**
948  * @brief Get the number of references to a talloc chunk.
949  *
950  * @param[in]  ptr      The pointer to retrieve the reference count from.
951  *
952  * @return              The number of references.
953  */
954 size_t talloc_reference_count(const void *ptr);
955
956 #ifdef DOXYGEN
957 /**
958  * @brief Create an additional talloc parent to a pointer.
959  *
960  * The talloc_reference() function makes "context" an additional parent of
961  * ptr. Each additional reference consumes around 48 bytes of memory on intel
962  * x86 platforms.
963  *
964  * If ptr is NULL, then the function is a no-op, and simply returns NULL.
965  *
966  * After creating a reference you can free it in one of the following ways:
967  *
968  * - you can talloc_free() any parent of the original pointer. That
969  *   will reduce the number of parents of this pointer by 1, and will
970  *   cause this pointer to be freed if it runs out of parents.
971  *
972  * - you can talloc_free() the pointer itself if it has at maximum one
973  *   parent. This behaviour has been changed since the release of version
974  *   2.0. Further information in the description of "talloc_free".
975  *
976  * For more control on which parent to remove, see talloc_unlink()
977  * @param[in]  ctx      The additional parent.
978  *
979  * @param[in]  ptr      The pointer you want to create an additional parent for.
980  *
981  * @return              The original pointer 'ptr', NULL if talloc ran out of
982  *                      memory in creating the reference.
983  *
984  * @warning You should try to avoid using this interface. It turns a beautiful
985  *          talloc-tree into a graph. It is often really hard to debug if you
986  *          screw something up by accident.
987  *
988  * Example:
989  * @code
990  *      unsigned int *a, *b, *c;
991  *      a = talloc(NULL, unsigned int);
992  *      b = talloc(NULL, unsigned int);
993  *      c = talloc(a, unsigned int);
994  *      // b also serves as a parent of c.
995  *      talloc_reference(b, c);
996  * @endcode
997  *
998  * @see talloc_unlink()
999  */
1000 void *talloc_reference(const void *ctx, const void *ptr);
1001 #else
1002 #define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference_loc((ctx),(ptr), __location__)
1003 void *_talloc_reference_loc(const void *context, const void *ptr, const char *location);
1004 #endif
1005
1006 /**
1007  * @brief Remove a specific parent from a talloc chunk.
1008  *
1009  * The function removes a specific parent from ptr. The context passed must
1010  * either be a context used in talloc_reference() with this pointer, or must be
1011  * a direct parent of ptr.
1012  *
1013  * You can just use talloc_free() instead of talloc_unlink() if there
1014  * is at maximum one parent. This behaviour has been changed since the
1015  * release of version 2.0. Further information in the description of
1016  * "talloc_free".
1017  *
1018  * @param[in]  context  The talloc parent to remove.
1019  *
1020  * @param[in]  ptr      The talloc ptr you want to remove the parent from.
1021  *
1022  * @return              0 on success, -1 on error.
1023  *
1024  * @note If the parent has already been removed using talloc_free() then
1025  * this function will fail and will return -1.  Likewise, if ptr is NULL,
1026  * then the function will make no modifications and return -1.
1027  *
1028  * @warning You should try to avoid using this interface. It turns a beautiful
1029  *          talloc-tree into a graph. It is often really hard to debug if you
1030  *          screw something up by accident.
1031  *
1032  * Example:
1033  * @code
1034  *      unsigned int *a, *b, *c;
1035  *      a = talloc(NULL, unsigned int);
1036  *      b = talloc(NULL, unsigned int);
1037  *      c = talloc(a, unsigned int);
1038  *      // b also serves as a parent of c.
1039  *      talloc_reference(b, c);
1040  *      talloc_unlink(b, c);
1041  * @endcode
1042  */
1043 int talloc_unlink(const void *context, void *ptr);
1044
1045 /**
1046  * @brief Provide a talloc context that is freed at program exit.
1047  *
1048  * This is a handy utility function that returns a talloc context
1049  * which will be automatically freed on program exit. This can be used
1050  * to reduce the noise in memory leak reports.
1051  *
1052  * Never use this in code that might be used in objects loaded with
1053  * dlopen and unloaded with dlclose. talloc_autofree_context()
1054  * internally uses atexit(3). Some platforms like modern Linux handles
1055  * this fine, but for example FreeBSD does not deal well with dlopen()
1056  * and atexit() used simultaneously: dlclose() does not clean up the
1057  * list of atexit-handlers, so when the program exits the code that
1058  * was registered from within talloc_autofree_context() is gone, the
1059  * program crashes at exit.
1060  *
1061  * @return              A talloc context, NULL on error.
1062  */
1063 void *talloc_autofree_context(void) _DEPRECATED_;
1064
1065 /**
1066  * @brief Get the size of a talloc chunk.
1067  *
1068  * This function lets you know the amount of memory allocated so far by
1069  * this context. It does NOT account for subcontext memory.
1070  * This can be used to calculate the size of an array.
1071  *
1072  * @param[in]  ctx      The talloc chunk.
1073  *
1074  * @return              The size of the talloc chunk.
1075  */
1076 size_t talloc_get_size(const void *ctx);
1077
1078 /**
1079  * @brief Show the parentage of a context.
1080  *
1081  * @param[in]  context            The talloc context to look at.
1082  *
1083  * @param[in]  file               The output to use, a file, stdout or stderr.
1084  */
1085 void talloc_show_parents(const void *context, FILE *file);
1086
1087 /**
1088  * @brief Check if a context is parent of a talloc chunk.
1089  *
1090  * This checks if context is referenced in the talloc hierarchy above ptr.
1091  *
1092  * @param[in]  context  The assumed talloc context.
1093  *
1094  * @param[in]  ptr      The talloc chunk to check.
1095  *
1096  * @return              Return 1 if this is the case, 0 if not.
1097  */
1098 int talloc_is_parent(const void *context, const void *ptr);
1099
1100 /**
1101  * @brief Change the parent context of a talloc pointer.
1102  *
1103  * The function changes the parent context of a talloc pointer. It is typically
1104  * used when the context that the pointer is currently a child of is going to be
1105  * freed and you wish to keep the memory for a longer time.
1106  *
1107  * The difference between talloc_reparent() and talloc_steal() is that
1108  * talloc_reparent() can specify which parent you wish to change. This is
1109  * useful when a pointer has multiple parents via references.
1110  *
1111  * @param[in]  old_parent
1112  * @param[in]  new_parent
1113  * @param[in]  ptr
1114  *
1115  * @return              Return the pointer you passed. It does not have any
1116  *                      failure modes.
1117  */
1118 void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr);
1119
1120 /* @} ******************************************************************/
1121
1122 /**
1123  * @defgroup talloc_array The talloc array functions
1124  * @ingroup talloc
1125  *
1126  * Talloc contains some handy helpers for handling Arrays conveniently
1127  *
1128  * @{
1129  */
1130
1131 #ifdef DOXYGEN
1132 /**
1133  * @brief Allocate an array.
1134  *
1135  * The macro is equivalent to:
1136  *
1137  * @code
1138  *      (type *)talloc_size(ctx, sizeof(type) * count);
1139  * @endcode
1140  *
1141  * except that it provides integer overflow protection for the multiply,
1142  * returning NULL if the multiply overflows.
1143  *
1144  * @param[in]  ctx      The talloc context to hang the result off.
1145  *
1146  * @param[in]  type     The type that we want to allocate.
1147  *
1148  * @param[in]  count    The number of 'type' elements you want to allocate.
1149  *
1150  * @return              The allocated result, properly cast to 'type *', NULL on
1151  *                      error.
1152  *
1153  * Example:
1154  * @code
1155  *      unsigned int *a, *b;
1156  *      a = talloc_zero(NULL, unsigned int);
1157  *      b = talloc_array(a, unsigned int, 100);
1158  * @endcode
1159  *
1160  * @see talloc()
1161  * @see talloc_zero_array()
1162  */
1163 void *talloc_array(const void *ctx, #type, unsigned count);
1164 #else
1165 #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
1166 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
1167 #endif
1168
1169 #ifdef DOXYGEN
1170 /**
1171  * @brief Allocate an array.
1172  *
1173  * @param[in]  ctx      The talloc context to hang the result off.
1174  *
1175  * @param[in]  size     The size of an array element.
1176  *
1177  * @param[in]  count    The number of elements you want to allocate.
1178  *
1179  * @return              The allocated result, NULL on error.
1180  */
1181 void *talloc_array_size(const void *ctx, size_t size, unsigned count);
1182 #else
1183 #define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
1184 #endif
1185
1186 #ifdef DOXYGEN
1187 /**
1188  * @brief Allocate an array into a typed pointer.
1189  *
1190  * The macro should be used when you have a pointer to an array and want to
1191  * allocate memory of an array to point at with this pointer. When compiling
1192  * with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
1193  * and talloc_get_name() will return the current location in the source file
1194  * and not the type.
1195  *
1196  * @param[in]  ctx      The talloc context to hang the result off.
1197  *
1198  * @param[in]  ptr      The pointer you want to assign the result to.
1199  *
1200  * @param[in]  count    The number of elements you want to allocate.
1201  *
1202  * @return              The allocated memory chunk, properly casted. NULL on
1203  *                      error.
1204  */
1205 void *talloc_array_ptrtype(const void *ctx, const void *ptr, unsigned count);
1206 #else
1207 #define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
1208 #endif
1209
1210 #ifdef DOXYGEN
1211 /**
1212  * @brief Get the number of elements in a talloc'ed array.
1213  *
1214  * A talloc chunk carries its own size, so for talloc'ed arrays it is not
1215  * necessary to store the number of elements explicitly.
1216  *
1217  * @param[in]  ctx      The allocated array.
1218  *
1219  * @return              The number of elements in ctx.
1220  */
1221 size_t talloc_array_length(const void *ctx);
1222 #else
1223 #define talloc_array_length(ctx) (talloc_get_size(ctx)/sizeof(*ctx))
1224 #endif
1225
1226 #ifdef DOXYGEN
1227 /**
1228  * @brief Allocate a zero-initialized array
1229  *
1230  * @param[in]  ctx      The talloc context to hang the result off.
1231  *
1232  * @param[in]  type     The type that we want to allocate.
1233  *
1234  * @param[in]  count    The number of "type" elements you want to allocate.
1235  *
1236  * @return              The allocated result casted to "type *", NULL on error.
1237  *
1238  * The talloc_zero_array() macro is equivalent to:
1239  *
1240  * @code
1241  *     ptr = talloc_array(ctx, type, count);
1242  *     if (ptr) memset(ptr, 0, sizeof(type) * count);
1243  * @endcode
1244  */
1245 void *talloc_zero_array(const void *ctx, #type, unsigned count);
1246 #else
1247 #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
1248 void *_talloc_zero_array(const void *ctx,
1249                          size_t el_size,
1250                          unsigned count,
1251                          const char *name);
1252 #endif
1253
1254 #ifdef DOXYGEN
1255 /**
1256  * @brief Change the size of a talloc array.
1257  *
1258  * The macro changes the size of a talloc pointer. The 'count' argument is the
1259  * number of elements of type 'type' that you want the resulting pointer to
1260  * hold.
1261  *
1262  * talloc_realloc() has the following equivalences:
1263  *
1264  * @code
1265  *      talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
1266  *      talloc_realloc(ctx, NULL, type, N) ==> talloc_array(ctx, type, N);
1267  *      talloc_realloc(ctx, ptr, type, 0)  ==> talloc_free(ptr);
1268  * @endcode
1269  *
1270  * The "context" argument is only used if "ptr" is NULL, otherwise it is
1271  * ignored.
1272  *
1273  * @param[in]  ctx      The parent context used if ptr is NULL.
1274  *
1275  * @param[in]  ptr      The chunk to be resized.
1276  *
1277  * @param[in]  type     The type of the array element inside ptr.
1278  *
1279  * @param[in]  count    The intended number of array elements.
1280  *
1281  * @return              The new array, NULL on error. The call will fail either
1282  *                      due to a lack of memory, or because the pointer has more
1283  *                      than one parent (see talloc_reference()).
1284  */
1285 void *talloc_realloc(const void *ctx, void *ptr, #type, size_t count);
1286 #else
1287 #define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
1288 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
1289 #endif
1290
1291 #ifdef DOXYGEN
1292 /**
1293  * @brief Untyped realloc to change the size of a talloc array.
1294  *
1295  * The macro is useful when the type is not known so the typesafe
1296  * talloc_realloc() cannot be used.
1297  *
1298  * @param[in]  ctx      The parent context used if 'ptr' is NULL.
1299  *
1300  * @param[in]  ptr      The chunk to be resized.
1301  *
1302  * @param[in]  size     The new chunk size.
1303  *
1304  * @return              The new array, NULL on error.
1305  */
1306 void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);
1307 #else
1308 #define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
1309 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
1310 #endif
1311
1312 /**
1313  * @brief Provide a function version of talloc_realloc_size.
1314  *
1315  * This is a non-macro version of talloc_realloc(), which is useful as
1316  * libraries sometimes want a ralloc function pointer. A realloc()
1317  * implementation encapsulates the functionality of malloc(), free() and
1318  * realloc() in one call, which is why it is useful to be able to pass around
1319  * a single function pointer.
1320  *
1321  * @param[in]  context  The parent context used if ptr is NULL.
1322  *
1323  * @param[in]  ptr      The chunk to be resized.
1324  *
1325  * @param[in]  size     The new chunk size.
1326  *
1327  * @return              The new chunk, NULL on error.
1328  */
1329 void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
1330
1331 /* @} ******************************************************************/
1332
1333 /**
1334  * @defgroup talloc_string The talloc string functions.
1335  * @ingroup talloc
1336  *
1337  * talloc string allocation and manipulation functions.
1338  * @{
1339  */
1340
1341 /**
1342  * @brief Duplicate a string into a talloc chunk.
1343  *
1344  * This function is equivalent to:
1345  *
1346  * @code
1347  *      ptr = talloc_size(ctx, strlen(p)+1);
1348  *      if (ptr) memcpy(ptr, p, strlen(p)+1);
1349  * @endcode
1350  *
1351  * This functions sets the name of the new pointer to the passed
1352  * string. This is equivalent to:
1353  *
1354  * @code
1355  *      talloc_set_name_const(ptr, ptr)
1356  * @endcode
1357  *
1358  * @param[in]  t        The talloc context to hang the result off.
1359  *
1360  * @param[in]  p        The string you want to duplicate.
1361  *
1362  * @return              The duplicated string, NULL on error.
1363  */
1364 char *talloc_strdup(const void *t, const char *p);
1365
1366 /**
1367  * @brief Append a string to given string.
1368  *
1369  * The destination string is reallocated to take
1370  * <code>strlen(s) + strlen(a) + 1</code> characters.
1371  *
1372  * This functions sets the name of the new pointer to the new
1373  * string. This is equivalent to:
1374  *
1375  * @code
1376  *      talloc_set_name_const(ptr, ptr)
1377  * @endcode
1378  *
1379  * If <code>s == NULL</code> then new context is created.
1380  *
1381  * @param[in]  s        The destination to append to.
1382  *
1383  * @param[in]  a        The string you want to append.
1384  *
1385  * @return              The concatenated strings, NULL on error.
1386  *
1387  * @see talloc_strdup()
1388  * @see talloc_strdup_append_buffer()
1389  */
1390 char *talloc_strdup_append(char *s, const char *a);
1391
1392 /**
1393  * @brief Append a string to a given buffer.
1394  *
1395  * This is a more efficient version of talloc_strdup_append(). It determines the
1396  * length of the destination string by the size of the talloc context.
1397  *
1398  * Use this very carefully as it produces a different result than
1399  * talloc_strdup_append() when a zero character is in the middle of the
1400  * destination string.
1401  *
1402  * @code
1403  *      char *str_a = talloc_strdup(NULL, "hello world");
1404  *      char *str_b = talloc_strdup(NULL, "hello world");
1405  *      str_a[5] = str_b[5] = '\0'
1406  *
1407  *      char *app = talloc_strdup_append(str_a, ", hello");
1408  *      char *buf = talloc_strdup_append_buffer(str_b, ", hello");
1409  *
1410  *      printf("%s\n", app); // hello, hello (app = "hello, hello")
1411  *      printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1412  * @endcode
1413  *
1414  * If <code>s == NULL</code> then new context is created.
1415  *
1416  * @param[in]  s        The destination buffer to append to.
1417  *
1418  * @param[in]  a        The string you want to append.
1419  *
1420  * @return              The concatenated strings, NULL on error.
1421  *
1422  * @see talloc_strdup()
1423  * @see talloc_strdup_append()
1424  * @see talloc_array_length()
1425  */
1426 char *talloc_strdup_append_buffer(char *s, const char *a);
1427
1428 /**
1429  * @brief Duplicate a length-limited string into a talloc chunk.
1430  *
1431  * This function is the talloc equivalent of the C library function strndup(3).
1432  *
1433  * This functions sets the name of the new pointer to the passed string. This is
1434  * equivalent to:
1435  *
1436  * @code
1437  *      talloc_set_name_const(ptr, ptr)
1438  * @endcode
1439  *
1440  * @param[in]  t        The talloc context to hang the result off.
1441  *
1442  * @param[in]  p        The string you want to duplicate.
1443  *
1444  * @param[in]  n        The maximum string length to duplicate.
1445  *
1446  * @return              The duplicated string, NULL on error.
1447  */
1448 char *talloc_strndup(const void *t, const char *p, size_t n);
1449
1450 /**
1451  * @brief Append at most n characters of a string to given string.
1452  *
1453  * The destination string is reallocated to take
1454  * <code>strlen(s) + strnlen(a, n) + 1</code> characters.
1455  *
1456  * This functions sets the name of the new pointer to the new
1457  * string. This is equivalent to:
1458  *
1459  * @code
1460  *      talloc_set_name_const(ptr, ptr)
1461  * @endcode
1462  *
1463  * If <code>s == NULL</code> then new context is created.
1464  *
1465  * @param[in]  s        The destination string to append to.
1466  *
1467  * @param[in]  a        The source string you want to append.
1468  *
1469  * @param[in]  n        The number of characters you want to append from the
1470  *                      string.
1471  *
1472  * @return              The concatenated strings, NULL on error.
1473  *
1474  * @see talloc_strndup()
1475  * @see talloc_strndup_append_buffer()
1476  */
1477 char *talloc_strndup_append(char *s, const char *a, size_t n);
1478
1479 /**
1480  * @brief Append at most n characters of a string to given buffer
1481  *
1482  * This is a more efficient version of talloc_strndup_append(). It determines
1483  * the length of the destination string by the size of the talloc context.
1484  *
1485  * Use this very carefully as it produces a different result than
1486  * talloc_strndup_append() when a zero character is in the middle of the
1487  * destination string.
1488  *
1489  * @code
1490  *      char *str_a = talloc_strdup(NULL, "hello world");
1491  *      char *str_b = talloc_strdup(NULL, "hello world");
1492  *      str_a[5] = str_b[5] = '\0'
1493  *
1494  *      char *app = talloc_strndup_append(str_a, ", hello", 7);
1495  *      char *buf = talloc_strndup_append_buffer(str_b, ", hello", 7);
1496  *
1497  *      printf("%s\n", app); // hello, hello (app = "hello, hello")
1498  *      printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1499  * @endcode
1500  *
1501  * If <code>s == NULL</code> then new context is created.
1502  *
1503  * @param[in]  s        The destination buffer to append to.
1504  *
1505  * @param[in]  a        The source string you want to append.
1506  *
1507  * @param[in]  n        The number of characters you want to append from the
1508  *                      string.
1509  *
1510  * @return              The concatenated strings, NULL on error.
1511  *
1512  * @see talloc_strndup()
1513  * @see talloc_strndup_append()
1514  * @see talloc_array_length()
1515  */
1516 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n);
1517
1518 /**
1519  * @brief Format a string given a va_list.
1520  *
1521  * This function is the talloc equivalent of the C library function
1522  * vasprintf(3).
1523  *
1524  * This functions sets the name of the new pointer to the new string. This is
1525  * equivalent to:
1526  *
1527  * @code
1528  *      talloc_set_name_const(ptr, ptr)
1529  * @endcode
1530  *
1531  * @param[in]  t        The talloc context to hang the result off.
1532  *
1533  * @param[in]  fmt      The format string.
1534  *
1535  * @param[in]  ap       The parameters used to fill fmt.
1536  *
1537  * @return              The formatted string, NULL on error.
1538  */
1539 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1540
1541 /**
1542  * @brief Format a string given a va_list and append it to the given destination
1543  *        string.
1544  *
1545  * @param[in]  s        The destination string to append to.
1546  *
1547  * @param[in]  fmt      The format string.
1548  *
1549  * @param[in]  ap       The parameters used to fill fmt.
1550  *
1551  * @return              The formatted string, NULL on error.
1552  *
1553  * @see talloc_vasprintf()
1554  */
1555 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1556
1557 /**
1558  * @brief Format a string given a va_list and append it to the given destination
1559  *        buffer.
1560  *
1561  * @param[in]  s        The destination buffer to append to.
1562  *
1563  * @param[in]  fmt      The format string.
1564  *
1565  * @param[in]  ap       The parameters used to fill fmt.
1566  *
1567  * @return              The formatted string, NULL on error.
1568  *
1569  * @see talloc_vasprintf()
1570  */
1571 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1572
1573 /**
1574  * @brief Format a string.
1575  *
1576  * This function is the talloc equivalent of the C library function asprintf(3).
1577  *
1578  * This functions sets the name of the new pointer to the new string. This is
1579  * equivalent to:
1580  *
1581  * @code
1582  *      talloc_set_name_const(ptr, ptr)
1583  * @endcode
1584  *
1585  * @param[in]  t        The talloc context to hang the result off.
1586  *
1587  * @param[in]  fmt      The format string.
1588  *
1589  * @param[in]  ...      The parameters used to fill fmt.
1590  *
1591  * @return              The formatted string, NULL on error.
1592  */
1593 char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1594
1595 /**
1596  * @brief Append a formatted string to another string.
1597  *
1598  * This function appends the given formatted string to the given string. Use
1599  * this variant when the string in the current talloc buffer may have been
1600  * truncated in length.
1601  *
1602  * This functions sets the name of the new pointer to the new
1603  * string. This is equivalent to:
1604  *
1605  * @code
1606  *      talloc_set_name_const(ptr, ptr)
1607  * @endcode
1608  *
1609  * If <code>s == NULL</code> then new context is created.
1610  *
1611  * @param[in]  s        The string to append to.
1612  *
1613  * @param[in]  fmt      The format string.
1614  *
1615  * @param[in]  ...      The parameters used to fill fmt.
1616  *
1617  * @return              The formatted string, NULL on error.
1618  */
1619 char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1620
1621 /**
1622  * @brief Append a formatted string to another string.
1623  *
1624  * This is a more efficient version of talloc_asprintf_append(). It determines
1625  * the length of the destination string by the size of the talloc context.
1626  *
1627  * Use this very carefully as it produces a different result than
1628  * talloc_asprintf_append() when a zero character is in the middle of the
1629  * destination string.
1630  *
1631  * @code
1632  *      char *str_a = talloc_strdup(NULL, "hello world");
1633  *      char *str_b = talloc_strdup(NULL, "hello world");
1634  *      str_a[5] = str_b[5] = '\0'
1635  *
1636  *      char *app = talloc_asprintf_append(str_a, "%s", ", hello");
1637  *      char *buf = talloc_strdup_append_buffer(str_b, "%s", ", hello");
1638  *
1639  *      printf("%s\n", app); // hello, hello (app = "hello, hello")
1640  *      printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1641  * @endcode
1642  *
1643  * If <code>s == NULL</code> then new context is created.
1644  *
1645  * @param[in]  s        The string to append to
1646  *
1647  * @param[in]  fmt      The format string.
1648  *
1649  * @param[in]  ...      The parameters used to fill fmt.
1650  *
1651  * @return              The formatted string, NULL on error.
1652  *
1653  * @see talloc_asprintf()
1654  * @see talloc_asprintf_append()
1655  */
1656 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1657
1658 /* @} ******************************************************************/
1659
1660 /**
1661  * @defgroup talloc_debug The talloc debugging support functions
1662  * @ingroup talloc
1663  *
1664  * To aid memory debugging, talloc contains routines to inspect the currently
1665  * allocated memory hierarchy.
1666  *
1667  * @{
1668  */
1669
1670 /**
1671  * @brief Walk a complete talloc hierarchy.
1672  *
1673  * This provides a more flexible reports than talloc_report(). It
1674  * will recursively call the callback for the entire tree of memory
1675  * referenced by the pointer. References in the tree are passed with
1676  * is_ref = 1 and the pointer that is referenced.
1677  *
1678  * You can pass NULL for the pointer, in which case a report is
1679  * printed for the top level memory context, but only if
1680  * talloc_enable_leak_report() or talloc_enable_leak_report_full()
1681  * has been called.
1682  *
1683  * The recursion is stopped when depth >= max_depth.
1684  * max_depth = -1 means only stop at leaf nodes.
1685  *
1686  * @param[in]  ptr      The talloc chunk.
1687  *
1688  * @param[in]  depth    Internal parameter to control recursion. Call with 0.
1689  *
1690  * @param[in]  max_depth  Maximum recursion level.
1691  *
1692  * @param[in]  callback  Function to be called on every chunk.
1693  *
1694  * @param[in]  private_data  Private pointer passed to callback.
1695  */
1696 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1697                             void (*callback)(const void *ptr,
1698                                              int depth, int max_depth,
1699                                              int is_ref,
1700                                              void *private_data),
1701                             void *private_data);
1702
1703 /**
1704  * @brief Print a talloc hierarchy.
1705  *
1706  * This provides a more flexible reports than talloc_report(). It
1707  * will let you specify the depth and max_depth.
1708  *
1709  * @param[in]  ptr      The talloc chunk.
1710  *
1711  * @param[in]  depth    Internal parameter to control recursion. Call with 0.
1712  *
1713  * @param[in]  max_depth  Maximum recursion level.
1714  *
1715  * @param[in]  f        The file handle to print to.
1716  */
1717 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
1718
1719 /**
1720  * @brief Print a summary report of all memory used by ptr.
1721  *
1722  * This provides a more detailed report than talloc_report(). It will
1723  * recursively print the entire tree of memory referenced by the
1724  * pointer. References in the tree are shown by giving the name of the
1725  * pointer that is referenced.
1726  *
1727  * You can pass NULL for the pointer, in which case a report is printed
1728  * for the top level memory context, but only if
1729  * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
1730  * been called.
1731  *
1732  * @param[in]  ptr      The talloc chunk.
1733  *
1734  * @param[in]  f        The file handle to print to.
1735  *
1736  * Example:
1737  * @code
1738  *      unsigned int *a, *b;
1739  *      a = talloc(NULL, unsigned int);
1740  *      b = talloc(a, unsigned int);
1741  *      fprintf(stderr, "Dumping memory tree for a:\n");
1742  *      talloc_report_full(a, stderr);
1743  * @endcode
1744  *
1745  * @see talloc_report()
1746  */
1747 void talloc_report_full(const void *ptr, FILE *f);
1748
1749 /**
1750  * @brief Print a summary report of all memory used by ptr.
1751  *
1752  * This function prints a summary report of all memory used by ptr. One line of
1753  * report is printed for each immediate child of ptr, showing the total memory
1754  * and number of blocks used by that child.
1755  *
1756  * You can pass NULL for the pointer, in which case a report is printed
1757  * for the top level memory context, but only if talloc_enable_leak_report()
1758  * or talloc_enable_leak_report_full() has been called.
1759  *
1760  * @param[in]  ptr      The talloc chunk.
1761  *
1762  * @param[in]  f        The file handle to print to.
1763  *
1764  * Example:
1765  * @code
1766  *      unsigned int *a, *b;
1767  *      a = talloc(NULL, unsigned int);
1768  *      b = talloc(a, unsigned int);
1769  *      fprintf(stderr, "Summary of memory tree for a:\n");
1770  *      talloc_report(a, stderr);
1771  * @endcode
1772  *
1773  * @see talloc_report_full()
1774  */
1775 void talloc_report(const void *ptr, FILE *f);
1776
1777 /**
1778  * @brief Enable tracking the use of NULL memory contexts.
1779  *
1780  * This enables tracking of the NULL memory context without enabling leak
1781  * reporting on exit. Useful for when you want to do your own leak
1782  * reporting call via talloc_report_null_full();
1783  */
1784 void talloc_enable_null_tracking(void);
1785
1786 /**
1787  * @brief Enable tracking the use of NULL memory contexts.
1788  *
1789  * This enables tracking of the NULL memory context without enabling leak
1790  * reporting on exit. Useful for when you want to do your own leak
1791  * reporting call via talloc_report_null_full();
1792  */
1793 void talloc_enable_null_tracking_no_autofree(void);
1794
1795 /**
1796  * @brief Disable tracking of the NULL memory context.
1797  *
1798  * This disables tracking of the NULL memory context.
1799  */
1800 void talloc_disable_null_tracking(void);
1801
1802 /**
1803  * @brief Enable leak report when a program exits.
1804  *
1805  * This enables calling of talloc_report(NULL, stderr) when the program
1806  * exits. In Samba4 this is enabled by using the --leak-report command
1807  * line option.
1808  *
1809  * For it to be useful, this function must be called before any other
1810  * talloc function as it establishes a "null context" that acts as the
1811  * top of the tree. If you don't call this function first then passing
1812  * NULL to talloc_report() or talloc_report_full() won't give you the
1813  * full tree printout.
1814  *
1815  * Here is a typical talloc report:
1816  *
1817  * @code
1818  * talloc report on 'null_context' (total 267 bytes in 15 blocks)
1819  *      libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
1820  *      libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
1821  *      iconv(UTF8,CP850)              contains     42 bytes in   2 blocks
1822  *      libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
1823  *      iconv(CP850,UTF8)              contains     42 bytes in   2 blocks
1824  *      iconv(UTF8,UTF-16LE)           contains     45 bytes in   2 blocks
1825  *      iconv(UTF-16LE,UTF8)           contains     45 bytes in   2 blocks
1826  * @endcode
1827  */
1828 void talloc_enable_leak_report(void);
1829
1830 /**
1831  * @brief Enable full leak report when a program exits.
1832  *
1833  * This enables calling of talloc_report_full(NULL, stderr) when the
1834  * program exits. In Samba4 this is enabled by using the
1835  * --leak-report-full command line option.
1836  *
1837  * For it to be useful, this function must be called before any other
1838  * talloc function as it establishes a "null context" that acts as the
1839  * top of the tree. If you don't call this function first then passing
1840  * NULL to talloc_report() or talloc_report_full() won't give you the
1841  * full tree printout.
1842  *
1843  * Here is a typical full report:
1844  *
1845  * @code
1846  * full talloc report on 'root' (total 18 bytes in 8 blocks)
1847  *      p1                             contains     18 bytes in   7 blocks (ref 0)
1848  *      r1                             contains     13 bytes in   2 blocks (ref 0)
1849  *      reference to: p2
1850  *      p2                             contains      1 bytes in   1 blocks (ref 1)
1851  *      x3                             contains      1 bytes in   1 blocks (ref 0)
1852  *      x2                             contains      1 bytes in   1 blocks (ref 0)
1853  *      x1                             contains      1 bytes in   1 blocks (ref 0)
1854  * @endcode
1855  */
1856 void talloc_enable_leak_report_full(void);
1857
1858 /**
1859  * @brief Set a custom "abort" function that is called on serious error.
1860  *
1861  * The default "abort" function is <code>abort()</code>.
1862  *
1863  * The "abort" function is called when:
1864  *
1865  * <ul>
1866  *  <li>talloc_get_type_abort() fails</li>
1867  *  <li>the provided pointer is not a valid talloc context</li>
1868  *  <li>when the context meta data are invalid</li>
1869  *  <li>when access after free is detected</li>
1870  * </ul>
1871  *
1872  * Example:
1873  *
1874  * @code
1875  * void my_abort(const char *reason)
1876  * {
1877  *      fprintf(stderr, "talloc abort: %s\n", reason);
1878  *      abort();
1879  * }
1880  *
1881  *      talloc_set_abort_fn(my_abort);
1882  * @endcode
1883  *
1884  * @param[in]  abort_fn      The new "abort" function.
1885  *
1886  * @see talloc_set_log_fn()
1887  * @see talloc_get_type()
1888  */
1889 void talloc_set_abort_fn(void (*abort_fn)(const char *reason));
1890
1891 /**
1892  * @brief Set a logging function.
1893  *
1894  * @param[in]  log_fn      The logging function.
1895  *
1896  * @see talloc_set_log_stderr()
1897  * @see talloc_set_abort_fn()
1898  */
1899 void talloc_set_log_fn(void (*log_fn)(const char *message));
1900
1901 /**
1902  * @brief Set stderr as the output for logs.
1903  *
1904  * @see talloc_set_log_fn()
1905  * @see talloc_set_abort_fn()
1906  */
1907 void talloc_set_log_stderr(void);
1908
1909 /**
1910  * @brief Set a max memory limit for the current context hierarchy
1911  *        This affects all children of this context and constrain any
1912  *        allocation in the hierarchy to never exceed the limit set.
1913  *        The limit can be removed by setting 0 (unlimited) as the
1914  *        max_size by calling the function again on the same context.
1915  *        Memory limits can also be nested, meaning a child can have
1916  *        a stricter memory limit than a parent.
1917  *        Memory limits are enforced only at memory allocation time.
1918  *        Stealing a context into a 'limited' hierarchy properly
1919  *        updates memory usage but does *not* cause failure if the
1920  *        move causes the new parent to exceed its limits. However
1921  *        any further allocation on that hierarchy will then fail.
1922  *
1923  * @warning talloc memlimit functionality is deprecated. Please
1924  *          consider using cgroup memory limits instead.
1925  *
1926  * @param[in]   ctx             The talloc context to set the limit on
1927  * @param[in]   max_size        The (new) max_size
1928  */
1929 int talloc_set_memlimit(const void *ctx, size_t max_size) _DEPRECATED_;
1930
1931 /* @} ******************************************************************/
1932
1933 #if TALLOC_DEPRECATED
1934 #define talloc_zero_p(ctx, type) talloc_zero(ctx, type)
1935 #define talloc_p(ctx, type) talloc(ctx, type)
1936 #define talloc_array_p(ctx, type, count) talloc_array(ctx, type, count)
1937 #define talloc_realloc_p(ctx, p, type, count) talloc_realloc(ctx, p, type, count)
1938 #define talloc_destroy(ctx) talloc_free(ctx)
1939 #define talloc_append_string(c, s, a) (s?talloc_strdup_append(s,a):talloc_strdup(c, a))
1940 #endif
1941
1942 #ifndef TALLOC_MAX_DEPTH
1943 #define TALLOC_MAX_DEPTH 10000
1944 #endif
1945
1946 #ifdef __cplusplus
1947 } /* end of extern "C" */
1948 #endif
1949
1950 #endif