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