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>int talloc_free(void *ptr);</title>
102 The talloc_free() function frees a piece of talloc memory, and
103 all its children. You can call talloc_free() on any pointer
104 returned by talloc().
107 The return value of talloc_free() indicates success or failure,
108 with 0 returned for success and -1 for failure. The only
109 possible failure condition is if <emphasis
110 role="italic">ptr</emphasis> had a destructor attached to it and
111 the destructor returned -1. See <link
112 linkend="talloc_set_destructor"><quote>talloc_set_destructor()</quote></link>
113 for details on destructors.
116 If this pointer has an additional parent when talloc_free() is
117 called then the memory is not actually released, but instead the
118 most recently established parent is destroyed. See <link
119 linkend="talloc_reference"><quote>talloc_reference()</quote></link>
120 for details on establishing additional parents.
123 For more control on which parent is removed, see <link
124 linkend="talloc_unlink"><quote>talloc_unlink()</quote></link>.
127 talloc_free() operates recursively on its children.
130 <refsect2 id="talloc_reference"><title>void *talloc_reference(const void *ctx, const void *ptr);</title>
132 The talloc_reference() function makes <emphasis
133 role="italic">ctx</emphasis> an additional parent of <emphasis
134 role="italic">ptr</emphasis>.
137 The return value of talloc_reference() is always the original
138 pointer <emphasis role="italic">ptr</emphasis>, unless talloc ran
139 out of memory in creating the reference in which case it will
140 return NULL (each additional reference consumes around 48 bytes
141 of memory on intel x86 platforms).
144 If <emphasis role="italic">ptr</emphasis> is NULL, then the
145 function is a no-op, and simply returns NULL.
148 After creating a reference you can free it in one of the
155 you can talloc_free() any parent of the original pointer.
156 That will reduce the number of parents of this pointer by 1,
157 and will cause this pointer to be freed if it runs out of
163 you can talloc_free() the pointer itself. That will destroy
164 the most recently established parent to the pointer and leave
165 the pointer as a child of its current parent.
171 For more control on which parent to remove, see <link
172 linkend="talloc_unlink"><quote>talloc_unlink()</quote></link>.
175 <refsect2 id="talloc_unlink"><title>int talloc_unlink(const void *ctx, const void *ptr);</title>
177 The talloc_unlink() function removes a specific parent from
178 <emphasis role="italic">ptr</emphasis>. The <emphasis
179 role="italic">ctx</emphasis> passed must either be a context used
180 in talloc_reference() with this pointer, or must be a direct
184 Note that if the parent has already been removed using
185 talloc_free() then this function will fail and will return -1.
186 Likewise, if <emphasis role="italic">ptr</emphasis> is NULL, then
187 the function will make no modifications and return -1.
190 Usually you can just use talloc_free() instead of
191 talloc_unlink(), but sometimes it is useful to have the
192 additional control on which parent is removed.
195 <refsect2 id="talloc_set_destructor"><title>void talloc_set_destructor(const void *ptr, int (*destructor)(void *));</title>
197 The function talloc_set_destructor() sets the <emphasis
198 role="italic">destructor</emphasis> for the pointer <emphasis
199 role="italic">ptr</emphasis>. A <emphasis
200 role="italic">destructor</emphasis> is a function that is called
201 when the memory used by a pointer is about to be released. The
202 destructor receives <emphasis role="italic">ptr</emphasis> as an
203 argument, and should return 0 for success and -1 for failure.
206 The <emphasis role="italic">destructor</emphasis> can do anything
207 it wants to, including freeing other pieces of memory. A common
208 use for destructors is to clean up operating system resources
209 (such as open file descriptors) contained in the structure the
210 destructor is placed on.
213 You can only place one destructor on a pointer. If you need more
214 than one destructor then you can create a zero-length child of
215 the pointer and place an additional destructor on that.
218 To remove a destructor call talloc_set_destructor() with NULL for
222 If your destructor attempts to talloc_free() the pointer that it
223 is the destructor for then talloc_free() will return -1 and the
224 free will be ignored. This would be a pointless operation
225 anyway, as the destructor is only called when the memory is just
229 <refsect2><title>void talloc_increase_ref_count(const void *<emphasis role="italic">ptr</emphasis>);</title>
231 The talloc_increase_ref_count(<emphasis
232 role="italic">ptr</emphasis>) function is exactly equivalent to:
234 <programlisting>talloc_reference(NULL, ptr);</programlisting>
236 You can use either syntax, depending on which you think is
237 clearer in your code.
240 <refsect2 id="talloc_set_name"><title>void talloc_set_name(const void *ptr, const char *fmt, ...);</title>
242 Each talloc pointer has a "name". The name is used principally
243 for debugging purposes, although it is also possible to set and
244 get the name on a pointer in as a way of "marking" pointers in
248 The main use for names on pointer is for "talloc reports". See
250 linkend="talloc_report"><quote>talloc_report()</quote></link>
252 linkend="talloc_report_full"><quote>talloc_report_full()</quote></link>
253 for details. Also see <link
254 linkend="talloc_enable_leak_report"><quote>talloc_enable_leak_report()</quote></link>
256 linkend="talloc_enable_leak_report_full"><quote>talloc_enable_leak_report_full()</quote></link>.
259 The talloc_set_name() function allocates memory as a child of the
260 pointer. It is logically equivalent to:
262 <programlisting>talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));</programlisting>
264 Note that multiple calls to talloc_set_name() will allocate more
265 memory without releasing the name. All of the memory is released
266 when the ptr is freed using talloc_free().
269 <refsect2><title>void talloc_set_name_const(const void *<emphasis role="italic">ptr</emphasis>, const char *<emphasis role="italic">name</emphasis>);</title>
271 The function talloc_set_name_const() is just like
272 talloc_set_name(), but it takes a string constant, and is much
273 faster. It is extensively used by the "auto naming" macros, such
277 This function does not allocate any memory. It just copies the
278 supplied pointer into the internal representation of the talloc
279 ptr. This means you must not pass a <emphasis
280 role="italic">name</emphasis> pointer to memory that will
281 disappear before <emphasis role="italic">ptr</emphasis> is freed
285 <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>
287 The talloc_named() function creates a named talloc pointer. It
290 <programlisting>ptr = talloc_size(ctx, size);
291 talloc_set_name(ptr, fmt, ....);</programlisting>
293 <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>
295 This is equivalent to:
297 <programlisting>ptr = talloc_size(ctx, size);
298 talloc_set_name_const(ptr, name);</programlisting>
300 <refsect2><title>const char *talloc_get_name(const void *<emphasis role="italic">ptr</emphasis>);</title>
302 This returns the current name for the given talloc pointer,
303 <emphasis role="italic">ptr</emphasis>. See <link
304 linkend="talloc_set_name"><quote>talloc_set_name()</quote></link>
308 <refsect2><title>void *talloc_init(const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
310 This function creates a zero length named talloc context as a top
311 level context. It is equivalent to:
313 <programlisting>talloc_named(NULL, 0, fmt, ...);</programlisting>
315 <refsect2><title>void *talloc_new(void *<emphasis role="italic">ctx</emphasis>);</title>
317 This is a utility macro that creates a new memory context hanging
318 off an exiting context, automatically naming it "talloc_new:
319 __location__" where __location__ is the source line it is called
320 from. It is particularly useful for creating a new temporary
324 <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>
326 The talloc_realloc() macro changes the size of a talloc pointer.
327 It has the following equivalences:
329 <programlisting>talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
330 talloc_realloc(ctx, ptr, type, 0) ==> talloc_free(ptr);</programlisting>
332 The <emphasis role="italic">ctx</emphasis> argument is only used
333 if <emphasis role="italic">ptr</emphasis> is not NULL, otherwise
337 talloc_realloc() returns the new pointer, or NULL on failure.
338 The call will fail either due to a lack of memory, or because the
339 pointer has more than one parent (see <link
340 linkend="talloc_reference"><quote>talloc_reference()</quote></link>).
343 <refsect2><title>void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);</title>
345 the talloc_realloc_size() function is useful when the type is not
346 known so the type-safe talloc_realloc() cannot be used.
349 <refsect2><title>void *talloc_steal(const void *<emphasis role="italic">new_ctx</emphasis>, const void *<emphasis role="italic">ptr</emphasis>);</title>
351 The talloc_steal() function changes the parent context of a
352 talloc pointer. It is typically used when the context that the
353 pointer is currently a child of is going to be freed and you wish
354 to keep the memory for a longer time.
357 The talloc_steal() function returns the pointer that you pass it.
358 It does not have any failure modes.
361 NOTE: It is possible to produce loops in the parent/child
362 relationship if you are not careful with talloc_steal(). No
363 guarantees are provided as to your sanity or the safety of your
367 <refsect2><title>off_t talloc_total_size(const void *<emphasis role="italic">ptr</emphasis>);</title>
369 The talloc_total_size() function returns the total size in bytes
370 used by this pointer and all child pointers. Mostly useful for
374 Passing NULL is allowed, but it will only give a meaningful
375 result if talloc_enable_leak_report() or
376 talloc_enable_leak_report_full() has been called.
379 <refsect2><title>off_t talloc_total_blocks(const void *<emphasis role="italic">ptr</emphasis>);</title>
381 The talloc_total_blocks() function returns the total memory block
382 count used by this pointer and all child pointers. Mostly useful
386 Passing NULL is allowed, but it will only give a meaningful
387 result if talloc_enable_leak_report() or
388 talloc_enable_leak_report_full() has been called.
391 <refsect2 id="talloc_report"><title>void talloc_report(const void *ptr, FILE *f);</title>
393 The talloc_report() function prints a summary report of all
394 memory used by <emphasis role="italic">ptr</emphasis>. One line
395 of report is printed for each immediate child of ptr, showing the
396 total memory and number of blocks used by that child.
399 You can pass NULL for the pointer, in which case a report is
400 printed for the top level memory context, but only if
401 talloc_enable_leak_report() or talloc_enable_leak_report_full()
405 <refsect2 id="talloc_report_full"><title>void talloc_report_full(const void *<emphasis role="italic">ptr</emphasis>, FILE *<emphasis role="italic">f</emphasis>);</title>
407 This provides a more detailed report than talloc_report(). It
408 will recursively print the entire tree of memory referenced by
409 the pointer. References in the tree are shown by giving the name
410 of the pointer that is referenced.
413 You can pass NULL for the pointer, in which case a report is
414 printed for the top level memory context, but only if
415 talloc_enable_leak_report() or talloc_enable_leak_report_full()
419 <refsect2 id="talloc_enable_leak_report"><title>void talloc_enable_leak_report(void);</title>
421 This enables calling of talloc_report(NULL, stderr) when the
422 program exits. In Samba4 this is enabled by using the
423 --leak-report command line option.
426 For it to be useful, this function must be called before any
427 other talloc function as it establishes a "null context" that
428 acts as the top of the tree. If you don't call this function
429 first then passing NULL to talloc_report() or
430 talloc_report_full() won't give you the full tree printout.
433 Here is a typical talloc report:
435 <screen format="linespecific">talloc report on 'null_context' (total 267 bytes in 15 blocks)
436 libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
437 libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
438 iconv(UTF8,CP850) contains 42 bytes in 2 blocks
439 libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
440 iconv(CP850,UTF8) contains 42 bytes in 2 blocks
441 iconv(UTF8,UTF-16LE) contains 45 bytes in 2 blocks
442 iconv(UTF-16LE,UTF8) contains 45 bytes in 2 blocks
445 <refsect2 id="talloc_enable_leak_report_full"><title>void talloc_enable_leak_report_full(void);</title>
447 This enables calling of talloc_report_full(NULL, stderr) when the
448 program exits. In Samba4 this is enabled by using the
449 --leak-report-full command line option.
452 For it to be useful, this function must be called before any
453 other talloc function as it establishes a "null context" that
454 acts as the top of the tree. If you don't call this function
455 first then passing NULL to talloc_report() or
456 talloc_report_full() won't give you the full tree printout.
459 Here is a typical full report:
461 <screen format="linespecific">full talloc report on 'root' (total 18 bytes in 8 blocks)
462 p1 contains 18 bytes in 7 blocks (ref 0)
463 r1 contains 13 bytes in 2 blocks (ref 0)
465 p2 contains 1 bytes in 1 blocks (ref 1)
466 x3 contains 1 bytes in 1 blocks (ref 0)
467 x2 contains 1 bytes in 1 blocks (ref 0)
468 x1 contains 1 bytes in 1 blocks (ref 0)
471 <refsect2><title>(<emphasis role="italic">type</emphasis> *)talloc_zero(const void *<emphasis role="italic">ctx</emphasis>, <emphasis role="italic">type</emphasis>);</title>
473 The talloc_zero() macro is equivalent to:
475 <programlisting>ptr = talloc(ctx, type);
476 if (ptr) memset(ptr, 0, sizeof(type));</programlisting>
478 <refsect2><title>void *talloc_zero_size(const void *<emphasis role="italic">ctx</emphasis>, size_t <emphasis role="italic">size</emphasis>)</title>
480 The talloc_zero_size() function is useful when you don't have a
484 <refsect2><title>void *talloc_memdup(const void *<emphasis role="italic">ctx</emphasis>, const void *<emphasis role="italic">p</emphasis>, size_t size);</title>
486 The talloc_memdup() function is equivalent to:
488 <programlisting>ptr = talloc_size(ctx, size);
489 if (ptr) memcpy(ptr, p, size);</programlisting>
491 <refsect2><title>char *talloc_strdup(const void *<emphasis role="italic">ctx</emphasis>, const char *<emphasis role="italic">p</emphasis>);</title>
493 The talloc_strdup() function is equivalent to:
495 <programlisting>ptr = talloc_size(ctx, strlen(p)+1);
496 if (ptr) memcpy(ptr, p, strlen(p)+1);</programlisting>
498 This function sets the name of the new pointer to the passed
499 string. This is equivalent to:
501 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
503 <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>
505 The talloc_strndup() function is the talloc equivalent of the C
506 library function strndup(3).
509 This function sets the name of the new pointer to the passed
510 string. This is equivalent to:
512 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
514 <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>
516 The talloc_vasprintf() function is the talloc equivalent of the C
517 library function vasprintf(3).
520 <refsect2><title>char *talloc_asprintf(const void *<emphasis role="italic">t</emphasis>, const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
522 The talloc_asprintf() function is the talloc equivalent of the C
523 library function asprintf(3).
526 This function sets the name of the new pointer to the passed
527 string. This is equivalent to:
529 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
531 <refsect2><title>char *talloc_asprintf_append(char *s, const char *fmt, ...);</title>
533 The talloc_asprintf_append() function appends the given formatted
534 string to the given string.
537 <refsect2><title>(type *)talloc_array(const void *ctx, type, uint_t count);</title>
539 The talloc_array() macro is equivalent to:
541 <programlisting>(type *)talloc_size(ctx, sizeof(type) * count);</programlisting>
543 except that it provides integer overflow protection for the
544 multiply, returning NULL if the multiply overflows.
547 <refsect2><title>void *talloc_array_size(const void *ctx, size_t size, uint_t count);</title>
549 The talloc_array_size() function is useful when the type is not
550 known. It operates in the same way as talloc_array(), but takes a
551 size instead of a type.
554 <refsect2><title>void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size)</title>
556 This is a non-macro version of talloc_realloc(), which is useful
557 as libraries sometimes want a realloc function pointer. A
558 realloc(3) implementation encapsulates the functionality of
559 malloc(3), free(3) and realloc(3) in one call, which is why it is
560 useful to be able to pass around a single function pointer.
563 <refsect2><title>void *talloc_autofree_context(void);</title>
565 This is a handy utility function that returns a talloc context
566 which will be automatically freed on program exit. This can be
567 used to reduce the noise in memory leak reports.
570 <refsect2><title>void *talloc_check_name(const void *ptr, const char *name);</title>
572 This function checks if a pointer has the specified <emphasis
573 role="italic">name</emphasis>. If it does then the pointer is
574 returned. It it doesn't then NULL is returned.
577 <refsect2><title>(type *)talloc_get_type(const void *ptr, type);</title>
579 This macro allows you to do type checking on talloc pointers. It
580 is particularly useful for void* private pointers. It is
583 <programlisting>(type *)talloc_check_name(ptr, #type)</programlisting>
585 <refsect2><title>talloc_set_type(const void *ptr, type);</title>
587 This macro allows you to force the name of a pointer to be a
588 particular <emphasis>type</emphasis>. This can be
589 used in conjunction with talloc_get_type() to do type checking on
593 It is equivalent to this:
595 <programlisting>talloc_set_name_const(ptr, #type)</programlisting>
598 <refsect1><title>PERFORMANCE</title>
600 All the additional features of talloc(3) over malloc(3) do come at a
601 price. We have a simple performance test in Samba4 that measures
602 talloc() versus malloc() performance, and it seems that talloc() is
603 about 10% slower than malloc() on my x86 Debian Linux box. For
604 Samba, the great reduction in code complexity that we get by using
605 talloc makes this worthwhile, especially as the total overhead of
606 talloc/malloc in Samba is already quite small.
609 <refsect1><title>SEE ALSO</title>
611 malloc(3), strndup(3), vasprintf(3), asprintf(3),
612 <ulink url="http://talloc.samba.org/"/>
615 <refsect1><title>COPYRIGHT/LICENSE</title>
617 Copyright (C) Andrew Tridgell 2004
620 This program is free software; you can redistribute it and/or modify
621 it under the terms of the GNU General Public License as published by
622 the Free Software Foundation; either version 2 of the License, or (at
623 your option) any later version.
626 This program is distributed in the hope that it will be useful, but
627 WITHOUT ANY WARRANTY; without even the implied warranty of
628 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
629 General Public License for more details.
632 You should have received a copy of the GNU General Public License
633 along with this program; if not, write to the Free Software
634 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.