1<?xml version="1.0"?>
2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
3<refentry>
4  <refmeta>
5    <refentrytitle>talloc</refentrytitle>
6    <manvolnum>3</manvolnum>
7  </refmeta>
8  <refnamediv>
9    <refname>talloc</refname>
10<refpurpose>hierarchical reference counted memory pool system with destructors</refpurpose>
11  </refnamediv>
12  <refsynopsisdiv>
13<synopsis>#include &lt;talloc/talloc.h&gt;</synopsis>
14  </refsynopsisdiv>
15  <refsect1><title>DESCRIPTION</title>
16    <para>
17      If you are used to talloc from Samba3 then please read this
18      carefully, as talloc has changed a lot.
19    </para>
20    <para>
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.
24    </para>
25    <para>
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:
30    </para>
31    <programlisting>
32    struct foo *X = talloc(mem_ctx, struct foo);
33    X->name = talloc_strdup(X, "foo");
34    </programlisting>
35    <para>
36      and the pointer <literal role="code">X-&gt;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-&gt;name</literal> are destroyed, and if
45      you do <literal
46      role="code">talloc_free(X-&gt;name)</literal> then just
47      the name element of <literal role="code">X</literal> is
48      destroyed.
49    </para>
50    <para>
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
53      talloc_free().
54    </para>
55    <para>
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.
61    </para>
62  </refsect1>
63  <refsect1><title>TALLOC API</title>
64    <para>
65      The following is a complete guide to the talloc API. Read it all at
66      least twice.
67    </para>
68    <refsect2><title>(type *)talloc(const void *ctx, type);</title>
69        <para>
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>.
75        </para>
76        <para>
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.
80        </para>
81        <para>
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.
86        </para>
87        <para>
88	  The <emphasis role="italic">ctx</emphasis> argument to talloc()
89	  can be NULL, in which case a new top level context is created.
90        </para>
91    </refsect2>
92    <refsect2><title>void *talloc_size(const void *ctx, size_t size);</title>
93        <para>
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
97	  type checking.
98        </para>
99    </refsect2>
100    <refsect2><title>(typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);</title>
101        <para>
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.
106	  and not the type.
107        </para>
108    </refsect2>
109    <refsect2><title>int talloc_free(void *ptr);</title>
110        <para>
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().
114        </para>
115        <para>
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.
123        </para>
124        <para>
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.
130        </para>
131        <para>
132	  For more control on which parent is removed, see <link
133	  linkend="talloc_unlink"><quote>talloc_unlink()</quote></link>.
134        </para>
135        <para>
136	  talloc_free() operates recursively on its children.
137        </para>
138    </refsect2>
139    <refsect2 id="talloc_reference"><title>void *talloc_reference(const void *ctx, const void *ptr);</title>
140        <para>
141	  The talloc_reference() function makes <emphasis
142	  role="italic">ctx</emphasis> an additional parent of <emphasis
143	  role="italic">ptr</emphasis>.
144        </para>
145        <para>
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).
151        </para>
152        <para>
153	  If <emphasis role="italic">ptr</emphasis> is NULL, then the
154	  function is a no-op, and simply returns NULL.
155        </para>
156        <para>
157	  After creating a reference you can free it in one of the
158	  following ways:
159        </para>
160      <para>
161        <itemizedlist>
162          <listitem>
163            <para>
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
167	      parents.
168            </para>
169          </listitem>
170          <listitem>
171            <para>
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.
175            </para>
176          </listitem>
177        </itemizedlist>
178      </para>
179      <para>
180	For more control on which parent to remove, see <link
181	linkend="talloc_unlink"><quote>talloc_unlink()</quote></link>.
182      </para>
183    </refsect2>
184    <refsect2 id="talloc_unlink"><title>int talloc_unlink(const void *ctx, const void *ptr);</title>
185        <para>
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
190	  parent of ptr.
191        </para>
192        <para>
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.
197        </para>
198        <para>
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.
202        </para>
203    </refsect2>
204    <refsect2 id="talloc_set_destructor"><title>void talloc_set_destructor(const void *ptr, int (*destructor)(void *));</title>
205        <para>
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.
213        </para>
214        <para>
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.
220        </para>
221        <para>
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.
225        </para>
226        <para>
227	  To remove a destructor call talloc_set_destructor() with NULL for
228	  the destructor.
229        </para>
230        <para>
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
235	  about to go away.
236        </para>
237    </refsect2>
238    <refsect2><title>int talloc_increase_ref_count(const void *<emphasis role="italic">ptr</emphasis>);</title>
239        <para>
240	  The talloc_increase_ref_count(<emphasis
241	  role="italic">ptr</emphasis>) function is exactly equivalent to:
242        </para>
243        <programlisting>talloc_reference(NULL, ptr);</programlisting>
244        <para>
245	  You can use either syntax, depending on which you think is
246	  clearer in your code.
247        </para>
248        <para>
249	  It returns 0 on success and -1 on failure.
250        </para>
251    </refsect2>
252    <refsect2><title>size_t talloc_reference_count(const void *<emphasis role="italic">ptr</emphasis>);</title>
253        <para>
254	  Return the number of references to the pointer.
255        </para>
256    </refsect2>
257    <refsect2 id="talloc_set_name"><title>void talloc_set_name(const void *ptr, const char *fmt, ...);</title>
258        <para>
259	  Each talloc pointer has a "name".  The name is used principally
260	  for debugging purposes, although it is also possible to set and
261	  get the name on a pointer in as a way of "marking" pointers in
262	  your code.
263        </para>
264        <para>
265	  The main use for names on pointer is for "talloc reports".  See
266	  <link
267	  linkend="talloc_report"><quote>talloc_report_depth_cb()</quote></link>,
268	  <link
269	  linkend="talloc_report"><quote>talloc_report_depth_file()</quote></link>,
270	  <link
271	  linkend="talloc_report"><quote>talloc_report()</quote></link>
272	  <link
273	  linkend="talloc_report"><quote>talloc_report()</quote></link>
274	  and <link
275	  linkend="talloc_report_full"><quote>talloc_report_full()</quote></link>
276	  for details.	Also see <link
277	  linkend="talloc_enable_leak_report"><quote>talloc_enable_leak_report()</quote></link>
278	  and <link
279	  linkend="talloc_enable_leak_report_full"><quote>talloc_enable_leak_report_full()</quote></link>.
280        </para>
281        <para>
282	  The talloc_set_name() function allocates memory as a child of the
283	  pointer.  It is logically equivalent to:
284        </para>
285        <programlisting>talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));</programlisting>
286        <para>
287	  Note that multiple calls to talloc_set_name() will allocate more
288	  memory without releasing the name.  All of the memory is released
289	  when the ptr is freed using talloc_free().
290        </para>
291    </refsect2>
292    <refsect2><title>void talloc_set_name_const(const void *<emphasis role="italic">ptr</emphasis>, const char *<emphasis role="italic">name</emphasis>);</title>
293        <para>
294	  The function talloc_set_name_const() is just like
295	  talloc_set_name(), but it takes a string constant, and is much
296	  faster.  It is extensively used by the "auto naming" macros, such
297	  as talloc_p().
298        </para>
299        <para>
300	  This function does not allocate any memory.  It just copies the
301	  supplied pointer into the internal representation of the talloc
302	  ptr. This means you must not pass a <emphasis
303	  role="italic">name</emphasis> pointer to memory that will
304	  disappear before <emphasis role="italic">ptr</emphasis> is freed
305	  with talloc_free().
306        </para>
307    </refsect2>
308    <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>
309        <para>
310	  The talloc_named() function creates a named talloc pointer.  It
311	  is equivalent to:
312        </para>
313        <programlisting>ptr = talloc_size(ctx, size);
314talloc_set_name(ptr, fmt, ....);</programlisting>
315    </refsect2>
316    <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>
317        <para>
318	  This is equivalent to:
319        </para>
320        <programlisting>ptr = talloc_size(ctx, size);
321talloc_set_name_const(ptr, name);</programlisting>
322    </refsect2>
323    <refsect2><title>const char *talloc_get_name(const void *<emphasis role="italic">ptr</emphasis>);</title>
324        <para>
325	  This returns the current name for the given talloc pointer,
326	  <emphasis role="italic">ptr</emphasis>. See <link
327	  linkend="talloc_set_name"><quote>talloc_set_name()</quote></link>
328	  for details.
329        </para>
330    </refsect2>
331    <refsect2><title>void *talloc_init(const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
332        <para>
333	  This function creates a zero length named talloc context as a top
334	  level context.  It is equivalent to:
335        </para>
336        <programlisting>talloc_named(NULL, 0, fmt, ...);</programlisting>
337    </refsect2>
338    <refsect2><title>void *talloc_new(void *<emphasis role="italic">ctx</emphasis>);</title>
339        <para>
340	  This is a utility macro that creates a new memory context hanging
341	  off an exiting context, automatically naming it "talloc_new:
342	  __location__" where __location__ is the source line it is called
343	  from.  It is particularly useful for creating a new temporary
344	  working context.
345        </para>
346    </refsect2>
347    <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>
348        <para>
349	  The talloc_realloc() macro changes the size of a talloc pointer. 
350	  It has the following equivalences:
351        </para>
352        <programlisting>talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
353talloc_realloc(ctx, ptr, type, 0)  ==> talloc_free(ptr);</programlisting>
354        <para>
355	  The <emphasis role="italic">ctx</emphasis> argument is only used
356	  if <emphasis role="italic">ptr</emphasis> is not NULL, otherwise
357	  it is ignored.
358        </para>
359        <para>
360	  talloc_realloc() returns the new pointer, or NULL on failure. 
361	  The call will fail either due to a lack of memory, or because the
362	  pointer has more than one parent (see <link
363	  linkend="talloc_reference"><quote>talloc_reference()</quote></link>).
364        </para>
365    </refsect2>
366    <refsect2><title>void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);</title>
367        <para>
368	  the talloc_realloc_size() function is useful when the type is not
369	  known so the type-safe talloc_realloc() cannot be used.
370        </para>
371    </refsect2>
372    <refsect2><title>TYPE *talloc_steal(const void *<emphasis role="italic">new_ctx</emphasis>, const TYPE *<emphasis role="italic">ptr</emphasis>);</title>
373        <para>
374	  The talloc_steal() function changes the parent context of a
375	  talloc pointer.  It is typically used when the context that the
376	  pointer is currently a child of is going to be freed and you wish
377	  to keep the memory for a longer time.
378        </para>
379        <para>
380	  The talloc_steal() function returns the pointer that you pass it.
381	   It does not have any failure modes.
382        </para>
383        <para>
384	  NOTE: It is possible to produce loops in the parent/child
385	  relationship if you are not careful with talloc_steal().  No
386	  guarantees are provided as to your sanity or the safety of your
387	  data if you do this.
388        </para>
389    </refsect2>
390    <refsect2><title>TYPE *talloc_move(const void *<emphasis role="italic">new_ctx</emphasis>, TYPE **<emphasis role="italic">ptr</emphasis>);</title>
391        <para>
392	  The talloc_move() function is a wrapper around
393	  talloc_steal() which zeros the source pointer after the
394	  move. This avoids a potential source of bugs where a
395	  programmer leaves a pointer in two structures, and uses the
396	  pointer from the old structure after it has been moved to a
397	  new one.
398        </para>
399    </refsect2>
400    <refsect2><title>size_t talloc_total_size(const void *<emphasis role="italic">ptr</emphasis>);</title>
401        <para>
402	  The talloc_total_size() function returns the total size in bytes
403	  used by this pointer and all child pointers.	Mostly useful for
404	  debugging.
405        </para>
406        <para>
407	  Passing NULL is allowed, but it will only give a meaningful
408	  result if talloc_enable_leak_report() or
409	  talloc_enable_leak_report_full() has been called.
410        </para>
411    </refsect2>
412    <refsect2><title>size_t talloc_total_blocks(const void *<emphasis role="italic">ptr</emphasis>);</title>
413        <para>
414	  The talloc_total_blocks() function returns the total memory block
415	  count used by this pointer and all child pointers.  Mostly useful
416	  for debugging.
417        </para>
418        <para>
419	  Passing NULL is allowed, but it will only give a meaningful
420	  result if talloc_enable_leak_report() or
421	  talloc_enable_leak_report_full() has been called.
422        </para>
423    </refsect2>
424    <refsect2 id="talloc_report"><title>void talloc_report(const void *ptr, FILE *f);</title>
425        <para>
426	  The talloc_report() function prints a summary report of all
427	  memory used by <emphasis role="italic">ptr</emphasis>.  One line
428	  of report is printed for each immediate child of ptr, showing the
429	  total memory and number of blocks used by that child.
430        </para>
431        <para>
432	  You can pass NULL for the pointer, in which case a report is
433	  printed for the top level memory context, but only if
434	  talloc_enable_leak_report() or talloc_enable_leak_report_full()
435	  has been called.
436        </para>
437    </refsect2>
438    <refsect2 id="talloc_report_full"><title>void talloc_report_full(const void *<emphasis role="italic">ptr</emphasis>, FILE *<emphasis role="italic">f</emphasis>);</title>
439        <para>
440	  This provides a more detailed report than talloc_report().  It
441	  will recursively print the entire tree of memory referenced by
442	  the pointer. References in the tree are shown by giving the name
443	  of the pointer that is referenced.
444        </para>
445        <para>
446	  You can pass NULL for the pointer, in which case a report is
447	  printed for the top level memory context, but only if
448	  talloc_enable_leak_report() or talloc_enable_leak_report_full()
449	  has been called.
450        </para>
451    </refsect2>
452    <refsect2 id="talloc_report_depth_cb">
453     <funcsynopsis><funcprototype>
454      <funcdef>void <function>talloc_report_depth_cb</function></funcdef>
455      <paramdef><parameter>const void *ptr</parameter></paramdef>
456      <paramdef><parameter>int depth</parameter></paramdef>
457      <paramdef><parameter>int max_depth</parameter></paramdef>
458      <paramdef><parameter>void (*callback)(const void *ptr, int depth, int max_depth, int is_ref, void *priv)</parameter></paramdef>
459      <paramdef><parameter>void *priv</parameter></paramdef>
460     </funcprototype></funcsynopsis>
461        <para>
462	  This provides a more flexible reports than talloc_report(). It
463	  will recursively call the callback for the entire tree of memory
464	  referenced by the pointer. References in the tree are passed with
465	  <emphasis role="italic">is_ref = 1</emphasis> and the pointer that is referenced.
466        </para>
467        <para>
468	  You can pass NULL for the pointer, in which case a report is
469	  printed for the top level memory context, but only if
470	  talloc_enable_leak_report() or talloc_enable_leak_report_full()
471	  has been called.
472        </para>
473        <para>
474	  The recursion is stopped when depth >= max_depth.
475	  max_depth = -1 means only stop at leaf nodes.
476        </para>
477    </refsect2>
478    <refsect2 id="talloc_report_depth_file">
479     <funcsynopsis><funcprototype>
480      <funcdef>void <function>talloc_report_depth_file</function></funcdef>
481      <paramdef><parameter>const void *ptr</parameter></paramdef>
482      <paramdef><parameter>int depth</parameter></paramdef>
483      <paramdef><parameter>int max_depth</parameter></paramdef>
484      <paramdef><parameter>FILE *f</parameter></paramdef>
485     </funcprototype></funcsynopsis>
486        <para>
487	  This provides a more flexible reports than talloc_report(). It
488	  will let you specify the depth and max_depth.
489        </para>
490    </refsect2>
491    <refsect2 id="talloc_enable_leak_report"><title>void talloc_enable_leak_report(void);</title>
492        <para>
493	  This enables calling of talloc_report(NULL, stderr) when the
494	  program exits.  In Samba4 this is enabled by using the
495	  --leak-report command line option.
496        </para>
497        <para>
498	  For it to be useful, this function must be called before any
499	  other talloc function as it establishes a "null context" that
500	  acts as the top of the tree.	If you don't call this function
501	  first then passing NULL to talloc_report() or
502	  talloc_report_full() won't give you the full tree printout.
503        </para>
504        <para>
505	  Here is a typical talloc report:
506        </para>
507        <screen format="linespecific">talloc report on 'null_context' (total 267 bytes in 15 blocks)
508libcli/auth/spnego_parse.c:55  contains   31 bytes in   2 blocks
509libcli/auth/spnego_parse.c:55  contains   31 bytes in   2 blocks
510iconv(UTF8,CP850)              contains   42 bytes in   2 blocks
511libcli/auth/spnego_parse.c:55  contains   31 bytes in   2 blocks
512iconv(CP850,UTF8)              contains   42 bytes in   2 blocks
513iconv(UTF8,UTF-16LE)           contains   45 bytes in   2 blocks
514iconv(UTF-16LE,UTF8)           contains   45 bytes in   2 blocks
515      </screen>
516    </refsect2>
517    <refsect2 id="talloc_enable_leak_report_full"><title>void talloc_enable_leak_report_full(void);</title>
518        <para>
519	  This enables calling of talloc_report_full(NULL, stderr) when the
520	  program exits.  In Samba4 this is enabled by using the
521	  --leak-report-full command line option.
522        </para>
523        <para>
524	  For it to be useful, this function must be called before any
525	  other talloc function as it establishes a "null context" that
526	  acts as the top of the tree.	If you don't call this function
527	  first then passing NULL to talloc_report() or
528	  talloc_report_full() won't give you the full tree printout.
529        </para>
530        <para>
531	  Here is a typical full report:
532        </para>
533        <screen format="linespecific">full talloc report on 'root' (total 18 bytes in 8 blocks)
534p1               contains     18 bytes in   7 blocks (ref 0)
535    r1               contains     13 bytes in   2 blocks (ref 0)
536        reference to: p2
537    p2               contains      1 bytes in   1 blocks (ref 1)
538    x3               contains      1 bytes in   1 blocks (ref 0)
539    x2               contains      1 bytes in   1 blocks (ref 0)
540    x1               contains      1 bytes in   1 blocks (ref 0)
541      </screen>
542    </refsect2>
543    <refsect2><title>(<emphasis role="italic">type</emphasis> *)talloc_zero(const void *<emphasis role="italic">ctx</emphasis>, <emphasis role="italic">type</emphasis>);</title>
544        <para>
545	  The talloc_zero() macro is equivalent to:
546        </para>
547        <programlisting>ptr = talloc(ctx, type);
548if (ptr) memset(ptr, 0, sizeof(type));</programlisting>
549    </refsect2>
550    <refsect2><title>void *talloc_zero_size(const void *<emphasis role="italic">ctx</emphasis>, size_t <emphasis role="italic">size</emphasis>)</title>
551        <para>
552	  The talloc_zero_size() function is useful when you don't have a
553	  known type.
554        </para>
555    </refsect2>
556    <refsect2><title>void *talloc_memdup(const void *<emphasis role="italic">ctx</emphasis>, const void *<emphasis role="italic">p</emphasis>, size_t size);</title>
557        <para>
558	  The talloc_memdup() function is equivalent to:
559        </para>
560        <programlisting>ptr = talloc_size(ctx, size);
561if (ptr) memcpy(ptr, p, size);</programlisting>
562    </refsect2>
563    <refsect2><title>char *talloc_strdup(const void *<emphasis role="italic">ctx</emphasis>, const char *<emphasis role="italic">p</emphasis>);</title>
564        <para>
565	  The talloc_strdup() function is equivalent to:
566        </para>
567        <programlisting>ptr = talloc_size(ctx, strlen(p)+1);
568if (ptr) memcpy(ptr, p, strlen(p)+1);</programlisting>
569        <para>
570	  This function sets the name of the new pointer to the passed
571	  string. This is equivalent to:
572        </para>
573        <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
574    </refsect2>
575    <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>
576        <para>
577	  The talloc_strndup() function is the talloc equivalent of the C
578	  library function strndup(3).
579        </para>
580        <para>
581	  This function sets the name of the new pointer to the passed
582	  string. This is equivalent to:
583        </para>
584        <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
585    </refsect2>
586    <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>
587        <para>
588	  The talloc_vasprintf() function is the talloc equivalent of the C
589	  library function vasprintf(3).
590        </para>
591    </refsect2>
592    <refsect2><title>char *talloc_asprintf(const void *<emphasis role="italic">t</emphasis>, const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
593        <para>
594	  The talloc_asprintf() function is the talloc equivalent of the C
595	  library function asprintf(3).
596        </para>
597        <para>
598	  This function sets the name of the new pointer to the passed
599	  string. This is equivalent to:
600        </para>
601        <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
602    </refsect2>
603    <refsect2><title>char *talloc_asprintf_append(char *s, const char *fmt, ...);</title>
604        <para>
605	  The talloc_asprintf_append() function appends the given formatted
606	  string to the given string.
607        </para>
608    </refsect2>
609    <refsect2><title>(type *)talloc_array(const void *ctx, type, uint_t count);</title>
610        <para>
611	  The talloc_array() macro is equivalent to:
612        </para>
613        <programlisting>(type *)talloc_size(ctx, sizeof(type) * count);</programlisting>
614        <para>
615	  except that it provides integer overflow protection for the
616	  multiply, returning NULL if the multiply overflows.
617        </para>
618    </refsect2>
619    <refsect2><title>void *talloc_array_size(const void *ctx, size_t size, uint_t count);</title>
620        <para>
621	  The talloc_array_size() function is useful when the type is not
622	  known. It operates in the same way as talloc_array(), but takes a
623	  size instead of a type.
624        </para>
625    </refsect2>
626    <refsect2><title>(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, uint_t count);</title>
627        <para>
628	  The talloc_ptrtype() macro should be used when you have a pointer to an array
629	  and want to allocate memory of an array to point at with this pointer. When compiling
630	  with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
631	  and talloc_get_name() will return the current location in the source file.
632	  and not the type.
633        </para>
634    </refsect2>
635    <refsect2><title>void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size)</title>
636        <para>
637	  This is a non-macro version of talloc_realloc(), which is useful
638	  as libraries sometimes want a realloc function pointer.  A
639	  realloc(3) implementation encapsulates the functionality of
640	  malloc(3), free(3) and realloc(3) in one call, which is why it is
641	  useful to be able to pass around a single function pointer.
642        </para>
643    </refsect2>
644    <refsect2><title>void *talloc_autofree_context(void);</title>
645        <para>
646	  This is a handy utility function that returns a talloc context
647	  which will be automatically freed on program exit.  This can be
648	  used to reduce the noise in memory leak reports.
649        </para>
650    </refsect2>
651    <refsect2><title>void *talloc_check_name(const void *ptr, const char *name);</title>
652        <para>
653	  This function checks if a pointer has the specified <emphasis
654	  role="italic">name</emphasis>.  If it does then the pointer is
655	  returned.  It it doesn't then NULL is returned.
656        </para>
657    </refsect2>
658    <refsect2><title>(type *)talloc_get_type(const void *ptr, type);</title>
659        <para>
660	  This macro allows you to do type checking on talloc pointers.  It
661	  is particularly useful for void* private pointers.  It is
662	  equivalent to this:
663        </para>
664        <programlisting>(type *)talloc_check_name(ptr, #type)</programlisting>
665    </refsect2>
666    <refsect2><title>talloc_set_type(const void *ptr, type);</title>
667        <para>
668	  This macro allows you to force the name of a pointer to be a
669	  particular <emphasis>type</emphasis>.  This can be
670	  used in conjunction with talloc_get_type() to do type checking on
671	  void* pointers.
672        </para>
673        <para>
674	  It is equivalent to this:
675        </para>
676        <programlisting>talloc_set_name_const(ptr, #type)</programlisting>
677    </refsect2>
678  </refsect1>
679  <refsect1><title>PERFORMANCE</title>
680    <para>
681      All the additional features of talloc(3) over malloc(3) do come at a
682      price.  We have a simple performance test in Samba4 that measures
683      talloc() versus malloc() performance, and it seems that talloc() is
684      about 10% slower than malloc() on my x86 Debian Linux box.  For
685      Samba, the great reduction in code complexity that we get by using
686      talloc makes this worthwhile, especially as the total overhead of
687      talloc/malloc in Samba is already quite small.
688    </para>
689  </refsect1>
690  <refsect1><title>SEE ALSO</title>
691    <para>
692      malloc(3), strndup(3), vasprintf(3), asprintf(3), 
693      <ulink url="http://talloc.samba.org/"/>
694    </para>
695  </refsect1>
696  <refsect1><title>COPYRIGHT/LICENSE</title>
697    <para>
698      Copyright (C) Andrew Tridgell 2004
699    </para>
700    <para>
701      This program is free software; you can redistribute it and/or modify
702      it under the terms of the GNU General Public License as published by
703      the Free Software Foundation; either version 2 of the License, or (at
704      your option) any later version.
705    </para>
706    <para>
707      This program is distributed in the hope that it will be useful, but
708      WITHOUT ANY WARRANTY; without even the implied warranty of
709      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
710      General Public License for more details.
711    </para>
712    <para>
713      You should have received a copy of the GNU General Public License
714      along with this program; if not, write to the Free Software
715      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
716    </para>
717  </refsect1>
718</refentry>
719