s4:ldb:python/api: use only one ldb file in test_contains()
[Samba/gebeck_regimport.git] / lib / talloc / talloc.h
blobbc5b0fae2a53949d312957fcf46d3078818ab964
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 0
48 int talloc_version_major(void);
49 int talloc_version_minor(void);
51 /**
52 * @brief Define a talloc parent type
54 * As talloc is a hierarchial memory allocator, every talloc chunk is a
55 * potential parent to other talloc chunks. So defining a separate type for a
56 * talloc chunk is not strictly necessary. TALLOC_CTX is defined nevertheless,
57 * as it provides an indicator for function arguments. You will frequently
58 * write code like
60 * @code
61 * struct foo *foo_create(TALLOC_CTX *mem_ctx)
62 * {
63 * struct foo *result;
64 * result = talloc(mem_ctx, struct foo);
65 * if (result == NULL) return NULL;
66 * ... initialize foo ...
67 * return result;
68 * }
69 * @endcode
71 * In this type of allocating functions it is handy to have a general
72 * TALLOC_CTX type to indicate which parent to put allocated structures on.
74 typedef void TALLOC_CTX;
77 this uses a little trick to allow __LINE__ to be stringified
79 #ifndef __location__
80 #define __TALLOC_STRING_LINE1__(s) #s
81 #define __TALLOC_STRING_LINE2__(s) __TALLOC_STRING_LINE1__(s)
82 #define __TALLOC_STRING_LINE3__ __TALLOC_STRING_LINE2__(__LINE__)
83 #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__
84 #endif
86 #ifndef TALLOC_DEPRECATED
87 #define TALLOC_DEPRECATED 0
88 #endif
90 #ifndef PRINTF_ATTRIBUTE
91 #if (__GNUC__ >= 3)
92 /** Use gcc attribute to check printf fns. a1 is the 1-based index of
93 * the parameter containing the format, and a2 the index of the first
94 * argument. Note that some gcc 2.x versions don't handle this
95 * properly **/
96 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
97 #else
98 #define PRINTF_ATTRIBUTE(a1, a2)
99 #endif
100 #endif
102 #ifdef DOXYGEN
104 * @brief Create a new talloc context.
106 * The talloc() macro is the core of the talloc library. It takes a memory
107 * context and a type, and returns a pointer to a new area of memory of the
108 * given type.
110 * The returned pointer is itself a talloc context, so you can use it as the
111 * context argument to more calls to talloc if you wish.
113 * The returned pointer is a "child" of the supplied context. This means that if
114 * you talloc_free() the context then the new child disappears as well.
115 * Alternatively you can free just the child.
117 * @param[in] ctx A talloc context to create a new reference on or NULL to
118 * create a new top level context.
120 * @param[in] type The type of memory to allocate.
122 * @return A type casted talloc context or NULL on error.
124 * @code
125 * unsigned int *a, *b;
127 * a = talloc(NULL, unsigned int);
128 * b = talloc(a, unsigned int);
129 * @endcode
131 * @see talloc_zero
132 * @see talloc_array
133 * @see talloc_steal
134 * @see talloc_free
136 void *talloc(const void *ctx, #type);
137 #else
138 #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
139 void *_talloc(const void *context, size_t size);
140 #endif
143 * @brief Create a new top level talloc context.
145 * This function creates a zero length named talloc context as a top level
146 * context. It is equivalent to:
148 * @code
149 * talloc_named(NULL, 0, fmt, ...);
150 * @endcode
151 * @param[in] fmt Format string for the name.
153 * @param[in] ... Additional printf-style arguments.
155 * @return The allocated memory chunk, NULL on error.
157 * @see talloc_named()
159 void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
161 #ifdef DOXYGEN
163 * @brief Free a chunk of talloc memory.
165 * The talloc_free() function frees a piece of talloc memory, and all its
166 * children. You can call talloc_free() on any pointer returned by
167 * talloc().
169 * The return value of talloc_free() indicates success or failure, with 0
170 * returned for success and -1 for failure. A possible failure condition
171 * is if the pointer had a destructor attached to it and the destructor
172 * returned -1. See talloc_set_destructor() for details on
173 * destructors. Likewise, if "ptr" is NULL, then the function will make
174 * no modifications and return -1.
176 * If this pointer has an additional parent when talloc_free() is called
177 * then the memory is not actually released, but instead the most
178 * recently established parent is destroyed. See talloc_reference() for
179 * details on establishing additional parents.
181 * For more control on which parent is removed, see talloc_unlink()
183 * talloc_free() operates recursively on its children.
185 * From the 2.0 version of talloc, as a special case, talloc_free() is
186 * refused on pointers that have more than one parent, as talloc would
187 * have no way of knowing which parent should be removed. To free a
188 * pointer that has more than one parent please use talloc_unlink().
190 * To help you find problems in your code caused by this behaviour, if
191 * you do try and free a pointer with more than one parent then the
192 * talloc logging function will be called to give output like this:
194 * @code
195 * ERROR: talloc_free with references at some_dir/source/foo.c:123
196 * reference at some_dir/source/other.c:325
197 * reference at some_dir/source/third.c:121
198 * @endcode
200 * Please see the documentation for talloc_set_log_fn() and
201 * talloc_set_log_stderr() for more information on talloc logging
202 * functions.
204 * @param[in] ptr The chunk to be freed.
206 * @return Returns 0 on success and -1 on error. A possible
207 * failure condition is if the pointer had a destructor
208 * attached to it and the destructor returned -1. Likewise,
209 * if "ptr" is NULL, then the function will make no
210 * modifications and returns -1.
212 * Example:
213 * @code
214 * unsigned int *a, *b;
215 * a = talloc(NULL, unsigned int);
216 * b = talloc(a, unsigned int);
218 * talloc_free(a); // Frees a and b
219 * @endcode
221 * @see talloc_set_destructor()
222 * @see talloc_unlink()
224 int talloc_free(void *ptr);
225 #else
226 #define talloc_free(ctx) _talloc_free(ctx, __location__)
227 int _talloc_free(void *ptr, const char *location);
228 #endif
231 * @brief Free a talloc chunk's children.
233 * The function walks along the list of all children of a talloc context and
234 * talloc_free()s only the children, not the context itself.
236 * @param[in] ptr The chunk that you want to free the children of.
238 void talloc_free_children(void *ptr);
240 #ifdef DOXYGEN
242 * @brief Assign a destructor function to be called when a chunk is freed.
244 * The function talloc_set_destructor() sets the "destructor" for the pointer
245 * "ptr". A destructor is a function that is called when the memory used by a
246 * pointer is about to be released. The destructor receives the pointer as an
247 * argument, and should return 0 for success and -1 for failure.
249 * The destructor can do anything it wants to, including freeing other pieces
250 * of memory. A common use for destructors is to clean up operating system
251 * resources (such as open file descriptors) contained in the structure the
252 * destructor is placed on.
254 * You can only place one destructor on a pointer. If you need more than one
255 * destructor then you can create a zero-length child of the pointer and place
256 * an additional destructor on that.
258 * To remove a destructor call talloc_set_destructor() with NULL for the
259 * destructor.
261 * If your destructor attempts to talloc_free() the pointer that it is the
262 * destructor for then talloc_free() will return -1 and the free will be
263 * ignored. This would be a pointless operation anyway, as the destructor is
264 * only called when the memory is just about to go away.
266 * @param[in] ptr The talloc chunk to add a destructor to.
268 * @param[in] destructor The destructor function to be called. NULL to remove
269 * it.
271 * Example:
272 * @code
273 * static int destroy_fd(int *fd) {
274 * close(*fd);
275 * return 0;
278 * int *open_file(const char *filename) {
279 * int *fd = talloc(NULL, int);
280 * *fd = open(filename, O_RDONLY);
281 * if (*fd < 0) {
282 * talloc_free(fd);
283 * return NULL;
285 * // Whenever they free this, we close the file.
286 * talloc_set_destructor(fd, destroy_fd);
287 * return fd;
289 * @endcode
291 * @see talloc()
292 * @see talloc_free()
294 void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
297 * @brief Change a talloc chunk's parent.
299 * The talloc_steal() function changes the parent context of a talloc
300 * pointer. It is typically used when the context that the pointer is
301 * currently a child of is going to be freed and you wish to keep the
302 * memory for a longer time.
304 * To make the changed hierarchy less error-prone, you might consider to use
305 * talloc_move().
307 * If you try and call talloc_steal() on a pointer that has more than one
308 * parent then the result is ambiguous. Talloc will choose to remove the
309 * parent that is currently indicated by talloc_parent() and replace it with
310 * the chosen parent. You will also get a message like this via the talloc
311 * logging functions:
313 * @code
314 * WARNING: talloc_steal with references at some_dir/source/foo.c:123
315 * reference at some_dir/source/other.c:325
316 * reference at some_dir/source/third.c:121
317 * @endcode
319 * To unambiguously change the parent of a pointer please see the function
320 * talloc_reparent(). See the talloc_set_log_fn() documentation for more
321 * information on talloc logging.
323 * @param[in] new_ctx The new parent context.
325 * @param[in] ptr The talloc chunk to move.
327 * @return Returns the pointer that you pass it. It does not have
328 * any failure modes.
330 * @note It is possible to produce loops in the parent/child relationship
331 * if you are not careful with talloc_steal(). No guarantees are provided
332 * as to your sanity or the safety of your data if you do this.
334 void *talloc_steal(const void *new_ctx, const void *ptr);
335 #else /* DOXYGEN */
336 /* try to make talloc_set_destructor() and talloc_steal() type safe,
337 if we have a recent gcc */
338 #if (__GNUC__ >= 3)
339 #define _TALLOC_TYPEOF(ptr) __typeof__(ptr)
340 #define talloc_set_destructor(ptr, function) \
341 do { \
342 int (*_talloc_destructor_fn)(_TALLOC_TYPEOF(ptr)) = (function); \
343 _talloc_set_destructor((ptr), (int (*)(void *))_talloc_destructor_fn); \
344 } while(0)
345 /* this extremely strange macro is to avoid some braindamaged warning
346 stupidity in gcc 4.1.x */
347 #define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__); __talloc_steal_ret; })
348 #else /* __GNUC__ >= 3 */
349 #define talloc_set_destructor(ptr, function) \
350 _talloc_set_destructor((ptr), (int (*)(void *))(function))
351 #define _TALLOC_TYPEOF(ptr) void *
352 #define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__)
353 #endif /* __GNUC__ >= 3 */
354 void _talloc_set_destructor(const void *ptr, int (*_destructor)(void *));
355 void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location);
356 #endif /* DOXYGEN */
359 * @brief Assign a name to a talloc chunk.
361 * Each talloc pointer has a "name". The name is used principally for
362 * debugging purposes, although it is also possible to set and get the name on
363 * a pointer in as a way of "marking" pointers in your code.
365 * The main use for names on pointer is for "talloc reports". See
366 * talloc_report() and talloc_report_full() for details. Also see
367 * talloc_enable_leak_report() and talloc_enable_leak_report_full().
369 * The talloc_set_name() function allocates memory as a child of the
370 * pointer. It is logically equivalent to:
372 * @code
373 * talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
374 * @endcode
376 * @param[in] ptr The talloc chunk to assign a name to.
378 * @param[in] fmt Format string for the name.
380 * @param[in] ... Add printf-style additional arguments.
382 * @return The assigned name, NULL on error.
384 * @note Multiple calls to talloc_set_name() will allocate more memory without
385 * releasing the name. All of the memory is released when the ptr is freed
386 * using talloc_free().
388 const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
390 #ifdef DOXYGEN
392 * @brief Change a talloc chunk's parent.
394 * This function has the same effect as talloc_steal(), and additionally sets
395 * the source pointer to NULL. You would use it like this:
397 * @code
398 * struct foo *X = talloc(tmp_ctx, struct foo);
399 * struct foo *Y;
400 * Y = talloc_move(new_ctx, &X);
401 * @endcode
403 * @param[in] new_ctx The new parent context.
405 * @param[in] ptr Pointer to the talloc chunk to move.
407 * @return The pointer of the talloc chunk it has been moved to,
408 * NULL on error.
410 void *talloc_move(const void *new_ctx, const void *ptr);
411 #else
412 #define talloc_move(ctx, ptr) (_TALLOC_TYPEOF(*(ptr)))_talloc_move((ctx),(void *)(ptr))
413 void *_talloc_move(const void *new_ctx, const void *pptr);
414 #endif
417 * @brief Assign a name to a talloc chunk.
419 * The function is just like talloc_set_name(), but it takes a string constant,
420 * and is much faster. It is extensively used by the "auto naming" macros, such
421 * as talloc_p().
423 * This function does not allocate any memory. It just copies the supplied
424 * pointer into the internal representation of the talloc ptr. This means you
425 * must not pass a name pointer to memory that will disappear before the ptr
426 * is freed with talloc_free().
428 * @param[in] ptr The talloc chunk to assign a name to.
430 * @param[in] name Format string for the name.
432 void talloc_set_name_const(const void *ptr, const char *name);
435 * @brief Create a named talloc chunk.
437 * The talloc_named() function creates a named talloc pointer. It is
438 * equivalent to:
440 * @code
441 * ptr = talloc_size(context, size);
442 * talloc_set_name(ptr, fmt, ....);
443 * @endcode
445 * @param[in] context The talloc context to hang the result off.
447 * @param[in] size Number of char's that you want to allocate.
449 * @param[in] fmt Format string for the name.
451 * @param[in] ... Additional printf-style arguments.
453 * @return The allocated memory chunk, NULL on error.
455 * @see talloc_set_name()
457 void *talloc_named(const void *context, size_t size,
458 const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
461 * @brief Basic routine to allocate a chunk of memory.
463 * This is equivalent to:
465 * @code
466 * ptr = talloc_size(context, size);
467 * talloc_set_name_const(ptr, name);
468 * @endcode
470 * @param[in] context The parent context.
472 * @param[in] size The number of char's that we want to allocate.
474 * @param[in] name The name the talloc block has.
476 * @return The allocated memory chunk, NULL on error.
478 void *talloc_named_const(const void *context, size_t size, const char *name);
480 #ifdef DOXYGEN
482 * @brief Untyped allocation.
484 * The function should be used when you don't have a convenient type to pass to
485 * talloc(). Unlike talloc(), it is not type safe (as it returns a void *), so
486 * you are on your own for type checking.
488 * Best to use talloc() or talloc_array() instead.
490 * @param[in] ctx The talloc context to hang the result off.
492 * @param[in] size Number of char's that you want to allocate.
494 * @return The allocated memory chunk, NULL on error.
496 * Example:
497 * @code
498 * void *mem = talloc_size(NULL, 100);
499 * @endcode
501 void *talloc_size(const void *ctx, size_t size);
502 #else
503 #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
504 #endif
506 #ifdef DOXYGEN
508 * @brief Allocate into a typed pointer.
510 * The talloc_ptrtype() macro should be used when you have a pointer and want
511 * to allocate memory to point at with this pointer. When compiling with
512 * gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size() and
513 * talloc_get_name() will return the current location in the source file and
514 * not the type.
516 * @param[in] ctx The talloc context to hang the result off.
518 * @param[in] type The pointer you want to assign the result to.
520 * @return The properly casted allocated memory chunk, NULL on
521 * error.
523 * Example:
524 * @code
525 * unsigned int *a = talloc_ptrtype(NULL, a);
526 * @endcode
528 void *talloc_ptrtype(const void *ctx, #type);
529 #else
530 #define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
531 #endif
533 #ifdef DOXYGEN
535 * @brief Allocate a new 0-sized talloc chunk.
537 * This is a utility macro that creates a new memory context hanging off an
538 * existing context, automatically naming it "talloc_new: __location__" where
539 * __location__ is the source line it is called from. It is particularly
540 * useful for creating a new temporary working context.
542 * @param[in] ctx The talloc parent context.
544 * @return A new talloc chunk, NULL on error.
546 void *talloc_new(const void *ctx);
547 #else
548 #define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
549 #endif
551 #ifdef DOXYGEN
553 * @brief Allocate a 0-initizialized structure.
555 * The macro is equivalent to:
557 * @code
558 * ptr = talloc(ctx, type);
559 * if (ptr) memset(ptr, 0, sizeof(type));
560 * @endcode
562 * @param[in] ctx The talloc context to hang the result off.
564 * @param[in] type The type that we want to allocate.
566 * @return Pointer to a piece of memory, properly cast to 'type *',
567 * NULL on error.
569 * Example:
570 * @code
571 * unsigned int *a, *b;
572 * a = talloc_zero(NULL, unsigned int);
573 * b = talloc_zero(a, unsigned int);
574 * @endcode
576 * @see talloc()
577 * @see talloc_zero_size()
578 * @see talloc_zero_array()
580 void *talloc_zero(const void *ctx, #type);
583 * @brief Allocate untyped, 0-initialized memory.
585 * @param[in] ctx The talloc context to hang the result off.
587 * @param[in] size Number of char's that you want to allocate.
589 * @return The allocated memory chunk.
591 void *talloc_zero_size(const void *ctx, size_t size);
592 #else
593 #define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
594 #define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
595 void *_talloc_zero(const void *ctx, size_t size, const char *name);
596 #endif
599 * @brief Return the name of a talloc chunk.
601 * @param[in] ptr The talloc chunk.
603 * @return The current name for the given talloc pointer.
605 * @see talloc_set_name()
607 const char *talloc_get_name(const void *ptr);
610 * @brief Verify that a talloc chunk carries a specified name.
612 * This function checks if a pointer has the specified name. If it does
613 * then the pointer is returned.
615 * @param[in] ptr The talloc chunk to check.
617 * @param[in] name The name to check against.
619 * @return The pointer if the name matches, NULL if it doesn't.
621 void *talloc_check_name(const void *ptr, const char *name);
624 * @brief Get the parent chunk of a pointer.
626 * @param[in] ptr The talloc pointer to inspect.
628 * @return The talloc parent of ptr, NULL on error.
630 void *talloc_parent(const void *ptr);
633 * @brief Get a talloc chunk's parent name.
635 * @param[in] ptr The talloc pointer to inspect.
637 * @return The name of ptr's parent chunk.
639 const char *talloc_parent_name(const void *ptr);
642 * @brief Get the total size of a talloc chunk including its children.
644 * The function returns the total size in bytes used by this pointer and all
645 * child pointers. Mostly useful for debugging.
647 * Passing NULL is allowed, but it will only give a meaningful result if
648 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
649 * been called.
651 * @param[in] ptr The talloc chunk.
653 * @return The total size.
655 size_t talloc_total_size(const void *ptr);
658 * @brief Get the number of talloc chunks hanging off a chunk.
660 * The talloc_total_blocks() function returns the total memory block
661 * count used by this pointer and all child pointers. Mostly useful for
662 * debugging.
664 * Passing NULL is allowed, but it will only give a meaningful result if
665 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
666 * been called.
668 * @param[in] ptr The talloc chunk.
670 * @return The total size.
672 size_t talloc_total_blocks(const void *ptr);
674 #ifdef DOXYGEN
676 * @brief Duplicate a memory area into a talloc chunk.
678 * The function is equivalent to:
680 * @code
681 * ptr = talloc_size(ctx, size);
682 * if (ptr) memcpy(ptr, p, size);
683 * @endcode
685 * @param[in] t The talloc context to hang the result off.
687 * @param[in] p The memory chunk you want to duplicate.
689 * @param[in] size Number of char's that you want copy.
691 * @return The allocated memory chunk.
693 * @see talloc_size()
695 void *talloc_memdup(const void *t, const void *p, size_t size);
696 #else
697 #define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
698 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
699 #endif
701 #ifdef DOXYGEN
703 * @brief Assign a type to a talloc chunk.
705 * This macro allows you to force the name of a pointer to be a particular type.
706 * This can be used in conjunction with talloc_get_type() to do type checking on
707 * void* pointers.
709 * It is equivalent to this:
711 * @code
712 * talloc_set_name_const(ptr, #type)
713 * @endcode
715 * @param[in] ptr The talloc chunk to assign the type to.
717 * @param[in] type The type to assign.
719 void talloc_set_type(const char *ptr, #type);
722 * @brief Get a typed pointer out of a talloc pointer.
724 * This macro allows you to do type checking on talloc pointers. It is
725 * particularly useful for void* private pointers. It is equivalent to
726 * this:
728 * @code
729 * (type *)talloc_check_name(ptr, #type)
730 * @endcode
732 * @param[in] ptr The talloc pointer to check.
734 * @param[in] type The type to check against.
736 * @return The properly casted pointer given by ptr, NULL on error.
738 type *talloc_get_type(const void *ptr, #type);
739 #else
740 #define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
741 #define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
742 #endif
744 #ifdef DOXYGEN
746 * @brief Safely turn a void pointer into a typed pointer.
748 * This macro is used together with talloc(mem_ctx, struct foo). If you had to
749 * assing the talloc chunk pointer to some void pointer variable,
750 * talloc_get_type_abort() is the recommended way to get the convert the void
751 * pointer back to a typed pointer.
753 * @param[in] ptr The void pointer to convert.
755 * @param[in] type The type that this chunk contains
757 * @return The same value as ptr, type-checked and properly cast.
759 void *talloc_get_type_abort(const void *ptr, #type);
760 #else
761 #define talloc_get_type_abort(ptr, type) (type *)_talloc_get_type_abort(ptr, #type, __location__)
762 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location);
763 #endif
766 * @brief Find a parent context by name.
768 * Find a parent memory context of the current context that has the given
769 * name. This can be very useful in complex programs where it may be
770 * difficult to pass all information down to the level you need, but you
771 * know the structure you want is a parent of another context.
773 * @param[in] ctx The talloc chunk to start from.
775 * @param[in] name The name of the parent we look for.
777 * @return The memory context we are looking for, NULL if not
778 * found.
780 void *talloc_find_parent_byname(const void *ctx, const char *name);
782 #ifdef DOXYGEN
784 * @brief Find a parent context by type.
786 * Find a parent memory context of the current context that has the given
787 * name. This can be very useful in complex programs where it may be
788 * difficult to pass all information down to the level you need, but you
789 * know the structure you want is a parent of another context.
791 * Like talloc_find_parent_byname() but takes a type, making it typesafe.
793 * @param[in] ptr The talloc chunk to start from.
795 * @param[in] type The type of the parent to look for.
797 * @return The memory context we are looking for, NULL if not
798 * found.
800 void *talloc_find_parent_bytype(const void *ptr, #type);
801 #else
802 #define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
803 #endif
806 * @brief Allocate a talloc pool.
808 * A talloc pool is a pure optimization for specific situations. In the
809 * release process for Samba 3.2 we found out that we had become considerably
810 * slower than Samba 3.0 was. Profiling showed that malloc(3) was a large CPU
811 * consumer in benchmarks. For Samba 3.2 we have internally converted many
812 * static buffers to dynamically allocated ones, so malloc(3) being beaten
813 * more was no surprise. But it made us slower.
815 * talloc_pool() is an optimization to call malloc(3) a lot less for the use
816 * pattern Samba has: The SMB protocol is mainly a request/response protocol
817 * where we have to allocate a certain amount of memory per request and free
818 * that after the SMB reply is sent to the client.
820 * talloc_pool() creates a talloc chunk that you can use as a talloc parent
821 * exactly as you would use any other ::TALLOC_CTX. The difference is that
822 * when you talloc a child of this pool, no malloc(3) is done. Instead, talloc
823 * just increments a pointer inside the talloc_pool. This also works
824 * recursively. If you use the child of the talloc pool as a parent for
825 * grand-children, their memory is also taken from the talloc pool.
827 * If you talloc_free() children of a talloc pool, the memory is not given
828 * back to the system. Instead, free(3) is only called if the talloc_pool()
829 * itself is released with talloc_free().
831 * The downside of a talloc pool is that if you talloc_move() a child of a
832 * talloc pool to a talloc parent outside the pool, the whole pool memory is
833 * not free(3)'ed until that moved chunk is also talloc_free()ed.
835 * @param[in] context The talloc context to hang the result off.
837 * @param[in] size Size of the talloc pool.
839 * @return The allocated talloc pool, NULL on error.
841 void *talloc_pool(const void *context, size_t size);
844 * @brief Free a talloc chunk and NULL out the pointer.
846 * TALLOC_FREE() frees a pointer and sets it to NULL. Use this if you want
847 * immediate feedback (i.e. crash) if you use a pointer after having free'ed
848 * it.
850 * @param[in] ctx The chunk to be freed.
852 #define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
854 /* @} ******************************************************************/
857 * \defgroup talloc_ref The talloc reference function.
858 * @ingroup talloc
860 * This module contains the definitions around talloc references
862 * @{
866 * @brief Increase the reference count of a talloc chunk.
868 * The talloc_increase_ref_count(ptr) function is exactly equivalent to:
870 * @code
871 * talloc_reference(NULL, ptr);
872 * @endcode
874 * You can use either syntax, depending on which you think is clearer in
875 * your code.
877 * @param[in] ptr The pointer to increase the reference count.
879 * @return 0 on success, -1 on error.
881 int talloc_increase_ref_count(const void *ptr);
884 * @brief Get the number of references to a talloc chunk.
886 * @param[in] ptr The pointer to retrieve the reference count from.
888 * @return The number of references.
890 size_t talloc_reference_count(const void *ptr);
892 #ifdef DOXYGEN
894 * @brief Create an additional talloc parent to a pointer.
896 * The talloc_reference() function makes "context" an additional parent of
897 * ptr. Each additional reference consumes around 48 bytes of memory on intel
898 * x86 platforms.
900 * If ptr is NULL, then the function is a no-op, and simply returns NULL.
902 * After creating a reference you can free it in one of the following ways:
904 * - you can talloc_free() any parent of the original pointer. That
905 * will reduce the number of parents of this pointer by 1, and will
906 * cause this pointer to be freed if it runs out of parents.
908 * - you can talloc_free() the pointer itself. That will destroy the
909 * most recently established parent to the pointer and leave the
910 * pointer as a child of its current parent.
912 * For more control on which parent to remove, see talloc_unlink()
913 * @param[in] ctx The additional parent.
915 * @param[in] ptr The pointer you want to create an additional parent for.
917 * @return The original pointer 'ptr', NULL if talloc ran out of
918 * memory in creating the reference.
920 * Example:
921 * @code
922 * unsigned int *a, *b, *c;
923 * a = talloc(NULL, unsigned int);
924 * b = talloc(NULL, unsigned int);
925 * c = talloc(a, unsigned int);
926 * // b also serves as a parent of c.
927 * talloc_reference(b, c);
928 * @endcode
930 * @see talloc_unlink()
932 void *talloc_reference(const void *ctx, const void *ptr);
933 #else
934 #define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference_loc((ctx),(ptr), __location__)
935 void *_talloc_reference_loc(const void *context, const void *ptr, const char *location);
936 #endif
939 * @brief Remove a specific parent from a talloc chunk.
941 * The function removes a specific parent from ptr. The context passed must
942 * either be a context used in talloc_reference() with this pointer, or must be
943 * a direct parent of ptr.
945 * Usually you can just use talloc_free() instead of talloc_unlink(), but
946 * sometimes it is useful to have the additional control on which parent is
947 * removed.
949 * @param[in] context The talloc parent to remove.
951 * @param[in] ptr The talloc ptr you want to remove the parent from.
953 * @return 0 on success, -1 on error.
955 * @note If the parent has already been removed using talloc_free() then
956 * this function will fail and will return -1. Likewise, if ptr is NULL,
957 * then the function will make no modifications and return -1.
959 * Example:
960 * @code
961 * unsigned int *a, *b, *c;
962 * a = talloc(NULL, unsigned int);
963 * b = talloc(NULL, unsigned int);
964 * c = talloc(a, unsigned int);
965 * // b also serves as a parent of c.
966 * talloc_reference(b, c);
967 * talloc_unlink(b, c);
968 * @endcode
970 int talloc_unlink(const void *context, void *ptr);
973 * @brief Provide a talloc context that is freed at program exit.
975 * This is a handy utility function that returns a talloc context
976 * which will be automatically freed on program exit. This can be used
977 * to reduce the noise in memory leak reports.
979 * Never use this in code that might be used in objects loaded with
980 * dlopen and unloaded with dlclose. talloc_autofree_context()
981 * internally uses atexit(3). Some platforms like modern Linux handles
982 * this fine, but for example FreeBSD does not deal well with dlopen()
983 * and atexit() used simultaneously: dlclose() does not clean up the
984 * list of atexit-handlers, so when the program exits the code that
985 * was registered from within talloc_autofree_context() is gone, the
986 * program crashes at exit.
988 * @return A talloc context, NULL on error.
990 void *talloc_autofree_context(void);
993 * @brief Get the size of a talloc chunk.
995 * This function lets you know the amount of memory alloced so far by
996 * this context. It does NOT account for subcontext memory.
997 * This can be used to calculate the size of an array.
999 * @param[in] ctx The talloc chunk.
1001 * @return The size of the talloc chunk.
1003 size_t talloc_get_size(const void *ctx);
1006 * @brief Show the parentage of a context.
1008 * @param[in] context The talloc context to look at.
1010 * @param[in] file The output to use, a file, stdout or stderr.
1012 void talloc_show_parents(const void *context, FILE *file);
1015 * @brief Check if a context is parent of a talloc chunk.
1017 * This checks if context is referenced in the talloc hierarchy above ptr.
1019 * @param[in] context The assumed talloc context.
1021 * @param[in] ptr The talloc chunk to check.
1023 * @return Return 1 if this is the case, 0 if not.
1025 int talloc_is_parent(const void *context, const void *ptr);
1028 * @brief Change the parent context of a talloc pointer.
1030 * The function changes the parent context of a talloc pointer. It is typically
1031 * used when the context that the pointer is currently a child of is going to be
1032 * freed and you wish to keep the memory for a longer time.
1034 * The difference between talloc_reparent() and talloc_steal() is that
1035 * talloc_reparent() can specify which parent you wish to change. This is
1036 * useful when a pointer has multiple parents via references.
1038 * @param[in] old_parent
1039 * @param[in] new_parent
1040 * @param[in] ptr
1042 * @return Return the pointer you passed. It does not have any
1043 * failure modes.
1045 void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr);
1047 /* @} ******************************************************************/
1050 * @defgroup talloc_array The talloc array functions
1051 * @ingroup talloc
1053 * Talloc contains some handy helpers for handling Arrays conveniently
1055 * @{
1058 #ifdef DOXYGEN
1060 * @brief Allocate an array.
1062 * The macro is equivalent to:
1064 * @code
1065 * (type *)talloc_size(ctx, sizeof(type) * count);
1066 * @endcode
1068 * except that it provides integer overflow protection for the multiply,
1069 * returning NULL if the multiply overflows.
1071 * @param[in] ctx The talloc context to hang the result off.
1073 * @param[in] type The type that we want to allocate.
1075 * @param[in] count The number of 'type' elements you want to allocate.
1077 * @return The allocated result, properly cast to 'type *', NULL on
1078 * error.
1080 * Example:
1081 * @code
1082 * unsigned int *a, *b;
1083 * a = talloc_zero(NULL, unsigned int);
1084 * b = talloc_array(a, unsigned int, 100);
1085 * @endcode
1087 * @see talloc()
1088 * @see talloc_array_zero()
1090 void *talloc_array(const void *ctx, #type, unsigned count);
1091 #else
1092 #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
1093 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
1094 #endif
1096 #ifdef DOXYGEN
1098 * @brief Allocate an array.
1100 * @param[in] ctx The talloc context to hang the result off.
1102 * @param[in] size The size of an array element.
1104 * @param[in] count The number of elements you want to allocate.
1106 * @return The allocated result, NULL on error.
1108 void *talloc_array_size(const void *ctx, size_t size, unsigned count);
1109 #else
1110 #define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
1111 #endif
1113 #ifdef DOXYGEN
1115 * @brief Allocate an array into a typed pointer.
1117 * The macro should be used when you have a pointer to an array and want to
1118 * allocate memory of an array to point at with this pointer. When compiling
1119 * with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
1120 * and talloc_get_name() will return the current location in the source file
1121 * and not the type.
1123 * @param[in] ctx The talloc context to hang the result off.
1125 * @param[in] ptr The pointer you want to assign the result to.
1127 * @param[in] count The number of elements you want to allocate.
1129 * @return The allocated memory chunk, properly casted. NULL on
1130 * error.
1132 void *talloc_array_ptrtype(const void *ctx, const void *ptr, unsigned count);
1133 #else
1134 #define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
1135 #endif
1137 #ifdef DOXYGEN
1139 * @brief Get the number of elements in a talloc'ed array.
1141 * A talloc chunk carries its own size, so for talloc'ed arrays it is not
1142 * necessary to store the number of elements explicitly.
1144 * @param[in] ctx The allocated array.
1146 * @return The number of elements in ctx.
1148 size_t talloc_array_length(const void *ctx);
1149 #else
1150 #define talloc_array_length(ctx) (talloc_get_size(ctx)/sizeof(*ctx))
1151 #endif
1153 #ifdef DOXYGEN
1155 * @brief Allocate a zero-initialized array
1157 * @param[in] ctx The talloc context to hang the result off.
1159 * @param[in] type The type that we want to allocate.
1161 * @param[in] count The number of "type" elements you want to allocate.
1163 * @return The allocated result casted to "type *", NULL on error.
1165 * The talloc_zero_array() macro is equivalent to:
1167 * @code
1168 * ptr = talloc_array(ctx, type, count);
1169 * if (ptr) memset(ptr, sizeof(type) * count);
1170 * @endcode
1172 void *talloc_zero_array(const void *ctx, #type, unsigned count);
1173 #else
1174 #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
1175 void *_talloc_zero_array(const void *ctx,
1176 size_t el_size,
1177 unsigned count,
1178 const char *name);
1179 #endif
1181 #ifdef DOXYGEN
1183 * @brief Change the size of a talloc array.
1185 * The macro changes the size of a talloc pointer. The 'count' argument is the
1186 * number of elements of type 'type' that you want the resulting pointer to
1187 * hold.
1189 * talloc_realloc() has the following equivalences:
1191 * @code
1192 * talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
1193 * talloc_realloc(ctx, NULL, type, N) ==> talloc_array(ctx, type, N);
1194 * talloc_realloc(ctx, ptr, type, 0) ==> talloc_free(ptr);
1195 * @endcode
1197 * The "context" argument is only used if "ptr" is NULL, otherwise it is
1198 * ignored.
1200 * @param[in] ctx The parent context used if ptr is NULL.
1202 * @param[in] ptr The chunk to be resized.
1204 * @param[in] type The type of the array element inside ptr.
1206 * @param[in] count The intended number of array elements.
1208 * @return The new array, NULL on error. The call will fail either
1209 * due to a lack of memory, or because the pointer has more
1210 * than one parent (see talloc_reference()).
1212 void *talloc_realloc(const void *ctx, void *ptr, #type, size_t count);
1213 #else
1214 #define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
1215 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
1216 #endif
1218 #ifdef DOXYGEN
1220 * @brief Untyped realloc to change the size of a talloc array.
1222 * The macro is useful when the type is not known so the typesafe
1223 * talloc_realloc() cannot be used.
1225 * @param[in] ctx The parent context used if 'ptr' is NULL.
1227 * @param[in] ptr The chunk to be resized.
1229 * @param[in] size The new chunk size.
1231 * @return The new array, NULL on error.
1233 void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);
1234 #else
1235 #define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
1236 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
1237 #endif
1240 * @brief Provide a function version of talloc_realloc_size.
1242 * This is a non-macro version of talloc_realloc(), which is useful as
1243 * libraries sometimes want a ralloc function pointer. A realloc()
1244 * implementation encapsulates the functionality of malloc(), free() and
1245 * realloc() in one call, which is why it is useful to be able to pass around
1246 * a single function pointer.
1248 * @param[in] context The parent context used if ptr is NULL.
1250 * @param[in] ptr The chunk to be resized.
1252 * @param[in] size The new chunk size.
1254 * @return The new chunk, NULL on error.
1256 void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
1258 /* @} ******************************************************************/
1261 * @defgroup talloc_string The talloc string functions.
1262 * @ingroup talloc
1264 * talloc string allocation and manipulation functions.
1265 * @{
1269 * @brief Duplicate a string into a talloc chunk.
1271 * This function is equivalent to:
1273 * @code
1274 * ptr = talloc_size(ctx, strlen(p)+1);
1275 * if (ptr) memcpy(ptr, p, strlen(p)+1);
1276 * @endcode
1278 * This functions sets the name of the new pointer to the passed
1279 * string. This is equivalent to:
1281 * @code
1282 * talloc_set_name_const(ptr, ptr)
1283 * @endcode
1285 * @param[in] t The talloc context to hang the result off.
1287 * @param[in] p The string you want to duplicate.
1289 * @return The duplicated string, NULL on error.
1291 char *talloc_strdup(const void *t, const char *p);
1294 * @brief Append a string to given string and duplicate the result.
1296 * @param[in] s The destination to append to.
1298 * @param[in] a The string you want to append.
1300 * @return The duplicated string, NULL on error.
1302 * @see talloc_strdup()
1304 char *talloc_strdup_append(char *s, const char *a);
1307 * @brief Append a string to a given buffer and duplicate the result.
1309 * @param[in] s The destination buffer to append to.
1311 * @param[in] a The string you want to append.
1313 * @return The duplicated string, NULL on error.
1315 * @see talloc_strdup()
1317 char *talloc_strdup_append_buffer(char *s, const char *a);
1320 * @brief Duplicate a length-limited string into a talloc chunk.
1322 * This function is the talloc equivalent of the C library function strndup(3).
1324 * This functions sets the name of the new pointer to the passed string. This is
1325 * equivalent to:
1327 * @code
1328 * talloc_set_name_const(ptr, ptr)
1329 * @endcode
1331 * @param[in] t The talloc context to hang the result off.
1333 * @param[in] p The string you want to duplicate.
1335 * @param[in] n The maximum string length to duplicate.
1337 * @return The duplicated string, NULL on error.
1339 char *talloc_strndup(const void *t, const char *p, size_t n);
1342 * @brief Append at most n characters of a string to given string and duplicate
1343 * the result.
1345 * @param[in] s The destination string to append to.
1347 * @param[in] a The source string you want to append.
1349 * @param[in] n The number of characters you want to append from the
1350 * string.
1352 * @return The duplicated string, NULL on error.
1354 * @see talloc_strndup()
1356 char *talloc_strndup_append(char *s, const char *a, size_t n);
1359 * @brief Append at most n characters of a string to given buffer and duplicate
1360 * the result.
1362 * @param[in] s The destination buffer to append to.
1364 * @param[in] a The source string you want to append.
1366 * @param[in] n The number of characters you want to append from the
1367 * string.
1369 * @return The duplicated string, NULL on error.
1371 * @see talloc_strndup()
1373 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n);
1376 * @brief Format a string given a va_list.
1378 * This function is the talloc equivalent of the C library function
1379 * vasprintf(3).
1381 * This functions sets the name of the new pointer to the new string. This is
1382 * equivalent to:
1384 * @code
1385 * talloc_set_name_const(ptr, ptr)
1386 * @endcode
1388 * @param[in] t The talloc context to hang the result off.
1390 * @param[in] fmt The format string.
1392 * @param[in] ap The parameters used to fill fmt.
1394 * @return The formatted string, NULL on error.
1396 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1399 * @brief Format a string given a va_list and append it to the given destination
1400 * string.
1402 * @param[in] s The destination string to append to.
1404 * @param[in] fmt The format string.
1406 * @param[in] ap The parameters used to fill fmt.
1408 * @return The formatted string, NULL on error.
1410 * @see talloc_vasprintf()
1412 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1415 * @brief Format a string given a va_list and append it to the given destination
1416 * buffer.
1418 * @param[in] s The destination buffer to append to.
1420 * @param[in] fmt The format string.
1422 * @param[in] ap The parameters used to fill fmt.
1424 * @return The formatted string, NULL on error.
1426 * @see talloc_vasprintf()
1428 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1431 * @brief Format a string.
1433 * This function is the talloc equivalent of the C library function asprintf(3).
1435 * This functions sets the name of the new pointer to the new string. This is
1436 * equivalent to:
1438 * @code
1439 * talloc_set_name_const(ptr, ptr)
1440 * @endcode
1442 * @param[in] t The talloc context to hang the result off.
1444 * @param[in] fmt The format string.
1446 * @param[in] ... The parameters used to fill fmt.
1448 * @return The formatted string, NULL on error.
1450 char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1453 * @brief Append a formatted string to another string.
1455 * This function appends the given formatted string to the given string. Use
1456 * this varient when the string in the current talloc buffer may have been
1457 * truncated in length.
1459 * This functions sets the name of the new pointer to the new
1460 * string. This is equivalent to:
1462 * @code
1463 * talloc_set_name_const(ptr, ptr)
1464 * @endcode
1466 * @param[in] s The string to append to.
1468 * @param[in] fmt The format string.
1470 * @param[in] ... The parameters used to fill fmt.
1472 * @return The formatted string, NULL on error.
1474 char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1477 * @brief Append a formatted string to another string.
1479 * @param[in] s The string to append to
1481 * @param[in] fmt The format string.
1483 * @param[in] ... The parameters used to fill fmt.
1485 * @return The formatted string, NULL on error.
1487 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1489 /* @} ******************************************************************/
1492 * @defgroup talloc_debug The talloc debugging support functions
1493 * @ingroup talloc
1495 * To aid memory debugging, talloc contains routines to inspect the currently
1496 * allocated memory hierarchy.
1498 * @{
1502 * @brief Walk a complete talloc hierarchy.
1504 * This provides a more flexible reports than talloc_report(). It
1505 * will recursively call the callback for the entire tree of memory
1506 * referenced by the pointer. References in the tree are passed with
1507 * is_ref = 1 and the pointer that is referenced.
1509 * You can pass NULL for the pointer, in which case a report is
1510 * printed for the top level memory context, but only if
1511 * talloc_enable_leak_report() or talloc_enable_leak_report_full()
1512 * has been called.
1514 * The recursion is stopped when depth >= max_depth.
1515 * max_depth = -1 means only stop at leaf nodes.
1517 * @param[in] ptr The talloc chunk.
1519 * @param[in] depth Internal parameter to control recursion. Call with 0.
1521 * @param[in] max_depth Maximum recursion level.
1523 * @param[in] callback Function to be called on every chunk.
1525 * @param[in] private_data Private pointer passed to callback.
1527 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1528 void (*callback)(const void *ptr,
1529 int depth, int max_depth,
1530 int is_ref,
1531 void *private_data),
1532 void *private_data);
1535 * @brief Print a talloc hierarchy.
1537 * This provides a more flexible reports than talloc_report(). It
1538 * will let you specify the depth and max_depth.
1540 * @param[in] ptr The talloc chunk.
1542 * @param[in] depth Internal parameter to control recursion. Call with 0.
1544 * @param[in] max_depth Maximum recursion level.
1546 * @param[in] f The file handle to print to.
1548 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
1551 * @brief Print a summary report of all memory used by ptr.
1553 * This provides a more detailed report than talloc_report(). It will
1554 * recursively print the ensire tree of memory referenced by the
1555 * pointer. References in the tree are shown by giving the name of the
1556 * pointer that is referenced.
1558 * You can pass NULL for the pointer, in which case a report is printed
1559 * for the top level memory context, but only if
1560 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
1561 * been called.
1563 * @param[in] ptr The talloc chunk.
1565 * @param[in] f The file handle to print to.
1567 * Example:
1568 * @code
1569 * unsigned int *a, *b;
1570 * a = talloc(NULL, unsigned int);
1571 * b = talloc(a, unsigned int);
1572 * fprintf(stderr, "Dumping memory tree for a:\n");
1573 * talloc_report_full(a, stderr);
1574 * @endcode
1576 * @see talloc_report()
1578 void talloc_report_full(const void *ptr, FILE *f);
1581 * @brief Print a summary report of all memory used by ptr.
1583 * This function prints a summary report of all memory used by ptr. One line of
1584 * report is printed for each immediate child of ptr, showing the total memory
1585 * and number of blocks used by that child.
1587 * You can pass NULL for the pointer, in which case a report is printed
1588 * for the top level memory context, but only if talloc_enable_leak_report()
1589 * or talloc_enable_leak_report_full() has been called.
1591 * @param[in] ptr The talloc chunk.
1593 * @param[in] f The file handle to print to.
1595 * Example:
1596 * @code
1597 * unsigned int *a, *b;
1598 * a = talloc(NULL, unsigned int);
1599 * b = talloc(a, unsigned int);
1600 * fprintf(stderr, "Summary of memory tree for a:\n");
1601 * talloc_report(a, stderr);
1602 * @endcode
1604 * @see talloc_report_full()
1606 void talloc_report(const void *ptr, FILE *f);
1609 * @brief Enable tracking the use of NULL memory contexts.
1611 * This enables tracking of the NULL memory context without enabling leak
1612 * reporting on exit. Useful for when you want to do your own leak
1613 * reporting call via talloc_report_null_full();
1615 void talloc_enable_null_tracking(void);
1618 * @brief Enable tracking the use of NULL memory contexts.
1620 * This enables tracking of the NULL memory context without enabling leak
1621 * reporting on exit. Useful for when you want to do your own leak
1622 * reporting call via talloc_report_null_full();
1624 void talloc_enable_null_tracking_no_autofree(void);
1627 * @brief Disable tracking of the NULL memory context.
1629 * This disables tracking of the NULL memory context.
1631 void talloc_disable_null_tracking(void);
1634 * @brief Enable leak report when a program exits.
1636 * This enables calling of talloc_report(NULL, stderr) when the program
1637 * exits. In Samba4 this is enabled by using the --leak-report command
1638 * line option.
1640 * For it to be useful, this function must be called before any other
1641 * talloc function as it establishes a "null context" that acts as the
1642 * top of the tree. If you don't call this function first then passing
1643 * NULL to talloc_report() or talloc_report_full() won't give you the
1644 * full tree printout.
1646 * Here is a typical talloc report:
1648 * @code
1649 * talloc report on 'null_context' (total 267 bytes in 15 blocks)
1650 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
1651 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
1652 * iconv(UTF8,CP850) contains 42 bytes in 2 blocks
1653 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
1654 * iconv(CP850,UTF8) contains 42 bytes in 2 blocks
1655 * iconv(UTF8,UTF-16LE) contains 45 bytes in 2 blocks
1656 * iconv(UTF-16LE,UTF8) contains 45 bytes in 2 blocks
1657 * @endcode
1659 void talloc_enable_leak_report(void);
1662 * @brief Enable full leak report when a program exits.
1664 * This enables calling of talloc_report_full(NULL, stderr) when the
1665 * program exits. In Samba4 this is enabled by using the
1666 * --leak-report-full command line option.
1668 * For it to be useful, this function must be called before any other
1669 * talloc function as it establishes a "null context" that acts as the
1670 * top of the tree. If you don't call this function first then passing
1671 * NULL to talloc_report() or talloc_report_full() won't give you the
1672 * full tree printout.
1674 * Here is a typical full report:
1676 * @code
1677 * full talloc report on 'root' (total 18 bytes in 8 blocks)
1678 * p1 contains 18 bytes in 7 blocks (ref 0)
1679 * r1 contains 13 bytes in 2 blocks (ref 0)
1680 * reference to: p2
1681 * p2 contains 1 bytes in 1 blocks (ref 1)
1682 * x3 contains 1 bytes in 1 blocks (ref 0)
1683 * x2 contains 1 bytes in 1 blocks (ref 0)
1684 * x1 contains 1 bytes in 1 blocks (ref 0)
1685 * @endcode
1687 void talloc_enable_leak_report_full(void);
1689 /* @} ******************************************************************/
1691 void talloc_set_abort_fn(void (*abort_fn)(const char *reason));
1692 void talloc_set_log_fn(void (*log_fn)(const char *message));
1693 void talloc_set_log_stderr(void);
1695 #if TALLOC_DEPRECATED
1696 #define talloc_zero_p(ctx, type) talloc_zero(ctx, type)
1697 #define talloc_p(ctx, type) talloc(ctx, type)
1698 #define talloc_array_p(ctx, type, count) talloc_array(ctx, type, count)
1699 #define talloc_realloc_p(ctx, p, type, count) talloc_realloc(ctx, p, type, count)
1700 #define talloc_destroy(ctx) talloc_free(ctx)
1701 #define talloc_append_string(c, s, a) (s?talloc_strdup_append(s,a):talloc_strdup(c, a))
1702 #endif
1704 #ifndef TALLOC_MAX_DEPTH
1705 #define TALLOC_MAX_DEPTH 10000
1706 #endif
1708 #ifdef __cplusplus
1709 } /* end of extern "C" */
1710 #endif
1712 #endif