auth/credentials: don't ignore "client use kerberos" and --use-kerberos for machine...
[Samba.git] / lib / talloc / talloc.h
blobeef1a701b1f3b473fe537cf3f2e6bbca04b424e9
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 /* for old gcc releases that don't have the feature test macro __has_attribute */
37 #ifndef __has_attribute
38 #define __has_attribute(x) 0
39 #endif
41 #ifndef _PUBLIC_
42 #if __has_attribute(visibility)
43 #define _PUBLIC_ __attribute__((visibility("default")))
44 #else
45 #define _PUBLIC_
46 #endif
47 #endif
49 /**
50 * @defgroup talloc The talloc API
52 * talloc is a hierarchical, reference counted memory pool system with
53 * destructors. It is the core memory allocator used in Samba.
55 * @{
58 #define TALLOC_VERSION_MAJOR 2
59 #define TALLOC_VERSION_MINOR 4
61 _PUBLIC_ int talloc_version_major(void);
62 _PUBLIC_ int talloc_version_minor(void);
63 /* This is mostly useful only for testing */
64 _PUBLIC_ int talloc_test_get_magic(void);
66 /**
67 * @brief Define a talloc parent type
69 * As talloc is a hierarchial memory allocator, every talloc chunk is a
70 * potential parent to other talloc chunks. So defining a separate type for a
71 * talloc chunk is not strictly necessary. TALLOC_CTX is defined nevertheless,
72 * as it provides an indicator for function arguments. You will frequently
73 * write code like
75 * @code
76 * struct foo *foo_create(TALLOC_CTX *mem_ctx)
77 * {
78 * struct foo *result;
79 * result = talloc(mem_ctx, struct foo);
80 * if (result == NULL) return NULL;
81 * ... initialize foo ...
82 * return result;
83 * }
84 * @endcode
86 * In this type of allocating functions it is handy to have a general
87 * TALLOC_CTX type to indicate which parent to put allocated structures on.
89 typedef void TALLOC_CTX;
92 this uses a little trick to allow __LINE__ to be stringified
94 #ifndef __location__
95 #define __TALLOC_STRING_LINE1__(s) #s
96 #define __TALLOC_STRING_LINE2__(s) __TALLOC_STRING_LINE1__(s)
97 #define __TALLOC_STRING_LINE3__ __TALLOC_STRING_LINE2__(__LINE__)
98 #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__
99 #endif
101 #ifndef TALLOC_DEPRECATED
102 #define TALLOC_DEPRECATED 0
103 #endif
105 #ifndef PRINTF_ATTRIBUTE
106 #if __has_attribute(format) || (__GNUC__ >= 3)
107 /** Use gcc attribute to check printf fns. a1 is the 1-based index of
108 * the parameter containing the format, and a2 the index of the first
109 * argument. Note that some gcc 2.x versions don't handle this
110 * properly **/
111 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
112 #else
113 #define PRINTF_ATTRIBUTE(a1, a2)
114 #endif
115 #endif
117 #ifndef _DEPRECATED_
118 #if __has_attribute(deprecated) || (__GNUC__ >= 3)
119 #define _DEPRECATED_ __attribute__ ((deprecated))
120 #else
121 #define _DEPRECATED_
122 #endif
123 #endif
124 #ifdef DOXYGEN
127 * @brief Create a new talloc context.
129 * The talloc() macro is the core of the talloc library. It takes a memory
130 * context and a type, and returns a pointer to a new area of memory of the
131 * given type.
133 * The returned pointer is itself a talloc context, so you can use it as the
134 * context argument to more calls to talloc if you wish.
136 * The returned pointer is a "child" of the supplied context. This means that if
137 * you talloc_free() the context then the new child disappears as well.
138 * Alternatively you can free just the child.
140 * @param[in] ctx A talloc context to create a new reference on or NULL to
141 * create a new top level context.
143 * @param[in] type The type of memory to allocate.
145 * @return A type casted talloc context or NULL on error.
147 * @code
148 * unsigned int *a, *b;
150 * a = talloc(NULL, unsigned int);
151 * b = talloc(a, unsigned int);
152 * @endcode
154 * @see talloc_zero
155 * @see talloc_array
156 * @see talloc_steal
157 * @see talloc_free
159 _PUBLIC_ void *talloc(const void *ctx, #type);
160 #else
161 #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
162 _PUBLIC_ void *_talloc(const void *context, size_t size);
163 #endif
166 * @brief Create a new top level talloc context.
168 * This function creates a zero length named talloc context as a top level
169 * context. It is equivalent to:
171 * @code
172 * talloc_named(NULL, 0, fmt, ...);
173 * @endcode
174 * @param[in] fmt Format string for the name.
176 * @param[in] ... Additional printf-style arguments.
178 * @return The allocated memory chunk, NULL on error.
180 * @see talloc_named()
182 _PUBLIC_ void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
184 #ifdef DOXYGEN
186 * @brief Free a chunk of talloc memory.
188 * The talloc_free() function frees a piece of talloc memory, and all its
189 * children. You can call talloc_free() on any pointer returned by
190 * talloc().
192 * The return value of talloc_free() indicates success or failure, with 0
193 * returned for success and -1 for failure. A possible failure condition
194 * is if the pointer had a destructor attached to it and the destructor
195 * returned -1. See talloc_set_destructor() for details on
196 * destructors. Likewise, if "ptr" is NULL, then the function will make
197 * no modifications and return -1.
199 * From version 2.0 and onwards, as a special case, talloc_free() is
200 * refused on pointers that have more than one parent associated, as talloc
201 * would have no way of knowing which parent should be removed. This is
202 * different from older versions in the sense that always the reference to
203 * the most recently established parent has been destroyed. Hence to free a
204 * pointer that has more than one parent please use talloc_unlink().
206 * To help you find problems in your code caused by this behaviour, if
207 * you do try and free a pointer with more than one parent then the
208 * talloc logging function will be called to give output like this:
210 * @code
211 * ERROR: talloc_free with references at some_dir/source/foo.c:123
212 * reference at some_dir/source/other.c:325
213 * reference at some_dir/source/third.c:121
214 * @endcode
216 * Please see the documentation for talloc_set_log_fn() and
217 * talloc_set_log_stderr() for more information on talloc logging
218 * functions.
220 * If <code>TALLOC_FREE_FILL</code> environment variable is set,
221 * the memory occupied by the context is filled with the value of this variable.
222 * The value should be a numeric representation of the character you want to
223 * use.
225 * talloc_free() operates recursively on its children.
227 * @param[in] ptr The chunk to be freed.
229 * @return Returns 0 on success and -1 on error. A possible
230 * failure condition is if the pointer had a destructor
231 * attached to it and the destructor returned -1. Likewise,
232 * if "ptr" is NULL, then the function will make no
233 * modifications and returns -1.
235 * Example:
236 * @code
237 * unsigned int *a, *b;
238 * a = talloc(NULL, unsigned int);
239 * b = talloc(a, unsigned int);
241 * talloc_free(a); // Frees a and b
242 * @endcode
244 * @see talloc_set_destructor()
245 * @see talloc_unlink()
247 _PUBLIC_ int talloc_free(void *ptr);
248 #else
249 #define talloc_free(ctx) _talloc_free(ctx, __location__)
250 _PUBLIC_ int _talloc_free(void *ptr, const char *location);
251 #endif
254 * @brief Free a talloc chunk's children.
256 * The function walks along the list of all children of a talloc context and
257 * talloc_free()s only the children, not the context itself.
259 * A NULL argument is handled as no-op.
261 * @param[in] ptr The chunk that you want to free the children of
262 * (NULL is allowed too)
264 _PUBLIC_ void talloc_free_children(void *ptr);
266 #ifdef DOXYGEN
268 * @brief Assign a destructor function to be called when a chunk is freed.
270 * The function talloc_set_destructor() sets the "destructor" for the pointer
271 * "ptr". A destructor is a function that is called when the memory used by a
272 * pointer is about to be released. The destructor receives the pointer as an
273 * argument, and should return 0 for success and -1 for failure.
275 * The destructor can do anything it wants to, including freeing other pieces
276 * of memory. A common use for destructors is to clean up operating system
277 * resources (such as open file descriptors) contained in the structure the
278 * destructor is placed on.
280 * You can only place one destructor on a pointer. If you need more than one
281 * destructor then you can create a zero-length child of the pointer and place
282 * an additional destructor on that.
284 * To remove a destructor call talloc_set_destructor() with NULL for the
285 * destructor.
287 * If your destructor attempts to talloc_free() the pointer that it is the
288 * destructor for then talloc_free() will return -1 and the free will be
289 * ignored. This would be a pointless operation anyway, as the destructor is
290 * only called when the memory is just about to go away.
292 * @param[in] ptr The talloc chunk to add a destructor to.
294 * @param[in] destructor The destructor function to be called. NULL to remove
295 * it.
297 * Example:
298 * @code
299 * static int destroy_fd(int *fd) {
300 * close(*fd);
301 * return 0;
304 * int *open_file(const char *filename) {
305 * int *fd = talloc(NULL, int);
306 * *fd = open(filename, O_RDONLY);
307 * if (*fd < 0) {
308 * talloc_free(fd);
309 * return NULL;
311 * // Whenever they free this, we close the file.
312 * talloc_set_destructor(fd, destroy_fd);
313 * return fd;
315 * @endcode
317 * @see talloc()
318 * @see talloc_free()
320 _PUBLIC_ void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
323 * @brief Change a talloc chunk's parent.
325 * The talloc_steal() function changes the parent context of a talloc
326 * pointer. It is typically used when the context that the pointer is
327 * currently a child of is going to be freed and you wish to keep the
328 * memory for a longer time.
330 * To make the changed hierarchy less error-prone, you might consider to use
331 * talloc_move().
333 * If you try and call talloc_steal() on a pointer that has more than one
334 * parent then the result is ambiguous. Talloc will choose to remove the
335 * parent that is currently indicated by talloc_parent() and replace it with
336 * the chosen parent. You will also get a message like this via the talloc
337 * logging functions:
339 * @code
340 * WARNING: talloc_steal with references at some_dir/source/foo.c:123
341 * reference at some_dir/source/other.c:325
342 * reference at some_dir/source/third.c:121
343 * @endcode
345 * To unambiguously change the parent of a pointer please see the function
346 * talloc_reparent(). See the talloc_set_log_fn() documentation for more
347 * information on talloc logging.
349 * @param[in] new_ctx The new parent context.
351 * @param[in] ptr The talloc chunk to move.
353 * @return Returns the pointer that you pass it. It does not have
354 * any failure modes.
356 * @note It is possible to produce loops in the parent/child relationship
357 * if you are not careful with talloc_steal(). No guarantees are provided
358 * as to your sanity or the safety of your data if you do this.
360 _PUBLIC_ void *talloc_steal(const void *new_ctx, const void *ptr);
361 #else /* DOXYGEN */
362 /* try to make talloc_set_destructor() and talloc_steal() type safe,
363 if we have a recent gcc */
364 #if (__GNUC__ >= 3)
365 #define _TALLOC_TYPEOF(ptr) __typeof__(ptr)
366 #define talloc_set_destructor(ptr, function) \
367 do { \
368 int (*_talloc_destructor_fn)(_TALLOC_TYPEOF(ptr)) = (function); \
369 _talloc_set_destructor((ptr), (int (*)(void *))_talloc_destructor_fn); \
370 } while(0)
371 /* this extremely strange macro is to avoid some braindamaged warning
372 stupidity in gcc 4.1.x */
373 #define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__); __talloc_steal_ret; })
374 #else /* __GNUC__ >= 3 */
375 #define talloc_set_destructor(ptr, function) \
376 _talloc_set_destructor((ptr), (int (*)(void *))(function))
377 #define _TALLOC_TYPEOF(ptr) void *
378 #define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__)
379 #endif /* __GNUC__ >= 3 */
380 _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*_destructor)(void *));
381 _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location);
382 #endif /* DOXYGEN */
385 * @brief Assign a name to a talloc chunk.
387 * Each talloc pointer has a "name". The name is used principally for
388 * debugging purposes, although it is also possible to set and get the name on
389 * a pointer in as a way of "marking" pointers in your code.
391 * The main use for names on pointer is for "talloc reports". See
392 * talloc_report() and talloc_report_full() for details. Also see
393 * talloc_enable_leak_report() and talloc_enable_leak_report_full().
395 * The talloc_set_name() function allocates memory as a child of the
396 * pointer. It is logically equivalent to:
398 * @code
399 * talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
400 * @endcode
402 * @param[in] ptr The talloc chunk to assign a name to.
404 * @param[in] fmt Format string for the name.
406 * @param[in] ... Add printf-style additional arguments.
408 * @return The assigned name, NULL on error.
410 * @note Multiple calls to talloc_set_name() will allocate more memory without
411 * releasing the name. All of the memory is released when the ptr is freed
412 * using talloc_free().
414 _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
416 #ifdef DOXYGEN
418 * @brief Change a talloc chunk's parent.
420 * This function has the same effect as talloc_steal(), and additionally sets
421 * the source pointer to NULL. You would use it like this:
423 * @code
424 * struct foo *X = talloc(tmp_ctx, struct foo);
425 * struct foo *Y;
426 * Y = talloc_move(new_ctx, &X);
427 * @endcode
429 * @param[in] new_ctx The new parent context.
431 * @param[in] pptr Pointer to a pointer to the talloc chunk to move.
433 * @return The pointer to the talloc chunk that moved.
434 * It does not have any failure modes.
437 _PUBLIC_ void *talloc_move(const void *new_ctx, void **pptr);
438 #else
439 #define talloc_move(ctx, pptr) (_TALLOC_TYPEOF(*(pptr)))_talloc_move((ctx),(void *)(pptr))
440 _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *pptr);
441 #endif
444 * @brief Assign a name to a talloc chunk.
446 * The function is just like talloc_set_name(), but it takes a string constant,
447 * and is much faster. It is extensively used by the "auto naming" macros, such
448 * as talloc_p().
450 * This function does not allocate any memory. It just copies the supplied
451 * pointer into the internal representation of the talloc ptr. This means you
452 * must not pass a name pointer to memory that will disappear before the ptr
453 * is freed with talloc_free().
455 * @param[in] ptr The talloc chunk to assign a name to.
457 * @param[in] name Format string for the name.
459 _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name);
462 * @brief Create a named talloc chunk.
464 * The talloc_named() function creates a named talloc pointer. It is
465 * equivalent to:
467 * @code
468 * ptr = talloc_size(context, size);
469 * talloc_set_name(ptr, fmt, ....);
470 * @endcode
472 * @param[in] context The talloc context to hang the result off.
474 * @param[in] size Number of char's that you want to allocate.
476 * @param[in] fmt Format string for the name.
478 * @param[in] ... Additional printf-style arguments.
480 * @return The allocated memory chunk, NULL on error.
482 * @see talloc_set_name()
484 _PUBLIC_ void *talloc_named(const void *context, size_t size,
485 const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
488 * @brief Basic routine to allocate a chunk of memory.
490 * This is equivalent to:
492 * @code
493 * ptr = talloc_size(context, size);
494 * talloc_set_name_const(ptr, name);
495 * @endcode
497 * @param[in] context The parent context.
499 * @param[in] size The number of char's that we want to allocate.
501 * @param[in] name The name the talloc block has.
503 * @return The allocated memory chunk, NULL on error.
505 _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name);
507 #ifdef DOXYGEN
509 * @brief Untyped allocation.
511 * The function should be used when you don't have a convenient type to pass to
512 * talloc(). Unlike talloc(), it is not type safe (as it returns a void *), so
513 * you are on your own for type checking.
515 * Best to use talloc() or talloc_array() instead.
517 * @param[in] ctx The talloc context to hang the result off.
519 * @param[in] size Number of char's that you want to allocate.
521 * @return The allocated memory chunk, NULL on error.
523 * Example:
524 * @code
525 * void *mem = talloc_size(NULL, 100);
526 * @endcode
528 _PUBLIC_ void *talloc_size(const void *ctx, size_t size);
529 #else
530 #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
531 #endif
533 #ifdef DOXYGEN
535 * @brief Allocate into a typed pointer.
537 * The talloc_ptrtype() macro should be used when you have a pointer and want
538 * to allocate memory to point at with this pointer. When compiling with
539 * gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size() and
540 * talloc_get_name() will return the current location in the source file and
541 * not the type.
543 * @param[in] ctx The talloc context to hang the result off.
545 * @param[in] type The pointer you want to assign the result to.
547 * @return The properly casted allocated memory chunk, NULL on
548 * error.
550 * Example:
551 * @code
552 * unsigned int *a = talloc_ptrtype(NULL, a);
553 * @endcode
555 _PUBLIC_ void *talloc_ptrtype(const void *ctx, #type);
556 #else
557 #define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
558 #endif
560 #ifdef DOXYGEN
562 * @brief Allocate a new 0-sized talloc chunk.
564 * This is a utility macro that creates a new memory context hanging off an
565 * existing context, automatically naming it "talloc_new: __location__" where
566 * __location__ is the source line it is called from. It is particularly
567 * useful for creating a new temporary working context.
569 * @param[in] ctx The talloc parent context.
571 * @return A new talloc chunk, NULL on error.
573 _PUBLIC_ void *talloc_new(const void *ctx);
574 #else
575 #define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
576 #endif
578 #ifdef DOXYGEN
580 * @brief Allocate a 0-initizialized structure.
582 * The macro is equivalent to:
584 * @code
585 * ptr = talloc(ctx, type);
586 * if (ptr) memset(ptr, 0, sizeof(type));
587 * @endcode
589 * @param[in] ctx The talloc context to hang the result off.
591 * @param[in] type The type that we want to allocate.
593 * @return Pointer to a piece of memory, properly cast to 'type *',
594 * NULL on error.
596 * Example:
597 * @code
598 * unsigned int *a, *b;
599 * a = talloc_zero(NULL, unsigned int);
600 * b = talloc_zero(a, unsigned int);
601 * @endcode
603 * @see talloc()
604 * @see talloc_zero_size()
605 * @see talloc_zero_array()
607 _PUBLIC_ void *talloc_zero(const void *ctx, #type);
610 * @brief Allocate untyped, 0-initialized memory.
612 * @param[in] ctx The talloc context to hang the result off.
614 * @param[in] size Number of char's that you want to allocate.
616 * @return The allocated memory chunk.
618 _PUBLIC_ void *talloc_zero_size(const void *ctx, size_t size);
619 #else
620 #define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
621 #define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
622 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name);
623 #endif
626 * @brief Return the name of a talloc chunk.
628 * @param[in] ptr The talloc chunk.
630 * @return The current name for the given talloc pointer.
632 * @see talloc_set_name()
634 _PUBLIC_ const char *talloc_get_name(const void *ptr);
637 * @brief Verify that a talloc chunk carries a specified name.
639 * This function checks if a pointer has the specified name. If it does
640 * then the pointer is returned.
642 * @param[in] ptr The talloc chunk to check.
644 * @param[in] name The name to check against.
646 * @return The pointer if the name matches, NULL if it doesn't.
648 _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name);
651 * @brief Get the parent chunk of a pointer.
653 * @param[in] ptr The talloc pointer to inspect.
655 * @return The talloc parent of ptr, NULL on error.
657 _PUBLIC_ void *talloc_parent(const void *ptr);
660 * @brief Get a talloc chunk's parent name.
662 * @param[in] ptr The talloc pointer to inspect.
664 * @return The name of ptr's parent chunk.
666 _PUBLIC_ const char *talloc_parent_name(const void *ptr);
669 * @brief Get the size of a talloc chunk.
671 * This function lets you know the amount of memory allocated so far by
672 * this context. It does NOT account for subcontext memory.
673 * This can be used to calculate the size of an array.
675 * @param[in] ctx The talloc chunk.
677 * @return The size of the talloc chunk.
679 _PUBLIC_ size_t talloc_get_size(const void *ctx);
682 * @brief Get the total size of a talloc chunk including its children.
684 * The function returns the total size in bytes used by this pointer and all
685 * child pointers. Mostly useful for debugging.
687 * Passing NULL is allowed, but it will only give a meaningful result if
688 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
689 * been called.
691 * @param[in] ptr The talloc chunk.
693 * @return The total size.
695 _PUBLIC_ size_t talloc_total_size(const void *ptr);
698 * @brief Get the number of talloc chunks hanging off a chunk.
700 * The talloc_total_blocks() function returns the total memory block
701 * count used by this pointer and all child pointers. Mostly useful for
702 * debugging.
704 * Passing NULL is allowed, but it will only give a meaningful result if
705 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
706 * been called.
708 * @param[in] ptr The talloc chunk.
710 * @return The total size.
712 _PUBLIC_ size_t talloc_total_blocks(const void *ptr);
714 #ifdef DOXYGEN
716 * @brief Duplicate a memory area into a talloc chunk.
718 * The function is equivalent to:
720 * @code
721 * ptr = talloc_size(ctx, size);
722 * if (ptr) memcpy(ptr, p, size);
723 * @endcode
725 * @param[in] t The talloc context to hang the result off.
727 * @param[in] p The memory chunk you want to duplicate.
729 * @param[in] size Number of chars that you want to copy.
731 * @return The allocated memory chunk.
733 * @see talloc_size()
735 _PUBLIC_ void *talloc_memdup(const void *t, const void *p, size_t size);
736 #else
737 #define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
738 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
739 #endif
741 #ifdef DOXYGEN
743 * @brief Assign a type to a talloc chunk.
745 * This macro allows you to force the name of a pointer to be of a particular
746 * type. This can be used in conjunction with talloc_get_type() to do type
747 * checking on void* pointers.
749 * It is equivalent to this:
751 * @code
752 * talloc_set_name_const(ptr, #type)
753 * @endcode
755 * @param[in] ptr The talloc chunk to assign the type to.
757 * @param[in] type The type to assign.
759 _PUBLIC_ void talloc_set_type(const char *ptr, #type);
762 * @brief Get a typed pointer out of a talloc pointer.
764 * This macro allows you to do type checking on talloc pointers. It is
765 * particularly useful for void* private pointers. It is equivalent to
766 * this:
768 * @code
769 * (type *)talloc_check_name(ptr, #type)
770 * @endcode
772 * @param[in] ptr The talloc pointer to check.
774 * @param[in] type The type to check against.
776 * @return The properly casted pointer given by ptr, NULL on error.
778 type *talloc_get_type(const void *ptr, #type);
779 #else
780 #define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
781 #define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
782 #endif
784 #ifdef DOXYGEN
786 * @brief Safely turn a void pointer into a typed pointer.
788 * This macro is used together with talloc(mem_ctx, struct foo). If you had to
789 * assign the talloc chunk pointer to some void pointer variable,
790 * talloc_get_type_abort() is the recommended way to convert the void
791 * pointer back to a typed pointer.
793 * @param[in] ptr The void pointer to convert.
795 * @param[in] type The type that this chunk contains
797 * @return The same value as ptr, type-checked and properly cast.
799 _PUBLIC_ void *talloc_get_type_abort(const void *ptr, #type);
800 #else
801 #ifdef TALLOC_GET_TYPE_ABORT_NOOP
802 #define talloc_get_type_abort(ptr, type) (type *)(ptr)
803 #else
804 #define talloc_get_type_abort(ptr, type) (type *)_talloc_get_type_abort(ptr, #type, __location__)
805 #endif
806 _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location);
807 #endif
810 * @brief Find a parent context by name.
812 * Find a parent memory context of the current context that has the given
813 * name. This can be very useful in complex programs where it may be
814 * difficult to pass all information down to the level you need, but you
815 * know the structure you want is a parent of another context.
817 * @param[in] ctx The talloc chunk to start from.
819 * @param[in] name The name of the parent we look for.
821 * @return The memory context we are looking for, NULL if not
822 * found.
824 _PUBLIC_ void *talloc_find_parent_byname(const void *ctx, const char *name);
826 #ifdef DOXYGEN
828 * @brief Find a parent context by type.
830 * Find a parent memory context of the current context that has the given
831 * name. This can be very useful in complex programs where it may be
832 * difficult to pass all information down to the level you need, but you
833 * know the structure you want is a parent of another context.
835 * Like talloc_find_parent_byname() but takes a type, making it typesafe.
837 * @param[in] ptr The talloc chunk to start from.
839 * @param[in] type The type of the parent to look for.
841 * @return The memory context we are looking for, NULL if not
842 * found.
844 _PUBLIC_ void *talloc_find_parent_bytype(const void *ptr, #type);
845 #else
846 #define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
847 #endif
850 * @brief Allocate a talloc pool.
852 * A talloc pool is a pure optimization for specific situations. In the
853 * release process for Samba 3.2 we found out that we had become considerably
854 * slower than Samba 3.0 was. Profiling showed that malloc(3) was a large CPU
855 * consumer in benchmarks. For Samba 3.2 we have internally converted many
856 * static buffers to dynamically allocated ones, so malloc(3) being beaten
857 * more was no surprise. But it made us slower.
859 * talloc_pool() is an optimization to call malloc(3) a lot less for the use
860 * pattern Samba has: The SMB protocol is mainly a request/response protocol
861 * where we have to allocate a certain amount of memory per request and free
862 * that after the SMB reply is sent to the client.
864 * talloc_pool() creates a talloc chunk that you can use as a talloc parent
865 * exactly as you would use any other ::TALLOC_CTX. The difference is that
866 * when you talloc a child of this pool, no malloc(3) is done. Instead, talloc
867 * just increments a pointer inside the talloc_pool. This also works
868 * recursively. If you use the child of the talloc pool as a parent for
869 * grand-children, their memory is also taken from the talloc pool.
871 * If there is not enough memory in the pool to allocate the new child,
872 * it will create a new talloc chunk as if the parent was a normal talloc
873 * context.
875 * If you talloc_free() children of a talloc pool, the memory is not given
876 * back to the system. Instead, free(3) is only called if the talloc_pool()
877 * itself is released with talloc_free().
879 * The downside of a talloc pool is that if you talloc_move() a child of a
880 * talloc pool to a talloc parent outside the pool, the whole pool memory is
881 * not free(3)'ed until that moved chunk is also talloc_free()ed.
883 * @param[in] context The talloc context to hang the result off.
885 * @param[in] size Size of the talloc pool.
887 * @return The allocated talloc pool, NULL on error.
889 _PUBLIC_ void *talloc_pool(const void *context, size_t size);
891 #ifdef DOXYGEN
893 * @brief Allocate a talloc object as/with an additional pool.
895 * This is like talloc_pool(), but's it's more flexible
896 * and allows an object to be a pool for its children.
898 * @param[in] ctx The talloc context to hang the result off.
900 * @param[in] type The type that we want to allocate.
902 * @param[in] num_subobjects The expected number of subobjects, which will
903 * be allocated within the pool. This allocates
904 * space for talloc_chunk headers.
906 * @param[in] total_subobjects_size The size that all subobjects can use in total.
909 * @return The allocated talloc object, NULL on error.
911 _PUBLIC_ void *talloc_pooled_object(const void *ctx, #type,
912 unsigned num_subobjects,
913 size_t total_subobjects_size);
914 #else
915 #define talloc_pooled_object(_ctx, _type, \
916 _num_subobjects, \
917 _total_subobjects_size) \
918 (_type *)_talloc_pooled_object((_ctx), sizeof(_type), #_type, \
919 (_num_subobjects), \
920 (_total_subobjects_size))
921 _PUBLIC_ void *_talloc_pooled_object(const void *ctx,
922 size_t type_size,
923 const char *type_name,
924 unsigned num_subobjects,
925 size_t total_subobjects_size);
926 #endif
929 * @brief Free a talloc chunk and NULL out the pointer.
931 * TALLOC_FREE() frees a pointer and sets it to NULL. Use this if you want
932 * immediate feedback (i.e. crash) if you use a pointer after having free'ed
933 * it.
935 * @param[in] ctx The chunk to be freed.
937 #define TALLOC_FREE(ctx) do { if (ctx != NULL) { talloc_free(ctx); ctx=NULL; } } while(0)
939 /* @} ******************************************************************/
942 * \defgroup talloc_ref The talloc reference function.
943 * @ingroup talloc
945 * This module contains the definitions around talloc references
947 * @{
951 * @brief Increase the reference count of a talloc chunk.
953 * The talloc_increase_ref_count(ptr) function is exactly equivalent to:
955 * @code
956 * talloc_reference(NULL, ptr);
957 * @endcode
959 * You can use either syntax, depending on which you think is clearer in
960 * your code.
962 * @param[in] ptr The pointer to increase the reference count.
964 * @return 0 on success, -1 on error.
966 _PUBLIC_ int talloc_increase_ref_count(const void *ptr);
969 * @brief Get the number of references to a talloc chunk.
971 * @param[in] ptr The pointer to retrieve the reference count from.
973 * @return The number of references.
975 _PUBLIC_ size_t talloc_reference_count(const void *ptr);
977 #ifdef DOXYGEN
979 * @brief Create an additional talloc parent to a pointer.
981 * The talloc_reference() function makes "context" an additional parent of
982 * ptr. Each additional reference consumes around 48 bytes of memory on intel
983 * x86 platforms.
985 * If ptr is NULL, then the function is a no-op, and simply returns NULL.
987 * After creating a reference you can free it in one of the following ways:
989 * - you can talloc_free() any parent of the original pointer. That
990 * will reduce the number of parents of this pointer by 1, and will
991 * cause this pointer to be freed if it runs out of parents.
993 * - you can talloc_free() the pointer itself if it has at maximum one
994 * parent. This behaviour has been changed since the release of version
995 * 2.0. Further information in the description of "talloc_free".
997 * For more control on which parent to remove, see talloc_unlink()
998 * @param[in] ctx The additional parent.
1000 * @param[in] ptr The pointer you want to create an additional parent for.
1002 * @return The original pointer 'ptr', NULL if talloc ran out of
1003 * memory in creating the reference.
1005 * @warning You should try to avoid using this interface. It turns a beautiful
1006 * talloc-tree into a graph. It is often really hard to debug if you
1007 * screw something up by accident.
1009 * Example:
1010 * @code
1011 * unsigned int *a, *b, *c;
1012 * a = talloc(NULL, unsigned int);
1013 * b = talloc(NULL, unsigned int);
1014 * c = talloc(a, unsigned int);
1015 * // b also serves as a parent of c.
1016 * talloc_reference(b, c);
1017 * @endcode
1019 * @see talloc_unlink()
1021 _PUBLIC_ void *talloc_reference(const void *ctx, const void *ptr);
1022 #else
1023 #define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference_loc((ctx),(ptr), __location__)
1024 _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location);
1025 #endif
1028 * @brief Remove a specific parent from a talloc chunk.
1030 * The function removes a specific parent from ptr. The context passed must
1031 * either be a context used in talloc_reference() with this pointer, or must be
1032 * a direct parent of ptr.
1034 * You can just use talloc_free() instead of talloc_unlink() if there
1035 * is at maximum one parent. This behaviour has been changed since the
1036 * release of version 2.0. Further information in the description of
1037 * "talloc_free".
1039 * @param[in] context The talloc parent to remove.
1041 * @param[in] ptr The talloc ptr you want to remove the parent from.
1043 * @return 0 on success, -1 on error.
1045 * @note If the parent has already been removed using talloc_free() then
1046 * this function will fail and will return -1. Likewise, if ptr is NULL,
1047 * then the function will make no modifications and return -1.
1049 * @warning You should try to avoid using this interface. It turns a beautiful
1050 * talloc-tree into a graph. It is often really hard to debug if you
1051 * screw something up by accident.
1053 * Example:
1054 * @code
1055 * unsigned int *a, *b, *c;
1056 * a = talloc(NULL, unsigned int);
1057 * b = talloc(NULL, unsigned int);
1058 * c = talloc(a, unsigned int);
1059 * // b also serves as a parent of c.
1060 * talloc_reference(b, c);
1061 * talloc_unlink(b, c);
1062 * @endcode
1064 _PUBLIC_ int talloc_unlink(const void *context, void *ptr);
1067 * @brief Provide a talloc context that is freed at program exit.
1069 * This is a handy utility function that returns a talloc context
1070 * which will be automatically freed on program exit. This can be used
1071 * to reduce the noise in memory leak reports.
1073 * Never use this in code that might be used in objects loaded with
1074 * dlopen and unloaded with dlclose. talloc_autofree_context()
1075 * internally uses atexit(3). Some platforms like modern Linux handles
1076 * this fine, but for example FreeBSD does not deal well with dlopen()
1077 * and atexit() used simultaneously: dlclose() does not clean up the
1078 * list of atexit-handlers, so when the program exits the code that
1079 * was registered from within talloc_autofree_context() is gone, the
1080 * program crashes at exit.
1082 * @return A talloc context, NULL on error.
1084 _PUBLIC_ void *talloc_autofree_context(void) _DEPRECATED_;
1087 * @brief Show the parentage of a context.
1089 * @param[in] context The talloc context to look at.
1091 * @param[in] file The output to use, a file, stdout or stderr.
1093 _PUBLIC_ void talloc_show_parents(const void *context, FILE *file);
1096 * @brief Check if a context is parent of a talloc chunk.
1098 * This checks if context is referenced in the talloc hierarchy above ptr.
1100 * @param[in] context The assumed talloc context.
1102 * @param[in] ptr The talloc chunk to check.
1104 * @return Return 1 if this is the case, 0 if not.
1106 _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr);
1109 * @brief Change the parent context of a talloc pointer.
1111 * The function changes the parent context of a talloc pointer. It is typically
1112 * used when the context that the pointer is currently a child of is going to be
1113 * freed and you wish to keep the memory for a longer time.
1115 * The difference between talloc_reparent() and talloc_steal() is that
1116 * talloc_reparent() can specify which parent you wish to change. This is
1117 * useful when a pointer has multiple parents via references.
1119 * @param[in] old_parent
1120 * @param[in] new_parent
1121 * @param[in] ptr
1123 * @return Return the pointer you passed. It does not have any
1124 * failure modes.
1126 _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr);
1128 /* @} ******************************************************************/
1131 * @defgroup talloc_array The talloc array functions
1132 * @ingroup talloc
1134 * Talloc contains some handy helpers for handling Arrays conveniently
1136 * @{
1139 #ifdef DOXYGEN
1141 * @brief Allocate an array.
1143 * The macro is equivalent to:
1145 * @code
1146 * (type *)talloc_size(ctx, sizeof(type) * count);
1147 * @endcode
1149 * except that it provides integer overflow protection for the multiply,
1150 * returning NULL if the multiply overflows.
1152 * @param[in] ctx The talloc context to hang the result off.
1154 * @param[in] type The type that we want to allocate.
1156 * @param[in] count The number of 'type' elements you want to allocate.
1158 * @return The allocated result, properly cast to 'type *', NULL on
1159 * error.
1161 * Example:
1162 * @code
1163 * unsigned int *a, *b;
1164 * a = talloc_zero(NULL, unsigned int);
1165 * b = talloc_array(a, unsigned int, 100);
1166 * @endcode
1168 * @see talloc()
1169 * @see talloc_zero_array()
1171 _PUBLIC_ void *talloc_array(const void *ctx, #type, unsigned count);
1172 #else
1173 #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
1174 _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
1175 #endif
1177 #ifdef DOXYGEN
1179 * @brief Allocate an array.
1181 * @param[in] ctx The talloc context to hang the result off.
1183 * @param[in] size The size of an array element.
1185 * @param[in] count The number of elements you want to allocate.
1187 * @return The allocated result, NULL on error.
1189 _PUBLIC_ void *talloc_array_size(const void *ctx, size_t size, unsigned count);
1190 #else
1191 #define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
1192 #endif
1194 #ifdef DOXYGEN
1196 * @brief Allocate an array into a typed pointer.
1198 * The macro should be used when you have a pointer to an array and want to
1199 * allocate memory of an array to point at with this pointer. When compiling
1200 * with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
1201 * and talloc_get_name() will return the current location in the source file
1202 * and not the type.
1204 * @param[in] ctx The talloc context to hang the result off.
1206 * @param[in] ptr The pointer you want to assign the result to.
1208 * @param[in] count The number of elements you want to allocate.
1210 * @return The allocated memory chunk, properly casted. NULL on
1211 * error.
1213 void *talloc_array_ptrtype(const void *ctx, const void *ptr, unsigned count);
1214 #else
1215 #define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
1216 #endif
1218 #ifdef DOXYGEN
1220 * @brief Get the number of elements in a talloc'ed array.
1222 * A talloc chunk carries its own size, so for talloc'ed arrays it is not
1223 * necessary to store the number of elements explicitly.
1225 * @param[in] ctx The allocated array.
1227 * @return The number of elements in ctx.
1229 size_t talloc_array_length(const void *ctx);
1230 #else
1231 #define talloc_array_length(ctx) (talloc_get_size(ctx)/sizeof(*ctx))
1232 #endif
1234 #ifdef DOXYGEN
1236 * @brief Allocate a zero-initialized array
1238 * @param[in] ctx The talloc context to hang the result off.
1240 * @param[in] type The type that we want to allocate.
1242 * @param[in] count The number of "type" elements you want to allocate.
1244 * @return The allocated result casted to "type *", NULL on error.
1246 * The talloc_zero_array() macro is equivalent to:
1248 * @code
1249 * ptr = talloc_array(ctx, type, count);
1250 * if (ptr) memset(ptr, 0, sizeof(type) * count);
1251 * @endcode
1253 void *talloc_zero_array(const void *ctx, #type, unsigned count);
1254 #else
1255 #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
1256 _PUBLIC_ void *_talloc_zero_array(const void *ctx,
1257 size_t el_size,
1258 unsigned count,
1259 const char *name);
1260 #endif
1262 #ifdef DOXYGEN
1264 * @brief Change the size of a talloc array.
1266 * The macro changes the size of a talloc pointer. The 'count' argument is the
1267 * number of elements of type 'type' that you want the resulting pointer to
1268 * hold.
1270 * talloc_realloc() has the following equivalences:
1272 * @code
1273 * talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
1274 * talloc_realloc(ctx, NULL, type, N) ==> talloc_array(ctx, type, N);
1275 * talloc_realloc(ctx, ptr, type, 0) ==> talloc_free(ptr);
1276 * @endcode
1278 * The "context" argument is only used if "ptr" is NULL, otherwise it is
1279 * ignored.
1281 * @param[in] ctx The parent context used if ptr is NULL.
1283 * @param[in] ptr The chunk to be resized.
1285 * @param[in] type The type of the array element inside ptr.
1287 * @param[in] count The intended number of array elements.
1289 * @return The new array, NULL on error. The call will fail either
1290 * due to a lack of memory, or because the pointer has more
1291 * than one parent (see talloc_reference()).
1293 _PUBLIC_ void *talloc_realloc(const void *ctx, void *ptr, #type, size_t count);
1294 #else
1295 #define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
1296 _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
1297 #endif
1299 #ifdef DOXYGEN
1301 * @brief Untyped realloc to change the size of a talloc array.
1303 * The macro is useful when the type is not known so the typesafe
1304 * talloc_realloc() cannot be used.
1306 * @param[in] ctx The parent context used if 'ptr' is NULL.
1308 * @param[in] ptr The chunk to be resized.
1310 * @param[in] size The new chunk size.
1312 * @return The new array, NULL on error.
1314 void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);
1315 #else
1316 #define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
1317 _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
1318 #endif
1321 * @brief Provide a function version of talloc_realloc_size.
1323 * This is a non-macro version of talloc_realloc(), which is useful as
1324 * libraries sometimes want a ralloc function pointer. A realloc()
1325 * implementation encapsulates the functionality of malloc(), free() and
1326 * realloc() in one call, which is why it is useful to be able to pass around
1327 * a single function pointer.
1329 * @param[in] context The parent context used if ptr is NULL.
1331 * @param[in] ptr The chunk to be resized.
1333 * @param[in] size The new chunk size.
1335 * @return The new chunk, NULL on error.
1337 _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
1339 /* @} ******************************************************************/
1342 * @defgroup talloc_string The talloc string functions.
1343 * @ingroup talloc
1345 * talloc string allocation and manipulation functions.
1346 * @{
1350 * @brief Duplicate a string into a talloc chunk.
1352 * This function is equivalent to:
1354 * @code
1355 * ptr = talloc_size(ctx, strlen(p)+1);
1356 * if (ptr) memcpy(ptr, p, strlen(p)+1);
1357 * @endcode
1359 * This functions sets the name of the new pointer to the passed
1360 * string. This is equivalent to:
1362 * @code
1363 * talloc_set_name_const(ptr, ptr)
1364 * @endcode
1366 * @param[in] t The talloc context to hang the result off.
1368 * @param[in] p The string you want to duplicate.
1370 * @return The duplicated string, NULL on error.
1372 _PUBLIC_ char *talloc_strdup(const void *t, const char *p);
1375 * @brief Append a string to given string.
1377 * The destination string is reallocated to take
1378 * <code>strlen(s) + strlen(a) + 1</code> characters.
1380 * This functions sets the name of the new pointer to the new
1381 * string. This is equivalent to:
1383 * @code
1384 * talloc_set_name_const(ptr, ptr)
1385 * @endcode
1387 * If <code>s == NULL</code> then new context is created.
1389 * @param[in] s The destination to append to.
1391 * @param[in] a The string you want to append.
1393 * @return The concatenated strings, NULL on error.
1395 * @see talloc_strdup()
1396 * @see talloc_strdup_append_buffer()
1398 _PUBLIC_ char *talloc_strdup_append(char *s, const char *a);
1401 * @brief Append a string to a given buffer.
1403 * This is a more efficient version of talloc_strdup_append(). It determines the
1404 * length of the destination string by the size of the talloc context.
1406 * Use this very carefully as it produces a different result than
1407 * talloc_strdup_append() when a zero character is in the middle of the
1408 * destination string.
1410 * @code
1411 * char *str_a = talloc_strdup(NULL, "hello world");
1412 * char *str_b = talloc_strdup(NULL, "hello world");
1413 * str_a[5] = str_b[5] = '\0'
1415 * char *app = talloc_strdup_append(str_a, ", hello");
1416 * char *buf = talloc_strdup_append_buffer(str_b, ", hello");
1418 * printf("%s\n", app); // hello, hello (app = "hello, hello")
1419 * printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1420 * @endcode
1422 * If <code>s == NULL</code> then new context is created.
1424 * @param[in] s The destination buffer to append to.
1426 * @param[in] a The string you want to append.
1428 * @return The concatenated strings, NULL on error.
1430 * @see talloc_strdup()
1431 * @see talloc_strdup_append()
1432 * @see talloc_array_length()
1434 _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a);
1437 * @brief Duplicate a length-limited string into a talloc chunk.
1439 * This function is the talloc equivalent of the C library function strndup(3).
1441 * This functions sets the name of the new pointer to the passed string. This is
1442 * equivalent to:
1444 * @code
1445 * talloc_set_name_const(ptr, ptr)
1446 * @endcode
1448 * @param[in] t The talloc context to hang the result off.
1450 * @param[in] p The string you want to duplicate.
1452 * @param[in] n The maximum string length to duplicate.
1454 * @return The duplicated string, NULL on error.
1456 _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n);
1459 * @brief Append at most n characters of a string to given string.
1461 * The destination string is reallocated to take
1462 * <code>strlen(s) + strnlen(a, n) + 1</code> characters.
1464 * This functions sets the name of the new pointer to the new
1465 * string. This is equivalent to:
1467 * @code
1468 * talloc_set_name_const(ptr, ptr)
1469 * @endcode
1471 * If <code>s == NULL</code> then new context is created.
1473 * @param[in] s The destination string to append to.
1475 * @param[in] a The source string you want to append.
1477 * @param[in] n The number of characters you want to append from the
1478 * string.
1480 * @return The concatenated strings, NULL on error.
1482 * @see talloc_strndup()
1483 * @see talloc_strndup_append_buffer()
1485 _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n);
1488 * @brief Append at most n characters of a string to given buffer
1490 * This is a more efficient version of talloc_strndup_append(). It determines
1491 * the length of the destination string by the size of the talloc context.
1493 * Use this very carefully as it produces a different result than
1494 * talloc_strndup_append() when a zero character is in the middle of the
1495 * destination string.
1497 * @code
1498 * char *str_a = talloc_strdup(NULL, "hello world");
1499 * char *str_b = talloc_strdup(NULL, "hello world");
1500 * str_a[5] = str_b[5] = '\0'
1502 * char *app = talloc_strndup_append(str_a, ", hello", 7);
1503 * char *buf = talloc_strndup_append_buffer(str_b, ", hello", 7);
1505 * printf("%s\n", app); // hello, hello (app = "hello, hello")
1506 * printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1507 * @endcode
1509 * If <code>s == NULL</code> then new context is created.
1511 * @param[in] s The destination buffer to append to.
1513 * @param[in] a The source string you want to append.
1515 * @param[in] n The number of characters you want to append from the
1516 * string.
1518 * @return The concatenated strings, NULL on error.
1520 * @see talloc_strndup()
1521 * @see talloc_strndup_append()
1522 * @see talloc_array_length()
1524 _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n);
1527 * @brief Format a string given a va_list.
1529 * This function is the talloc equivalent of the C library function
1530 * vasprintf(3).
1532 * This functions sets the name of the new pointer to the new string. This is
1533 * equivalent to:
1535 * @code
1536 * talloc_set_name_const(ptr, ptr)
1537 * @endcode
1539 * @param[in] t The talloc context to hang the result off.
1541 * @param[in] fmt The format string.
1543 * @param[in] ap The parameters used to fill fmt.
1545 * @return The formatted string, NULL on error.
1547 _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1550 * @brief Format a string given a va_list and append it to the given destination
1551 * string.
1553 * @param[in] s The destination string to append to.
1555 * @param[in] fmt The format string.
1557 * @param[in] ap The parameters used to fill fmt.
1559 * @return The formatted string, NULL on error.
1561 * @see talloc_vasprintf()
1563 _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1566 * @brief Format a string given a va_list and append it to the given destination
1567 * buffer.
1569 * @param[in] s The destination buffer to append to.
1571 * @param[in] fmt The format string.
1573 * @param[in] ap The parameters used to fill fmt.
1575 * @return The formatted string, NULL on error.
1577 * @see talloc_vasprintf()
1579 _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1582 * @brief Build up a string buffer, handle allocation failure
1584 * @param[in] ps Pointer to the talloc'ed string to be extended
1585 * @param[in] fmt The format string
1586 * @param[in] ... The parameters used to fill fmt.
1588 * This does nothing if *ps is NULL and sets *ps to NULL if the
1589 * intermediate reallocation fails. Useful when building up a string
1590 * step by step, no intermediate NULL checks are required.
1592 _PUBLIC_ void talloc_asprintf_addbuf(char **ps, const char *fmt, ...) \
1593 PRINTF_ATTRIBUTE(2,3);
1596 * @brief Format a string.
1598 * This function is the talloc equivalent of the C library function asprintf(3).
1600 * This functions sets the name of the new pointer to the new string. This is
1601 * equivalent to:
1603 * @code
1604 * talloc_set_name_const(ptr, ptr)
1605 * @endcode
1607 * @param[in] t The talloc context to hang the result off.
1609 * @param[in] fmt The format string.
1611 * @param[in] ... The parameters used to fill fmt.
1613 * @return The formatted string, NULL on error.
1615 _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1618 * @brief Append a formatted string to another string.
1620 * This function appends the given formatted string to the given string. Use
1621 * this variant when the string in the current talloc buffer may have been
1622 * truncated in length.
1624 * This functions sets the name of the new pointer to the new
1625 * string. This is equivalent to:
1627 * @code
1628 * talloc_set_name_const(ptr, ptr)
1629 * @endcode
1631 * If <code>s == NULL</code> then new context is created.
1633 * @param[in] s The string to append to.
1635 * @param[in] fmt The format string.
1637 * @param[in] ... The parameters used to fill fmt.
1639 * @return The formatted string, NULL on error.
1641 _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1644 * @brief Append a formatted string to another string.
1646 * This is a more efficient version of talloc_asprintf_append(). It determines
1647 * the length of the destination string by the size of the talloc context.
1649 * Use this very carefully as it produces a different result than
1650 * talloc_asprintf_append() when a zero character is in the middle of the
1651 * destination string.
1653 * @code
1654 * char *str_a = talloc_strdup(NULL, "hello world");
1655 * char *str_b = talloc_strdup(NULL, "hello world");
1656 * str_a[5] = str_b[5] = '\0'
1658 * char *app = talloc_asprintf_append(str_a, "%s", ", hello");
1659 * char *buf = talloc_strdup_append_buffer(str_b, "%s", ", hello");
1661 * printf("%s\n", app); // hello, hello (app = "hello, hello")
1662 * printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1663 * @endcode
1665 * If <code>s == NULL</code> then new context is created.
1667 * @param[in] s The string to append to
1669 * @param[in] fmt The format string.
1671 * @param[in] ... The parameters used to fill fmt.
1673 * @return The formatted string, NULL on error.
1675 * @see talloc_asprintf()
1676 * @see talloc_asprintf_append()
1678 _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1680 /* @} ******************************************************************/
1683 * @defgroup talloc_debug The talloc debugging support functions
1684 * @ingroup talloc
1686 * To aid memory debugging, talloc contains routines to inspect the currently
1687 * allocated memory hierarchy.
1689 * @{
1693 * @brief Walk a complete talloc hierarchy.
1695 * This provides a more flexible reports than talloc_report(). It
1696 * will recursively call the callback for the entire tree of memory
1697 * referenced by the pointer. References in the tree are passed with
1698 * is_ref = 1 and the pointer that is referenced.
1700 * You can pass NULL for the pointer, in which case a report is
1701 * printed for the top level memory context, but only if
1702 * talloc_enable_leak_report() or talloc_enable_leak_report_full()
1703 * has been called.
1705 * The recursion is stopped when depth >= max_depth.
1706 * max_depth = -1 means only stop at leaf nodes.
1708 * @param[in] ptr The talloc chunk.
1710 * @param[in] depth Internal parameter to control recursion. Call with 0.
1712 * @param[in] max_depth Maximum recursion level.
1714 * @param[in] callback Function to be called on every chunk.
1716 * @param[in] private_data Private pointer passed to callback.
1718 _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
1719 void (*callback)(const void *ptr,
1720 int depth, int max_depth,
1721 int is_ref,
1722 void *private_data),
1723 void *private_data);
1726 * @brief Print a talloc hierarchy.
1728 * This provides a more flexible reports than talloc_report(). It
1729 * will let you specify the depth and max_depth.
1731 * @param[in] ptr The talloc chunk.
1733 * @param[in] depth Internal parameter to control recursion. Call with 0.
1735 * @param[in] max_depth Maximum recursion level.
1737 * @param[in] f The file handle to print to.
1739 _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
1742 * @brief Print a summary report of all memory used by ptr.
1744 * This provides a more detailed report than talloc_report(). It will
1745 * recursively print the entire tree of memory referenced by the
1746 * pointer. References in the tree are shown by giving the name of the
1747 * pointer that is referenced.
1749 * You can pass NULL for the pointer, in which case a report is printed
1750 * for the top level memory context, but only if
1751 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
1752 * been called.
1754 * @param[in] ptr The talloc chunk.
1756 * @param[in] f The file handle to print to.
1758 * Example:
1759 * @code
1760 * unsigned int *a, *b;
1761 * a = talloc(NULL, unsigned int);
1762 * b = talloc(a, unsigned int);
1763 * fprintf(stderr, "Dumping memory tree for a:\n");
1764 * talloc_report_full(a, stderr);
1765 * @endcode
1767 * @see talloc_report()
1769 _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f);
1772 * @brief Print a summary report of all memory used by ptr.
1774 * This function prints a summary report of all memory used by ptr. One line of
1775 * report is printed for each immediate child of ptr, showing the total memory
1776 * and number of blocks used by that child.
1778 * You can pass NULL for the pointer, in which case a report is printed
1779 * for the top level memory context, but only if talloc_enable_leak_report()
1780 * or talloc_enable_leak_report_full() has been called.
1782 * @param[in] ptr The talloc chunk.
1784 * @param[in] f The file handle to print to.
1786 * Example:
1787 * @code
1788 * unsigned int *a, *b;
1789 * a = talloc(NULL, unsigned int);
1790 * b = talloc(a, unsigned int);
1791 * fprintf(stderr, "Summary of memory tree for a:\n");
1792 * talloc_report(a, stderr);
1793 * @endcode
1795 * @see talloc_report_full()
1797 _PUBLIC_ void talloc_report(const void *ptr, FILE *f);
1800 * @brief Enable tracking the use of NULL memory contexts.
1802 * This enables tracking of the NULL memory context without enabling leak
1803 * reporting on exit. Useful for when you want to do your own leak
1804 * reporting call via talloc_report_null_full();
1806 _PUBLIC_ void talloc_enable_null_tracking(void);
1809 * @brief Enable tracking the use of NULL memory contexts.
1811 * This enables tracking of the NULL memory context without enabling leak
1812 * reporting on exit. Useful for when you want to do your own leak
1813 * reporting call via talloc_report_null_full();
1815 _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void);
1818 * @brief Disable tracking of the NULL memory context.
1820 * This disables tracking of the NULL memory context.
1822 _PUBLIC_ void talloc_disable_null_tracking(void);
1825 * @brief Enable leak report when a program exits.
1827 * This enables calling of talloc_report(NULL, stderr) when the program
1828 * exits. In Samba4 this is enabled by using the --leak-report command
1829 * line option.
1831 * For it to be useful, this function must be called before any other
1832 * talloc function as it establishes a "null context" that acts as the
1833 * top of the tree. If you don't call this function first then passing
1834 * NULL to talloc_report() or talloc_report_full() won't give you the
1835 * full tree printout.
1837 * Here is a typical talloc report:
1839 * @code
1840 * talloc report on 'null_context' (total 267 bytes in 15 blocks)
1841 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
1842 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
1843 * iconv(UTF8,CP850) contains 42 bytes in 2 blocks
1844 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
1845 * iconv(CP850,UTF8) contains 42 bytes in 2 blocks
1846 * iconv(UTF8,UTF-16LE) contains 45 bytes in 2 blocks
1847 * iconv(UTF-16LE,UTF8) contains 45 bytes in 2 blocks
1848 * @endcode
1850 _PUBLIC_ void talloc_enable_leak_report(void);
1853 * @brief Enable full leak report when a program exits.
1855 * This enables calling of talloc_report_full(NULL, stderr) when the
1856 * program exits. In Samba4 this is enabled by using the
1857 * --leak-report-full command line option.
1859 * For it to be useful, this function must be called before any other
1860 * talloc function as it establishes a "null context" that acts as the
1861 * top of the tree. If you don't call this function first then passing
1862 * NULL to talloc_report() or talloc_report_full() won't give you the
1863 * full tree printout.
1865 * Here is a typical full report:
1867 * @code
1868 * full talloc report on 'root' (total 18 bytes in 8 blocks)
1869 * p1 contains 18 bytes in 7 blocks (ref 0)
1870 * r1 contains 13 bytes in 2 blocks (ref 0)
1871 * reference to: p2
1872 * p2 contains 1 bytes in 1 blocks (ref 1)
1873 * x3 contains 1 bytes in 1 blocks (ref 0)
1874 * x2 contains 1 bytes in 1 blocks (ref 0)
1875 * x1 contains 1 bytes in 1 blocks (ref 0)
1876 * @endcode
1878 _PUBLIC_ void talloc_enable_leak_report_full(void);
1881 * @brief Set a custom "abort" function that is called on serious error.
1883 * The default "abort" function is <code>abort()</code>.
1885 * The "abort" function is called when:
1887 * <ul>
1888 * <li>talloc_get_type_abort() fails</li>
1889 * <li>the provided pointer is not a valid talloc context</li>
1890 * <li>when the context meta data are invalid</li>
1891 * <li>when access after free is detected</li>
1892 * </ul>
1894 * Example:
1896 * @code
1897 * void my_abort(const char *reason)
1899 * fprintf(stderr, "talloc abort: %s\n", reason);
1900 * abort();
1903 * talloc_set_abort_fn(my_abort);
1904 * @endcode
1906 * @param[in] abort_fn The new "abort" function.
1908 * @see talloc_set_log_fn()
1909 * @see talloc_get_type()
1911 _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason));
1914 * @brief Set a logging function.
1916 * @param[in] log_fn The logging function.
1918 * @see talloc_set_log_stderr()
1919 * @see talloc_set_abort_fn()
1921 _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message));
1924 * @brief Set stderr as the output for logs.
1926 * @see talloc_set_log_fn()
1927 * @see talloc_set_abort_fn()
1929 _PUBLIC_ void talloc_set_log_stderr(void);
1932 * @brief Set a max memory limit for the current context hierarchy
1933 * This affects all children of this context and constrain any
1934 * allocation in the hierarchy to never exceed the limit set.
1935 * The limit can be removed by setting 0 (unlimited) as the
1936 * max_size by calling the function again on the same context.
1937 * Memory limits can also be nested, meaning a child can have
1938 * a stricter memory limit than a parent.
1939 * Memory limits are enforced only at memory allocation time.
1940 * Stealing a context into a 'limited' hierarchy properly
1941 * updates memory usage but does *not* cause failure if the
1942 * move causes the new parent to exceed its limits. However
1943 * any further allocation on that hierarchy will then fail.
1945 * @warning talloc memlimit functionality is deprecated. Please
1946 * consider using cgroup memory limits instead.
1948 * @param[in] ctx The talloc context to set the limit on
1949 * @param[in] max_size The (new) max_size
1951 _PUBLIC_ int talloc_set_memlimit(const void *ctx, size_t max_size) _DEPRECATED_;
1953 /* @} ******************************************************************/
1955 #if TALLOC_DEPRECATED
1956 #define talloc_zero_p(ctx, type) talloc_zero(ctx, type)
1957 #define talloc_p(ctx, type) talloc(ctx, type)
1958 #define talloc_array_p(ctx, type, count) talloc_array(ctx, type, count)
1959 #define talloc_realloc_p(ctx, p, type, count) talloc_realloc(ctx, p, type, count)
1960 #define talloc_destroy(ctx) talloc_free(ctx)
1961 #define talloc_append_string(c, s, a) (s?talloc_strdup_append(s,a):talloc_strdup(c, a))
1962 #endif
1964 #ifndef TALLOC_MAX_DEPTH
1965 #define TALLOC_MAX_DEPTH 10000
1966 #endif
1968 #ifdef __cplusplus
1969 } /* end of extern "C" */
1970 #endif
1972 #endif