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>
7 <refmiscinfo class="source">Samba</refmiscinfo>
8 <refmiscinfo class="manual">System Administration tools</refmiscinfo>
9 <refmiscinfo class="version">4.0</refmiscinfo>
12 <refname>talloc</refname>
13 <refpurpose>hierarchical reference counted memory pool system with destructors</refpurpose>
16 <synopsis>#include <talloc.h></synopsis>
18 <refsect1><title>DESCRIPTION</title>
20 If you are used to talloc from Samba3 then please read this
21 carefully, as talloc has changed a lot.
24 The new talloc is a hierarchical, reference counted memory pool
25 system with destructors. Quite a mouthful really, but not too bad
26 once you get used to it.
29 Perhaps the biggest change from Samba3 is that there is no
30 distinction between a "talloc context" and a "talloc pointer". Any
31 pointer returned from talloc() is itself a valid talloc context.
32 This means you can do this:
35 struct foo *X = talloc(mem_ctx, struct foo);
36 X->name = talloc_strdup(X, "foo");
39 and the pointer <literal role="code">X->name</literal>
40 would be a "child" of the talloc context <literal
41 role="code">X</literal> which is itself a child of
42 <literal role="code">mem_ctx</literal>. So if you do
43 <literal role="code">talloc_free(mem_ctx)</literal> then
44 it is all destroyed, whereas if you do <literal
45 role="code">talloc_free(X)</literal> then just <literal
46 role="code">X</literal> and <literal
47 role="code">X->name</literal> are destroyed, and if
49 role="code">talloc_free(X->name)</literal> then just
50 the name element of <literal role="code">X</literal> is
54 If you think about this, then what this effectively gives you is an
55 n-ary tree, where you can free any part of the tree with
59 If you find this confusing, then I suggest you run the <literal
60 role="code">testsuite</literal> program to watch talloc
61 in action. You may also like to add your own tests to <literal
62 role="code">testsuite.c</literal> to clarify how some
63 particular situation is handled.
66 <refsect1><title>TALLOC API</title>
68 The following is a complete guide to the talloc API. Read it all at
71 <refsect2><title>(type *)talloc(const void *ctx, type);</title>
73 The talloc() macro is the core of the talloc library. It takes a
74 memory <emphasis role="italic">ctx</emphasis> and a <emphasis
75 role="italic">type</emphasis>, and returns a pointer to a new
76 area of memory of the given <emphasis
77 role="italic">type</emphasis>.
80 The returned pointer is itself a talloc context, so you can use
81 it as the <emphasis role="italic">ctx</emphasis> argument to more
82 calls to talloc() if you wish.
85 The returned pointer is a "child" of the supplied context. This
86 means that if you talloc_free() the <emphasis
87 role="italic">ctx</emphasis> then the new child disappears as
88 well. Alternatively you can free just the child.
91 The <emphasis role="italic">ctx</emphasis> argument to talloc()
92 can be NULL, in which case a new top level context is created.
95 <refsect2><title>void *talloc_size(const void *ctx, size_t size);</title>
97 The function talloc_size() should be used when you don't have a
98 convenient type to pass to talloc(). Unlike talloc(), it is not
99 type safe (as it returns a void *), so you are on your own for
103 <refsect2><title>(typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);</title>
105 The talloc_ptrtype() macro should be used when you have a pointer and
106 want to allocate memory to point at with this pointer. When compiling
107 with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()
108 and talloc_get_name() will return the current location in the source file.
112 <refsect2><title>int talloc_free(void *ptr);</title>
114 The talloc_free() function frees a piece of talloc memory, and
115 all its children. You can call talloc_free() on any pointer
116 returned by talloc().
119 The return value of talloc_free() indicates success or failure,
120 with 0 returned for success and -1 for failure. The only
121 possible failure condition is if <emphasis
122 role="italic">ptr</emphasis> had a destructor attached to it and
123 the destructor returned -1. See <link
124 linkend="talloc_set_destructor"><quote>talloc_set_destructor()</quote></link>
125 for details on destructors.
128 If this pointer has an additional parent when talloc_free() is
129 called then the memory is not actually released, but instead the
130 most recently established parent is destroyed. See <link
131 linkend="talloc_reference"><quote>talloc_reference()</quote></link>
132 for details on establishing additional parents.
135 For more control on which parent is removed, see <link
136 linkend="talloc_unlink"><quote>talloc_unlink()</quote></link>.
139 talloc_free() operates recursively on its children.
142 From the 2.0 version of talloc, as a special case,
143 talloc_free() is refused on pointers that have more than one
144 parent, as talloc would have no way of knowing which parent
145 should be removed. To free a pointer that has more than one
146 parent please use talloc_unlink().
149 To help you find problems in your code caused by this behaviour, if
150 you do try and free a pointer with more than one parent then the
151 talloc logging function will be called to give output like this:
154 <screen format="linespecific">
155 ERROR: talloc_free with references at some_dir/source/foo.c:123
156 reference at some_dir/source/other.c:325
157 reference at some_dir/source/third.c:121
161 Please see the documentation for talloc_set_log_fn() and
162 talloc_set_log_stderr() for more information on talloc logging
166 <refsect2 id="talloc_reference"><title>void *talloc_reference(const void *ctx, const void *ptr);</title>
168 The talloc_reference() function makes <emphasis
169 role="italic">ctx</emphasis> an additional parent of <emphasis
170 role="italic">ptr</emphasis>.
173 The return value of talloc_reference() is always the original
174 pointer <emphasis role="italic">ptr</emphasis>, unless talloc ran
175 out of memory in creating the reference in which case it will
176 return NULL (each additional reference consumes around 48 bytes
177 of memory on intel x86 platforms).
180 If <emphasis role="italic">ptr</emphasis> is NULL, then the
181 function is a no-op, and simply returns NULL.
184 After creating a reference you can free it in one of the
191 you can talloc_free() any parent of the original pointer.
192 That will reduce the number of parents of this pointer by 1,
193 and will cause this pointer to be freed if it runs out of
199 you can talloc_free() the pointer itself. That will destroy
200 the most recently established parent to the pointer and leave
201 the pointer as a child of its current parent.
207 For more control on which parent to remove, see <link
208 linkend="talloc_unlink"><quote>talloc_unlink()</quote></link>.
211 <refsect2 id="talloc_unlink"><title>int talloc_unlink(const void *ctx, const void *ptr);</title>
213 The talloc_unlink() function removes a specific parent from
214 <emphasis role="italic">ptr</emphasis>. The <emphasis
215 role="italic">ctx</emphasis> passed must either be a context used
216 in talloc_reference() with this pointer, or must be a direct
220 Note that if the parent has already been removed using
221 talloc_free() then this function will fail and will return -1.
222 Likewise, if <emphasis role="italic">ptr</emphasis> is NULL, then
223 the function will make no modifications and return -1.
226 Usually you can just use talloc_free() instead of
227 talloc_unlink(), but sometimes it is useful to have the
228 additional control on which parent is removed.
231 <refsect2 id="talloc_set_destructor"><title>void talloc_set_destructor(const void *ptr, int (*destructor)(void *));</title>
233 The function talloc_set_destructor() sets the <emphasis
234 role="italic">destructor</emphasis> for the pointer <emphasis
235 role="italic">ptr</emphasis>. A <emphasis
236 role="italic">destructor</emphasis> is a function that is called
237 when the memory used by a pointer is about to be released. The
238 destructor receives <emphasis role="italic">ptr</emphasis> as an
239 argument, and should return 0 for success and -1 for failure.
242 The <emphasis role="italic">destructor</emphasis> can do anything
243 it wants to, including freeing other pieces of memory. A common
244 use for destructors is to clean up operating system resources
245 (such as open file descriptors) contained in the structure the
246 destructor is placed on.
249 You can only place one destructor on a pointer. If you need more
250 than one destructor then you can create a zero-length child of
251 the pointer and place an additional destructor on that.
254 To remove a destructor call talloc_set_destructor() with NULL for
258 If your destructor attempts to talloc_free() the pointer that it
259 is the destructor for then talloc_free() will return -1 and the
260 free will be ignored. This would be a pointless operation
261 anyway, as the destructor is only called when the memory is just
265 <refsect2><title>int talloc_increase_ref_count(const void *<emphasis role="italic">ptr</emphasis>);</title>
267 The talloc_increase_ref_count(<emphasis
268 role="italic">ptr</emphasis>) function is exactly equivalent to:
270 <programlisting>talloc_reference(NULL, ptr);</programlisting>
272 You can use either syntax, depending on which you think is
273 clearer in your code.
276 It returns 0 on success and -1 on failure.
279 <refsect2><title>size_t talloc_reference_count(const void *<emphasis role="italic">ptr</emphasis>);</title>
281 Return the number of references to the pointer.
284 <refsect2 id="talloc_set_name"><title>void talloc_set_name(const void *ptr, const char *fmt, ...);</title>
286 Each talloc pointer has a "name". The name is used principally
287 for debugging purposes, although it is also possible to set and
288 get the name on a pointer in as a way of "marking" pointers in
292 The main use for names on pointer is for "talloc reports". See
294 linkend="talloc_report"><quote>talloc_report_depth_cb()</quote></link>,
296 linkend="talloc_report"><quote>talloc_report_depth_file()</quote></link>,
298 linkend="talloc_report"><quote>talloc_report()</quote></link>
300 linkend="talloc_report"><quote>talloc_report()</quote></link>
302 linkend="talloc_report_full"><quote>talloc_report_full()</quote></link>
303 for details. Also see <link
304 linkend="talloc_enable_leak_report"><quote>talloc_enable_leak_report()</quote></link>
306 linkend="talloc_enable_leak_report_full"><quote>talloc_enable_leak_report_full()</quote></link>.
309 The talloc_set_name() function allocates memory as a child of the
310 pointer. It is logically equivalent to:
312 <programlisting>talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));</programlisting>
314 Note that multiple calls to talloc_set_name() will allocate more
315 memory without releasing the name. All of the memory is released
316 when the ptr is freed using talloc_free().
319 <refsect2><title>void talloc_set_name_const(const void *<emphasis role="italic">ptr</emphasis>, const char *<emphasis role="italic">name</emphasis>);</title>
321 The function talloc_set_name_const() is just like
322 talloc_set_name(), but it takes a string constant, and is much
323 faster. It is extensively used by the "auto naming" macros, such
327 This function does not allocate any memory. It just copies the
328 supplied pointer into the internal representation of the talloc
329 ptr. This means you must not pass a <emphasis
330 role="italic">name</emphasis> pointer to memory that will
331 disappear before <emphasis role="italic">ptr</emphasis> is freed
335 <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>
337 The talloc_named() function creates a named talloc pointer. It
340 <programlisting>ptr = talloc_size(ctx, size);
341 talloc_set_name(ptr, fmt, ....);</programlisting>
343 <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>
345 This is equivalent to:
347 <programlisting>ptr = talloc_size(ctx, size);
348 talloc_set_name_const(ptr, name);</programlisting>
350 <refsect2><title>const char *talloc_get_name(const void *<emphasis role="italic">ptr</emphasis>);</title>
352 This returns the current name for the given talloc pointer,
353 <emphasis role="italic">ptr</emphasis>. See <link
354 linkend="talloc_set_name"><quote>talloc_set_name()</quote></link>
358 <refsect2><title>void *talloc_init(const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
360 This function creates a zero length named talloc context as a top
361 level context. It is equivalent to:
363 <programlisting>talloc_named(NULL, 0, fmt, ...);</programlisting>
365 <refsect2><title>void *talloc_new(void *<emphasis role="italic">ctx</emphasis>);</title>
367 This is a utility macro that creates a new memory context hanging
368 off an existing context, automatically naming it "talloc_new:
369 __location__" where __location__ is the source line it is called
370 from. It is particularly useful for creating a new temporary
374 <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>
376 The talloc_realloc() macro changes the size of a talloc pointer.
377 It has the following equivalences:
379 <programlisting>talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
380 talloc_realloc(ctx, ptr, type, 0) ==> talloc_free(ptr);</programlisting>
382 The <emphasis role="italic">ctx</emphasis> argument is only used
383 if <emphasis role="italic">ptr</emphasis> is not NULL, otherwise
387 talloc_realloc() returns the new pointer, or NULL on failure.
388 The call will fail either due to a lack of memory, or because the
389 pointer has more than one parent (see <link
390 linkend="talloc_reference"><quote>talloc_reference()</quote></link>).
393 <refsect2><title>void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);</title>
395 the talloc_realloc_size() function is useful when the type is not
396 known so the type-safe talloc_realloc() cannot be used.
399 <refsect2><title>TYPE *talloc_steal(const void *<emphasis role="italic">new_ctx</emphasis>, const TYPE *<emphasis role="italic">ptr</emphasis>);</title>
401 The talloc_steal() function changes the parent context of a
402 talloc pointer. It is typically used when the context that the
403 pointer is currently a child of is going to be freed and you wish
404 to keep the memory for a longer time.
407 The talloc_steal() function returns the pointer that you pass it.
408 It does not have any failure modes.
411 It is possible to produce loops in the parent/child
412 relationship if you are not careful with talloc_steal(). No
413 guarantees are provided as to your sanity or the safety of your
417 Note that if you try and call talloc_steal() on a pointer that has
418 more than one parent then the result is ambiguous. Talloc will choose
419 to remove the parent that is currently indicated by talloc_parent()
420 and replace it with the chosen parent. You will also get a message
421 like this via the talloc logging functions:
424 <screen format="linespecific">
425 WARNING: talloc_steal with references at some_dir/source/foo.c:123
426 reference at some_dir/source/other.c:325
427 reference at some_dir/source/third.c:121
431 To unambiguously change the parent of a pointer please see
433 function <link linkend="talloc_reference"><quote>talloc_reparent()</quote></link>. See
434 the talloc_set_log_fn() documentation for more information
438 <refsect2><title>TYPE *talloc_reparent(const void *<emphasis role="italic">old_parent</emphasis>, const void *<emphasis role="italic">new_parent</emphasis>, const TYPE *<emphasis role="italic">ptr</emphasis>);</title>
440 The talloc_reparent() function changes the parent context of a talloc
441 pointer. It is typically used when the context that the pointer is
442 currently a child of is going to be freed and you wish to keep the
443 memory for a longer time.
446 The talloc_reparent() function returns the pointer that you pass it. It
447 does not have any failure modes.
450 The difference between talloc_reparent() and talloc_steal() is that
451 talloc_reparent() can specify which parent you wish to change. This is
452 useful when a pointer has multiple parents via references.
455 <refsect2><title>TYPE *talloc_move(const void *<emphasis role="italic">new_ctx</emphasis>, TYPE **<emphasis role="italic">ptr</emphasis>);</title>
457 The talloc_move() function is a wrapper around
458 talloc_steal() which zeros the source pointer after the
459 move. This avoids a potential source of bugs where a
460 programmer leaves a pointer in two structures, and uses the
461 pointer from the old structure after it has been moved to a
465 <refsect2><title>size_t talloc_total_size(const void *<emphasis role="italic">ptr</emphasis>);</title>
467 The talloc_total_size() function returns the total size in bytes
468 used by this pointer and all child pointers. Mostly useful for
472 Passing NULL is allowed, but it will only give a meaningful
473 result if talloc_enable_leak_report() or
474 talloc_enable_leak_report_full() has been called.
477 <refsect2><title>size_t talloc_total_blocks(const void *<emphasis role="italic">ptr</emphasis>);</title>
479 The talloc_total_blocks() function returns the total memory block
480 count used by this pointer and all child pointers. Mostly useful
484 Passing NULL is allowed, but it will only give a meaningful
485 result if talloc_enable_leak_report() or
486 talloc_enable_leak_report_full() has been called.
489 <refsect2 id="talloc_report"><title>void talloc_report(const void *ptr, FILE *f);</title>
491 The talloc_report() function prints a summary report of all
492 memory used by <emphasis role="italic">ptr</emphasis>. One line
493 of report is printed for each immediate child of ptr, showing the
494 total memory and number of blocks used by that child.
497 You can pass NULL for the pointer, in which case a report is
498 printed for the top level memory context, but only if
499 talloc_enable_leak_report() or talloc_enable_leak_report_full()
503 <refsect2 id="talloc_report_full"><title>void talloc_report_full(const void *<emphasis role="italic">ptr</emphasis>, FILE *<emphasis role="italic">f</emphasis>);</title>
505 This provides a more detailed report than talloc_report(). It
506 will recursively print the entire tree of memory referenced by
507 the pointer. References in the tree are shown by giving the name
508 of the pointer that is referenced.
511 You can pass NULL for the pointer, in which case a report is
512 printed for the top level memory context, but only if
513 talloc_enable_leak_report() or talloc_enable_leak_report_full()
517 <refsect2 id="talloc_report_depth_cb">
518 <funcsynopsis><funcprototype>
519 <funcdef>void <function>talloc_report_depth_cb</function></funcdef>
520 <paramdef><parameter>const void *ptr</parameter></paramdef>
521 <paramdef><parameter>int depth</parameter></paramdef>
522 <paramdef><parameter>int max_depth</parameter></paramdef>
523 <paramdef><parameter>void (*callback)(const void *ptr, int depth, int max_depth, int is_ref, void *priv)</parameter></paramdef>
524 <paramdef><parameter>void *priv</parameter></paramdef>
525 </funcprototype></funcsynopsis>
527 This provides a more flexible reports than talloc_report(). It
528 will recursively call the callback for the entire tree of memory
529 referenced by the pointer. References in the tree are passed with
530 <emphasis role="italic">is_ref = 1</emphasis> and the pointer that is referenced.
533 You can pass NULL for the pointer, in which case a report is
534 printed for the top level memory context, but only if
535 talloc_enable_leak_report() or talloc_enable_leak_report_full()
539 The recursion is stopped when depth >= max_depth.
540 max_depth = -1 means only stop at leaf nodes.
543 <refsect2 id="talloc_report_depth_file">
544 <funcsynopsis><funcprototype>
545 <funcdef>void <function>talloc_report_depth_file</function></funcdef>
546 <paramdef><parameter>const void *ptr</parameter></paramdef>
547 <paramdef><parameter>int depth</parameter></paramdef>
548 <paramdef><parameter>int max_depth</parameter></paramdef>
549 <paramdef><parameter>FILE *f</parameter></paramdef>
550 </funcprototype></funcsynopsis>
552 This provides a more flexible reports than talloc_report(). It
553 will let you specify the depth and max_depth.
556 <refsect2 id="talloc_enable_leak_report"><title>void talloc_enable_leak_report(void);</title>
558 This enables calling of talloc_report(NULL, stderr) when the
559 program exits. In Samba4 this is enabled by using the
560 --leak-report command line option.
563 For it to be useful, this function must be called before any
564 other talloc function as it establishes a "null context" that
565 acts as the top of the tree. If you don't call this function
566 first then passing NULL to talloc_report() or
567 talloc_report_full() won't give you the full tree printout.
570 Here is a typical talloc report:
572 <screen format="linespecific">talloc report on 'null_context' (total 267 bytes in 15 blocks)
573 libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
574 libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
575 iconv(UTF8,CP850) contains 42 bytes in 2 blocks
576 libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
577 iconv(CP850,UTF8) contains 42 bytes in 2 blocks
578 iconv(UTF8,UTF-16LE) contains 45 bytes in 2 blocks
579 iconv(UTF-16LE,UTF8) contains 45 bytes in 2 blocks
582 <refsect2 id="talloc_enable_leak_report_full"><title>void talloc_enable_leak_report_full(void);</title>
584 This enables calling of talloc_report_full(NULL, stderr) when the
585 program exits. In Samba4 this is enabled by using the
586 --leak-report-full command line option.
589 For it to be useful, this function must be called before any
590 other talloc function as it establishes a "null context" that
591 acts as the top of the tree. If you don't call this function
592 first then passing NULL to talloc_report() or
593 talloc_report_full() won't give you the full tree printout.
596 Here is a typical full report:
598 <screen format="linespecific">full talloc report on 'root' (total 18 bytes in 8 blocks)
599 p1 contains 18 bytes in 7 blocks (ref 0)
600 r1 contains 13 bytes in 2 blocks (ref 0)
602 p2 contains 1 bytes in 1 blocks (ref 1)
603 x3 contains 1 bytes in 1 blocks (ref 0)
604 x2 contains 1 bytes in 1 blocks (ref 0)
605 x1 contains 1 bytes in 1 blocks (ref 0)
608 <refsect2><title>(<emphasis role="italic">type</emphasis> *)talloc_zero(const void *<emphasis role="italic">ctx</emphasis>, <emphasis role="italic">type</emphasis>);</title>
610 The talloc_zero() macro is equivalent to:
612 <programlisting>ptr = talloc(ctx, type);
613 if (ptr) memset(ptr, 0, sizeof(type));</programlisting>
615 <refsect2><title>void *talloc_zero_size(const void *<emphasis role="italic">ctx</emphasis>, size_t <emphasis role="italic">size</emphasis>)</title>
617 The talloc_zero_size() function is useful when you don't have a
621 <refsect2><title>void *talloc_memdup(const void *<emphasis role="italic">ctx</emphasis>, const void *<emphasis role="italic">p</emphasis>, size_t size);</title>
623 The talloc_memdup() function is equivalent to:
625 <programlisting>ptr = talloc_size(ctx, size);
626 if (ptr) memcpy(ptr, p, size);</programlisting>
628 <refsect2><title>char *talloc_strdup(const void *<emphasis role="italic">ctx</emphasis>, const char *<emphasis role="italic">p</emphasis>);</title>
630 The talloc_strdup() function is equivalent to:
632 <programlisting>ptr = talloc_size(ctx, strlen(p)+1);
633 if (ptr) memcpy(ptr, p, strlen(p)+1);</programlisting>
635 This function sets the name of the new pointer to the passed
636 string. This is equivalent to:
638 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
640 <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>
642 The talloc_strndup() function is the talloc equivalent of the C
643 library function strndup(3).
646 This function sets the name of the new pointer to the passed
647 string. This is equivalent to:
649 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
651 <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>
653 The talloc_vasprintf() function is the talloc equivalent of the C
654 library function vasprintf(3).
657 This function sets the name of the new pointer to the new
658 string. This is equivalent to:
660 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
662 <refsect2><title>char *talloc_asprintf(const void *<emphasis role="italic">t</emphasis>, const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
664 The talloc_asprintf() function is the talloc equivalent of the C
665 library function asprintf(3).
668 This function sets the name of the new pointer to the passed
669 string. This is equivalent to:
671 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
673 <refsect2><title>char *talloc_asprintf_append(char *s, const char *fmt, ...);</title>
675 The talloc_asprintf_append() function appends the given formatted
676 string to the given string.
679 This function sets the name of the new pointer to the new
680 string. This is equivalent to:
682 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
684 <refsect2><title>(type *)talloc_array(const void *ctx, type, unsigned int count);</title>
686 The talloc_array() macro is equivalent to:
688 <programlisting>(type *)talloc_size(ctx, sizeof(type) * count);</programlisting>
690 except that it provides integer overflow protection for the
691 multiply, returning NULL if the multiply overflows.
694 <refsect2><title>void *talloc_array_size(const void *ctx, size_t size, unsigned int count);</title>
696 The talloc_array_size() function is useful when the type is not
697 known. It operates in the same way as talloc_array(), but takes a
698 size instead of a type.
701 <refsect2><title>(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, unsigned int count);</title>
703 The talloc_ptrtype() macro should be used when you have a pointer to an array
704 and want to allocate memory of an array to point at with this pointer. When compiling
705 with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
706 and talloc_get_name() will return the current location in the source file.
710 <refsect2><title>void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size)</title>
712 This is a non-macro version of talloc_realloc(), which is useful
713 as libraries sometimes want a realloc function pointer. A
714 realloc(3) implementation encapsulates the functionality of
715 malloc(3), free(3) and realloc(3) in one call, which is why it is
716 useful to be able to pass around a single function pointer.
719 <refsect2><title>void *talloc_autofree_context(void);</title>
721 This is a handy utility function that returns a talloc context
722 which will be automatically freed on program exit. This can be
723 used to reduce the noise in memory leak reports.
726 <refsect2><title>void *talloc_check_name(const void *ptr, const char *name);</title>
728 This function checks if a pointer has the specified <emphasis
729 role="italic">name</emphasis>. If it does then the pointer is
730 returned. It it doesn't then NULL is returned.
733 <refsect2><title>(type *)talloc_get_type(const void *ptr, type);</title>
735 This macro allows you to do type checking on talloc pointers. It
736 is particularly useful for void* private pointers. It is
739 <programlisting>(type *)talloc_check_name(ptr, #type)</programlisting>
741 <refsect2><title>talloc_set_type(const void *ptr, type);</title>
743 This macro allows you to force the name of a pointer to be a
744 particular <emphasis>type</emphasis>. This can be
745 used in conjunction with talloc_get_type() to do type checking on
749 It is equivalent to this:
751 <programlisting>talloc_set_name_const(ptr, #type)</programlisting>
753 <refsect2><title>talloc_set_log_fn(void (*log_fn)(const char *message));</title>
755 This function sets a logging function that talloc will use for
756 warnings and errors. By default talloc will not print any warnings or
760 <refsect2><title>talloc_set_log_stderr(void);</title>
762 This sets the talloc log function to write log messages to stderr
766 <refsect1><title>PERFORMANCE</title>
768 All the additional features of talloc(3) over malloc(3) do come at a
769 price. We have a simple performance test in Samba4 that measures
770 talloc() versus malloc() performance, and it seems that talloc() is
771 about 10% slower than malloc() on my x86 Debian Linux box. For
772 Samba, the great reduction in code complexity that we get by using
773 talloc makes this worthwhile, especially as the total overhead of
774 talloc/malloc in Samba is already quite small.
777 <refsect1><title>SEE ALSO</title>
779 malloc(3), strndup(3), vasprintf(3), asprintf(3),
780 <ulink url="http://talloc.samba.org/"/>
784 <refsect1><title>AUTHOR</title>
785 <para> The original Samba software and related utilities were
786 created by Andrew Tridgell. Samba is now developed by the
787 Samba Team as an Open Source project similar to the way the
788 Linux kernel is developed.
792 <refsect1><title>COPYRIGHT/LICENSE</title>
794 Copyright (C) Andrew Tridgell 2004
797 This program is free software; you can redistribute it and/or modify
798 it under the terms of the GNU Lesser General Public License as
799 published by the Free Software Foundation; either version 3 of the
800 License, or (at your option) any later version.
803 This program is distributed in the hope that it will be useful, but
804 WITHOUT ANY WARRANTY; without even the implied warranty of
805 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
806 General Public License for more details.
809 You should have received a copy of the GNU General Public License
810 along with this program; if not, see http://www.gnu.org/licenses/.