2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4 <refentryinfo><date>2015-04-10</date></refentryinfo>
6 <refentrytitle>talloc</refentrytitle>
7 <manvolnum>3</manvolnum>
8 <refmiscinfo class="source">Samba</refmiscinfo>
9 <refmiscinfo class="manual">System Administration tools</refmiscinfo>
10 <refmiscinfo class="version">4.0</refmiscinfo>
13 <refname>talloc</refname>
14 <refpurpose>hierarchical reference counted memory pool system with destructors</refpurpose>
17 <synopsis>#include <talloc.h></synopsis>
19 <refsect1><title>DESCRIPTION</title>
21 If you are used to talloc from Samba3 then please read this
22 carefully, as talloc has changed a lot.
25 The new talloc is a hierarchical, reference counted memory pool
26 system with destructors. Quite a mouthful really, but not too bad
27 once you get used to it.
30 Perhaps the biggest change from Samba3 is that there is no
31 distinction between a "talloc context" and a "talloc pointer". Any
32 pointer returned from talloc() is itself a valid talloc context.
33 This means you can do this:
36 struct foo *X = talloc(mem_ctx, struct foo);
37 X->name = talloc_strdup(X, "foo");
40 and the pointer <literal role="code">X->name</literal>
41 would be a "child" of the talloc context <literal
42 role="code">X</literal> which is itself a child of
43 <literal role="code">mem_ctx</literal>. So if you do
44 <literal role="code">talloc_free(mem_ctx)</literal> then
45 it is all destroyed, whereas if you do <literal
46 role="code">talloc_free(X)</literal> then just <literal
47 role="code">X</literal> and <literal
48 role="code">X->name</literal> are destroyed, and if
50 role="code">talloc_free(X->name)</literal> then just
51 the name element of <literal role="code">X</literal> is
55 If you think about this, then what this effectively gives you is an
56 n-ary tree, where you can free any part of the tree with
60 If you find this confusing, then I suggest you run the <literal
61 role="code">testsuite</literal> program to watch talloc
62 in action. You may also like to add your own tests to <literal
63 role="code">testsuite.c</literal> to clarify how some
64 particular situation is handled.
67 <refsect1><title>TALLOC API</title>
69 The following is a complete guide to the talloc API. Read it all at
72 <refsect2><title>(type *)talloc(const void *ctx, type);</title>
74 The talloc() macro is the core of the talloc library. It takes a
75 memory <emphasis role="italic">ctx</emphasis> and a <emphasis
76 role="italic">type</emphasis>, and returns a pointer to a new
77 area of memory of the given <emphasis
78 role="italic">type</emphasis>.
81 The returned pointer is itself a talloc context, so you can use
82 it as the <emphasis role="italic">ctx</emphasis> argument to more
83 calls to talloc() if you wish.
86 The returned pointer is a "child" of the supplied context. This
87 means that if you talloc_free() the <emphasis
88 role="italic">ctx</emphasis> then the new child disappears as
89 well. Alternatively you can free just the child.
92 The <emphasis role="italic">ctx</emphasis> argument to talloc()
93 can be NULL, in which case a new top level context is created.
96 <refsect2><title>void *talloc_size(const void *ctx, size_t size);</title>
98 The function talloc_size() should be used when you don't have a
99 convenient type to pass to talloc(). Unlike talloc(), it is not
100 type safe (as it returns a void *), so you are on your own for
104 <refsect2><title>(typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);</title>
106 The talloc_ptrtype() macro should be used when you have a pointer and
107 want to allocate memory to point at with this pointer. When compiling
108 with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()
109 and talloc_get_name() will return the current location in the source file.
113 <refsect2><title>int talloc_free(void *ptr);</title>
115 The talloc_free() function frees a piece of talloc memory, and
116 all its children. You can call talloc_free() on any pointer
117 returned by talloc().
120 The return value of talloc_free() indicates success or failure,
121 with 0 returned for success and -1 for failure. The only
122 possible failure condition is if <emphasis
123 role="italic">ptr</emphasis> had a destructor attached to it and
124 the destructor returned -1. See <link
125 linkend="talloc_set_destructor"><quote>talloc_set_destructor()</quote></link>
126 for details on destructors.
129 If this pointer has an additional parent when talloc_free() is
130 called then the memory is not actually released, but instead the
131 most recently established parent is destroyed. See <link
132 linkend="talloc_reference"><quote>talloc_reference()</quote></link>
133 for details on establishing additional parents.
136 For more control on which parent is removed, see <link
137 linkend="talloc_unlink"><quote>talloc_unlink()</quote></link>.
140 talloc_free() operates recursively on its children.
143 From the 2.0 version of talloc, as a special case,
144 talloc_free() is refused on pointers that have more than one
145 parent, as talloc would have no way of knowing which parent
146 should be removed. To free a pointer that has more than one
147 parent please use talloc_unlink().
150 To help you find problems in your code caused by this behaviour, if
151 you do try and free a pointer with more than one parent then the
152 talloc logging function will be called to give output like this:
155 <screen format="linespecific">
156 ERROR: talloc_free with references at some_dir/source/foo.c:123
157 reference at some_dir/source/other.c:325
158 reference at some_dir/source/third.c:121
162 Please see the documentation for talloc_set_log_fn() and
163 talloc_set_log_stderr() for more information on talloc logging
167 <refsect2 id="talloc_reference"><title>void *talloc_reference(const void *ctx, const void *ptr);</title>
169 The talloc_reference() function makes <emphasis
170 role="italic">ctx</emphasis> an additional parent of <emphasis
171 role="italic">ptr</emphasis>.
174 The return value of talloc_reference() is always the original
175 pointer <emphasis role="italic">ptr</emphasis>, unless talloc ran
176 out of memory in creating the reference in which case it will
177 return NULL (each additional reference consumes around 48 bytes
178 of memory on intel x86 platforms).
181 If <emphasis role="italic">ptr</emphasis> is NULL, then the
182 function is a no-op, and simply returns NULL.
185 After creating a reference you can free it in one of the
192 you can talloc_free() any parent of the original pointer.
193 That will reduce the number of parents of this pointer by 1,
194 and will cause this pointer to be freed if it runs out of
200 you can talloc_free() the pointer itself if it has at maximum one
201 parent. This behaviour has been changed since the release of version
202 2.0. Further information in the description of "talloc_free".
208 For more control on which parent to remove, see <link
209 linkend="talloc_unlink"><quote>talloc_unlink()</quote></link>.
212 <refsect2 id="talloc_unlink"><title>int talloc_unlink(const void *ctx, void *ptr);</title>
214 The talloc_unlink() function removes a specific parent from
215 <emphasis role="italic">ptr</emphasis>. The <emphasis
216 role="italic">ctx</emphasis> passed must either be a context used
217 in talloc_reference() with this pointer, or must be a direct
221 Note that if the parent has already been removed using
222 talloc_free() then this function will fail and will return -1.
223 Likewise, if <emphasis role="italic">ptr</emphasis> is NULL, then
224 the function will make no modifications and return -1.
227 Usually you can just use talloc_free() instead of
228 talloc_unlink(), but sometimes it is useful to have the
229 additional control on which parent is removed.
232 <refsect2 id="talloc_set_destructor"><title>void talloc_set_destructor(const void *ptr, int (*destructor)(void *));</title>
234 The function talloc_set_destructor() sets the <emphasis
235 role="italic">destructor</emphasis> for the pointer <emphasis
236 role="italic">ptr</emphasis>. A <emphasis
237 role="italic">destructor</emphasis> is a function that is called
238 when the memory used by a pointer is about to be released. The
239 destructor receives <emphasis role="italic">ptr</emphasis> as an
240 argument, and should return 0 for success and -1 for failure.
243 The <emphasis role="italic">destructor</emphasis> can do anything
244 it wants to, including freeing other pieces of memory. A common
245 use for destructors is to clean up operating system resources
246 (such as open file descriptors) contained in the structure the
247 destructor is placed on.
250 You can only place one destructor on a pointer. If you need more
251 than one destructor then you can create a zero-length child of
252 the pointer and place an additional destructor on that.
255 To remove a destructor call talloc_set_destructor() with NULL for
259 If your destructor attempts to talloc_free() the pointer that it
260 is the destructor for then talloc_free() will return -1 and the
261 free will be ignored. This would be a pointless operation
262 anyway, as the destructor is only called when the memory is just
266 <refsect2><title>int talloc_increase_ref_count(const void *<emphasis role="italic">ptr</emphasis>);</title>
268 The talloc_increase_ref_count(<emphasis
269 role="italic">ptr</emphasis>) function is exactly equivalent to:
271 <programlisting>talloc_reference(NULL, ptr);</programlisting>
273 You can use either syntax, depending on which you think is
274 clearer in your code.
277 It returns 0 on success and -1 on failure.
280 <refsect2><title>size_t talloc_reference_count(const void *<emphasis role="italic">ptr</emphasis>);</title>
282 Return the number of references to the pointer.
285 <refsect2 id="talloc_set_name"><title>void talloc_set_name(const void *ptr, const char *fmt, ...);</title>
287 Each talloc pointer has a "name". The name is used principally
288 for debugging purposes, although it is also possible to set and
289 get the name on a pointer in as a way of "marking" pointers in
293 The main use for names on pointer is for "talloc reports". See
295 linkend="talloc_report"><quote>talloc_report_depth_cb()</quote></link>,
297 linkend="talloc_report"><quote>talloc_report_depth_file()</quote></link>,
299 linkend="talloc_report"><quote>talloc_report()</quote></link>
301 linkend="talloc_report"><quote>talloc_report()</quote></link>
303 linkend="talloc_report_full"><quote>talloc_report_full()</quote></link>
304 for details. Also see <link
305 linkend="talloc_enable_leak_report"><quote>talloc_enable_leak_report()</quote></link>
307 linkend="talloc_enable_leak_report_full"><quote>talloc_enable_leak_report_full()</quote></link>.
310 The talloc_set_name() function allocates memory as a child of the
311 pointer. It is logically equivalent to:
313 <programlisting>talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));</programlisting>
315 Note that multiple calls to talloc_set_name() will allocate more
316 memory without releasing the name. All of the memory is released
317 when the ptr is freed using talloc_free().
320 <refsect2><title>void talloc_set_name_const(const void *<emphasis role="italic">ptr</emphasis>, const char *<emphasis role="italic">name</emphasis>);</title>
322 The function talloc_set_name_const() is just like
323 talloc_set_name(), but it takes a string constant, and is much
324 faster. It is extensively used by the "auto naming" macros, such
328 This function does not allocate any memory. It just copies the
329 supplied pointer into the internal representation of the talloc
330 ptr. This means you must not pass a <emphasis
331 role="italic">name</emphasis> pointer to memory that will
332 disappear before <emphasis role="italic">ptr</emphasis> is freed
336 <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>
338 The talloc_named() function creates a named talloc pointer. It
341 <programlisting>ptr = talloc_size(ctx, size);
342 talloc_set_name(ptr, fmt, ....);</programlisting>
344 <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>
346 This is equivalent to:
348 <programlisting>ptr = talloc_size(ctx, size);
349 talloc_set_name_const(ptr, name);</programlisting>
351 <refsect2><title>const char *talloc_get_name(const void *<emphasis role="italic">ptr</emphasis>);</title>
353 This returns the current name for the given talloc pointer,
354 <emphasis role="italic">ptr</emphasis>. See <link
355 linkend="talloc_set_name"><quote>talloc_set_name()</quote></link>
359 <refsect2><title>void *talloc_init(const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
361 This function creates a zero length named talloc context as a top
362 level context. It is equivalent to:
364 <programlisting>talloc_named(NULL, 0, fmt, ...);</programlisting>
366 <refsect2><title>void *talloc_new(void *<emphasis role="italic">ctx</emphasis>);</title>
368 This is a utility macro that creates a new memory context hanging
369 off an existing context, automatically naming it "talloc_new:
370 __location__" where __location__ is the source line it is called
371 from. It is particularly useful for creating a new temporary
375 <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>
377 The talloc_realloc() macro changes the size of a talloc pointer.
378 It has the following equivalences:
380 <programlisting>talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
381 talloc_realloc(ctx, ptr, type, 0) ==> talloc_free(ptr);</programlisting>
383 The <emphasis role="italic">ctx</emphasis> argument is only used
384 if <emphasis role="italic">ptr</emphasis> is not NULL, otherwise
388 talloc_realloc() returns the new pointer, or NULL on failure.
389 The call will fail either due to a lack of memory, or because the
390 pointer has more than one parent (see <link
391 linkend="talloc_reference"><quote>talloc_reference()</quote></link>).
394 <refsect2><title>void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);</title>
396 the talloc_realloc_size() function is useful when the type is not
397 known so the type-safe talloc_realloc() cannot be used.
400 <refsect2><title>TYPE *talloc_steal(const void *<emphasis role="italic">new_ctx</emphasis>, const TYPE *<emphasis role="italic">ptr</emphasis>);</title>
402 The talloc_steal() function changes the parent context of a
403 talloc pointer. It is typically used when the context that the
404 pointer is currently a child of is going to be freed and you wish
405 to keep the memory for a longer time.
408 The talloc_steal() function returns the pointer that you pass it.
409 It does not have any failure modes.
412 It is possible to produce loops in the parent/child
413 relationship if you are not careful with talloc_steal(). No
414 guarantees are provided as to your sanity or the safety of your
418 Note that if you try and call talloc_steal() on a pointer that has
419 more than one parent then the result is ambiguous. Talloc will choose
420 to remove the parent that is currently indicated by talloc_parent()
421 and replace it with the chosen parent. You will also get a message
422 like this via the talloc logging functions:
425 <screen format="linespecific">
426 WARNING: talloc_steal with references at some_dir/source/foo.c:123
427 reference at some_dir/source/other.c:325
428 reference at some_dir/source/third.c:121
432 To unambiguously change the parent of a pointer please see
434 function <link linkend="talloc_reference"><quote>talloc_reparent()</quote></link>. See
435 the talloc_set_log_fn() documentation for more information
439 <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>
441 The talloc_reparent() function changes the parent context of a talloc
442 pointer. It is typically used when the context that the pointer is
443 currently a child of is going to be freed and you wish to keep the
444 memory for a longer time.
447 The talloc_reparent() function returns the pointer that you pass it. It
448 does not have any failure modes.
451 The difference between talloc_reparent() and talloc_steal() is that
452 talloc_reparent() can specify which parent you wish to change. This is
453 useful when a pointer has multiple parents via references.
456 <refsect2><title>TYPE *talloc_move(const void *<emphasis role="italic">new_ctx</emphasis>, TYPE **<emphasis role="italic">ptr</emphasis>);</title>
458 The talloc_move() function is a wrapper around
459 talloc_steal() which zeros the source pointer after the
460 move. This avoids a potential source of bugs where a
461 programmer leaves a pointer in two structures, and uses the
462 pointer from the old structure after it has been moved to a
466 <refsect2><title>size_t talloc_total_size(const void *<emphasis role="italic">ptr</emphasis>);</title>
468 The talloc_total_size() function returns the total size in bytes
469 used by this pointer and all child pointers. Mostly useful for
473 Passing NULL is allowed, but it will only give a meaningful
474 result if talloc_enable_leak_report() or
475 talloc_enable_leak_report_full() has been called.
478 <refsect2><title>size_t talloc_total_blocks(const void *<emphasis role="italic">ptr</emphasis>);</title>
480 The talloc_total_blocks() function returns the total memory block
481 count used by this pointer and all child pointers. Mostly useful
485 Passing NULL is allowed, but it will only give a meaningful
486 result if talloc_enable_leak_report() or
487 talloc_enable_leak_report_full() has been called.
490 <refsect2 id="talloc_report"><title>void talloc_report(const void *ptr, FILE *f);</title>
492 The talloc_report() function prints a summary report of all
493 memory used by <emphasis role="italic">ptr</emphasis>. One line
494 of report is printed for each immediate child of ptr, showing the
495 total memory and number of blocks used by that child.
498 You can pass NULL for the pointer, in which case a report is
499 printed for the top level memory context, but only if
500 talloc_enable_leak_report() or talloc_enable_leak_report_full()
504 <refsect2 id="talloc_report_full"><title>void talloc_report_full(const void *<emphasis role="italic">ptr</emphasis>, FILE *<emphasis role="italic">f</emphasis>);</title>
506 This provides a more detailed report than talloc_report(). It
507 will recursively print the entire tree of memory referenced by
508 the pointer. References in the tree are shown by giving the name
509 of the pointer that is referenced.
512 You can pass NULL for the pointer, in which case a report is
513 printed for the top level memory context, but only if
514 talloc_enable_leak_report() or talloc_enable_leak_report_full()
518 <refsect2 id="talloc_report_depth_cb">
519 <funcsynopsis><funcprototype>
520 <funcdef>void <function>talloc_report_depth_cb</function></funcdef>
521 <paramdef><parameter>const void *ptr</parameter></paramdef>
522 <paramdef><parameter>int depth</parameter></paramdef>
523 <paramdef><parameter>int max_depth</parameter></paramdef>
524 <paramdef><parameter>void (*callback)(const void *ptr, int depth, int max_depth, int is_ref, void *priv)</parameter></paramdef>
525 <paramdef><parameter>void *priv</parameter></paramdef>
526 </funcprototype></funcsynopsis>
528 This provides a more flexible reports than talloc_report(). It
529 will recursively call the callback for the entire tree of memory
530 referenced by the pointer. References in the tree are passed with
531 <emphasis role="italic">is_ref = 1</emphasis> and the pointer that is referenced.
534 You can pass NULL for the pointer, in which case a report is
535 printed for the top level memory context, but only if
536 talloc_enable_leak_report() or talloc_enable_leak_report_full()
540 The recursion is stopped when depth >= max_depth.
541 max_depth = -1 means only stop at leaf nodes.
544 <refsect2 id="talloc_report_depth_file">
545 <funcsynopsis><funcprototype>
546 <funcdef>void <function>talloc_report_depth_file</function></funcdef>
547 <paramdef><parameter>const void *ptr</parameter></paramdef>
548 <paramdef><parameter>int depth</parameter></paramdef>
549 <paramdef><parameter>int max_depth</parameter></paramdef>
550 <paramdef><parameter>FILE *f</parameter></paramdef>
551 </funcprototype></funcsynopsis>
553 This provides a more flexible reports than talloc_report(). It
554 will let you specify the depth and max_depth.
557 <refsect2 id="talloc_enable_leak_report"><title>void talloc_enable_leak_report(void);</title>
559 This enables calling of talloc_report(NULL, stderr) when the
560 program exits. In Samba4 this is enabled by using the
561 --leak-report command line option.
564 For it to be useful, this function must be called before any
565 other talloc function as it establishes a "null context" that
566 acts as the top of the tree. If you don't call this function
567 first then passing NULL to talloc_report() or
568 talloc_report_full() won't give you the full tree printout.
571 Here is a typical talloc report:
573 <screen format="linespecific">talloc report on 'null_context' (total 267 bytes in 15 blocks)
574 libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
575 libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
576 iconv(UTF8,CP850) contains 42 bytes in 2 blocks
577 libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
578 iconv(CP850,UTF8) contains 42 bytes in 2 blocks
579 iconv(UTF8,UTF-16LE) contains 45 bytes in 2 blocks
580 iconv(UTF-16LE,UTF8) contains 45 bytes in 2 blocks
583 <refsect2 id="talloc_enable_leak_report_full"><title>void talloc_enable_leak_report_full(void);</title>
585 This enables calling of talloc_report_full(NULL, stderr) when the
586 program exits. In Samba4 this is enabled by using the
587 --leak-report-full command line option.
590 For it to be useful, this function must be called before any
591 other talloc function as it establishes a "null context" that
592 acts as the top of the tree. If you don't call this function
593 first then passing NULL to talloc_report() or
594 talloc_report_full() won't give you the full tree printout.
597 Here is a typical full report:
599 <screen format="linespecific">full talloc report on 'root' (total 18 bytes in 8 blocks)
600 p1 contains 18 bytes in 7 blocks (ref 0)
601 r1 contains 13 bytes in 2 blocks (ref 0)
603 p2 contains 1 bytes in 1 blocks (ref 1)
604 x3 contains 1 bytes in 1 blocks (ref 0)
605 x2 contains 1 bytes in 1 blocks (ref 0)
606 x1 contains 1 bytes in 1 blocks (ref 0)
609 <refsect2><title>(<emphasis role="italic">type</emphasis> *)talloc_zero(const void *<emphasis role="italic">ctx</emphasis>, <emphasis role="italic">type</emphasis>);</title>
611 The talloc_zero() macro is equivalent to:
613 <programlisting>ptr = talloc(ctx, type);
614 if (ptr) memset(ptr, 0, sizeof(type));</programlisting>
616 <refsect2><title>void *talloc_zero_size(const void *<emphasis role="italic">ctx</emphasis>, size_t <emphasis role="italic">size</emphasis>)</title>
618 The talloc_zero_size() function is useful when you don't have a
622 <refsect2><title>void *talloc_memdup(const void *<emphasis role="italic">ctx</emphasis>, const void *<emphasis role="italic">p</emphasis>, size_t size);</title>
624 The talloc_memdup() function is equivalent to:
626 <programlisting>ptr = talloc_size(ctx, size);
627 if (ptr) memcpy(ptr, p, size);</programlisting>
629 <refsect2><title>char *talloc_strdup(const void *<emphasis role="italic">ctx</emphasis>, const char *<emphasis role="italic">p</emphasis>);</title>
631 The talloc_strdup() function is equivalent to:
633 <programlisting>ptr = talloc_size(ctx, strlen(p)+1);
634 if (ptr) memcpy(ptr, p, strlen(p)+1);</programlisting>
636 This function sets the name of the new pointer to the passed
637 string. This is equivalent to:
639 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
641 <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>
643 The talloc_strndup() function is the talloc equivalent of the C
644 library function strndup(3).
647 This function sets the name of the new pointer to the passed
648 string. This is equivalent to:
650 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
652 <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>
654 The talloc_vasprintf() function is the talloc equivalent of the C
655 library function vasprintf(3).
658 This function sets the name of the new pointer to the new
659 string. This is equivalent to:
661 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
663 <refsect2><title>char *talloc_asprintf(const void *<emphasis role="italic">t</emphasis>, const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
665 The talloc_asprintf() function is the talloc equivalent of the C
666 library function asprintf(3).
669 This function sets the name of the new pointer to the passed
670 string. This is equivalent to:
672 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
674 <refsect2><title>char *talloc_asprintf_append(char *s, const char *fmt, ...);</title>
676 The talloc_asprintf_append() function appends the given formatted
677 string to the given string.
680 This function sets the name of the new pointer to the new
681 string. This is equivalent to:
683 <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
685 <refsect2><title>(type *)talloc_array(const void *ctx, type, unsigned int count);</title>
687 The talloc_array() macro is equivalent to:
689 <programlisting>(type *)talloc_size(ctx, sizeof(type) * count);</programlisting>
691 except that it provides integer overflow protection for the
692 multiply, returning NULL if the multiply overflows.
695 <refsect2><title>void *talloc_array_size(const void *ctx, size_t size, unsigned int count);</title>
697 The talloc_array_size() function is useful when the type is not
698 known. It operates in the same way as talloc_array(), but takes a
699 size instead of a type.
702 <refsect2><title>(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, unsigned int count);</title>
704 The talloc_ptrtype() macro should be used when you have a pointer to an array
705 and want to allocate memory of an array to point at with this pointer. When compiling
706 with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
707 and talloc_get_name() will return the current location in the source file.
711 <refsect2><title>void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size)</title>
713 This is a non-macro version of talloc_realloc(), which is useful
714 as libraries sometimes want a realloc function pointer. A
715 realloc(3) implementation encapsulates the functionality of
716 malloc(3), free(3) and realloc(3) in one call, which is why it is
717 useful to be able to pass around a single function pointer.
720 <refsect2><title>void *talloc_autofree_context(void);</title>
722 This is a handy utility function that returns a talloc context
723 which will be automatically freed on program exit. This can be
724 used to reduce the noise in memory leak reports.
727 <refsect2><title>void *talloc_check_name(const void *ptr, const char *name);</title>
729 This function checks if a pointer has the specified <emphasis
730 role="italic">name</emphasis>. If it does then the pointer is
731 returned. It it doesn't then NULL is returned.
734 <refsect2><title>(type *)talloc_get_type(const void *ptr, type);</title>
736 This macro allows you to do type checking on talloc pointers. It
737 is particularly useful for void* private pointers. It is
740 <programlisting>(type *)talloc_check_name(ptr, #type)</programlisting>
742 <refsect2><title>talloc_set_type(const void *ptr, type);</title>
744 This macro allows you to force the name of a pointer to be a
745 particular <emphasis>type</emphasis>. This can be
746 used in conjunction with talloc_get_type() to do type checking on
750 It is equivalent to this:
752 <programlisting>talloc_set_name_const(ptr, #type)</programlisting>
754 <refsect2><title>talloc_set_log_fn(void (*log_fn)(const char *message));</title>
756 This function sets a logging function that talloc will use for
757 warnings and errors. By default talloc will not print any warnings or
761 <refsect2><title>talloc_set_log_stderr(void);</title>
763 This sets the talloc log function to write log messages to stderr
767 <refsect1><title>PERFORMANCE</title>
769 All the additional features of talloc(3) over malloc(3) do come at a
770 price. We have a simple performance test in Samba4 that measures
771 talloc() versus malloc() performance, and it seems that talloc() is
772 about 10% slower than malloc() on my x86 Debian Linux box. For
773 Samba, the great reduction in code complexity that we get by using
774 talloc makes this worthwhile, especially as the total overhead of
775 talloc/malloc in Samba is already quite small.
778 <refsect1><title>SEE ALSO</title>
780 malloc(3), strndup(3), vasprintf(3), asprintf(3),
781 <ulink url="http://talloc.samba.org/"/>
785 <refsect1><title>AUTHOR</title>
786 <para> The original Samba software and related utilities were
787 created by Andrew Tridgell. Samba is now developed by the
788 Samba Team as an Open Source project similar to the way the
789 Linux kernel is developed.
793 <refsect1><title>COPYRIGHT/LICENSE</title>
795 Copyright (C) Andrew Tridgell 2004
798 This program is free software; you can redistribute it and/or modify
799 it under the terms of the GNU Lesser General Public License as
800 published by the Free Software Foundation; either version 3 of the
801 License, or (at your option) any later version.
804 This program is distributed in the hope that it will be useful, but
805 WITHOUT ANY WARRANTY; without even the implied warranty of
806 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
807 General Public License for more details.
810 You should have received a copy of the GNU General Public License
811 along with this program; if not, see http://www.gnu.org/licenses/.