winbindd: as DC we should try to get the target_domain from @SOMETHING part of the...
[Samba.git] / lib / talloc / talloc.h
blob618430a50ab59a511846d42c11fecfe0ad04e2f7
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 1
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 #ifndef PRINTF_ATTRIBUTE
93 #if (__GNUC__ >= 3)
94 /** Use gcc attribute to check printf fns. a1 is the 1-based index of
95 * the parameter containing the format, and a2 the index of the first
96 * argument. Note that some gcc 2.x versions don't handle this
97 * properly **/
98 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
99 #else
100 #define PRINTF_ATTRIBUTE(a1, a2)
101 #endif
102 #endif
104 #ifdef DOXYGEN
106 * @brief Create a new talloc context.
108 * The talloc() macro is the core of the talloc library. It takes a memory
109 * context and a type, and returns a pointer to a new area of memory of the
110 * given type.
112 * The returned pointer is itself a talloc context, so you can use it as the
113 * context argument to more calls to talloc if you wish.
115 * The returned pointer is a "child" of the supplied context. This means that if
116 * you talloc_free() the context then the new child disappears as well.
117 * Alternatively you can free just the child.
119 * @param[in] ctx A talloc context to create a new reference on or NULL to
120 * create a new top level context.
122 * @param[in] type The type of memory to allocate.
124 * @return A type casted talloc context or NULL on error.
126 * @code
127 * unsigned int *a, *b;
129 * a = talloc(NULL, unsigned int);
130 * b = talloc(a, unsigned int);
131 * @endcode
133 * @see talloc_zero
134 * @see talloc_array
135 * @see talloc_steal
136 * @see talloc_free
138 void *talloc(const void *ctx, #type);
139 #else
140 #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
141 void *_talloc(const void *context, size_t size);
142 #endif
145 * @brief Create a new top level talloc context.
147 * This function creates a zero length named talloc context as a top level
148 * context. It is equivalent to:
150 * @code
151 * talloc_named(NULL, 0, fmt, ...);
152 * @endcode
153 * @param[in] fmt Format string for the name.
155 * @param[in] ... Additional printf-style arguments.
157 * @return The allocated memory chunk, NULL on error.
159 * @see talloc_named()
161 void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
163 #ifdef DOXYGEN
165 * @brief Free a chunk of talloc memory.
167 * The talloc_free() function frees a piece of talloc memory, and all its
168 * children. You can call talloc_free() on any pointer returned by
169 * talloc().
171 * The return value of talloc_free() indicates success or failure, with 0
172 * returned for success and -1 for failure. A possible failure condition
173 * is if the pointer had a destructor attached to it and the destructor
174 * returned -1. See talloc_set_destructor() for details on
175 * destructors. Likewise, if "ptr" is NULL, then the function will make
176 * no modifications and return -1.
178 * From version 2.0 and onwards, as a special case, talloc_free() is
179 * refused on pointers that have more than one parent associated, as talloc
180 * would have no way of knowing which parent should be removed. This is
181 * different from older versions in the sense that always the reference to
182 * the most recently established parent has been destroyed. Hence to free a
183 * pointer that has more than one parent please use talloc_unlink().
185 * To help you find problems in your code caused by this behaviour, if
186 * you do try and free a pointer with more than one parent then the
187 * talloc logging function will be called to give output like this:
189 * @code
190 * ERROR: talloc_free with references at some_dir/source/foo.c:123
191 * reference at some_dir/source/other.c:325
192 * reference at some_dir/source/third.c:121
193 * @endcode
195 * Please see the documentation for talloc_set_log_fn() and
196 * talloc_set_log_stderr() for more information on talloc logging
197 * functions.
199 * If <code>TALLOC_FREE_FILL</code> environment variable is set,
200 * the memory occupied by the context is filled with the value of this variable.
201 * The value should be a numeric representation of the character you want to
202 * use.
204 * talloc_free() operates recursively on its children.
206 * @param[in] ptr The chunk to be freed.
208 * @return Returns 0 on success and -1 on error. A possible
209 * failure condition is if the pointer had a destructor
210 * attached to it and the destructor returned -1. Likewise,
211 * if "ptr" is NULL, then the function will make no
212 * modifications and returns -1.
214 * Example:
215 * @code
216 * unsigned int *a, *b;
217 * a = talloc(NULL, unsigned int);
218 * b = talloc(a, unsigned int);
220 * talloc_free(a); // Frees a and b
221 * @endcode
223 * @see talloc_set_destructor()
224 * @see talloc_unlink()
226 int talloc_free(void *ptr);
227 #else
228 #define talloc_free(ctx) _talloc_free(ctx, __location__)
229 int _talloc_free(void *ptr, const char *location);
230 #endif
233 * @brief Free a talloc chunk's children.
235 * The function walks along the list of all children of a talloc context and
236 * talloc_free()s only the children, not the context itself.
238 * A NULL argument is handled as no-op.
240 * @param[in] ptr The chunk that you want to free the children of
241 * (NULL is allowed too)
243 void talloc_free_children(void *ptr);
245 #ifdef DOXYGEN
247 * @brief Assign a destructor function to be called when a chunk is freed.
249 * The function talloc_set_destructor() sets the "destructor" for the pointer
250 * "ptr". A destructor is a function that is called when the memory used by a
251 * pointer is about to be released. The destructor receives the pointer as an
252 * argument, and should return 0 for success and -1 for failure.
254 * The destructor can do anything it wants to, including freeing other pieces
255 * of memory. A common use for destructors is to clean up operating system
256 * resources (such as open file descriptors) contained in the structure the
257 * destructor is placed on.
259 * You can only place one destructor on a pointer. If you need more than one
260 * destructor then you can create a zero-length child of the pointer and place
261 * an additional destructor on that.
263 * To remove a destructor call talloc_set_destructor() with NULL for the
264 * destructor.
266 * If your destructor attempts to talloc_free() the pointer that it is the
267 * destructor for then talloc_free() will return -1 and the free will be
268 * ignored. This would be a pointless operation anyway, as the destructor is
269 * only called when the memory is just about to go away.
271 * @param[in] ptr The talloc chunk to add a destructor to.
273 * @param[in] destructor The destructor function to be called. NULL to remove
274 * it.
276 * Example:
277 * @code
278 * static int destroy_fd(int *fd) {
279 * close(*fd);
280 * return 0;
283 * int *open_file(const char *filename) {
284 * int *fd = talloc(NULL, int);
285 * *fd = open(filename, O_RDONLY);
286 * if (*fd < 0) {
287 * talloc_free(fd);
288 * return NULL;
290 * // Whenever they free this, we close the file.
291 * talloc_set_destructor(fd, destroy_fd);
292 * return fd;
294 * @endcode
296 * @see talloc()
297 * @see talloc_free()
299 void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
302 * @brief Change a talloc chunk's parent.
304 * The talloc_steal() function changes the parent context of a talloc
305 * pointer. It is typically used when the context that the pointer is
306 * currently a child of is going to be freed and you wish to keep the
307 * memory for a longer time.
309 * To make the changed hierarchy less error-prone, you might consider to use
310 * talloc_move().
312 * If you try and call talloc_steal() on a pointer that has more than one
313 * parent then the result is ambiguous. Talloc will choose to remove the
314 * parent that is currently indicated by talloc_parent() and replace it with
315 * the chosen parent. You will also get a message like this via the talloc
316 * logging functions:
318 * @code
319 * WARNING: talloc_steal with references at some_dir/source/foo.c:123
320 * reference at some_dir/source/other.c:325
321 * reference at some_dir/source/third.c:121
322 * @endcode
324 * To unambiguously change the parent of a pointer please see the function
325 * talloc_reparent(). See the talloc_set_log_fn() documentation for more
326 * information on talloc logging.
328 * @param[in] new_ctx The new parent context.
330 * @param[in] ptr The talloc chunk to move.
332 * @return Returns the pointer that you pass it. It does not have
333 * any failure modes.
335 * @note It is possible to produce loops in the parent/child relationship
336 * if you are not careful with talloc_steal(). No guarantees are provided
337 * as to your sanity or the safety of your data if you do this.
339 void *talloc_steal(const void *new_ctx, const void *ptr);
340 #else /* DOXYGEN */
341 /* try to make talloc_set_destructor() and talloc_steal() type safe,
342 if we have a recent gcc */
343 #if (__GNUC__ >= 3)
344 #define _TALLOC_TYPEOF(ptr) __typeof__(ptr)
345 #define talloc_set_destructor(ptr, function) \
346 do { \
347 int (*_talloc_destructor_fn)(_TALLOC_TYPEOF(ptr)) = (function); \
348 _talloc_set_destructor((ptr), (int (*)(void *))_talloc_destructor_fn); \
349 } while(0)
350 /* this extremely strange macro is to avoid some braindamaged warning
351 stupidity in gcc 4.1.x */
352 #define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__); __talloc_steal_ret; })
353 #else /* __GNUC__ >= 3 */
354 #define talloc_set_destructor(ptr, function) \
355 _talloc_set_destructor((ptr), (int (*)(void *))(function))
356 #define _TALLOC_TYPEOF(ptr) void *
357 #define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__)
358 #endif /* __GNUC__ >= 3 */
359 void _talloc_set_destructor(const void *ptr, int (*_destructor)(void *));
360 void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location);
361 #endif /* DOXYGEN */
364 * @brief Assign a name to a talloc chunk.
366 * Each talloc pointer has a "name". The name is used principally for
367 * debugging purposes, although it is also possible to set and get the name on
368 * a pointer in as a way of "marking" pointers in your code.
370 * The main use for names on pointer is for "talloc reports". See
371 * talloc_report() and talloc_report_full() for details. Also see
372 * talloc_enable_leak_report() and talloc_enable_leak_report_full().
374 * The talloc_set_name() function allocates memory as a child of the
375 * pointer. It is logically equivalent to:
377 * @code
378 * talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
379 * @endcode
381 * @param[in] ptr The talloc chunk to assign a name to.
383 * @param[in] fmt Format string for the name.
385 * @param[in] ... Add printf-style additional arguments.
387 * @return The assigned name, NULL on error.
389 * @note Multiple calls to talloc_set_name() will allocate more memory without
390 * releasing the name. All of the memory is released when the ptr is freed
391 * using talloc_free().
393 const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
395 #ifdef DOXYGEN
397 * @brief Change a talloc chunk's parent.
399 * This function has the same effect as talloc_steal(), and additionally sets
400 * the source pointer to NULL. You would use it like this:
402 * @code
403 * struct foo *X = talloc(tmp_ctx, struct foo);
404 * struct foo *Y;
405 * Y = talloc_move(new_ctx, &X);
406 * @endcode
408 * @param[in] new_ctx The new parent context.
410 * @param[in] pptr Pointer to a pointer to the talloc chunk to move.
412 * @return The pointer to the talloc chunk that moved.
413 * It does not have any failure modes.
416 void *talloc_move(const void *new_ctx, void **pptr);
417 #else
418 #define talloc_move(ctx, pptr) (_TALLOC_TYPEOF(*(pptr)))_talloc_move((ctx),(void *)(pptr))
419 void *_talloc_move(const void *new_ctx, const void *pptr);
420 #endif
423 * @brief Assign a name to a talloc chunk.
425 * The function is just like talloc_set_name(), but it takes a string constant,
426 * and is much faster. It is extensively used by the "auto naming" macros, such
427 * as talloc_p().
429 * This function does not allocate any memory. It just copies the supplied
430 * pointer into the internal representation of the talloc ptr. This means you
431 * must not pass a name pointer to memory that will disappear before the ptr
432 * is freed with talloc_free().
434 * @param[in] ptr The talloc chunk to assign a name to.
436 * @param[in] name Format string for the name.
438 void talloc_set_name_const(const void *ptr, const char *name);
441 * @brief Create a named talloc chunk.
443 * The talloc_named() function creates a named talloc pointer. It is
444 * equivalent to:
446 * @code
447 * ptr = talloc_size(context, size);
448 * talloc_set_name(ptr, fmt, ....);
449 * @endcode
451 * @param[in] context The talloc context to hang the result off.
453 * @param[in] size Number of char's that you want to allocate.
455 * @param[in] fmt Format string for the name.
457 * @param[in] ... Additional printf-style arguments.
459 * @return The allocated memory chunk, NULL on error.
461 * @see talloc_set_name()
463 void *talloc_named(const void *context, size_t size,
464 const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
467 * @brief Basic routine to allocate a chunk of memory.
469 * This is equivalent to:
471 * @code
472 * ptr = talloc_size(context, size);
473 * talloc_set_name_const(ptr, name);
474 * @endcode
476 * @param[in] context The parent context.
478 * @param[in] size The number of char's that we want to allocate.
480 * @param[in] name The name the talloc block has.
482 * @return The allocated memory chunk, NULL on error.
484 void *talloc_named_const(const void *context, size_t size, const char *name);
486 #ifdef DOXYGEN
488 * @brief Untyped allocation.
490 * The function should be used when you don't have a convenient type to pass to
491 * talloc(). Unlike talloc(), it is not type safe (as it returns a void *), so
492 * you are on your own for type checking.
494 * Best to use talloc() or talloc_array() instead.
496 * @param[in] ctx The talloc context to hang the result off.
498 * @param[in] size Number of char's that you want to allocate.
500 * @return The allocated memory chunk, NULL on error.
502 * Example:
503 * @code
504 * void *mem = talloc_size(NULL, 100);
505 * @endcode
507 void *talloc_size(const void *ctx, size_t size);
508 #else
509 #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
510 #endif
512 #ifdef DOXYGEN
514 * @brief Allocate into a typed pointer.
516 * The talloc_ptrtype() macro should be used when you have a pointer and want
517 * to allocate memory to point at with this pointer. When compiling with
518 * gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size() and
519 * talloc_get_name() will return the current location in the source file and
520 * not the type.
522 * @param[in] ctx The talloc context to hang the result off.
524 * @param[in] type The pointer you want to assign the result to.
526 * @return The properly casted allocated memory chunk, NULL on
527 * error.
529 * Example:
530 * @code
531 * unsigned int *a = talloc_ptrtype(NULL, a);
532 * @endcode
534 void *talloc_ptrtype(const void *ctx, #type);
535 #else
536 #define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
537 #endif
539 #ifdef DOXYGEN
541 * @brief Allocate a new 0-sized talloc chunk.
543 * This is a utility macro that creates a new memory context hanging off an
544 * existing context, automatically naming it "talloc_new: __location__" where
545 * __location__ is the source line it is called from. It is particularly
546 * useful for creating a new temporary working context.
548 * @param[in] ctx The talloc parent context.
550 * @return A new talloc chunk, NULL on error.
552 void *talloc_new(const void *ctx);
553 #else
554 #define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
555 #endif
557 #ifdef DOXYGEN
559 * @brief Allocate a 0-initizialized structure.
561 * The macro is equivalent to:
563 * @code
564 * ptr = talloc(ctx, type);
565 * if (ptr) memset(ptr, 0, sizeof(type));
566 * @endcode
568 * @param[in] ctx The talloc context to hang the result off.
570 * @param[in] type The type that we want to allocate.
572 * @return Pointer to a piece of memory, properly cast to 'type *',
573 * NULL on error.
575 * Example:
576 * @code
577 * unsigned int *a, *b;
578 * a = talloc_zero(NULL, unsigned int);
579 * b = talloc_zero(a, unsigned int);
580 * @endcode
582 * @see talloc()
583 * @see talloc_zero_size()
584 * @see talloc_zero_array()
586 void *talloc_zero(const void *ctx, #type);
589 * @brief Allocate untyped, 0-initialized memory.
591 * @param[in] ctx The talloc context to hang the result off.
593 * @param[in] size Number of char's that you want to allocate.
595 * @return The allocated memory chunk.
597 void *talloc_zero_size(const void *ctx, size_t size);
598 #else
599 #define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
600 #define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
601 void *_talloc_zero(const void *ctx, size_t size, const char *name);
602 #endif
605 * @brief Return the name of a talloc chunk.
607 * @param[in] ptr The talloc chunk.
609 * @return The current name for the given talloc pointer.
611 * @see talloc_set_name()
613 const char *talloc_get_name(const void *ptr);
616 * @brief Verify that a talloc chunk carries a specified name.
618 * This function checks if a pointer has the specified name. If it does
619 * then the pointer is returned.
621 * @param[in] ptr The talloc chunk to check.
623 * @param[in] name The name to check against.
625 * @return The pointer if the name matches, NULL if it doesn't.
627 void *talloc_check_name(const void *ptr, const char *name);
630 * @brief Get the parent chunk of a pointer.
632 * @param[in] ptr The talloc pointer to inspect.
634 * @return The talloc parent of ptr, NULL on error.
636 void *talloc_parent(const void *ptr);
639 * @brief Get a talloc chunk's parent name.
641 * @param[in] ptr The talloc pointer to inspect.
643 * @return The name of ptr's parent chunk.
645 const char *talloc_parent_name(const void *ptr);
648 * @brief Get the total size of a talloc chunk including its children.
650 * The function returns the total size in bytes used by this pointer and all
651 * child pointers. Mostly useful for debugging.
653 * Passing NULL is allowed, but it will only give a meaningful result if
654 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
655 * been called.
657 * @param[in] ptr The talloc chunk.
659 * @return The total size.
661 size_t talloc_total_size(const void *ptr);
664 * @brief Get the number of talloc chunks hanging off a chunk.
666 * The talloc_total_blocks() function returns the total memory block
667 * count used by this pointer and all child pointers. Mostly useful for
668 * debugging.
670 * Passing NULL is allowed, but it will only give a meaningful result if
671 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
672 * been called.
674 * @param[in] ptr The talloc chunk.
676 * @return The total size.
678 size_t talloc_total_blocks(const void *ptr);
680 #ifdef DOXYGEN
682 * @brief Duplicate a memory area into a talloc chunk.
684 * The function is equivalent to:
686 * @code
687 * ptr = talloc_size(ctx, size);
688 * if (ptr) memcpy(ptr, p, size);
689 * @endcode
691 * @param[in] t The talloc context to hang the result off.
693 * @param[in] p The memory chunk you want to duplicate.
695 * @param[in] size Number of char's that you want copy.
697 * @return The allocated memory chunk.
699 * @see talloc_size()
701 void *talloc_memdup(const void *t, const void *p, size_t size);
702 #else
703 #define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
704 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
705 #endif
707 #ifdef DOXYGEN
709 * @brief Assign a type to a talloc chunk.
711 * This macro allows you to force the name of a pointer to be of a particular
712 * type. This can be used in conjunction with talloc_get_type() to do type
713 * checking on void* pointers.
715 * It is equivalent to this:
717 * @code
718 * talloc_set_name_const(ptr, #type)
719 * @endcode
721 * @param[in] ptr The talloc chunk to assign the type to.
723 * @param[in] type The type to assign.
725 void talloc_set_type(const char *ptr, #type);
728 * @brief Get a typed pointer out of a talloc pointer.
730 * This macro allows you to do type checking on talloc pointers. It is
731 * particularly useful for void* private pointers. It is equivalent to
732 * this:
734 * @code
735 * (type *)talloc_check_name(ptr, #type)
736 * @endcode
738 * @param[in] ptr The talloc pointer to check.
740 * @param[in] type The type to check against.
742 * @return The properly casted pointer given by ptr, NULL on error.
744 type *talloc_get_type(const void *ptr, #type);
745 #else
746 #define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
747 #define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
748 #endif
750 #ifdef DOXYGEN
752 * @brief Safely turn a void pointer into a typed pointer.
754 * This macro is used together with talloc(mem_ctx, struct foo). If you had to
755 * assign the talloc chunk pointer to some void pointer variable,
756 * talloc_get_type_abort() is the recommended way to get the convert the void
757 * pointer back to a typed pointer.
759 * @param[in] ptr The void pointer to convert.
761 * @param[in] type The type that this chunk contains
763 * @return The same value as ptr, type-checked and properly cast.
765 void *talloc_get_type_abort(const void *ptr, #type);
766 #else
767 #ifdef TALLOC_GET_TYPE_ABORT_NOOP
768 #define talloc_get_type_abort(ptr, type) (type *)(ptr)
769 #else
770 #define talloc_get_type_abort(ptr, type) (type *)_talloc_get_type_abort(ptr, #type, __location__)
771 #endif
772 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location);
773 #endif
776 * @brief Find a parent context by name.
778 * Find a parent memory context of the current context that has the given
779 * name. This can be very useful in complex programs where it may be
780 * difficult to pass all information down to the level you need, but you
781 * know the structure you want is a parent of another context.
783 * @param[in] ctx The talloc chunk to start from.
785 * @param[in] name The name of the parent we look for.
787 * @return The memory context we are looking for, NULL if not
788 * found.
790 void *talloc_find_parent_byname(const void *ctx, const char *name);
792 #ifdef DOXYGEN
794 * @brief Find a parent context by type.
796 * Find a parent memory context of the current context that has the given
797 * name. This can be very useful in complex programs where it may be
798 * difficult to pass all information down to the level you need, but you
799 * know the structure you want is a parent of another context.
801 * Like talloc_find_parent_byname() but takes a type, making it typesafe.
803 * @param[in] ptr The talloc chunk to start from.
805 * @param[in] type The type of the parent to look for.
807 * @return The memory context we are looking for, NULL if not
808 * found.
810 void *talloc_find_parent_bytype(const void *ptr, #type);
811 #else
812 #define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
813 #endif
816 * @brief Allocate a talloc pool.
818 * A talloc pool is a pure optimization for specific situations. In the
819 * release process for Samba 3.2 we found out that we had become considerably
820 * slower than Samba 3.0 was. Profiling showed that malloc(3) was a large CPU
821 * consumer in benchmarks. For Samba 3.2 we have internally converted many
822 * static buffers to dynamically allocated ones, so malloc(3) being beaten
823 * more was no surprise. But it made us slower.
825 * talloc_pool() is an optimization to call malloc(3) a lot less for the use
826 * pattern Samba has: The SMB protocol is mainly a request/response protocol
827 * where we have to allocate a certain amount of memory per request and free
828 * that after the SMB reply is sent to the client.
830 * talloc_pool() creates a talloc chunk that you can use as a talloc parent
831 * exactly as you would use any other ::TALLOC_CTX. The difference is that
832 * when you talloc a child of this pool, no malloc(3) is done. Instead, talloc
833 * just increments a pointer inside the talloc_pool. This also works
834 * recursively. If you use the child of the talloc pool as a parent for
835 * grand-children, their memory is also taken from the talloc pool.
837 * If there is not enough memory in the pool to allocate the new child,
838 * it will create a new talloc chunk as if the parent was a normal talloc
839 * context.
841 * If you talloc_free() children of a talloc pool, the memory is not given
842 * back to the system. Instead, free(3) is only called if the talloc_pool()
843 * itself is released with talloc_free().
845 * The downside of a talloc pool is that if you talloc_move() a child of a
846 * talloc pool to a talloc parent outside the pool, the whole pool memory is
847 * not free(3)'ed until that moved chunk is also talloc_free()ed.
849 * @param[in] context The talloc context to hang the result off.
851 * @param[in] size Size of the talloc pool.
853 * @return The allocated talloc pool, NULL on error.
855 void *talloc_pool(const void *context, size_t size);
857 #ifdef DOXYGEN
859 * @brief Allocate a talloc object as/with an additional pool.
861 * This is like talloc_pool(), but's it's more flexible
862 * and allows an object to be a pool for its children.
864 * @param[in] ctx The talloc context to hang the result off.
866 * @param[in] type The type that we want to allocate.
868 * @param[in] num_subobjects The expected number of subobjects, which will
869 * be allocated within the pool. This allocates
870 * space for talloc_chunk headers.
872 * @param[in] total_subobjects_size The size that all subobjects can use in total.
875 * @return The allocated talloc object, NULL on error.
877 void *talloc_pooled_object(const void *ctx, #type,
878 unsigned num_subobjects,
879 size_t total_subobjects_size);
880 #else
881 #define talloc_pooled_object(_ctx, _type, \
882 _num_subobjects, \
883 _total_subobjects_size) \
884 (_type *)_talloc_pooled_object((_ctx), sizeof(_type), #_type, \
885 (_num_subobjects), \
886 (_total_subobjects_size))
887 void *_talloc_pooled_object(const void *ctx,
888 size_t type_size,
889 const char *type_name,
890 unsigned num_subobjects,
891 size_t total_subobjects_size);
892 #endif
895 * @brief Free a talloc chunk and NULL out the pointer.
897 * TALLOC_FREE() frees a pointer and sets it to NULL. Use this if you want
898 * immediate feedback (i.e. crash) if you use a pointer after having free'ed
899 * it.
901 * @param[in] ctx The chunk to be freed.
903 #define TALLOC_FREE(ctx) do { if (ctx != NULL) { talloc_free(ctx); ctx=NULL; } } while(0)
905 /* @} ******************************************************************/
908 * \defgroup talloc_ref The talloc reference function.
909 * @ingroup talloc
911 * This module contains the definitions around talloc references
913 * @{
917 * @brief Increase the reference count of a talloc chunk.
919 * The talloc_increase_ref_count(ptr) function is exactly equivalent to:
921 * @code
922 * talloc_reference(NULL, ptr);
923 * @endcode
925 * You can use either syntax, depending on which you think is clearer in
926 * your code.
928 * @param[in] ptr The pointer to increase the reference count.
930 * @return 0 on success, -1 on error.
932 int talloc_increase_ref_count(const void *ptr);
935 * @brief Get the number of references to a talloc chunk.
937 * @param[in] ptr The pointer to retrieve the reference count from.
939 * @return The number of references.
941 size_t talloc_reference_count(const void *ptr);
943 #ifdef DOXYGEN
945 * @brief Create an additional talloc parent to a pointer.
947 * The talloc_reference() function makes "context" an additional parent of
948 * ptr. Each additional reference consumes around 48 bytes of memory on intel
949 * x86 platforms.
951 * If ptr is NULL, then the function is a no-op, and simply returns NULL.
953 * After creating a reference you can free it in one of the following ways:
955 * - you can talloc_free() any parent of the original pointer. That
956 * will reduce the number of parents of this pointer by 1, and will
957 * cause this pointer to be freed if it runs out of parents.
959 * - you can talloc_free() the pointer itself if it has at maximum one
960 * parent. This behaviour has been changed since the release of version
961 * 2.0. Further informations in the description of "talloc_free".
963 * For more control on which parent to remove, see talloc_unlink()
964 * @param[in] ctx The additional parent.
966 * @param[in] ptr The pointer you want to create an additional parent for.
968 * @return The original pointer 'ptr', NULL if talloc ran out of
969 * memory in creating the reference.
971 * @warning You should try to avoid using this interface. It turns a beautiful
972 * talloc-tree into a graph. It is often really hard to debug if you
973 * screw something up by accident.
975 * Example:
976 * @code
977 * unsigned int *a, *b, *c;
978 * a = talloc(NULL, unsigned int);
979 * b = talloc(NULL, unsigned int);
980 * c = talloc(a, unsigned int);
981 * // b also serves as a parent of c.
982 * talloc_reference(b, c);
983 * @endcode
985 * @see talloc_unlink()
987 void *talloc_reference(const void *ctx, const void *ptr);
988 #else
989 #define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference_loc((ctx),(ptr), __location__)
990 void *_talloc_reference_loc(const void *context, const void *ptr, const char *location);
991 #endif
994 * @brief Remove a specific parent from a talloc chunk.
996 * The function removes a specific parent from ptr. The context passed must
997 * either be a context used in talloc_reference() with this pointer, or must be
998 * a direct parent of ptr.
1000 * You can just use talloc_free() instead of talloc_unlink() if there
1001 * is at maximum one parent. This behaviour has been changed since the
1002 * release of version 2.0. Further informations in the description of
1003 * "talloc_free".
1005 * @param[in] context The talloc parent to remove.
1007 * @param[in] ptr The talloc ptr you want to remove the parent from.
1009 * @return 0 on success, -1 on error.
1011 * @note If the parent has already been removed using talloc_free() then
1012 * this function will fail and will return -1. Likewise, if ptr is NULL,
1013 * then the function will make no modifications and return -1.
1015 * @warning You should try to avoid using this interface. It turns a beautiful
1016 * talloc-tree into a graph. It is often really hard to debug if you
1017 * screw something up by accident.
1019 * Example:
1020 * @code
1021 * unsigned int *a, *b, *c;
1022 * a = talloc(NULL, unsigned int);
1023 * b = talloc(NULL, unsigned int);
1024 * c = talloc(a, unsigned int);
1025 * // b also serves as a parent of c.
1026 * talloc_reference(b, c);
1027 * talloc_unlink(b, c);
1028 * @endcode
1030 int talloc_unlink(const void *context, void *ptr);
1033 * @brief Provide a talloc context that is freed at program exit.
1035 * This is a handy utility function that returns a talloc context
1036 * which will be automatically freed on program exit. This can be used
1037 * to reduce the noise in memory leak reports.
1039 * Never use this in code that might be used in objects loaded with
1040 * dlopen and unloaded with dlclose. talloc_autofree_context()
1041 * internally uses atexit(3). Some platforms like modern Linux handles
1042 * this fine, but for example FreeBSD does not deal well with dlopen()
1043 * and atexit() used simultaneously: dlclose() does not clean up the
1044 * list of atexit-handlers, so when the program exits the code that
1045 * was registered from within talloc_autofree_context() is gone, the
1046 * program crashes at exit.
1048 * @return A talloc context, NULL on error.
1050 void *talloc_autofree_context(void);
1053 * @brief Get the size of a talloc chunk.
1055 * This function lets you know the amount of memory allocated so far by
1056 * this context. It does NOT account for subcontext memory.
1057 * This can be used to calculate the size of an array.
1059 * @param[in] ctx The talloc chunk.
1061 * @return The size of the talloc chunk.
1063 size_t talloc_get_size(const void *ctx);
1066 * @brief Show the parentage of a context.
1068 * @param[in] context The talloc context to look at.
1070 * @param[in] file The output to use, a file, stdout or stderr.
1072 void talloc_show_parents(const void *context, FILE *file);
1075 * @brief Check if a context is parent of a talloc chunk.
1077 * This checks if context is referenced in the talloc hierarchy above ptr.
1079 * @param[in] context The assumed talloc context.
1081 * @param[in] ptr The talloc chunk to check.
1083 * @return Return 1 if this is the case, 0 if not.
1085 int talloc_is_parent(const void *context, const void *ptr);
1088 * @brief Change the parent context of a talloc pointer.
1090 * The function changes the parent context of a talloc pointer. It is typically
1091 * used when the context that the pointer is currently a child of is going to be
1092 * freed and you wish to keep the memory for a longer time.
1094 * The difference between talloc_reparent() and talloc_steal() is that
1095 * talloc_reparent() can specify which parent you wish to change. This is
1096 * useful when a pointer has multiple parents via references.
1098 * @param[in] old_parent
1099 * @param[in] new_parent
1100 * @param[in] ptr
1102 * @return Return the pointer you passed. It does not have any
1103 * failure modes.
1105 void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr);
1107 /* @} ******************************************************************/
1110 * @defgroup talloc_array The talloc array functions
1111 * @ingroup talloc
1113 * Talloc contains some handy helpers for handling Arrays conveniently
1115 * @{
1118 #ifdef DOXYGEN
1120 * @brief Allocate an array.
1122 * The macro is equivalent to:
1124 * @code
1125 * (type *)talloc_size(ctx, sizeof(type) * count);
1126 * @endcode
1128 * except that it provides integer overflow protection for the multiply,
1129 * returning NULL if the multiply overflows.
1131 * @param[in] ctx The talloc context to hang the result off.
1133 * @param[in] type The type that we want to allocate.
1135 * @param[in] count The number of 'type' elements you want to allocate.
1137 * @return The allocated result, properly cast to 'type *', NULL on
1138 * error.
1140 * Example:
1141 * @code
1142 * unsigned int *a, *b;
1143 * a = talloc_zero(NULL, unsigned int);
1144 * b = talloc_array(a, unsigned int, 100);
1145 * @endcode
1147 * @see talloc()
1148 * @see talloc_zero_array()
1150 void *talloc_array(const void *ctx, #type, unsigned count);
1151 #else
1152 #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
1153 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
1154 #endif
1156 #ifdef DOXYGEN
1158 * @brief Allocate an array.
1160 * @param[in] ctx The talloc context to hang the result off.
1162 * @param[in] size The size of an array element.
1164 * @param[in] count The number of elements you want to allocate.
1166 * @return The allocated result, NULL on error.
1168 void *talloc_array_size(const void *ctx, size_t size, unsigned count);
1169 #else
1170 #define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
1171 #endif
1173 #ifdef DOXYGEN
1175 * @brief Allocate an array into a typed pointer.
1177 * The macro should be used when you have a pointer to an array and want to
1178 * allocate memory of an array to point at with this pointer. When compiling
1179 * with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
1180 * and talloc_get_name() will return the current location in the source file
1181 * and not the type.
1183 * @param[in] ctx The talloc context to hang the result off.
1185 * @param[in] ptr The pointer you want to assign the result to.
1187 * @param[in] count The number of elements you want to allocate.
1189 * @return The allocated memory chunk, properly casted. NULL on
1190 * error.
1192 void *talloc_array_ptrtype(const void *ctx, const void *ptr, unsigned count);
1193 #else
1194 #define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
1195 #endif
1197 #ifdef DOXYGEN
1199 * @brief Get the number of elements in a talloc'ed array.
1201 * A talloc chunk carries its own size, so for talloc'ed arrays it is not
1202 * necessary to store the number of elements explicitly.
1204 * @param[in] ctx The allocated array.
1206 * @return The number of elements in ctx.
1208 size_t talloc_array_length(const void *ctx);
1209 #else
1210 #define talloc_array_length(ctx) (talloc_get_size(ctx)/sizeof(*ctx))
1211 #endif
1213 #ifdef DOXYGEN
1215 * @brief Allocate a zero-initialized array
1217 * @param[in] ctx The talloc context to hang the result off.
1219 * @param[in] type The type that we want to allocate.
1221 * @param[in] count The number of "type" elements you want to allocate.
1223 * @return The allocated result casted to "type *", NULL on error.
1225 * The talloc_zero_array() macro is equivalent to:
1227 * @code
1228 * ptr = talloc_array(ctx, type, count);
1229 * if (ptr) memset(ptr, sizeof(type) * count);
1230 * @endcode
1232 void *talloc_zero_array(const void *ctx, #type, unsigned count);
1233 #else
1234 #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
1235 void *_talloc_zero_array(const void *ctx,
1236 size_t el_size,
1237 unsigned count,
1238 const char *name);
1239 #endif
1241 #ifdef DOXYGEN
1243 * @brief Change the size of a talloc array.
1245 * The macro changes the size of a talloc pointer. The 'count' argument is the
1246 * number of elements of type 'type' that you want the resulting pointer to
1247 * hold.
1249 * talloc_realloc() has the following equivalences:
1251 * @code
1252 * talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
1253 * talloc_realloc(ctx, NULL, type, N) ==> talloc_array(ctx, type, N);
1254 * talloc_realloc(ctx, ptr, type, 0) ==> talloc_free(ptr);
1255 * @endcode
1257 * The "context" argument is only used if "ptr" is NULL, otherwise it is
1258 * ignored.
1260 * @param[in] ctx The parent context used if ptr is NULL.
1262 * @param[in] ptr The chunk to be resized.
1264 * @param[in] type The type of the array element inside ptr.
1266 * @param[in] count The intended number of array elements.
1268 * @return The new array, NULL on error. The call will fail either
1269 * due to a lack of memory, or because the pointer has more
1270 * than one parent (see talloc_reference()).
1272 void *talloc_realloc(const void *ctx, void *ptr, #type, size_t count);
1273 #else
1274 #define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
1275 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
1276 #endif
1278 #ifdef DOXYGEN
1280 * @brief Untyped realloc to change the size of a talloc array.
1282 * The macro is useful when the type is not known so the typesafe
1283 * talloc_realloc() cannot be used.
1285 * @param[in] ctx The parent context used if 'ptr' is NULL.
1287 * @param[in] ptr The chunk to be resized.
1289 * @param[in] size The new chunk size.
1291 * @return The new array, NULL on error.
1293 void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);
1294 #else
1295 #define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
1296 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
1297 #endif
1300 * @brief Provide a function version of talloc_realloc_size.
1302 * This is a non-macro version of talloc_realloc(), which is useful as
1303 * libraries sometimes want a ralloc function pointer. A realloc()
1304 * implementation encapsulates the functionality of malloc(), free() and
1305 * realloc() in one call, which is why it is useful to be able to pass around
1306 * a single function pointer.
1308 * @param[in] context The parent context used if ptr is NULL.
1310 * @param[in] ptr The chunk to be resized.
1312 * @param[in] size The new chunk size.
1314 * @return The new chunk, NULL on error.
1316 void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
1318 /* @} ******************************************************************/
1321 * @defgroup talloc_string The talloc string functions.
1322 * @ingroup talloc
1324 * talloc string allocation and manipulation functions.
1325 * @{
1329 * @brief Duplicate a string into a talloc chunk.
1331 * This function is equivalent to:
1333 * @code
1334 * ptr = talloc_size(ctx, strlen(p)+1);
1335 * if (ptr) memcpy(ptr, p, strlen(p)+1);
1336 * @endcode
1338 * This functions sets the name of the new pointer to the passed
1339 * string. This is equivalent to:
1341 * @code
1342 * talloc_set_name_const(ptr, ptr)
1343 * @endcode
1345 * @param[in] t The talloc context to hang the result off.
1347 * @param[in] p The string you want to duplicate.
1349 * @return The duplicated string, NULL on error.
1351 char *talloc_strdup(const void *t, const char *p);
1354 * @brief Append a string to given string.
1356 * The destination string is reallocated to take
1357 * <code>strlen(s) + strlen(a) + 1</code> characters.
1359 * This functions sets the name of the new pointer to the new
1360 * string. This is equivalent to:
1362 * @code
1363 * talloc_set_name_const(ptr, ptr)
1364 * @endcode
1366 * If <code>s == NULL</code> then new context is created.
1368 * @param[in] s The destination to append to.
1370 * @param[in] a The string you want to append.
1372 * @return The concatenated strings, NULL on error.
1374 * @see talloc_strdup()
1375 * @see talloc_strdup_append_buffer()
1377 char *talloc_strdup_append(char *s, const char *a);
1380 * @brief Append a string to a given buffer.
1382 * This is a more efficient version of talloc_strdup_append(). It determines the
1383 * length of the destination string by the size of the talloc context.
1385 * Use this very carefully as it produces a different result than
1386 * talloc_strdup_append() when a zero character is in the middle of the
1387 * destination string.
1389 * @code
1390 * char *str_a = talloc_strdup(NULL, "hello world");
1391 * char *str_b = talloc_strdup(NULL, "hello world");
1392 * str_a[5] = str_b[5] = '\0'
1394 * char *app = talloc_strdup_append(str_a, ", hello");
1395 * char *buf = talloc_strdup_append_buffer(str_b, ", hello");
1397 * printf("%s\n", app); // hello, hello (app = "hello, hello")
1398 * printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1399 * @endcode
1401 * If <code>s == NULL</code> then new context is created.
1403 * @param[in] s The destination buffer to append to.
1405 * @param[in] a The string you want to append.
1407 * @return The concatenated strings, NULL on error.
1409 * @see talloc_strdup()
1410 * @see talloc_strdup_append()
1411 * @see talloc_array_length()
1413 char *talloc_strdup_append_buffer(char *s, const char *a);
1416 * @brief Duplicate a length-limited string into a talloc chunk.
1418 * This function is the talloc equivalent of the C library function strndup(3).
1420 * This functions sets the name of the new pointer to the passed string. This is
1421 * equivalent to:
1423 * @code
1424 * talloc_set_name_const(ptr, ptr)
1425 * @endcode
1427 * @param[in] t The talloc context to hang the result off.
1429 * @param[in] p The string you want to duplicate.
1431 * @param[in] n The maximum string length to duplicate.
1433 * @return The duplicated string, NULL on error.
1435 char *talloc_strndup(const void *t, const char *p, size_t n);
1438 * @brief Append at most n characters of a string to given string.
1440 * The destination string is reallocated to take
1441 * <code>strlen(s) + strnlen(a, n) + 1</code> characters.
1443 * This functions sets the name of the new pointer to the new
1444 * string. This is equivalent to:
1446 * @code
1447 * talloc_set_name_const(ptr, ptr)
1448 * @endcode
1450 * If <code>s == NULL</code> then new context is created.
1452 * @param[in] s The destination string to append to.
1454 * @param[in] a The source string you want to append.
1456 * @param[in] n The number of characters you want to append from the
1457 * string.
1459 * @return The concatenated strings, NULL on error.
1461 * @see talloc_strndup()
1462 * @see talloc_strndup_append_buffer()
1464 char *talloc_strndup_append(char *s, const char *a, size_t n);
1467 * @brief Append at most n characters of a string to given buffer
1469 * This is a more efficient version of talloc_strndup_append(). It determines
1470 * the length of the destination string by the size of the talloc context.
1472 * Use this very carefully as it produces a different result than
1473 * talloc_strndup_append() when a zero character is in the middle of the
1474 * destination string.
1476 * @code
1477 * char *str_a = talloc_strdup(NULL, "hello world");
1478 * char *str_b = talloc_strdup(NULL, "hello world");
1479 * str_a[5] = str_b[5] = '\0'
1481 * char *app = talloc_strndup_append(str_a, ", hello", 7);
1482 * char *buf = talloc_strndup_append_buffer(str_b, ", hello", 7);
1484 * printf("%s\n", app); // hello, hello (app = "hello, hello")
1485 * printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1486 * @endcode
1488 * If <code>s == NULL</code> then new context is created.
1490 * @param[in] s The destination buffer to append to.
1492 * @param[in] a The source string you want to append.
1494 * @param[in] n The number of characters you want to append from the
1495 * string.
1497 * @return The concatenated strings, NULL on error.
1499 * @see talloc_strndup()
1500 * @see talloc_strndup_append()
1501 * @see talloc_array_length()
1503 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n);
1506 * @brief Format a string given a va_list.
1508 * This function is the talloc equivalent of the C library function
1509 * vasprintf(3).
1511 * This functions sets the name of the new pointer to the new string. This is
1512 * equivalent to:
1514 * @code
1515 * talloc_set_name_const(ptr, ptr)
1516 * @endcode
1518 * @param[in] t The talloc context to hang the result off.
1520 * @param[in] fmt The format string.
1522 * @param[in] ap The parameters used to fill fmt.
1524 * @return The formatted string, NULL on error.
1526 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1529 * @brief Format a string given a va_list and append it to the given destination
1530 * string.
1532 * @param[in] s The destination string to append to.
1534 * @param[in] fmt The format string.
1536 * @param[in] ap The parameters used to fill fmt.
1538 * @return The formatted string, NULL on error.
1540 * @see talloc_vasprintf()
1542 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1545 * @brief Format a string given a va_list and append it to the given destination
1546 * buffer.
1548 * @param[in] s The destination buffer to append to.
1550 * @param[in] fmt The format string.
1552 * @param[in] ap The parameters used to fill fmt.
1554 * @return The formatted string, NULL on error.
1556 * @see talloc_vasprintf()
1558 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1561 * @brief Format a string.
1563 * This function is the talloc equivalent of the C library function asprintf(3).
1565 * This functions sets the name of the new pointer to the new string. This is
1566 * equivalent to:
1568 * @code
1569 * talloc_set_name_const(ptr, ptr)
1570 * @endcode
1572 * @param[in] t The talloc context to hang the result off.
1574 * @param[in] fmt The format string.
1576 * @param[in] ... The parameters used to fill fmt.
1578 * @return The formatted string, NULL on error.
1580 char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1583 * @brief Append a formatted string to another string.
1585 * This function appends the given formatted string to the given string. Use
1586 * this variant when the string in the current talloc buffer may have been
1587 * truncated in length.
1589 * This functions sets the name of the new pointer to the new
1590 * string. This is equivalent to:
1592 * @code
1593 * talloc_set_name_const(ptr, ptr)
1594 * @endcode
1596 * If <code>s == NULL</code> then new context is created.
1598 * @param[in] s The string to append to.
1600 * @param[in] fmt The format string.
1602 * @param[in] ... The parameters used to fill fmt.
1604 * @return The formatted string, NULL on error.
1606 char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1609 * @brief Append a formatted string to another string.
1611 * This is a more efficient version of talloc_asprintf_append(). It determines
1612 * the length of the destination string by the size of the talloc context.
1614 * Use this very carefully as it produces a different result than
1615 * talloc_asprintf_append() when a zero character is in the middle of the
1616 * destination string.
1618 * @code
1619 * char *str_a = talloc_strdup(NULL, "hello world");
1620 * char *str_b = talloc_strdup(NULL, "hello world");
1621 * str_a[5] = str_b[5] = '\0'
1623 * char *app = talloc_asprintf_append(str_a, "%s", ", hello");
1624 * char *buf = talloc_strdup_append_buffer(str_b, "%s", ", hello");
1626 * printf("%s\n", app); // hello, hello (app = "hello, hello")
1627 * printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1628 * @endcode
1630 * If <code>s == NULL</code> then new context is created.
1632 * @param[in] s The string to append to
1634 * @param[in] fmt The format string.
1636 * @param[in] ... The parameters used to fill fmt.
1638 * @return The formatted string, NULL on error.
1640 * @see talloc_asprintf()
1641 * @see talloc_asprintf_append()
1643 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1645 /* @} ******************************************************************/
1648 * @defgroup talloc_debug The talloc debugging support functions
1649 * @ingroup talloc
1651 * To aid memory debugging, talloc contains routines to inspect the currently
1652 * allocated memory hierarchy.
1654 * @{
1658 * @brief Walk a complete talloc hierarchy.
1660 * This provides a more flexible reports than talloc_report(). It
1661 * will recursively call the callback for the entire tree of memory
1662 * referenced by the pointer. References in the tree are passed with
1663 * is_ref = 1 and the pointer that is referenced.
1665 * You can pass NULL for the pointer, in which case a report is
1666 * printed for the top level memory context, but only if
1667 * talloc_enable_leak_report() or talloc_enable_leak_report_full()
1668 * has been called.
1670 * The recursion is stopped when depth >= max_depth.
1671 * max_depth = -1 means only stop at leaf nodes.
1673 * @param[in] ptr The talloc chunk.
1675 * @param[in] depth Internal parameter to control recursion. Call with 0.
1677 * @param[in] max_depth Maximum recursion level.
1679 * @param[in] callback Function to be called on every chunk.
1681 * @param[in] private_data Private pointer passed to callback.
1683 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1684 void (*callback)(const void *ptr,
1685 int depth, int max_depth,
1686 int is_ref,
1687 void *private_data),
1688 void *private_data);
1691 * @brief Print a talloc hierarchy.
1693 * This provides a more flexible reports than talloc_report(). It
1694 * will let you specify the depth and max_depth.
1696 * @param[in] ptr The talloc chunk.
1698 * @param[in] depth Internal parameter to control recursion. Call with 0.
1700 * @param[in] max_depth Maximum recursion level.
1702 * @param[in] f The file handle to print to.
1704 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
1707 * @brief Print a summary report of all memory used by ptr.
1709 * This provides a more detailed report than talloc_report(). It will
1710 * recursively print the entire tree of memory referenced by the
1711 * pointer. References in the tree are shown by giving the name of the
1712 * pointer that is referenced.
1714 * You can pass NULL for the pointer, in which case a report is printed
1715 * for the top level memory context, but only if
1716 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
1717 * been called.
1719 * @param[in] ptr The talloc chunk.
1721 * @param[in] f The file handle to print to.
1723 * Example:
1724 * @code
1725 * unsigned int *a, *b;
1726 * a = talloc(NULL, unsigned int);
1727 * b = talloc(a, unsigned int);
1728 * fprintf(stderr, "Dumping memory tree for a:\n");
1729 * talloc_report_full(a, stderr);
1730 * @endcode
1732 * @see talloc_report()
1734 void talloc_report_full(const void *ptr, FILE *f);
1737 * @brief Print a summary report of all memory used by ptr.
1739 * This function prints a summary report of all memory used by ptr. One line of
1740 * report is printed for each immediate child of ptr, showing the total memory
1741 * and number of blocks used by that child.
1743 * You can pass NULL for the pointer, in which case a report is printed
1744 * for the top level memory context, but only if talloc_enable_leak_report()
1745 * or talloc_enable_leak_report_full() has been called.
1747 * @param[in] ptr The talloc chunk.
1749 * @param[in] f The file handle to print to.
1751 * Example:
1752 * @code
1753 * unsigned int *a, *b;
1754 * a = talloc(NULL, unsigned int);
1755 * b = talloc(a, unsigned int);
1756 * fprintf(stderr, "Summary of memory tree for a:\n");
1757 * talloc_report(a, stderr);
1758 * @endcode
1760 * @see talloc_report_full()
1762 void talloc_report(const void *ptr, FILE *f);
1765 * @brief Enable tracking the use of NULL memory contexts.
1767 * This enables tracking of the NULL memory context without enabling leak
1768 * reporting on exit. Useful for when you want to do your own leak
1769 * reporting call via talloc_report_null_full();
1771 void talloc_enable_null_tracking(void);
1774 * @brief Enable tracking the use of NULL memory contexts.
1776 * This enables tracking of the NULL memory context without enabling leak
1777 * reporting on exit. Useful for when you want to do your own leak
1778 * reporting call via talloc_report_null_full();
1780 void talloc_enable_null_tracking_no_autofree(void);
1783 * @brief Disable tracking of the NULL memory context.
1785 * This disables tracking of the NULL memory context.
1787 void talloc_disable_null_tracking(void);
1790 * @brief Enable leak report when a program exits.
1792 * This enables calling of talloc_report(NULL, stderr) when the program
1793 * exits. In Samba4 this is enabled by using the --leak-report command
1794 * line option.
1796 * For it to be useful, this function must be called before any other
1797 * talloc function as it establishes a "null context" that acts as the
1798 * top of the tree. If you don't call this function first then passing
1799 * NULL to talloc_report() or talloc_report_full() won't give you the
1800 * full tree printout.
1802 * Here is a typical talloc report:
1804 * @code
1805 * talloc report on 'null_context' (total 267 bytes in 15 blocks)
1806 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
1807 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
1808 * iconv(UTF8,CP850) contains 42 bytes in 2 blocks
1809 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
1810 * iconv(CP850,UTF8) contains 42 bytes in 2 blocks
1811 * iconv(UTF8,UTF-16LE) contains 45 bytes in 2 blocks
1812 * iconv(UTF-16LE,UTF8) contains 45 bytes in 2 blocks
1813 * @endcode
1815 void talloc_enable_leak_report(void);
1818 * @brief Enable full leak report when a program exits.
1820 * This enables calling of talloc_report_full(NULL, stderr) when the
1821 * program exits. In Samba4 this is enabled by using the
1822 * --leak-report-full command line option.
1824 * For it to be useful, this function must be called before any other
1825 * talloc function as it establishes a "null context" that acts as the
1826 * top of the tree. If you don't call this function first then passing
1827 * NULL to talloc_report() or talloc_report_full() won't give you the
1828 * full tree printout.
1830 * Here is a typical full report:
1832 * @code
1833 * full talloc report on 'root' (total 18 bytes in 8 blocks)
1834 * p1 contains 18 bytes in 7 blocks (ref 0)
1835 * r1 contains 13 bytes in 2 blocks (ref 0)
1836 * reference to: p2
1837 * p2 contains 1 bytes in 1 blocks (ref 1)
1838 * x3 contains 1 bytes in 1 blocks (ref 0)
1839 * x2 contains 1 bytes in 1 blocks (ref 0)
1840 * x1 contains 1 bytes in 1 blocks (ref 0)
1841 * @endcode
1843 void talloc_enable_leak_report_full(void);
1846 * @brief Set a custom "abort" function that is called on serious error.
1848 * The default "abort" function is <code>abort()</code>.
1850 * The "abort" function is called when:
1852 * <ul>
1853 * <li>talloc_get_type_abort() fails</li>
1854 * <li>the provided pointer is not a valid talloc context</li>
1855 * <li>when the context meta data are invalid</li>
1856 * <li>when access after free is detected</li>
1857 * </ul>
1859 * Example:
1861 * @code
1862 * void my_abort(const char *reason)
1864 * fprintf(stderr, "talloc abort: %s\n", reason);
1865 * abort();
1868 * talloc_set_abort_fn(my_abort);
1869 * @endcode
1871 * @param[in] abort_fn The new "abort" function.
1873 * @see talloc_set_log_fn()
1874 * @see talloc_get_type()
1876 void talloc_set_abort_fn(void (*abort_fn)(const char *reason));
1879 * @brief Set a logging function.
1881 * @param[in] log_fn The logging function.
1883 * @see talloc_set_log_stderr()
1884 * @see talloc_set_abort_fn()
1886 void talloc_set_log_fn(void (*log_fn)(const char *message));
1889 * @brief Set stderr as the output for logs.
1891 * @see talloc_set_log_fn()
1892 * @see talloc_set_abort_fn()
1894 void talloc_set_log_stderr(void);
1897 * @brief Set a max memory limit for the current context hierarchy
1898 * This affects all children of this context and constrain any
1899 * allocation in the hierarchy to never exceed the limit set.
1900 * The limit can be removed by setting 0 (unlimited) as the
1901 * max_size by calling the funciton again on the sam context.
1902 * Memory limits can also be nested, meaning a hild can have
1903 * a stricter memory limit than a parent.
1904 * Memory limits are enforced only at memory allocation time.
1905 * Stealing a context into a 'limited' hierarchy properly
1906 * updates memory usage but does *not* cause failure if the
1907 * move causes the new parent to exceed its limits. However
1908 * any further allocation on that hierarchy will then fail.
1910 * @param[in] ctx The talloc context to set the limit on
1911 * @param[in] max_size The (new) max_size
1913 int talloc_set_memlimit(const void *ctx, size_t max_size);
1915 /* @} ******************************************************************/
1917 #if TALLOC_DEPRECATED
1918 #define talloc_zero_p(ctx, type) talloc_zero(ctx, type)
1919 #define talloc_p(ctx, type) talloc(ctx, type)
1920 #define talloc_array_p(ctx, type, count) talloc_array(ctx, type, count)
1921 #define talloc_realloc_p(ctx, p, type, count) talloc_realloc(ctx, p, type, count)
1922 #define talloc_destroy(ctx) talloc_free(ctx)
1923 #define talloc_append_string(c, s, a) (s?talloc_strdup_append(s,a):talloc_strdup(c, a))
1924 #endif
1926 #ifndef TALLOC_MAX_DEPTH
1927 #define TALLOC_MAX_DEPTH 10000
1928 #endif
1930 #ifdef __cplusplus
1931 } /* end of extern "C" */
1932 #endif
1934 #endif