]> git.ozlabs.org Git - ccan/blob - ccan/talloc/talloc.h
Fix "make check": this is temporary until we use ccanlint for it.
[ccan] / ccan / talloc / talloc.h
1 #ifndef CCAN_TALLOC_H
2 #define CCAN_TALLOC_H
3 /* 
4    Copyright (C) Andrew Tridgell 2004-2005
5    Copyright (C) Stefan Metzmacher 2006
6    
7      ** NOTE! The following LGPL license applies to the talloc
8      ** library. This does NOT imply that all of Samba is released
9      ** under the LGPL
10    
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 2 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20
21    You should have received a copy of the GNU Lesser General Public
22    License along with this library; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <ccan/typesafe_cb/typesafe_cb.h>
30 #include "config.h"
31
32 /*
33   this uses a little trick to allow __LINE__ to be stringified
34 */
35 #ifndef __location__
36 #define __TALLOC_STRING_LINE1__(s)    #s
37 #define __TALLOC_STRING_LINE2__(s)   __TALLOC_STRING_LINE1__(s)
38 #define __TALLOC_STRING_LINE3__  __TALLOC_STRING_LINE2__(__LINE__)
39 #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__
40 #endif
41
42 #if HAVE_ATTRIBUTE_PRINTF
43 /** Use gcc attribute to check printf fns.  a1 is the 1-based index of
44  * the parameter containing the format, and a2 the index of the first
45  * argument. Note that some gcc 2.x versions don't handle this
46  * properly **/
47 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
48 #else
49 #define PRINTF_ATTRIBUTE(a1, a2)
50 #endif
51
52 /* try to make talloc_set_destructor() and talloc_steal() type safe,
53    if we have a recent gcc */
54 #if HAVE_TYPEOF
55 #define _TALLOC_TYPEOF(ptr) __typeof__(ptr)
56 #else
57 #define _TALLOC_TYPEOF(ptr) void *
58 #endif
59
60 #define talloc_move(ctx, ptr) (_TALLOC_TYPEOF(*(ptr)))_talloc_move((ctx),(void *)(ptr))
61
62 /**
63  * talloc - allocate dynamic memory for a type
64  * @ctx: context to be parent of this allocation, or NULL.
65  * @type: the type to be allocated.
66  *
67  * The talloc() macro is the core of the talloc library. It takes a memory
68  * context and a type, and returns a pointer to a new area of memory of the
69  * given type.
70  *
71  * The returned pointer is itself a talloc context, so you can use it as the
72  * context argument to more calls to talloc if you wish.
73  *
74  * The returned pointer is a "child" of @ctx. This means that if you
75  * talloc_free() @ctx then the new child disappears as well.  Alternatively you
76  * can free just the child.
77  *
78  * @ctx can be NULL, in which case a new top level context is created.
79  *
80  * Example:
81  *      unsigned int *a, *b;
82  *      a = talloc(NULL, unsigned int);
83  *      b = talloc(a, unsigned int);
84  *
85  * See Also:
86  *      talloc_zero, talloc_array, talloc_steal, talloc_free.
87  */
88 #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
89
90 /**
91  * talloc_free - free talloc'ed memory and its children
92  * @ptr: the talloced pointer to free
93  *
94  * The talloc_free() function frees a piece of talloc memory, and all its
95  * children. You can call talloc_free() on any pointer returned by talloc().
96  *
97  * The return value of talloc_free() indicates success or failure, with 0
98  * returned for success and -1 for failure. The only possible failure condition
99  * is if the pointer had a destructor attached to it and the destructor
100  * returned -1. See talloc_set_destructor() for details on destructors.
101  * errno will be preserved unless the talloc_free fails.
102  *
103  * If this pointer has an additional parent when talloc_free() is called then
104  * the memory is not actually released, but instead the most recently
105  * established parent is destroyed. See talloc_reference() for details on
106  * establishing additional parents.
107  *
108  * For more control on which parent is removed, see talloc_unlink().
109  *
110  * talloc_free() operates recursively on its children.
111  *
112  * Example:
113  *      unsigned int *a, *b;
114  *      a = talloc(NULL, unsigned int);
115  *      b = talloc(a, unsigned int);
116  *      // Frees a and b
117  *      talloc_free(a);
118  *
119  * See Also:
120  *      talloc_set_destructor, talloc_unlink
121  */
122 int talloc_free(const void *ptr);
123
124 /**
125  * talloc_set_destructor: set a destructor for when this pointer is freed
126  * @ptr: the talloc pointer to set the destructor on
127  * @destructor: the function to be called
128  *
129  * The function talloc_set_destructor() sets the "destructor" for the pointer
130  * @ptr.  A destructor is a function that is called when the memory used by a
131  * pointer is about to be released.  The destructor receives the pointer as an
132  * argument, and should return 0 for success and -1 for failure.
133  *
134  * The destructor can do anything it wants to, including freeing other pieces
135  * of memory. A common use for destructors is to clean up operating system
136  * resources (such as open file descriptors) contained in the structure the
137  * destructor is placed on.
138  *
139  * You can only place one destructor on a pointer. If you need more than one
140  * destructor then you can create a zero-length child of the pointer and place
141  * an additional destructor on that.
142  *
143  * To remove a destructor call talloc_set_destructor() with NULL for the
144  * destructor.
145  *
146  * If your destructor attempts to talloc_free() the pointer that it is the
147  * destructor for then talloc_free() will return -1 and the free will be
148  * ignored. This would be a pointless operation anyway, as the destructor is
149  * only called when the memory is just about to go away.
150  *
151  * Example:
152  * static int destroy_fd(int *fd)
153  * {
154  *      close(*fd);
155  *      return 0;
156  * }
157  *
158  * int *open_file(const char *filename)
159  * {
160  *      int *fd = talloc(NULL, int);
161  *      *fd = open(filename, O_RDONLY);
162  *      if (*fd < 0) {
163  *              talloc_free(fd);
164  *              return NULL;
165  *      }
166  *      // Whenever they free this, we close the file.
167  *      talloc_set_destructor(fd, destroy_fd);
168  *      return fd;
169  * }
170  *
171  * See Also:
172  *      talloc, talloc_free
173  */
174 #define talloc_set_destructor(ptr, function)                                  \
175         _talloc_set_destructor((ptr), typesafe_cb(int, (function), (ptr)))
176
177 /**
178  * talloc_zero - allocate zeroed dynamic memory for a type
179  * @ctx: context to be parent of this allocation, or NULL.
180  * @type: the type to be allocated.
181  *
182  * The talloc_zero() macro is equivalent to:
183  *
184  *  ptr = talloc(ctx, type);
185  *  if (ptr) memset(ptr, 0, sizeof(type));
186  *
187  * Example:
188  *      unsigned int *a, *b;
189  *      a = talloc_zero(NULL, unsigned int);
190  *      b = talloc_zero(a, unsigned int);
191  *
192  * See Also:
193  *      talloc, talloc_zero_size, talloc_zero_array
194  */
195 #define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
196
197 /**
198  * talloc_array - allocate dynamic memory for an array of a given type
199  * @ctx: context to be parent of this allocation, or NULL.
200  * @type: the type to be allocated.
201  * @count: the number of elements to be allocated.
202  *
203  * The talloc_array() macro is a safe way of allocating an array.  It is
204  * equivalent to:
205  *
206  *  (type *)talloc_size(ctx, sizeof(type) * count);
207  *
208  * except that it provides integer overflow protection for the multiply,
209  * returning NULL if the multiply overflows.
210  *
211  * Example:
212  *      unsigned int *a, *b;
213  *      a = talloc_zero(NULL, unsigned int);
214  *      b = talloc_array(a, unsigned int, 100);
215  *
216  * See Also:
217  *      talloc, talloc_zero_array
218  */
219 #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
220
221 /**
222  * talloc_size - allocate a particular size of memory
223  * @ctx: context to be parent of this allocation, or NULL.
224  * @size: the number of bytes to allocate
225  *
226  * The function talloc_size() should be used when you don't have a convenient
227  * type to pass to talloc(). Unlike talloc(), it is not type safe (as it
228  * returns a void *), so you are on your own for type checking.
229  *
230  * Best to use talloc() or talloc_array() instead.
231  *
232  * Example:
233  *      void *mem = talloc_size(NULL, 100);
234  *
235  * See Also:
236  *      talloc, talloc_array, talloc_zero_size
237  */
238 #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
239
240 #ifdef HAVE_TYPEOF
241 /**
242  * talloc_steal - change/set the parent context of a talloc pointer
243  * @ctx: the new parent
244  * @ptr: the talloc pointer to reparent
245  *
246  * The talloc_steal() function changes the parent context of a talloc
247  * pointer. It is typically used when the context that the pointer is currently
248  * a child of is going to be freed and you wish to keep the memory for a longer
249  * time.
250  *
251  * The talloc_steal() function returns the pointer that you pass it. It does
252  * not have any failure modes.
253  *
254  * NOTE: It is possible to produce loops in the parent/child relationship if
255  * you are not careful with talloc_steal(). No guarantees are provided as to
256  * your sanity or the safety of your data if you do this.
257  *
258  * talloc_steal (new_ctx, NULL) will return NULL with no sideeffects.
259  *
260  * Example:
261  *      unsigned int *a, *b;
262  *      a = talloc(NULL, unsigned int);
263  *      b = talloc(NULL, unsigned int);
264  *      // Reparent b to a as if we'd done 'b = talloc(a, unsigned int)'.
265  *      talloc_steal(a, b);
266  *
267  * See Also:
268  *      talloc_reference
269  */
270 #define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) _talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr)); _talloc_steal_ret; }) /* this extremely strange macro is to avoid some braindamaged warning stupidity in gcc 4.1.x */
271 #else
272 #define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr))
273 #endif /* HAVE_TYPEOF */
274
275 /**
276  * talloc_report_full - report all the memory used by a pointer and children.
277  * @ptr: the context to report on
278  * @f: the file to report to
279  *
280  * Recursively print the entire tree of memory referenced by the
281  * pointer. References in the tree are shown by giving the name of the pointer
282  * that is referenced.
283  *
284  * You can pass NULL for the pointer, in which case a report is printed for the
285  * top level memory context, but only if talloc_enable_null_tracking() has been
286  * called.
287  *
288  * Example:
289  *      unsigned int *a, *b;
290  *      a = talloc(NULL, unsigned int);
291  *      b = talloc(a, unsigned int);
292  *      fprintf(stderr, "Dumping memory tree for a:\n");
293  *      talloc_report_full(a, stderr);
294  *
295  * See Also:
296  *      talloc_report
297  */
298 void talloc_report_full(const void *ptr, FILE *f);
299
300 /**
301  * talloc_reference - add an additional parent to a context
302  * @ctx: the additional parent
303  * @ptr: the talloc pointer
304  *
305  * The talloc_reference() function makes @ctx an additional parent of @ptr.
306  *
307  * The return value of talloc_reference() is always the original pointer @ptr,
308  * unless talloc ran out of memory in creating the reference in which case it
309  * will return NULL (each additional reference consumes around 48 bytes of
310  * memory on intel x86 platforms).
311  *
312  * If @ptr is NULL, then the function is a no-op, and simply returns NULL.
313  *
314  * After creating a reference you can free it in one of the following ways:
315  *
316  *  - you can talloc_free() any parent of the original pointer. That will
317  *    reduce the number of parents of this pointer by 1, and will cause this
318  *    pointer to be freed if it runs out of parents.
319  *
320  *  - you can talloc_free() the pointer itself. That will destroy the most
321  *    recently established parent to the pointer and leave the pointer as a
322  *    child of its current parent.
323  *
324  * For more control on which parent to remove, see talloc_unlink().
325  * Example:
326  *      unsigned int *a, *b, *c;
327  *      a = talloc(NULL, unsigned int);
328  *      b = talloc(NULL, unsigned int);
329  *      c = talloc(a, unsigned int);
330  *      // b also serves as a parent of c.
331  *      talloc_reference(b, c);
332  */
333 #define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference((ctx),(ptr))
334
335 /**
336  * talloc_unlink: remove a specific parent from a talloc pointer.
337  * @context: the parent to remove
338  * @ptr: the talloc pointer
339  *
340  * The talloc_unlink() function removes a specific parent from @ptr. The
341  * context passed must either be a context used in talloc_reference() with this
342  * pointer, or must be a direct parent of @ptr.
343  *
344  * Note that if the parent has already been removed using talloc_free() then
345  * this function will fail and will return -1.  Likewise, if @ptr is NULL,
346  * then the function will make no modifications and return -1.
347  *
348  * Usually you can just use talloc_free() instead of talloc_unlink(), but
349  * sometimes it is useful to have the additional control on which parent is
350  * removed.
351  * Example:
352  *      unsigned int *a, *b, *c;
353  *      a = talloc(NULL, unsigned int);
354  *      b = talloc(NULL, unsigned int);
355  *      c = talloc(a, unsigned int);
356  *      // b also serves as a parent of c.
357  *      talloc_reference(b, c);
358  *      talloc_unlink(b, c);
359  */
360 int talloc_unlink(const void *context, void *ptr);
361
362 /**
363  * talloc_report - print a summary of memory used by a pointer
364  *
365  * The talloc_report() function prints a summary report of all memory
366  * used by @ptr.  One line of report is printed for each immediate child of
367  * @ptr, showing the total memory and number of blocks used by that child.
368  *
369  * You can pass NULL for the pointer, in which case a report is printed for the
370  * top level memory context, but only if talloc_enable_null_tracking() has been
371  * called.
372  *
373  * Example:
374  *      unsigned int *a, *b;
375  *      a = talloc(NULL, unsigned int);
376  *      b = talloc(a, unsigned int);
377  *      fprintf(stderr, "Summary of memory tree for a:\n");
378  *      talloc_report(a, stderr);
379  *
380  * See Also:
381  *      talloc_report_full
382  */
383 void talloc_report(const void *ptr, FILE *f);
384
385 /**
386  * talloc_ptrtype - allocate a size of memory suitable for this pointer
387  * @ctx: context to be parent of this allocation, or NULL.
388  * @ptr: the pointer whose type we are to allocate
389  *
390  * The talloc_ptrtype() macro should be used when you have a pointer and
391  * want to allocate memory to point at with this pointer. When compiling
392  * with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()
393  * and talloc_get_name() will return the current location in the source file.
394  * and not the type.
395  *
396  * Example:
397  *      unsigned int *a = talloc_ptrtype(NULL, a);
398  */
399 #define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
400
401 /**
402  * talloc_new - create a new context
403  * @ctx: the context to use as a parent.
404  *
405  * This is a utility macro that creates a new memory context hanging off an
406  * exiting context, automatically naming it "talloc_new: __location__" where
407  * __location__ is the source line it is called from. It is particularly useful
408  * for creating a new temporary working context.
409  */
410 #define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
411
412 /**
413  * talloc_zero_size -  allocate a particular size of zeroed memory
414  *
415  * The talloc_zero_size() function is useful when you don't have a known type.
416  */
417 #define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
418
419 /**
420  * talloc_zero_array -  allocate an array of zeroed types
421  * @ctx: context to be parent of this allocation, or NULL.
422  * @type: the type to be allocated.
423  * @count: the number of elements to be allocated.
424  *
425  * Just like talloc_array, but zeroes the memory.
426  */
427 #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
428
429 /**
430  * talloc_array_size - allocate an array of elements of the given size
431  * @ctx: context to be parent of this allocation, or NULL.
432  * @size: the size of each element
433  * @count: the number of elements to be allocated.
434  *
435  * Typeless form of talloc_array.
436  */
437 #define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
438
439 /**
440  * talloc_array_ptrtype - allocate an array of memory suitable for this pointer
441  * @ctx: context to be parent of this allocation, or NULL.
442  * @ptr: the pointer whose type we are to allocate
443  * @count: the number of elements for the array
444  *
445  * Like talloc_ptrtype(), except it allocates an array.
446  */
447 #define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
448
449 /**
450  * talloc_realloc - resize a talloc array
451  * @ctx: the parent to assign (if p is NULL)
452  * @p: the memory to reallocate
453  * @type: the type of the object to allocate
454  * @count: the number of objects to reallocate
455  *
456  * The talloc_realloc() macro changes the size of a talloc pointer. The "count"
457  * argument is the number of elements of type "type" that you want the
458  * resulting pointer to hold.
459  *
460  * talloc_realloc() has the following equivalences:
461  *
462  *  talloc_realloc(context, NULL, type, 1) ==> talloc(context, type);
463  *  talloc_realloc(context, NULL, type, N) ==> talloc_array(context, type, N);
464  *  talloc_realloc(context, ptr, type, 0)  ==> talloc_free(ptr);
465  *
466  * The "context" argument is only used if "ptr" is NULL, otherwise it is
467  * ignored.
468  *
469  * talloc_realloc() returns the new pointer, or NULL on failure. The call will
470  * fail either due to a lack of memory, or because the pointer has more than
471  * one parent (see talloc_reference()).
472  */
473 #define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
474
475 /**
476  * talloc_realloc_size - resize talloc memory
477  * @ctx: the parent to assign (if p is NULL)
478  * @ptr: the memory to reallocate
479  * @size: the new size of memory.
480  *
481  * The talloc_realloc_size() function is useful when the type is not known so
482  * the typesafe talloc_realloc() cannot be used.
483  */
484 #define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
485
486 /**
487  * talloc_strdup - duplicate a string
488  * @ctx: the talloc context for the new string
489  * @p: the string to copy
490  *
491  * The talloc_strdup() function is equivalent to:
492  *
493  *  ptr = talloc_size(ctx, strlen(p)+1);
494  *  if (ptr) memcpy(ptr, p, strlen(p)+1);
495  *
496  * This functions sets the name of the new pointer to the passed string. This
497  * is equivalent to:
498  *
499  *  talloc_set_name_const(ptr, ptr)
500  */
501 char *talloc_strdup(const void *t, const char *p);
502
503 /**
504  * talloc_strndup - duplicate a limited length of a string
505  * @ctx: the talloc context for the new string
506  * @p: the string to copy
507  * @n: the maximum length of the returned string.
508  *
509  * The talloc_strndup() function is the talloc equivalent of the C library
510  * function strndup(): the result will be truncated to @n characters before
511  * the nul terminator.
512  *
513  * This functions sets the name of the new pointer to the passed string. This
514  * is equivalent to:
515  *
516  *   talloc_set_name_const(ptr, ptr)
517  */
518 char *talloc_strndup(const void *t, const char *p, size_t n);
519
520 /**
521  * talloc_memdup - duplicate some talloc memory
522  *
523  * The talloc_memdup() function is equivalent to:
524  *
525  *  ptr = talloc_size(ctx, size);
526  *  if (ptr) memcpy(ptr, p, size);
527  */
528 #define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
529
530 /**
531  * talloc_asprintf - sprintf into a talloc buffer.
532  * @t: The context to allocate the buffer from
533  * @fmt: printf-style format for the buffer.
534  *
535  * The talloc_asprintf() function is the talloc equivalent of the C library
536  * function asprintf().
537  *
538  * This functions sets the name of the new pointer to the new string. This is
539  * equivalent to:
540  *
541  *   talloc_set_name_const(ptr, ptr)
542  */
543 char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
544
545 /**
546  * talloc_append_string - concatenate onto a tallocated string 
547  * @orig: the tallocated string to append to
548  * @append: the string to add, or NULL to add nothing.
549  *
550  * The talloc_append_string() function appends the given formatted string to
551  * the given string.
552  *
553  * This function sets the name of the new pointer to the new string. This is
554  * equivalent to:
555  *
556  *    talloc_set_name_const(ptr, ptr)
557  */
558 char *talloc_append_string(char *orig, const char *append);
559
560 /**
561  * talloc_asprintf_append - sprintf onto the end of a talloc buffer.
562  * @s: The tallocated string buffer
563  * @fmt: printf-style format to append to the buffer.
564  *
565  * The talloc_asprintf_append() function appends the given formatted string to
566  * the given string.
567  *
568  * This functions sets the name of the new pointer to the new string. This is
569  * equivalent to:
570  *   talloc_set_name_const(ptr, ptr)
571  */
572 char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
573
574 /**
575  * talloc_vasprintf - vsprintf into a talloc buffer.
576  * @t: The context to allocate the buffer from
577  * @fmt: printf-style format for the buffer
578  * @ap: va_list arguments
579  *
580  * The talloc_vasprintf() function is the talloc equivalent of the C library
581  * function vasprintf()
582  *
583  * This functions sets the name of the new pointer to the new string. This is
584  * equivalent to:
585  *
586  *   talloc_set_name_const(ptr, ptr)
587  */
588 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
589
590 /**
591  * talloc_vasprintf_append - sprintf onto the end of a talloc buffer.
592  * @t: The context to allocate the buffer from
593  * @fmt: printf-style format for the buffer
594  * @ap: va_list arguments
595  *
596  * The talloc_vasprintf_append() function is equivalent to
597  * talloc_asprintf_append(), except it takes a va_list.
598  */
599 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
600
601 /**
602  * talloc_set_type - force the name of a pointer to a particular type
603  * @ptr: the talloc pointer
604  * @type: the type whose name to set the ptr name to.
605  *
606  * This macro allows you to force the name of a pointer to be a particular
607  * type. This can be used in conjunction with talloc_get_type() to do type
608  * checking on void* pointers.
609  *
610  * It is equivalent to this:
611  *   talloc_set_name_const(ptr, #type)
612  */
613 #define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
614
615 /**
616  * talloc_get_type - convert a talloced pointer with typechecking
617  * @ptr: the talloc pointer
618  * @type: the type which we expect the talloced pointer to be.
619  *
620  * This macro allows you to do type checking on talloc pointers. It is
621  * particularly useful for void* private pointers. It is equivalent to this:
622  *
623  *   (type *)talloc_check_name(ptr, #type)
624  */
625 #define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
626
627 /**
628  * talloc_find_parent_byname - find a talloc parent by type
629  * @ptr: the talloc pointer
630  * @type: the type we're looking for
631  *
632  * Find a parent memory context of the current context that has the given
633  * name. This can be very useful in complex programs where it may be difficult
634  * to pass all information down to the level you need, but you know the
635  * structure you want is a parent of another context.
636  */
637 #define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
638
639 /**
640  * talloc_increase_ref_count - hold a reference to a talloc pointer
641  * @ptr: the talloc pointer
642  *
643  * The talloc_increase_ref_count(ptr) function is exactly equivalent to:
644  *
645  *  talloc_reference(NULL, ptr);
646  *
647  * You can use either syntax, depending on which you think is clearer in your
648  * code.
649  *
650  * It returns 0 on success and -1 on failure.
651  */
652 int talloc_increase_ref_count(const void *ptr);
653
654 /**
655  * talloc_set_name - set the name for a talloc pointer
656  * @ptr: the talloc pointer
657  * @fmt: the printf-style format string for the name
658  *
659  * Each talloc pointer has a "name". The name is used principally for debugging
660  * purposes, although it is also possible to set and get the name on a pointer
661  * in as a way of "marking" pointers in your code.
662  *
663  * The main use for names on pointer is for "talloc reports". See
664  * talloc_report() and talloc_report_full() for details. Also see
665  * talloc_enable_leak_report() and talloc_enable_leak_report_full().
666  *
667  * The talloc_set_name() function allocates memory as a child of the
668  * pointer. It is logically equivalent to:
669  *   talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
670  *
671  * Note that multiple calls to talloc_set_name() will allocate more memory
672  * without releasing the name. All of the memory is released when the ptr is
673  * freed using talloc_free().
674  */
675 const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
676
677 /**
678  * talloc_set_name_const - set a talloc pointer name to a string constant
679  * @ptr: the talloc pointer to name
680  * @name: the strucng constant.
681  *
682  * The function talloc_set_name_const() is just like talloc_set_name(), but it
683  * takes a string constant, and is much faster. It is extensively used by the
684  * "auto naming" macros, such as talloc().
685  *
686  * This function does not allocate any memory. It just copies the supplied
687  * pointer into the internal representation of the talloc ptr. This means you
688  * must not pass a name pointer to memory that will disappear before the ptr is
689  * freed with talloc_free().
690  */
691 void talloc_set_name_const(const void *ptr, const char *name);
692
693 /**
694  * talloc_named - create a specifically-named talloc pointer
695  * @context: the parent context for the allocation
696  * @size: the size to allocate
697  * @fmt: the printf-style format for the name
698  *
699  * The talloc_named() function creates a named talloc pointer. It is equivalent
700  * to:
701  *
702  *   ptr = talloc_size(context, size);
703  *   talloc_set_name(ptr, fmt, ....);
704  */
705 void *talloc_named(const void *context, size_t size, 
706                    const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
707
708 /**
709  * talloc_named_const - create a specifically-named talloc pointer
710  * @context: the parent context for the allocation
711  * @size: the size to allocate
712  * @name: the string constant to use as the name
713  *
714  * This is equivalent to:
715  *
716  *   ptr = talloc_size(context, size);
717  *   talloc_set_name_const(ptr, name);
718  */
719 void *talloc_named_const(const void *context, size_t size, const char *name);
720
721 /**
722  * talloc_get_name - get the name of a talloc pointer
723  * @ptr: the talloc pointer
724  *
725  * This returns the current name for the given talloc pointer. See
726  * talloc_set_name() for details.
727  */
728 const char *talloc_get_name(const void *ptr);
729
730 /**
731  * talloc_check_name - check if a pointer has the specified name
732  * @ptr: the talloc pointer
733  * @name: the name to compare with the pointer's name
734  *
735  * This function checks if a pointer has the specified name. If it does then
736  * the pointer is returned. It it doesn't then NULL is returned.
737  */
738 void *talloc_check_name(const void *ptr, const char *name);
739
740 /**
741  * talloc_init - create a top-level context of particular name
742  * @fmt: the printf-style format of the name
743  *
744  * This function creates a zero length named talloc context as a top level
745  * context. It is equivalent to:
746  *
747  *   talloc_named(NULL, 0, fmt, ...);
748  */
749 void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
750
751 /**
752  * talloc_total_size - get the bytes used by the pointer and its children
753  * @ptr: the talloc pointer
754  *
755  * The talloc_total_size() function returns the total size in bytes used by
756  * this pointer and all child pointers. Mostly useful for debugging.
757  *
758  * Passing NULL is allowed, but it will only give a meaningful result if
759  * talloc_enable_leak_report() or talloc_enable_leak_report_full() has been
760  * called.
761  */
762 size_t talloc_total_size(const void *ptr);
763
764 /**
765  * talloc_total_blocks - get the number of allocations for the pointer
766  * @ptr: the talloc pointer
767  *
768  * The talloc_total_blocks() function returns the total allocations used by
769  * this pointer and all child pointers. Mostly useful for debugging. For
770  * example, a pointer with no children will return "1".
771  *
772  * Passing NULL is allowed, but it will only give a meaningful result if
773  * talloc_enable_leak_report() or talloc_enable_leak_report_full() has been
774  * called.
775  */
776 size_t talloc_total_blocks(const void *ptr);
777
778 /**
779  * talloc_report_depth_cb - walk the entire talloc tree under a talloc pointer
780  * @ptr: the talloc pointer to recurse under
781  * @depth: the current depth of traversal
782  * @max_depth: maximum depth to traverse, or -1 for no maximum
783  * @callback: the function to call on each pointer
784  * @private_data: pointer to hand to @callback.
785  *
786  * This provides a more flexible reports than talloc_report(). It will
787  * recursively call the callback for the entire tree of memory referenced by
788  * the pointer. References in the tree are passed with is_ref = 1 and the
789  * pointer that is referenced.
790  *
791  * You can pass NULL for the pointer, in which case a report is printed for the
792  * top level memory context, but only if talloc_enable_leak_report() or
793  * talloc_enable_leak_report_full() has been called.
794  *
795  * The recursion is stopped when depth >= max_depth.  max_depth = -1 means only
796  * stop at leaf nodes.
797  */
798 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
799                             void (*callback)(const void *ptr,
800                                              int depth, int max_depth,
801                                              int is_ref,
802                                              void *private_data),
803                             void *private_data);
804
805 /**
806  * talloc_report_depth_file - report talloc usage to a maximum depth
807  * @ptr: the talloc pointer to recurse under
808  * @depth: the current depth of traversal
809  * @max_depth: maximum depth to traverse, or -1 for no maximum
810  * @f: the file to report to
811  *
812  * This provides a more flexible reports than talloc_report(). It will let you
813  * specify the depth and max_depth.
814  */
815 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
816
817 /**
818  * talloc_enable_null_tracking - enable tracking of top-level tallocs
819  *
820  * This enables tracking of the NULL memory context without enabling leak
821  * reporting on exit. Useful for when you want to do your own leak reporting
822  * call via talloc_report_null_full();
823  */
824 void talloc_enable_null_tracking(void);
825
826 /**
827  * talloc_disable_null_tracking - enable tracking of top-level tallocs
828  *
829  * This disables tracking of the NULL memory context.
830  */
831 void talloc_disable_null_tracking(void);
832
833 /**
834  * talloc_enable_leak_report - call talloc_report on program exit
835  *
836  * This enables calling of talloc_report(NULL, stderr) when the program
837  * exits. In Samba4 this is enabled by using the --leak-report command line
838  * option.
839  *
840  * For it to be useful, this function must be called before any other talloc
841  * function as it establishes a "null context" that acts as the top of the
842  * tree. If you don't call this function first then passing NULL to
843  * talloc_report() or talloc_report_full() won't give you the full tree
844  * printout.
845  *
846  * Here is a typical talloc report:
847  *
848  * talloc report on 'null_context' (total 267 bytes in 15 blocks)
849  *         libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
850  *         libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
851  *         iconv(UTF8,CP850)              contains     42 bytes in   2 blocks
852  *         libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
853  *         iconv(CP850,UTF8)              contains     42 bytes in   2 blocks
854  *         iconv(UTF8,UTF-16LE)           contains     45 bytes in   2 blocks
855  *         iconv(UTF-16LE,UTF8)           contains     45 bytes in   2 blocks
856  */
857 void talloc_enable_leak_report(void);
858
859 /**
860  * talloc_enable_leak_report - call talloc_report_full on program exit
861  *
862  * This enables calling of talloc_report_full(NULL, stderr) when the program
863  * exits. In Samba4 this is enabled by using the --leak-report-full command
864  * line option.
865  *
866  * For it to be useful, this function must be called before any other talloc
867  * function as it establishes a "null context" that acts as the top of the
868  * tree. If you don't call this function first then passing NULL to
869  * talloc_report() or talloc_report_full() won't give you the full tree
870  * printout.
871  *
872  * Here is a typical full report:
873  *
874  * full talloc report on 'root' (total 18 bytes in 8 blocks)
875  *    p1                        contains     18 bytes in   7 blocks (ref 0)
876  *         r1                        contains     13 bytes in   2 blocks (ref 0)
877  *             reference to: p2
878  *         p2                        contains      1 bytes in   1 blocks (ref 1)
879  *         x3                        contains      1 bytes in   1 blocks (ref 0)
880  *         x2                        contains      1 bytes in   1 blocks (ref 0)
881  *         x1                        contains      1 bytes in   1 blocks (ref 0)
882  */
883 void talloc_enable_leak_report_full(void);
884
885 /**
886  * talloc_autofree_context - a context which will be freed at exit
887  *
888  * This is a handy utility function that returns a talloc context which will be
889  * automatically freed on program exit. This can be used to reduce the noise in
890  * memory leak reports.
891  */
892 void *talloc_autofree_context(void);
893
894 /**
895  * talloc_get_size - get the size of an allocation
896  * @ctx: the talloc pointer whose allocation to measure.
897  *
898  * This function lets you know the amount of memory alloced so far by this
899  * context. It does NOT account for subcontext memory.  This can be used to
900  * calculate the size of an array.
901  */
902 size_t talloc_get_size(const void *ctx);
903
904 /**
905  * talloc_find_parent_byname - find a parent of this context with this name
906  * @ctx: the context whose ancestors to search
907  * @name: the name to look for
908  *
909  * Find a parent memory context of @ctx that has the given name. This can be
910  * very useful in complex programs where it may be difficult to pass all
911  * information down to the level you need, but you know the structure you want
912  * is a parent of another context.
913  */
914 void *talloc_find_parent_byname(const void *ctx, const char *name);
915
916 /**
917  * talloc_add_external - create an externally allocated node
918  * @ctx: the parent
919  * @realloc: the realloc() equivalent
920  * @lock: the call to lock before manipulation of external nodes
921  * @unlock: the call to unlock after manipulation of external nodes
922  *
923  * talloc_add_external() creates a node which uses a separate allocator.  All
924  * children allocated from that node will also use that allocator.
925  *
926  * Note: Currently there is only one external allocator, not per-node,
927  * and it is set with this function.
928  *
929  * @lock is handed a pointer which was previous returned from your realloc
930  * function; you should use that to figure out which lock to get if you have
931  * multiple external pools.
932  *
933  * The parent pointers in realloc is the talloc pointer of the parent, if any.
934  */
935 void *talloc_add_external(const void *ctx,
936                           void *(*realloc)(const void *parent,
937                                            void *ptr, size_t),
938                           void (*lock)(const void *p),
939                           void (*unlock)(void));
940
941 /* The following definitions come from talloc.c  */
942 void *_talloc(const void *context, size_t size);
943 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *));
944 size_t talloc_reference_count(const void *ptr);
945 void *_talloc_reference(const void *context, const void *ptr);
946
947 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
948 void *talloc_parent(const void *ptr);
949 const char *talloc_parent_name(const void *ptr);
950 void *_talloc_steal(const void *new_ctx, const void *ptr);
951 void *_talloc_move(const void *new_ctx, const void *pptr);
952 void *_talloc_zero(const void *ctx, size_t size, const char *name);
953 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
954 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
955 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name);
956 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
957 void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
958 void talloc_show_parents(const void *context, FILE *file);
959 int talloc_is_parent(const void *context, const void *ptr);
960
961 #endif /* CCAN_TALLOC_H */