4 Copyright (C) Andrew Tridgell 2004-2005
5 Copyright (C) Stefan Metzmacher 2006
7 ** NOTE! The following LGPL license applies to the talloc
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <ccan/typesafe_cb/typesafe_cb.h>
30 #include <ccan/compiler/compiler.h>
34 this uses a little trick to allow __LINE__ to be stringified
37 #define __TALLOC_STRING_LINE1__(s) #s
38 #define __TALLOC_STRING_LINE2__(s) __TALLOC_STRING_LINE1__(s)
39 #define __TALLOC_STRING_LINE3__ __TALLOC_STRING_LINE2__(__LINE__)
40 #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__
43 /* try to make talloc_set_destructor() and talloc_steal() type safe,
44 if we have a recent gcc */
46 #define _TALLOC_TYPEOF(ptr) __typeof__(ptr)
48 #define _TALLOC_TYPEOF(ptr) void *
51 #define talloc_move(ctx, ptr) (_TALLOC_TYPEOF(*(ptr)))_talloc_move((ctx),(void *)(ptr))
54 * talloc - allocate dynamic memory for a type
55 * @ctx: context to be parent of this allocation, or NULL.
56 * @type: the type to be allocated.
58 * The talloc() macro is the core of the talloc library. It takes a memory
59 * context and a type, and returns a pointer to a new area of memory of the
62 * The returned pointer is itself a talloc context, so you can use it as the
63 * context argument to more calls to talloc if you wish.
65 * The returned pointer is a "child" of @ctx. This means that if you
66 * talloc_free() @ctx then the new child disappears as well. Alternatively you
67 * can free just the child.
69 * @ctx can be NULL, in which case a new top level context is created.
72 * unsigned int *a, *b;
73 * a = talloc(NULL, unsigned int);
74 * b = talloc(a, unsigned int);
77 * talloc_zero, talloc_array, talloc_steal, talloc_free.
79 #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
82 * talloc_set - allocate dynamic memory for a type, into a pointer
83 * @ptr: pointer to the pointer to assign.
84 * @ctx: context to be parent of this allocation, or NULL.
86 * talloc_set() does a talloc, but also adds a destructor which will make the
87 * pointer invalid when it is freed. This can find many use-after-free bugs.
89 * Note that the destructor is chained off a zero-length allocation, and so
90 * is not affected by talloc_set_destructor().
94 * a = talloc(NULL, unsigned int);
95 * talloc_set(&b, a, unsigned int);
97 * *b = 1; // This will crash!
102 #define talloc_set(pptr, ctx) \
103 _talloc_set((pptr), (ctx), sizeof(&**(pptr)), __location__)
106 * talloc_free - free talloc'ed memory and its children
107 * @ptr: the talloced pointer to free
109 * The talloc_free() function frees a piece of talloc memory, and all its
110 * children. You can call talloc_free() on any pointer returned by talloc().
112 * The return value of talloc_free() indicates success or failure, with 0
113 * returned for success and -1 for failure. The only possible failure condition
114 * is if the pointer had a destructor attached to it and the destructor
115 * returned -1. See talloc_set_destructor() for details on destructors.
116 * errno will be preserved unless the talloc_free fails.
118 * If this pointer has an additional parent when talloc_free() is called then
119 * the memory is not actually released, but instead the most recently
120 * established parent is destroyed. See talloc_reference() for details on
121 * establishing additional parents.
123 * For more control on which parent is removed, see talloc_unlink().
125 * talloc_free() operates recursively on its children.
128 * unsigned int *a, *b;
129 * a = talloc(NULL, unsigned int);
130 * b = talloc(a, unsigned int);
135 * talloc_set_destructor, talloc_unlink
137 int talloc_free(const void *ptr);
140 * talloc_set_destructor: set a destructor for when this pointer is freed
141 * @ptr: the talloc pointer to set the destructor on
142 * @destructor: the function to be called
144 * The function talloc_set_destructor() sets the "destructor" for the pointer
145 * @ptr. A destructor is a function that is called when the memory used by a
146 * pointer is about to be released. The destructor receives the pointer as an
147 * argument, and should return 0 for success and -1 for failure.
149 * The destructor can do anything it wants to, including freeing other pieces
150 * of memory. A common use for destructors is to clean up operating system
151 * resources (such as open file descriptors) contained in the structure the
152 * destructor is placed on.
154 * You can only place one destructor on a pointer. If you need more than one
155 * destructor then you can create a zero-length child of the pointer and place
156 * an additional destructor on that.
158 * To remove a destructor call talloc_set_destructor() with NULL for the
161 * If your destructor attempts to talloc_free() the pointer that it is the
162 * destructor for then talloc_free() will return -1 and the free will be
163 * ignored. This would be a pointless operation anyway, as the destructor is
164 * only called when the memory is just about to go away.
167 * static int destroy_fd(int *fd)
173 * int *open_file(const char *filename)
175 * int *fd = talloc(NULL, int);
176 * *fd = open(filename, O_RDONLY);
181 * // Whenever they free this, we close the file.
182 * talloc_set_destructor(fd, destroy_fd);
187 * talloc, talloc_free
189 #define talloc_set_destructor(ptr, function) \
190 _talloc_set_destructor((ptr), typesafe_cb_def(int, (function), (ptr)))
193 * talloc_zero - allocate zeroed dynamic memory for a type
194 * @ctx: context to be parent of this allocation, or NULL.
195 * @type: the type to be allocated.
197 * The talloc_zero() macro is equivalent to:
199 * ptr = talloc(ctx, type);
200 * if (ptr) memset(ptr, 0, sizeof(type));
203 * unsigned int *a, *b;
204 * a = talloc_zero(NULL, unsigned int);
205 * b = talloc_zero(a, unsigned int);
208 * talloc, talloc_zero_size, talloc_zero_array
210 #define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
213 * talloc_array - allocate dynamic memory for an array of a given type
214 * @ctx: context to be parent of this allocation, or NULL.
215 * @type: the type to be allocated.
216 * @count: the number of elements to be allocated.
218 * The talloc_array() macro is a safe way of allocating an array. It is
221 * (type *)talloc_size(ctx, sizeof(type) * count);
223 * except that it provides integer overflow protection for the multiply,
224 * returning NULL if the multiply overflows.
227 * unsigned int *a, *b;
228 * a = talloc_zero(NULL, unsigned int);
229 * b = talloc_array(a, unsigned int, 100);
232 * talloc, talloc_zero_array
234 #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
237 * talloc_size - allocate a particular size of memory
238 * @ctx: context to be parent of this allocation, or NULL.
239 * @size: the number of bytes to allocate
241 * The function talloc_size() should be used when you don't have a convenient
242 * type to pass to talloc(). Unlike talloc(), it is not type safe (as it
243 * returns a void *), so you are on your own for type checking.
245 * Best to use talloc() or talloc_array() instead.
248 * void *mem = talloc_size(NULL, 100);
251 * talloc, talloc_array, talloc_zero_size
253 #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
257 * talloc_steal - change/set the parent context of a talloc pointer
258 * @ctx: the new parent
259 * @ptr: the talloc pointer to reparent
261 * The talloc_steal() function changes the parent context of a talloc
262 * pointer. It is typically used when the context that the pointer is currently
263 * a child of is going to be freed and you wish to keep the memory for a longer
266 * The talloc_steal() function returns the pointer that you pass it. It does
267 * not have any failure modes.
269 * NOTE: It is possible to produce loops in the parent/child relationship if
270 * you are not careful with talloc_steal(). No guarantees are provided as to
271 * your sanity or the safety of your data if you do this.
273 * talloc_steal (new_ctx, NULL) will return NULL with no sideeffects.
276 * unsigned int *a, *b;
277 * a = talloc(NULL, unsigned int);
278 * b = talloc(NULL, unsigned int);
279 * // Reparent b to a as if we'd done 'b = talloc(a, unsigned int)'.
280 * talloc_steal(a, b);
285 #define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) _talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr)); _talloc_steal_ret; }) /* this extremely strange macro is to avoid some braindamaged warning stupidity in gcc 4.1.x */
287 #define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr))
288 #endif /* HAVE_TYPEOF */
291 * talloc_report_full - report all the memory used by a pointer and children.
292 * @ptr: the context to report on
293 * @f: the file to report to
295 * Recursively print the entire tree of memory referenced by the
296 * pointer. References in the tree are shown by giving the name of the pointer
297 * that is referenced.
299 * You can pass NULL for the pointer, in which case a report is printed for the
300 * top level memory context, but only if talloc_enable_null_tracking() has been
304 * unsigned int *a, *b;
305 * a = talloc(NULL, unsigned int);
306 * b = talloc(a, unsigned int);
307 * fprintf(stderr, "Dumping memory tree for a:\n");
308 * talloc_report_full(a, stderr);
313 void talloc_report_full(const void *ptr, FILE *f);
316 * talloc_reference - add an additional parent to a context
317 * @ctx: the additional parent
318 * @ptr: the talloc pointer
320 * The talloc_reference() function makes @ctx an additional parent of @ptr.
322 * The return value of talloc_reference() is always the original pointer @ptr,
323 * unless talloc ran out of memory in creating the reference in which case it
324 * will return NULL (each additional reference consumes around 48 bytes of
325 * memory on intel x86 platforms).
327 * If @ptr is NULL, then the function is a no-op, and simply returns NULL.
329 * After creating a reference you can free it in one of the following ways:
331 * - you can talloc_free() any parent of the original pointer. That will
332 * reduce the number of parents of this pointer by 1, and will cause this
333 * pointer to be freed if it runs out of parents.
335 * - you can talloc_free() the pointer itself. That will destroy the most
336 * recently established parent to the pointer and leave the pointer as a
337 * child of its current parent.
339 * For more control on which parent to remove, see talloc_unlink().
341 * unsigned int *a, *b, *c;
342 * a = talloc(NULL, unsigned int);
343 * b = talloc(NULL, unsigned int);
344 * c = talloc(a, unsigned int);
345 * // b also serves as a parent of c.
346 * talloc_reference(b, c);
348 #define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference((ctx),(ptr))
351 * talloc_unlink: remove a specific parent from a talloc pointer.
352 * @context: the parent to remove
353 * @ptr: the talloc pointer
355 * The talloc_unlink() function removes a specific parent from @ptr. The
356 * context passed must either be a context used in talloc_reference() with this
357 * pointer, or must be a direct parent of @ptr.
359 * Note that if the parent has already been removed using talloc_free() then
360 * this function will fail and will return -1. Likewise, if @ptr is NULL,
361 * then the function will make no modifications and return -1.
363 * Usually you can just use talloc_free() instead of talloc_unlink(), but
364 * sometimes it is useful to have the additional control on which parent is
367 * unsigned int *a, *b, *c;
368 * a = talloc(NULL, unsigned int);
369 * b = talloc(NULL, unsigned int);
370 * c = talloc(a, unsigned int);
371 * // b also serves as a parent of c.
372 * talloc_reference(b, c);
373 * talloc_unlink(b, c);
375 int talloc_unlink(const void *context, void *ptr);
378 * talloc_report - print a summary of memory used by a pointer
380 * The talloc_report() function prints a summary report of all memory
381 * used by @ptr. One line of report is printed for each immediate child of
382 * @ptr, showing the total memory and number of blocks used by that child.
384 * You can pass NULL for the pointer, in which case a report is printed for the
385 * top level memory context, but only if talloc_enable_null_tracking() has been
389 * unsigned int *a, *b;
390 * a = talloc(NULL, unsigned int);
391 * b = talloc(a, unsigned int);
392 * fprintf(stderr, "Summary of memory tree for a:\n");
393 * talloc_report(a, stderr);
398 void talloc_report(const void *ptr, FILE *f);
401 * talloc_ptrtype - allocate a size of memory suitable for this pointer
402 * @ctx: context to be parent of this allocation, or NULL.
403 * @ptr: the pointer whose type we are to allocate
405 * The talloc_ptrtype() macro should be used when you have a pointer and
406 * want to allocate memory to point at with this pointer. When compiling
407 * with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()
408 * and talloc_get_name() will return the current location in the source file.
412 * unsigned int *a = talloc_ptrtype(NULL, a);
414 #define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
417 * talloc_new - create a new context
418 * @ctx: the context to use as a parent.
420 * This is a utility macro that creates a new memory context hanging off an
421 * exiting context, automatically naming it "talloc_new: __location__" where
422 * __location__ is the source line it is called from. It is particularly useful
423 * for creating a new temporary working context.
425 #define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
428 * talloc_zero_size - allocate a particular size of zeroed memory
430 * The talloc_zero_size() function is useful when you don't have a known type.
432 #define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
435 * talloc_zero_array - allocate an array of zeroed types
436 * @ctx: context to be parent of this allocation, or NULL.
437 * @type: the type to be allocated.
438 * @count: the number of elements to be allocated.
440 * Just like talloc_array, but zeroes the memory.
442 #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
445 * talloc_array_size - allocate an array of elements of the given size
446 * @ctx: context to be parent of this allocation, or NULL.
447 * @size: the size of each element
448 * @count: the number of elements to be allocated.
450 * Typeless form of talloc_array.
452 #define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
455 * talloc_array_ptrtype - allocate an array of memory suitable for this pointer
456 * @ctx: context to be parent of this allocation, or NULL.
457 * @ptr: the pointer whose type we are to allocate
458 * @count: the number of elements for the array
460 * Like talloc_ptrtype(), except it allocates an array.
462 #define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
465 * talloc_realloc - resize a talloc array
466 * @ctx: the parent to assign (if p is NULL)
467 * @p: the memory to reallocate
468 * @type: the type of the object to allocate
469 * @count: the number of objects to reallocate
471 * The talloc_realloc() macro changes the size of a talloc pointer. The "count"
472 * argument is the number of elements of type "type" that you want the
473 * resulting pointer to hold.
475 * talloc_realloc() has the following equivalences:
477 * talloc_realloc(context, NULL, type, 1) ==> talloc(context, type);
478 * talloc_realloc(context, NULL, type, N) ==> talloc_array(context, type, N);
479 * talloc_realloc(context, ptr, type, 0) ==> talloc_free(ptr);
481 * The "context" argument is only used if "ptr" is NULL, otherwise it is
484 * talloc_realloc() returns the new pointer, or NULL on failure. The call will
485 * fail either due to a lack of memory, or because the pointer has more than
486 * one parent (see talloc_reference()).
488 #define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
491 * talloc_realloc_size - resize talloc memory
492 * @ctx: the parent to assign (if p is NULL)
493 * @ptr: the memory to reallocate
494 * @size: the new size of memory.
496 * The talloc_realloc_size() function is useful when the type is not known so
497 * the typesafe talloc_realloc() cannot be used.
499 #define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
502 * talloc_strdup - duplicate a string
503 * @ctx: the talloc context for the new string
504 * @p: the string to copy
506 * The talloc_strdup() function is equivalent to:
508 * ptr = talloc_size(ctx, strlen(p)+1);
509 * if (ptr) memcpy(ptr, p, strlen(p)+1);
511 * This functions sets the name of the new pointer to the passed string. This
514 * talloc_set_name_const(ptr, ptr)
516 char *talloc_strdup(const void *t, const char *p);
519 * talloc_strndup - duplicate a limited length of a string
520 * @ctx: the talloc context for the new string
521 * @p: the string to copy
522 * @n: the maximum length of the returned string.
524 * The talloc_strndup() function is the talloc equivalent of the C library
525 * function strndup(): the result will be truncated to @n characters before
526 * the nul terminator.
528 * This functions sets the name of the new pointer to the passed string. This
531 * talloc_set_name_const(ptr, ptr)
533 char *talloc_strndup(const void *t, const char *p, size_t n);
536 * talloc_memdup - duplicate some talloc memory
538 * The talloc_memdup() function is equivalent to:
540 * ptr = talloc_size(ctx, size);
541 * if (ptr) memcpy(ptr, p, size);
543 #define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
546 * talloc_asprintf - sprintf into a talloc buffer.
547 * @t: The context to allocate the buffer from
548 * @fmt: printf-style format for the buffer.
550 * The talloc_asprintf() function is the talloc equivalent of the C library
551 * function asprintf().
553 * This functions sets the name of the new pointer to the new string. This is
556 * talloc_set_name_const(ptr, ptr)
558 char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
561 * talloc_append_string - concatenate onto a tallocated string
562 * @orig: the tallocated string to append to
563 * @append: the string to add, or NULL to add nothing.
565 * The talloc_append_string() function appends the given formatted string to
568 * This function sets the name of the new pointer to the new string. This is
571 * talloc_set_name_const(ptr, ptr)
573 char *talloc_append_string(char *orig, const char *append);
576 * talloc_asprintf_append - sprintf onto the end of a talloc buffer.
577 * @s: The tallocated string buffer
578 * @fmt: printf-style format to append to the buffer.
580 * The talloc_asprintf_append() function appends the given formatted string to
583 * This functions sets the name of the new pointer to the new string. This is
585 * talloc_set_name_const(ptr, ptr)
587 char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
590 * talloc_vasprintf - vsprintf into a talloc buffer.
591 * @t: The context to allocate the buffer from
592 * @fmt: printf-style format for the buffer
593 * @ap: va_list arguments
595 * The talloc_vasprintf() function is the talloc equivalent of the C library
596 * function vasprintf()
598 * This functions sets the name of the new pointer to the new string. This is
601 * talloc_set_name_const(ptr, ptr)
603 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
606 * talloc_vasprintf_append - sprintf onto the end of a talloc buffer.
607 * @t: The context to allocate the buffer from
608 * @fmt: printf-style format for the buffer
609 * @ap: va_list arguments
611 * The talloc_vasprintf_append() function is equivalent to
612 * talloc_asprintf_append(), except it takes a va_list.
614 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
617 * talloc_set_type - force the name of a pointer to a particular type
618 * @ptr: the talloc pointer
619 * @type: the type whose name to set the ptr name to.
621 * This macro allows you to force the name of a pointer to be a particular
622 * type. This can be used in conjunction with talloc_get_type() to do type
623 * checking on void* pointers.
625 * It is equivalent to this:
626 * talloc_set_name_const(ptr, #type)
628 #define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
631 * talloc_get_type - convert a talloced pointer with typechecking
632 * @ptr: the talloc pointer
633 * @type: the type which we expect the talloced pointer to be.
635 * This macro allows you to do type checking on talloc pointers. It is
636 * particularly useful for void* private pointers. It is equivalent to this:
638 * (type *)talloc_check_name(ptr, #type)
640 #define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
643 * talloc_find_parent_byname - find a talloc parent by type
644 * @ptr: the talloc pointer
645 * @type: the type we're looking for
647 * Find a parent memory context of the current context that has the given
648 * name. This can be very useful in complex programs where it may be difficult
649 * to pass all information down to the level you need, but you know the
650 * structure you want is a parent of another context.
652 #define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
655 * talloc_increase_ref_count - hold a reference to a talloc pointer
656 * @ptr: the talloc pointer
658 * The talloc_increase_ref_count(ptr) function is exactly equivalent to:
660 * talloc_reference(NULL, ptr);
662 * You can use either syntax, depending on which you think is clearer in your
665 * It returns 0 on success and -1 on failure.
667 int talloc_increase_ref_count(const void *ptr);
670 * talloc_set_name - set the name for a talloc pointer
671 * @ptr: the talloc pointer
672 * @fmt: the printf-style format string for the name
674 * Each talloc pointer has a "name". The name is used principally for debugging
675 * purposes, although it is also possible to set and get the name on a pointer
676 * in as a way of "marking" pointers in your code.
678 * The main use for names on pointer is for "talloc reports". See
679 * talloc_report() and talloc_report_full() for details. Also see
680 * talloc_enable_leak_report() and talloc_enable_leak_report_full().
682 * The talloc_set_name() function allocates memory as a child of the
683 * pointer. It is logically equivalent to:
684 * talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
686 * Note that multiple calls to talloc_set_name() will allocate more memory
687 * without releasing the name. All of the memory is released when the ptr is
688 * freed using talloc_free().
690 const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
693 * talloc_set_name_const - set a talloc pointer name to a string constant
694 * @ptr: the talloc pointer to name
695 * @name: the strucng constant.
697 * The function talloc_set_name_const() is just like talloc_set_name(), but it
698 * takes a string constant, and is much faster. It is extensively used by the
699 * "auto naming" macros, such as talloc().
701 * This function does not allocate any memory. It just copies the supplied
702 * pointer into the internal representation of the talloc ptr. This means you
703 * must not pass a name pointer to memory that will disappear before the ptr is
704 * freed with talloc_free().
706 void talloc_set_name_const(const void *ptr, const char *name);
709 * talloc_named - create a specifically-named talloc pointer
710 * @context: the parent context for the allocation
711 * @size: the size to allocate
712 * @fmt: the printf-style format for the name
714 * The talloc_named() function creates a named talloc pointer. It is equivalent
717 * ptr = talloc_size(context, size);
718 * talloc_set_name(ptr, fmt, ....);
720 void *talloc_named(const void *context, size_t size,
721 const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
724 * talloc_named_const - create a specifically-named talloc pointer
725 * @context: the parent context for the allocation
726 * @size: the size to allocate
727 * @name: the string constant to use as the name
729 * This is equivalent to:
731 * ptr = talloc_size(context, size);
732 * talloc_set_name_const(ptr, name);
734 void *talloc_named_const(const void *context, size_t size, const char *name);
737 * talloc_get_name - get the name of a talloc pointer
738 * @ptr: the talloc pointer
740 * This returns the current name for the given talloc pointer. See
741 * talloc_set_name() for details.
743 const char *talloc_get_name(const void *ptr);
746 * talloc_check_name - check if a pointer has the specified name
747 * @ptr: the talloc pointer
748 * @name: the name to compare with the pointer's name
750 * This function checks if a pointer has the specified name. If it does then
751 * the pointer is returned. It it doesn't then NULL is returned.
753 void *talloc_check_name(const void *ptr, const char *name);
756 * talloc_init - create a top-level context of particular name
757 * @fmt: the printf-style format of the name
759 * This function creates a zero length named talloc context as a top level
760 * context. It is equivalent to:
762 * talloc_named(NULL, 0, fmt, ...);
764 void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
767 * talloc_total_size - get the bytes used by the pointer and its children
768 * @ptr: the talloc pointer
770 * The talloc_total_size() function returns the total size in bytes used by
771 * this pointer and all child pointers. Mostly useful for debugging.
773 * Passing NULL is allowed, but it will only give a meaningful result if
774 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has been
777 size_t talloc_total_size(const void *ptr);
780 * talloc_total_blocks - get the number of allocations for the pointer
781 * @ptr: the talloc pointer
783 * The talloc_total_blocks() function returns the total allocations used by
784 * this pointer and all child pointers. Mostly useful for debugging. For
785 * example, a pointer with no children will return "1".
787 * Passing NULL is allowed, but it will only give a meaningful result if
788 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has been
791 size_t talloc_total_blocks(const void *ptr);
794 * talloc_report_depth_cb - walk the entire talloc tree under a talloc pointer
795 * @ptr: the talloc pointer to recurse under
796 * @depth: the current depth of traversal
797 * @max_depth: maximum depth to traverse, or -1 for no maximum
798 * @callback: the function to call on each pointer
799 * @private_data: pointer to hand to @callback.
801 * This provides a more flexible reports than talloc_report(). It will
802 * recursively call the callback for the entire tree of memory referenced by
803 * the pointer. References in the tree are passed with is_ref = 1 and the
804 * pointer that is referenced.
806 * You can pass NULL for the pointer, in which case a report is printed for the
807 * top level memory context, but only if talloc_enable_leak_report() or
808 * talloc_enable_leak_report_full() has been called.
810 * The recursion is stopped when depth >= max_depth. max_depth = -1 means only
811 * stop at leaf nodes.
813 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
814 void (*callback)(const void *ptr,
815 int depth, int max_depth,
821 * talloc_report_depth_file - report talloc usage to a maximum depth
822 * @ptr: the talloc pointer to recurse under
823 * @depth: the current depth of traversal
824 * @max_depth: maximum depth to traverse, or -1 for no maximum
825 * @f: the file to report to
827 * This provides a more flexible reports than talloc_report(). It will let you
828 * specify the depth and max_depth.
830 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
833 * talloc_enable_null_tracking - enable tracking of top-level tallocs
835 * This enables tracking of the NULL memory context without enabling leak
836 * reporting on exit. Useful for when you want to do your own leak reporting
837 * call via talloc_report_null_full();
839 void talloc_enable_null_tracking(void);
842 * talloc_disable_null_tracking - enable tracking of top-level tallocs
844 * This disables tracking of the NULL memory context.
846 void talloc_disable_null_tracking(void);
849 * talloc_enable_leak_report - call talloc_report on program exit
851 * This enables calling of talloc_report(NULL, stderr) when the program
852 * exits. In Samba4 this is enabled by using the --leak-report command line
855 * For it to be useful, this function must be called before any other talloc
856 * function as it establishes a "null context" that acts as the top of the
857 * tree. If you don't call this function first then passing NULL to
858 * talloc_report() or talloc_report_full() won't give you the full tree
861 * Here is a typical talloc report:
863 * talloc report on 'null_context' (total 267 bytes in 15 blocks)
864 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
865 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
866 * iconv(UTF8,CP850) contains 42 bytes in 2 blocks
867 * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
868 * iconv(CP850,UTF8) contains 42 bytes in 2 blocks
869 * iconv(UTF8,UTF-16LE) contains 45 bytes in 2 blocks
870 * iconv(UTF-16LE,UTF8) contains 45 bytes in 2 blocks
872 void talloc_enable_leak_report(void);
875 * talloc_enable_leak_report - call talloc_report_full on program exit
877 * This enables calling of talloc_report_full(NULL, stderr) when the program
878 * exits. In Samba4 this is enabled by using the --leak-report-full command
881 * For it to be useful, this function must be called before any other talloc
882 * function as it establishes a "null context" that acts as the top of the
883 * tree. If you don't call this function first then passing NULL to
884 * talloc_report() or talloc_report_full() won't give you the full tree
887 * Here is a typical full report:
889 * full talloc report on 'root' (total 18 bytes in 8 blocks)
890 * p1 contains 18 bytes in 7 blocks (ref 0)
891 * r1 contains 13 bytes in 2 blocks (ref 0)
893 * p2 contains 1 bytes in 1 blocks (ref 1)
894 * x3 contains 1 bytes in 1 blocks (ref 0)
895 * x2 contains 1 bytes in 1 blocks (ref 0)
896 * x1 contains 1 bytes in 1 blocks (ref 0)
898 void talloc_enable_leak_report_full(void);
901 * talloc_autofree_context - a context which will be freed at exit
903 * This is a handy utility function that returns a talloc context which will be
904 * automatically freed on program exit. This can be used to reduce the noise in
905 * memory leak reports.
907 void *talloc_autofree_context(void);
910 * talloc_get_size - get the size of an allocation
911 * @ctx: the talloc pointer whose allocation to measure.
913 * This function lets you know the amount of memory alloced so far by this
914 * context. It does NOT account for subcontext memory. This can be used to
915 * calculate the size of an array.
917 size_t talloc_get_size(const void *ctx);
920 * talloc_find_parent_byname - find a parent of this context with this name
921 * @ctx: the context whose ancestors to search
922 * @name: the name to look for
924 * Find a parent memory context of @ctx that has the given name. This can be
925 * very useful in complex programs where it may be difficult to pass all
926 * information down to the level you need, but you know the structure you want
927 * is a parent of another context.
929 void *talloc_find_parent_byname(const void *ctx, const char *name);
932 * talloc_add_external - create an externally allocated node
934 * @realloc: the realloc() equivalent
935 * @lock: the call to lock before manipulation of external nodes
936 * @unlock: the call to unlock after manipulation of external nodes
938 * talloc_add_external() creates a node which uses a separate allocator. All
939 * children allocated from that node will also use that allocator.
941 * Note: Currently there is only one external allocator, not per-node,
942 * and it is set with this function.
944 * @lock is handed a pointer which was previous returned from your realloc
945 * function; you should use that to figure out which lock to get if you have
946 * multiple external pools.
948 * The parent pointers in realloc is the talloc pointer of the parent, if any.
950 void *talloc_add_external(const void *ctx,
951 void *(*realloc)(const void *parent,
953 void (*lock)(const void *p),
954 void (*unlock)(void));
956 /* The following definitions come from talloc.c */
957 void *_talloc(const void *context, size_t size);
958 void _talloc_set(void *ptr, const void *ctx, size_t size, const char *name);
959 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *));
960 size_t talloc_reference_count(const void *ptr);
961 void *_talloc_reference(const void *context, const void *ptr);
963 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
964 void *talloc_parent(const void *ptr);
965 const char *talloc_parent_name(const void *ptr);
966 void *_talloc_steal(const void *new_ctx, const void *ptr);
967 void *_talloc_move(const void *new_ctx, const void *pptr);
968 void *_talloc_zero(const void *ctx, size_t size, const char *name);
969 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
970 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
971 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name);
972 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
973 void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
974 void talloc_show_parents(const void *context, FILE *file);
975 int talloc_is_parent(const void *context, const void *ptr);
977 #endif /* CCAN_TALLOC_H */