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