1TALLOC(3)                       [FIXME: manual]                      TALLOC(3)
2
3
4

NAME

6       talloc - hierarchical reference counted memory pool system with
7       destructors
8

SYNOPSIS

10       #include <talloc/talloc.h>
11

DESCRIPTION

13       If you are used to talloc from Samba3 then please read this carefully,
14       as talloc has changed a lot.
15
16       The new talloc is a hierarchical, reference counted memory pool system
17       with destructors. Quite a mouthful really, but not too bad once you get
18       used to it.
19
20       Perhaps the biggest change from Samba3 is that there is no distinction
21       between a "talloc context" and a "talloc pointer". Any pointer returned
22       from talloc() is itself a valid talloc context. This means you can do
23       this:
24
25               struct foo *X = talloc(mem_ctx, struct foo);
26               X->name = talloc_strdup(X, "foo");
27
28
29       and the pointer X->name would be a "child" of the talloc context X
30       which is itself a child of mem_ctx. So if you do talloc_free(mem_ctx)
31       then it is all destroyed, whereas if you do talloc_free(X) then just X
32       and X->name are destroyed, and if you do talloc_free(X->name) then just
33       the name element of X is destroyed.
34
35       If you think about this, then what this effectively gives you is an
36       n-ary tree, where you can free any part of the tree with talloc_free().
37
38       If you find this confusing, then I suggest you run the testsuite
39       program to watch talloc in action. You may also like to add your own
40       tests to testsuite.c to clarify how some particular situation is
41       handled.
42

TALLOC API

44       The following is a complete guide to the talloc API. Read it all at
45       least twice.
46
47   (type *)talloc(const void *ctx, type);
48       The talloc() macro is the core of the talloc library. It takes a memory
49       ctx and a type, and returns a pointer to a new area of memory of the
50       given type.
51
52       The returned pointer is itself a talloc context, so you can use it as
53       the ctx argument to more calls to talloc() if you wish.
54
55       The returned pointer is a "child" of the supplied context. This means
56       that if you talloc_free() the ctx then the new child disappears as
57       well. Alternatively you can free just the child.
58
59       The ctx argument to talloc() can be NULL, in which case a new top level
60       context is created.
61
62   void *talloc_size(const void *ctx, size_t size);
63       The function talloc_size() should be used when you don´t have a
64       convenient type to pass to talloc(). Unlike talloc(), it is not type
65       safe (as it returns a void *), so you are on your own for type
66       checking.
67
68   (typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);
69       The talloc_ptrtype() macro should be used when you have a pointer and
70       want to allocate memory to point at with this pointer. When compiling
71       with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()
72       and talloc_get_name() will return the current location in the source
73       file. and not the type.
74
75   int talloc_free(void *ptr);
76       The talloc_free() function frees a piece of talloc memory, and all its
77       children. You can call talloc_free() on any pointer returned by
78       talloc().
79
80       The return value of talloc_free() indicates success or failure, with 0
81       returned for success and -1 for failure. The only possible failure
82       condition is if ptr had a destructor attached to it and the destructor
83       returned -1. See “talloc_set_destructor()” for details on destructors.
84
85       If this pointer has an additional parent when talloc_free() is called
86       then the memory is not actually released, but instead the most recently
87       established parent is destroyed. See “talloc_reference()” for details
88       on establishing additional parents.
89
90       For more control on which parent is removed, see “talloc_unlink()”.
91
92       talloc_free() operates recursively on its children.
93
94       From the 2.0 version of talloc, as a special case, talloc_free() is
95       refused on pointers that have more than one parent, as talloc would
96       have no way of knowing which parent should be removed. To free a
97       pointer that has more than one parent please use talloc_unlink().
98
99       To help you find problems in your code caused by this behaviour, if you
100       do try and free a pointer with more than one parent then the talloc
101       logging function will be called to give output like this:
102
103                    ERROR: talloc_free with references at some_dir/source/foo.c:123
104                     reference at some_dir/source/other.c:325
105                     reference at some_dir/source/third.c:121
106
107
108       Please see the documentation for talloc_set_log_fn() and
109       talloc_set_log_stderr() for more information on talloc logging
110       functions.
111
112   void *talloc_reference(const void *ctx, const void *ptr);
113       The talloc_reference() function makes ctx an additional parent of ptr.
114
115       The return value of talloc_reference() is always the original pointer
116       ptr, unless talloc ran out of memory in creating the reference in which
117       case it will return NULL (each additional reference consumes around 48
118       bytes of memory on intel x86 platforms).
119
120       If ptr is NULL, then the function is a no-op, and simply returns NULL.
121
122       After creating a reference you can free it in one of the following
123       ways:
124
125       ·   you can talloc_free() any parent of the original pointer. That will
126           reduce the number of parents of this pointer by 1, and will cause
127           this pointer to be freed if it runs out of parents.
128
129       ·   you can talloc_free() the pointer itself. That will destroy the
130           most recently established parent to the pointer and leave the
131           pointer as a child of its current parent.
132
133
134       For more control on which parent to remove, see “talloc_unlink()”.
135
136   int talloc_unlink(const void *ctx, const void *ptr);
137       The talloc_unlink() function removes a specific parent from ptr. The
138       ctx passed must either be a context used in talloc_reference() with
139       this pointer, or must be a direct parent of ptr.
140
141       Note that if the parent has already been removed using talloc_free()
142       then this function will fail and will return -1. Likewise, if ptr is
143       NULL, then the function will make no modifications and return -1.
144
145       Usually you can just use talloc_free() instead of talloc_unlink(), but
146       sometimes it is useful to have the additional control on which parent
147       is removed.
148
149   void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
150       The function talloc_set_destructor() sets the destructor for the
151       pointer ptr. A destructor is a function that is called when the memory
152       used by a pointer is about to be released. The destructor receives ptr
153       as an argument, and should return 0 for success and -1 for failure.
154
155       The destructor can do anything it wants to, including freeing other
156       pieces of memory. A common use for destructors is to clean up operating
157       system resources (such as open file descriptors) contained in the
158       structure the destructor is placed on.
159
160       You can only place one destructor on a pointer. If you need more than
161       one destructor then you can create a zero-length child of the pointer
162       and place an additional destructor on that.
163
164       To remove a destructor call talloc_set_destructor() with NULL for the
165       destructor.
166
167       If your destructor attempts to talloc_free() the pointer that it is the
168       destructor for then talloc_free() will return -1 and the free will be
169       ignored. This would be a pointless operation anyway, as the destructor
170       is only called when the memory is just about to go away.
171
172   int talloc_increase_ref_count(const void *ptr);
173       The talloc_increase_ref_count(ptr) function is exactly equivalent to:
174
175           talloc_reference(NULL, ptr);
176
177       You can use either syntax, depending on which you think is clearer in
178       your code.
179
180       It returns 0 on success and -1 on failure.
181
182   size_t talloc_reference_count(const void *ptr);
183       Return the number of references to the pointer.
184
185   void talloc_set_name(const void *ptr, const char *fmt, ...);
186       Each talloc pointer has a "name". The name is used principally for
187       debugging purposes, although it is also possible to set and get the
188       name on a pointer in as a way of "marking" pointers in your code.
189
190       The main use for names on pointer is for "talloc reports". See
191       “talloc_report_depth_cb()”, “talloc_report_depth_file()”,
192       “talloc_report()” “talloc_report()” and “talloc_report_full()” for
193       details. Also see “talloc_enable_leak_report()” and
194       “talloc_enable_leak_report_full()”.
195
196       The talloc_set_name() function allocates memory as a child of the
197       pointer. It is logically equivalent to:
198
199           talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
200
201       Note that multiple calls to talloc_set_name() will allocate more memory
202       without releasing the name. All of the memory is released when the ptr
203       is freed using talloc_free().
204
205   void talloc_set_name_const(const void *ptr, const char *name);
206       The function talloc_set_name_const() is just like talloc_set_name(),
207       but it takes a string constant, and is much faster. It is extensively
208       used by the "auto naming" macros, such as talloc_p().
209
210       This function does not allocate any memory. It just copies the supplied
211       pointer into the internal representation of the talloc ptr. This means
212       you must not pass a name pointer to memory that will disappear before
213       ptr is freed with talloc_free().
214
215   void *talloc_named(const void *ctx, size_t size, const char *fmt, ...);
216       The talloc_named() function creates a named talloc pointer. It is
217       equivalent to:
218
219           ptr = talloc_size(ctx, size);
220           talloc_set_name(ptr, fmt, ....);
221
222   void *talloc_named_const(const void *ctx, size_t size, const char *name);
223       This is equivalent to:
224
225           ptr = talloc_size(ctx, size);
226           talloc_set_name_const(ptr, name);
227
228   const char *talloc_get_name(const void *ptr);
229       This returns the current name for the given talloc pointer, ptr. See
230       “talloc_set_name()” for details.
231
232   void *talloc_init(const char *fmt, ...);
233       This function creates a zero length named talloc context as a top level
234       context. It is equivalent to:
235
236           talloc_named(NULL, 0, fmt, ...);
237
238   void *talloc_new(void *ctx);
239       This is a utility macro that creates a new memory context hanging off
240       an exiting context, automatically naming it "talloc_new: __location__"
241       where __location__ is the source line it is called from. It is
242       particularly useful for creating a new temporary working context.
243
244   (type *)talloc_realloc(const void *ctx, void *ptr, type, count);
245       The talloc_realloc() macro changes the size of a talloc pointer. It has
246       the following equivalences:
247
248           talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
249           talloc_realloc(ctx, ptr, type, 0)  ==> talloc_free(ptr);
250
251       The ctx argument is only used if ptr is not NULL, otherwise it is
252       ignored.
253
254       talloc_realloc() returns the new pointer, or NULL on failure. The call
255       will fail either due to a lack of memory, or because the pointer has
256       more than one parent (see “talloc_reference()”).
257
258   void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);
259       the talloc_realloc_size() function is useful when the type is not known
260       so the type-safe talloc_realloc() cannot be used.
261
262   TYPE *talloc_steal(const void *new_ctx, const TYPE *ptr);
263       The talloc_steal() function changes the parent context of a talloc
264       pointer. It is typically used when the context that the pointer is
265       currently a child of is going to be freed and you wish to keep the
266       memory for a longer time.
267
268       The talloc_steal() function returns the pointer that you pass it. It
269       does not have any failure modes.
270
271       It is possible to produce loops in the parent/child relationship if you
272       are not careful with talloc_steal(). No guarantees are provided as to
273       your sanity or the safety of your data if you do this.
274
275       Note that if you try and call talloc_steal() on a pointer that has more
276       than one parent then the result is ambiguous. Talloc will choose to
277       remove the parent that is currently indicated by talloc_parent() and
278       replace it with the chosen parent. You will also get a message like
279       this via the talloc logging functions:
280
281                  WARNING: talloc_steal with references at some_dir/source/foo.c:123
282                     reference at some_dir/source/other.c:325
283                     reference at some_dir/source/third.c:121
284
285
286       To unambiguously change the parent of a pointer please see the function
287       “talloc_reparent()”. See the talloc_set_log_fn() documentation for more
288       information on talloc logging.
289
290   TYPE *talloc_reparent(const void *old_parent, const void *new_parent, const
291       TYPE *ptr);
292       The talloc_reparent() function changes the parent context of a talloc
293       pointer. It is typically used when the context that the pointer is
294       currently a child of is going to be freed and you wish to keep the
295       memory for a longer time.
296
297       The talloc_reparent() function returns the pointer that you pass it. It
298       does not have any failure modes.
299
300       The difference between talloc_reparent() and talloc_steal() is that
301       talloc_reparent() can specify which parent you wish to change. This is
302       useful when a pointer has multiple parents via references.
303
304   TYPE *talloc_move(const void *new_ctx, TYPE **ptr);
305       The talloc_move() function is a wrapper around talloc_steal() which
306       zeros the source pointer after the move. This avoids a potential source
307       of bugs where a programmer leaves a pointer in two structures, and uses
308       the pointer from the old structure after it has been moved to a new
309       one.
310
311   size_t talloc_total_size(const void *ptr);
312       The talloc_total_size() function returns the total size in bytes used
313       by this pointer and all child pointers. Mostly useful for debugging.
314
315       Passing NULL is allowed, but it will only give a meaningful result if
316       talloc_enable_leak_report() or talloc_enable_leak_report_full() has
317       been called.
318
319   size_t talloc_total_blocks(const void *ptr);
320       The talloc_total_blocks() function returns the total memory block count
321       used by this pointer and all child pointers. Mostly useful for
322       debugging.
323
324       Passing NULL is allowed, but it will only give a meaningful result if
325       talloc_enable_leak_report() or talloc_enable_leak_report_full() has
326       been called.
327
328   void talloc_report(const void *ptr, FILE *f);
329       The talloc_report() function prints a summary report of all memory used
330       by ptr. One line of report is printed for each immediate child of ptr,
331       showing the total memory and number of blocks used by that child.
332
333       You can pass NULL for the pointer, in which case a report is printed
334       for the top level memory context, but only if
335       talloc_enable_leak_report() or talloc_enable_leak_report_full() has
336       been called.
337
338   void talloc_report_full(const void *ptr, FILE *f);
339       This provides a more detailed report than talloc_report(). It will
340       recursively print the entire tree of memory referenced by the pointer.
341       References in the tree are shown by giving the name of the pointer that
342       is referenced.
343
344       You can pass NULL for the pointer, in which case a report is printed
345       for the top level memory context, but only if
346       talloc_enable_leak_report() or talloc_enable_leak_report_full() has
347       been called.
348
349
350       void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
351                                   void (*callback)(const void *ptr, int depth, int max_depth, int is_ref, void *priv),
352                                   void *priv);
353
354       This provides a more flexible reports than talloc_report(). It will
355       recursively call the callback for the entire tree of memory referenced
356       by the pointer. References in the tree are passed with is_ref = 1 and
357       the pointer that is referenced.
358
359       You can pass NULL for the pointer, in which case a report is printed
360       for the top level memory context, but only if
361       talloc_enable_leak_report() or talloc_enable_leak_report_full() has
362       been called.
363
364       The recursion is stopped when depth >= max_depth. max_depth = -1 means
365       only stop at leaf nodes.
366
367
368       void talloc_report_depth_file(const void *ptr, int depth,
369                                     int max_depth, FILE *f);
370
371       This provides a more flexible reports than talloc_report(). It will let
372       you specify the depth and max_depth.
373
374   void talloc_enable_leak_report(void);
375       This enables calling of talloc_report(NULL, stderr) when the program
376       exits. In Samba4 this is enabled by using the --leak-report command
377       line option.
378
379       For it to be useful, this function must be called before any other
380       talloc function as it establishes a "null context" that acts as the top
381       of the tree. If you don´t call this function first then passing NULL to
382       talloc_report() or talloc_report_full() won´t give you the full tree
383       printout.
384
385       Here is a typical talloc report:
386
387           talloc report on ´null_context´ (total 267 bytes in 15 blocks)
388           libcli/auth/spnego_parse.c:55  contains   31 bytes in   2 blocks
389           libcli/auth/spnego_parse.c:55  contains   31 bytes in   2 blocks
390           iconv(UTF8,CP850)              contains   42 bytes in   2 blocks
391           libcli/auth/spnego_parse.c:55  contains   31 bytes in   2 blocks
392           iconv(CP850,UTF8)              contains   42 bytes in   2 blocks
393           iconv(UTF8,UTF-16LE)           contains   45 bytes in   2 blocks
394           iconv(UTF-16LE,UTF8)           contains   45 bytes in   2 blocks
395
396
397   void talloc_enable_leak_report_full(void);
398       This enables calling of talloc_report_full(NULL, stderr) when the
399       program exits. In Samba4 this is enabled by using the
400       --leak-report-full command line option.
401
402       For it to be useful, this function must be called before any other
403       talloc function as it establishes a "null context" that acts as the top
404       of the tree. If you don´t call this function first then passing NULL to
405       talloc_report() or talloc_report_full() won´t give you the full tree
406       printout.
407
408       Here is a typical full report:
409
410           full talloc report on ´root´ (total 18 bytes in 8 blocks)
411           p1               contains     18 bytes in   7 blocks (ref 0)
412               r1               contains     13 bytes in   2 blocks (ref 0)
413                   reference to: p2
414               p2               contains      1 bytes in   1 blocks (ref 1)
415               x3               contains      1 bytes in   1 blocks (ref 0)
416               x2               contains      1 bytes in   1 blocks (ref 0)
417               x1               contains      1 bytes in   1 blocks (ref 0)
418
419
420   (type *)talloc_zero(const void *ctx, type);
421       The talloc_zero() macro is equivalent to:
422
423           ptr = talloc(ctx, type);
424           if (ptr) memset(ptr, 0, sizeof(type));
425
426   void *talloc_zero_size(const void *ctx, size_t size)
427       The talloc_zero_size() function is useful when you don´t have a known
428       type.
429
430   void *talloc_memdup(const void *ctx, const void *p, size_t size);
431       The talloc_memdup() function is equivalent to:
432
433           ptr = talloc_size(ctx, size);
434           if (ptr) memcpy(ptr, p, size);
435
436   char *talloc_strdup(const void *ctx, const char *p);
437       The talloc_strdup() function is equivalent to:
438
439           ptr = talloc_size(ctx, strlen(p)+1);
440           if (ptr) memcpy(ptr, p, strlen(p)+1);
441
442       This function sets the name of the new pointer to the passed string.
443       This is equivalent to:
444
445           talloc_set_name_const(ptr, ptr)
446
447   char *talloc_strndup(const void *t, const char *p, size_t n);
448       The talloc_strndup() function is the talloc equivalent of the C library
449       function strndup(3).
450
451       This function sets the name of the new pointer to the passed string.
452       This is equivalent to:
453
454           talloc_set_name_const(ptr, ptr)
455
456   char *talloc_append_string(const void *t, char *orig, const char *append);
457       The talloc_append_string() function appends the given formatted string
458       to the given string.
459
460       This function sets the name of the new pointer to the new string. This
461       is equivalent to:
462
463           talloc_set_name_const(ptr, ptr)
464
465   char *talloc_vasprintf(const void *t, const char *fmt, va_list ap);
466       The talloc_vasprintf() function is the talloc equivalent of the C
467       library function vasprintf(3).
468
469       This function sets the name of the new pointer to the new string. This
470       is equivalent to:
471
472           talloc_set_name_const(ptr, ptr)
473
474   char *talloc_asprintf(const void *t, const char *fmt, ...);
475       The talloc_asprintf() function is the talloc equivalent of the C
476       library function asprintf(3).
477
478       This function sets the name of the new pointer to the passed string.
479       This is equivalent to:
480
481           talloc_set_name_const(ptr, ptr)
482
483   char *talloc_asprintf_append(char *s, const char *fmt, ...);
484       The talloc_asprintf_append() function appends the given formatted
485       string to the given string.
486
487       This function sets the name of the new pointer to the new string. This
488       is equivalent to:
489
490           talloc_set_name_const(ptr, ptr)
491
492   (type *)talloc_array(const void *ctx, type, uint_t count);
493       The talloc_array() macro is equivalent to:
494
495           (type *)talloc_size(ctx, sizeof(type) * count);
496
497       except that it provides integer overflow protection for the multiply,
498       returning NULL if the multiply overflows.
499
500   void *talloc_array_size(const void *ctx, size_t size, uint_t count);
501       The talloc_array_size() function is useful when the type is not known.
502       It operates in the same way as talloc_array(), but takes a size instead
503       of a type.
504
505   (typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, uint_t count);
506       The talloc_ptrtype() macro should be used when you have a pointer to an
507       array and want to allocate memory of an array to point at with this
508       pointer. When compiling with gcc >= 3 it is typesafe. Note this is a
509       wrapper of talloc_array_size() and talloc_get_name() will return the
510       current location in the source file. and not the type.
511
512   void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size)
513       This is a non-macro version of talloc_realloc(), which is useful as
514       libraries sometimes want a realloc function pointer. A realloc(3)
515       implementation encapsulates the functionality of malloc(3), free(3) and
516       realloc(3) in one call, which is why it is useful to be able to pass
517       around a single function pointer.
518
519   void *talloc_autofree_context(void);
520       This is a handy utility function that returns a talloc context which
521       will be automatically freed on program exit. This can be used to reduce
522       the noise in memory leak reports.
523
524   void *talloc_check_name(const void *ptr, const char *name);
525       This function checks if a pointer has the specified name. If it does
526       then the pointer is returned. It it doesn´t then NULL is returned.
527
528   (type *)talloc_get_type(const void *ptr, type);
529       This macro allows you to do type checking on talloc pointers. It is
530       particularly useful for void* private pointers. It is equivalent to
531       this:
532
533           (type *)talloc_check_name(ptr, #type)
534
535   talloc_set_type(const void *ptr, type);
536       This macro allows you to force the name of a pointer to be a particular
537       type. This can be used in conjunction with talloc_get_type() to do type
538       checking on void* pointers.
539
540       It is equivalent to this:
541
542           talloc_set_name_const(ptr, #type)
543
544   talloc_set_log_fn(void (*log_fn)(const char *message));
545       This function sets a logging function that talloc will use for warnings
546       and errors. By default talloc will not print any warnings or errors.
547
548   talloc_set_log_stderr(void);
549       This sets the talloc log function to write log messages to stderr
550

PERFORMANCE

552       All the additional features of talloc(3) over malloc(3) do come at a
553       price. We have a simple performance test in Samba4 that measures
554       talloc() versus malloc() performance, and it seems that talloc() is
555       about 10% slower than malloc() on my x86 Debian Linux box. For Samba,
556       the great reduction in code complexity that we get by using talloc
557       makes this worthwhile, especially as the total overhead of
558       talloc/malloc in Samba is already quite small.
559

SEE ALSO

561       malloc(3), strndup(3), vasprintf(3), asprintf(3),
562       http://talloc.samba.org/
563

COPYRIGHT/LICENSE

565       Copyright (C) Andrew Tridgell 2004
566
567       This program is free software; you can redistribute it and/or modify it
568       under the terms of the GNU General Public License as published by the
569       Free Software Foundation; either version 3 of the License, or (at your
570       option) any later version.
571
572       This program is distributed in the hope that it will be useful, but
573       WITHOUT ANY WARRANTY; without even the implied warranty of
574       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
575       General Public License for more details.
576
577       You should have received a copy of the GNU General Public License along
578       with this program; if not, see http://www.gnu.org/licenses/.
579
580
581
582[FIXME: source]                   12/15/2009                         TALLOC(3)
Impressum