Debian Codegraph @

crt/aws-c-common/include/aws/common/array_list.h

raw symbols SCIP
1 #ifndef AWS_COMMON_ARRAY_LIST_H
2 #define AWS_COMMON_ARRAY_LIST_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8 #include <aws/common/common.h>
9 #include <aws/common/math.h>
10
11 #include <stdlib.h>
12
13 AWS_PUSH_SANE_WARNING_LEVEL
14
15 enum { AWS_ARRAY_LIST_DEBUG_FILL = 0xDD };
16
17 struct aws_array_list {
18 struct aws_allocator *alloc;
19 size_t current_size;
20 size_t length;
21 size_t item_size;
22 void *data;
23 };
24
25 /**
26 * Prototype for a comparator function for sorting elements.
27 *
28 * a and b should be cast to pointers to the element type held in the list
29 * before being dereferenced. The function should compare the elements and
30 * return a positive number if a > b, zero if a = b, and a negative number
31 * if a < b.
32 */
33 typedef int(aws_array_list_comparator_fn)(const void *a, const void *b);
34
35 AWS_EXTERN_C_BEGIN
36
37 /**
38 * Initializes an array list with an array of size initial_item_allocation * item_size. In this mode, the array size
39 * will grow by a factor of 2 upon insertion if space is not available. initial_item_allocation is the number of
40 * elements you want space allocated for. item_size is the size of each element in bytes. Mixing items types is not
41 * supported by this API.
42 */
43 AWS_STATIC_IMPL
44 int aws_array_list_init_dynamic(
45 struct aws_array_list *AWS_RESTRICT list,
46 struct aws_allocator *alloc,
47 size_t initial_item_allocation,
48 size_t item_size);
49
50 /**
51 * Initializes an array list with a preallocated array of void *. item_count is the number of elements in the array,
52 * and item_size is the size in bytes of each element. Mixing items types is not supported
53 * by this API. Once this list is full, new items will be rejected.
54 */
55 AWS_STATIC_IMPL
56 void aws_array_list_init_static(
57 struct aws_array_list *AWS_RESTRICT list,
58 void *raw_array,
59 size_t item_count,
60 size_t item_size);
61
62 /**
63 * Initializes an array list with a preallocated array of *already-initialized* elements. item_count is the number of
64 * elements in the array, and item_size is the size in bytes of each element.
65 *
66 * Once initialized, nothing further can be added to the list, since it will be full and cannot resize.
67 *
68 * Primary use case is to treat an already-initialized C array as an array list.
69 */
70 AWS_STATIC_IMPL
71 void aws_array_list_init_static_from_initialized(
72 struct aws_array_list *AWS_RESTRICT list,
73 void *raw_array,
74 size_t item_count,
75 size_t item_size);
76
77 /**
78 * Set of properties of a valid aws_array_list.
79 */
80 AWS_STATIC_IMPL
81 bool aws_array_list_is_valid(const struct aws_array_list *AWS_RESTRICT list);
82
83 /**
84 * Deallocates any memory that was allocated for this list, and resets list for reuse or deletion.
85 */
86 AWS_STATIC_IMPL
87 void aws_array_list_clean_up(struct aws_array_list *AWS_RESTRICT list);
88
89 /**
90 * Erases and then deallocates any memory that was allocated for this list, and resets list for reuse or deletion.
91 */
92 AWS_STATIC_IMPL
93 void aws_array_list_clean_up_secure(struct aws_array_list *AWS_RESTRICT list);
94
95 /**
96 * Pushes the memory pointed to by val onto the end of internal list
97 */
98 AWS_STATIC_IMPL
99 int aws_array_list_push_back(struct aws_array_list *AWS_RESTRICT list, const void *val);
100
101 /**
102 * Copies the element at the front of the list if it exists. If list is empty, AWS_ERROR_LIST_EMPTY will be raised
103 */
104 AWS_STATIC_IMPL
105 int aws_array_list_front(const struct aws_array_list *AWS_RESTRICT list, void *val);
106
107 /**
108 * Pushes the memory pointed to by val onto the front of internal list.
109 * This call results in shifting all of the elements in the list. Avoid this call unless that
110 * is intended behavior.
111 */
112 AWS_STATIC_IMPL
113 int aws_array_list_push_front(struct aws_array_list *AWS_RESTRICT list, const void *val);
114
115 /**
116 * Deletes the element at the front of the list if it exists. If list is empty, AWS_ERROR_LIST_EMPTY will be raised.
117 * This call results in shifting all of the elements at the end of the array to the front. Avoid this call unless that
118 * is intended behavior.
119 */
120 AWS_STATIC_IMPL
121 int aws_array_list_pop_front(struct aws_array_list *AWS_RESTRICT list);
122
123 /**
124 * Delete N elements from the front of the list.
125 * Remaining elements are shifted to the front of the list.
126 * If the list has less than N elements, the list is cleared.
127 * This call is more efficient than calling aws_array_list_pop_front() N times.
128 */
129 AWS_STATIC_IMPL
130 void aws_array_list_pop_front_n(struct aws_array_list *AWS_RESTRICT list, size_t n);
131
132 /**
133 * Deletes the element this index in the list if it exists.
134 * If element does not exist, AWS_ERROR_INVALID_INDEX will be raised.
135 * This call results in shifting all remaining elements towards the front.
136 * Avoid this call unless that is intended behavior.
137 */
138 AWS_STATIC_IMPL
139 int aws_array_list_erase(struct aws_array_list *AWS_RESTRICT list, size_t index);
140
141 /**
142 * Copies the element at the end of the list if it exists. If list is empty, AWS_ERROR_LIST_EMPTY will be raised.
143 */
144 AWS_STATIC_IMPL
145 int aws_array_list_back(const struct aws_array_list *AWS_RESTRICT list, void *val);
146
147 /**
148 * Deletes the element at the end of the list if it exists. If list is empty, AWS_ERROR_LIST_EMPTY will be raised.
149 */
150 AWS_STATIC_IMPL
151 int aws_array_list_pop_back(struct aws_array_list *AWS_RESTRICT list);
152
153 /**
154 * Clears all elements in the array and resets length to zero. Size does not change in this operation.
155 */
156 AWS_STATIC_IMPL
157 void aws_array_list_clear(struct aws_array_list *AWS_RESTRICT list);
158
159 /**
160 * If in dynamic mode, shrinks the allocated array size to the minimum amount necessary to store its elements.
161 */
162 AWS_COMMON_API
163 int aws_array_list_shrink_to_fit(struct aws_array_list *AWS_RESTRICT list);
164
165 /**
166 * Copies the elements from from to to. If to is in static mode, it must at least be the same length as from. Any data
167 * in to will be overwritten in this copy.
168 */
169 AWS_COMMON_API
170 int aws_array_list_copy(const struct aws_array_list *AWS_RESTRICT from, struct aws_array_list *AWS_RESTRICT to);
171
172 /**
173 * Swap contents between two dynamic lists. Both lists must use the same allocator.
174 */
175 AWS_STATIC_IMPL
176 void aws_array_list_swap_contents(
177 struct aws_array_list *AWS_RESTRICT list_a,
178 struct aws_array_list *AWS_RESTRICT list_b);
179
180 /**
181 * Returns the number of elements that can fit in the internal array. If list is initialized in dynamic mode,
182 * the capacity changes over time.
183 */
184 AWS_STATIC_IMPL
185 size_t aws_array_list_capacity(const struct aws_array_list *AWS_RESTRICT list);
186
187 /**
188 * Returns the number of elements in the internal array.
189 */
190 AWS_STATIC_IMPL
191 size_t aws_array_list_length(const struct aws_array_list *AWS_RESTRICT list);
192
193 /**
194 * Copies the memory at index to val. If element does not exist, AWS_ERROR_INVALID_INDEX will be raised.
195 */
196 AWS_STATIC_IMPL
197 int aws_array_list_get_at(const struct aws_array_list *AWS_RESTRICT list, void *val, size_t index);
198
199 /**
200 * Copies the memory address of the element at index to *val. If element does not exist, AWS_ERROR_INVALID_INDEX will be
201 * raised.
202 */
203 AWS_STATIC_IMPL
204 int aws_array_list_get_at_ptr(const struct aws_array_list *AWS_RESTRICT list, void **val, size_t index);
205
206 /**
207 * Ensures that the array list has enough capacity to store a value at the specified index. If there is not already
208 * enough capacity, and the list is in dynamic mode, this function will attempt to allocate more memory, expanding the
209 * list. In static mode, if 'index' is beyond the maximum index, AWS_ERROR_INVALID_INDEX will be raised.
210 */
211 AWS_COMMON_API
212 int aws_array_list_ensure_capacity(struct aws_array_list *AWS_RESTRICT list, size_t index);
213
214 /**
215 * Copies the the memory pointed to by val into the array at index. If in dynamic mode, the size will grow by a factor
216 * of two when the array is full. In static mode, AWS_ERROR_INVALID_INDEX will be raised if the index is past the bounds
217 * of the array.
218 */
219 AWS_STATIC_IMPL
220 int aws_array_list_set_at(struct aws_array_list *AWS_RESTRICT list, const void *val, size_t index);
221
222 /**
223 * Swap elements at the specified indices, which must be within the bounds of the array.
224 */
225 AWS_COMMON_API
226 void aws_array_list_swap(struct aws_array_list *AWS_RESTRICT list, size_t a, size_t b);
227
228 /**
229 * Sort elements in the list in-place according to the comparator function.
230 */
231 AWS_COMMON_API
232 void aws_array_list_sort(struct aws_array_list *AWS_RESTRICT list, aws_array_list_comparator_fn *compare_fn);
233
234 AWS_EXTERN_C_END
235 #ifndef AWS_NO_STATIC_IMPL
236 # include <aws/common/array_list.inl>
237 #endif /* AWS_NO_STATIC_IMPL */
238
239 AWS_POP_SANE_WARNING_LEVEL
240
241 #endif /* AWS_COMMON_ARRAY_LIST_H */