WHATSNEW: Add release notes for Samba 4.13.3.
[Samba.git] / lib / talloc / talloc.h
blob0154bf3bbf61e7ad45aed67f308dfc3f19aa3282
1 #ifndef _TALLOC_H_
2 #define _TALLOC_H_
3 /*
4 Unix SMB/CIFS implementation.
5 Samba temporary memory allocation functions
7 Copyright (C) Andrew Tridgell 2004-2005
8 Copyright (C) Stefan Metzmacher 2006
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
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.
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.
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/>.
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdarg.h>
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
36 /**
37 * @defgroup talloc The talloc API
39 * talloc is a hierarchical, reference counted memory pool system with
40 * destructors. It is the core memory allocator used in Samba.
42 * @{
45 #define TALLOC_VERSION_MAJOR 2
46 #define TALLOC_VERSION_MINOR 3
48 int talloc_version_major(void);
49 int talloc_version_minor(void);
50 /* This is mostly useful only for testing */
51 int talloc_test_get_magic(void);
53 /**
54 * @brief Define a talloc parent type
56 * As talloc is a hierarchial memory allocator, every talloc chunk is a
57 * potential parent to other talloc chunks. So defining a separate type for a
58 * talloc chunk is not strictly necessary. TALLOC_CTX is defined nevertheless,
59 * as it provides an indicator for function arguments. You will frequently
60 * write code like
62 * @code
63 * struct foo *foo_create(TALLOC_CTX *mem_ctx)
64 * {
65 * struct foo *result;
66 * result = talloc(mem_ctx, struct foo);
67 * if (result == NULL) return NULL;
68 * ... initialize foo ...
69 * return result;
70 * }
71 * @endcode
73 * In this type of allocating functions it is handy to have a general
74 * TALLOC_CTX type to indicate which parent to put allocated structures on.
76 typedef void TALLOC_CTX;
79 this uses a little trick to allow __LINE__ to be stringified
81 #ifndef __location__
82 #define __TALLOC_STRING_LINE1__(s) #s
83 #define __TALLOC_STRING_LINE2__(s) __TALLOC_STRING_LINE1__(s)
84 #define __TALLOC_STRING_LINE3__ __TALLOC_STRING_LINE2__(__LINE__)
85 #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__
86 #endif
88 #ifndef TALLOC_DEPRECATED
89 #define TALLOC_DEPRECATED 0
90 #endif
92 /* for old gcc releases that don't have the feature test macro __has_attribute */
93 #ifndef __has_attribute
94 #define __has_attribute(x) 0
95 #endif
97 #ifndef PRINTF_ATTRIBUTE
98 #if __has_attribute(format) || (__GNUC__ >= 3)
99 /** Use gcc attribute to check printf fns. a1 is the 1-based index of
100 * the parameter containing the format, and a2 the index of the first
101 * argument. Note that some gcc 2.x versions don't handle this
102 * properly **/
103 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
104 #else
105 #define PRINTF_ATTRIBUTE(a1, a2)
106 #endif
107 #endif
109 #ifndef _DEPRECATED_
110 #ifdef HAVE___ATTRIBUTE__
111 #define _DEPRECATED_ __attribute__ ((deprecated))
112 #else
113 #define _DEPRECATED_
114 #endif
115 #endif
116 #ifdef DOXYGEN
119 * @brief Create a new talloc context.
121 * The talloc() macro is the core of the talloc library. It takes a memory
122 * context and a type, and returns a pointer to a new area of memory of the
123 * given type.
125 * The returned pointer is itself a talloc context, so you can use it as the
126 * context argument to more calls to talloc if you wish.
128 * The returned pointer is a "child" of the supplied context. This means that if
129 * you talloc_free() the context then the new child disappears as well.
130 * Alternatively you can free just the child.
132 * @param[in] ctx A talloc context to create a new reference on or NULL to
133 * create a new top level context.
135 * @param[in] type The type of memory to allocate.
137 * @return A type casted talloc context or NULL on error.
139 * @code
140 * unsigned int *a, *b;
142 * a = talloc(NULL, unsigned int);
143 * b = talloc(a, unsigned int);
144 * @endcode
146 * @see talloc_zero
147 * @see talloc_array
148 * @see talloc_steal
149 * @see talloc_free
151 void *talloc(const void *ctx, #type);
152 #else
153 #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
154 void *_talloc(const void *context, size_t size);
155 #endif
158 * @brief Create a new top level talloc context.
160 * This function creates a zero length named talloc context as a top level
161 * context. It is equivalent to:
163 * @code
164 * talloc_named(NULL, 0, fmt, ...);
165 * @endcode
166 * @param[in] fmt Format string for the name.
168 * @param[in] ... Additional printf-style arguments.
170 * @return The allocated memory chunk, NULL on error.
172 * @see talloc_named()
174 void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
176 #ifdef DOXYGEN
178 * @brief Free a chunk of talloc memory.
180 * The talloc_free() function frees a piece of talloc memory, and all its
181 * children. You can call talloc_free() on any pointer returned by
182 * talloc().
184 * The return value of talloc_free() indicates success or failure, with 0
185 * returned for success and -1 for failure. A possible failure condition
186 * is if the pointer had a destructor attached to it and the destructor
187 * returned -1. See talloc_set_destructor() for details on
188 * destructors. Likewise, if "ptr" is NULL, then the function will make
189 * no modifications and return -1.
191 * From version 2.0 and onwards, as a special case, talloc_free() is
192 * refused on pointers that have more than one parent associated, as talloc
193 * would have no way of knowing which parent should be removed. This is
194 * different from older versions in the sense that always the reference to
195 * the most recently established parent has been destroyed. Hence to free a
196 * pointer that has more than one parent please use talloc_unlink().
198 * To help you find problems in your code caused by this behaviour, if
199 * you do try and free a pointer with more than one parent then the
200 * talloc logging function will be called to give output like this:
202 * @code
203 * ERROR: talloc_free with references at some_dir/source/foo.c:123
204 * reference at some_dir/source/other.c:325
205 * reference at some_dir/source/third.c:121
206 * @endcode
208 * Please see the documentation for talloc_set_log_fn() and
209 * talloc_set_log_stderr() for more information on talloc logging
210 * functions.
212 * If <code>TALLOC_FREE_FILL</code> environment variable is set,
213 * the memory occupied by the context is filled with the value of this variable.
214 * The value should be a numeric representation of the character you want to
215 * use.
217 * talloc_free() operates recursively on its children.
219 * @param[in] ptr The chunk to be freed.
221 * @return Returns 0 on success and -1 on error. A possible
222 * failure condition is if the pointer had a destructor
223 * attached to it and the destructor returned -1. Likewise,
224 * if "ptr" is NULL, then the function will make no
225 * modifications and returns -1.
227 * Example:
228 * @code
229 * unsigned int *a, *b;
230 * a = talloc(NULL, unsigned int);
231 * b = talloc(a, unsigned int);
233 * talloc_free(a); // Frees a and b
234 * @endcode
236 * @see talloc_set_destructor()
237 * @see talloc_unlink()
239 int talloc_free(void *ptr);
240 #else
241 #define talloc_free(ctx) _talloc_free(ctx, __location__)
242 int _talloc_free(void *ptr, const char *location);
243 #endif
246 * @brief Free a talloc chunk's children.
248 * The function walks along the list of all children of a talloc context and
249 * talloc_free()s only the children, not the context itself.
251 * A NULL argument is handled as no-op.
253 * @param[in] ptr The chunk that you want to free the children of
254 * (NULL is allowed too)
256 void talloc_free_children(void *ptr);
258 #ifdef DOXYGEN
260 * @brief Assign a destructor function to be called when a chunk is freed.
262 * The function talloc_set_destructor() sets the "destructor" for the pointer
263 * "ptr". A destructor is a function that is called when the memory used by a
264 * pointer is about to be released. The destructor receives the pointer as an
265 * argument, and should return 0 for success and -1 for failure.
267 * The destructor can do anything it wants to, including freeing other pieces
268 * of memory. A common use for destructors is to clean up operating system
269 * resources (such as open file descriptors) contained in the structure the
270 * destructor is placed on.
272 * You can only place one destructor on a pointer. If you need more than one
273 * destructor then you can create a zero-length child of the pointer and place
274 * an additional destructor on that.
276 * To remove a destructor call talloc_set_destructor() with NULL for the
277 * destructor.
279 * If your destructor attempts to talloc_free() the pointer that it is the
280 * destructor for then talloc_free() will return -1 and the free will be
281 * ignored. This would be a pointless operation anyway, as the destructor is
282 * only called when the memory is just about to go away.
284 * @param[in] ptr The talloc chunk to add a destructor to.
286 * @param[in] destructor The destructor function to be called. NULL to remove
287 * it.
289 * Example:
290 * @code
291 * static int destroy_fd(int *fd) {
292 * close(*fd);
293 * return 0;
296 * int *open_file(const char *filename) {
297 * int *fd = talloc(NULL, int);
298 * *fd = open(filename, O_RDONLY);
299 * if (*fd < 0) {
300 * talloc_free(fd);
301 * return NULL;
303 * // Whenever they free this, we close the file.
304 * talloc_set_destructor(fd, destroy_fd);
305 * return fd;
307 * @endcode
309 * @see talloc()
310 * @see talloc_free()
312 void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
315 * @brief Change a talloc chunk's parent.
317 * The talloc_steal() function changes the parent context of a talloc
318 * pointer. It is typically used when the context that the pointer is
319 * currently a child of is going to be freed and you wish to keep the
320 * memory for a longer time.
322 * To make the changed hierarchy less error-prone, you might consider to use
323 * talloc_move().
325 * If you try and call talloc_steal() on a pointer that has more than one
326 * parent then the result is ambiguous. Talloc will choose to remove the
327 * parent that is currently indicated by talloc_parent() and replace it with
328 * the chosen parent. You will also get a message like this via the talloc
329 * logging functions:
331 * @code
332 * WARNING: talloc_steal with references at some_dir/source/foo.c:123
333 * reference at some_dir/source/other.c:325
334 * reference at some_dir/source/third.c:121
335 * @endcode
337 * To unambiguously change the parent of a pointer please see the function
338 * talloc_reparent(). See the talloc_set_log_fn() documentation for more
339 * information on talloc logging.
341 * @param[in] new_ctx The new parent context.
343 * @param[in] ptr The talloc chunk to move.
345 * @return Returns the pointer that you pass it. It does not have
346 * any failure modes.
348 * @note It is possible to produce loops in the parent/child relationship
349 * if you are not careful with talloc_steal(). No guarantees are provided
350 * as to your sanity or the safety of your data if you do this.
352 void *talloc_steal(const void *new_ctx, const void *ptr);
353 #else /* DOXYGEN */
354 /* try to make talloc_set_destructor() and talloc_steal() type safe,
355 if we have a recent gcc */
356 #if (__GNUC__ >= 3)
357 #define _TALLOC_TYPEOF(ptr) __typeof__(ptr)
358 #define talloc_set_destructor(ptr, function) \
359 do { \
360 int (*_talloc_destructor_fn)(_TALLOC_TYPEOF(ptr)) = (function); \
361 _talloc_set_destructor((ptr), (int (*)(void *))_talloc_destructor_fn); \
362 } while(0)
363 /* this extremely strange macro is to avoid some braindamaged warning
364 stupidity in gcc 4.1.x */
365 #define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__); __talloc_steal_ret; })
366 #else /* __GNUC__ >= 3 */
367 #define talloc_set_destructor(ptr, function) \
368 _talloc_set_destructor((ptr), (int (*)(void *))(function))
369 #define _TALLOC_TYPEOF(ptr) void *
370 #define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__)
371 #endif /* __GNUC__ >= 3 */
372 void _talloc_set_destructor(const void *ptr, int (*_destructor)(void *));
373 void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location);
374 #endif /* DOXYGEN */
377 * @brief Assign a name to a talloc chunk.
379 * Each talloc pointer has a "name". The name is used principally for
380 * debugging purposes, although it is also possible to set and get the name on
381 * a pointer in as a way of "marking" pointers in your code.
383 * The main use for names on pointer is for "talloc reports". See
384 * talloc_report() and talloc_report_full() for details. Also see
385 * talloc_enable_leak_report() and talloc_enable_leak_report_full().
387 * The talloc_set_name() function allocates memory as a child of the
388 * pointer. It is logically equivalent to:
390 * @code
391 * talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
392 * @endcode
394 * @param[in] ptr The talloc chunk to assign a name to.
396 * @param[in] fmt Format string for the name.
398 * @param[in] ... Add printf-style additional arguments.
400 * @return The assigned name, NULL on error.
402 * @note Multiple calls to talloc_set_name() will allocate more memory without
403 * releasing the name. All of the memory is released when the ptr is freed
404 * using talloc_free().
406 const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
408 #ifdef DOXYGEN
410 * @brief Change a talloc chunk's parent.
412 * This function has the same effect as talloc_steal(), and additionally sets
413 * the source pointer to NULL. You would use it like this:
415 * @code
416 * struct foo *X = talloc(tmp_ctx, struct foo);
417 * struct foo *Y;
418 * Y = talloc_move(new_ctx, &X);
419 * @endcode
421 * @param[in] new_ctx The new parent context.
423 * @param[in] pptr Pointer to a pointer to the talloc chunk to move.
425 * @return The pointer to the talloc chunk that moved.
426 * It does not have any failure modes.
429 void *talloc_move(const void *new_ctx, void **pptr);
430 #else
431 #define talloc_move(ctx, pptr) (_TALLOC_TYPEOF(*(pptr)))_talloc_move((ctx),(void *)(pptr))
432 void *_talloc_move(const void *new_ctx, const void *pptr);
433 #endif
436 * @brief Assign a name to a talloc chunk.
438 * The function is just like talloc_set_name(), but it takes a string constant,
439 * and is much faster. It is extensively used by the "auto naming" macros, such
440 * as talloc_p().
442 * This function does not allocate any memory. It just copies the supplied
443 * pointer into the internal representation of the talloc ptr. This means you
444 * must not pass a name pointer to memory that will disappear before the ptr
445 * is freed with talloc_free().
447 * @param[in] ptr The talloc chunk to assign a name to.
449 * @param[in] name Format string for the name.
451 void talloc_set_name_const(const void *ptr, const char *name);
454 * @brief Create a named talloc chunk.
456 * The talloc_named() function creates a named talloc pointer. It is
457 * equivalent to:
459 * @code
460 * ptr = talloc_size(context, size);
461 * talloc_set_name(ptr, fmt, ....);
462 * @endcode
464 * @param[in] context The talloc context to hang the result off.
466 * @param[in] size Number of char's that you want to allocate.
468 * @param[in] fmt Format string for the name.
470 * @param[in] ... Additional printf-style arguments.
472 * @return The allocated memory chunk, NULL on error.
474 * @see talloc_set_name()
476 void *talloc_named(const void *context, size_t size,
477 const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
480 * @brief Basic routine to allocate a chunk of memory.
482 * This is equivalent to:
484 * @code
485 * ptr = talloc_size(context, size);
486 * talloc_set_name_const(ptr, name);
487 * @endcode
489 * @param[in] context The parent context.
491 * @param[in] size The number of char's that we want to allocate.
493 * @param[in] name The name the talloc block has.
495 * @return The allocated memory chunk, NULL on error.
497 void *talloc_named_const(const void *context, size_t size, const char *name);
499 #ifdef DOXYGEN
501 * @brief Untyped allocation.
503 * The function should be used when you don't have a convenient type to pass to
504 * talloc(). Unlike talloc(), it is not type safe (as it returns a void *), so
505 * you are on your own for type checking.
507 * Best to use talloc() or talloc_array() instead.
509 * @param[in] ctx The talloc context to hang the result off.
511 * @param[in] size Number of char's that you want to allocate.
513 * @return The allocated memory chunk, NULL on error.
515 * Example:
516 * @code
517 * void *mem = talloc_size(NULL, 100);
518 * @endcode
520 void *talloc_size(const void *ctx, size_t size);
521 #else
522 #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
523 #endif
525 #ifdef DOXYGEN
527 * @brief Allocate into a typed pointer.
529 * The talloc_ptrtype() macro should be used when you have a pointer and want
530 * to allocate memory to point at with this pointer. When compiling with
531 * gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size() and
532 * talloc_get_name() will return the current location in the source file and
533 * not the type.
535 * @param[in] ctx The talloc context to hang the result off.
537 * @param[in] type The pointer you want to assign the result to.
539 * @return The properly casted allocated memory chunk, NULL on
540 * error.
542 * Example:
543 * @code
544 * unsigned int *a = talloc_ptrtype(NULL, a);
545 * @endcode
547 void *talloc_ptrtype(const void *ctx, #type);
548 #else
549 #define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
550 #endif
552 #ifdef DOXYGEN
554 * @brief Allocate a new 0-sized talloc chunk.
556 * This is a utility macro that creates a new memory context hanging off an
557 * existing context, automatically naming it "talloc_new: __location__" where
558 * __location__ is the source line it is called from. It is particularly
559 * useful for creating a new temporary working context.
561 * @param[in] ctx The talloc parent context.
563 * @return A new talloc chunk, NULL on error.
565 void *talloc_new(const void *ctx);
566 #else
567 #define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
568 #endif
570 #ifdef DOXYGEN
572 * @brief Allocate a 0-initizialized structure.
574 * The macro is equivalent to:
576 * @code
577 * ptr = talloc(ctx, type);
578 * if (ptr) memset(ptr, 0, sizeof(type));
579 * @endcode
581 * @param[in] ctx The talloc context to hang the result off.
583 * @param[in] type The type that we want to allocate.
585 * @return Pointer to a piece of memory, properly cast to 'type *',
586 * NULL on error.
588 * Example:
589 * @code
590 * unsigned int *a, *b;
591 * a = talloc_zero(NULL, unsigned int);
592 * b = talloc_zero(a, unsigned int);
593 * @endcode
595 * @see talloc()
596 * @see talloc_zero_size()
597 * @see talloc_zero_array()
599 void *talloc_zero(const void *ctx, #type);
602 * @brief Allocate untyped, 0-initialized memory.
604 * @param[in] ctx The talloc context to hang the result off.
606 * @param[in] size Number of char's that you want to allocate.
608 * @return The allocated memory chunk.
610 void *talloc_zero_size(const void *ctx, size_t size);
611 #else
612 #define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
613 #define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
614 void *_talloc_zero(const void *ctx, size_t size, const char *name);
615 #endif
618 * @brief Return the name of a talloc chunk.
620 * @param[in] ptr The talloc chunk.
622 * @return The current name for the given talloc pointer.
624 * @see talloc_set_name()
626 const char *talloc_get_name(const void *ptr);
629 * @brief Verify that a talloc chunk carries a specified name.
631 * This function checks if a pointer has the specified name. If it does
632 * then the pointer is returned.
634 * @param[in] ptr The talloc chunk to check.
636 * @param[in] name The name to check against.
638 * @return The pointer if the name matches, NULL if it doesn't.
640 void *talloc_check_name(const void *ptr, const char *name);
643 * @brief Get the parent chunk of a pointer.
645 * @param[in] ptr The talloc pointer to inspect.
647 * @return The talloc parent of ptr, NULL on error.
649 void *talloc_parent(const void *ptr);
652 * @brief Get a talloc chunk's parent name.
654 * @param[in] ptr The talloc pointer to inspect.
656 * @return The name of ptr's parent chunk.
658 const char *talloc_parent_name(const void *ptr);
661 * @brief Get the total size of a talloc chunk including its children.
663 * The function returns the total size in bytes used by this pointer and all
664 * child pointers. Mostly useful for debugging.
666 * Passing NULL is allowed, but it will only give a meaningful result if
667 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
668 * been called.
670 * @param[in] ptr The talloc chunk.
672 * @return The total size.
674 size_t talloc_total_size(const void *ptr);
677 * @brief Get the number of talloc chunks hanging off a chunk.
679 * The talloc_total_blocks() function returns the total memory block
680 * count used by this pointer and all child pointers. Mostly useful for
681 * debugging.
683 * Passing NULL is allowed, but it will only give a meaningful result if
684 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
685 * been called.
687 * @param[in] ptr The talloc chunk.
689 * @return The total size.
691 size_t talloc_total_blocks(const void *ptr);
693 #ifdef DOXYGEN
695 * @brief Duplicate a memory area into a talloc chunk.
697 * The function is equivalent to:
699 * @code
700 * ptr = talloc_size(ctx, size);
701 * if (ptr) memcpy(ptr, p, size);
702 * @endcode
704 * @param[in] t The talloc context to hang the result off.
706 * @param[in] p The memory chunk you want to duplicate.
708 * @param[in] size Number of char's that you want copy.
710 * @return The allocated memory chunk.
712 * @see talloc_size()
714 void *talloc_memdup(const void *t, const void *p, size_t size);
715 #else
716 #define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
717 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
718 #endif
720 #ifdef DOXYGEN
722 * @brief Assign a type to a talloc chunk.
724 * This macro allows you to force the name of a pointer to be of a particular
725 * type. This can be used in conjunction with talloc_get_type() to do type
726 * checking on void* pointers.
728 * It is equivalent to this:
730 * @code
731 * talloc_set_name_const(ptr, #type)
732 * @endcode
734 * @param[in] ptr The talloc chunk to assign the type to.
736 * @param[in] type The type to assign.
738 void talloc_set_type(const char *ptr, #type);
741 * @brief Get a typed pointer out of a talloc pointer.
743 * This macro allows you to do type checking on talloc pointers. It is
744 * particularly useful for void* private pointers. It is equivalent to
745 * this:
747 * @code
748 * (type *)talloc_check_name(ptr, #type)
749 * @endcode
751 * @param[in] ptr The talloc pointer to check.
753 * @param[in] type The type to check against.
755 * @return The properly casted pointer given by ptr, NULL on error.
757 type *talloc_get_type(const void *ptr, #type);
758 #else
759 #define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
760 #define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
761 #endif
763 #ifdef DOXYGEN
765 * @brief Safely turn a void pointer into a typed pointer.
767 * This macro is used together with talloc(mem_ctx, struct foo). If you had to
768 * assign the talloc chunk pointer to some void pointer variable,
769 * talloc_get_type_abort() is the recommended way to get the convert the void
770 * pointer back to a typed pointer.
772 * @param[in] ptr The void pointer to convert.
774 * @param[in] type The type that this chunk contains
776 * @return The same value as ptr, type-checked and properly cast.
778 void *talloc_get_type_abort(const void *ptr, #type);
779 #else
780 #ifdef TALLOC_GET_TYPE_ABORT_NOOP
781 #define talloc_get_type_abort(ptr, type) (type *)(ptr)
782 #else
783 #define talloc_get_type_abort(ptr, type) (type *)_talloc_get_type_abort(ptr, #type, __location__)
784 #endif
785 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location);
786 #endif
789 * @brief Find a parent context by name.
791 * Find a parent memory context of the current context that has the given
792 * name. This can be very useful in complex programs where it may be
793 * difficult to pass all information down to the level you need, but you
794 * know the structure you want is a parent of another context.
796 * @param[in] ctx The talloc chunk to start from.
798 * @param[in] name The name of the parent we look for.
800 * @return The memory context we are looking for, NULL if not
801 * found.
803 void *talloc_find_parent_byname(const void *ctx, const char *name);
805 #ifdef DOXYGEN
807 * @brief Find a parent context by type.
809 * Find a parent memory context of the current context that has the given
810 * name. This can be very useful in complex programs where it may be
811 * difficult to pass all information down to the level you need, but you
812 * know the structure you want is a parent of another context.
814 * Like talloc_find_parent_byname() but takes a type, making it typesafe.
816 * @param[in] ptr The talloc chunk to start from.
818 * @param[in] type The type of the parent to look for.
820 * @return The memory context we are looking for, NULL if not
821 * found.
823 void *talloc_find_parent_bytype(const void *ptr, #type);
824 #else
825 #define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
826 #endif
829 * @brief Allocate a talloc pool.
831 * A talloc pool is a pure optimization for specific situations. In the
832 * release process for Samba 3.2 we found out that we had become considerably
833 * slower than Samba 3.0 was. Profiling showed that malloc(3) was a large CPU
834 * consumer in benchmarks. For Samba 3.2 we have internally converted many
835 * static buffers to dynamically allocated ones, so malloc(3) being beaten
836 * more was no surprise. But it made us slower.
838 * talloc_pool() is an optimization to call malloc(3) a lot less for the use
839 * pattern Samba has: The SMB protocol is mainly a request/response protocol
840 * where we have to allocate a certain amount of memory per request and free
841 * that after the SMB reply is sent to the client.
843 * talloc_pool() creates a talloc chunk that you can use as a talloc parent
844 * exactly as you would use any other ::TALLOC_CTX. The difference is that
845 * when you talloc a child of this pool, no malloc(3) is done. Instead, talloc
846 * just increments a pointer inside the talloc_pool. This also works
847 * recursively. If you use the child of the talloc pool as a parent for
848 * grand-children, their memory is also taken from the talloc pool.
850 * If there is not enough memory in the pool to allocate the new child,
851 * it will create a new talloc chunk as if the parent was a normal talloc
852 * context.
854 * If you talloc_free() children of a talloc pool, the memory is not given
855 * back to the system. Instead, free(3) is only called if the talloc_pool()
856 * itself is released with talloc_free().
858 * The downside of a talloc pool is that if you talloc_move() a child of a
859 * talloc pool to a talloc parent outside the pool, the whole pool memory is
860 * not free(3)'ed until that moved chunk is also talloc_free()ed.
862 * @param[in] context The talloc context to hang the result off.
864 * @param[in] size Size of the talloc pool.
866 * @return The allocated talloc pool, NULL on error.
868 void *talloc_pool(const void *context, size_t size);
870 #ifdef DOXYGEN
872 * @brief Allocate a talloc object as/with an additional pool.
874 * This is like talloc_pool(), but's it's more flexible
875 * and allows an object to be a pool for its children.
877 * @param[in] ctx The talloc context to hang the result off.
879 * @param[in] type The type that we want to allocate.
881 * @param[in] num_subobjects The expected number of subobjects, which will
882 * be allocated within the pool. This allocates
883 * space for talloc_chunk headers.
885 * @param[in] total_subobjects_size The size that all subobjects can use in total.
888 * @return The allocated talloc object, NULL on error.
890 void *talloc_pooled_object(const void *ctx, #type,
891 unsigned num_subobjects,
892 size_t total_subobjects_size);
893 #else
894 #define talloc_pooled_object(_ctx, _type, \
895 _num_subobjects, \
896 _total_subobjects_size) \
897 (_type *)_talloc_pooled_object((_ctx), sizeof(_type), #_type, \
898 (_num_subobjects), \
899 (_total_subobjects_size))
900 void *_talloc_pooled_object(const void *ctx,
901 size_t type_size,
902 const char *type_name,
903 unsigned num_subobjects,
904 size_t total_subobjects_size);
905 #endif
908 * @brief Free a talloc chunk and NULL out the pointer.
910 * TALLOC_FREE() frees a pointer and sets it to NULL. Use this if you want
911 * immediate feedback (i.e. crash) if you use a pointer after having free'ed
912 * it.
914 * @param[in] ctx The chunk to be freed.
916 #define TALLOC_FREE(ctx) do { if (ctx != NULL) { talloc_free(ctx); ctx=NULL; } } while(0)
918 /* @} ******************************************************************/
921 * \defgroup talloc_ref The talloc reference function.
922 * @ingroup talloc
924 * This module contains the definitions around talloc references
926 * @{
930 * @brief Increase the reference count of a talloc chunk.
932 * The talloc_increase_ref_count(ptr) function is exactly equivalent to:
934 * @code
935 * talloc_reference(NULL, ptr);
936 * @endcode
938 * You can use either syntax, depending on which you think is clearer in
939 * your code.
941 * @param[in] ptr The pointer to increase the reference count.
943 * @return 0 on success, -1 on error.
945 int talloc_increase_ref_count(const void *ptr);
948 * @brief Get the number of references to a talloc chunk.
950 * @param[in] ptr The pointer to retrieve the reference count from.
952 * @return The number of references.
954 size_t talloc_reference_count(const void *ptr);
956 #ifdef DOXYGEN
958 * @brief Create an additional talloc parent to a pointer.
960 * The talloc_reference() function makes "context" an additional parent of
961 * ptr. Each additional reference consumes around 48 bytes of memory on intel
962 * x86 platforms.
964 * If ptr is NULL, then the function is a no-op, and simply returns NULL.
966 * After creating a reference you can free it in one of the following ways:
968 * - you can talloc_free() any parent of the original pointer. That
969 * will reduce the number of parents of this pointer by 1, and will
970 * cause this pointer to be freed if it runs out of parents.
972 * - you can talloc_free() the pointer itself if it has at maximum one
973 * parent. This behaviour has been changed since the release of version
974 * 2.0. Further information in the description of "talloc_free".
976 * For more control on which parent to remove, see talloc_unlink()
977 * @param[in] ctx The additional parent.
979 * @param[in] ptr The pointer you want to create an additional parent for.
981 * @return The original pointer 'ptr', NULL if talloc ran out of
982 * memory in creating the reference.
984 * @warning You should try to avoid using this interface. It turns a beautiful
985 * talloc-tree into a graph. It is often really hard to debug if you
986 * screw something up by accident.
988 * Example:
989 * @code
990 * unsigned int *a, *b, *c;
991 * a = talloc(NULL, unsigned int);
992 * b = talloc(NULL, unsigned int);
993 * c = talloc(a, unsigned int);
994 * // b also serves as a parent of c.
995 * talloc_reference(b, c);
996 * @endcode
998 * @see talloc_unlink()
1000 void *talloc_reference(const void *ctx, const void *ptr);
1001 #else
1002 #define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference_loc((ctx),(ptr), __location__)
1003 void *_talloc_reference_loc(const void *context, const void *ptr, const char *location);
1004 #endif
1007 * @brief Remove a specific parent from a talloc chunk.
1009 * The function removes a specific parent from ptr. The context passed must
1010 * either be a context used in talloc_reference() with this pointer, or must be
1011 * a direct parent of ptr.
1013 * You can just use talloc_free() instead of talloc_unlink() if there
1014 * is at maximum one parent. This behaviour has been changed since the
1015 * release of version 2.0. Further information in the description of
1016 * "talloc_free".
1018 * @param[in] context The talloc parent to remove.
1020 * @param[in] ptr The talloc ptr you want to remove the parent from.
1022 * @return 0 on success, -1 on error.
1024 * @note If the parent has already been removed using talloc_free() then
1025 * this function will fail and will return -1. Likewise, if ptr is NULL,
1026 * then the function will make no modifications and return -1.
1028 * @warning You should try to avoid using this interface. It turns a beautiful
1029 * talloc-tree into a graph. It is often really hard to debug if you
1030 * screw something up by accident.
1032 * Example:
1033 * @code
1034 * unsigned int *a, *b, *c;
1035 * a = talloc(NULL, unsigned int);
1036 * b = talloc(NULL, unsigned int);
1037 * c = talloc(a, unsigned int);
1038 * // b also serves as a parent of c.
1039 * talloc_reference(b, c);
1040 * talloc_unlink(b, c);
1041 * @endcode
1043 int talloc_unlink(const void *context, void *ptr);
1046 * @brief Provide a talloc context that is freed at program exit.
1048 * This is a handy utility function that returns a talloc context
1049 * which will be automatically freed on program exit. This can be used
1050 * to reduce the noise in memory leak reports.
1052 * Never use this in code that might be used in objects loaded with
1053 * dlopen and unloaded with dlclose. talloc_autofree_context()
1054 * internally uses atexit(3). Some platforms like modern Linux handles
1055 * this fine, but for example FreeBSD does not deal well with dlopen()
1056 * and atexit() used simultaneously: dlclose() does not clean up the
1057 * list of atexit-handlers, so when the program exits the code that
1058 * was registered from within talloc_autofree_context() is gone, the
1059 * program crashes at exit.
1061 * @return A talloc context, NULL on error.
1063 void *talloc_autofree_context(void) _DEPRECATED_;
1066 * @brief Get the size of a talloc chunk.
1068 * This function lets you know the amount of memory allocated so far by
1069 * this context. It does NOT account for subcontext memory.
1070 * This can be used to calculate the size of an array.
1072 * @param[in] ctx The talloc chunk.
1074 * @return The size of the talloc chunk.
1076 size_t talloc_get_size(const void *ctx);
1079 * @brief Show the parentage of a context.
1081 * @param[in] context The talloc context to look at.
1083 * @param[in] file The output to use, a file, stdout or stderr.
1085 void talloc_show_parents(const void *context, FILE *file);
1088 * @brief Check if a context is parent of a talloc chunk.
1090 * This checks if context is referenced in the talloc hierarchy above ptr.
1092 * @param[in] context The assumed talloc context.
1094 * @param[in] ptr The talloc chunk to check.
1096 * @return Return 1 if this is the case, 0 if not.
1098 int talloc_is_parent(const void *context, const void *ptr);
1101 * @brief Change the parent context of a talloc pointer.
1103 * The function changes the parent context of a talloc pointer. It is typically
1104 * used when the context that the pointer is currently a child of is going to be
1105 * freed and you wish to keep the memory for a longer time.
1107 * The difference between talloc_reparent() and talloc_steal() is that
1108 * talloc_reparent() can specify which parent you wish to change. This is
1109 * useful when a pointer has multiple parents via references.
1111 * @param[in] old_parent
1112 * @param[in] new_parent
1113 * @param[in] ptr
1115 * @return Return the pointer you passed. It does not have any
1116 * failure modes.
1118 void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr);
1120 /* @} ******************************************************************/
1123 * @defgroup talloc_array The talloc array functions
1124 * @ingroup talloc
1126 * Talloc contains some handy helpers for handling Arrays conveniently
1128 * @{
1131 #ifdef DOXYGEN
1133 * @brief Allocate an array.
1135 * The macro is equivalent to:
1137 * @code
1138 * (type *)talloc_size(ctx, sizeof(type) * count);
1139 * @endcode
1141 * except that it provides integer overflow protection for the multiply,
1142 * returning NULL if the multiply overflows.
1144 * @param[in] ctx The talloc context to hang the result off.
1146 * @param[in] type The type that we want to allocate.
1148 * @param[in] count The number of 'type' elements you want to allocate.
1150 * @return The allocated result, properly cast to 'type *', NULL on
1151 * error.
1153 * Example:
1154 * @code
1155 * unsigned int *a, *b;
1156 * a = talloc_zero(NULL, unsigned int);
1157 * b = talloc_array(a, unsigned int, 100);
1158 * @endcode
1160 * @see talloc()
1161 * @see talloc_zero_array()
1163 void *talloc_array(const void *ctx, #type, unsigned count);
1164 #else
1165 #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
1166 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
1167 #endif
1169 #ifdef DOXYGEN
1171 * @brief Allocate an array.
1173 * @param[in] ctx The talloc context to hang the result off.
1175 * @param[in] size The size of an array element.
1177 * @param[in] count The number of elements you want to allocate.
1179 * @return The allocated result, NULL on error.
1181 void *talloc_array_size(const void *ctx, size_t size, unsigned count);
1182 #else
1183 #define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
1184 #endif
1186 #ifdef DOXYGEN
1188 * @brief Allocate an array into a typed pointer.
1190 * The macro should be used when you have a pointer to an array and want to
1191 * allocate memory of an array to point at with this pointer. When compiling
1192 * with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
1193 * and talloc_get_name() will return the current location in the source file
1194 * and not the type.
1196 * @param[in] ctx The talloc context to hang the result off.
1198 * @param[in] ptr The pointer you want to assign the result to.
1200 * @param[in] count The number of elements you want to allocate.
1202 * @return The allocated memory chunk, properly casted. NULL on
1203 * error.
1205 void *talloc_array_ptrtype(const void *ctx, const void *ptr, unsigned count);
1206 #else
1207 #define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
1208 #endif
1210 #ifdef DOXYGEN
1212 * @brief Get the number of elements in a talloc'ed array.
1214 * A talloc chunk carries its own size, so for talloc'ed arrays it is not
1215 * necessary to store the number of elements explicitly.
1217 * @param[in] ctx The allocated array.
1219 * @return The number of elements in ctx.
1221 size_t talloc_array_length(const void *ctx);
1222 #else
1223 #define talloc_array_length(ctx) (talloc_get_size(ctx)/sizeof(*ctx))
1224 #endif
1226 #ifdef DOXYGEN
1228 * @brief Allocate a zero-initialized array
1230 * @param[in] ctx The talloc context to hang the result off.
1232 * @param[in] type The type that we want to allocate.
1234 * @param[in] count The number of "type" elements you want to allocate.
1236 * @return The allocated result casted to "type *", NULL on error.
1238 * The talloc_zero_array() macro is equivalent to:
1240 * @code
1241 * ptr = talloc_array(ctx, type, count);
1242 * if (ptr) memset(ptr, 0, sizeof(type) * count);
1243 * @endcode
1245 void *talloc_zero_array(const void *ctx, #type, unsigned count);
1246 #else
1247 #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
1248 void *_talloc_zero_array(const void *ctx,
1249 size_t el_size,
1250 unsigned count,
1251 const char *name);
1252 #endif
1254 #ifdef DOXYGEN
1256 * @brief Change the size of a talloc array.
1258 * The macro changes the size of a talloc pointer. The 'count' argument is the
1259 * number of elements of type 'type' that you want the resulting pointer to
1260 * hold.
1262 * talloc_realloc() has the following equivalences:
1264 * @code
1265 * talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
1266 * talloc_realloc(ctx, NULL, type, N) ==> talloc_array(ctx, type, N);
1267 * talloc_realloc(ctx, ptr, type, 0) ==> talloc_free(ptr);
1268 * @endcode
1270 * The "context" argument is only used if "ptr" is NULL, otherwise it is
1271 * ignored.
1273 * @param[in] ctx The parent context used if ptr is NULL.
1275 * @param[in] ptr The chunk to be resized.
1277 * @param[in] type The type of the array element inside ptr.
1279 * @param[in] count The intended number of array elements.
1281 * @return The new array, NULL on error. The call will fail either
1282 * due to a lack of memory, or because the pointer has more
1283 * than one parent (see talloc_reference()).
1285 void *talloc_realloc(const void *ctx, void *ptr, #type, size_t count);
1286 #else
1287 #define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
1288 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
1289 #endif
1291 #ifdef DOXYGEN
1293 * @brief Untyped realloc to change the size of a talloc array.
1295 * The macro is useful when the type is not known so the typesafe
1296 * talloc_realloc() cannot be used.
1298 * @param[in] ctx The parent context used if 'ptr' is NULL.
1300 * @param[in] ptr The chunk to be resized.
1302 * @param[in] size The new chunk size.
1304 * @return The new array, NULL on error.
1306 void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);
1307 #else
1308 #define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
1309 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
1310 #endif
1313 * @brief Provide a function version of talloc_realloc_size.
1315 * This is a non-macro version of talloc_realloc(), which is useful as
1316 * libraries sometimes want a ralloc function pointer. A realloc()
1317 * implementation encapsulates the functionality of malloc(), free() and
1318 * realloc() in one call, which is why it is useful to be able to pass around
1319 * a single function pointer.
1321 * @param[in] context The parent context used if ptr is NULL.
1323 * @param[in] ptr The chunk to be resized.
1325 * @param[in] size The new chunk size.
1327 * @return The new chunk, NULL on error.
1329 void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
1331 /* @} ******************************************************************/
1334 * @defgroup talloc_string The talloc string functions.
1335 * @ingroup talloc
1337 * talloc string allocation and manipulation functions.
1338 * @{
1342 * @brief Duplicate a string into a talloc chunk.
1344 * This function is equivalent to:
1346 * @code
1347 * ptr = talloc_size(ctx, strlen(p)+1);
1348 * if (ptr) memcpy(ptr, p, strlen(p)+1);
1349 * @endcode
1351 * This functions sets the name of the new pointer to the passed
1352 * string. This is equivalent to:
1354 * @code
1355 * talloc_set_name_const(ptr, ptr)
1356 * @endcode
1358 * @param[in] t The talloc context to hang the result off.
1360 * @param[in] p The string you want to duplicate.
1362 * @return The duplicated string, NULL on error.
1364 char *talloc_strdup(const void *t, const char *p);
1367 * @brief Append a string to given string.
1369 * The destination string is reallocated to take
1370 * <code>strlen(s) + strlen(a) + 1</code> characters.
1372 * This functions sets the name of the new pointer to the new
1373 * string. This is equivalent to:
1375 * @code
1376 * talloc_set_name_const(ptr, ptr)
1377 * @endcode
1379 * If <code>s == NULL</code> then new context is created.
1381 * @param[in] s The destination to append to.
1383 * @param[in] a The string you want to append.
1385 * @return The concatenated strings, NULL on error.
1387 * @see talloc_strdup()
1388 * @see talloc_strdup_append_buffer()
1390 char *talloc_strdup_append(char *s, const char *a);
1393 * @brief Append a string to a given buffer.
1395 * This is a more efficient version of talloc_strdup_append(). It determines the
1396 * length of the destination string by the size of the talloc context.
1398 * Use this very carefully as it produces a different result than
1399 * talloc_strdup_append() when a zero character is in the middle of the
1400 * destination string.
1402 * @code
1403 * char *str_a = talloc_strdup(NULL, "hello world");
1404 * char *str_b = talloc_strdup(NULL, "hello world");
1405 * str_a[5] = str_b[5] = '\0'
1407 * char *app = talloc_strdup_append(str_a, ", hello");
1408 * char *buf = talloc_strdup_append_buffer(str_b, ", hello");
1410 * printf("%s\n", app); // hello, hello (app = "hello, hello")
1411 * printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1412 * @endcode
1414 * If <code>s == NULL</code> then new context is created.
1416 * @param[in] s The destination buffer to append to.
1418 * @param[in] a The string you want to append.
1420 * @return The concatenated strings, NULL on error.
1422 * @see talloc_strdup()
1423 * @see talloc_strdup_append()
1424 * @see talloc_array_length()
1426 char *talloc_strdup_append_buffer(char *s, const char *a);
1429 * @brief Duplicate a length-limited string into a talloc chunk.
1431 * This function is the talloc equivalent of the C library function strndup(3).
1433 * This functions sets the name of the new pointer to the passed string. This is
1434 * equivalent to:
1436 * @code
1437 * talloc_set_name_const(ptr, ptr)
1438 * @endcode
1440 * @param[in] t The talloc context to hang the result off.
1442 * @param[in] p The string you want to duplicate.
1444 * @param[in] n The maximum string length to duplicate.
1446 * @return The duplicated string, NULL on error.
1448 char *talloc_strndup(const void *t, const char *p, size_t n);
1451 * @brief Append at most n characters of a string to given string.
1453 * The destination string is reallocated to take
1454 * <code>strlen(s) + strnlen(a, n) + 1</code> characters.
1456 * This functions sets the name of the new pointer to the new
1457 * string. This is equivalent to:
1459 * @code
1460 * talloc_set_name_const(ptr, ptr)
1461 * @endcode
1463 * If <code>s == NULL</code> then new context is created.
1465 * @param[in] s The destination string to append to.
1467 * @param[in] a The source string you want to append.
1469 * @param[in] n The number of characters you want to append from the
1470 * string.
1472 * @return The concatenated strings, NULL on error.
1474 * @see talloc_strndup()
1475 * @see talloc_strndup_append_buffer()
1477 char *talloc_strndup_append(char *s, const char *a, size_t n);
1480 * @brief Append at most n characters of a string to given buffer
1482 * This is a more efficient version of talloc_strndup_append(). It determines
1483 * the length of the destination string by the size of the talloc context.
1485 * Use this very carefully as it produces a different result than
1486 * talloc_strndup_append() when a zero character is in the middle of the
1487 * destination string.
1489 * @code
1490 * char *str_a = talloc_strdup(NULL, "hello world");
1491 * char *str_b = talloc_strdup(NULL, "hello world");
1492 * str_a[5] = str_b[5] = '\0'
1494 * char *app = talloc_strndup_append(str_a, ", hello", 7);
1495 * char *buf = talloc_strndup_append_buffer(str_b, ", hello", 7);
1497 * printf("%s\n", app); // hello, hello (app = "hello, hello")
1498 * printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1499 * @endcode
1501 * If <code>s == NULL</code> then new context is created.
1503 * @param[in] s The destination buffer to append to.
1505 * @param[in] a The source string you want to append.
1507 * @param[in] n The number of characters you want to append from the
1508 * string.
1510 * @return The concatenated strings, NULL on error.
1512 * @see talloc_strndup()
1513 * @see talloc_strndup_append()
1514 * @see talloc_array_length()
1516 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n);
1519 * @brief Format a string given a va_list.
1521 * This function is the talloc equivalent of the C library function
1522 * vasprintf(3).
1524 * This functions sets the name of the new pointer to the new string. This is
1525 * equivalent to:
1527 * @code
1528 * talloc_set_name_const(ptr, ptr)
1529 * @endcode
1531 * @param[in] t The talloc context to hang the result off.
1533 * @param[in] fmt The format string.
1535 * @param[in] ap The parameters used to fill fmt.
1537 * @return The formatted string, NULL on error.
1539 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1542 * @brief Format a string given a va_list and append it to the given destination
1543 * string.
1545 * @param[in] s The destination string to append to.
1547 * @param[in] fmt The format string.
1549 * @param[in] ap The parameters used to fill fmt.
1551 * @return The formatted string, NULL on error.
1553 * @see talloc_vasprintf()
1555 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1558 * @brief Format a string given a va_list and append it to the given destination
1559 * buffer.
1561 * @param[in] s The destination buffer to append to.
1563 * @param[in] fmt The format string.
1565 * @param[in] ap The parameters used to fill fmt.
1567 * @return The formatted string, NULL on error.
1569 * @see talloc_vasprintf()
1571 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1574 * @brief Format a string.
1576 * This function is the talloc equivalent of the C library function asprintf(3).
1578 * This functions sets the name of the new pointer to the new string. This is
1579 * equivalent to:
1581 * @code
1582 * talloc_set_name_const(ptr, ptr)
1583 * @endcode
1585 * @param[in] t The talloc context to hang the result off.
1587 * @param[in] fmt The format string.
1589 * @param[in] ... The parameters used to fill fmt.
1591 * @return The formatted string, NULL on error.
1593 char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1596 * @brief Append a formatted string to another string.
1598 * This function appends the given formatted string to the given string. Use
1599 * this variant when the string in the current talloc buffer may have been
1600 * truncated in length.
1602 * This functions sets the name of the new pointer to the new
1603 * string. This is equivalent to:
1605 * @code
1606 * talloc_set_name_const(ptr, ptr)
1607 * @endcode
1609 * If <code>s == NULL</code> then new context is created.
1611 * @param[in] s The string to append to.
1613 * @param[in] fmt The format string.
1615 * @param[in] ... The parameters used to fill fmt.
1617 * @return The formatted string, NULL on error.
1619 char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1622 * @brief Append a formatted string to another string.
1624 * This is a more efficient version of talloc_asprintf_append(). It determines
1625 * the length of the destination string by the size of the talloc context.
1627 * Use this very carefully as it produces a different result than
1628 * talloc_asprintf_append() when a zero character is in the middle of the
1629 * destination string.
1631 * @code
1632 * char *str_a = talloc_strdup(NULL, "hello world");
1633 * char *str_b = talloc_strdup(NULL, "hello world");
1634 * str_a[5] = str_b[5] = '\0'
1636 * char *app = talloc_asprintf_append(str_a, "%s", ", hello");
1637 * char *buf = talloc_strdup_append_buffer(str_b, "%s", ", hello");
1639 * printf("%s\n", app); // hello, hello (app = "hello, hello")
1640 * printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1641 * @endcode
1643 * If <code>s == NULL</code> then new context is created.
1645 * @param[in] s The string to append to
1647 * @param[in] fmt The format string.
1649 * @param[in] ... The parameters used to fill fmt.
1651 * @return The formatted string, NULL on error.
1653 * @see talloc_asprintf()
1654 * @see talloc_asprintf_append()
1656 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1658 /* @} ******************************************************************/
1661 * @defgroup talloc_debug The talloc debugging support functions
1662 * @ingroup talloc
1664 * To aid memory debugging, talloc contains routines to inspect the currently
1665 * allocated memory hierarchy.
1667 * @{
1671 * @brief Walk a complete talloc hierarchy.
1673 * This provides a more flexible reports than talloc_report(). It
1674 * will recursively call the callback for the entire tree of memory
1675 * referenced by the pointer. References in the tree are passed with
1676 * is_ref = 1 and the pointer that is referenced.
1678 * You can pass NULL for the pointer, in which case a report is
1679 * printed for the top level memory context, but only if
1680 * talloc_enable_leak_report() or talloc_enable_leak_report_full()
1681 * has been called.
1683 * The recursion is stopped when depth >= max_depth.
1684 * max_depth = -1 means only stop at leaf nodes.
1686 * @param[in] ptr The talloc chunk.
1688 * @param[in] depth Internal parameter to control recursion. Call with 0.
1690 * @param[in] max_depth Maximum recursion level.
1692 * @param[in] callback Function to be called on every chunk.
1694 * @param[in] private_data Private pointer passed to callback.
1696 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1697 void (*callback)(const void *ptr,
1698 int depth, int max_depth,
1699 int is_ref,
1700 void *private_data),
1701 void *private_data);
1704 * @brief Print a talloc hierarchy.
1706 * This provides a more flexible reports than talloc_report(). It
1707 * will let you specify the depth and max_depth.
1709 * @param[in] ptr The talloc chunk.
1711 * @param[in] depth Internal parameter to control recursion. Call with 0.
1713 * @param[in] max_depth Maximum recursion level.
1715 * @param[in] f The file handle to print to.
1717 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
1720 * @brief Print a summary report of all memory used by ptr.
1722 * This provides a more detailed report than talloc_report(). It will
1723 * recursively print the entire tree of memory referenced by the
1724 * pointer. References in the tree are shown by giving the name of the
1725 * pointer that is referenced.
1727 * You can pass NULL for the pointer, in which case a report is printed
1728 * for the top level memory context, but only if
1729 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
1730 * been called.
1732 * @param[in] ptr The talloc chunk.
1734 * @param[in] f The file handle to print to.
1736 * Example:
1737 * @code
1738 * unsigned int *a, *b;
1739 * a = talloc(NULL, unsigned int);
1740 * b = talloc(a, unsigned int);
1741 * fprintf(stderr, "Dumping memory tree for a:\n");
1742 * talloc_report_full(a, stderr);
1743 * @endcode
1745 * @see talloc_report()
1747 void talloc_report_full(const void *ptr, FILE *f);
1750 * @brief Print a summary report of all memory used by ptr.
1752 * This function prints a summary report of all memory used by ptr. One line of
1753 * report is printed for each immediate child of ptr, showing the total memory
1754 * and number of blocks used by that child.
1756 * You can pass NULL for the pointer, in which case a report is printed
1757 * for the top level memory context, but only if talloc_enable_leak_report()
1758 * or talloc_enable_leak_report_full() has been called.
1760 * @param[in] ptr The talloc chunk.
1762 * @param[in] f The file handle to print to.
1764 * Example:
1765 * @code
1766 * unsigned int *a, *b;
1767 * a = talloc(NULL, unsigned int);
1768 * b = talloc(a, unsigned int);
1769 * fprintf(stderr, "Summary of memory tree for a:\n");
1770 * talloc_report(a, stderr);
1771 * @endcode
1773 * @see talloc_report_full()
1775 void talloc_report(const void *ptr, FILE *f);
1778 * @brief Enable tracking the use of NULL memory contexts.
1780 * This enables tracking of the NULL memory context without enabling leak
1781 * reporting on exit. Useful for when you want to do your own leak
1782 * reporting call via talloc_report_null_full();
1784 void talloc_enable_null_tracking(void);
1787 * @brief Enable tracking the use of NULL memory contexts.
1789 * This enables tracking of the NULL memory context without enabling leak
1790 * reporting on exit. Useful for when you want to do your own leak
1791 * reporting call via talloc_report_null_full();
1793 void talloc_enable_null_tracking_no_autofree(void);
1796 * @brief Disable tracking of the NULL memory context.
1798 * This disables tracking of the NULL memory context.
1800 void talloc_disable_null_tracking(void);
1803 * @brief Enable leak report when a program exits.
1805 * This enables calling of talloc_report(NULL, stderr) when the program
1806 * exits. In Samba4 this is enabled by using the --leak-report command
1807 * line option.
1809 * For it to be useful, this function must be called before any other
1810 * talloc function as it establishes a "null context" that acts as the
1811 * top of the tree. If you don't call this function first then passing
1812 * NULL to talloc_report() or talloc_report_full() won't give you the
1813 * full tree printout.
1815 * Here is a typical talloc report:
1817 * @code
1818 * talloc report on 'null_context' (total 267 bytes in 15 blocks)
1819 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
1820 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
1821 * iconv(UTF8,CP850) contains 42 bytes in 2 blocks
1822 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
1823 * iconv(CP850,UTF8) contains 42 bytes in 2 blocks
1824 * iconv(UTF8,UTF-16LE) contains 45 bytes in 2 blocks
1825 * iconv(UTF-16LE,UTF8) contains 45 bytes in 2 blocks
1826 * @endcode
1828 void talloc_enable_leak_report(void);
1831 * @brief Enable full leak report when a program exits.
1833 * This enables calling of talloc_report_full(NULL, stderr) when the
1834 * program exits. In Samba4 this is enabled by using the
1835 * --leak-report-full command line option.
1837 * For it to be useful, this function must be called before any other
1838 * talloc function as it establishes a "null context" that acts as the
1839 * top of the tree. If you don't call this function first then passing
1840 * NULL to talloc_report() or talloc_report_full() won't give you the
1841 * full tree printout.
1843 * Here is a typical full report:
1845 * @code
1846 * full talloc report on 'root' (total 18 bytes in 8 blocks)
1847 * p1 contains 18 bytes in 7 blocks (ref 0)
1848 * r1 contains 13 bytes in 2 blocks (ref 0)
1849 * reference to: p2
1850 * p2 contains 1 bytes in 1 blocks (ref 1)
1851 * x3 contains 1 bytes in 1 blocks (ref 0)
1852 * x2 contains 1 bytes in 1 blocks (ref 0)
1853 * x1 contains 1 bytes in 1 blocks (ref 0)
1854 * @endcode
1856 void talloc_enable_leak_report_full(void);
1859 * @brief Set a custom "abort" function that is called on serious error.
1861 * The default "abort" function is <code>abort()</code>.
1863 * The "abort" function is called when:
1865 * <ul>
1866 * <li>talloc_get_type_abort() fails</li>
1867 * <li>the provided pointer is not a valid talloc context</li>
1868 * <li>when the context meta data are invalid</li>
1869 * <li>when access after free is detected</li>
1870 * </ul>
1872 * Example:
1874 * @code
1875 * void my_abort(const char *reason)
1877 * fprintf(stderr, "talloc abort: %s\n", reason);
1878 * abort();
1881 * talloc_set_abort_fn(my_abort);
1882 * @endcode
1884 * @param[in] abort_fn The new "abort" function.
1886 * @see talloc_set_log_fn()
1887 * @see talloc_get_type()
1889 void talloc_set_abort_fn(void (*abort_fn)(const char *reason));
1892 * @brief Set a logging function.
1894 * @param[in] log_fn The logging function.
1896 * @see talloc_set_log_stderr()
1897 * @see talloc_set_abort_fn()
1899 void talloc_set_log_fn(void (*log_fn)(const char *message));
1902 * @brief Set stderr as the output for logs.
1904 * @see talloc_set_log_fn()
1905 * @see talloc_set_abort_fn()
1907 void talloc_set_log_stderr(void);
1910 * @brief Set a max memory limit for the current context hierarchy
1911 * This affects all children of this context and constrain any
1912 * allocation in the hierarchy to never exceed the limit set.
1913 * The limit can be removed by setting 0 (unlimited) as the
1914 * max_size by calling the function again on the same context.
1915 * Memory limits can also be nested, meaning a child can have
1916 * a stricter memory limit than a parent.
1917 * Memory limits are enforced only at memory allocation time.
1918 * Stealing a context into a 'limited' hierarchy properly
1919 * updates memory usage but does *not* cause failure if the
1920 * move causes the new parent to exceed its limits. However
1921 * any further allocation on that hierarchy will then fail.
1923 * @warning talloc memlimit functionality is deprecated. Please
1924 * consider using cgroup memory limits instead.
1926 * @param[in] ctx The talloc context to set the limit on
1927 * @param[in] max_size The (new) max_size
1929 int talloc_set_memlimit(const void *ctx, size_t max_size) _DEPRECATED_;
1931 /* @} ******************************************************************/
1933 #if TALLOC_DEPRECATED
1934 #define talloc_zero_p(ctx, type) talloc_zero(ctx, type)
1935 #define talloc_p(ctx, type) talloc(ctx, type)
1936 #define talloc_array_p(ctx, type, count) talloc_array(ctx, type, count)
1937 #define talloc_realloc_p(ctx, p, type, count) talloc_realloc(ctx, p, type, count)
1938 #define talloc_destroy(ctx) talloc_free(ctx)
1939 #define talloc_append_string(c, s, a) (s?talloc_strdup_append(s,a):talloc_strdup(c, a))
1940 #endif
1942 #ifndef TALLOC_MAX_DEPTH
1943 #define TALLOC_MAX_DEPTH 10000
1944 #endif
1946 #ifdef __cplusplus
1947 } /* end of extern "C" */
1948 #endif
1950 #endif