26
26
X->name = talloc_strdup(X, "foo");
28
28
and the pointer X->name would be a "child" of the talloc context "X"
29
which is itself a child of mem_ctx. So if you do talloc_free(mem_ctx)
29
which is itself a child of "mem_ctx". So if you do talloc_free(mem_ctx)
30
30
then it is all destroyed, whereas if you do talloc_free(X) then just X
31
31
and X->name are destroyed, and if you do talloc_free(X->name) then
32
32
just the name element of X is destroyed.
64
64
talloc itself does not deal with threads. It is thread-safe (assuming
65
65
the underlying "malloc" is), as long as each thread uses different
67
If two threads uses the same context then they need to synchronize in
67
If two threads use the same context then they need to synchronize in
68
68
order to be safe. In particular:
69
69
- when using talloc_enable_leak_report(), giving directly NULL as a
70
70
parent context implicitly refers to a hidden "null context" global
74
74
shouldn't be used by several threads simultaneously without
77
talloc and shared objects
78
-------------------------
80
talloc can be used in shared objects. Special care needs to be taken
81
to never use talloc_autofree_context() in code that might be loaded
82
with dlopen() and unloaded with dlclose(), as talloc_autofree_context()
83
internally uses atexit(3). Some platforms like modern Linux handles
84
this fine, but for example FreeBSD does not deal well with dlopen()
85
and atexit() used simultaneously: dlclose() does not clean up the list
86
of atexit-handlers, so when the program exits the code that was
87
registered from within talloc_autofree_context() is gone, the program
78
91
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
79
92
(type *)talloc(const void *context, type);
119
132
The return value of talloc_free() indicates success or failure, with 0
120
returned for success and -1 for failure. The only possible failure
121
condition is if the pointer had a destructor attached to it and the
122
destructor returned -1. See talloc_set_destructor() for details on
125
If this pointer has an additional parent when talloc_free() is called
126
then the memory is not actually released, but instead the most
127
recently established parent is destroyed. See talloc_reference() for
128
details on establishing additional parents.
130
For more control on which parent is removed, see talloc_unlink()
132
talloc_free() operates recursively on its children.
134
From the 2.0 version of talloc, as a special case, talloc_free() is
135
refused on pointers that have more than one parent, as talloc would
136
have no way of knowing which parent should be removed. To free a
133
returned for success and -1 for failure. A possible failure condition
134
is if the pointer had a destructor attached to it and the destructor
135
returned -1. See talloc_set_destructor() for details on
136
destructors. Likewise, if "ptr" is NULL, then the function will make
137
no modifications and returns -1.
139
From version 2.0 and onwards, as a special case, talloc_free() is
140
refused on pointers that have more than one parent associated, as talloc
141
would have no way of knowing which parent should be removed. This is
142
different from older versions in the sense that always the reference to
143
the most recently established parent has been destroyed. Hence to free a
137
144
pointer that has more than one parent please use talloc_unlink().
139
146
To help you find problems in your code caused by this behaviour, if
148
155
talloc_set_log_stderr() for more information on talloc logging
158
talloc_free() operates recursively on its children.
151
160
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
152
int talloc_free_children(void *ptr);
161
void talloc_free_children(void *ptr);
154
163
The talloc_free_children() walks along the list of all children of a
155
164
talloc context and talloc_free()s only the children, not the context
167
A NULL argument is handled as no-op.
159
169
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
160
170
void *talloc_reference(const void *context, const void *ptr);
176
186
will reduce the number of parents of this pointer by 1, and will
177
187
cause this pointer to be freed if it runs out of parents.
179
- you can talloc_free() the pointer itself. That will destroy the
180
most recently established parent to the pointer and leave the
181
pointer as a child of its current parent.
189
- you can talloc_free() the pointer itself if it has at maximum one
190
parent. This behaviour has been changed since the release of version
191
2.0. Further informations in the description of "talloc_free".
183
193
For more control on which parent to remove, see talloc_unlink()
194
204
then this function will fail and will return -1. Likewise, if "ptr"
195
205
is NULL, then the function will make no modifications and return -1.
197
Usually you can just use talloc_free() instead of talloc_unlink(), but
198
sometimes it is useful to have the additional control on which parent
207
You can just use talloc_free() instead of talloc_unlink() if there
208
is at maximum one parent. This behaviour has been changed since the
209
release of version 2.0. Further informations in the description of
202
212
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
203
213
void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
469
479
void talloc_report_full(const void *ptr, FILE *f);
471
481
This provides a more detailed report than talloc_report(). It will
472
recursively print the ensire tree of memory referenced by the
482
recursively print the entire tree of memory referenced by the
473
483
pointer. References in the tree are shown by giving the name of the
474
484
pointer that is referenced.
629
639
The talloc_asprintf_append() function appends the given formatted
630
640
string to the given string.
631
Use this varient when the string in the current talloc buffer may
641
Use this variant when the string in the current talloc buffer may
632
642
have been truncated in length.
634
644
This functions sets the name of the new pointer to the new
643
653
The talloc_asprintf_append() function appends the given formatted
644
654
string to the end of the currently allocated talloc buffer.
645
Use this varient when the string in the current talloc buffer has
655
Use this variant when the string in the current talloc buffer has
646
656
not been changed.
648
658
This functions sets the name of the new pointer to the new
654
664
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
655
((type *)talloc_array(const void *ctx, type, uint_t count);
665
((type *)talloc_array(const void *ctx, type, unsigned int count);
657
667
The talloc_array() macro is equivalent to::
665
675
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
666
void *talloc_array_size(const void *ctx, size_t size, uint_t count);
676
void *talloc_array_size(const void *ctx, size_t size, unsigned int count);
668
678
The talloc_array_size() function is useful when the type is not
669
679
known. It operates in the same way as talloc_array(), but takes a size
670
680
instead of a type.
672
682
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
673
(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, uint_t count);
683
(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, unsigned int count);
675
685
The talloc_ptrtype() macro should be used when you have a pointer to an array
676
686
and want to allocate memory of an array to point at with this pointer. When compiling
716
726
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
717
727
talloc_set_type(const void *ptr, type);
719
This macro allows you to force the name of a pointer to be a
729
This macro allows you to force the name of a pointer to be of a
720
730
particular type. This can be used in conjunction with
721
731
talloc_get_type() to do type checking on void* pointers.
727
737
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
728
738
talloc_get_size(const void *ctx);
730
This function lets you know the amount of memory alloced so far by
740
This function lets you know the amount of memory allocated so far by
731
741
this context. It does NOT account for subcontext memory.
732
742
This can be used to calculate the size of an array.
754
764
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
755
765
void talloc_set_log_stderr(void)
757
This sets the talloc log function to write log messages to stderr
767
This sets the talloc log function to write log messages to stderr.