1 /* CC0 (Public domain) - see LICENSE file for details */
8 * take - record a pointer to be consumed by the function its handed to.
9 * @p: the pointer to mark, or NULL.
11 * This marks a pointer object to be freed by the called function,
12 * which is extremely useful for chaining functions. It works on
13 * NULL, for pass-through error handling.
15 #define take(p) (take_typeof(p) take_((p)))
18 * taken - check (and un-take) a pointer was passed with take()
19 * @p: the pointer to check.
21 * A function which accepts take() arguments uses this to see if it
22 * should own the pointer; it will be removed from the take list, so
23 * this only returns true once.
26 * // Silly routine to add 1
27 * static int *add_one(const int *num)
33 * ret = malloc(sizeof(int));
39 bool taken(const void *p);
42 * is_taken - check if a pointer was passed with take()
43 * @p: the pointer to check.
45 * This is like the above, but doesn't remove it from the taken list.
48 * // Silly routine to add 1: doesn't handle taken args!
49 * static int *add_one_notake(const int *num)
51 * int *ret = malloc(sizeof(int));
52 * assert(!is_taken(num));
58 bool is_taken(const void *p);
61 * taken_any - are there any taken pointers?
63 * Mainly useful for debugging take() leaks.
66 * static void cleanup(void)
68 * assert(!taken_any());
74 * take_cleanup - remove all taken pointers from list.
76 * This is useful in atexit() handlers for valgrind-style leak detection.
79 * static void cleanup2(void)
84 void take_cleanup(void);
87 * take_allocfail - set function to call if we can't reallocated taken array.
90 * If this is not set, then if the array reallocation fails, the
91 * pointer won't be marked taken(). If @fn returns, it is expected to
92 * free the pointer; we return NULL from take() and the function handles
93 * it like any allocation failure.
96 * static void free_on_fail(const void *p)
101 * static void init(void)
103 * take_allocfail(free_on_fail);
106 void take_allocfail(void (*fn)(const void *p));
108 /* Private functions */
110 #define take_typeof(ptr) (__typeof__(ptr))
112 #define take_typeof(ptr)
115 void *take_(const void *p);
116 #endif /* CCAN_TAKE_H */