1 /* CC0 (Public domain) - see LICENSE file for details */
6 #include <ccan/str/str.h>
9 #define TAKE_LABEL(p) __FILE__ ":" stringify(__LINE__) ":" stringify(p)
11 #define TAKE_LABEL(p) NULL
15 * take - record a pointer to be consumed by the function its handed to.
16 * @p: the pointer to mark, or NULL.
18 * This marks a pointer object to be freed by the called function,
19 * which is extremely useful for chaining functions. It works on
20 * NULL, for pass-through error handling.
22 #define take(p) (take_typeof(p) take_((p), TAKE_LABEL(p)))
25 * taken - check (and un-take) a pointer was passed with take()
26 * @p: the pointer to check.
28 * A function which accepts take() arguments uses this to see if it
29 * should own the pointer; it will be removed from the take list, so
30 * this only returns true once.
33 * // Silly routine to add 1
34 * static int *add_one(const int *num)
40 * ret = malloc(sizeof(int));
46 bool taken(const void *p);
49 * is_taken - check if a pointer was passed with take()
50 * @p: the pointer to check.
52 * This is like the above, but doesn't remove it from the taken list.
55 * // Silly routine to add 1: doesn't handle taken args!
56 * static int *add_one_notake(const int *num)
58 * int *ret = malloc(sizeof(int));
59 * assert(!is_taken(num));
65 bool is_taken(const void *p);
68 * taken_any - are there any taken pointers?
70 * Mainly useful for debugging take() leaks. With CCAN_TAKE_DEBUG, returns
71 * the label where the pointer was passed to take(), otherwise returns
72 * a static char buffer with the pointer value in it. NULL if none are taken.
75 * static void cleanup(void)
77 * assert(!taken_any());
80 const char *taken_any(void);
83 * take_cleanup - remove all taken pointers from list.
85 * This is useful in atexit() handlers for valgrind-style leak detection.
88 * static void cleanup2(void)
93 void take_cleanup(void);
96 * take_allocfail - set function to call if we can't reallocated taken array.
99 * If this is not set, then if the array reallocation fails, the
100 * pointer won't be marked taken(). If @fn returns, it is expected to
101 * free the pointer; we return NULL from take() and the function handles
102 * it like any allocation failure.
105 * static void free_on_fail(const void *p)
110 * static void init(void)
112 * take_allocfail(free_on_fail);
115 void take_allocfail(void (*fn)(const void *p));
117 /* Private functions */
119 #define take_typeof(ptr) (__typeof__(ptr))
121 #define take_typeof(ptr)
124 void *take_(const void *p, const char *label);
125 #endif /* CCAN_TAKE_H */