hw/tpm/tpm_tis_common.c: Assert that locty is in range
[qemu/rayw.git] / fsdev / p9array.h
blob90e83a7c7b5453c6198c169a39f0601777ee53d4
1 /*
2 * P9Array - deep auto free C-array
4 * Copyright (c) 2021 Crudebyte
6 * Authors:
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
25 * THE SOFTWARE.
27 #ifndef QEMU_P9ARRAY_H
28 #define QEMU_P9ARRAY_H
30 #include "qemu/compiler.h"
32 /**
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
38 * elements.
40 * Example:
42 * Consider the following user struct @c Foo which shall be used as scalar
43 * (element) type of an array:
44 * @code
45 * typedef struct Foo {
46 * int i;
47 * char *s;
48 * } Foo;
49 * @endcode
50 * and assume it has the following function to free memory allocated by @c Foo
51 * instances:
52 * @code
53 * void free_foo(Foo *foo) {
54 * free(foo->s);
55 * }
56 * @endcode
57 * Add the following to a shared header file:
58 * @code
59 * P9ARRAY_DECLARE_TYPE(Foo);
60 * @endcode
61 * and the following to a C unit file:
62 * @code
63 * P9ARRAY_DEFINE_TYPE(Foo, free_foo);
64 * @endcode
65 * Finally the array may then be used like this:
66 * @code
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) {
71 * foos[i].i = i;
72 * foos[i].s = calloc(4096, 1);
73 * snprintf(foos[i].s, 4096, "foo %d", i);
74 * if (...) {
75 * return; // array auto freed here
76 * }
77 * }
78 * // array auto freed here
79 * }
80 * @endcode
83 /**
84 * P9ARRAY_DECLARE_TYPE() - Declares an array type for the passed @scalar_type.
86 * @scalar_type: type of the individual array elements
88 * This is typically used from a shared header file.
90 #define P9ARRAY_DECLARE_TYPE(scalar_type) \
91 typedef struct P9Array##scalar_type { \
92 size_t len; \
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); \
99 /**
100 * P9ARRAY_DEFINE_TYPE() - Defines an array type for the passed @scalar_type
101 * and appropriate @scalar_cleanup_func.
103 * @scalar_type: type of the individual array elements
104 * @scalar_cleanup_func: appropriate function to free memory dynamically
105 * allocated by individual array elements before
107 * This is typically used from a C unit file.
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)); \
115 arr->len = len; \
116 *auto_var = &arr->first[0]; \
119 void p9array_auto_free_##scalar_type(scalar_type **auto_var) \
121 scalar_type *first = (*auto_var); \
122 if (!first) { \
123 return; \
125 P9Array##scalar_type *arr = (P9Array##scalar_type *) ( \
126 ((char *)first) - offsetof(P9Array##scalar_type, first) \
127 ); \
128 for (size_t i = 0; i < arr->len; ++i) { \
129 scalar_cleanup_func(&arr->first[i]); \
131 g_free(arr); \
135 * P9ARRAY_REF() - Declare a reference variable for an array.
137 * @scalar_type: type of the individual array elements
139 * Used to declare a reference variable (unique pointer) for an array. After
140 * leaving the scope of the reference variable, the associated array is
141 * automatically freed.
143 #define P9ARRAY_REF(scalar_type) \
144 __attribute((__cleanup__(p9array_auto_free_##scalar_type))) scalar_type*
147 * P9ARRAY_NEW() - Allocate a new array.
149 * @scalar_type: type of the individual array elements
150 * @auto_var: destination reference variable
151 * @len: amount of array elements to be allocated immediately
153 * Allocates a new array of passed @scalar_type with @len number of array
154 * elements and assigns the created array to the reference variable
155 * @auto_var.
157 #define P9ARRAY_NEW(scalar_type, auto_var, len) \
158 QEMU_BUILD_BUG_MSG( \
159 !__builtin_types_compatible_p(scalar_type, typeof(*auto_var)), \
160 "P9Array scalar type mismatch" \
161 ); \
162 p9array_new_##scalar_type((&auto_var), len)
164 #endif /* QEMU_P9ARRAY_H */