2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
5 <refentrytitle>talloc</refentrytitle>
6 <manvolnum>3</manvolnum>
9 <refname>talloc</refname>
10 <refpurpose>hierarchical reference counted memory pool system with destructors</refpurpose>
13 <synopsis>#include <talloc/talloc.h></synopsis>
15 <refsect1><title>DESCRIPTION</title>
17 If you are used to talloc from Samba3 then please read this
18 carefully, as talloc has changed a lot.
21 The new talloc is a hierarchical, reference counted memory pool
22 system with destructors. Quite a mouthful really, but not too bad
23 once you get used to it.
26 Perhaps the biggest change from Samba3 is that there is no
27 distinction between a "talloc context" and a "talloc pointer". Any
28 pointer returned from talloc() is itself a valid talloc context.
29 This means you can do this:
32 struct foo *X = talloc(mem_ctx, struct foo);
33 X->name = talloc_strdup(X, "foo");
36 and the pointer <literal role="code">X->name</literal>
37 would be a "child" of the talloc context <literal
38 role="code">X</literal> which is itself a child of
39 <literal role="code">mem_ctx</literal>. So if you do
40 <literal role="code">talloc_free(mem_ctx)</literal> then
41 it is all destroyed, whereas if you do <literal
42 role="code">talloc_free(X)</literal> then just <literal
43 role="code">X</literal> and <literal
44 role="code">X->name</literal> are destroyed, and if
46 role="code">talloc_free(X->name)</literal> then just
47 the name element of <literal role="code">X</literal> is
51 If you think about this, then what this effectively gives you is an
52 n-ary tree, where you can free any part of the tree with
56 If you find this confusing, then I suggest you run the <literal
57 role="code">testsuite</literal> program to watch talloc
58 in action. You may also like to add your own tests to <literal
59 role="code">testsuite.c</literal> to clarify how some
60 particular situation is handled.
63 <refsect1><title>TALLOC API</title>
65 The following is a complete guide to the talloc API. Read it all at
68 <refsect2><title>(type *)talloc(const void *ctx, type);</title>
70 The talloc() macro is the core of the talloc library. It takes a
71 memory <emphasis role="italic">ctx</emphasis> and a <emphasis
72 role="italic">type</emphasis>, and returns a pointer to a new
73 area of memory of the given <emphasis
74 role="italic">type</emphasis>.
77 The returned pointer is itself a talloc context, so you can use
78 it as the <emphasis role="italic">ctx</emphasis> argument to more
79 calls to talloc() if you wish.
82 The returned pointer is a "child" of the supplied context. This
83 means that if you talloc_free() the <emphasis
84 role="italic">ctx</emphasis> then the new child disappears as
85 well. Alternatively you can free just the child.
88 The <emphasis role="italic">ctx</emphasis> argument to talloc()
89 can be NULL, in which case a new top level context is created.
92 <refsect2><title>void *talloc_size(const void *ctx, size_t size);</title>
94 The function talloc_size() should be used when you don't have a
95 convenient type to pass to talloc(). Unlike talloc(), it is not
96 type safe (as it returns a void *), so you are on your own for
100 <refsect2><title>(typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);</title>
102 The talloc_ptrtype() macro should be used when you have a pointer and
103 want to allocate memory to point at with this pointer. When compiling
104 with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()
105 and talloc_get_name() will return the current location in the source file.
109 <refsect2><title>int talloc_free(void *ptr);</title>
111 The talloc_free() function frees a piece of talloc memory, and
112 all its children. You can call talloc_free() on any pointer
113 returned by talloc().
116 The return value of talloc_free() indicates success or failure,
117 with 0 returned for success and -1 for failure. The only
118 possible failure condition is if <emphasis
119 role="italic">ptr</emphasis> had a destructor attached to it and
120 the destructor returned -1. See <link
121 linkend="talloc_set_destructor"><quote>talloc_set_destructor()</quote></link>
122 for details on destructors.
125 If this pointer has an additional parent when talloc_free() is
126 called then the memory is not actually released, but instead the
127 most recently established parent is destroyed. See <link
128 linkend="talloc_reference"><quote>talloc_reference()</quote></link>
129 for details on establishing additional parents.
132 For more control on which parent is removed, see <link
133 linkend="talloc_unlink"><quote>talloc_unlink()</quote></link>.
136 talloc_free() operates recursively on its children.
139 <refsect2 id="talloc_reference"><title>void *talloc_reference(const void *ctx, const void *ptr);</title>
141 The talloc_reference() function makes <emphasis
142 role="italic">ctx</emphasis> an additional parent of <emphasis
143 role="italic">ptr</emphasis>.
146 The return value of talloc_reference() is always the original
147 pointer <emphasis role="italic">ptr</emphasis>, unless talloc ran
148 out of memory in creating the reference in which case it will
149 return NULL (each additional reference consumes around 48 bytes
150 of memory on intel x86 platforms).
153 If <emphasis role="italic">ptr</emphasis> is NULL, then the
154 function is a no-op, and simply returns NULL.
157 After creating a reference you can free it in one of the
164 you can talloc_free() any parent of the original pointer.
165 That will reduce the number of parents of this pointer by 1,
166 and will cause this pointer to be freed if it runs out of
172 you can talloc_free() the pointer itself. That will destroy
173 the most recently established parent to the pointer and leave
174 the pointer as a child of its current parent.
180 For more control on which parent to remove, see <link
181 linkend="talloc_unlink"><quote>talloc_unlink()</quote></link>.
184 <refsect2 id="talloc_unlink"><title>int talloc_unlink(const void *ctx, const void *ptr);</title>
186 The talloc_unlink() function removes a specific parent from
187 <emphasis role="italic">ptr</emphasis>. The <emphasis
188 role="italic">ctx</emphasis> passed must either be a context used
189 in talloc_reference() with this pointer, or must be a direct
193 Note that if the parent has already been removed using
194 talloc_free() then this function will fail and will return -1.
195 Likewise, if <emphasis role="italic">ptr</emphasis> is NULL, then
196 the function will make no modifications and return -1.
199 Usually you can just use talloc_free() instead of
200 talloc_unlink(), but sometimes it is useful to have the
201 additional control on which parent is removed.
204 <refsect2 id="talloc_set_destructor"><title>void talloc_set_destructor(const void *ptr, int (*destructor)(void *));</title>
206 The function talloc_set_destructor() sets the <emphasis
207 role="italic">destructor</emphasis> for the pointer <emphasis
208 role="italic">ptr</emphasis>. A <emphasis
209 role="italic">destructor</emphasis> is a function that is called
210 when the memory used by a pointer is about to be released. The
211 destructor receives <emphasis role="italic">ptr</emphasis> as an
212 argument, and should return 0 for success and -1 for failure.
215 The <emphasis role="italic">destructor</emphasis> can do anything
216 it wants to, including freeing other pieces of memory. A common
217 use for destructors is to clean up operating system resources
218 (such as open file descriptors) contained in the structure the
219 destructor is placed on.
222 You can only place one destructor on a pointer. If you need more
223 than one destructor then you can create a zero-length child of
224 the pointer and place an additional destructor on that.
227 To remove a destructor call talloc_set_destructor() with NULL for
231 If your destructor attempts to talloc_free() the pointer that it
232 is the destructor for then talloc_free() will return -1 and the
233 free will be ignored. This would be a pointless operation
234 anyway, as the destructor is only called when the memory is just
238 <refsect2><title>void talloc_increase_ref_count(const void *<emphasis role="italic">ptr</emphasis>);</title>
240 The talloc_increase_ref_count(<emphasis
241 role="italic">ptr</emphasis>) function is exactly equivalent to:
243 <programlisting>talloc_reference(NULL, ptr);</programlisting>
245 You can use either syntax, depending on which you think is
246 clearer in your code.
249 <refsect2 id="talloc_set_name"><title>void talloc_set_name(const void *ptr, const char *fmt, ...);</title>
251 Each talloc pointer has a "name". The name is used principally
252 for debugging purposes, although it is also possible to set and
253 get the name on a pointer in as a way of "marking" pointers in
257 The main use for names on pointer is for "talloc reports". See
259 linkend="talloc_report"><quote>talloc_report()</quote></link>
261 linkend="talloc_report_full"><quote>talloc_report_full()</quote></link>
262 for details. Also see <link
263 linkend="talloc_enable_leak_report"><quote>talloc_enable_leak_report()</quote></link>
265 linkend="talloc_enable_leak_report_full"><quote>talloc_enable_leak_report_full()</quote></link>.
268 The talloc_set_name() function allocates memory as a child of the
269 pointer. It is logically equivalent to:
271 <programlisting>talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));</programlisting>
273 Note that multiple calls to talloc_set_name() will allocate more
274 memory without releasing the name. All of the memory is released
275 when the ptr is freed using talloc_free().
278 <refsect2><title>void talloc_set_name_const(const void *<emphasis role="italic">ptr</emphasis>, const char *<emphasis role="italic">name</emphasis>);</title>
280 The function talloc_set_name_const() is just like
281 talloc_set_name(), but it takes a string constant, and is much
282 faster. It is extensively used by the "auto naming" macros, such
286 This function does not allocate any memory. It just copies the
287 supplied pointer into the internal representation of the talloc
288 ptr. This means you must not pass a <emphasis
289 role="italic">name</emphasis> pointer to memory that will
290 disappear before <emphasis role="italic">ptr</emphasis> is freed
294 <refsect2><title>void *talloc_named(const void *<emphasis role="italic">ctx</emphasis>, size_t <emphasis role="italic">size</emphasis>, const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
296 The talloc_named() function creates a named talloc pointer. It
299 <programlisting>ptr = talloc_size(ctx, size);
300 talloc_set_name(ptr, fmt, ....);</programlisting>
302 <refsect2><title>void *talloc_named_const(const void *<emphasis role="italic">ctx</emphasis>, size_t <emphasis role="italic">size</emphasis>, const char *<emphasis role="italic">name</emphasis>);</title>
304 This is equivalent to:
306 <programlisting>ptr = talloc_size(ctx, size);
307 talloc_set_name_const(ptr, name);</programlisting>
309 <refsect2><title>const char *talloc_get_name(const void *<emphasis role="italic">ptr</emphasis>);</title>
311 This returns the current name for the given talloc pointer,
312 <emphasis role="italic">ptr</emphasis>. See <link
313 linkend="talloc_set_name"><quote>talloc_set_name()</quote></link>
317 <refsect2><title>void *talloc_init(const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
319 This function creates a zero length named talloc context as a top
320 level context. It is equivalent to:
322 <programlisting>talloc_named(NULL, 0, fmt, ...);</programlisting>
324 <refsect2><title>void *talloc_new(void *<emphasis role="italic">ctx</emphasis>);</title>
326 This is a utility macro that creates a new memory context hanging
327 off an exiting context, automatically naming it "talloc_new:
328 __location__" where __location__ is the source line it is called
329 from. It is particularly useful for creating a new temporary
333 <refsect2><title>(<emphasis role="italic">type</emphasis> *)talloc_realloc(const void *<emphasis role="italic">ctx</emphasis>, void *<emphasis role="italic">ptr</emphasis>, <emphasis role="italic">type</emphasis>, <emphasis role="italic">count</emphasis>);</title>
335 The talloc_realloc() macro changes the size of a talloc pointer.
336 It has the following equivalences:
338 <programlisting>talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
339 talloc_realloc(ctx, ptr, type, 0) ==> talloc_free(ptr);</programlisting>
341 The <emphasis role="italic">ctx</emphasis> argument is only used
342 if <emphasis role="italic">ptr</emphasis> is not NULL, otherwise
346 talloc_realloc() returns the new pointer, or NULL on failure.
347 The call will fail either due to a lack of memory, or because the
348 pointer has more than one parent (see <link
349 linkend="talloc_reference"><quote>talloc_reference()</quote></link>).
352 <refsect2><title>void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);</title>
354 the talloc_realloc_size() function is useful when the type is not
355 known so the type-safe talloc_realloc() cannot be used.
358 <refsect2><title>void *talloc_steal(const void *<emphasis role="italic">new_ctx</emphasis>, const void *<emphasis role="italic">ptr</emphasis>);</title>
360 The talloc_steal() function changes the parent context of a
361 talloc pointer. It is typically used when the context that the
362 pointer is currently a child of is going to be freed and you wish
363 to keep the memory for a longer time.
366 The talloc_steal() function returns the pointer that you pass it.
367 It does not have any failure modes.
370 NOTE: It is possible to produce loops in the parent/child
371 relationship if you are not careful with talloc_steal(). No
372 guarantees are provided as to your sanity or the safety of your
376 <refsect2><title>size_t talloc_total_size(const void *<emphasis role="italic">ptr</emphasis>);</title>
378 The talloc_total_size() function returns the total size in bytes
379 used by this pointer and all child pointers. Mostly useful for
383 Passing NULL is allowed, but it will only give a meaningful
384 result if talloc_enable_leak_report() or
385 talloc_enable_leak_report_full() has been called.
388 <refsect2><title>size_t talloc_total_blocks(const void *<emphasis role="italic">ptr</emphasis>);</title>
390 The talloc_total_blocks() function returns the total memory block
391 count used by this pointer and all child pointers. Mostly useful
395 Passing NULL is allowed, but it will only give a meaningful
396 result if talloc_enable_leak_report() or
397 talloc_enable_leak_report_full() has been called.
400 <refsect2 id="talloc_report"><title>void talloc_report(const void *ptr, FILE *f);</title>
402 The talloc_report() function prints a summary report of all
403 memory used by <emphasis role="italic">ptr</emphasis>. One line
404 of report is printed for each immediate child of ptr, showing the
405 total memory and number of blocks used by that child.
408 You can pass NULL for the pointer, in which case a report is
409 printed for the top level memory context, but only if
410 talloc_enable_leak_report() or talloc_enable_leak_report_full()
414 <refsect2 id="talloc_report_full"><title>void talloc_report_full(const void *<emphasis role="italic">ptr</emphasis>, FILE *<emphasis role="italic">f</emphasis>);</title>
416 This provides a more detailed report than talloc_report(). It
417 will recursively print the entire tree of memory referenced by
418 the pointer. References in the tree are shown by giving the name
419 of the pointer that is referenced.
422 You can pass NULL for the pointer, in which case a report is
423 printed for the top level memory context, but only if
424 talloc_enable_leak_report() or talloc_enable_leak_report_full()
428 <refsect2 id="talloc_enable_leak_report"><title>void talloc_enable_leak_report(void);</title>
430 This enables calling of talloc_report(NULL, stderr) when the
431 program exits. In Samba4 this is enabled by using the
432 --leak-report command line option.
435 For it to be useful, this function must be called before any
436 other talloc function as it establishes a "null context" that
437 acts as the top of the tree. If you don't call this function
438 first then passing NULL to talloc_report() or
439 talloc_report_full() won't give you the full tree printout.
442 Here is a typical talloc report:
444 <screen format="linespecific">talloc report on 'null_context' (total 267 bytes in 15 blocks)
445 libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
446 libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
447 iconv(UTF8,CP850) contains 42 bytes in 2 blocks
448 libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
449 iconv(CP850,UTF8) contains 42 bytes in 2 blocks
450 iconv(UTF8,UTF-16LE) contains 45 bytes in 2 blocks
451 iconv(UTF-16LE,UTF8) contains 45 bytes in 2 blocks
454 <refsect2 id="talloc_enable_leak_report_full"><title>void talloc_enable_leak_report_full(void);</title>
456 This enables calling of talloc_report_full(NULL, stderr) when the
457 program exits. In Samba4 this is enabled by using the
458 --leak-report-full command line option.
461 For it to be useful, this function must be called before any
462 other talloc function as it establishes a "null context" that
463 acts as the top of the tree. If you don't call this function
464 first then passing NULL to talloc_report() or
465 talloc_report_full() won't give you the full tree printout.
468 Here is a typical full report:
470 <screen format="linespecific">full talloc report on 'root' (total 18 bytes in 8 blocks)
471 p1 contains 18 bytes in 7 blocks (ref 0)
472 r1 contains 13 bytes in 2 blocks (ref 0)
474 p2 contains 1 bytes in 1 blocks (ref 1)
475 x3 contains 1 bytes in 1 blocks (ref 0)
476 x2 contains 1 bytes in 1 blocks (ref 0)
477 x1 contains 1 bytes in 1 blocks (ref 0)
480 <refsect2><title>(<emphasis role="italic">type</emphasis> *)talloc_zero(const void *<emphasis role="italic">ctx</emphasis>, <emphasis role="italic">type</emphasis>);</title>
482 The talloc_zero() macro is equivalent to:
484 <programlisting>ptr = talloc(ctx, type);
485 if (ptr) memset(ptr, 0, sizeof(type));</programlisting>
487 <refsect2><title>void *talloc_zero_size(const void *<emphasis role="italic">ctx</emphasis>, size_t <emphasis role="italic">size</emphasis>)</title>
489 The talloc_zero_size() function is useful when you don't have a
493 <refsect2><title>void *talloc_memdup(const void *<emphasis role="italic">ctx</emphasis>, const void *<emphasis role="italic">p</emphasis>, size_t size);</title>
495 The talloc_memdup() function is equivalent to:
497 <programlisting>ptr = talloc_size(ctx, size);
498 if (ptr) memcpy(ptr, p, size);</programlisting>
500 <refsect2><title>char *talloc_strdup(const void *<emphasis role="italic">ctx</emphasis>, const char *<emphasis role="italic">p</emphasis>);</title>
502 The talloc_strdup() function is equivalent to:
504 <programlisting>ptr = talloc_size(ctx, strlen(p)+1);
505 if (ptr) memcpy(ptr, p, strlen(p)+1);</programlisting>
507 This function sets the name of the new pointer to the passed
508 string. This is equivalent to:
510 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
512 <refsect2><title>char *talloc_strndup(const void *<emphasis role="italic">t</emphasis>, const char *<emphasis role="italic">p</emphasis>, size_t <emphasis role="italic">n</emphasis>);</title>
514 The talloc_strndup() function is the talloc equivalent of the C
515 library function strndup(3).
518 This function sets the name of the new pointer to the passed
519 string. This is equivalent to:
521 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
523 <refsect2><title>char *talloc_vasprintf(const void *<emphasis role="italic">t</emphasis>, const char *<emphasis role="italic">fmt</emphasis>, va_list <emphasis role="italic">ap</emphasis>);</title>
525 The talloc_vasprintf() function is the talloc equivalent of the C
526 library function vasprintf(3).
529 <refsect2><title>char *talloc_asprintf(const void *<emphasis role="italic">t</emphasis>, const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
531 The talloc_asprintf() function is the talloc equivalent of the C
532 library function asprintf(3).
535 This function sets the name of the new pointer to the passed
536 string. This is equivalent to:
538 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
540 <refsect2><title>char *talloc_asprintf_append(char *s, const char *fmt, ...);</title>
542 The talloc_asprintf_append() function appends the given formatted
543 string to the given string.
546 <refsect2><title>(type *)talloc_array(const void *ctx, type, uint_t count);</title>
548 The talloc_array() macro is equivalent to:
550 <programlisting>(type *)talloc_size(ctx, sizeof(type) * count);</programlisting>
552 except that it provides integer overflow protection for the
553 multiply, returning NULL if the multiply overflows.
556 <refsect2><title>void *talloc_array_size(const void *ctx, size_t size, uint_t count);</title>
558 The talloc_array_size() function is useful when the type is not
559 known. It operates in the same way as talloc_array(), but takes a
560 size instead of a type.
563 <refsect2><title>(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, uint_t count);</title>
565 The talloc_ptrtype() macro should be used when you have a pointer to an array
566 and want to allocate memory of an array to point at with this pointer. When compiling
567 with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
568 and talloc_get_name() will return the current location in the source file.
572 <refsect2><title>void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size)</title>
574 This is a non-macro version of talloc_realloc(), which is useful
575 as libraries sometimes want a realloc function pointer. A
576 realloc(3) implementation encapsulates the functionality of
577 malloc(3), free(3) and realloc(3) in one call, which is why it is
578 useful to be able to pass around a single function pointer.
581 <refsect2><title>void *talloc_autofree_context(void);</title>
583 This is a handy utility function that returns a talloc context
584 which will be automatically freed on program exit. This can be
585 used to reduce the noise in memory leak reports.
588 <refsect2><title>void *talloc_check_name(const void *ptr, const char *name);</title>
590 This function checks if a pointer has the specified <emphasis
591 role="italic">name</emphasis>. If it does then the pointer is
592 returned. It it doesn't then NULL is returned.
595 <refsect2><title>(type *)talloc_get_type(const void *ptr, type);</title>
597 This macro allows you to do type checking on talloc pointers. It
598 is particularly useful for void* private pointers. It is
601 <programlisting>(type *)talloc_check_name(ptr, #type)</programlisting>
603 <refsect2><title>talloc_set_type(const void *ptr, type);</title>
605 This macro allows you to force the name of a pointer to be a
606 particular <emphasis>type</emphasis>. This can be
607 used in conjunction with talloc_get_type() to do type checking on
611 It is equivalent to this:
613 <programlisting>talloc_set_name_const(ptr, #type)</programlisting>
616 <refsect1><title>PERFORMANCE</title>
618 All the additional features of talloc(3) over malloc(3) do come at a
619 price. We have a simple performance test in Samba4 that measures
620 talloc() versus malloc() performance, and it seems that talloc() is
621 about 10% slower than malloc() on my x86 Debian Linux box. For
622 Samba, the great reduction in code complexity that we get by using
623 talloc makes this worthwhile, especially as the total overhead of
624 talloc/malloc in Samba is already quite small.
627 <refsect1><title>SEE ALSO</title>
629 malloc(3), strndup(3), vasprintf(3), asprintf(3),
630 <ulink url="http://talloc.samba.org/"/>
633 <refsect1><title>COPYRIGHT/LICENSE</title>
635 Copyright (C) Andrew Tridgell 2004
638 This program is free software; you can redistribute it and/or modify
639 it under the terms of the GNU General Public License as published by
640 the Free Software Foundation; either version 2 of the License, or (at
641 your option) any later version.
644 This program is distributed in the hope that it will be useful, but
645 WITHOUT ANY WARRANTY; without even the implied warranty of
646 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
647 General Public License for more details.
650 You should have received a copy of the GNU General Public License
651 along with this program; if not, write to the Free Software
652 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.