blob: 7372df16fe8eb63cdfca5201fa95cf9f6427c0e8 [file] [log] [blame]
Harald Welte5df0be62019-04-17 20:54:29 +02001#ifndef _TALLOC_H_
2#define _TALLOC_H_
3/*
4 Unix SMB/CIFS implementation.
5 Samba temporary memory allocation functions
6
7 Copyright (C) Andrew Tridgell 2004-2005
8 Copyright (C) Stefan Metzmacher 2006
9
10 ** NOTE! The following LGPL license applies to the talloc
11 ** library. This does NOT imply that all of Samba is released
12 ** under the LGPL
13
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 3 of the License, or (at your option) any later version.
18
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
23
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, see <http://www.gnu.org/licenses/>.
26*/
27
28#include <stdlib.h>
29#include <stdio.h>
30#include <stdarg.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36/**
37 * @defgroup talloc The talloc API
38 *
39 * talloc is a hierarchical, reference counted memory pool system with
40 * destructors. It is the core memory allocator used in Samba.
41 *
42 * @{
43 */
44
45#define TALLOC_VERSION_MAJOR 2
46#define TALLOC_VERSION_MINOR 1
47
48int talloc_version_major(void);
49int talloc_version_minor(void);
50/* This is mostly useful only for testing */
51int talloc_test_get_magic(void);
52
53/**
54 * @brief Define a talloc parent type
55 *
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
61 *
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
72 *
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.
75 */
76typedef void TALLOC_CTX;
77
78/*
79 this uses a little trick to allow __LINE__ to be stringified
80*/
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
87
88#ifndef TALLOC_DEPRECATED
89#define TALLOC_DEPRECATED 0
90#endif
91
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
103
104#ifdef DOXYGEN
105/**
106 * @brief Create a new talloc context.
107 *
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.
111 *
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.
114 *
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.
118 *
119 * @param[in] ctx A talloc context to create a new reference on or NULL to
120 * create a new top level context.
121 *
122 * @param[in] type The type of memory to allocate.
123 *
124 * @return A type casted talloc context or NULL on error.
125 *
126 * @code
127 * unsigned int *a, *b;
128 *
129 * a = talloc(NULL, unsigned int);
130 * b = talloc(a, unsigned int);
131 * @endcode
132 *
133 * @see talloc_zero
134 * @see talloc_array
135 * @see talloc_steal
136 * @see talloc_free
137 */
138void *talloc(const void *ctx, #type);
139#else
140#define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
141void *_talloc(const void *context, size_t size);
142#endif
143
144/**
145 * @brief Create a new top level talloc context.
146 *
147 * This function creates a zero length named talloc context as a top level
148 * context. It is equivalent to:
149 *
150 * @code
151 * talloc_named(NULL, 0, fmt, ...);
152 * @endcode
153 * @param[in] fmt Format string for the name.
154 *
155 * @param[in] ... Additional printf-style arguments.
156 *
157 * @return The allocated memory chunk, NULL on error.
158 *
159 * @see talloc_named()
160 */
161void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
162
163#ifdef DOXYGEN
164/**
165 * @brief Free a chunk of talloc memory.
166 *
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().
170 *
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.
177 *
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().
184 *
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:
188 *
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
194 *
195 * Please see the documentation for talloc_set_log_fn() and
196 * talloc_set_log_stderr() for more information on talloc logging
197 * functions.
198 *
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.
203 *
204 * talloc_free() operates recursively on its children.
205 *
206 * @param[in] ptr The chunk to be freed.
207 *
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.
213 *
214 * Example:
215 * @code
216 * unsigned int *a, *b;
217 * a = talloc(NULL, unsigned int);
218 * b = talloc(a, unsigned int);
219 *
220 * talloc_free(a); // Frees a and b
221 * @endcode
222 *
223 * @see talloc_set_destructor()
224 * @see talloc_unlink()
225 */
226int talloc_free(void *ptr);
227#else
228#define talloc_free(ctx) _talloc_free(ctx, __location__)
229int _talloc_free(void *ptr, const char *location);
230#endif
231
232/**
233 * @brief Free a talloc chunk's children.
234 *
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.
237 *
238 * A NULL argument is handled as no-op.
239 *
240 * @param[in] ptr The chunk that you want to free the children of
241 * (NULL is allowed too)
242 */
243void talloc_free_children(void *ptr);
244
245#ifdef DOXYGEN
246/**
247 * @brief Assign a destructor function to be called when a chunk is freed.
248 *
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.
253 *
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.
258 *
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.
262 *
263 * To remove a destructor call talloc_set_destructor() with NULL for the
264 * destructor.
265 *
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.
270 *
271 * @param[in] ptr The talloc chunk to add a destructor to.
272 *
273 * @param[in] destructor The destructor function to be called. NULL to remove
274 * it.
275 *
276 * Example:
277 * @code
278 * static int destroy_fd(int *fd) {
279 * close(*fd);
280 * return 0;
281 * }
282 *
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;
289 * }
290 * // Whenever they free this, we close the file.
291 * talloc_set_destructor(fd, destroy_fd);
292 * return fd;
293 * }
294 * @endcode
295 *
296 * @see talloc()
297 * @see talloc_free()
298 */
299void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
300
301/**
302 * @brief Change a talloc chunk's parent.
303 *
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.
308 *
309 * To make the changed hierarchy less error-prone, you might consider to use
310 * talloc_move().
311 *
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:
317 *
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
323 *
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.
327 *
328 * @param[in] new_ctx The new parent context.
329 *
330 * @param[in] ptr The talloc chunk to move.
331 *
332 * @return Returns the pointer that you pass it. It does not have
333 * any failure modes.
334 *
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.
338 */
339void *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 */
359void _talloc_set_destructor(const void *ptr, int (*_destructor)(void *));
360void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location);
361#endif /* DOXYGEN */
362
363/**
364 * @brief Assign a name to a talloc chunk.
365 *
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.
369 *
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().
373 *
374 * The talloc_set_name() function allocates memory as a child of the
375 * pointer. It is logically equivalent to:
376 *
377 * @code
378 * talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
379 * @endcode
380 *
381 * @param[in] ptr The talloc chunk to assign a name to.
382 *
383 * @param[in] fmt Format string for the name.
384 *
385 * @param[in] ... Add printf-style additional arguments.
386 *
387 * @return The assigned name, NULL on error.
388 *
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().
392 */
393const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
394
395#ifdef DOXYGEN
396/**
397 * @brief Change a talloc chunk's parent.
398 *
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:
401 *
402 * @code
403 * struct foo *X = talloc(tmp_ctx, struct foo);
404 * struct foo *Y;
405 * Y = talloc_move(new_ctx, &X);
406 * @endcode
407 *
408 * @param[in] new_ctx The new parent context.
409 *
410 * @param[in] pptr Pointer to a pointer to the talloc chunk to move.
411 *
412 * @return The pointer to the talloc chunk that moved.
413 * It does not have any failure modes.
414 *
415 */
416void *talloc_move(const void *new_ctx, void **pptr);
417#else
418#define talloc_move(ctx, pptr) (_TALLOC_TYPEOF(*(pptr)))_talloc_move((ctx),(void *)(pptr))
419void *_talloc_move(const void *new_ctx, const void *pptr);
420#endif
421
422/**
423 * @brief Assign a name to a talloc chunk.
424 *
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().
428 *
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().
433 *
434 * @param[in] ptr The talloc chunk to assign a name to.
435 *
436 * @param[in] name Format string for the name.
437 */
438void talloc_set_name_const(const void *ptr, const char *name);
439
440/**
441 * @brief Create a named talloc chunk.
442 *
443 * The talloc_named() function creates a named talloc pointer. It is
444 * equivalent to:
445 *
446 * @code
447 * ptr = talloc_size(context, size);
448 * talloc_set_name(ptr, fmt, ....);
449 * @endcode
450 *
451 * @param[in] context The talloc context to hang the result off.
452 *
453 * @param[in] size Number of char's that you want to allocate.
454 *
455 * @param[in] fmt Format string for the name.
456 *
457 * @param[in] ... Additional printf-style arguments.
458 *
459 * @return The allocated memory chunk, NULL on error.
460 *
461 * @see talloc_set_name()
462 */
463void *talloc_named(const void *context, size_t size,
464 const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
465
466/**
467 * @brief Basic routine to allocate a chunk of memory.
468 *
469 * This is equivalent to:
470 *
471 * @code
472 * ptr = talloc_size(context, size);
473 * talloc_set_name_const(ptr, name);
474 * @endcode
475 *
476 * @param[in] context The parent context.
477 *
478 * @param[in] size The number of char's that we want to allocate.
479 *
480 * @param[in] name The name the talloc block has.
481 *
482 * @return The allocated memory chunk, NULL on error.
483 */
484void *talloc_named_const(const void *context, size_t size, const char *name);
485
486#ifdef DOXYGEN
487/**
488 * @brief Untyped allocation.
489 *
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.
493 *
494 * Best to use talloc() or talloc_array() instead.
495 *
496 * @param[in] ctx The talloc context to hang the result off.
497 *
498 * @param[in] size Number of char's that you want to allocate.
499 *
500 * @return The allocated memory chunk, NULL on error.
501 *
502 * Example:
503 * @code
504 * void *mem = talloc_size(NULL, 100);
505 * @endcode
506 */
507void *talloc_size(const void *ctx, size_t size);
508#else
509#define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
510#endif
511
512#ifdef DOXYGEN
513/**
514 * @brief Allocate into a typed pointer.
515 *
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.
521 *
522 * @param[in] ctx The talloc context to hang the result off.
523 *
524 * @param[in] type The pointer you want to assign the result to.
525 *
526 * @return The properly casted allocated memory chunk, NULL on
527 * error.
528 *
529 * Example:
530 * @code
531 * unsigned int *a = talloc_ptrtype(NULL, a);
532 * @endcode
533 */
534void *talloc_ptrtype(const void *ctx, #type);
535#else
536#define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
537#endif
538
539#ifdef DOXYGEN
540/**
541 * @brief Allocate a new 0-sized talloc chunk.
542 *
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.
547 *
548 * @param[in] ctx The talloc parent context.
549 *
550 * @return A new talloc chunk, NULL on error.
551 */
552void *talloc_new(const void *ctx);
553#else
554#define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
555#endif
556
557#ifdef DOXYGEN
558/**
559 * @brief Allocate a 0-initizialized structure.
560 *
561 * The macro is equivalent to:
562 *
563 * @code
564 * ptr = talloc(ctx, type);
565 * if (ptr) memset(ptr, 0, sizeof(type));
566 * @endcode
567 *
568 * @param[in] ctx The talloc context to hang the result off.
569 *
570 * @param[in] type The type that we want to allocate.
571 *
572 * @return Pointer to a piece of memory, properly cast to 'type *',
573 * NULL on error.
574 *
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
581 *
582 * @see talloc()
583 * @see talloc_zero_size()
584 * @see talloc_zero_array()
585 */
586void *talloc_zero(const void *ctx, #type);
587
588/**
589 * @brief Allocate untyped, 0-initialized memory.
590 *
591 * @param[in] ctx The talloc context to hang the result off.
592 *
593 * @param[in] size Number of char's that you want to allocate.
594 *
595 * @return The allocated memory chunk.
596 */
597void *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__)
601void *_talloc_zero(const void *ctx, size_t size, const char *name);
602#endif
603
604/**
605 * @brief Return the name of a talloc chunk.
606 *
607 * @param[in] ptr The talloc chunk.
608 *
609 * @return The current name for the given talloc pointer.
610 *
611 * @see talloc_set_name()
612 */
613const char *talloc_get_name(const void *ptr);
614
615/**
616 * @brief Verify that a talloc chunk carries a specified name.
617 *
618 * This function checks if a pointer has the specified name. If it does
619 * then the pointer is returned.
620 *
621 * @param[in] ptr The talloc chunk to check.
622 *
623 * @param[in] name The name to check against.
624 *
625 * @return The pointer if the name matches, NULL if it doesn't.
626 */
627void *talloc_check_name(const void *ptr, const char *name);
628
629/**
630 * @brief Get the parent chunk of a pointer.
631 *
632 * @param[in] ptr The talloc pointer to inspect.
633 *
634 * @return The talloc parent of ptr, NULL on error.
635 */
636void *talloc_parent(const void *ptr);
637
638/**
639 * @brief Get a talloc chunk's parent name.
640 *
641 * @param[in] ptr The talloc pointer to inspect.
642 *
643 * @return The name of ptr's parent chunk.
644 */
645const char *talloc_parent_name(const void *ptr);
646
647/**
648 * @brief Get the total size of a talloc chunk including its children.
649 *
650 * The function returns the total size in bytes used by this pointer and all
651 * child pointers. Mostly useful for debugging.
652 *
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.
656 *
657 * @param[in] ptr The talloc chunk.
658 *
659 * @return The total size.
660 */
661size_t talloc_total_size(const void *ptr);
662
663/**
664 * @brief Get the number of talloc chunks hanging off a chunk.
665 *
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.
669 *
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.
673 *
674 * @param[in] ptr The talloc chunk.
675 *
676 * @return The total size.
677 */
678size_t talloc_total_blocks(const void *ptr);
679
680#ifdef DOXYGEN
681/**
682 * @brief Duplicate a memory area into a talloc chunk.
683 *
684 * The function is equivalent to:
685 *
686 * @code
687 * ptr = talloc_size(ctx, size);
688 * if (ptr) memcpy(ptr, p, size);
689 * @endcode
690 *
691 * @param[in] t The talloc context to hang the result off.
692 *
693 * @param[in] p The memory chunk you want to duplicate.
694 *
695 * @param[in] size Number of char's that you want copy.
696 *
697 * @return The allocated memory chunk.
698 *
699 * @see talloc_size()
700 */
701void *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__)
704void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
705#endif
706
707#ifdef DOXYGEN
708/**
709 * @brief Assign a type to a talloc chunk.
710 *
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.
714 *
715 * It is equivalent to this:
716 *
717 * @code
718 * talloc_set_name_const(ptr, #type)
719 * @endcode
720 *
721 * @param[in] ptr The talloc chunk to assign the type to.
722 *
723 * @param[in] type The type to assign.
724 */
725void talloc_set_type(const char *ptr, #type);
726
727/**
728 * @brief Get a typed pointer out of a talloc pointer.
729 *
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:
733 *
734 * @code
735 * (type *)talloc_check_name(ptr, #type)
736 * @endcode
737 *
738 * @param[in] ptr The talloc pointer to check.
739 *
740 * @param[in] type The type to check against.
741 *
742 * @return The properly casted pointer given by ptr, NULL on error.
743 */
744type *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
749
750#ifdef DOXYGEN
751/**
752 * @brief Safely turn a void pointer into a typed pointer.
753 *
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.
758 *
759 * @param[in] ptr The void pointer to convert.
760 *
761 * @param[in] type The type that this chunk contains
762 *
763 * @return The same value as ptr, type-checked and properly cast.
764 */
765void *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
772void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location);
773#endif
774
775/**
776 * @brief Find a parent context by name.
777 *
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.
782 *
783 * @param[in] ctx The talloc chunk to start from.
784 *
785 * @param[in] name The name of the parent we look for.
786 *
787 * @return The memory context we are looking for, NULL if not
788 * found.
789 */
790void *talloc_find_parent_byname(const void *ctx, const char *name);
791
792#ifdef DOXYGEN
793/**
794 * @brief Find a parent context by type.
795 *
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.
800 *
801 * Like talloc_find_parent_byname() but takes a type, making it typesafe.
802 *
803 * @param[in] ptr The talloc chunk to start from.
804 *
805 * @param[in] type The type of the parent to look for.
806 *
807 * @return The memory context we are looking for, NULL if not
808 * found.
809 */
810void *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
814
815/**
816 * @brief Allocate a talloc pool.
817 *
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.
824 *
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.
829 *
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.
836 *
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.
840 *
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().
844 *
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.
848 *
849 * @param[in] context The talloc context to hang the result off.
850 *
851 * @param[in] size Size of the talloc pool.
852 *
853 * @return The allocated talloc pool, NULL on error.
854 */
855void *talloc_pool(const void *context, size_t size);
856
857#ifdef DOXYGEN
858/**
859 * @brief Allocate a talloc object as/with an additional pool.
860 *
861 * This is like talloc_pool(), but's it's more flexible
862 * and allows an object to be a pool for its children.
863 *
864 * @param[in] ctx The talloc context to hang the result off.
865 *
866 * @param[in] type The type that we want to allocate.
867 *
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.
871 *
872 * @param[in] total_subobjects_size The size that all subobjects can use in total.
873 *
874 *
875 * @return The allocated talloc object, NULL on error.
876 */
877void *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))
887void *_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
893
894/**
895 * @brief Free a talloc chunk and NULL out the pointer.
896 *
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.
900 *
901 * @param[in] ctx The chunk to be freed.
902 */
903#define TALLOC_FREE(ctx) do { if (ctx != NULL) { talloc_free(ctx); ctx=NULL; } } while(0)
904
905/* @} ******************************************************************/
906
907/**
908 * \defgroup talloc_ref The talloc reference function.
909 * @ingroup talloc
910 *
911 * This module contains the definitions around talloc references
912 *
913 * @{
914 */
915
916/**
917 * @brief Increase the reference count of a talloc chunk.
918 *
919 * The talloc_increase_ref_count(ptr) function is exactly equivalent to:
920 *
921 * @code
922 * talloc_reference(NULL, ptr);
923 * @endcode
924 *
925 * You can use either syntax, depending on which you think is clearer in
926 * your code.
927 *
928 * @param[in] ptr The pointer to increase the reference count.
929 *
930 * @return 0 on success, -1 on error.
931 */
932int talloc_increase_ref_count(const void *ptr);
933
934/**
935 * @brief Get the number of references to a talloc chunk.
936 *
937 * @param[in] ptr The pointer to retrieve the reference count from.
938 *
939 * @return The number of references.
940 */
941size_t talloc_reference_count(const void *ptr);
942
943#ifdef DOXYGEN
944/**
945 * @brief Create an additional talloc parent to a pointer.
946 *
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.
950 *
951 * If ptr is NULL, then the function is a no-op, and simply returns NULL.
952 *
953 * After creating a reference you can free it in one of the following ways:
954 *
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.
958 *
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".
962 *
963 * For more control on which parent to remove, see talloc_unlink()
964 * @param[in] ctx The additional parent.
965 *
966 * @param[in] ptr The pointer you want to create an additional parent for.
967 *
968 * @return The original pointer 'ptr', NULL if talloc ran out of
969 * memory in creating the reference.
970 *
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.
974 *
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
984 *
985 * @see talloc_unlink()
986 */
987void *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__)
990void *_talloc_reference_loc(const void *context, const void *ptr, const char *location);
991#endif
992
993/**
994 * @brief Remove a specific parent from a talloc chunk.
995 *
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.
999 *
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".
1004 *
1005 * @param[in] context The talloc parent to remove.
1006 *
1007 * @param[in] ptr The talloc ptr you want to remove the parent from.
1008 *
1009 * @return 0 on success, -1 on error.
1010 *
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.
1014 *
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.
1018 *
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
1029 */
1030int talloc_unlink(const void *context, void *ptr);
1031
1032/**
1033 * @brief Provide a talloc context that is freed at program exit.
1034 *
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.
1038 *
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.
1047 *
1048 * @return A talloc context, NULL on error.
1049 */
1050void *talloc_autofree_context(void);
1051
1052/**
1053 * @brief Get the size of a talloc chunk.
1054 *
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.
1058 *
1059 * @param[in] ctx The talloc chunk.
1060 *
1061 * @return The size of the talloc chunk.
1062 */
1063size_t talloc_get_size(const void *ctx);
1064
1065/**
1066 * @brief Show the parentage of a context.
1067 *
1068 * @param[in] context The talloc context to look at.
1069 *
1070 * @param[in] file The output to use, a file, stdout or stderr.
1071 */
1072void talloc_show_parents(const void *context, FILE *file);
1073
1074/**
1075 * @brief Check if a context is parent of a talloc chunk.
1076 *
1077 * This checks if context is referenced in the talloc hierarchy above ptr.
1078 *
1079 * @param[in] context The assumed talloc context.
1080 *
1081 * @param[in] ptr The talloc chunk to check.
1082 *
1083 * @return Return 1 if this is the case, 0 if not.
1084 */
1085int talloc_is_parent(const void *context, const void *ptr);
1086
1087/**
1088 * @brief Change the parent context of a talloc pointer.
1089 *
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.
1093 *
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.
1097 *
1098 * @param[in] old_parent
1099 * @param[in] new_parent
1100 * @param[in] ptr
1101 *
1102 * @return Return the pointer you passed. It does not have any
1103 * failure modes.
1104 */
1105void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr);
1106
1107/* @} ******************************************************************/
1108
1109/**
1110 * @defgroup talloc_array The talloc array functions
1111 * @ingroup talloc
1112 *
1113 * Talloc contains some handy helpers for handling Arrays conveniently
1114 *
1115 * @{
1116 */
1117
1118#ifdef DOXYGEN
1119/**
1120 * @brief Allocate an array.
1121 *
1122 * The macro is equivalent to:
1123 *
1124 * @code
1125 * (type *)talloc_size(ctx, sizeof(type) * count);
1126 * @endcode
1127 *
1128 * except that it provides integer overflow protection for the multiply,
1129 * returning NULL if the multiply overflows.
1130 *
1131 * @param[in] ctx The talloc context to hang the result off.
1132 *
1133 * @param[in] type The type that we want to allocate.
1134 *
1135 * @param[in] count The number of 'type' elements you want to allocate.
1136 *
1137 * @return The allocated result, properly cast to 'type *', NULL on
1138 * error.
1139 *
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
1146 *
1147 * @see talloc()
1148 * @see talloc_zero_array()
1149 */
1150void *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)
1153void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
1154#endif
1155
1156#ifdef DOXYGEN
1157/**
1158 * @brief Allocate an array.
1159 *
1160 * @param[in] ctx The talloc context to hang the result off.
1161 *
1162 * @param[in] size The size of an array element.
1163 *
1164 * @param[in] count The number of elements you want to allocate.
1165 *
1166 * @return The allocated result, NULL on error.
1167 */
1168void *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
1172
1173#ifdef DOXYGEN
1174/**
1175 * @brief Allocate an array into a typed pointer.
1176 *
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.
1182 *
1183 * @param[in] ctx The talloc context to hang the result off.
1184 *
1185 * @param[in] ptr The pointer you want to assign the result to.
1186 *
1187 * @param[in] count The number of elements you want to allocate.
1188 *
1189 * @return The allocated memory chunk, properly casted. NULL on
1190 * error.
1191 */
1192void *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
1196
1197#ifdef DOXYGEN
1198/**
1199 * @brief Get the number of elements in a talloc'ed array.
1200 *
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.
1203 *
1204 * @param[in] ctx The allocated array.
1205 *
1206 * @return The number of elements in ctx.
1207 */
1208size_t talloc_array_length(const void *ctx);
1209#else
1210#define talloc_array_length(ctx) (talloc_get_size(ctx)/sizeof(*ctx))
1211#endif
1212
1213#ifdef DOXYGEN
1214/**
1215 * @brief Allocate a zero-initialized array
1216 *
1217 * @param[in] ctx The talloc context to hang the result off.
1218 *
1219 * @param[in] type The type that we want to allocate.
1220 *
1221 * @param[in] count The number of "type" elements you want to allocate.
1222 *
1223 * @return The allocated result casted to "type *", NULL on error.
1224 *
1225 * The talloc_zero_array() macro is equivalent to:
1226 *
1227 * @code
1228 * ptr = talloc_array(ctx, type, count);
1229 * if (ptr) memset(ptr, 0, sizeof(type) * count);
1230 * @endcode
1231 */
1232void *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)
1235void *_talloc_zero_array(const void *ctx,
1236 size_t el_size,
1237 unsigned count,
1238 const char *name);
1239#endif
1240
1241#ifdef DOXYGEN
1242/**
1243 * @brief Change the size of a talloc array.
1244 *
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.
1248 *
1249 * talloc_realloc() has the following equivalences:
1250 *
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
1256 *
1257 * The "context" argument is only used if "ptr" is NULL, otherwise it is
1258 * ignored.
1259 *
1260 * @param[in] ctx The parent context used if ptr is NULL.
1261 *
1262 * @param[in] ptr The chunk to be resized.
1263 *
1264 * @param[in] type The type of the array element inside ptr.
1265 *
1266 * @param[in] count The intended number of array elements.
1267 *
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()).
1271 */
1272void *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)
1275void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
1276#endif
1277
1278#ifdef DOXYGEN
1279/**
1280 * @brief Untyped realloc to change the size of a talloc array.
1281 *
1282 * The macro is useful when the type is not known so the typesafe
1283 * talloc_realloc() cannot be used.
1284 *
1285 * @param[in] ctx The parent context used if 'ptr' is NULL.
1286 *
1287 * @param[in] ptr The chunk to be resized.
1288 *
1289 * @param[in] size The new chunk size.
1290 *
1291 * @return The new array, NULL on error.
1292 */
1293void *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__)
1296void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
1297#endif
1298
1299/**
1300 * @brief Provide a function version of talloc_realloc_size.
1301 *
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.
1307 *
1308 * @param[in] context The parent context used if ptr is NULL.
1309 *
1310 * @param[in] ptr The chunk to be resized.
1311 *
1312 * @param[in] size The new chunk size.
1313 *
1314 * @return The new chunk, NULL on error.
1315 */
1316void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
1317
1318/* @} ******************************************************************/
1319
1320/**
1321 * @defgroup talloc_string The talloc string functions.
1322 * @ingroup talloc
1323 *
1324 * talloc string allocation and manipulation functions.
1325 * @{
1326 */
1327
1328/**
1329 * @brief Duplicate a string into a talloc chunk.
1330 *
1331 * This function is equivalent to:
1332 *
1333 * @code
1334 * ptr = talloc_size(ctx, strlen(p)+1);
1335 * if (ptr) memcpy(ptr, p, strlen(p)+1);
1336 * @endcode
1337 *
1338 * This functions sets the name of the new pointer to the passed
1339 * string. This is equivalent to:
1340 *
1341 * @code
1342 * talloc_set_name_const(ptr, ptr)
1343 * @endcode
1344 *
1345 * @param[in] t The talloc context to hang the result off.
1346 *
1347 * @param[in] p The string you want to duplicate.
1348 *
1349 * @return The duplicated string, NULL on error.
1350 */
1351char *talloc_strdup(const void *t, const char *p);
1352
1353/**
1354 * @brief Append a string to given string.
1355 *
1356 * The destination string is reallocated to take
1357 * <code>strlen(s) + strlen(a) + 1</code> characters.
1358 *
1359 * This functions sets the name of the new pointer to the new
1360 * string. This is equivalent to:
1361 *
1362 * @code
1363 * talloc_set_name_const(ptr, ptr)
1364 * @endcode
1365 *
1366 * If <code>s == NULL</code> then new context is created.
1367 *
1368 * @param[in] s The destination to append to.
1369 *
1370 * @param[in] a The string you want to append.
1371 *
1372 * @return The concatenated strings, NULL on error.
1373 *
1374 * @see talloc_strdup()
1375 * @see talloc_strdup_append_buffer()
1376 */
1377char *talloc_strdup_append(char *s, const char *a);
1378
1379/**
1380 * @brief Append a string to a given buffer.
1381 *
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.
1384 *
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.
1388 *
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'
1393 *
1394 * char *app = talloc_strdup_append(str_a, ", hello");
1395 * char *buf = talloc_strdup_append_buffer(str_b, ", hello");
1396 *
1397 * printf("%s\n", app); // hello, hello (app = "hello, hello")
1398 * printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1399 * @endcode
1400 *
1401 * If <code>s == NULL</code> then new context is created.
1402 *
1403 * @param[in] s The destination buffer to append to.
1404 *
1405 * @param[in] a The string you want to append.
1406 *
1407 * @return The concatenated strings, NULL on error.
1408 *
1409 * @see talloc_strdup()
1410 * @see talloc_strdup_append()
1411 * @see talloc_array_length()
1412 */
1413char *talloc_strdup_append_buffer(char *s, const char *a);
1414
1415/**
1416 * @brief Duplicate a length-limited string into a talloc chunk.
1417 *
1418 * This function is the talloc equivalent of the C library function strndup(3).
1419 *
1420 * This functions sets the name of the new pointer to the passed string. This is
1421 * equivalent to:
1422 *
1423 * @code
1424 * talloc_set_name_const(ptr, ptr)
1425 * @endcode
1426 *
1427 * @param[in] t The talloc context to hang the result off.
1428 *
1429 * @param[in] p The string you want to duplicate.
1430 *
1431 * @param[in] n The maximum string length to duplicate.
1432 *
1433 * @return The duplicated string, NULL on error.
1434 */
1435char *talloc_strndup(const void *t, const char *p, size_t n);
1436
1437/**
1438 * @brief Append at most n characters of a string to given string.
1439 *
1440 * The destination string is reallocated to take
1441 * <code>strlen(s) + strnlen(a, n) + 1</code> characters.
1442 *
1443 * This functions sets the name of the new pointer to the new
1444 * string. This is equivalent to:
1445 *
1446 * @code
1447 * talloc_set_name_const(ptr, ptr)
1448 * @endcode
1449 *
1450 * If <code>s == NULL</code> then new context is created.
1451 *
1452 * @param[in] s The destination string to append to.
1453 *
1454 * @param[in] a The source string you want to append.
1455 *
1456 * @param[in] n The number of characters you want to append from the
1457 * string.
1458 *
1459 * @return The concatenated strings, NULL on error.
1460 *
1461 * @see talloc_strndup()
1462 * @see talloc_strndup_append_buffer()
1463 */
1464char *talloc_strndup_append(char *s, const char *a, size_t n);
1465
1466/**
1467 * @brief Append at most n characters of a string to given buffer
1468 *
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.
1471 *
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.
1475 *
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'
1480 *
1481 * char *app = talloc_strndup_append(str_a, ", hello", 7);
1482 * char *buf = talloc_strndup_append_buffer(str_b, ", hello", 7);
1483 *
1484 * printf("%s\n", app); // hello, hello (app = "hello, hello")
1485 * printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1486 * @endcode
1487 *
1488 * If <code>s == NULL</code> then new context is created.
1489 *
1490 * @param[in] s The destination buffer to append to.
1491 *
1492 * @param[in] a The source string you want to append.
1493 *
1494 * @param[in] n The number of characters you want to append from the
1495 * string.
1496 *
1497 * @return The concatenated strings, NULL on error.
1498 *
1499 * @see talloc_strndup()
1500 * @see talloc_strndup_append()
1501 * @see talloc_array_length()
1502 */
1503char *talloc_strndup_append_buffer(char *s, const char *a, size_t n);
1504
1505/**
1506 * @brief Format a string given a va_list.
1507 *
1508 * This function is the talloc equivalent of the C library function
1509 * vasprintf(3).
1510 *
1511 * This functions sets the name of the new pointer to the new string. This is
1512 * equivalent to:
1513 *
1514 * @code
1515 * talloc_set_name_const(ptr, ptr)
1516 * @endcode
1517 *
1518 * @param[in] t The talloc context to hang the result off.
1519 *
1520 * @param[in] fmt The format string.
1521 *
1522 * @param[in] ap The parameters used to fill fmt.
1523 *
1524 * @return The formatted string, NULL on error.
1525 */
1526char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1527
1528/**
1529 * @brief Format a string given a va_list and append it to the given destination
1530 * string.
1531 *
1532 * @param[in] s The destination string to append to.
1533 *
1534 * @param[in] fmt The format string.
1535 *
1536 * @param[in] ap The parameters used to fill fmt.
1537 *
1538 * @return The formatted string, NULL on error.
1539 *
1540 * @see talloc_vasprintf()
1541 */
1542char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1543
1544/**
1545 * @brief Format a string given a va_list and append it to the given destination
1546 * buffer.
1547 *
1548 * @param[in] s The destination buffer to append to.
1549 *
1550 * @param[in] fmt The format string.
1551 *
1552 * @param[in] ap The parameters used to fill fmt.
1553 *
1554 * @return The formatted string, NULL on error.
1555 *
1556 * @see talloc_vasprintf()
1557 */
1558char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
1559
1560/**
1561 * @brief Format a string.
1562 *
1563 * This function is the talloc equivalent of the C library function asprintf(3).
1564 *
1565 * This functions sets the name of the new pointer to the new string. This is
1566 * equivalent to:
1567 *
1568 * @code
1569 * talloc_set_name_const(ptr, ptr)
1570 * @endcode
1571 *
1572 * @param[in] t The talloc context to hang the result off.
1573 *
1574 * @param[in] fmt The format string.
1575 *
1576 * @param[in] ... The parameters used to fill fmt.
1577 *
1578 * @return The formatted string, NULL on error.
1579 */
1580char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1581
1582/**
1583 * @brief Append a formatted string to another string.
1584 *
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.
1588 *
1589 * This functions sets the name of the new pointer to the new
1590 * string. This is equivalent to:
1591 *
1592 * @code
1593 * talloc_set_name_const(ptr, ptr)
1594 * @endcode
1595 *
1596 * If <code>s == NULL</code> then new context is created.
1597 *
1598 * @param[in] s The string to append to.
1599 *
1600 * @param[in] fmt The format string.
1601 *
1602 * @param[in] ... The parameters used to fill fmt.
1603 *
1604 * @return The formatted string, NULL on error.
1605 */
1606char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1607
1608/**
1609 * @brief Append a formatted string to another string.
1610 *
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.
1613 *
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.
1617 *
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'
1622 *
1623 * char *app = talloc_asprintf_append(str_a, "%s", ", hello");
1624 * char *buf = talloc_strdup_append_buffer(str_b, "%s", ", hello");
1625 *
1626 * printf("%s\n", app); // hello, hello (app = "hello, hello")
1627 * printf("%s\n", buf); // hello (buf = "hello\0world, hello")
1628 * @endcode
1629 *
1630 * If <code>s == NULL</code> then new context is created.
1631 *
1632 * @param[in] s The string to append to
1633 *
1634 * @param[in] fmt The format string.
1635 *
1636 * @param[in] ... The parameters used to fill fmt.
1637 *
1638 * @return The formatted string, NULL on error.
1639 *
1640 * @see talloc_asprintf()
1641 * @see talloc_asprintf_append()
1642 */
1643char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1644
1645/* @} ******************************************************************/
1646
1647/**
1648 * @defgroup talloc_debug The talloc debugging support functions
1649 * @ingroup talloc
1650 *
1651 * To aid memory debugging, talloc contains routines to inspect the currently
1652 * allocated memory hierarchy.
1653 *
1654 * @{
1655 */
1656
1657/**
1658 * @brief Walk a complete talloc hierarchy.
1659 *
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.
1664 *
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.
1669 *
1670 * The recursion is stopped when depth >= max_depth.
1671 * max_depth = -1 means only stop at leaf nodes.
1672 *
1673 * @param[in] ptr The talloc chunk.
1674 *
1675 * @param[in] depth Internal parameter to control recursion. Call with 0.
1676 *
1677 * @param[in] max_depth Maximum recursion level.
1678 *
1679 * @param[in] callback Function to be called on every chunk.
1680 *
1681 * @param[in] private_data Private pointer passed to callback.
1682 */
1683void 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);
1689
1690/**
1691 * @brief Print a talloc hierarchy.
1692 *
1693 * This provides a more flexible reports than talloc_report(). It
1694 * will let you specify the depth and max_depth.
1695 *
1696 * @param[in] ptr The talloc chunk.
1697 *
1698 * @param[in] depth Internal parameter to control recursion. Call with 0.
1699 *
1700 * @param[in] max_depth Maximum recursion level.
1701 *
1702 * @param[in] f The file handle to print to.
1703 */
1704void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
1705
1706/**
1707 * @brief Print a summary report of all memory used by ptr.
1708 *
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.
1713 *
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.
1718 *
1719 * @param[in] ptr The talloc chunk.
1720 *
1721 * @param[in] f The file handle to print to.
1722 *
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
1731 *
1732 * @see talloc_report()
1733 */
1734void talloc_report_full(const void *ptr, FILE *f);
1735
1736/**
1737 * @brief Print a summary report of all memory used by ptr.
1738 *
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.
1742 *
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.
1746 *
1747 * @param[in] ptr The talloc chunk.
1748 *
1749 * @param[in] f The file handle to print to.
1750 *
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
1759 *
1760 * @see talloc_report_full()
1761 */
1762void talloc_report(const void *ptr, FILE *f);
1763
1764/**
1765 * @brief Enable tracking the use of NULL memory contexts.
1766 *
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();
1770 */
1771void talloc_enable_null_tracking(void);
1772
1773/**
1774 * @brief Enable tracking the use of NULL memory contexts.
1775 *
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();
1779 */
1780void talloc_enable_null_tracking_no_autofree(void);
1781
1782/**
1783 * @brief Disable tracking of the NULL memory context.
1784 *
1785 * This disables tracking of the NULL memory context.
1786 */
1787void talloc_disable_null_tracking(void);
1788
1789/**
1790 * @brief Enable leak report when a program exits.
1791 *
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.
1795 *
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.
1801 *
1802 * Here is a typical talloc report:
1803 *
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
1814 */
1815void talloc_enable_leak_report(void);
1816
1817/**
1818 * @brief Enable full leak report when a program exits.
1819 *
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.
1823 *
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.
1829 *
1830 * Here is a typical full report:
1831 *
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
1842 */
1843void talloc_enable_leak_report_full(void);
1844
1845/**
1846 * @brief Set a custom "abort" function that is called on serious error.
1847 *
1848 * The default "abort" function is <code>abort()</code>.
1849 *
1850 * The "abort" function is called when:
1851 *
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>
1858 *
1859 * Example:
1860 *
1861 * @code
1862 * void my_abort(const char *reason)
1863 * {
1864 * fprintf(stderr, "talloc abort: %s\n", reason);
1865 * abort();
1866 * }
1867 *
1868 * talloc_set_abort_fn(my_abort);
1869 * @endcode
1870 *
1871 * @param[in] abort_fn The new "abort" function.
1872 *
1873 * @see talloc_set_log_fn()
1874 * @see talloc_get_type()
1875 */
1876void talloc_set_abort_fn(void (*abort_fn)(const char *reason));
1877
1878/**
1879 * @brief Set a logging function.
1880 *
1881 * @param[in] log_fn The logging function.
1882 *
1883 * @see talloc_set_log_stderr()
1884 * @see talloc_set_abort_fn()
1885 */
1886void talloc_set_log_fn(void (*log_fn)(const char *message));
1887
1888/**
1889 * @brief Set stderr as the output for logs.
1890 *
1891 * @see talloc_set_log_fn()
1892 * @see talloc_set_abort_fn()
1893 */
1894void talloc_set_log_stderr(void);
1895
1896/**
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 function again on the same context.
1902 * Memory limits can also be nested, meaning a child 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.
1909 *
1910 * @param[in] ctx The talloc context to set the limit on
1911 * @param[in] max_size The (new) max_size
1912 */
1913int talloc_set_memlimit(const void *ctx, size_t max_size);
1914
1915/* @} ******************************************************************/
1916
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
1925
1926#ifndef TALLOC_MAX_DEPTH
1927#define TALLOC_MAX_DEPTH 10000
1928#endif
1929
1930#ifdef __cplusplus
1931} /* end of extern "C" */
1932#endif
1933
1934#endif