2
0

p9array.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * P9Array - deep auto free C-array
  3. *
  4. * Copyright (c) 2021 Crudebyte
  5. *
  6. * Authors:
  7. * Christian Schoenebeck <qemu_oss@crudebyte.com>
  8. *
  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:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  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.
  26. */
  27. #ifndef QEMU_P9ARRAY_H
  28. #define QEMU_P9ARRAY_H
  29. /**
  30. * P9Array provides a mechanism to access arrays in common C-style (e.g. by
  31. * square bracket [] operator) in conjunction with reference variables that
  32. * perform deep auto free of the array when leaving the scope of the auto
  33. * reference variable. That means not only is the array itself automatically
  34. * freed, but also memory dynamically allocated by the individual array
  35. * elements.
  36. *
  37. * Example:
  38. *
  39. * Consider the following user struct @c Foo which shall be used as scalar
  40. * (element) type of an array:
  41. * @code
  42. * typedef struct Foo {
  43. * int i;
  44. * char *s;
  45. * } Foo;
  46. * @endcode
  47. * and assume it has the following function to free memory allocated by @c Foo
  48. * instances:
  49. * @code
  50. * void free_foo(Foo *foo) {
  51. * free(foo->s);
  52. * }
  53. * @endcode
  54. * Add the following to a shared header file:
  55. * @code
  56. * P9ARRAY_DECLARE_TYPE(Foo);
  57. * @endcode
  58. * and the following to a C unit file:
  59. * @code
  60. * P9ARRAY_DEFINE_TYPE(Foo, free_foo);
  61. * @endcode
  62. * Finally the array may then be used like this:
  63. * @code
  64. * void doSomething(size_t n) {
  65. * P9ARRAY_REF(Foo) foos = NULL;
  66. * P9ARRAY_NEW(Foo, foos, n);
  67. * for (size_t i = 0; i < n; ++i) {
  68. * foos[i].i = i;
  69. * foos[i].s = calloc(4096, 1);
  70. * snprintf(foos[i].s, 4096, "foo %d", i);
  71. * if (...) {
  72. * return; // array auto freed here
  73. * }
  74. * }
  75. * // array auto freed here
  76. * }
  77. * @endcode
  78. */
  79. /**
  80. * P9ARRAY_DECLARE_TYPE() - Declares an array type for the passed @scalar_type.
  81. *
  82. * @scalar_type: type of the individual array elements
  83. *
  84. * This is typically used from a shared header file.
  85. */
  86. #define P9ARRAY_DECLARE_TYPE(scalar_type) \
  87. typedef struct P9Array##scalar_type { \
  88. size_t len; \
  89. scalar_type first[]; \
  90. } P9Array##scalar_type; \
  91. \
  92. void p9array_new_##scalar_type(scalar_type **auto_var, size_t len); \
  93. void p9array_auto_free_##scalar_type(scalar_type **auto_var); \
  94. /**
  95. * P9ARRAY_DEFINE_TYPE() - Defines an array type for the passed @scalar_type
  96. * and appropriate @scalar_cleanup_func.
  97. *
  98. * @scalar_type: type of the individual array elements
  99. * @scalar_cleanup_func: appropriate function to free memory dynamically
  100. * allocated by individual array elements before
  101. *
  102. * This is typically used from a C unit file.
  103. */
  104. #define P9ARRAY_DEFINE_TYPE(scalar_type, scalar_cleanup_func) \
  105. void p9array_new_##scalar_type(scalar_type **auto_var, size_t len) \
  106. { \
  107. p9array_auto_free_##scalar_type(auto_var); \
  108. P9Array##scalar_type *arr = g_malloc0(sizeof(P9Array##scalar_type) + \
  109. len * sizeof(scalar_type)); \
  110. arr->len = len; \
  111. *auto_var = &arr->first[0]; \
  112. } \
  113. \
  114. void p9array_auto_free_##scalar_type(scalar_type **auto_var) \
  115. { \
  116. scalar_type *first = (*auto_var); \
  117. if (!first) { \
  118. return; \
  119. } \
  120. P9Array##scalar_type *arr = (P9Array##scalar_type *) ( \
  121. ((char *)first) - offsetof(P9Array##scalar_type, first) \
  122. ); \
  123. for (size_t i = 0; i < arr->len; ++i) { \
  124. scalar_cleanup_func(&arr->first[i]); \
  125. } \
  126. g_free(arr); \
  127. } \
  128. /**
  129. * P9ARRAY_REF() - Declare a reference variable for an array.
  130. *
  131. * @scalar_type: type of the individual array elements
  132. *
  133. * Used to declare a reference variable (unique pointer) for an array. After
  134. * leaving the scope of the reference variable, the associated array is
  135. * automatically freed.
  136. */
  137. #define P9ARRAY_REF(scalar_type) \
  138. __attribute((__cleanup__(p9array_auto_free_##scalar_type))) scalar_type*
  139. /**
  140. * P9ARRAY_NEW() - Allocate a new array.
  141. *
  142. * @scalar_type: type of the individual array elements
  143. * @auto_var: destination reference variable
  144. * @len: amount of array elements to be allocated immediately
  145. *
  146. * Allocates a new array of passed @scalar_type with @len number of array
  147. * elements and assigns the created array to the reference variable
  148. * @auto_var.
  149. */
  150. #define P9ARRAY_NEW(scalar_type, auto_var, len) \
  151. QEMU_BUILD_BUG_MSG( \
  152. !__builtin_types_compatible_p(scalar_type, typeof(*auto_var)), \
  153. "P9Array scalar type mismatch" \
  154. ); \
  155. p9array_new_##scalar_type((&auto_var), len)
  156. #endif /* QEMU_P9ARRAY_H */