Document talloc_parent, talloc_parent_name and talloc_is_parent
[mdw/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 /** \mainpage
33  *
34  * \section intro_sec Introduction
35  *
36  * Talloc is a hierarchical, reference counted memory pool system with
37  * destructors. Quite a mouthful really, but not too bad once you get used to
38  * it.
39  *
40  * Perhaps the biggest difference from other memory pool systems is that there
41  * is no distinction between a "talloc context" and a "talloc pointer". Any
42  * pointer returned from talloc() is itself a valid talloc context. This means
43  * you can do this:
44  *
45  * \code
46  * struct foo *X = talloc(mem_ctx, struct foo);
47  * X->name = talloc_strdup(X, "foo");
48  * \endcode
49  *
50  * and the pointer X->name would be a "child" of the talloc context "X" which
51  * is itself a child of mem_ctx. So if you do talloc_free(mem_ctx) then it is
52  * all destroyed, whereas if you do talloc_free(X) then just X and X->name are
53  * destroyed, and if you do talloc_free(X->name) then just the name element of
54  * X is destroyed.
55  *
56  * If you think about this, then what this effectively gives you is an n-ary
57  * tree, where you can free any part of the tree with talloc_free().
58  *
59  * To start, you should probably first look at the definitions of
60  * ::TALLOC_CTX, talloc_init(), talloc() and talloc_free().
61  *
62  * \section named_blocks Named blocks
63  *
64  * Every talloc chunk has a name that can be used as a dynamic type-checking
65  * system. If for some reason like a callback function you had to cast a
66  * "struct foo *" to a "void *" variable, later you can safely reassign the
67  * "void *" pointer to a "struct foo *" by using the talloc_get_type() or
68  * talloc_get_type_abort() macros.
69  *
70  * \code
71  * struct foo *X = talloc_get_type_abort(ptr, struct foo);
72  * \endcode
73  *
74  * This will abort if "ptr" does not contain a pointer that has been created
75  * with talloc(mem_ctx, struct foo).
76  *
77  * \section multi_threading Multi-Threading
78  *
79  * talloc itself does not deal with threads. It is thread-safe (assuming the
80  * underlying "malloc" is), as long as each thread uses different memory
81  * contexts.
82  *
83  * If two threads uses the same context then they need to synchronize in order
84  * to be safe. In particular:
85  *
86  *
87  * - when using talloc_enable_leak_report(), giving directly NULL as a
88  *   parent context implicitly refers to a hidden "null context" global
89  *   variable, so this should not be used in a multi-threaded environment
90  *   without proper synchronization
91  * - the context returned by talloc_autofree_context() is also global so
92  *   shouldn't be used by several threads simultaneously without
93  *   synchronization.
94  */
95
96 /** \defgroup talloc_basic Basic Talloc Routines
97  *
98  * This module contains the basic talloc routines that are used in everyday
99  * programming.
100  */
101
102 /**
103  * \defgroup talloc_ref Talloc References
104  *
105  * This module contains the definitions around talloc references
106  */
107
108 /**
109  * \defgroup talloc_array Array routines
110  *
111  * Talloc contains some handy helpers for handling Arrays conveniently
112  */
113
114 /**
115  * \defgroup talloc_string String handling routines
116  *
117  * Talloc contains some handy string handling functions
118  */
119
120 /**
121  * \defgroup talloc_debug Debugging support routines
122  *
123  * To aid memory debugging, talloc contains routines to inspect the currently
124  * allocated memory hierarchy.
125  */
126
127 /**
128  * \defgroup talloc_internal Internal routines
129  *
130  * To achieve type-safety, talloc.h defines a lot of macros with type
131  * casts. These macros define the user interface to the internal routines you
132  * find here. You should not really use these routines directly but go through
133  * the external API.
134  */
135
136 /**
137  * \defgroup talloc_undoc Default group of undocumented stuff
138  *
139  * This should be empty...
140  */
141
142 /*\{*/
143
144 /**
145  * \typedef TALLOC_CTX
146  * \brief Define a talloc parent type
147  * \ingroup talloc_basic
148  *
149  * As talloc is a hierarchial memory allocator, every talloc chunk is a
150  * potential parent to other talloc chunks. So defining a separate type for a
151  * talloc chunk is not strictly necessary. TALLOC_CTX is defined nevertheless,
152  * as it provides an indicator for function arguments. You will frequently
153  * write code like
154  *
155  * \code
156  * struct foo *foo_create(TALLOC_CTX *mem_ctx)
157  * {
158  *      struct foo *result;
159  *      result = talloc(mem_ctx, struct foo);
160  *      if (result == NULL) return NULL;
161  *      ... initialize foo ...
162  *      return result;
163  * }
164  * \endcode
165  *
166  * In this type of allocating functions it is handy to have a general
167  * TALLOC_CTX type to indicate which parent to put allocated structures on.
168  */
169 typedef void TALLOC_CTX;
170
171 /*
172   this uses a little trick to allow __LINE__ to be stringified
173 */
174 #ifndef __location__
175 #define __TALLOC_STRING_LINE1__(s)    #s
176 #define __TALLOC_STRING_LINE2__(s)   __TALLOC_STRING_LINE1__(s)
177 #define __TALLOC_STRING_LINE3__  __TALLOC_STRING_LINE2__(__LINE__)
178 #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__
179 #endif
180
181 #ifndef TALLOC_DEPRECATED
182 #define TALLOC_DEPRECATED 0
183 #endif
184
185 #ifndef PRINTF_ATTRIBUTE
186 #if (__GNUC__ >= 3)
187 /** Use gcc attribute to check printf fns.  a1 is the 1-based index of
188  * the parameter containing the format, and a2 the index of the first
189  * argument. Note that some gcc 2.x versions don't handle this
190  * properly **/
191 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
192 #else
193 #define PRINTF_ATTRIBUTE(a1, a2)
194 #endif
195 #endif
196
197 /**
198  * \def talloc_set_destructor
199  * \brief Assign a function to be called when a chunk is freed
200  * \param ptr The talloc chunk to add a destructor to
201  * \param function The destructor function to be called
202  * \ingroup talloc_basic
203  *
204  * The function talloc_set_destructor() sets the "destructor" for the pointer
205  * "ptr". A destructor is a function that is called when the memory used by a
206  * pointer is about to be released. The destructor receives the pointer as an
207  * argument, and should return 0 for success and -1 for failure.
208  *
209  * The destructor can do anything it wants to, including freeing other pieces
210  * of memory. A common use for destructors is to clean up operating system
211  * resources (such as open file descriptors) contained in the structure the
212  * destructor is placed on.
213  *
214  * You can only place one destructor on a pointer. If you need more than one
215  * destructor then you can create a zero-length child of the pointer and place
216  * an additional destructor on that.
217  *
218  * To remove a destructor call talloc_set_destructor() with NULL for the
219  * destructor.
220  *
221  * If your destructor attempts to talloc_free() the pointer that it is the
222  * destructor for then talloc_free() will return -1 and the free will be
223  * ignored. This would be a pointless operation anyway, as the destructor is
224  * only called when the memory is just about to go away.
225  */
226
227 /**
228  * \def talloc_steal(ctx, ptr)
229  * \brief Change a talloc chunk's parent
230  * \param ctx The new parent context
231  * \param ptr The talloc chunk to move
232  * \return ptr
233  * \ingroup talloc_basic
234  *
235  * The talloc_steal() function changes the parent context of a talloc
236  * pointer. It is typically used when the context that the pointer is
237  * currently a child of is going to be freed and you wish to keep the
238  * memory for a longer time.
239  *
240  * The talloc_steal() function returns the pointer that you pass it. It
241  * does not have any failure modes.
242  *
243  * NOTE: It is possible to produce loops in the parent/child relationship
244  * if you are not careful with talloc_steal(). No guarantees are provided
245  * as to your sanity or the safety of your data if you do this.
246  *
247  * To make the changed hierarchy less error-prone, you might consider to use
248  * talloc_move().
249  *
250  * talloc_steal (ctx, NULL) will return NULL with no sideeffects.
251  */
252
253 /* try to make talloc_set_destructor() and talloc_steal() type safe,
254    if we have a recent gcc */
255 #if (__GNUC__ >= 3)
256 #define _TALLOC_TYPEOF(ptr) __typeof__(ptr)
257 #define talloc_set_destructor(ptr, function)                                  \
258         do {                                                                  \
259                 int (*_talloc_destructor_fn)(_TALLOC_TYPEOF(ptr)) = (function);       \
260                 _talloc_set_destructor((ptr), (int (*)(void *))_talloc_destructor_fn); \
261         } while(0)
262 /* this extremely strange macro is to avoid some braindamaged warning
263    stupidity in gcc 4.1.x */
264 #define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr)); __talloc_steal_ret; })
265 #else
266 #define talloc_set_destructor(ptr, function) \
267         _talloc_set_destructor((ptr), (int (*)(void *))(function))
268 #define _TALLOC_TYPEOF(ptr) void *
269 #define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr))
270 #endif
271
272 /**
273  * \def talloc_reference(ctx, ptr)
274  * \brief Create an additional talloc parent to a pointer
275  * \param ctx The additional parent
276  * \param ptr The pointer you want to create an additional parent for
277  * \return ptr
278  * \ingroup talloc_ref
279  *
280  * The talloc_reference() function makes "context" an additional parent of
281  * "ptr".
282  *
283  * The return value of talloc_reference() is always the original pointer
284  * "ptr", unless talloc ran out of memory in creating the reference in which
285  * case it will return NULL (each additional reference consumes around 48
286  * bytes of memory on intel x86 platforms).
287  *
288  * If "ptr" is NULL, then the function is a no-op, and simply returns NULL.
289  *
290  * After creating a reference you can free it in one of the following ways:
291  *
292  * - you can talloc_free() any parent of the original pointer. That
293  *   will reduce the number of parents of this pointer by 1, and will
294  *   cause this pointer to be freed if it runs out of parents.
295  *
296  * - you can talloc_free() the pointer itself. That will destroy the
297  *   most recently established parent to the pointer and leave the
298  *   pointer as a child of its current parent.
299  *
300  * For more control on which parent to remove, see talloc_unlink()
301  */
302 #define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference((ctx),(ptr))
303
304
305 /**
306  * \def talloc_move(ctx, ptr)
307  * \brief Change a talloc chunk's parent
308  * \param ctx The new parent context
309  * \param ptr Pointer to the talloc chunk to move
310  * \return ptr
311  * \ingroup talloc_basic
312  *
313  * talloc_move() has the same effect as talloc_steal(), and additionally sets
314  * the source pointer to NULL. You would use it like this:
315  *
316  * \code
317  * struct foo *X = talloc(tmp_ctx, struct foo);
318  * struct foo *Y;
319  * Y = talloc_move(new_ctx, &X);
320  * \endcode
321  */
322 #define talloc_move(ctx, ptr) (_TALLOC_TYPEOF(*(ptr)))_talloc_move((ctx),(void *)(ptr))
323
324 /* useful macros for creating type checked pointers */
325
326 /**
327  * \def talloc(ctx, type)
328  * \brief Main entry point to allocate structures
329  * \param ctx The talloc context to hang the result off
330  * \param type The type that we want to allocate
331  * \return Pointer to a piece of memory, properly cast to "type *"
332  * \ingroup talloc_basic
333  *
334  * The talloc() macro is the core of the talloc library. It takes a memory
335  * context and a type, and returns a pointer to a new area of memory of the
336  * given type.
337  *
338  * The returned pointer is itself a talloc context, so you can use it as the
339  * context argument to more calls to talloc if you wish.
340  *
341  * The returned pointer is a "child" of the supplied context. This means that
342  * if you talloc_free() the context then the new child disappears as
343  * well. Alternatively you can free just the child.
344  *
345  * The context argument to talloc() can be NULL, in which case a new top
346  * level context is created.
347  */
348 #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
349
350 /**
351  * \def talloc_size(ctx, size)
352  * \brief Untyped allocation
353  * \param ctx The talloc context to hang the result off
354  * \param size Number of char's that you want to allocate
355  * \return The allocated memory chunk
356  * \ingroup talloc_basic
357  *
358  * The function talloc_size() should be used when you don't have a convenient
359  * type to pass to talloc(). Unlike talloc(), it is not type safe (as it
360  * returns a void *), so you are on your own for type checking.
361  */
362 #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
363
364 /**
365  * \def talloc_ptrtype(ctx, ptr)
366  * \brief Allocate into a typed pointer
367  * \param ctx The talloc context to hang the result off
368  * \param ptr The pointer you want to assign the result to
369  * \result The allocated memory chunk, properly cast
370  * \ingroup talloc_basic
371  *
372  * The talloc_ptrtype() macro should be used when you have a pointer and
373  * want to allocate memory to point at with this pointer. When compiling
374  * with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()
375  * and talloc_get_name() will return the current location in the source file.
376  * and not the type.
377  */
378 #define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
379
380 /**
381  * \def talloc_new(ctx)
382  * \brief Allocate a new 0-sized talloc chunk
383  * \param ctx The talloc parent context
384  * \return A new talloc chunk
385  * \ingroup talloc_basic
386  *
387  * This is a utility macro that creates a new memory context hanging off an
388  * exiting context, automatically naming it "talloc_new: __location__" where
389  * __location__ is the source line it is called from. It is particularly
390  * useful for creating a new temporary working context.
391  */
392 #define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
393
394 /**
395  * \def talloc_zero(ctx, type)
396  * \brief Allocate a 0-initizialized structure
397  * \param ctx The talloc context to hang the result off
398  * \param type The type that we want to allocate
399  * \return Pointer to a piece of memory, properly cast to "type *"
400  * \ingroup talloc_basic
401  *
402  * The talloc_zero() macro is equivalent to:
403  *
404  * \code
405  * ptr = talloc(ctx, type);
406  * if (ptr) memset(ptr, 0, sizeof(type));
407  * \endcode
408  */
409 #define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
410
411 /**
412  * \def talloc_zero_size(ctx, size)
413  * \brief Untyped, 0-initialized allocation
414  * \param ctx The talloc context to hang the result off
415  * \param size Number of char's that you want to allocate
416  * \return The allocated memory chunk
417  * \ingroup talloc_basic
418  *
419  * The talloc_zero_size() macro is equivalent to:
420  *
421  * \code
422  * ptr = talloc_size(ctx, size);
423  * if (ptr) memset(ptr, 0, size);
424  * \endcode
425  */
426
427 #define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
428
429 #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
430
431 /**
432  * \def talloc_array(ctx, type, count)
433  * \brief Allocate an array
434  * \param ctx The talloc context to hang the result off
435  * \param type The type that we want to allocate
436  * \param count The number of "type" elements you want to allocate
437  * \return The allocated result, properly cast to "type *"
438  * \ingroup talloc_array
439  *
440  * The talloc_array() macro is equivalent to::
441  *
442  * \code
443  * (type *)talloc_size(ctx, sizeof(type) * count);
444  * \endcode
445  *
446  * except that it provides integer overflow protection for the multiply,
447  * returning NULL if the multiply overflows.
448  */
449 #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
450
451 /**
452  * \def talloc_array_size(ctx, size, count)
453  * \brief Allocate an array
454  * \param ctx The talloc context to hang the result off
455  * \param size The size of an array element
456  * \param count The number of "type" elements you want to allocate
457  * \return The allocated result, properly cast to "type *"
458  * \ingroup talloc_array
459  *
460  * The talloc_array_size() function is useful when the type is not
461  * known. It operates in the same way as talloc_array(), but takes a size
462  * instead of a type.
463  */
464 #define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
465
466 /**
467  * \def talloc_array_ptrtype(ctx, ptr, count)
468  * \brief Allocate an array into a typed pointer
469  * \param ctx The talloc context to hang the result off
470  * \param ptr The pointer you want to assign the result to
471  * \param count The number of elements you want to allocate
472  * \result The allocated memory chunk, properly cast
473  * \ingroup talloc_array
474  *
475  * The talloc_array_ptrtype() macro should be used when you have a pointer to
476  * an array and want to allocate memory of an array to point at with this
477  * pointer. When compiling with gcc >= 3 it is typesafe. Note this is a
478  * wrapper of talloc_array_size() and talloc_get_name() will return the
479  * current location in the source file.  and not the type.
480  */
481 #define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
482
483 /**
484  * \def talloc_array_length(ctx)
485  * \brief Return the number of elements in a talloc'ed array
486  * \param ctx The talloc'ed array
487  * \return The number of elements in ctx
488  * \ingroup talloc_array
489  *
490  * A talloc chunk carries its own size, so for talloc'ed arrays it is not
491  * necessary to store the number of elements explicitly.
492  */
493 #define talloc_array_length(ctx) ((ctx) ? talloc_get_size(ctx)/sizeof(*ctx) : 0)
494
495 /**
496  * \def talloc_realloc(ctx, p, type, count)
497  * \brief Change the size of a talloc array
498  * \param ctx The parent context used if "p" is NULL
499  * \param p The chunk to be resized
500  * \param type The type of the array element inside p
501  * \param count The intended number of array elements
502  * \return The new array
503  * \ingroup talloc_array
504  *
505  * The talloc_realloc() macro changes the size of a talloc
506  * pointer. The "count" argument is the number of elements of type "type"
507  * that you want the resulting pointer to hold.
508  *
509  * talloc_realloc() has the following equivalences::
510  *
511  * \code
512  * talloc_realloc(context, NULL, type, 1) ==> talloc(context, type);
513  * talloc_realloc(context, NULL, type, N) ==> talloc_array(context, type, N);
514  * talloc_realloc(context, ptr, type, 0)  ==> talloc_free(ptr);
515  * \endcode
516  *
517  * The "context" argument is only used if "ptr" is NULL, otherwise it is
518  * ignored.
519  *
520  * talloc_realloc() returns the new pointer, or NULL on failure. The call
521  * will fail either due to a lack of memory, or because the pointer has
522  * more than one parent (see talloc_reference()).
523  */
524 #define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
525
526 /**
527  * \def talloc_realloc_size(ctx, ptr, size)
528  * \brief Untyped realloc
529  * \param ctx The parent context used if "ptr" is NULL
530  * \param ptr The chunk to be resized
531  * \param size The new chunk size
532  * \return The new chunk
533  * \ingroup talloc_array
534  *
535  * The talloc_realloc_size() function is useful when the type is not known so
536  * the typesafe talloc_realloc() cannot be used.
537  */
538 #define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
539
540 /**
541  * \def talloc_memdup(t, p, size)
542  * \brief Duplicate a memory area into a talloc chunk
543  * \param t The talloc context to hang the result off
544  * \param p The memory chunk you want to duplicate
545  * \param size Number of char's that you want copy
546  * \return The allocated memory chunk
547  * \ingroup talloc_basic
548  *
549  * The talloc_memdup() function is equivalent to::
550  *
551  * \code
552  * ptr = talloc_size(ctx, size);
553  * if (ptr) memcpy(ptr, p, size);
554  * \endcode
555  */
556 #define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
557
558 /**
559  * \def talloc_set_type(ptr, type)
560  * \brief Assign a type to a talloc chunk
561  * \param ptr The talloc chunk to assign the type to
562  * \param type The type to assign
563  * \ingroup talloc_basic
564  *
565  * This macro allows you to force the name of a pointer to be a
566  * particular type. This can be used in conjunction with
567  * talloc_get_type() to do type checking on void* pointers.
568  *
569  * It is equivalent to this::
570  *
571  * \code
572  * talloc_set_name_const(ptr, #type)
573  * \endcode
574  */
575 #define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
576
577 /**
578  * \def talloc_get_type(ptr, type)
579  * \brief Get a typed pointer out of a talloc pointer
580  * \param ptr The talloc pointer to check
581  * \param type The type to check against
582  * \return ptr, properly cast, or NULL
583  * \ingroup talloc_basic
584  *
585  * This macro allows you to do type checking on talloc pointers. It is
586  * particularly useful for void* private pointers. It is equivalent to
587  * this:
588  *
589  * \code
590  * (type *)talloc_check_name(ptr, #type)
591  * \endcode
592  */
593
594 #define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
595
596 /**
597  * \def talloc_get_type_abort(ptr, type)
598  * \brief Helper macro to safely turn a void * into a typed pointer
599  * \param ptr The void * to convert
600  * \param type The type that this chunk contains
601  * \return Same value as ptr, type-checked and properly cast
602  * \ingroup talloc_basic
603  *
604  * This macro is used together with talloc(mem_ctx, struct foo). If you had to
605  * assing the talloc chunk pointer to some void * variable,
606  * talloc_get_type_abort() is the recommended way to get the convert the void
607  * pointer back to a typed pointer.
608  */
609 #define talloc_get_type_abort(ptr, type) (type *)_talloc_get_type_abort(ptr, #type, __location__)
610
611 /**
612  * \def talloc_find_parent_bytype(ptr, type)
613  * \brief Find a parent context by type
614  * \param ptr The talloc chunk to start from
615  * \param type The type of the parent to look for
616  * \ingroup talloc_basic
617  *
618  * Find a parent memory context of the current context that has the given
619  * name. This can be very useful in complex programs where it may be
620  * difficult to pass all information down to the level you need, but you
621  * know the structure you want is a parent of another context.
622  *
623  * Like talloc_find_parent_byname() but takes a type, making it typesafe.
624  */
625 #define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
626
627 #if TALLOC_DEPRECATED
628 #define talloc_zero_p(ctx, type) talloc_zero(ctx, type)
629 #define talloc_p(ctx, type) talloc(ctx, type)
630 #define talloc_array_p(ctx, type, count) talloc_array(ctx, type, count)
631 #define talloc_realloc_p(ctx, p, type, count) talloc_realloc(ctx, p, type, count)
632 #define talloc_destroy(ctx) talloc_free(ctx)
633 #define talloc_append_string(c, s, a) (s?talloc_strdup_append(s,a):talloc_strdup(c, a))
634 #endif
635
636 /**
637  * \def TALLOC_FREE(ctx)
638  * \brief talloc_free a chunk and NULL out the pointer
639  * \param ctx The chunk to be freed
640  * \ingroup talloc_basic
641  *
642  * TALLOC_FREE() frees a pointer and sets it to NULL. Use this if you want
643  * immediate feedback (i.e. crash) if you use a pointer after having free'ed
644  * it.
645  */
646 #define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
647
648 /**
649  * \brief Allocate untyped, unnamed memory
650  * \param context The talloc context to hang the result off
651  * \param size Number of char's that you want to allocate
652  * \return The allocated memory chunk
653  * \ingroup talloc_internal
654  *
655  * Essentially the same as talloc_size() without setting the chunk name to the
656  * current file/line number.
657  */
658 void *_talloc(const void *context, size_t size);
659
660 /**
661  * \brief Allocate a talloc pool
662  * \param context The talloc context to hang the result off
663  * \param size Size of the talloc pool
664  * \result The talloc pool
665  * \ingroup talloc_basic
666  *
667  * A talloc pool is a pure optimization for specific situations. In the
668  * release process for Samba 3.2 we found out that we had become considerably
669  * slower than Samba 3.0 was. Profiling showed that malloc(3) was a large CPU
670  * consumer in benchmarks. For Samba 3.2 we have internally converted many
671  * static buffers to dynamically allocated ones, so malloc(3) being beaten
672  * more was no surprise. But it made us slower.
673  *
674  * talloc_pool() is an optimization to call malloc(3) a lot less for the use
675  * pattern Samba has: The SMB protocol is mainly a request/response protocol
676  * where we have to allocate a certain amount of memory per request and free
677  * that after the SMB reply is sent to the client.
678  *
679  * talloc_pool() creates a talloc chunk that you can use as a talloc parent
680  * exactly as you would use any other ::TALLOC_CTX. The difference is that
681  * when you talloc a child of this pool, no malloc(3) is done. Instead, talloc
682  * just increments a pointer inside the talloc_pool. This also works
683  * recursively. If you use the child of the talloc pool as a parent for
684  * grand-children, their memory is also taken from the talloc pool.
685  *
686  * If you talloc_free() children of a talloc pool, the memory is not given
687  * back to the system. Instead, free(3) is only called if the talloc_pool()
688  * itself is released with talloc_free().
689  *
690  * The downside of a talloc pool is that if you talloc_move() a child of a
691  * talloc pool to a talloc parent outside the pool, the whole pool memory is
692  * not free(3)'ed until that moved chunk is also talloc_free()ed.
693  */
694 void *talloc_pool(const void *context, size_t size);
695 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *));
696
697 /**
698  * \brief Increase the reference count of a talloc chunk
699  * \param ptr
700  * \return success?
701  * \ingroup talloc_ref
702  *
703  * The talloc_increase_ref_count(ptr) function is exactly equivalent to:
704  *
705  * \code
706  * talloc_reference(NULL, ptr);
707  * \endcode
708  *
709  * You can use either syntax, depending on which you think is clearer in
710  * your code.
711  *
712  * It returns 0 on success and -1 on failure.
713  */
714 int talloc_increase_ref_count(const void *ptr);
715
716 /**
717  * \brief Return the number of references to a talloc chunk
718  * \param ptr The chunk you are interested in
719  * \return Number of refs
720  * \ingroup talloc_ref
721  */
722 size_t talloc_reference_count(const void *ptr);
723 void *_talloc_reference(const void *context, const void *ptr);
724
725 /**
726  * \brief Remove a specific parent from a talloc chunk
727  * \param context The talloc parent to remove
728  * \param ptr The talloc ptr you want to remove the parent from
729  * \ingroup talloc_ref
730  *
731  * The talloc_unlink() function removes a specific parent from ptr. The
732  * context passed must either be a context used in talloc_reference() with
733  * this pointer, or must be a direct parent of ptr.
734  *
735  * Note that if the parent has already been removed using talloc_free() then
736  * this function will fail and will return -1.  Likewise, if "ptr" is NULL,
737  * then the function will make no modifications and return -1.
738  *
739  * Usually you can just use talloc_free() instead of talloc_unlink(), but
740  * sometimes it is useful to have the additional control on which parent is
741  * removed.
742  */
743 int talloc_unlink(const void *context, void *ptr);
744
745 /**
746  * \brief Assign a name to a talloc chunk
747  * \param ptr The talloc chunk to assign a name to
748  * \param fmt Format string for the name
749  * \param ... printf-style additional arguments
750  * \return The assigned name
751  * \ingroup talloc_basic
752  *
753  * Each talloc pointer has a "name". The name is used principally for
754  * debugging purposes, although it is also possible to set and get the name on
755  * a pointer in as a way of "marking" pointers in your code.
756  *
757  * The main use for names on pointer is for "talloc reports". See
758  * talloc_report() and talloc_report_full() for details. Also see
759  * talloc_enable_leak_report() and talloc_enable_leak_report_full().
760  *
761  * The talloc_set_name() function allocates memory as a child of the
762  * pointer. It is logically equivalent to:
763  *
764  * \code
765  * talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
766  * \endcode
767  *
768  * Note that multiple calls to talloc_set_name() will allocate more memory
769  * without releasing the name. All of the memory is released when the ptr is
770  * freed using talloc_free().
771  */
772 const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
773
774 /**
775  * \brief Assign a name to a talloc chunk
776  * \param ptr The talloc chunk to assign a name to
777  * \param name Format string for the name
778  * \ingroup talloc_basic
779  *
780  * The function talloc_set_name_const() is just like talloc_set_name(), but it
781  * takes a string constant, and is much faster. It is extensively used by the
782  * "auto naming" macros, such as talloc_p().
783  *
784  * This function does not allocate any memory. It just copies the supplied
785  * pointer into the internal representation of the talloc ptr. This means you
786  * must not pass a name pointer to memory that will disappear before the ptr
787  * is freed with talloc_free().
788  */
789 void talloc_set_name_const(const void *ptr, const char *name);
790
791 /**
792  * \brief Create a named talloc chunk
793  * \param context The talloc context to hang the result off
794  * \param size Number of char's that you want to allocate
795  * \param fmt Format string for the name
796  * \param ... printf-style additional arguments
797  * \return The allocated memory chunk
798  * \ingroup talloc_basic
799  *
800  * The talloc_named() function creates a named talloc pointer. It is
801  * equivalent to:
802  *
803  * \code
804  * ptr = talloc_size(context, size);
805  * talloc_set_name(ptr, fmt, ....);
806  * \endcode
807  *
808  */
809 void *talloc_named(const void *context, size_t size, 
810                    const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
811
812 /**
813  * \brief Basic routine to allocate a chunk of memory
814  * \param context The parent context
815  * \param size The number of char's that we want to allocate
816  * \param name The name the talloc block has
817  * \return The allocated chunk
818  * \ingroup talloc_basic
819  *
820  * This is equivalent to:
821  *
822  * \code
823  * ptr = talloc_size(context, size);
824  * talloc_set_name_const(ptr, name);
825  * \endcode
826  */
827 void *talloc_named_const(const void *context, size_t size, const char *name);
828
829 /**
830  * \brief Return the name of a talloc chunk
831  * \param ptr The talloc chunk
832  * \return The name
833  * \ingroup talloc_basic
834  *
835  * This returns the current name for the given talloc pointer. See
836  * talloc_set_name() for details.
837  */
838 const char *talloc_get_name(const void *ptr);
839
840 /**
841  * \brief Verify that a talloc chunk carries a specified name
842  * \param ptr The talloc chunk to check
843  * \param name The name to check agains
844  * \ingroup talloc_basic
845  *
846  * This function checks if a pointer has the specified name. If it does
847  * then the pointer is returned. It it doesn't then NULL is returned.
848  */
849 void *talloc_check_name(const void *ptr, const char *name);
850
851 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location);
852
853 /**
854  * \brief Return the parent chunk of a pointer
855  * \param ptr The talloc pointer to inspect
856  * \return The talloc parent of "ptr"
857  * \ingroup talloc_basic
858  *
859  * Return the parent chunk of a pointer
860  */
861 void *talloc_parent(const void *ptr);
862
863 /**
864  * \brief Return a talloc chunk's parent name
865  * \param ptr The talloc pointer to inspect
866  * \return The name of ptr's parent chunk
867  * \ingroup talloc_basic
868  *
869  * Return a talloc chunk's parent name
870  */
871 const char *talloc_parent_name(const void *ptr);
872
873 /**
874  * \brief Create a new top level talloc context
875  * \param fmt Format string for the name
876  * \param ... printf-style additional arguments
877  * \return The allocated memory chunk
878  * \ingroup talloc_basic
879  *
880  * This function creates a zero length named talloc context as a top level
881  * context. It is equivalent to:
882  *
883  * \code
884  *   talloc_named(NULL, 0, fmt, ...);
885  * \endcode
886  */
887 void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
888
889 /**
890  * \brief Free a chunk of talloc memory
891  * \param ptr The chunk to be freed
892  * \return success?
893  * \ingroup talloc_basic
894  *
895  * The talloc_free() function frees a piece of talloc memory, and all its
896  * children. You can call talloc_free() on any pointer returned by talloc().
897  *
898  * The return value of talloc_free() indicates success or failure, with 0
899  * returned for success and -1 for failure. The only possible failure
900  * condition is if the pointer had a destructor attached to it and the
901  * destructor returned -1. See talloc_set_destructor() for details on
902  * destructors.
903  *
904  * If this pointer has an additional parent when talloc_free() is called
905  * then the memory is not actually released, but instead the most
906  * recently established parent is destroyed. See talloc_reference() for
907  * details on establishing additional parents.
908  *
909  * For more control on which parent is removed, see talloc_unlink()
910  *
911  * talloc_free() operates recursively on its children.
912  */
913 int talloc_free(void *ptr);
914
915 /**
916  * \brief Free a talloc chunk's children
917  * \param ptr The chunk that you want to free the children of
918  * \return success?
919  * \ingroup talloc_basic
920  *
921  * The talloc_free_children() walks along the list of all children of a talloc
922  * context and talloc_free()s only the children, not the context itself.
923  */
924 void talloc_free_children(void *ptr);
925 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
926 void *_talloc_steal(const void *new_ctx, const void *ptr);
927 void *_talloc_move(const void *new_ctx, const void *pptr);
928
929 /**
930  * \brief Return the total size of a talloc chunk including its children
931  * \param ptr The talloc chunk
932  * \return The total size
933  * \ingroup talloc_basic
934  *
935  * The talloc_total_size() function returns the total size in bytes used
936  * by this pointer and all child pointers. Mostly useful for debugging.
937  *
938  * Passing NULL is allowed, but it will only give a meaningful result if
939  * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
940  * been called.
941  */
942 size_t talloc_total_size(const void *ptr);
943
944 /**
945  * \brief Return the number of talloc chunks hanging off a chunk
946  * \param ptr The talloc chunk
947  * \return The total size
948  * \ingroup talloc_basic
949  *
950  * The talloc_total_blocks() function returns the total memory block
951  * count used by this pointer and all child pointers. Mostly useful for
952  * debugging.
953  *
954  * Passing NULL is allowed, but it will only give a meaningful result if
955  * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
956  * been called.
957  */
958 size_t talloc_total_blocks(const void *ptr);
959
960 /**
961  * \brief Walk a complete talloc hierarchy
962  * \param ptr The talloc chunk
963  * \param depth Internal parameter to control recursion. Call with 0.
964  * \param max_depth Maximum recursion level.
965  * \param callback Function to be called on every chunk
966  * \param private_data Private pointer passed to callback
967  * \ingroup talloc_debug
968  *
969  * This provides a more flexible reports than talloc_report(). It
970  * will recursively call the callback for the entire tree of memory
971  * referenced by the pointer. References in the tree are passed with
972  * is_ref = 1 and the pointer that is referenced.
973  *
974  * You can pass NULL for the pointer, in which case a report is
975  * printed for the top level memory context, but only if
976  * talloc_enable_leak_report() or talloc_enable_leak_report_full()
977  * has been called.
978  *
979  * The recursion is stopped when depth >= max_depth.
980  * max_depth = -1 means only stop at leaf nodes.
981  */
982 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
983                             void (*callback)(const void *ptr,
984                                              int depth, int max_depth,
985                                              int is_ref,
986                                              void *private_data),
987                             void *private_data);
988
989 /**
990  * \brief Print a talloc hierarchy
991  * \param ptr The talloc chunk
992  * \param depth Internal parameter to control recursion. Call with 0.
993  * \param max_depth Maximum recursion level.
994  * \param f The file handle to print to
995  * \ingroup talloc_debug
996  *
997  * This provides a more flexible reports than talloc_report(). It
998  * will let you specify the depth and max_depth.
999  */
1000 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
1001
1002 /**
1003  * \brief Print a summary report of all memory used by ptr
1004  * \param ptr The talloc chunk
1005  * \param f The file handle to print to
1006  * \ingroup talloc_debug
1007  *
1008  * This provides a more detailed report than talloc_report(). It will
1009  * recursively print the ensire tree of memory referenced by the
1010  * pointer. References in the tree are shown by giving the name of the
1011  * pointer that is referenced.
1012  *
1013  * You can pass NULL for the pointer, in which case a report is printed
1014  * for the top level memory context, but only if
1015  * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
1016  * been called.
1017  */
1018 void talloc_report_full(const void *ptr, FILE *f);
1019
1020 /**
1021  * \brief Print a summary report of all memory used by ptr
1022  * \param ptr The talloc chunk
1023  * \param f The file handle to print to
1024  * \ingroup talloc_debug
1025  *
1026  * The talloc_report() function prints a summary report of all memory
1027  * used by ptr. One line of report is printed for each immediate child of
1028  * ptr, showing the total memory and number of blocks used by that child.
1029  *
1030  * You can pass NULL for the pointer, in which case a report is printed
1031  * for the top level memory context, but only if
1032  * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
1033  * been called.
1034  */
1035 void talloc_report(const void *ptr, FILE *f);
1036
1037 /**
1038  * \brief Enable tracking the use of NULL memory contexts
1039  * \ingroup talloc_debug
1040  *
1041  * This enables tracking of the NULL memory context without enabling leak
1042  * reporting on exit. Useful for when you want to do your own leak
1043  * reporting call via talloc_report_null_full();
1044  */
1045 void talloc_enable_null_tracking(void);
1046
1047 /**
1048  * \brief Disable tracking of the NULL memory context
1049  * \ingroup talloc_debug
1050  *
1051  * This disables tracking of the NULL memory context.
1052  */
1053
1054 void talloc_disable_null_tracking(void);
1055
1056 /**
1057  * \brief Enable calling of talloc_report(NULL, stderr) when a program exits
1058  * \ingroup talloc_debug
1059  *
1060  * This enables calling of talloc_report(NULL, stderr) when the program
1061  * exits. In Samba4 this is enabled by using the --leak-report command
1062  * line option.
1063  *
1064  * For it to be useful, this function must be called before any other
1065  * talloc function as it establishes a "null context" that acts as the
1066  * top of the tree. If you don't call this function first then passing
1067  * NULL to talloc_report() or talloc_report_full() won't give you the
1068  * full tree printout.
1069  *
1070  * Here is a typical talloc report:
1071  *
1072 \verbatim
1073 talloc report on 'null_context' (total 267 bytes in 15 blocks)
1074          libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
1075          libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
1076          iconv(UTF8,CP850)              contains     42 bytes in   2 blocks
1077          libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
1078          iconv(CP850,UTF8)              contains     42 bytes in   2 blocks
1079          iconv(UTF8,UTF-16LE)           contains     45 bytes in   2 blocks
1080          iconv(UTF-16LE,UTF8)           contains     45 bytes in   2 blocks
1081 \endverbatim
1082  */
1083 void talloc_enable_leak_report(void);
1084
1085 /**
1086  * \brief Enable calling of talloc_report(NULL, stderr) when a program exits
1087  * \ingroup talloc_debug
1088  *
1089  * This enables calling of talloc_report_full(NULL, stderr) when the
1090  * program exits. In Samba4 this is enabled by using the
1091  * --leak-report-full command line option.
1092  *
1093  * For it to be useful, this function must be called before any other
1094  * talloc function as it establishes a "null context" that acts as the
1095  * top of the tree. If you don't call this function first then passing
1096  * NULL to talloc_report() or talloc_report_full() won't give you the
1097  * full tree printout.
1098  *
1099  * Here is a typical full report:
1100 \verbatim
1101 full talloc report on 'root' (total 18 bytes in 8 blocks)
1102     p1                             contains     18 bytes in   7 blocks (ref 0)
1103         r1                             contains     13 bytes in   2 blocks (ref 0)
1104             reference to: p2
1105         p2                             contains      1 bytes in   1 blocks (ref 1)
1106         x3                             contains      1 bytes in   1 blocks (ref 0)
1107         x2                             contains      1 bytes in   1 blocks (ref 0)
1108         x1                             contains      1 bytes in   1 blocks (ref 0)
1109 \endverbatim
1110 */
1111 void talloc_enable_leak_report_full(void);
1112 void *_talloc_zero(const void *ctx, size_t size, const char *name);
1113 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
1114 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
1115 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name);
1116 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
1117
1118 /**
1119  * \brief Provide a function version of talloc_realloc_size
1120  * \param context The parent context used if "ptr" is NULL
1121  * \param ptr The chunk to be resized
1122  * \param size The new chunk size
1123  * \return The new chunk
1124  * \ingroup talloc_array
1125  *
1126  * This is a non-macro version of talloc_realloc(), which is useful as
1127  * libraries sometimes want a ralloc function pointer. A realloc()
1128  * implementation encapsulates the functionality of malloc(), free() and
1129  * realloc() in one call, which is why it is useful to be able to pass around
1130  * a single function pointer.
1131 */
1132 void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
1133
1134 /**
1135  * \brief Provide a talloc context that is freed at program exit
1136  * \return A talloc context
1137  * \ingroup talloc_basic
1138  *
1139  * This is a handy utility function that returns a talloc context
1140  * which will be automatically freed on program exit. This can be used
1141  * to reduce the noise in memory leak reports.
1142  */
1143 void *talloc_autofree_context(void);
1144
1145 /**
1146  * \brief Get the size of a talloc chunk
1147  * \param ctx The talloc chunk
1148  * \return The size
1149  * \ingroup talloc_basic
1150  *
1151  * This function lets you know the amount of memory alloced so far by
1152  * this context. It does NOT account for subcontext memory.
1153  * This can be used to calculate the size of an array.
1154  */
1155 size_t talloc_get_size(const void *ctx);
1156
1157 /**
1158  * \brief Find a parent context by name
1159  * \param ctx The talloc chunk to start from
1160  * \param name The name of the parent we look for
1161  * \ingroup talloc_basic
1162  *
1163  * Find a parent memory context of the current context that has the given
1164  * name. This can be very useful in complex programs where it may be
1165  * difficult to pass all information down to the level you need, but you
1166  * know the structure you want is a parent of another context.
1167  */
1168 void *talloc_find_parent_byname(const void *ctx, const char *name);
1169 void talloc_show_parents(const void *context, FILE *file);
1170
1171 /**
1172  * \brief Check if a context is parent of a talloc chunk
1173  * \param context The assumed talloc context
1174  * \param ptr The talloc chunk to check
1175  * \return Is context an anchestor of ptr
1176  * \ingroup talloc_basic
1177  *
1178  * talloc_is_parent() checks if context is referenced in the talloc
1179  * hierarchy above ptr. Return 1 if this is the case, 0 if not.
1180  */
1181 int talloc_is_parent(const void *context, const void *ptr);
1182
1183 /**
1184  * \brief Duplicate a string into a talloc chunk
1185  * \param t The talloc context to hang the result off
1186  * \param p The string you want to duplicate
1187  * \return The duplicated string
1188  * \ingroup talloc_string
1189  *
1190  * The talloc_strdup() function is equivalent to:
1191  *
1192  * \code
1193  * ptr = talloc_size(ctx, strlen(p)+1);
1194  * if (ptr) memcpy(ptr, p, strlen(p)+1);
1195  * \endcode
1196  *
1197  * This functions sets the name of the new pointer to the passed
1198  * string. This is equivalent to:
1199  *
1200  * \code
1201  * talloc_set_name_const(ptr, ptr)
1202  * \endcode
1203  */
1204 char *talloc_strdup(const void *t, const char *p);
1205 char *talloc_strdup_append(char *s, const char *a);
1206 char *talloc_strdup_append_buffer(char *s, const char *a);
1207
1208 /**
1209  * \brief Duplicate a length-limited string into a talloc chunk
1210  * \param t The talloc context to hang the result off
1211  * \param p The string you want to duplicate
1212  * \param n The maximum string length to duplicate
1213  * \return The duplicated string
1214  * \ingroup talloc_string
1215  *
1216  * The talloc_strndup() function is the talloc equivalent of the C
1217  * library function strndup()
1218  *
1219  * This functions sets the name of the new pointer to the passed
1220  * string. This is equivalent to:
1221  *
1222  * \code
1223  * talloc_set_name_const(ptr, ptr)
1224  * \endcode
1225  */
1226 char *talloc_strndup(const void *t, const char *p, size_t n);
1227 char *talloc_strndup_append(char *s, const char *a, size_t n);
1228 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n);
1229
1230 /**
1231  * \brief Format a string given a va_list
1232  * \param t The talloc context to hang the result off
1233  * \param fmt The format string
1234  * \param ap The parameters used to fill fmt
1235  * \return The formatted string
1236  * \ingroup talloc_string
1237  *
1238  * The talloc_vasprintf() function is the talloc equivalent of the C
1239  * library function vasprintf()
1240  *
1241  * This functions sets the name of the new pointer to the new
1242  * string. This is equivalent to:
1243  *
1244  * \code
1245  * talloc_set_name_const(ptr, ptr)
1246  * \endcode
1247  */
1248 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1249 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1250 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1251
1252 /**
1253  * \brief Format a string
1254  * \param t The talloc context to hang the result off
1255  * \param fmt The format string
1256  * \param ... The parameters used to fill fmt
1257  * \return The formatted string
1258  * \ingroup talloc_string
1259  *
1260  * The talloc_asprintf() function is the talloc equivalent of the C
1261  * library function asprintf()
1262  *
1263  * This functions sets the name of the new pointer to the new
1264  * string. This is equivalent to:
1265  *
1266  * \code
1267  * talloc_set_name_const(ptr, ptr)
1268  * \endcode
1269  */
1270 char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1271
1272 /**
1273  * \brief Append a formatted string to another string
1274  * \param s The string to append to
1275  * \param fmt The format string
1276  * \param ... The parameters used to fill fmt
1277  * \return The formatted string
1278  * \ingroup talloc_string
1279  *
1280  * The talloc_asprintf_append() function appends the given formatted string to
1281  * the given string. Use this varient when the string in the current talloc
1282  * buffer may have been truncated in length.
1283  *
1284  * This functions sets the name of the new pointer to the new
1285  * string. This is equivalent to:
1286  *
1287  * \code
1288  * talloc_set_name_const(ptr, ptr)
1289  * \endcode
1290  */
1291 char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1292
1293 /**
1294  * \brief Append a formatted string to another string
1295  * \param s The string to append to
1296  * \param fmt The format string
1297  * \param ... The parameters used to fill fmt
1298  * \return The formatted string
1299  * \ingroup talloc_string
1300  *
1301  * The talloc_asprintf_append() function appends the given formatted string to
1302  * the end of the currently allocated talloc buffer. This routine should be
1303  * used if you create a large string step by step. talloc_asprintf() or
1304  * talloc_asprintf_append() call strlen() at every
1305  * step. talloc_asprintf_append_buffer() uses the existing buffer size of the
1306  * talloc chunk to calculate where to append the string.
1307  *
1308  * This functions sets the name of the new pointer to the new
1309  * string. This is equivalent to:
1310  *
1311  * \code
1312  * talloc_set_name_const(ptr, ptr)
1313  * \endcode
1314  */
1315 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1316
1317 void talloc_set_abort_fn(void (*abort_fn)(const char *reason));
1318
1319 #endif
1320
1321 /*\}*/