dsdb: reset schema->{classes,attributes}_to_remove_size to 0
[Samba/gebeck_regimport.git] / lib / talloc / man / talloc.3.xml
blob553eaec7ea8c88f242d122734880a4f90d9ade63
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     <refmiscinfo class="source">Samba</refmiscinfo>
8     <refmiscinfo class="manual">System Administration tools</refmiscinfo>
9     <refmiscinfo class="version">4.0</refmiscinfo>
10   </refmeta>
11   <refnamediv>
12     <refname>talloc</refname>
13 <refpurpose>hierarchical reference counted memory pool system with destructors</refpurpose>
14   </refnamediv>
15   <refsynopsisdiv>
16 <synopsis>#include &lt;talloc.h&gt;</synopsis>
17   </refsynopsisdiv>
18   <refsect1><title>DESCRIPTION</title>
19     <para>
20       If you are used to talloc from Samba3 then please read this
21       carefully, as talloc has changed a lot.
22     </para>
23     <para>
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.
27     </para>
28     <para>
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:
33     </para>
34     <programlisting>
35     struct foo *X = talloc(mem_ctx, struct foo);
36     X->name = talloc_strdup(X, "foo");
37     </programlisting>
38     <para>
39       and the pointer <literal role="code">X-&gt;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-&gt;name</literal> are destroyed, and if
48       you do <literal
49       role="code">talloc_free(X-&gt;name)</literal> then just
50       the name element of <literal role="code">X</literal> is
51       destroyed.
52     </para>
53     <para>
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
56       talloc_free().
57     </para>
58     <para>
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.
64     </para>
65   </refsect1>
66   <refsect1><title>TALLOC API</title>
67     <para>
68       The following is a complete guide to the talloc API. Read it all at
69       least twice.
70     </para>
71     <refsect2><title>(type *)talloc(const void *ctx, type);</title>
72         <para>
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>.
78         </para>
79         <para>
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.
83         </para>
84         <para>
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.
89         </para>
90         <para>
91           The <emphasis role="italic">ctx</emphasis> argument to talloc()
92           can be NULL, in which case a new top level context is created.
93         </para>
94     </refsect2>
95     <refsect2><title>void *talloc_size(const void *ctx, size_t size);</title>
96         <para>
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
100           type checking.
101         </para>
102     </refsect2>
103     <refsect2><title>(typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);</title>
104         <para>
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.
109           and not the type.
110         </para>
111     </refsect2>
112     <refsect2><title>int talloc_free(void *ptr);</title>
113         <para>
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().
117         </para>
118         <para>
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.
126         </para>
127         <para>
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.
133         </para>
134         <para>
135           For more control on which parent is removed, see <link
136           linkend="talloc_unlink"><quote>talloc_unlink()</quote></link>.
137         </para>
138         <para>
139           talloc_free() operates recursively on its children.
140         </para>
141         <para>
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().
147         </para>
148         <para>
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:
152         </para>
153         <para>
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
158           </screen>
159         </para>
160         <para>
161           Please see the documentation for talloc_set_log_fn() and
162           talloc_set_log_stderr() for more information on talloc logging
163           functions.
164         </para>
165     </refsect2>
166     <refsect2 id="talloc_reference"><title>void *talloc_reference(const void *ctx, const void *ptr);</title>
167         <para>
168           The talloc_reference() function makes <emphasis
169           role="italic">ctx</emphasis> an additional parent of <emphasis
170           role="italic">ptr</emphasis>.
171         </para>
172         <para>
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).
178         </para>
179         <para>
180           If <emphasis role="italic">ptr</emphasis> is NULL, then the
181           function is a no-op, and simply returns NULL.
182         </para>
183         <para>
184           After creating a reference you can free it in one of the
185           following ways:
186         </para>
187       <para>
188         <itemizedlist>
189           <listitem>
190             <para>
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
194               parents.
195             </para>
196           </listitem>
197           <listitem>
198             <para>
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.
202             </para>
203           </listitem>
204         </itemizedlist>
205       </para>
206       <para>
207         For more control on which parent to remove, see <link
208         linkend="talloc_unlink"><quote>talloc_unlink()</quote></link>.
209       </para>
210     </refsect2>
211     <refsect2 id="talloc_unlink"><title>int talloc_unlink(const void *ctx, const void *ptr);</title>
212         <para>
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
217           parent of ptr.
218         </para>
219         <para>
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.
224         </para>
225         <para>
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.
229         </para>
230     </refsect2>
231     <refsect2 id="talloc_set_destructor"><title>void talloc_set_destructor(const void *ptr, int (*destructor)(void *));</title>
232         <para>
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.
240         </para>
241         <para>
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.
247         </para>
248         <para>
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.
252         </para>
253         <para>
254           To remove a destructor call talloc_set_destructor() with NULL for
255           the destructor.
256         </para>
257         <para>
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
262           about to go away.
263         </para>
264     </refsect2>
265     <refsect2><title>int talloc_increase_ref_count(const void *<emphasis role="italic">ptr</emphasis>);</title>
266         <para>
267           The talloc_increase_ref_count(<emphasis
268           role="italic">ptr</emphasis>) function is exactly equivalent to:
269         </para>
270         <programlisting>talloc_reference(NULL, ptr);</programlisting>
271         <para>
272           You can use either syntax, depending on which you think is
273           clearer in your code.
274         </para>
275         <para>
276           It returns 0 on success and -1 on failure.
277         </para>
278     </refsect2>
279     <refsect2><title>size_t talloc_reference_count(const void *<emphasis role="italic">ptr</emphasis>);</title>
280         <para>
281           Return the number of references to the pointer.
282         </para>
283     </refsect2>
284     <refsect2 id="talloc_set_name"><title>void talloc_set_name(const void *ptr, const char *fmt, ...);</title>
285         <para>
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
289           your code.
290         </para>
291         <para>
292           The main use for names on pointer is for "talloc reports".  See
293           <link
294           linkend="talloc_report"><quote>talloc_report_depth_cb()</quote></link>,
295           <link
296           linkend="talloc_report"><quote>talloc_report_depth_file()</quote></link>,
297           <link
298           linkend="talloc_report"><quote>talloc_report()</quote></link>
299           <link
300           linkend="talloc_report"><quote>talloc_report()</quote></link>
301           and <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>
305           and <link
306           linkend="talloc_enable_leak_report_full"><quote>talloc_enable_leak_report_full()</quote></link>.
307         </para>
308         <para>
309           The talloc_set_name() function allocates memory as a child of the
310           pointer.  It is logically equivalent to:
311         </para>
312         <programlisting>talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));</programlisting>
313         <para>
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().
317         </para>
318     </refsect2>
319     <refsect2><title>void talloc_set_name_const(const void *<emphasis role="italic">ptr</emphasis>, const char *<emphasis role="italic">name</emphasis>);</title>
320         <para>
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
324           as talloc_p().
325         </para>
326         <para>
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
332           with talloc_free().
333         </para>
334     </refsect2>
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>
336         <para>
337           The talloc_named() function creates a named talloc pointer.  It
338           is equivalent to:
339         </para>
340         <programlisting>ptr = talloc_size(ctx, size);
341 talloc_set_name(ptr, fmt, ....);</programlisting>
342     </refsect2>
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>
344         <para>
345           This is equivalent to:
346         </para>
347         <programlisting>ptr = talloc_size(ctx, size);
348 talloc_set_name_const(ptr, name);</programlisting>
349     </refsect2>
350     <refsect2><title>const char *talloc_get_name(const void *<emphasis role="italic">ptr</emphasis>);</title>
351         <para>
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>
355           for details.
356         </para>
357     </refsect2>
358     <refsect2><title>void *talloc_init(const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
359         <para>
360           This function creates a zero length named talloc context as a top
361           level context.  It is equivalent to:
362         </para>
363         <programlisting>talloc_named(NULL, 0, fmt, ...);</programlisting>
364     </refsect2>
365     <refsect2><title>void *talloc_new(void *<emphasis role="italic">ctx</emphasis>);</title>
366         <para>
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
371           working context.
372         </para>
373     </refsect2>
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>
375         <para>
376           The talloc_realloc() macro changes the size of a talloc pointer.
377           It has the following equivalences:
378         </para>
379         <programlisting>talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
380 talloc_realloc(ctx, ptr, type, 0)  ==> talloc_free(ptr);</programlisting>
381         <para>
382           The <emphasis role="italic">ctx</emphasis> argument is only used
383           if <emphasis role="italic">ptr</emphasis> is not NULL, otherwise
384           it is ignored.
385         </para>
386         <para>
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>).
391         </para>
392     </refsect2>
393     <refsect2><title>void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);</title>
394         <para>
395           the talloc_realloc_size() function is useful when the type is not
396           known so the type-safe talloc_realloc() cannot be used.
397         </para>
398     </refsect2>
399     <refsect2><title>TYPE *talloc_steal(const void *<emphasis role="italic">new_ctx</emphasis>, const TYPE *<emphasis role="italic">ptr</emphasis>);</title>
400         <para>
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.
405         </para>
406         <para>
407           The talloc_steal() function returns the pointer that you pass it.
408            It does not have any failure modes.
409         </para>
410         <para>
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
414           data if you do this.
415         </para>
416         <para>
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:
422         </para>
423         <para>
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
428           </screen>
429         </para>
430         <para>
431           To unambiguously change the parent of a pointer please see
432           the
433           function <link linkend="talloc_reference"><quote>talloc_reparent()</quote></link>. See
434           the talloc_set_log_fn() documentation for more information
435           on talloc logging.
436         </para>
437     </refsect2>
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>
439         <para>
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.
444         </para>
445         <para>
446           The talloc_reparent() function returns the pointer that you pass it. It
447           does not have any failure modes.
448         </para>
449         <para>
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.
453         </para>
454     </refsect2>
455     <refsect2><title>TYPE *talloc_move(const void *<emphasis role="italic">new_ctx</emphasis>, TYPE **<emphasis role="italic">ptr</emphasis>);</title>
456         <para>
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
462           new one.
463         </para>
464     </refsect2>
465     <refsect2><title>size_t talloc_total_size(const void *<emphasis role="italic">ptr</emphasis>);</title>
466         <para>
467           The talloc_total_size() function returns the total size in bytes
468           used by this pointer and all child pointers.  Mostly useful for
469           debugging.
470         </para>
471         <para>
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.
475         </para>
476     </refsect2>
477     <refsect2><title>size_t talloc_total_blocks(const void *<emphasis role="italic">ptr</emphasis>);</title>
478         <para>
479           The talloc_total_blocks() function returns the total memory block
480           count used by this pointer and all child pointers.  Mostly useful
481           for debugging.
482         </para>
483         <para>
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.
487         </para>
488     </refsect2>
489     <refsect2 id="talloc_report"><title>void talloc_report(const void *ptr, FILE *f);</title>
490         <para>
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.
495         </para>
496         <para>
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()
500           has been called.
501         </para>
502     </refsect2>
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>
504         <para>
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.
509         </para>
510         <para>
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()
514           has been called.
515         </para>
516     </refsect2>
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>
526         <para>
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.
531         </para>
532         <para>
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()
536           has been called.
537         </para>
538         <para>
539           The recursion is stopped when depth >= max_depth.
540           max_depth = -1 means only stop at leaf nodes.
541         </para>
542     </refsect2>
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>
551         <para>
552           This provides a more flexible reports than talloc_report(). It
553           will let you specify the depth and max_depth.
554         </para>
555     </refsect2>
556     <refsect2 id="talloc_enable_leak_report"><title>void talloc_enable_leak_report(void);</title>
557         <para>
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.
561         </para>
562         <para>
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.
568         </para>
569         <para>
570           Here is a typical talloc report:
571         </para>
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
580       </screen>
581     </refsect2>
582     <refsect2 id="talloc_enable_leak_report_full"><title>void talloc_enable_leak_report_full(void);</title>
583         <para>
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.
587         </para>
588         <para>
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.
594         </para>
595         <para>
596           Here is a typical full report:
597         </para>
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)
601         reference to: p2
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)
606       </screen>
607     </refsect2>
608     <refsect2><title>(<emphasis role="italic">type</emphasis> *)talloc_zero(const void *<emphasis role="italic">ctx</emphasis>, <emphasis role="italic">type</emphasis>);</title>
609         <para>
610           The talloc_zero() macro is equivalent to:
611         </para>
612         <programlisting>ptr = talloc(ctx, type);
613 if (ptr) memset(ptr, 0, sizeof(type));</programlisting>
614     </refsect2>
615     <refsect2><title>void *talloc_zero_size(const void *<emphasis role="italic">ctx</emphasis>, size_t <emphasis role="italic">size</emphasis>)</title>
616         <para>
617           The talloc_zero_size() function is useful when you don't have a
618           known type.
619         </para>
620     </refsect2>
621     <refsect2><title>void *talloc_memdup(const void *<emphasis role="italic">ctx</emphasis>, const void *<emphasis role="italic">p</emphasis>, size_t size);</title>
622         <para>
623           The talloc_memdup() function is equivalent to:
624         </para>
625         <programlisting>ptr = talloc_size(ctx, size);
626 if (ptr) memcpy(ptr, p, size);</programlisting>
627     </refsect2>
628     <refsect2><title>char *talloc_strdup(const void *<emphasis role="italic">ctx</emphasis>, const char *<emphasis role="italic">p</emphasis>);</title>
629         <para>
630           The talloc_strdup() function is equivalent to:
631         </para>
632         <programlisting>ptr = talloc_size(ctx, strlen(p)+1);
633 if (ptr) memcpy(ptr, p, strlen(p)+1);</programlisting>
634         <para>
635           This function sets the name of the new pointer to the passed
636           string. This is equivalent to:
637         </para>
638         <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
639     </refsect2>
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>
641         <para>
642           The talloc_strndup() function is the talloc equivalent of the C
643           library function strndup(3).
644         </para>
645         <para>
646           This function sets the name of the new pointer to the passed
647           string. This is equivalent to:
648         </para>
649         <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
650     </refsect2>
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>
652         <para>
653           The talloc_vasprintf() function is the talloc equivalent of the C
654           library function vasprintf(3).
655         </para>
656         <para>
657           This function sets the name of the new pointer to the new
658           string. This is equivalent to:
659         </para>
660         <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
661     </refsect2>
662     <refsect2><title>char *talloc_asprintf(const void *<emphasis role="italic">t</emphasis>, const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
663         <para>
664           The talloc_asprintf() function is the talloc equivalent of the C
665           library function asprintf(3).
666         </para>
667         <para>
668           This function sets the name of the new pointer to the passed
669           string. This is equivalent to:
670         </para>
671         <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
672     </refsect2>
673     <refsect2><title>char *talloc_asprintf_append(char *s, const char *fmt, ...);</title>
674         <para>
675           The talloc_asprintf_append() function appends the given formatted
676           string to the given string.
677         </para>
678         <para>
679           This function sets the name of the new pointer to the new
680           string. This is equivalent to:
681         </para>
682         <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
683     </refsect2>
684     <refsect2><title>(type *)talloc_array(const void *ctx, type, unsigned int count);</title>
685         <para>
686           The talloc_array() macro is equivalent to:
687         </para>
688         <programlisting>(type *)talloc_size(ctx, sizeof(type) * count);</programlisting>
689         <para>
690           except that it provides integer overflow protection for the
691           multiply, returning NULL if the multiply overflows.
692         </para>
693     </refsect2>
694     <refsect2><title>void *talloc_array_size(const void *ctx, size_t size, unsigned int count);</title>
695         <para>
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.
699         </para>
700     </refsect2>
701     <refsect2><title>(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, unsigned int count);</title>
702         <para>
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.
707           and not the type.
708         </para>
709     </refsect2>
710     <refsect2><title>void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size)</title>
711         <para>
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.
717         </para>
718     </refsect2>
719     <refsect2><title>void *talloc_autofree_context(void);</title>
720         <para>
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.
724         </para>
725     </refsect2>
726     <refsect2><title>void *talloc_check_name(const void *ptr, const char *name);</title>
727         <para>
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.
731         </para>
732     </refsect2>
733     <refsect2><title>(type *)talloc_get_type(const void *ptr, type);</title>
734         <para>
735           This macro allows you to do type checking on talloc pointers.  It
736           is particularly useful for void* private pointers.  It is
737           equivalent to this:
738         </para>
739         <programlisting>(type *)talloc_check_name(ptr, #type)</programlisting>
740     </refsect2>
741     <refsect2><title>talloc_set_type(const void *ptr, type);</title>
742         <para>
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
746           void* pointers.
747         </para>
748         <para>
749           It is equivalent to this:
750         </para>
751         <programlisting>talloc_set_name_const(ptr, #type)</programlisting>
752     </refsect2>
753     <refsect2><title>talloc_set_log_fn(void (*log_fn)(const char *message));</title>
754         <para>
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
757           errors.
758         </para>
759     </refsect2>
760     <refsect2><title>talloc_set_log_stderr(void);</title>
761         <para>
762           This sets the talloc log function to write log messages to stderr
763         </para>
764     </refsect2>
765   </refsect1>
766   <refsect1><title>PERFORMANCE</title>
767     <para>
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.
775     </para>
776   </refsect1>
777   <refsect1><title>SEE ALSO</title>
778     <para>
779       malloc(3), strndup(3), vasprintf(3), asprintf(3),
780       <ulink url="http://talloc.samba.org/"/>
781     </para>
782   </refsect1>
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.
789     </para>
790   </refsect1>
792   <refsect1><title>COPYRIGHT/LICENSE</title>
793     <para>
794       Copyright (C) Andrew Tridgell 2004
795     </para>
796     <para>
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.
801     </para>
802     <para>
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.
807     </para>
808     <para>
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/.
811     </para>
812   </refsect1>
813 </refentry>