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
30 #include "qemu/compiler.h"
33 * P9Array provides a mechanism to access arrays in common C-style (e.g. by
34 * square bracket [] operator) in conjunction with reference variables that
35 * perform deep auto free of the array when leaving the scope of the auto
36 * reference variable. That means not only is the array itself automatically
37 * freed, but also memory dynamically allocated by the individual array
42 * Consider the following user struct @c Foo which shall be used as scalar
43 * (element) type of an array:
45 * typedef struct Foo {
50 * and assume it has the following function to free memory allocated by @c Foo
53 * void free_foo(Foo *foo) {
57 * Add the following to a shared header file:
59 * P9ARRAY_DECLARE_TYPE(Foo);
61 * and the following to a C unit file:
63 * P9ARRAY_DEFINE_TYPE(Foo, free_foo);
65 * Finally the array may then be used like this:
67 * void doSomething(size_t n) {
68 * P9ARRAY_REF(Foo) foos = NULL;
69 * P9ARRAY_NEW(Foo, foos, n);
70 * for (size_t i = 0; i < n; ++i) {
72 * foos[i].s = calloc(4096, 1);
73 * snprintf(foos[i].s, 4096, "foo %d", i);
75 * return; // array auto freed here
78 * // array auto freed here
84 * Declares an array type for the passed @a scalar_type.
86 * This is typically used from a shared header file.
88 * @param scalar_type - type of the individual array elements
90 #define P9ARRAY_DECLARE_TYPE(scalar_type) \
91 typedef struct P9Array##scalar_type { \
93 scalar_type first[]; \
94 } P9Array##scalar_type; \
96 void p9array_new_##scalar_type(scalar_type **auto_var, size_t len); \
97 void p9array_auto_free_##scalar_type(scalar_type **auto_var); \
100 * Defines an array type for the passed @a scalar_type and appropriate
101 * @a scalar_cleanup_func.
103 * This is typically used from a C unit file.
105 * @param scalar_type - type of the individual array elements
106 * @param scalar_cleanup_func - appropriate function to free memory dynamically
107 * allocated by individual array elements before
109 #define P9ARRAY_DEFINE_TYPE(scalar_type, scalar_cleanup_func) \
110 void p9array_new_##scalar_type(scalar_type **auto_var, size_t len) \
112 p9array_auto_free_##scalar_type(auto_var); \
113 P9Array##scalar_type *arr = g_malloc0(sizeof(P9Array##scalar_type) + \
114 len * sizeof(scalar_type)); \
116 *auto_var = &arr->first[0]; \
119 void p9array_auto_free_##scalar_type(scalar_type **auto_var) \
121 scalar_type *first = (*auto_var); \
125 P9Array##scalar_type *arr = (P9Array##scalar_type *) ( \
126 ((char *)first) - offsetof(P9Array##scalar_type, first) \
128 for (size_t i = 0; i < arr->len; ++i) { \
129 scalar_cleanup_func(&arr->first[i]); \
135 * Used to declare a reference variable (unique pointer) for an array. After
136 * leaving the scope of the reference variable, the associated array is
137 * automatically freed.
139 * @param scalar_type - type of the individual array elements
141 #define P9ARRAY_REF(scalar_type) \
142 __attribute((__cleanup__(p9array_auto_free_##scalar_type))) scalar_type*
145 * Allocates a new array of passed @a scalar_type with @a len number of array
146 * elements and assigns the created array to the reference variable
149 * @param scalar_type - type of the individual array elements
150 * @param auto_var - destination reference variable
151 * @param len - amount of array elements to be allocated immediately
153 #define P9ARRAY_NEW(scalar_type, auto_var, len) \
154 QEMU_BUILD_BUG_MSG( \
155 !__builtin_types_compatible_p(scalar_type, typeof(*auto_var)), \
156 "P9Array scalar type mismatch" \
158 p9array_new_##scalar_type((&auto_var), len)
160 #endif /* QEMU_P9ARRAY_H */