Wed, 31 May 2023 12:43:30 +0200
fix missing connection ssl_error initialization
436 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include "cx/array_list.h" | |
30 | #include <assert.h> | |
31 | #include <string.h> | |
32 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
33 | // LOW LEVEL ARRAY LIST FUNCTIONS |
436 | 34 | |
35 | enum cx_array_copy_result cx_array_copy( | |
36 | void **target, | |
37 | size_t *size, | |
38 | size_t *capacity, | |
39 | size_t index, | |
40 | void const *src, | |
41 | size_t elem_size, | |
42 | size_t elem_count, | |
43 | struct cx_array_reallocator_s *reallocator | |
44 | ) { | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
45 | // assert pointers |
436 | 46 | assert(target != NULL); |
47 | assert(size != NULL); | |
48 | assert(src != NULL); | |
49 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
50 | // determine capacity |
436 | 51 | size_t cap = capacity == NULL ? *size : *capacity; |
52 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
53 | // check if resize is required |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
54 | size_t minsize = index + elem_count; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
55 | size_t newsize = *size < minsize ? minsize : *size; |
436 | 56 | bool needrealloc = newsize > cap; |
57 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
58 | // reallocate if possible |
436 | 59 | if (needrealloc) { |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
60 | // a reallocator and a capacity variable must be available |
436 | 61 | if (reallocator == NULL || capacity == NULL) { |
62 | return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED; | |
63 | } | |
64 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
65 | // check, if we need to repair the src pointer |
436 | 66 | uintptr_t targetaddr = (uintptr_t) *target; |
67 | uintptr_t srcaddr = (uintptr_t) src; | |
68 | bool repairsrc = targetaddr <= srcaddr | |
69 | && srcaddr < targetaddr + cap * elem_size; | |
70 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
71 | // calculate new capacity (next number divisible by 16) |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
72 | cap = newsize - (newsize % 16) + 16; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
73 | assert(cap > newsize); |
436 | 74 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
75 | // perform reallocation |
436 | 76 | void *newmem = reallocator->realloc( |
77 | *target, cap, elem_size, reallocator | |
78 | ); | |
79 | if (newmem == NULL) { | |
80 | return CX_ARRAY_COPY_REALLOC_FAILED; | |
81 | } | |
82 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
83 | // repair src pointer, if necessary |
436 | 84 | if (repairsrc) { |
85 | src = ((char *) newmem) + (srcaddr - targetaddr); | |
86 | } | |
87 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
88 | // store new pointer and capacity |
436 | 89 | *target = newmem; |
90 | *capacity = cap; | |
91 | } | |
92 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
93 | // determine target pointer |
436 | 94 | char *start = *target; |
95 | start += index * elem_size; | |
96 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
97 | // copy elements and set new size |
436 | 98 | memmove(start, src, elem_count * elem_size); |
99 | *size = newsize; | |
100 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
101 | // return successfully |
436 | 102 | return CX_ARRAY_COPY_SUCCESS; |
103 | } | |
104 | ||
490 | 105 | #ifndef CX_ARRAY_SWAP_SBO_SIZE |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
106 | #define CX_ARRAY_SWAP_SBO_SIZE 512 |
490 | 107 | #endif |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
108 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
109 | void cx_array_swap( |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
110 | void *arr, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
111 | size_t elem_size, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
112 | size_t idx1, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
113 | size_t idx2 |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
114 | ) { |
490 | 115 | assert(arr != NULL); |
116 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
117 | // short circuit |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
118 | if (idx1 == idx2) return; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
119 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
120 | char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE]; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
121 | void *tmp; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
122 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
123 | // decide if we can use the local buffer |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
124 | if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
125 | tmp = malloc(elem_size); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
126 | // we don't want to enforce error handling |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
127 | if (tmp == NULL) abort(); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
128 | } else { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
129 | tmp = sbo_mem; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
130 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
131 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
132 | // calculate memory locations |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
133 | char *left = arr, *right = arr; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
134 | left += idx1 * elem_size; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
135 | right += idx2 * elem_size; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
136 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
137 | // three-way swap |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
138 | memcpy(tmp, left, elem_size); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
139 | memcpy(left, right, elem_size); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
140 | memcpy(right, tmp, elem_size); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
141 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
142 | // free dynamic memory, if it was needed |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
143 | if (tmp != sbo_mem) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
144 | free(tmp); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
145 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
146 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
147 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
148 | // HIGH LEVEL ARRAY LIST FUNCTIONS |
436 | 149 | |
150 | typedef struct { | |
151 | struct cx_list_s base; | |
152 | void *data; | |
490 | 153 | size_t capacity; |
436 | 154 | struct cx_array_reallocator_s reallocator; |
155 | } cx_array_list; | |
156 | ||
157 | static void *cx_arl_realloc( | |
158 | void *array, | |
159 | size_t capacity, | |
160 | size_t elem_size, | |
161 | struct cx_array_reallocator_s *alloc | |
162 | ) { | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
163 | // retrieve the pointer to the list allocator |
436 | 164 | CxAllocator const *al = alloc->ptr1; |
165 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
166 | // use the list allocator to reallocate the memory |
436 | 167 | return cxRealloc(al, array, capacity * elem_size); |
168 | } | |
169 | ||
170 | static void cx_arl_destructor(struct cx_list_s *list) { | |
171 | cx_array_list *arl = (cx_array_list *) list; | |
172 | cxFree(list->allocator, arl->data); | |
173 | } | |
174 | ||
490 | 175 | static size_t cx_arl_insert_array( |
436 | 176 | struct cx_list_s *list, |
490 | 177 | size_t index, |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
178 | void const *array, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
179 | size_t n |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
180 | ) { |
490 | 181 | // out of bounds and special case check |
182 | if (index > list->size || n == 0) return 0; | |
183 | ||
184 | // get a correctly typed pointer to the list | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
185 | cx_array_list *arl = (cx_array_list *) list; |
490 | 186 | |
187 | // do we need to move some elements? | |
188 | if (index < list->size) { | |
189 | char const *first_to_move = (char const *) arl->data; | |
190 | first_to_move += index * list->item_size; | |
191 | size_t elems_to_move = list->size - index; | |
192 | size_t start_of_moved = index + n; | |
193 | ||
194 | if (CX_ARRAY_COPY_SUCCESS != cx_array_copy( | |
195 | &arl->data, | |
196 | &list->size, | |
197 | &arl->capacity, | |
198 | start_of_moved, | |
199 | first_to_move, | |
200 | list->item_size, | |
201 | elems_to_move, | |
202 | &arl->reallocator | |
203 | )) { | |
204 | // if moving existing elems is unsuccessful, abort | |
205 | return 0; | |
206 | } | |
207 | } | |
208 | ||
209 | // note that if we had to move the elements, the following operation | |
210 | // is guaranteed to succeed, because we have the memory already allocated | |
211 | // therefore, it is impossible to leave this function with an invalid array | |
212 | ||
213 | // place the new elements | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
214 | if (CX_ARRAY_COPY_SUCCESS == cx_array_copy( |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
215 | &arl->data, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
216 | &list->size, |
490 | 217 | &arl->capacity, |
218 | index, | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
219 | array, |
490 | 220 | list->item_size, |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
221 | n, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
222 | &arl->reallocator |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
223 | )) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
224 | return n; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
225 | } else { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
226 | // array list implementation is "all or nothing" |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
227 | return 0; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
228 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
229 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
230 | |
490 | 231 | static int cx_arl_insert_element( |
436 | 232 | struct cx_list_s *list, |
233 | size_t index, | |
490 | 234 | void const *element |
436 | 235 | ) { |
490 | 236 | return 1 != cx_arl_insert_array(list, index, element, 1); |
436 | 237 | } |
238 | ||
239 | static int cx_arl_insert_iter( | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
240 | struct cx_mut_iterator_s *iter, |
436 | 241 | void const *elem, |
242 | int prepend | |
243 | ) { | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
244 | struct cx_list_s *list = iter->src_handle; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
245 | if (iter->index < list->size) { |
490 | 246 | int result = cx_arl_insert_element( |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
247 | list, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
248 | iter->index + 1 - prepend, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
249 | elem |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
250 | ); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
251 | if (result == 0 && prepend != 0) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
252 | iter->index++; |
490 | 253 | iter->elem_handle = ((char *) iter->elem_handle) + list->item_size; |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
254 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
255 | return result; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
256 | } else { |
490 | 257 | int result = cx_arl_insert_element(list, list->size, elem); |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
258 | iter->index = list->size; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
259 | return result; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
260 | } |
436 | 261 | } |
262 | ||
263 | static int cx_arl_remove( | |
264 | struct cx_list_s *list, | |
265 | size_t index | |
266 | ) { | |
490 | 267 | cx_array_list *arl = (cx_array_list *) list; |
268 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
269 | // out-of-bounds check |
436 | 270 | if (index >= list->size) { |
271 | return 1; | |
272 | } | |
273 | ||
490 | 274 | // content destruction |
275 | cx_invoke_destructor(list, ((char *) arl->data) + index * list->item_size); | |
276 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
277 | // short-circuit removal of last element |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
278 | if (index == list->size - 1) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
279 | list->size--; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
280 | return 0; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
281 | } |
436 | 282 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
283 | // just move the elements starting at index to the left |
436 | 284 | int result = cx_array_copy( |
285 | &arl->data, | |
286 | &list->size, | |
490 | 287 | &arl->capacity, |
436 | 288 | index, |
490 | 289 | ((char *) arl->data) + (index + 1) * list->item_size, |
290 | list->item_size, | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
291 | list->size - index - 1, |
436 | 292 | &arl->reallocator |
293 | ); | |
294 | if (result == 0) { | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
295 | // decrease the size |
436 | 296 | list->size--; |
297 | } | |
298 | return result; | |
299 | } | |
300 | ||
490 | 301 | static void cx_arl_clear(struct cx_list_s *list) { |
302 | if (list->size == 0) return; | |
303 | ||
304 | cx_array_list *arl = (cx_array_list *) list; | |
305 | char *ptr = arl->data; | |
306 | ||
307 | if (list->simple_destructor) { | |
308 | for (size_t i = 0; i < list->size; i++) { | |
309 | cx_invoke_simple_destructor(list, ptr); | |
310 | ptr += list->item_size; | |
311 | } | |
312 | } | |
313 | if (list->advanced_destructor) { | |
314 | for (size_t i = 0; i < list->size; i++) { | |
315 | cx_invoke_advanced_destructor(list, ptr); | |
316 | ptr += list->item_size; | |
317 | } | |
318 | } | |
319 | ||
320 | memset(arl->data, 0, list->size * list->item_size); | |
321 | list->size = 0; | |
322 | } | |
323 | ||
324 | static int cx_arl_swap( | |
325 | struct cx_list_s *list, | |
326 | size_t i, | |
327 | size_t j | |
328 | ) { | |
329 | if (i >= list->size || j >= list->size) return 1; | |
330 | cx_array_list *arl = (cx_array_list *) list; | |
331 | cx_array_swap(arl->data, list->item_size, i, j); | |
332 | return 0; | |
333 | } | |
334 | ||
436 | 335 | static void *cx_arl_at( |
336 | struct cx_list_s const *list, | |
337 | size_t index | |
338 | ) { | |
339 | if (index < list->size) { | |
340 | cx_array_list const *arl = (cx_array_list const *) list; | |
341 | char *space = arl->data; | |
490 | 342 | return space + index * list->item_size; |
436 | 343 | } else { |
344 | return NULL; | |
345 | } | |
346 | } | |
347 | ||
490 | 348 | static ssize_t cx_arl_find( |
436 | 349 | struct cx_list_s const *list, |
350 | void const *elem | |
351 | ) { | |
490 | 352 | assert(list->cmpfunc != NULL); |
353 | assert(list->size < SIZE_MAX / 2); | |
436 | 354 | char *cur = ((cx_array_list const *) list)->data; |
355 | ||
490 | 356 | for (ssize_t i = 0; i < (ssize_t) list->size; i++) { |
436 | 357 | if (0 == list->cmpfunc(elem, cur)) { |
358 | return i; | |
359 | } | |
490 | 360 | cur += list->item_size; |
436 | 361 | } |
362 | ||
490 | 363 | return -1; |
436 | 364 | } |
365 | ||
366 | static void cx_arl_sort(struct cx_list_s *list) { | |
490 | 367 | assert(list->cmpfunc != NULL); |
436 | 368 | qsort(((cx_array_list *) list)->data, |
369 | list->size, | |
490 | 370 | list->item_size, |
436 | 371 | list->cmpfunc |
372 | ); | |
373 | } | |
374 | ||
375 | static int cx_arl_compare( | |
376 | struct cx_list_s const *list, | |
377 | struct cx_list_s const *other | |
378 | ) { | |
490 | 379 | assert(list->cmpfunc != NULL); |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
380 | if (list->size == other->size) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
381 | char const *left = ((cx_array_list const *) list)->data; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
382 | char const *right = ((cx_array_list const *) other)->data; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
383 | for (size_t i = 0; i < list->size; i++) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
384 | int d = list->cmpfunc(left, right); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
385 | if (d != 0) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
386 | return d; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
387 | } |
490 | 388 | left += list->item_size; |
389 | right += other->item_size; | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
390 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
391 | return 0; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
392 | } else { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
393 | return list->size < other->size ? -1 : 1; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
394 | } |
436 | 395 | } |
396 | ||
397 | static void cx_arl_reverse(struct cx_list_s *list) { | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
398 | if (list->size < 2) return; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
399 | void *data = ((cx_array_list const *) list)->data; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
400 | size_t half = list->size / 2; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
401 | for (size_t i = 0; i < half; i++) { |
490 | 402 | cx_array_swap(data, list->item_size, i, list->size - 1 - i); |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
403 | } |
436 | 404 | } |
405 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
406 | static bool cx_arl_iter_valid(void const *it) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
407 | struct cx_iterator_s const *iter = it; |
436 | 408 | struct cx_list_s const *list = iter->src_handle; |
409 | return iter->index < list->size; | |
410 | } | |
411 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
412 | static void *cx_arl_iter_current(void const *it) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
413 | struct cx_iterator_s const *iter = it; |
436 | 414 | return iter->elem_handle; |
415 | } | |
416 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
417 | static void cx_arl_iter_next(void *it) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
418 | struct cx_iterator_base_s *itbase = it; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
419 | if (itbase->remove) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
420 | struct cx_mut_iterator_s *iter = it; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
421 | itbase->remove = false; |
436 | 422 | cx_arl_remove(iter->src_handle, iter->index); |
423 | } else { | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
424 | struct cx_iterator_s *iter = it; |
436 | 425 | iter->index++; |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
426 | iter->elem_handle = |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
427 | ((char *) iter->elem_handle) |
490 | 428 | + ((struct cx_list_s const *) iter->src_handle)->item_size; |
429 | } | |
430 | } | |
431 | ||
432 | static void cx_arl_iter_prev(void *it) { | |
433 | struct cx_iterator_base_s *itbase = it; | |
434 | struct cx_mut_iterator_s *iter = it; | |
435 | cx_array_list *const list = iter->src_handle; | |
436 | if (itbase->remove) { | |
437 | itbase->remove = false; | |
438 | cx_arl_remove(iter->src_handle, iter->index); | |
439 | } | |
440 | iter->index--; | |
441 | if (iter->index < list->base.size) { | |
442 | iter->elem_handle = ((char *) list->data) | |
443 | + iter->index * list->base.item_size; | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
444 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
445 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
446 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
447 | static bool cx_arl_iter_flag_rm(void *it) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
448 | struct cx_iterator_base_s *iter = it; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
449 | if (iter->mutating) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
450 | iter->remove = true; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
451 | return true; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
452 | } else { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
453 | return false; |
436 | 454 | } |
455 | } | |
456 | ||
457 | static struct cx_iterator_s cx_arl_iterator( | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
458 | struct cx_list_s const *list, |
490 | 459 | size_t index, |
460 | bool backwards | |
436 | 461 | ) { |
462 | struct cx_iterator_s iter; | |
463 | ||
464 | iter.index = index; | |
465 | iter.src_handle = list; | |
466 | iter.elem_handle = cx_arl_at(list, index); | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
467 | iter.base.valid = cx_arl_iter_valid; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
468 | iter.base.current = cx_arl_iter_current; |
490 | 469 | iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next; |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
470 | iter.base.flag_removal = cx_arl_iter_flag_rm; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
471 | iter.base.remove = false; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
472 | iter.base.mutating = false; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
473 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
474 | return iter; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
475 | } |
436 | 476 | |
477 | static cx_list_class cx_array_list_class = { | |
478 | cx_arl_destructor, | |
490 | 479 | cx_arl_insert_element, |
480 | cx_arl_insert_array, | |
436 | 481 | cx_arl_insert_iter, |
482 | cx_arl_remove, | |
490 | 483 | cx_arl_clear, |
484 | cx_arl_swap, | |
436 | 485 | cx_arl_at, |
486 | cx_arl_find, | |
487 | cx_arl_sort, | |
488 | cx_arl_compare, | |
489 | cx_arl_reverse, | |
490 | cx_arl_iterator, | |
491 | }; | |
492 | ||
493 | CxList *cxArrayListCreate( | |
494 | CxAllocator const *allocator, | |
490 | 495 | cx_compare_func comparator, |
436 | 496 | size_t item_size, |
497 | size_t initial_capacity | |
498 | ) { | |
490 | 499 | if (allocator == NULL) { |
500 | allocator = cxDefaultAllocator; | |
501 | } | |
502 | ||
436 | 503 | cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list)); |
504 | if (list == NULL) return NULL; | |
505 | ||
490 | 506 | list->base.cl = &cx_array_list_class; |
507 | list->base.allocator = allocator; | |
508 | list->base.cmpfunc = comparator; | |
509 | list->capacity = initial_capacity; | |
510 | ||
511 | if (item_size > 0) { | |
512 | list->base.item_size = item_size; | |
513 | } else { | |
514 | item_size = sizeof(void *); | |
515 | cxListStorePointers((CxList *) list); | |
516 | } | |
517 | ||
518 | // allocate the array after the real item_size is known | |
436 | 519 | list->data = cxCalloc(allocator, initial_capacity, item_size); |
520 | if (list->data == NULL) { | |
521 | cxFree(allocator, list); | |
522 | return NULL; | |
523 | } | |
524 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
525 | // configure the reallocator |
436 | 526 | list->reallocator.realloc = cx_arl_realloc; |
527 | list->reallocator.ptr1 = (void *) allocator; | |
528 | ||
529 | return (CxList *) list; | |
530 | } |