1 | #ifndef _TALLOC_H_
|
---|
2 | #define _TALLOC_H_
|
---|
3 | /*
|
---|
4 | Unix SMB/CIFS implementation.
|
---|
5 | Samba temporary memory allocation functions
|
---|
6 |
|
---|
7 | Copyright (C) Andrew Tridgell 2004-2005
|
---|
8 | Copyright (C) Stefan Metzmacher 2006
|
---|
9 |
|
---|
10 | ** NOTE! The following LGPL license applies to the talloc
|
---|
11 | ** library. This does NOT imply that all of Samba is released
|
---|
12 | ** under the LGPL
|
---|
13 |
|
---|
14 | This library is free software; you can redistribute it and/or
|
---|
15 | modify it under the terms of the GNU Lesser General Public
|
---|
16 | License as published by the Free Software Foundation; either
|
---|
17 | version 3 of the License, or (at your option) any later version.
|
---|
18 |
|
---|
19 | This library is distributed in the hope that it will be useful,
|
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
22 | Lesser General Public License for more details.
|
---|
23 |
|
---|
24 | You should have received a copy of the GNU Lesser General Public
|
---|
25 | License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include <stdlib.h>
|
---|
29 | #include <stdio.h>
|
---|
30 | #include <stdarg.h>
|
---|
31 |
|
---|
32 | #ifdef __cplusplus
|
---|
33 | extern "C" {
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @defgroup talloc The talloc API
|
---|
38 | *
|
---|
39 | * talloc is a hierarchical, reference counted memory pool system with
|
---|
40 | * destructors. It is the core memory allocator used in Samba.
|
---|
41 | *
|
---|
42 | * @{
|
---|
43 | */
|
---|
44 |
|
---|
45 | #define TALLOC_VERSION_MAJOR 2
|
---|
46 | #define TALLOC_VERSION_MINOR 0
|
---|
47 |
|
---|
48 | int talloc_version_major(void);
|
---|
49 | int talloc_version_minor(void);
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * @brief Define a talloc parent type
|
---|
53 | *
|
---|
54 | * As talloc is a hierarchial memory allocator, every talloc chunk is a
|
---|
55 | * potential parent to other talloc chunks. So defining a separate type for a
|
---|
56 | * talloc chunk is not strictly necessary. TALLOC_CTX is defined nevertheless,
|
---|
57 | * as it provides an indicator for function arguments. You will frequently
|
---|
58 | * write code like
|
---|
59 | *
|
---|
60 | * @code
|
---|
61 | * struct foo *foo_create(TALLOC_CTX *mem_ctx)
|
---|
62 | * {
|
---|
63 | * struct foo *result;
|
---|
64 | * result = talloc(mem_ctx, struct foo);
|
---|
65 | * if (result == NULL) return NULL;
|
---|
66 | * ... initialize foo ...
|
---|
67 | * return result;
|
---|
68 | * }
|
---|
69 | * @endcode
|
---|
70 | *
|
---|
71 | * In this type of allocating functions it is handy to have a general
|
---|
72 | * TALLOC_CTX type to indicate which parent to put allocated structures on.
|
---|
73 | */
|
---|
74 | typedef void TALLOC_CTX;
|
---|
75 |
|
---|
76 | /*
|
---|
77 | this uses a little trick to allow __LINE__ to be stringified
|
---|
78 | */
|
---|
79 | #ifndef __location__
|
---|
80 | #define __TALLOC_STRING_LINE1__(s) #s
|
---|
81 | #define __TALLOC_STRING_LINE2__(s) __TALLOC_STRING_LINE1__(s)
|
---|
82 | #define __TALLOC_STRING_LINE3__ __TALLOC_STRING_LINE2__(__LINE__)
|
---|
83 | #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | #ifndef TALLOC_DEPRECATED
|
---|
87 | #define TALLOC_DEPRECATED 0
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | #ifndef PRINTF_ATTRIBUTE
|
---|
91 | #if (__GNUC__ >= 3)
|
---|
92 | /** Use gcc attribute to check printf fns. a1 is the 1-based index of
|
---|
93 | * the parameter containing the format, and a2 the index of the first
|
---|
94 | * argument. Note that some gcc 2.x versions don't handle this
|
---|
95 | * properly **/
|
---|
96 | #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
|
---|
97 | #else
|
---|
98 | #define PRINTF_ATTRIBUTE(a1, a2)
|
---|
99 | #endif
|
---|
100 | #endif
|
---|
101 |
|
---|
102 | #ifdef DOXYGEN
|
---|
103 | /**
|
---|
104 | * @brief Create a new talloc context.
|
---|
105 | *
|
---|
106 | * The talloc() macro is the core of the talloc library. It takes a memory
|
---|
107 | * context and a type, and returns a pointer to a new area of memory of the
|
---|
108 | * given type.
|
---|
109 | *
|
---|
110 | * The returned pointer is itself a talloc context, so you can use it as the
|
---|
111 | * context argument to more calls to talloc if you wish.
|
---|
112 | *
|
---|
113 | * The returned pointer is a "child" of the supplied context. This means that if
|
---|
114 | * you talloc_free() the context then the new child disappears as well.
|
---|
115 | * Alternatively you can free just the child.
|
---|
116 | *
|
---|
117 | * @param[in] ctx A talloc context to create a new reference on or NULL to
|
---|
118 | * create a new top level context.
|
---|
119 | *
|
---|
120 | * @param[in] type The type of memory to allocate.
|
---|
121 | *
|
---|
122 | * @return A type casted talloc context or NULL on error.
|
---|
123 | *
|
---|
124 | * @code
|
---|
125 | * unsigned int *a, *b;
|
---|
126 | *
|
---|
127 | * a = talloc(NULL, unsigned int);
|
---|
128 | * b = talloc(a, unsigned int);
|
---|
129 | * @endcode
|
---|
130 | *
|
---|
131 | * @see talloc_zero
|
---|
132 | * @see talloc_array
|
---|
133 | * @see talloc_steal
|
---|
134 | * @see talloc_free
|
---|
135 | */
|
---|
136 | void *talloc(const void *ctx, #type);
|
---|
137 | #else
|
---|
138 | #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
|
---|
139 | void *_talloc(const void *context, size_t size);
|
---|
140 | #endif
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * @brief Create a new top level talloc context.
|
---|
144 | *
|
---|
145 | * This function creates a zero length named talloc context as a top level
|
---|
146 | * context. It is equivalent to:
|
---|
147 | *
|
---|
148 | * @code
|
---|
149 | * talloc_named(NULL, 0, fmt, ...);
|
---|
150 | * @endcode
|
---|
151 | * @param[in] fmt Format string for the name.
|
---|
152 | *
|
---|
153 | * @param[in] ... Additional printf-style arguments.
|
---|
154 | *
|
---|
155 | * @return The allocated memory chunk, NULL on error.
|
---|
156 | *
|
---|
157 | * @see talloc_named()
|
---|
158 | */
|
---|
159 | void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
|
---|
160 |
|
---|
161 | #ifdef DOXYGEN
|
---|
162 | /**
|
---|
163 | * @brief Free a chunk of talloc memory.
|
---|
164 | *
|
---|
165 | * The talloc_free() function frees a piece of talloc memory, and all its
|
---|
166 | * children. You can call talloc_free() on any pointer returned by
|
---|
167 | * talloc().
|
---|
168 | *
|
---|
169 | * The return value of talloc_free() indicates success or failure, with 0
|
---|
170 | * returned for success and -1 for failure. A possible failure condition
|
---|
171 | * is if the pointer had a destructor attached to it and the destructor
|
---|
172 | * returned -1. See talloc_set_destructor() for details on
|
---|
173 | * destructors. Likewise, if "ptr" is NULL, then the function will make
|
---|
174 | * no modifications and return -1.
|
---|
175 | *
|
---|
176 | * From version 2.0 and onwards, as a special case, talloc_free() is
|
---|
177 | * refused on pointers that have more than one parent associated, as talloc
|
---|
178 | * would have no way of knowing which parent should be removed. This is
|
---|
179 | * different from older versions in the sense that always the reference to
|
---|
180 | * the most recently established parent has been destroyed. Hence to free a
|
---|
181 | * pointer that has more than one parent please use talloc_unlink().
|
---|
182 | *
|
---|
183 | * To help you find problems in your code caused by this behaviour, if
|
---|
184 | * you do try and free a pointer with more than one parent then the
|
---|
185 | * talloc logging function will be called to give output like this:
|
---|
186 | *
|
---|
187 | * @code
|
---|
188 | * ERROR: talloc_free with references at some_dir/source/foo.c:123
|
---|
189 | * reference at some_dir/source/other.c:325
|
---|
190 | * reference at some_dir/source/third.c:121
|
---|
191 | * @endcode
|
---|
192 | *
|
---|
193 | * Please see the documentation for talloc_set_log_fn() and
|
---|
194 | * talloc_set_log_stderr() for more information on talloc logging
|
---|
195 | * functions.
|
---|
196 | *
|
---|
197 | * talloc_free() operates recursively on its children.
|
---|
198 | *
|
---|
199 | * @param[in] ptr The chunk to be freed.
|
---|
200 | *
|
---|
201 | * @return Returns 0 on success and -1 on error. A possible
|
---|
202 | * failure condition is if the pointer had a destructor
|
---|
203 | * attached to it and the destructor returned -1. Likewise,
|
---|
204 | * if "ptr" is NULL, then the function will make no
|
---|
205 | * modifications and returns -1.
|
---|
206 | *
|
---|
207 | * Example:
|
---|
208 | * @code
|
---|
209 | * unsigned int *a, *b;
|
---|
210 | * a = talloc(NULL, unsigned int);
|
---|
211 | * b = talloc(a, unsigned int);
|
---|
212 | *
|
---|
213 | * talloc_free(a); // Frees a and b
|
---|
214 | * @endcode
|
---|
215 | *
|
---|
216 | * @see talloc_set_destructor()
|
---|
217 | * @see talloc_unlink()
|
---|
218 | */
|
---|
219 | int talloc_free(void *ptr);
|
---|
220 | #else
|
---|
221 | #define talloc_free(ctx) _talloc_free(ctx, __location__)
|
---|
222 | int _talloc_free(void *ptr, const char *location);
|
---|
223 | #endif
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * @brief Free a talloc chunk's children.
|
---|
227 | *
|
---|
228 | * The function walks along the list of all children of a talloc context and
|
---|
229 | * talloc_free()s only the children, not the context itself.
|
---|
230 | *
|
---|
231 | * A NULL argument is handled as no-op.
|
---|
232 | *
|
---|
233 | * @param[in] ptr The chunk that you want to free the children of
|
---|
234 | * (NULL is allowed too)
|
---|
235 | */
|
---|
236 | void talloc_free_children(void *ptr);
|
---|
237 |
|
---|
238 | #ifdef DOXYGEN
|
---|
239 | /**
|
---|
240 | * @brief Assign a destructor function to be called when a chunk is freed.
|
---|
241 | *
|
---|
242 | * The function talloc_set_destructor() sets the "destructor" for the pointer
|
---|
243 | * "ptr". A destructor is a function that is called when the memory used by a
|
---|
244 | * pointer is about to be released. The destructor receives the pointer as an
|
---|
245 | * argument, and should return 0 for success and -1 for failure.
|
---|
246 | *
|
---|
247 | * The destructor can do anything it wants to, including freeing other pieces
|
---|
248 | * of memory. A common use for destructors is to clean up operating system
|
---|
249 | * resources (such as open file descriptors) contained in the structure the
|
---|
250 | * destructor is placed on.
|
---|
251 | *
|
---|
252 | * You can only place one destructor on a pointer. If you need more than one
|
---|
253 | * destructor then you can create a zero-length child of the pointer and place
|
---|
254 | * an additional destructor on that.
|
---|
255 | *
|
---|
256 | * To remove a destructor call talloc_set_destructor() with NULL for the
|
---|
257 | * destructor.
|
---|
258 | *
|
---|
259 | * If your destructor attempts to talloc_free() the pointer that it is the
|
---|
260 | * destructor for then talloc_free() will return -1 and the free will be
|
---|
261 | * ignored. This would be a pointless operation anyway, as the destructor is
|
---|
262 | * only called when the memory is just about to go away.
|
---|
263 | *
|
---|
264 | * @param[in] ptr The talloc chunk to add a destructor to.
|
---|
265 | *
|
---|
266 | * @param[in] destructor The destructor function to be called. NULL to remove
|
---|
267 | * it.
|
---|
268 | *
|
---|
269 | * Example:
|
---|
270 | * @code
|
---|
271 | * static int destroy_fd(int *fd) {
|
---|
272 | * close(*fd);
|
---|
273 | * return 0;
|
---|
274 | * }
|
---|
275 | *
|
---|
276 | * int *open_file(const char *filename) {
|
---|
277 | * int *fd = talloc(NULL, int);
|
---|
278 | * *fd = open(filename, O_RDONLY);
|
---|
279 | * if (*fd < 0) {
|
---|
280 | * talloc_free(fd);
|
---|
281 | * return NULL;
|
---|
282 | * }
|
---|
283 | * // Whenever they free this, we close the file.
|
---|
284 | * talloc_set_destructor(fd, destroy_fd);
|
---|
285 | * return fd;
|
---|
286 | * }
|
---|
287 | * @endcode
|
---|
288 | *
|
---|
289 | * @see talloc()
|
---|
290 | * @see talloc_free()
|
---|
291 | */
|
---|
292 | void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * @brief Change a talloc chunk's parent.
|
---|
296 | *
|
---|
297 | * The talloc_steal() function changes the parent context of a talloc
|
---|
298 | * pointer. It is typically used when the context that the pointer is
|
---|
299 | * currently a child of is going to be freed and you wish to keep the
|
---|
300 | * memory for a longer time.
|
---|
301 | *
|
---|
302 | * To make the changed hierarchy less error-prone, you might consider to use
|
---|
303 | * talloc_move().
|
---|
304 | *
|
---|
305 | * If you try and call talloc_steal() on a pointer that has more than one
|
---|
306 | * parent then the result is ambiguous. Talloc will choose to remove the
|
---|
307 | * parent that is currently indicated by talloc_parent() and replace it with
|
---|
308 | * the chosen parent. You will also get a message like this via the talloc
|
---|
309 | * logging functions:
|
---|
310 | *
|
---|
311 | * @code
|
---|
312 | * WARNING: talloc_steal with references at some_dir/source/foo.c:123
|
---|
313 | * reference at some_dir/source/other.c:325
|
---|
314 | * reference at some_dir/source/third.c:121
|
---|
315 | * @endcode
|
---|
316 | *
|
---|
317 | * To unambiguously change the parent of a pointer please see the function
|
---|
318 | * talloc_reparent(). See the talloc_set_log_fn() documentation for more
|
---|
319 | * information on talloc logging.
|
---|
320 | *
|
---|
321 | * @param[in] new_ctx The new parent context.
|
---|
322 | *
|
---|
323 | * @param[in] ptr The talloc chunk to move.
|
---|
324 | *
|
---|
325 | * @return Returns the pointer that you pass it. It does not have
|
---|
326 | * any failure modes.
|
---|
327 | *
|
---|
328 | * @note It is possible to produce loops in the parent/child relationship
|
---|
329 | * if you are not careful with talloc_steal(). No guarantees are provided
|
---|
330 | * as to your sanity or the safety of your data if you do this.
|
---|
331 | */
|
---|
332 | void *talloc_steal(const void *new_ctx, const void *ptr);
|
---|
333 | #else /* DOXYGEN */
|
---|
334 | /* try to make talloc_set_destructor() and talloc_steal() type safe,
|
---|
335 | if we have a recent gcc */
|
---|
336 | #if (__GNUC__ >= 3)
|
---|
337 | #define _TALLOC_TYPEOF(ptr) __typeof__(ptr)
|
---|
338 | #define talloc_set_destructor(ptr, function) \
|
---|
339 | do { \
|
---|
340 | int (*_talloc_destructor_fn)(_TALLOC_TYPEOF(ptr)) = (function); \
|
---|
341 | _talloc_set_destructor((ptr), (int (*)(void *))_talloc_destructor_fn); \
|
---|
342 | } while(0)
|
---|
343 | /* this extremely strange macro is to avoid some braindamaged warning
|
---|
344 | stupidity in gcc 4.1.x */
|
---|
345 | #define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__); __talloc_steal_ret; })
|
---|
346 | #else /* __GNUC__ >= 3 */
|
---|
347 | #define talloc_set_destructor(ptr, function) \
|
---|
348 | _talloc_set_destructor((ptr), (int (*)(void *))(function))
|
---|
349 | #define _TALLOC_TYPEOF(ptr) void *
|
---|
350 | #define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__)
|
---|
351 | #endif /* __GNUC__ >= 3 */
|
---|
352 | void _talloc_set_destructor(const void *ptr, int (*_destructor)(void *));
|
---|
353 | void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location);
|
---|
354 | #endif /* DOXYGEN */
|
---|
355 |
|
---|
356 | /**
|
---|
357 | * @brief Assign a name to a talloc chunk.
|
---|
358 | *
|
---|
359 | * Each talloc pointer has a "name". The name is used principally for
|
---|
360 | * debugging purposes, although it is also possible to set and get the name on
|
---|
361 | * a pointer in as a way of "marking" pointers in your code.
|
---|
362 | *
|
---|
363 | * The main use for names on pointer is for "talloc reports". See
|
---|
364 | * talloc_report() and talloc_report_full() for details. Also see
|
---|
365 | * talloc_enable_leak_report() and talloc_enable_leak_report_full().
|
---|
366 | *
|
---|
367 | * The talloc_set_name() function allocates memory as a child of the
|
---|
368 | * pointer. It is logically equivalent to:
|
---|
369 | *
|
---|
370 | * @code
|
---|
371 | * talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
|
---|
372 | * @endcode
|
---|
373 | *
|
---|
374 | * @param[in] ptr The talloc chunk to assign a name to.
|
---|
375 | *
|
---|
376 | * @param[in] fmt Format string for the name.
|
---|
377 | *
|
---|
378 | * @param[in] ... Add printf-style additional arguments.
|
---|
379 | *
|
---|
380 | * @return The assigned name, NULL on error.
|
---|
381 | *
|
---|
382 | * @note Multiple calls to talloc_set_name() will allocate more memory without
|
---|
383 | * releasing the name. All of the memory is released when the ptr is freed
|
---|
384 | * using talloc_free().
|
---|
385 | */
|
---|
386 | const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
|
---|
387 |
|
---|
388 | #ifdef DOXYGEN
|
---|
389 | /**
|
---|
390 | * @brief Change a talloc chunk's parent.
|
---|
391 | *
|
---|
392 | * This function has the same effect as talloc_steal(), and additionally sets
|
---|
393 | * the source pointer to NULL. You would use it like this:
|
---|
394 | *
|
---|
395 | * @code
|
---|
396 | * struct foo *X = talloc(tmp_ctx, struct foo);
|
---|
397 | * struct foo *Y;
|
---|
398 | * Y = talloc_move(new_ctx, &X);
|
---|
399 | * @endcode
|
---|
400 | *
|
---|
401 | * @param[in] new_ctx The new parent context.
|
---|
402 | *
|
---|
403 | * @param[in] pptr Pointer to the talloc chunk to move.
|
---|
404 | *
|
---|
405 | * @return The pointer of the talloc chunk it has been moved to,
|
---|
406 | * NULL on error.
|
---|
407 | */
|
---|
408 | void *talloc_move(const void *new_ctx, void **pptr);
|
---|
409 | #else
|
---|
410 | #define talloc_move(ctx, pptr) (_TALLOC_TYPEOF(*(pptr)))_talloc_move((ctx),(void *)(pptr))
|
---|
411 | void *_talloc_move(const void *new_ctx, const void *pptr);
|
---|
412 | #endif
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * @brief Assign a name to a talloc chunk.
|
---|
416 | *
|
---|
417 | * The function is just like talloc_set_name(), but it takes a string constant,
|
---|
418 | * and is much faster. It is extensively used by the "auto naming" macros, such
|
---|
419 | * as talloc_p().
|
---|
420 | *
|
---|
421 | * This function does not allocate any memory. It just copies the supplied
|
---|
422 | * pointer into the internal representation of the talloc ptr. This means you
|
---|
423 | * must not pass a name pointer to memory that will disappear before the ptr
|
---|
424 | * is freed with talloc_free().
|
---|
425 | *
|
---|
426 | * @param[in] ptr The talloc chunk to assign a name to.
|
---|
427 | *
|
---|
428 | * @param[in] name Format string for the name.
|
---|
429 | */
|
---|
430 | void talloc_set_name_const(const void *ptr, const char *name);
|
---|
431 |
|
---|
432 | /**
|
---|
433 | * @brief Create a named talloc chunk.
|
---|
434 | *
|
---|
435 | * The talloc_named() function creates a named talloc pointer. It is
|
---|
436 | * equivalent to:
|
---|
437 | *
|
---|
438 | * @code
|
---|
439 | * ptr = talloc_size(context, size);
|
---|
440 | * talloc_set_name(ptr, fmt, ....);
|
---|
441 | * @endcode
|
---|
442 | *
|
---|
443 | * @param[in] context The talloc context to hang the result off.
|
---|
444 | *
|
---|
445 | * @param[in] size Number of char's that you want to allocate.
|
---|
446 | *
|
---|
447 | * @param[in] fmt Format string for the name.
|
---|
448 | *
|
---|
449 | * @param[in] ... Additional printf-style arguments.
|
---|
450 | *
|
---|
451 | * @return The allocated memory chunk, NULL on error.
|
---|
452 | *
|
---|
453 | * @see talloc_set_name()
|
---|
454 | */
|
---|
455 | void *talloc_named(const void *context, size_t size,
|
---|
456 | const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
|
---|
457 |
|
---|
458 | /**
|
---|
459 | * @brief Basic routine to allocate a chunk of memory.
|
---|
460 | *
|
---|
461 | * This is equivalent to:
|
---|
462 | *
|
---|
463 | * @code
|
---|
464 | * ptr = talloc_size(context, size);
|
---|
465 | * talloc_set_name_const(ptr, name);
|
---|
466 | * @endcode
|
---|
467 | *
|
---|
468 | * @param[in] context The parent context.
|
---|
469 | *
|
---|
470 | * @param[in] size The number of char's that we want to allocate.
|
---|
471 | *
|
---|
472 | * @param[in] name The name the talloc block has.
|
---|
473 | *
|
---|
474 | * @return The allocated memory chunk, NULL on error.
|
---|
475 | */
|
---|
476 | void *talloc_named_const(const void *context, size_t size, const char *name);
|
---|
477 |
|
---|
478 | #ifdef DOXYGEN
|
---|
479 | /**
|
---|
480 | * @brief Untyped allocation.
|
---|
481 | *
|
---|
482 | * The function should be used when you don't have a convenient type to pass to
|
---|
483 | * talloc(). Unlike talloc(), it is not type safe (as it returns a void *), so
|
---|
484 | * you are on your own for type checking.
|
---|
485 | *
|
---|
486 | * Best to use talloc() or talloc_array() instead.
|
---|
487 | *
|
---|
488 | * @param[in] ctx The talloc context to hang the result off.
|
---|
489 | *
|
---|
490 | * @param[in] size Number of char's that you want to allocate.
|
---|
491 | *
|
---|
492 | * @return The allocated memory chunk, NULL on error.
|
---|
493 | *
|
---|
494 | * Example:
|
---|
495 | * @code
|
---|
496 | * void *mem = talloc_size(NULL, 100);
|
---|
497 | * @endcode
|
---|
498 | */
|
---|
499 | void *talloc_size(const void *ctx, size_t size);
|
---|
500 | #else
|
---|
501 | #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
|
---|
502 | #endif
|
---|
503 |
|
---|
504 | #ifdef DOXYGEN
|
---|
505 | /**
|
---|
506 | * @brief Allocate into a typed pointer.
|
---|
507 | *
|
---|
508 | * The talloc_ptrtype() macro should be used when you have a pointer and want
|
---|
509 | * to allocate memory to point at with this pointer. When compiling with
|
---|
510 | * gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size() and
|
---|
511 | * talloc_get_name() will return the current location in the source file and
|
---|
512 | * not the type.
|
---|
513 | *
|
---|
514 | * @param[in] ctx The talloc context to hang the result off.
|
---|
515 | *
|
---|
516 | * @param[in] type The pointer you want to assign the result to.
|
---|
517 | *
|
---|
518 | * @return The properly casted allocated memory chunk, NULL on
|
---|
519 | * error.
|
---|
520 | *
|
---|
521 | * Example:
|
---|
522 | * @code
|
---|
523 | * unsigned int *a = talloc_ptrtype(NULL, a);
|
---|
524 | * @endcode
|
---|
525 | */
|
---|
526 | void *talloc_ptrtype(const void *ctx, #type);
|
---|
527 | #else
|
---|
528 | #define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
|
---|
529 | #endif
|
---|
530 |
|
---|
531 | #ifdef DOXYGEN
|
---|
532 | /**
|
---|
533 | * @brief Allocate a new 0-sized talloc chunk.
|
---|
534 | *
|
---|
535 | * This is a utility macro that creates a new memory context hanging off an
|
---|
536 | * existing context, automatically naming it "talloc_new: __location__" where
|
---|
537 | * __location__ is the source line it is called from. It is particularly
|
---|
538 | * useful for creating a new temporary working context.
|
---|
539 | *
|
---|
540 | * @param[in] ctx The talloc parent context.
|
---|
541 | *
|
---|
542 | * @return A new talloc chunk, NULL on error.
|
---|
543 | */
|
---|
544 | void *talloc_new(const void *ctx);
|
---|
545 | #else
|
---|
546 | #define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
|
---|
547 | #endif
|
---|
548 |
|
---|
549 | #ifdef DOXYGEN
|
---|
550 | /**
|
---|
551 | * @brief Allocate a 0-initizialized structure.
|
---|
552 | *
|
---|
553 | * The macro is equivalent to:
|
---|
554 | *
|
---|
555 | * @code
|
---|
556 | * ptr = talloc(ctx, type);
|
---|
557 | * if (ptr) memset(ptr, 0, sizeof(type));
|
---|
558 | * @endcode
|
---|
559 | *
|
---|
560 | * @param[in] ctx The talloc context to hang the result off.
|
---|
561 | *
|
---|
562 | * @param[in] type The type that we want to allocate.
|
---|
563 | *
|
---|
564 | * @return Pointer to a piece of memory, properly cast to 'type *',
|
---|
565 | * NULL on error.
|
---|
566 | *
|
---|
567 | * Example:
|
---|
568 | * @code
|
---|
569 | * unsigned int *a, *b;
|
---|
570 | * a = talloc_zero(NULL, unsigned int);
|
---|
571 | * b = talloc_zero(a, unsigned int);
|
---|
572 | * @endcode
|
---|
573 | *
|
---|
574 | * @see talloc()
|
---|
575 | * @see talloc_zero_size()
|
---|
576 | * @see talloc_zero_array()
|
---|
577 | */
|
---|
578 | void *talloc_zero(const void *ctx, #type);
|
---|
579 |
|
---|
580 | /**
|
---|
581 | * @brief Allocate untyped, 0-initialized memory.
|
---|
582 | *
|
---|
583 | * @param[in] ctx The talloc context to hang the result off.
|
---|
584 | *
|
---|
585 | * @param[in] size Number of char's that you want to allocate.
|
---|
586 | *
|
---|
587 | * @return The allocated memory chunk.
|
---|
588 | */
|
---|
589 | void *talloc_zero_size(const void *ctx, size_t size);
|
---|
590 | #else
|
---|
591 | #define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
|
---|
592 | #define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
|
---|
593 | void *_talloc_zero(const void *ctx, size_t size, const char *name);
|
---|
594 | #endif
|
---|
595 |
|
---|
596 | /**
|
---|
597 | * @brief Return the name of a talloc chunk.
|
---|
598 | *
|
---|
599 | * @param[in] ptr The talloc chunk.
|
---|
600 | *
|
---|
601 | * @return The current name for the given talloc pointer.
|
---|
602 | *
|
---|
603 | * @see talloc_set_name()
|
---|
604 | */
|
---|
605 | const char *talloc_get_name(const void *ptr);
|
---|
606 |
|
---|
607 | /**
|
---|
608 | * @brief Verify that a talloc chunk carries a specified name.
|
---|
609 | *
|
---|
610 | * This function checks if a pointer has the specified name. If it does
|
---|
611 | * then the pointer is returned.
|
---|
612 | *
|
---|
613 | * @param[in] ptr The talloc chunk to check.
|
---|
614 | *
|
---|
615 | * @param[in] name The name to check against.
|
---|
616 | *
|
---|
617 | * @return The pointer if the name matches, NULL if it doesn't.
|
---|
618 | */
|
---|
619 | void *talloc_check_name(const void *ptr, const char *name);
|
---|
620 |
|
---|
621 | /**
|
---|
622 | * @brief Get the parent chunk of a pointer.
|
---|
623 | *
|
---|
624 | * @param[in] ptr The talloc pointer to inspect.
|
---|
625 | *
|
---|
626 | * @return The talloc parent of ptr, NULL on error.
|
---|
627 | */
|
---|
628 | void *talloc_parent(const void *ptr);
|
---|
629 |
|
---|
630 | /**
|
---|
631 | * @brief Get a talloc chunk's parent name.
|
---|
632 | *
|
---|
633 | * @param[in] ptr The talloc pointer to inspect.
|
---|
634 | *
|
---|
635 | * @return The name of ptr's parent chunk.
|
---|
636 | */
|
---|
637 | const char *talloc_parent_name(const void *ptr);
|
---|
638 |
|
---|
639 | /**
|
---|
640 | * @brief Get the total size of a talloc chunk including its children.
|
---|
641 | *
|
---|
642 | * The function returns the total size in bytes used by this pointer and all
|
---|
643 | * child pointers. Mostly useful for debugging.
|
---|
644 | *
|
---|
645 | * Passing NULL is allowed, but it will only give a meaningful result if
|
---|
646 | * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
|
---|
647 | * been called.
|
---|
648 | *
|
---|
649 | * @param[in] ptr The talloc chunk.
|
---|
650 | *
|
---|
651 | * @return The total size.
|
---|
652 | */
|
---|
653 | size_t talloc_total_size(const void *ptr);
|
---|
654 |
|
---|
655 | /**
|
---|
656 | * @brief Get the number of talloc chunks hanging off a chunk.
|
---|
657 | *
|
---|
658 | * The talloc_total_blocks() function returns the total memory block
|
---|
659 | * count used by this pointer and all child pointers. Mostly useful for
|
---|
660 | * debugging.
|
---|
661 | *
|
---|
662 | * Passing NULL is allowed, but it will only give a meaningful result if
|
---|
663 | * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
|
---|
664 | * been called.
|
---|
665 | *
|
---|
666 | * @param[in] ptr The talloc chunk.
|
---|
667 | *
|
---|
668 | * @return The total size.
|
---|
669 | */
|
---|
670 | size_t talloc_total_blocks(const void *ptr);
|
---|
671 |
|
---|
672 | #ifdef DOXYGEN
|
---|
673 | /**
|
---|
674 | * @brief Duplicate a memory area into a talloc chunk.
|
---|
675 | *
|
---|
676 | * The function is equivalent to:
|
---|
677 | *
|
---|
678 | * @code
|
---|
679 | * ptr = talloc_size(ctx, size);
|
---|
680 | * if (ptr) memcpy(ptr, p, size);
|
---|
681 | * @endcode
|
---|
682 | *
|
---|
683 | * @param[in] t The talloc context to hang the result off.
|
---|
684 | *
|
---|
685 | * @param[in] p The memory chunk you want to duplicate.
|
---|
686 | *
|
---|
687 | * @param[in] size Number of char's that you want copy.
|
---|
688 | *
|
---|
689 | * @return The allocated memory chunk.
|
---|
690 | *
|
---|
691 | * @see talloc_size()
|
---|
692 | */
|
---|
693 | void *talloc_memdup(const void *t, const void *p, size_t size);
|
---|
694 | #else
|
---|
695 | #define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
|
---|
696 | void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
|
---|
697 | #endif
|
---|
698 |
|
---|
699 | #ifdef DOXYGEN
|
---|
700 | /**
|
---|
701 | * @brief Assign a type to a talloc chunk.
|
---|
702 | *
|
---|
703 | * This macro allows you to force the name of a pointer to be of a particular
|
---|
704 | * type. This can be used in conjunction with talloc_get_type() to do type
|
---|
705 | * checking on void* pointers.
|
---|
706 | *
|
---|
707 | * It is equivalent to this:
|
---|
708 | *
|
---|
709 | * @code
|
---|
710 | * talloc_set_name_const(ptr, #type)
|
---|
711 | * @endcode
|
---|
712 | *
|
---|
713 | * @param[in] ptr The talloc chunk to assign the type to.
|
---|
714 | *
|
---|
715 | * @param[in] type The type to assign.
|
---|
716 | */
|
---|
717 | void talloc_set_type(const char *ptr, #type);
|
---|
718 |
|
---|
719 | /**
|
---|
720 | * @brief Get a typed pointer out of a talloc pointer.
|
---|
721 | *
|
---|
722 | * This macro allows you to do type checking on talloc pointers. It is
|
---|
723 | * particularly useful for void* private pointers. It is equivalent to
|
---|
724 | * this:
|
---|
725 | *
|
---|
726 | * @code
|
---|
727 | * (type *)talloc_check_name(ptr, #type)
|
---|
728 | * @endcode
|
---|
729 | *
|
---|
730 | * @param[in] ptr The talloc pointer to check.
|
---|
731 | *
|
---|
732 | * @param[in] type The type to check against.
|
---|
733 | *
|
---|
734 | * @return The properly casted pointer given by ptr, NULL on error.
|
---|
735 | */
|
---|
736 | type *talloc_get_type(const void *ptr, #type);
|
---|
737 | #else
|
---|
738 | #define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
|
---|
739 | #define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
|
---|
740 | #endif
|
---|
741 |
|
---|
742 | #ifdef DOXYGEN
|
---|
743 | /**
|
---|
744 | * @brief Safely turn a void pointer into a typed pointer.
|
---|
745 | *
|
---|
746 | * This macro is used together with talloc(mem_ctx, struct foo). If you had to
|
---|
747 | * assing the talloc chunk pointer to some void pointer variable,
|
---|
748 | * talloc_get_type_abort() is the recommended way to get the convert the void
|
---|
749 | * pointer back to a typed pointer.
|
---|
750 | *
|
---|
751 | * @param[in] ptr The void pointer to convert.
|
---|
752 | *
|
---|
753 | * @param[in] type The type that this chunk contains
|
---|
754 | *
|
---|
755 | * @return The same value as ptr, type-checked and properly cast.
|
---|
756 | */
|
---|
757 | void *talloc_get_type_abort(const void *ptr, #type);
|
---|
758 | #else
|
---|
759 | #define talloc_get_type_abort(ptr, type) (type *)_talloc_get_type_abort(ptr, #type, __location__)
|
---|
760 | void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location);
|
---|
761 | #endif
|
---|
762 |
|
---|
763 | /**
|
---|
764 | * @brief Find a parent context by name.
|
---|
765 | *
|
---|
766 | * Find a parent memory context of the current context that has the given
|
---|
767 | * name. This can be very useful in complex programs where it may be
|
---|
768 | * difficult to pass all information down to the level you need, but you
|
---|
769 | * know the structure you want is a parent of another context.
|
---|
770 | *
|
---|
771 | * @param[in] ctx The talloc chunk to start from.
|
---|
772 | *
|
---|
773 | * @param[in] name The name of the parent we look for.
|
---|
774 | *
|
---|
775 | * @return The memory context we are looking for, NULL if not
|
---|
776 | * found.
|
---|
777 | */
|
---|
778 | void *talloc_find_parent_byname(const void *ctx, const char *name);
|
---|
779 |
|
---|
780 | #ifdef DOXYGEN
|
---|
781 | /**
|
---|
782 | * @brief Find a parent context by type.
|
---|
783 | *
|
---|
784 | * Find a parent memory context of the current context that has the given
|
---|
785 | * name. This can be very useful in complex programs where it may be
|
---|
786 | * difficult to pass all information down to the level you need, but you
|
---|
787 | * know the structure you want is a parent of another context.
|
---|
788 | *
|
---|
789 | * Like talloc_find_parent_byname() but takes a type, making it typesafe.
|
---|
790 | *
|
---|
791 | * @param[in] ptr The talloc chunk to start from.
|
---|
792 | *
|
---|
793 | * @param[in] type The type of the parent to look for.
|
---|
794 | *
|
---|
795 | * @return The memory context we are looking for, NULL if not
|
---|
796 | * found.
|
---|
797 | */
|
---|
798 | void *talloc_find_parent_bytype(const void *ptr, #type);
|
---|
799 | #else
|
---|
800 | #define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
|
---|
801 | #endif
|
---|
802 |
|
---|
803 | /**
|
---|
804 | * @brief Allocate a talloc pool.
|
---|
805 | *
|
---|
806 | * A talloc pool is a pure optimization for specific situations. In the
|
---|
807 | * release process for Samba 3.2 we found out that we had become considerably
|
---|
808 | * slower than Samba 3.0 was. Profiling showed that malloc(3) was a large CPU
|
---|
809 | * consumer in benchmarks. For Samba 3.2 we have internally converted many
|
---|
810 | * static buffers to dynamically allocated ones, so malloc(3) being beaten
|
---|
811 | * more was no surprise. But it made us slower.
|
---|
812 | *
|
---|
813 | * talloc_pool() is an optimization to call malloc(3) a lot less for the use
|
---|
814 | * pattern Samba has: The SMB protocol is mainly a request/response protocol
|
---|
815 | * where we have to allocate a certain amount of memory per request and free
|
---|
816 | * that after the SMB reply is sent to the client.
|
---|
817 | *
|
---|
818 | * talloc_pool() creates a talloc chunk that you can use as a talloc parent
|
---|
819 | * exactly as you would use any other ::TALLOC_CTX. The difference is that
|
---|
820 | * when you talloc a child of this pool, no malloc(3) is done. Instead, talloc
|
---|
821 | * just increments a pointer inside the talloc_pool. This also works
|
---|
822 | * recursively. If you use the child of the talloc pool as a parent for
|
---|
823 | * grand-children, their memory is also taken from the talloc pool.
|
---|
824 | *
|
---|
825 | * If you talloc_free() children of a talloc pool, the memory is not given
|
---|
826 | * back to the system. Instead, free(3) is only called if the talloc_pool()
|
---|
827 | * itself is released with talloc_free().
|
---|
828 | *
|
---|
829 | * The downside of a talloc pool is that if you talloc_move() a child of a
|
---|
830 | * talloc pool to a talloc parent outside the pool, the whole pool memory is
|
---|
831 | * not free(3)'ed until that moved chunk is also talloc_free()ed.
|
---|
832 | *
|
---|
833 | * @param[in] context The talloc context to hang the result off.
|
---|
834 | *
|
---|
835 | * @param[in] size Size of the talloc pool.
|
---|
836 | *
|
---|
837 | * @return The allocated talloc pool, NULL on error.
|
---|
838 | */
|
---|
839 | void *talloc_pool(const void *context, size_t size);
|
---|
840 |
|
---|
841 | /**
|
---|
842 | * @brief Free a talloc chunk and NULL out the pointer.
|
---|
843 | *
|
---|
844 | * TALLOC_FREE() frees a pointer and sets it to NULL. Use this if you want
|
---|
845 | * immediate feedback (i.e. crash) if you use a pointer after having free'ed
|
---|
846 | * it.
|
---|
847 | *
|
---|
848 | * @param[in] ctx The chunk to be freed.
|
---|
849 | */
|
---|
850 | #define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
|
---|
851 |
|
---|
852 | /* @} ******************************************************************/
|
---|
853 |
|
---|
854 | /**
|
---|
855 | * \defgroup talloc_ref The talloc reference function.
|
---|
856 | * @ingroup talloc
|
---|
857 | *
|
---|
858 | * This module contains the definitions around talloc references
|
---|
859 | *
|
---|
860 | * @{
|
---|
861 | */
|
---|
862 |
|
---|
863 | /**
|
---|
864 | * @brief Increase the reference count of a talloc chunk.
|
---|
865 | *
|
---|
866 | * The talloc_increase_ref_count(ptr) function is exactly equivalent to:
|
---|
867 | *
|
---|
868 | * @code
|
---|
869 | * talloc_reference(NULL, ptr);
|
---|
870 | * @endcode
|
---|
871 | *
|
---|
872 | * You can use either syntax, depending on which you think is clearer in
|
---|
873 | * your code.
|
---|
874 | *
|
---|
875 | * @param[in] ptr The pointer to increase the reference count.
|
---|
876 | *
|
---|
877 | * @return 0 on success, -1 on error.
|
---|
878 | */
|
---|
879 | int talloc_increase_ref_count(const void *ptr);
|
---|
880 |
|
---|
881 | /**
|
---|
882 | * @brief Get the number of references to a talloc chunk.
|
---|
883 | *
|
---|
884 | * @param[in] ptr The pointer to retrieve the reference count from.
|
---|
885 | *
|
---|
886 | * @return The number of references.
|
---|
887 | */
|
---|
888 | size_t talloc_reference_count(const void *ptr);
|
---|
889 |
|
---|
890 | #ifdef DOXYGEN
|
---|
891 | /**
|
---|
892 | * @brief Create an additional talloc parent to a pointer.
|
---|
893 | *
|
---|
894 | * The talloc_reference() function makes "context" an additional parent of
|
---|
895 | * ptr. Each additional reference consumes around 48 bytes of memory on intel
|
---|
896 | * x86 platforms.
|
---|
897 | *
|
---|
898 | * If ptr is NULL, then the function is a no-op, and simply returns NULL.
|
---|
899 | *
|
---|
900 | * After creating a reference you can free it in one of the following ways:
|
---|
901 | *
|
---|
902 | * - you can talloc_free() any parent of the original pointer. That
|
---|
903 | * will reduce the number of parents of this pointer by 1, and will
|
---|
904 | * cause this pointer to be freed if it runs out of parents.
|
---|
905 | *
|
---|
906 | * - you can talloc_free() the pointer itself if it has at maximum one
|
---|
907 | * parent. This behaviour has been changed since the release of version
|
---|
908 | * 2.0. Further informations in the description of "talloc_free".
|
---|
909 | *
|
---|
910 | * For more control on which parent to remove, see talloc_unlink()
|
---|
911 | * @param[in] ctx The additional parent.
|
---|
912 | *
|
---|
913 | * @param[in] ptr The pointer you want to create an additional parent for.
|
---|
914 | *
|
---|
915 | * @return The original pointer 'ptr', NULL if talloc ran out of
|
---|
916 | * memory in creating the reference.
|
---|
917 | *
|
---|
918 | * Example:
|
---|
919 | * @code
|
---|
920 | * unsigned int *a, *b, *c;
|
---|
921 | * a = talloc(NULL, unsigned int);
|
---|
922 | * b = talloc(NULL, unsigned int);
|
---|
923 | * c = talloc(a, unsigned int);
|
---|
924 | * // b also serves as a parent of c.
|
---|
925 | * talloc_reference(b, c);
|
---|
926 | * @endcode
|
---|
927 | *
|
---|
928 | * @see talloc_unlink()
|
---|
929 | */
|
---|
930 | void *talloc_reference(const void *ctx, const void *ptr);
|
---|
931 | #else
|
---|
932 | #define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference_loc((ctx),(ptr), __location__)
|
---|
933 | void *_talloc_reference_loc(const void *context, const void *ptr, const char *location);
|
---|
934 | #endif
|
---|
935 |
|
---|
936 | /**
|
---|
937 | * @brief Remove a specific parent from a talloc chunk.
|
---|
938 | *
|
---|
939 | * The function removes a specific parent from ptr. The context passed must
|
---|
940 | * either be a context used in talloc_reference() with this pointer, or must be
|
---|
941 | * a direct parent of ptr.
|
---|
942 | *
|
---|
943 | * You can just use talloc_free() instead of talloc_unlink() if there
|
---|
944 | * is at maximum one parent. This behaviour has been changed since the
|
---|
945 | * release of version 2.0. Further informations in the description of
|
---|
946 | * "talloc_free".
|
---|
947 | *
|
---|
948 | * @param[in] context The talloc parent to remove.
|
---|
949 | *
|
---|
950 | * @param[in] ptr The talloc ptr you want to remove the parent from.
|
---|
951 | *
|
---|
952 | * @return 0 on success, -1 on error.
|
---|
953 | *
|
---|
954 | * @note If the parent has already been removed using talloc_free() then
|
---|
955 | * this function will fail and will return -1. Likewise, if ptr is NULL,
|
---|
956 | * then the function will make no modifications and return -1.
|
---|
957 | *
|
---|
958 | * Example:
|
---|
959 | * @code
|
---|
960 | * unsigned int *a, *b, *c;
|
---|
961 | * a = talloc(NULL, unsigned int);
|
---|
962 | * b = talloc(NULL, unsigned int);
|
---|
963 | * c = talloc(a, unsigned int);
|
---|
964 | * // b also serves as a parent of c.
|
---|
965 | * talloc_reference(b, c);
|
---|
966 | * talloc_unlink(b, c);
|
---|
967 | * @endcode
|
---|
968 | */
|
---|
969 | int talloc_unlink(const void *context, void *ptr);
|
---|
970 |
|
---|
971 | /**
|
---|
972 | * @brief Provide a talloc context that is freed at program exit.
|
---|
973 | *
|
---|
974 | * This is a handy utility function that returns a talloc context
|
---|
975 | * which will be automatically freed on program exit. This can be used
|
---|
976 | * to reduce the noise in memory leak reports.
|
---|
977 | *
|
---|
978 | * Never use this in code that might be used in objects loaded with
|
---|
979 | * dlopen and unloaded with dlclose. talloc_autofree_context()
|
---|
980 | * internally uses atexit(3). Some platforms like modern Linux handles
|
---|
981 | * this fine, but for example FreeBSD does not deal well with dlopen()
|
---|
982 | * and atexit() used simultaneously: dlclose() does not clean up the
|
---|
983 | * list of atexit-handlers, so when the program exits the code that
|
---|
984 | * was registered from within talloc_autofree_context() is gone, the
|
---|
985 | * program crashes at exit.
|
---|
986 | *
|
---|
987 | * @return A talloc context, NULL on error.
|
---|
988 | */
|
---|
989 | void *talloc_autofree_context(void);
|
---|
990 |
|
---|
991 | /**
|
---|
992 | * @brief Get the size of a talloc chunk.
|
---|
993 | *
|
---|
994 | * This function lets you know the amount of memory allocated so far by
|
---|
995 | * this context. It does NOT account for subcontext memory.
|
---|
996 | * This can be used to calculate the size of an array.
|
---|
997 | *
|
---|
998 | * @param[in] ctx The talloc chunk.
|
---|
999 | *
|
---|
1000 | * @return The size of the talloc chunk.
|
---|
1001 | */
|
---|
1002 | size_t talloc_get_size(const void *ctx);
|
---|
1003 |
|
---|
1004 | /**
|
---|
1005 | * @brief Show the parentage of a context.
|
---|
1006 | *
|
---|
1007 | * @param[in] context The talloc context to look at.
|
---|
1008 | *
|
---|
1009 | * @param[in] file The output to use, a file, stdout or stderr.
|
---|
1010 | */
|
---|
1011 | void talloc_show_parents(const void *context, FILE *file);
|
---|
1012 |
|
---|
1013 | /**
|
---|
1014 | * @brief Check if a context is parent of a talloc chunk.
|
---|
1015 | *
|
---|
1016 | * This checks if context is referenced in the talloc hierarchy above ptr.
|
---|
1017 | *
|
---|
1018 | * @param[in] context The assumed talloc context.
|
---|
1019 | *
|
---|
1020 | * @param[in] ptr The talloc chunk to check.
|
---|
1021 | *
|
---|
1022 | * @return Return 1 if this is the case, 0 if not.
|
---|
1023 | */
|
---|
1024 | int talloc_is_parent(const void *context, const void *ptr);
|
---|
1025 |
|
---|
1026 | /**
|
---|
1027 | * @brief Change the parent context of a talloc pointer.
|
---|
1028 | *
|
---|
1029 | * The function changes the parent context of a talloc pointer. It is typically
|
---|
1030 | * used when the context that the pointer is currently a child of is going to be
|
---|
1031 | * freed and you wish to keep the memory for a longer time.
|
---|
1032 | *
|
---|
1033 | * The difference between talloc_reparent() and talloc_steal() is that
|
---|
1034 | * talloc_reparent() can specify which parent you wish to change. This is
|
---|
1035 | * useful when a pointer has multiple parents via references.
|
---|
1036 | *
|
---|
1037 | * @param[in] old_parent
|
---|
1038 | * @param[in] new_parent
|
---|
1039 | * @param[in] ptr
|
---|
1040 | *
|
---|
1041 | * @return Return the pointer you passed. It does not have any
|
---|
1042 | * failure modes.
|
---|
1043 | */
|
---|
1044 | void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr);
|
---|
1045 |
|
---|
1046 | /* @} ******************************************************************/
|
---|
1047 |
|
---|
1048 | /**
|
---|
1049 | * @defgroup talloc_array The talloc array functions
|
---|
1050 | * @ingroup talloc
|
---|
1051 | *
|
---|
1052 | * Talloc contains some handy helpers for handling Arrays conveniently
|
---|
1053 | *
|
---|
1054 | * @{
|
---|
1055 | */
|
---|
1056 |
|
---|
1057 | #ifdef DOXYGEN
|
---|
1058 | /**
|
---|
1059 | * @brief Allocate an array.
|
---|
1060 | *
|
---|
1061 | * The macro is equivalent to:
|
---|
1062 | *
|
---|
1063 | * @code
|
---|
1064 | * (type *)talloc_size(ctx, sizeof(type) * count);
|
---|
1065 | * @endcode
|
---|
1066 | *
|
---|
1067 | * except that it provides integer overflow protection for the multiply,
|
---|
1068 | * returning NULL if the multiply overflows.
|
---|
1069 | *
|
---|
1070 | * @param[in] ctx The talloc context to hang the result off.
|
---|
1071 | *
|
---|
1072 | * @param[in] type The type that we want to allocate.
|
---|
1073 | *
|
---|
1074 | * @param[in] count The number of 'type' elements you want to allocate.
|
---|
1075 | *
|
---|
1076 | * @return The allocated result, properly cast to 'type *', NULL on
|
---|
1077 | * error.
|
---|
1078 | *
|
---|
1079 | * Example:
|
---|
1080 | * @code
|
---|
1081 | * unsigned int *a, *b;
|
---|
1082 | * a = talloc_zero(NULL, unsigned int);
|
---|
1083 | * b = talloc_array(a, unsigned int, 100);
|
---|
1084 | * @endcode
|
---|
1085 | *
|
---|
1086 | * @see talloc()
|
---|
1087 | * @see talloc_zero_array()
|
---|
1088 | */
|
---|
1089 | void *talloc_array(const void *ctx, #type, unsigned count);
|
---|
1090 | #else
|
---|
1091 | #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
|
---|
1092 | void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
|
---|
1093 | #endif
|
---|
1094 |
|
---|
1095 | #ifdef DOXYGEN
|
---|
1096 | /**
|
---|
1097 | * @brief Allocate an array.
|
---|
1098 | *
|
---|
1099 | * @param[in] ctx The talloc context to hang the result off.
|
---|
1100 | *
|
---|
1101 | * @param[in] size The size of an array element.
|
---|
1102 | *
|
---|
1103 | * @param[in] count The number of elements you want to allocate.
|
---|
1104 | *
|
---|
1105 | * @return The allocated result, NULL on error.
|
---|
1106 | */
|
---|
1107 | void *talloc_array_size(const void *ctx, size_t size, unsigned count);
|
---|
1108 | #else
|
---|
1109 | #define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
|
---|
1110 | #endif
|
---|
1111 |
|
---|
1112 | #ifdef DOXYGEN
|
---|
1113 | /**
|
---|
1114 | * @brief Allocate an array into a typed pointer.
|
---|
1115 | *
|
---|
1116 | * The macro should be used when you have a pointer to an array and want to
|
---|
1117 | * allocate memory of an array to point at with this pointer. When compiling
|
---|
1118 | * with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
|
---|
1119 | * and talloc_get_name() will return the current location in the source file
|
---|
1120 | * and not the type.
|
---|
1121 | *
|
---|
1122 | * @param[in] ctx The talloc context to hang the result off.
|
---|
1123 | *
|
---|
1124 | * @param[in] ptr The pointer you want to assign the result to.
|
---|
1125 | *
|
---|
1126 | * @param[in] count The number of elements you want to allocate.
|
---|
1127 | *
|
---|
1128 | * @return The allocated memory chunk, properly casted. NULL on
|
---|
1129 | * error.
|
---|
1130 | */
|
---|
1131 | void *talloc_array_ptrtype(const void *ctx, const void *ptr, unsigned count);
|
---|
1132 | #else
|
---|
1133 | #define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
|
---|
1134 | #endif
|
---|
1135 |
|
---|
1136 | #ifdef DOXYGEN
|
---|
1137 | /**
|
---|
1138 | * @brief Get the number of elements in a talloc'ed array.
|
---|
1139 | *
|
---|
1140 | * A talloc chunk carries its own size, so for talloc'ed arrays it is not
|
---|
1141 | * necessary to store the number of elements explicitly.
|
---|
1142 | *
|
---|
1143 | * @param[in] ctx The allocated array.
|
---|
1144 | *
|
---|
1145 | * @return The number of elements in ctx.
|
---|
1146 | */
|
---|
1147 | size_t talloc_array_length(const void *ctx);
|
---|
1148 | #else
|
---|
1149 | #define talloc_array_length(ctx) (talloc_get_size(ctx)/sizeof(*ctx))
|
---|
1150 | #endif
|
---|
1151 |
|
---|
1152 | #ifdef DOXYGEN
|
---|
1153 | /**
|
---|
1154 | * @brief Allocate a zero-initialized array
|
---|
1155 | *
|
---|
1156 | * @param[in] ctx The talloc context to hang the result off.
|
---|
1157 | *
|
---|
1158 | * @param[in] type The type that we want to allocate.
|
---|
1159 | *
|
---|
1160 | * @param[in] count The number of "type" elements you want to allocate.
|
---|
1161 | *
|
---|
1162 | * @return The allocated result casted to "type *", NULL on error.
|
---|
1163 | *
|
---|
1164 | * The talloc_zero_array() macro is equivalent to:
|
---|
1165 | *
|
---|
1166 | * @code
|
---|
1167 | * ptr = talloc_array(ctx, type, count);
|
---|
1168 | * if (ptr) memset(ptr, sizeof(type) * count);
|
---|
1169 | * @endcode
|
---|
1170 | */
|
---|
1171 | void *talloc_zero_array(const void *ctx, #type, unsigned count);
|
---|
1172 | #else
|
---|
1173 | #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
|
---|
1174 | void *_talloc_zero_array(const void *ctx,
|
---|
1175 | size_t el_size,
|
---|
1176 | unsigned count,
|
---|
1177 | const char *name);
|
---|
1178 | #endif
|
---|
1179 |
|
---|
1180 | #ifdef DOXYGEN
|
---|
1181 | /**
|
---|
1182 | * @brief Change the size of a talloc array.
|
---|
1183 | *
|
---|
1184 | * The macro changes the size of a talloc pointer. The 'count' argument is the
|
---|
1185 | * number of elements of type 'type' that you want the resulting pointer to
|
---|
1186 | * hold.
|
---|
1187 | *
|
---|
1188 | * talloc_realloc() has the following equivalences:
|
---|
1189 | *
|
---|
1190 | * @code
|
---|
1191 | * talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
|
---|
1192 | * talloc_realloc(ctx, NULL, type, N) ==> talloc_array(ctx, type, N);
|
---|
1193 | * talloc_realloc(ctx, ptr, type, 0) ==> talloc_free(ptr);
|
---|
1194 | * @endcode
|
---|
1195 | *
|
---|
1196 | * The "context" argument is only used if "ptr" is NULL, otherwise it is
|
---|
1197 | * ignored.
|
---|
1198 | *
|
---|
1199 | * @param[in] ctx The parent context used if ptr is NULL.
|
---|
1200 | *
|
---|
1201 | * @param[in] ptr The chunk to be resized.
|
---|
1202 | *
|
---|
1203 | * @param[in] type The type of the array element inside ptr.
|
---|
1204 | *
|
---|
1205 | * @param[in] count The intended number of array elements.
|
---|
1206 | *
|
---|
1207 | * @return The new array, NULL on error. The call will fail either
|
---|
1208 | * due to a lack of memory, or because the pointer has more
|
---|
1209 | * than one parent (see talloc_reference()).
|
---|
1210 | */
|
---|
1211 | void *talloc_realloc(const void *ctx, void *ptr, #type, size_t count);
|
---|
1212 | #else
|
---|
1213 | #define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
|
---|
1214 | void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
|
---|
1215 | #endif
|
---|
1216 |
|
---|
1217 | #ifdef DOXYGEN
|
---|
1218 | /**
|
---|
1219 | * @brief Untyped realloc to change the size of a talloc array.
|
---|
1220 | *
|
---|
1221 | * The macro is useful when the type is not known so the typesafe
|
---|
1222 | * talloc_realloc() cannot be used.
|
---|
1223 | *
|
---|
1224 | * @param[in] ctx The parent context used if 'ptr' is NULL.
|
---|
1225 | *
|
---|
1226 | * @param[in] ptr The chunk to be resized.
|
---|
1227 | *
|
---|
1228 | * @param[in] size The new chunk size.
|
---|
1229 | *
|
---|
1230 | * @return The new array, NULL on error.
|
---|
1231 | */
|
---|
1232 | void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);
|
---|
1233 | #else
|
---|
1234 | #define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
|
---|
1235 | void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
|
---|
1236 | #endif
|
---|
1237 |
|
---|
1238 | /**
|
---|
1239 | * @brief Provide a function version of talloc_realloc_size.
|
---|
1240 | *
|
---|
1241 | * This is a non-macro version of talloc_realloc(), which is useful as
|
---|
1242 | * libraries sometimes want a ralloc function pointer. A realloc()
|
---|
1243 | * implementation encapsulates the functionality of malloc(), free() and
|
---|
1244 | * realloc() in one call, which is why it is useful to be able to pass around
|
---|
1245 | * a single function pointer.
|
---|
1246 | *
|
---|
1247 | * @param[in] context The parent context used if ptr is NULL.
|
---|
1248 | *
|
---|
1249 | * @param[in] ptr The chunk to be resized.
|
---|
1250 | *
|
---|
1251 | * @param[in] size The new chunk size.
|
---|
1252 | *
|
---|
1253 | * @return The new chunk, NULL on error.
|
---|
1254 | */
|
---|
1255 | void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
|
---|
1256 |
|
---|
1257 | /* @} ******************************************************************/
|
---|
1258 |
|
---|
1259 | /**
|
---|
1260 | * @defgroup talloc_string The talloc string functions.
|
---|
1261 | * @ingroup talloc
|
---|
1262 | *
|
---|
1263 | * talloc string allocation and manipulation functions.
|
---|
1264 | * @{
|
---|
1265 | */
|
---|
1266 |
|
---|
1267 | /**
|
---|
1268 | * @brief Duplicate a string into a talloc chunk.
|
---|
1269 | *
|
---|
1270 | * This function is equivalent to:
|
---|
1271 | *
|
---|
1272 | * @code
|
---|
1273 | * ptr = talloc_size(ctx, strlen(p)+1);
|
---|
1274 | * if (ptr) memcpy(ptr, p, strlen(p)+1);
|
---|
1275 | * @endcode
|
---|
1276 | *
|
---|
1277 | * This functions sets the name of the new pointer to the passed
|
---|
1278 | * string. This is equivalent to:
|
---|
1279 | *
|
---|
1280 | * @code
|
---|
1281 | * talloc_set_name_const(ptr, ptr)
|
---|
1282 | * @endcode
|
---|
1283 | *
|
---|
1284 | * @param[in] t The talloc context to hang the result off.
|
---|
1285 | *
|
---|
1286 | * @param[in] p The string you want to duplicate.
|
---|
1287 | *
|
---|
1288 | * @return The duplicated string, NULL on error.
|
---|
1289 | */
|
---|
1290 | char *talloc_strdup(const void *t, const char *p);
|
---|
1291 |
|
---|
1292 | /**
|
---|
1293 | * @brief Append a string to given string and duplicate the result.
|
---|
1294 | *
|
---|
1295 | * @param[in] s The destination to append to.
|
---|
1296 | *
|
---|
1297 | * @param[in] a The string you want to append.
|
---|
1298 | *
|
---|
1299 | * @return The duplicated string, NULL on error.
|
---|
1300 | *
|
---|
1301 | * @see talloc_strdup()
|
---|
1302 | */
|
---|
1303 | char *talloc_strdup_append(char *s, const char *a);
|
---|
1304 |
|
---|
1305 | /**
|
---|
1306 | * @brief Append a string to a given buffer and duplicate the result.
|
---|
1307 | *
|
---|
1308 | * @param[in] s The destination buffer to append to.
|
---|
1309 | *
|
---|
1310 | * @param[in] a The string you want to append.
|
---|
1311 | *
|
---|
1312 | * @return The duplicated string, NULL on error.
|
---|
1313 | *
|
---|
1314 | * @see talloc_strdup()
|
---|
1315 | */
|
---|
1316 | char *talloc_strdup_append_buffer(char *s, const char *a);
|
---|
1317 |
|
---|
1318 | /**
|
---|
1319 | * @brief Duplicate a length-limited string into a talloc chunk.
|
---|
1320 | *
|
---|
1321 | * This function is the talloc equivalent of the C library function strndup(3).
|
---|
1322 | *
|
---|
1323 | * This functions sets the name of the new pointer to the passed string. This is
|
---|
1324 | * equivalent to:
|
---|
1325 | *
|
---|
1326 | * @code
|
---|
1327 | * talloc_set_name_const(ptr, ptr)
|
---|
1328 | * @endcode
|
---|
1329 | *
|
---|
1330 | * @param[in] t The talloc context to hang the result off.
|
---|
1331 | *
|
---|
1332 | * @param[in] p The string you want to duplicate.
|
---|
1333 | *
|
---|
1334 | * @param[in] n The maximum string length to duplicate.
|
---|
1335 | *
|
---|
1336 | * @return The duplicated string, NULL on error.
|
---|
1337 | */
|
---|
1338 | char *talloc_strndup(const void *t, const char *p, size_t n);
|
---|
1339 |
|
---|
1340 | /**
|
---|
1341 | * @brief Append at most n characters of a string to given string and duplicate
|
---|
1342 | * the result.
|
---|
1343 | *
|
---|
1344 | * @param[in] s The destination string to append to.
|
---|
1345 | *
|
---|
1346 | * @param[in] a The source string you want to append.
|
---|
1347 | *
|
---|
1348 | * @param[in] n The number of characters you want to append from the
|
---|
1349 | * string.
|
---|
1350 | *
|
---|
1351 | * @return The duplicated string, NULL on error.
|
---|
1352 | *
|
---|
1353 | * @see talloc_strndup()
|
---|
1354 | */
|
---|
1355 | char *talloc_strndup_append(char *s, const char *a, size_t n);
|
---|
1356 |
|
---|
1357 | /**
|
---|
1358 | * @brief Append at most n characters of a string to given buffer and duplicate
|
---|
1359 | * the result.
|
---|
1360 | *
|
---|
1361 | * @param[in] s The destination buffer to append to.
|
---|
1362 | *
|
---|
1363 | * @param[in] a The source string you want to append.
|
---|
1364 | *
|
---|
1365 | * @param[in] n The number of characters you want to append from the
|
---|
1366 | * string.
|
---|
1367 | *
|
---|
1368 | * @return The duplicated string, NULL on error.
|
---|
1369 | *
|
---|
1370 | * @see talloc_strndup()
|
---|
1371 | */
|
---|
1372 | char *talloc_strndup_append_buffer(char *s, const char *a, size_t n);
|
---|
1373 |
|
---|
1374 | /**
|
---|
1375 | * @brief Format a string given a va_list.
|
---|
1376 | *
|
---|
1377 | * This function is the talloc equivalent of the C library function
|
---|
1378 | * vasprintf(3).
|
---|
1379 | *
|
---|
1380 | * This functions sets the name of the new pointer to the new string. This is
|
---|
1381 | * equivalent to:
|
---|
1382 | *
|
---|
1383 | * @code
|
---|
1384 | * talloc_set_name_const(ptr, ptr)
|
---|
1385 | * @endcode
|
---|
1386 | *
|
---|
1387 | * @param[in] t The talloc context to hang the result off.
|
---|
1388 | *
|
---|
1389 | * @param[in] fmt The format string.
|
---|
1390 | *
|
---|
1391 | * @param[in] ap The parameters used to fill fmt.
|
---|
1392 | *
|
---|
1393 | * @return The formatted string, NULL on error.
|
---|
1394 | */
|
---|
1395 | char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
|
---|
1396 |
|
---|
1397 | /**
|
---|
1398 | * @brief Format a string given a va_list and append it to the given destination
|
---|
1399 | * string.
|
---|
1400 | *
|
---|
1401 | * @param[in] s The destination string to append to.
|
---|
1402 | *
|
---|
1403 | * @param[in] fmt The format string.
|
---|
1404 | *
|
---|
1405 | * @param[in] ap The parameters used to fill fmt.
|
---|
1406 | *
|
---|
1407 | * @return The formatted string, NULL on error.
|
---|
1408 | *
|
---|
1409 | * @see talloc_vasprintf()
|
---|
1410 | */
|
---|
1411 | char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
|
---|
1412 |
|
---|
1413 | /**
|
---|
1414 | * @brief Format a string given a va_list and append it to the given destination
|
---|
1415 | * buffer.
|
---|
1416 | *
|
---|
1417 | * @param[in] s The destination buffer to append to.
|
---|
1418 | *
|
---|
1419 | * @param[in] fmt The format string.
|
---|
1420 | *
|
---|
1421 | * @param[in] ap The parameters used to fill fmt.
|
---|
1422 | *
|
---|
1423 | * @return The formatted string, NULL on error.
|
---|
1424 | *
|
---|
1425 | * @see talloc_vasprintf()
|
---|
1426 | */
|
---|
1427 | char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
|
---|
1428 |
|
---|
1429 | /**
|
---|
1430 | * @brief Format a string.
|
---|
1431 | *
|
---|
1432 | * This function is the talloc equivalent of the C library function asprintf(3).
|
---|
1433 | *
|
---|
1434 | * This functions sets the name of the new pointer to the new string. This is
|
---|
1435 | * equivalent to:
|
---|
1436 | *
|
---|
1437 | * @code
|
---|
1438 | * talloc_set_name_const(ptr, ptr)
|
---|
1439 | * @endcode
|
---|
1440 | *
|
---|
1441 | * @param[in] t The talloc context to hang the result off.
|
---|
1442 | *
|
---|
1443 | * @param[in] fmt The format string.
|
---|
1444 | *
|
---|
1445 | * @param[in] ... The parameters used to fill fmt.
|
---|
1446 | *
|
---|
1447 | * @return The formatted string, NULL on error.
|
---|
1448 | */
|
---|
1449 | char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
|
---|
1450 |
|
---|
1451 | /**
|
---|
1452 | * @brief Append a formatted string to another string.
|
---|
1453 | *
|
---|
1454 | * This function appends the given formatted string to the given string. Use
|
---|
1455 | * this variant when the string in the current talloc buffer may have been
|
---|
1456 | * truncated in length.
|
---|
1457 | *
|
---|
1458 | * This functions sets the name of the new pointer to the new
|
---|
1459 | * string. This is equivalent to:
|
---|
1460 | *
|
---|
1461 | * @code
|
---|
1462 | * talloc_set_name_const(ptr, ptr)
|
---|
1463 | * @endcode
|
---|
1464 | *
|
---|
1465 | * @param[in] s The string to append to.
|
---|
1466 | *
|
---|
1467 | * @param[in] fmt The format string.
|
---|
1468 | *
|
---|
1469 | * @param[in] ... The parameters used to fill fmt.
|
---|
1470 | *
|
---|
1471 | * @return The formatted string, NULL on error.
|
---|
1472 | */
|
---|
1473 | char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
|
---|
1474 |
|
---|
1475 | /**
|
---|
1476 | * @brief Append a formatted string to another string.
|
---|
1477 | *
|
---|
1478 | * @param[in] s The string to append to
|
---|
1479 | *
|
---|
1480 | * @param[in] fmt The format string.
|
---|
1481 | *
|
---|
1482 | * @param[in] ... The parameters used to fill fmt.
|
---|
1483 | *
|
---|
1484 | * @return The formatted string, NULL on error.
|
---|
1485 | */
|
---|
1486 | char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
|
---|
1487 |
|
---|
1488 | /* @} ******************************************************************/
|
---|
1489 |
|
---|
1490 | /**
|
---|
1491 | * @defgroup talloc_debug The talloc debugging support functions
|
---|
1492 | * @ingroup talloc
|
---|
1493 | *
|
---|
1494 | * To aid memory debugging, talloc contains routines to inspect the currently
|
---|
1495 | * allocated memory hierarchy.
|
---|
1496 | *
|
---|
1497 | * @{
|
---|
1498 | */
|
---|
1499 |
|
---|
1500 | /**
|
---|
1501 | * @brief Walk a complete talloc hierarchy.
|
---|
1502 | *
|
---|
1503 | * This provides a more flexible reports than talloc_report(). It
|
---|
1504 | * will recursively call the callback for the entire tree of memory
|
---|
1505 | * referenced by the pointer. References in the tree are passed with
|
---|
1506 | * is_ref = 1 and the pointer that is referenced.
|
---|
1507 | *
|
---|
1508 | * You can pass NULL for the pointer, in which case a report is
|
---|
1509 | * printed for the top level memory context, but only if
|
---|
1510 | * talloc_enable_leak_report() or talloc_enable_leak_report_full()
|
---|
1511 | * has been called.
|
---|
1512 | *
|
---|
1513 | * The recursion is stopped when depth >= max_depth.
|
---|
1514 | * max_depth = -1 means only stop at leaf nodes.
|
---|
1515 | *
|
---|
1516 | * @param[in] ptr The talloc chunk.
|
---|
1517 | *
|
---|
1518 | * @param[in] depth Internal parameter to control recursion. Call with 0.
|
---|
1519 | *
|
---|
1520 | * @param[in] max_depth Maximum recursion level.
|
---|
1521 | *
|
---|
1522 | * @param[in] callback Function to be called on every chunk.
|
---|
1523 | *
|
---|
1524 | * @param[in] private_data Private pointer passed to callback.
|
---|
1525 | */
|
---|
1526 | void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
|
---|
1527 | void (*callback)(const void *ptr,
|
---|
1528 | int depth, int max_depth,
|
---|
1529 | int is_ref,
|
---|
1530 | void *private_data),
|
---|
1531 | void *private_data);
|
---|
1532 |
|
---|
1533 | /**
|
---|
1534 | * @brief Print a talloc hierarchy.
|
---|
1535 | *
|
---|
1536 | * This provides a more flexible reports than talloc_report(). It
|
---|
1537 | * will let you specify the depth and max_depth.
|
---|
1538 | *
|
---|
1539 | * @param[in] ptr The talloc chunk.
|
---|
1540 | *
|
---|
1541 | * @param[in] depth Internal parameter to control recursion. Call with 0.
|
---|
1542 | *
|
---|
1543 | * @param[in] max_depth Maximum recursion level.
|
---|
1544 | *
|
---|
1545 | * @param[in] f The file handle to print to.
|
---|
1546 | */
|
---|
1547 | void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
|
---|
1548 |
|
---|
1549 | /**
|
---|
1550 | * @brief Print a summary report of all memory used by ptr.
|
---|
1551 | *
|
---|
1552 | * This provides a more detailed report than talloc_report(). It will
|
---|
1553 | * recursively print the entire tree of memory referenced by the
|
---|
1554 | * pointer. References in the tree are shown by giving the name of the
|
---|
1555 | * pointer that is referenced.
|
---|
1556 | *
|
---|
1557 | * You can pass NULL for the pointer, in which case a report is printed
|
---|
1558 | * for the top level memory context, but only if
|
---|
1559 | * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
|
---|
1560 | * been called.
|
---|
1561 | *
|
---|
1562 | * @param[in] ptr The talloc chunk.
|
---|
1563 | *
|
---|
1564 | * @param[in] f The file handle to print to.
|
---|
1565 | *
|
---|
1566 | * Example:
|
---|
1567 | * @code
|
---|
1568 | * unsigned int *a, *b;
|
---|
1569 | * a = talloc(NULL, unsigned int);
|
---|
1570 | * b = talloc(a, unsigned int);
|
---|
1571 | * fprintf(stderr, "Dumping memory tree for a:\n");
|
---|
1572 | * talloc_report_full(a, stderr);
|
---|
1573 | * @endcode
|
---|
1574 | *
|
---|
1575 | * @see talloc_report()
|
---|
1576 | */
|
---|
1577 | void talloc_report_full(const void *ptr, FILE *f);
|
---|
1578 |
|
---|
1579 | /**
|
---|
1580 | * @brief Print a summary report of all memory used by ptr.
|
---|
1581 | *
|
---|
1582 | * This function prints a summary report of all memory used by ptr. One line of
|
---|
1583 | * report is printed for each immediate child of ptr, showing the total memory
|
---|
1584 | * and number of blocks used by that child.
|
---|
1585 | *
|
---|
1586 | * You can pass NULL for the pointer, in which case a report is printed
|
---|
1587 | * for the top level memory context, but only if talloc_enable_leak_report()
|
---|
1588 | * or talloc_enable_leak_report_full() has been called.
|
---|
1589 | *
|
---|
1590 | * @param[in] ptr The talloc chunk.
|
---|
1591 | *
|
---|
1592 | * @param[in] f The file handle to print to.
|
---|
1593 | *
|
---|
1594 | * Example:
|
---|
1595 | * @code
|
---|
1596 | * unsigned int *a, *b;
|
---|
1597 | * a = talloc(NULL, unsigned int);
|
---|
1598 | * b = talloc(a, unsigned int);
|
---|
1599 | * fprintf(stderr, "Summary of memory tree for a:\n");
|
---|
1600 | * talloc_report(a, stderr);
|
---|
1601 | * @endcode
|
---|
1602 | *
|
---|
1603 | * @see talloc_report_full()
|
---|
1604 | */
|
---|
1605 | void talloc_report(const void *ptr, FILE *f);
|
---|
1606 |
|
---|
1607 | /**
|
---|
1608 | * @brief Enable tracking the use of NULL memory contexts.
|
---|
1609 | *
|
---|
1610 | * This enables tracking of the NULL memory context without enabling leak
|
---|
1611 | * reporting on exit. Useful for when you want to do your own leak
|
---|
1612 | * reporting call via talloc_report_null_full();
|
---|
1613 | */
|
---|
1614 | void talloc_enable_null_tracking(void);
|
---|
1615 |
|
---|
1616 | /**
|
---|
1617 | * @brief Enable tracking the use of NULL memory contexts.
|
---|
1618 | *
|
---|
1619 | * This enables tracking of the NULL memory context without enabling leak
|
---|
1620 | * reporting on exit. Useful for when you want to do your own leak
|
---|
1621 | * reporting call via talloc_report_null_full();
|
---|
1622 | */
|
---|
1623 | void talloc_enable_null_tracking_no_autofree(void);
|
---|
1624 |
|
---|
1625 | /**
|
---|
1626 | * @brief Disable tracking of the NULL memory context.
|
---|
1627 | *
|
---|
1628 | * This disables tracking of the NULL memory context.
|
---|
1629 | */
|
---|
1630 | void talloc_disable_null_tracking(void);
|
---|
1631 |
|
---|
1632 | /**
|
---|
1633 | * @brief Enable leak report when a program exits.
|
---|
1634 | *
|
---|
1635 | * This enables calling of talloc_report(NULL, stderr) when the program
|
---|
1636 | * exits. In Samba4 this is enabled by using the --leak-report command
|
---|
1637 | * line option.
|
---|
1638 | *
|
---|
1639 | * For it to be useful, this function must be called before any other
|
---|
1640 | * talloc function as it establishes a "null context" that acts as the
|
---|
1641 | * top of the tree. If you don't call this function first then passing
|
---|
1642 | * NULL to talloc_report() or talloc_report_full() won't give you the
|
---|
1643 | * full tree printout.
|
---|
1644 | *
|
---|
1645 | * Here is a typical talloc report:
|
---|
1646 | *
|
---|
1647 | * @code
|
---|
1648 | * talloc report on 'null_context' (total 267 bytes in 15 blocks)
|
---|
1649 | * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
|
---|
1650 | * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
|
---|
1651 | * iconv(UTF8,CP850) contains 42 bytes in 2 blocks
|
---|
1652 | * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
|
---|
1653 | * iconv(CP850,UTF8) contains 42 bytes in 2 blocks
|
---|
1654 | * iconv(UTF8,UTF-16LE) contains 45 bytes in 2 blocks
|
---|
1655 | * iconv(UTF-16LE,UTF8) contains 45 bytes in 2 blocks
|
---|
1656 | * @endcode
|
---|
1657 | */
|
---|
1658 | void talloc_enable_leak_report(void);
|
---|
1659 |
|
---|
1660 | /**
|
---|
1661 | * @brief Enable full leak report when a program exits.
|
---|
1662 | *
|
---|
1663 | * This enables calling of talloc_report_full(NULL, stderr) when the
|
---|
1664 | * program exits. In Samba4 this is enabled by using the
|
---|
1665 | * --leak-report-full command line option.
|
---|
1666 | *
|
---|
1667 | * For it to be useful, this function must be called before any other
|
---|
1668 | * talloc function as it establishes a "null context" that acts as the
|
---|
1669 | * top of the tree. If you don't call this function first then passing
|
---|
1670 | * NULL to talloc_report() or talloc_report_full() won't give you the
|
---|
1671 | * full tree printout.
|
---|
1672 | *
|
---|
1673 | * Here is a typical full report:
|
---|
1674 | *
|
---|
1675 | * @code
|
---|
1676 | * full talloc report on 'root' (total 18 bytes in 8 blocks)
|
---|
1677 | * p1 contains 18 bytes in 7 blocks (ref 0)
|
---|
1678 | * r1 contains 13 bytes in 2 blocks (ref 0)
|
---|
1679 | * reference to: p2
|
---|
1680 | * p2 contains 1 bytes in 1 blocks (ref 1)
|
---|
1681 | * x3 contains 1 bytes in 1 blocks (ref 0)
|
---|
1682 | * x2 contains 1 bytes in 1 blocks (ref 0)
|
---|
1683 | * x1 contains 1 bytes in 1 blocks (ref 0)
|
---|
1684 | * @endcode
|
---|
1685 | */
|
---|
1686 | void talloc_enable_leak_report_full(void);
|
---|
1687 |
|
---|
1688 | /* @} ******************************************************************/
|
---|
1689 |
|
---|
1690 | void talloc_set_abort_fn(void (*abort_fn)(const char *reason));
|
---|
1691 | void talloc_set_log_fn(void (*log_fn)(const char *message));
|
---|
1692 | void talloc_set_log_stderr(void);
|
---|
1693 |
|
---|
1694 | #if TALLOC_DEPRECATED
|
---|
1695 | #define talloc_zero_p(ctx, type) talloc_zero(ctx, type)
|
---|
1696 | #define talloc_p(ctx, type) talloc(ctx, type)
|
---|
1697 | #define talloc_array_p(ctx, type, count) talloc_array(ctx, type, count)
|
---|
1698 | #define talloc_realloc_p(ctx, p, type, count) talloc_realloc(ctx, p, type, count)
|
---|
1699 | #define talloc_destroy(ctx) talloc_free(ctx)
|
---|
1700 | #define talloc_append_string(c, s, a) (s?talloc_strdup_append(s,a):talloc_strdup(c, a))
|
---|
1701 | #endif
|
---|
1702 |
|
---|
1703 | #ifndef TALLOC_MAX_DEPTH
|
---|
1704 | #define TALLOC_MAX_DEPTH 10000
|
---|
1705 | #endif
|
---|
1706 |
|
---|
1707 | #ifdef __cplusplus
|
---|
1708 | } /* end of extern "C" */
|
---|
1709 | #endif
|
---|
1710 |
|
---|
1711 | #endif
|
---|