dynarray.gl.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
  2. /* Type-safe arrays which grow dynamically. Shared definitions.
  3. Copyright (C) 2017-2021 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <https://www.gnu.org/licenses/>. */
  16. /* To use the dynarray facility, you need to include
  17. <malloc/dynarray-skeleton.c> and define the parameter macros
  18. documented in that file.
  19. A minimal example which provides a growing list of integers can be
  20. defined like this:
  21. struct int_array
  22. {
  23. // Pointer to result array followed by its length,
  24. // as required by DYNARRAY_FINAL_TYPE.
  25. int *array;
  26. size_t length;
  27. };
  28. #define DYNARRAY_STRUCT dynarray_int
  29. #define DYNARRAY_ELEMENT int
  30. #define DYNARRAY_PREFIX dynarray_int_
  31. #define DYNARRAY_FINAL_TYPE struct int_array
  32. #include <malloc/dynarray-skeleton.c>
  33. To create a three-element array with elements 1, 2, 3, use this
  34. code:
  35. struct dynarray_int dyn;
  36. dynarray_int_init (&dyn);
  37. for (int i = 1; i <= 3; ++i)
  38. {
  39. int *place = dynarray_int_emplace (&dyn);
  40. assert (place != NULL);
  41. *place = i;
  42. }
  43. struct int_array result;
  44. bool ok = dynarray_int_finalize (&dyn, &result);
  45. assert (ok);
  46. assert (result.length == 3);
  47. assert (result.array[0] == 1);
  48. assert (result.array[1] == 2);
  49. assert (result.array[2] == 3);
  50. free (result.array);
  51. If the elements contain resources which must be freed, define
  52. DYNARRAY_ELEMENT_FREE appropriately, like this:
  53. struct str_array
  54. {
  55. char **array;
  56. size_t length;
  57. };
  58. #define DYNARRAY_STRUCT dynarray_str
  59. #define DYNARRAY_ELEMENT char *
  60. #define DYNARRAY_ELEMENT_FREE(ptr) free (*ptr)
  61. #define DYNARRAY_PREFIX dynarray_str_
  62. #define DYNARRAY_FINAL_TYPE struct str_array
  63. #include <malloc/dynarray-skeleton.c>
  64. Compared to scratch buffers, dynamic arrays have the following
  65. features:
  66. - They have an element type, and are not just an untyped buffer of
  67. bytes.
  68. - When growing, previously stored elements are preserved. (It is
  69. expected that scratch_buffer_grow_preserve and
  70. scratch_buffer_set_array_size eventually go away because all
  71. current users are moved to dynamic arrays.)
  72. - Scratch buffers have a more aggressive growth policy because
  73. growing them typically means a retry of an operation (across an
  74. NSS service module boundary), which is expensive.
  75. - For the same reason, scratch buffers have a much larger initial
  76. stack allocation. */
  77. #ifndef _DYNARRAY_H
  78. #define _DYNARRAY_H
  79. #include <stdbool.h>
  80. #include <stddef.h>
  81. #include <string.h>
  82. struct dynarray_header
  83. {
  84. size_t used;
  85. size_t allocated;
  86. void *array;
  87. };
  88. /* Marker used in the allocated member to indicate that an error was
  89. encountered. */
  90. static inline size_t
  91. __dynarray_error_marker (void)
  92. {
  93. return -1;
  94. }
  95. /* Internal function. See the has_failed function in
  96. dynarray-skeleton.c. */
  97. static inline bool
  98. __dynarray_error (struct dynarray_header *list)
  99. {
  100. return list->allocated == __dynarray_error_marker ();
  101. }
  102. /* Internal function. Enlarge the dynamically allocated area of the
  103. array to make room for one more element. SCRATCH is a pointer to
  104. the scratch area (which is not heap-allocated and must not be
  105. freed). ELEMENT_SIZE is the size, in bytes, of one element.
  106. Return false on failure, true on success. */
  107. bool __libc_dynarray_emplace_enlarge (struct dynarray_header *,
  108. void *scratch, size_t element_size);
  109. /* Internal function. Enlarge the dynamically allocated area of the
  110. array to make room for at least SIZE elements (which must be larger
  111. than the existing used part of the dynamic array). SCRATCH is a
  112. pointer to the scratch area (which is not heap-allocated and must
  113. not be freed). ELEMENT_SIZE is the size, in bytes, of one element.
  114. Return false on failure, true on success. */
  115. bool __libc_dynarray_resize (struct dynarray_header *, size_t size,
  116. void *scratch, size_t element_size);
  117. /* Internal function. Like __libc_dynarray_resize, but clear the new
  118. part of the dynamic array. */
  119. bool __libc_dynarray_resize_clear (struct dynarray_header *, size_t size,
  120. void *scratch, size_t element_size);
  121. /* Internal type. */
  122. struct dynarray_finalize_result
  123. {
  124. void *array;
  125. size_t length;
  126. };
  127. /* Internal function. Copy the dynamically-allocated area to an
  128. explicitly-sized heap allocation. SCRATCH is a pointer to the
  129. embedded scratch space. ELEMENT_SIZE is the size, in bytes, of the
  130. element type. On success, true is returned, and pointer and length
  131. are written to *RESULT. On failure, false is returned. The caller
  132. has to take care of some of the memory management; this function is
  133. expected to be called from dynarray-skeleton.c. */
  134. bool __libc_dynarray_finalize (struct dynarray_header *list, void *scratch,
  135. size_t element_size,
  136. struct dynarray_finalize_result *result);
  137. /* Internal function. Terminate the process after an index error.
  138. SIZE is the number of elements of the dynamic array. INDEX is the
  139. lookup index which triggered the failure. */
  140. _Noreturn void __libc_dynarray_at_failure (size_t size, size_t index);
  141. #ifndef _ISOMAC
  142. #endif
  143. #endif /* _DYNARRAY_H */