2 * P9Array - deep auto free C-array
4 * Copyright (c) 2021 Crudebyte
7 * Christian Schoenebeck <qemu_oss@crudebyte.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #ifndef QEMU_P9ARRAY_H
28 #define QEMU_P9ARRAY_H
31 * P9Array provides a mechanism to access arrays in common C-style (e.g. by
32 * square bracket [] operator) in conjunction with reference variables that
33 * perform deep auto free of the array when leaving the scope of the auto
34 * reference variable. That means not only is the array itself automatically
35 * freed, but also memory dynamically allocated by the individual array
40 * Consider the following user struct @c Foo which shall be used as scalar
41 * (element) type of an array:
43 * typedef struct Foo {
48 * and assume it has the following function to free memory allocated by @c Foo
51 * void free_foo(Foo *foo) {
55 * Add the following to a shared header file:
57 * P9ARRAY_DECLARE_TYPE(Foo);
59 * and the following to a C unit file:
61 * P9ARRAY_DEFINE_TYPE(Foo, free_foo);
63 * Finally the array may then be used like this:
65 * void doSomething(size_t n) {
66 * P9ARRAY_REF(Foo) foos = NULL;
67 * P9ARRAY_NEW(Foo, foos, n);
68 * for (size_t i = 0; i < n; ++i) {
70 * foos[i].s = calloc(4096, 1);
71 * snprintf(foos[i].s, 4096, "foo %d", i);
73 * return; // array auto freed here
76 * // array auto freed here
82 * P9ARRAY_DECLARE_TYPE() - Declares an array type for the passed @scalar_type.
84 * @scalar_type: type of the individual array elements
86 * This is typically used from a shared header file.
88 #define P9ARRAY_DECLARE_TYPE(scalar_type) \
89 typedef struct P9Array##scalar_type { \
91 scalar_type first[]; \
92 } P9Array##scalar_type; \
94 void p9array_new_##scalar_type(scalar_type **auto_var, size_t len); \
95 void p9array_auto_free_##scalar_type(scalar_type **auto_var); \
98 * P9ARRAY_DEFINE_TYPE() - Defines an array type for the passed @scalar_type
99 * and appropriate @scalar_cleanup_func.
101 * @scalar_type: type of the individual array elements
102 * @scalar_cleanup_func: appropriate function to free memory dynamically
103 * allocated by individual array elements before
105 * This is typically used from a C unit file.
107 #define P9ARRAY_DEFINE_TYPE(scalar_type, scalar_cleanup_func) \
108 void p9array_new_##scalar_type(scalar_type **auto_var, size_t len) \
110 p9array_auto_free_##scalar_type(auto_var); \
111 P9Array##scalar_type *arr = g_malloc0(sizeof(P9Array##scalar_type) + \
112 len * sizeof(scalar_type)); \
114 *auto_var = &arr->first[0]; \
117 void p9array_auto_free_##scalar_type(scalar_type **auto_var) \
119 scalar_type *first = (*auto_var); \
123 P9Array##scalar_type *arr = (P9Array##scalar_type *) ( \
124 ((char *)first) - offsetof(P9Array##scalar_type, first) \
126 for (size_t i = 0; i < arr->len; ++i) { \
127 scalar_cleanup_func(&arr->first[i]); \
133 * P9ARRAY_REF() - Declare a reference variable for an array.
135 * @scalar_type: type of the individual array elements
137 * Used to declare a reference variable (unique pointer) for an array. After
138 * leaving the scope of the reference variable, the associated array is
139 * automatically freed.
141 #define P9ARRAY_REF(scalar_type) \
142 __attribute((__cleanup__(p9array_auto_free_##scalar_type))) scalar_type*
145 * P9ARRAY_NEW() - Allocate a new array.
147 * @scalar_type: type of the individual array elements
148 * @auto_var: destination reference variable
149 * @len: amount of array elements to be allocated immediately
151 * Allocates a new array of passed @scalar_type with @len number of array
152 * elements and assigns the created array to the reference variable
155 #define P9ARRAY_NEW(scalar_type, auto_var, len) \
156 QEMU_BUILD_BUG_MSG( \
157 !__builtin_types_compatible_p(scalar_type, typeof(*auto_var)), \
158 "P9Array scalar type mismatch" \
160 p9array_new_##scalar_type((&auto_var), len)
162 #endif /* QEMU_P9ARRAY_H */