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