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