Debian Codegraph @

src/fundamental/cleanup-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "assert-util.h"
5
6 /* A wrapper for 'func' to return void.
7 * Only useful when a void-returning function is required by some API. */
8 #define DEFINE_TRIVIAL_DESTRUCTOR(name, type, func) \
9 static inline void name(type *p) { \
10 func(p); \
11 }
12
13 /* When func() returns the void value (NULL, -1, …) of the appropriate type */
14 #define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
15 static inline void func##p(type *p) { \
16 if (*p) \
17 *p = func(*p); \
18 }
19
20 /* When func() doesn't return the appropriate type, set variable to empty afterwards. The func() may be
21 * provided by a dynamically loaded (dlopen()) shared library, hence add an assertion. */
22 #define DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_RENAME(type, func, name, empty) \
23 static inline void name(type *p) { \
24 if (*p != (empty)) { \
25 DISABLE_WARNING_ADDRESS; \
26 assert(func); \
27 REENABLE_WARNING; \
28 func(*p); \
29 *p = (empty); \
30 } \
31 }
32
33 #define DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(type, func, empty) \
34 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_RENAME(type, func, func##p, empty)
35
36 /* When func() doesn't return the appropriate type, and is also a macro, set variable to empty afterwards. */
37 #define DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_MACRO_RENAME(type, macro, name, empty) \
38 static inline void name(type *p) { \
39 if (*p != (empty)) { \
40 macro(*p); \
41 *p = (empty); \
42 } \
43 }
44
45 #define DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_MACRO(type, macro, empty) \
46 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_MACRO_RENAME(type, macro, macro##p, empty)
47
48 /* Clean up an array of pointers to objects by dropping all the items in it.
49 * The size of the array is passed in as a parameter, so NULL items may appear in the middle of the array.
50 * Free the array itself afterwards. */
51 #define DEFINE_POINTER_ARRAY_FREE_FUNC(type, helper) \
52 void helper ## _array(type *array, size_t n) { \
53 assert(array || n == 0); \
54 FOREACH_ARRAY(item, array, n) \
55 helper(*item); \
56 free(array); \
57 }
58
59 /* Like DEFINE_POINTER_ARRAY_FREE_FUNC() but does not deallocate the array itself, useful for
60 * arrays with automatic storage duration (e.g. on the stack). */
61 #define DEFINE_POINTER_ARRAY_CLEAR_FUNC(type, helper) \
62 void helper ## _array_clear(type *array, size_t n) { \
63 assert(array || n == 0); \
64 FOREACH_ARRAY(item, array, n) \
65 *item = helper(*item); \
66 }
67
68 /* Clean up an array of objects of known size by dropping all the items in it.
69 * Then free the array itself. */
70 #define DEFINE_ARRAY_FREE_FUNC(name, type, helper) \
71 void name(type *array, size_t n) { \
72 assert(array || n == 0); \
73 FOREACH_ARRAY(item, array, n) \
74 helper(item); \
75 free(array); \
76 }
77
78 typedef void (*free_array_func_t)(void *p, size_t n);
79
80 /* An automatic _cleanup_-like logic for destroy arrays (i.e. pointers + size) when leaving scope */
81 typedef struct ArrayCleanup {
82 void **parray;
83 size_t *pn;
84 free_array_func_t pfunc;
85 } ArrayCleanup;
86
87 static inline void array_cleanup(const ArrayCleanup *c) {
88 assert(c);
89 assert(!c->parray == !c->pn);
90
91 if (!c->parray)
92 return;
93
94 if (*c->parray) {
95 assert(c->pfunc);
96 c->pfunc(*c->parray, *c->pn);
97 *c->parray = NULL;
98 }
99
100 *c->pn = 0;
101 }
102
103 #define CLEANUP_ARRAY(array, n, func) \
104 _cleanup_(array_cleanup) _unused_ const ArrayCleanup CONCATENATE(_cleanup_array_, UNIQ) = { \
105 .parray = (void**) &(array), \
106 .pn = &(n), \
107 .pfunc = (free_array_func_t) ({ \
108 void (*_f)(typeof(array[0]) *a, size_t b) = func; \
109 _f; \
110 }), \
111 }
112
113 /* An automatic _cleanup_-like logic for fixed-size arrays where the bound is known via
114 * ELEMENTSOF(). Unlike CLEANUP_ARRAY() this neither frees the storage nor zeroes it: it just
115 * invokes func() across the elements when leaving scope. */
116 typedef struct ElementsCleanup {
117 void *array;
118 size_t n;
119 free_array_func_t pfunc;
120 } ElementsCleanup;
121
122 static inline void elements_cleanup(const ElementsCleanup *c) {
123 assert(c);
124
125 if (c->n == 0)
126 return;
127
128 assert(c->array);
129 assert(c->pfunc);
130 c->pfunc(c->array, c->n);
131 }
132
133 #define CLEANUP_ELEMENTS(_array, _func) \
134 _cleanup_(elements_cleanup) _unused_ const ElementsCleanup CONCATENATE(_cleanup_elements_, UNIQ) = { \
135 .array = (_array), \
136 .n = ELEMENTSOF(_array), \
137 .pfunc = (free_array_func_t) ({ \
138 void (*_f)(typeof((_array)[0]) *a, size_t b) = _func; \
139 _f; \
140 }), \
141 }