Line data Source code
1 : /*
2 : MIT License
3 :
4 : Copyright (c) 2026 - A bunch of nerds
5 :
6 : Permission is hereby granted, free of charge, to any person obtaining a copy
7 : of this software and associated documentation files (the "Software"), to deal
8 : in the Software without restriction, including without limitation the rights
9 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 : copies of the Software, and to permit persons to whom the Software is
11 : furnished to do so, subject to the following conditions:
12 :
13 : The above copyright notice and this permission notice shall be included in all
14 : copies or substantial portions of the Software.
15 :
16 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 : SOFTWARE.
23 : */
24 :
25 : #include <cvector.h>
26 : #include <internal/cpow2.h>
27 : #include <internal/cprocsintern.h>
28 : #include <internal/csortimpl.h>
29 : #include <internal/test_helpers/cvector.h>
30 : #include <stdlib.h>
31 : #include <string.h>
32 :
33 : // The smallest capacity a vector ever has, and the factor it grows and shrinks
34 : // by. They're global so the white-box tests can read them.
35 : const size_t ccol__cvector_minimum_capacity = 4;
36 : const size_t ccol__cvector_scaling_factor = 2;
37 :
38 : // A pop shrinks the buffer once less than 1/4 of the capacity is in use. It's a
39 : // constant of its own, separate from the minimum capacity, so tuning one never
40 : // changes the other.
41 : static const size_t shrink_threshold_divisor = 4;
42 :
43 : // The vector itself. m_procs is NULL for the default allocator, an interned
44 : // procs pointer, or a pointer to the copy that sits right after this struct in
45 : // the same block.
46 : struct cvector {
47 : size_t elem_size;
48 : size_t elem_count;
49 : size_t capacity;
50 : ccol_memmgmt_procs *m_procs;
51 : void *data_ptr;
52 : };
53 :
54 : // The procs copy after the struct must be aligned for its type.
55 : _Static_assert(sizeof(struct cvector) % _Alignof(ccol_memmgmt_procs) == 0,
56 : "the procs copy after struct cvector must be aligned");
57 :
58 : // Frees the data buffer and the vector's own block, so cvector_destroy() and
59 : // the scope cleanup have one place that releases a vector. A NULL v does
60 : // nothing. The free function is read before the block goes away, because
61 : // m_procs can point into that block.
62 2266032 : void cvector__destroy(cvec v) {
63 2266032 : if (v) {
64 2245991 : if (v->data_ptr) {
65 2245895 : ccol__mem_free(v->m_procs, v->data_ptr);
66 : }
67 :
68 2245993 : if (v->m_procs) {
69 69229 : ccol_free free_func = v->m_procs->free;
70 69229 : free_func(v);
71 : } else {
72 2176764 : ccol__default_mem_free(v);
73 : }
74 : }
75 2266034 : }
76 :
77 : // Checks the arguments of cvector_create_full before anything is allocated, so
78 : // a bad argument never leaves a half-built vector. It gives false, with *err
79 : // set, for an elem_size of zero, an elem_size whose first 4-element buffer
80 : // would overflow size_t, or a procs struct with a NULL function.
81 2246077 : static bool verify_cvector_create_inputs(size_t elem_size,
82 : ccol_memmgmt_procs *mmgt_procs,
83 : char **err) {
84 2246077 : if (elem_size == 0) {
85 2 : if (err) {
86 2 : *err = CCOL__ERR_STR("elem_size is zero");
87 : }
88 2 : return false;
89 : }
90 :
91 2246075 : if (elem_size > SIZE_MAX / ccol__cvector_minimum_capacity) {
92 2 : if (err) {
93 2 : *err = CCOL__ERR_STR(
94 : "elem_size too large: initial capacity allocation would overflow");
95 : }
96 2 : return false;
97 : }
98 :
99 2246073 : if (!ccol__verify_memmgmt_procs(mmgt_procs, err)) {
100 3 : return false;
101 : }
102 :
103 : return true;
104 : }
105 :
106 : // Creates an empty vector with room for 4 elements of elem_size bytes; it's
107 : // the one constructor that every create path goes through. It gives NULL,
108 : // with *err set, when an argument is bad or an allocation fails. An interned
109 : // procs pointer is kept as it is; any other procs struct is copied into the
110 : // tail of the vector's block, so a vector costs two allocations.
111 2246077 : cvec cvector_create_full(size_t elem_size, ccol_memmgmt_procs *mmgt_procs,
112 : char **err) {
113 2246077 : if (!verify_cvector_create_inputs(elem_size, mmgt_procs, err)) {
114 : return NULL;
115 : }
116 :
117 2246069 : bool copy_procs = mmgt_procs && !ccol_procs_is_interned(mmgt_procs);
118 2246069 : cvec v;
119 2315212 : v = ccol__mem_calloc(
120 : mmgt_procs, 1,
121 : sizeof(cvector) + (copy_procs ? sizeof(ccol_memmgmt_procs) : 0));
122 2246068 : if (!v) {
123 98 : if (err) {
124 3 : *err = CCOL__ERR_STR("failed to allocate vector container");
125 : }
126 98 : return NULL;
127 : }
128 :
129 2245970 : if (copy_procs) {
130 178 : v->m_procs = (ccol_memmgmt_procs *)(v + 1);
131 178 : memcpy(v->m_procs, mmgt_procs, sizeof(ccol_memmgmt_procs));
132 : } else {
133 2245792 : v->m_procs = mmgt_procs;
134 : }
135 :
136 178 : v->data_ptr =
137 2245970 : ccol__mem_alloc(mmgt_procs, ccol__cvector_minimum_capacity * elem_size);
138 2245971 : if (!v->data_ptr) {
139 96 : cvector__destroy(v);
140 96 : if (err) {
141 2 : *err = CCOL__ERR_STR("failed to allocate data container");
142 : }
143 96 : return NULL;
144 : }
145 :
146 2245875 : if (err) {
147 864 : *err = NULL;
148 : }
149 :
150 2245875 : v->capacity = ccol__cvector_minimum_capacity;
151 2245875 : v->elem_count = 0;
152 2245875 : v->elem_size = elem_size;
153 :
154 2245875 : return v;
155 : }
156 :
157 : // Gives the vector's allocator (NULL for the default one), so a caller can
158 : // allocate related memory the same way. Asserts on a NULL v.
159 5 : ccol_memmgmt_procs *cvector_get_mprocs(cvec v) {
160 5 : if (!v) {
161 1 : ccol__assert(false);
162 : }
163 :
164 4 : return v->m_procs;
165 : }
166 :
167 : // Doubles the capacity of the buffer, which keeps push_back amortized O(1).
168 : // It gives ccol_container_full when the element count or the byte size would
169 : // overflow, and ccol_not_enough_memory when realloc fails, so push_back can
170 : // report the two apart. On failure the old buffer stays.
171 5587 : static ccol_retval scale_the_cvector_size_up(cvec v) {
172 5587 : if (!v) {
173 0 : ccol__assert(false);
174 : }
175 :
176 5587 : if (v->capacity > (ccol_max_elem_count / ccol__cvector_scaling_factor)) {
177 : return ccol_container_full;
178 : }
179 :
180 5587 : size_t new_capacity = ccol__cvector_scaling_factor * v->capacity;
181 5587 : if (new_capacity > SIZE_MAX / v->elem_size) {
182 : return ccol_container_full;
183 : }
184 :
185 5587 : void *orig = v->data_ptr;
186 11174 : v->data_ptr =
187 5587 : ccol__mem_realloc(v->m_procs, v->data_ptr, new_capacity * v->elem_size);
188 5587 : if (!v->data_ptr) {
189 2 : v->data_ptr = orig;
190 2 : return ccol_not_enough_memory;
191 : }
192 :
193 5585 : v->capacity *= ccol__cvector_scaling_factor;
194 5585 : return ccol_success;
195 : }
196 :
197 : // Halves the capacity of the buffer, never below the minimum, so a vector that
198 : // shrank gives memory back. A failed realloc is ignored, since the old buffer
199 : // holds the data just as well.
200 51074 : static void scale_the_cvector_size_down(cvec v) {
201 51074 : if (!v) {
202 0 : ccol__assert(false);
203 : }
204 :
205 51074 : if (v->capacity == ccol__cvector_minimum_capacity) {
206 : return;
207 : }
208 :
209 425 : void *orig = v->data_ptr;
210 425 : v->data_ptr = ccol__mem_realloc(
211 : v->m_procs, v->data_ptr,
212 : (v->capacity / ccol__cvector_scaling_factor) * v->elem_size);
213 425 : if (!v->data_ptr) {
214 1 : v->data_ptr = orig;
215 1 : return;
216 : }
217 :
218 424 : v->capacity /= ccol__cvector_scaling_factor;
219 : }
220 :
221 : // Copies *new_elem to the end of the vector, growing the buffer when it's
222 : // full; it's the raw append that cvec_push uses. It gives ccol_success or the
223 : // error of the growth. new_elem may point into v's own buffer, so before a
224 : // growth that can move the buffer we turn it into an offset and rebuild the
225 : // pointer afterwards.
226 3588010 : ccol_retval cvector_push_back(cvec v, const void *new_elem) {
227 3588010 : if (!v) {
228 1 : ccol__assert(false);
229 : }
230 :
231 3588009 : if (!new_elem) {
232 : return ccol_invalid_args;
233 : }
234 :
235 3588008 : if (v->elem_count == ccol_max_elem_count) {
236 : return ccol_container_full;
237 : }
238 :
239 3588008 : ccol_retval result = ccol_success;
240 :
241 3588008 : if (v->elem_count < v->capacity) {
242 3582421 : memcpy((void *)((char *)v->data_ptr + v->elem_count * v->elem_size),
243 : new_elem, v->elem_size);
244 3582421 : ++v->elem_count;
245 : } else {
246 5587 : const char *buf_start = (const char *)v->data_ptr;
247 5587 : const char *buf_end = buf_start + v->capacity * v->elem_size;
248 5587 : bool new_elem_aliases_self =
249 5587 : (const char *)new_elem >= buf_start && (const char *)new_elem < buf_end;
250 11174 : size_t new_elem_offset = new_elem_aliases_self
251 1 : ? (size_t)((const char *)new_elem - buf_start)
252 5587 : : 0;
253 :
254 5587 : ccol_retval grow_result = scale_the_cvector_size_up(v);
255 5587 : if (grow_result == ccol_success) {
256 5585 : if (new_elem_aliases_self) {
257 1 : new_elem = (const char *)v->data_ptr + new_elem_offset;
258 : }
259 5585 : memcpy((void *)((char *)v->data_ptr + v->elem_count * v->elem_size),
260 : new_elem, v->elem_size);
261 5585 : ++v->elem_count;
262 : } else {
263 : result = grow_result;
264 : }
265 : }
266 :
267 : return result;
268 : }
269 :
270 : // Removes the last element and copies it to *target_elem; it's the raw pop
271 : // that cvec_pop uses. The buffer shrinks once the vector is less than 1/4
272 : // full. An empty vector gives ccol_container_empty and leaves *target_elem
273 : // alone.
274 59391 : ccol_retval cvector_pop_back(cvec v, void *target_elem) {
275 59391 : if (!v) {
276 1 : ccol__assert(false);
277 : }
278 :
279 59390 : if (!target_elem) {
280 : return ccol_invalid_args;
281 : }
282 :
283 59389 : ccol_retval result = ccol_container_empty;
284 :
285 59389 : if (v->elem_count > 0) {
286 59302 : result = ccol_success;
287 59302 : --v->elem_count;
288 :
289 59302 : memcpy(target_elem,
290 59302 : (void *)((char *)v->data_ptr + v->elem_count * v->elem_size),
291 : v->elem_size);
292 :
293 59302 : if (v->elem_count < (v->capacity / shrink_threshold_divisor)) {
294 51074 : scale_the_cvector_size_down(v);
295 : }
296 : }
297 :
298 : return result;
299 : }
300 :
301 : // Grows the buffer to hold at least new_capacity_count elements, rounded up to
302 : // a power of two (at least the minimum), so a caller who knows the final size
303 : // avoids repeated reallocations. It never shrinks, and gives false on an
304 : // overflow or a failed realloc, with the vector unchanged.
305 537 : bool cvector_reserve(cvec v, size_t new_capacity_count) {
306 537 : if (!v) {
307 1 : ccol__assert(false);
308 : }
309 :
310 536 : size_t refined_new_capacity_count =
311 536 : ccol__find_nearest_gte_power_of_two(new_capacity_count);
312 536 : if (refined_new_capacity_count < ccol__cvector_minimum_capacity) {
313 : refined_new_capacity_count = ccol__cvector_minimum_capacity;
314 : }
315 :
316 528 : if (refined_new_capacity_count > ccol_max_elem_count) {
317 : return false;
318 : }
319 :
320 534 : if (refined_new_capacity_count > SIZE_MAX / v->elem_size) {
321 : return false;
322 : }
323 :
324 534 : if (refined_new_capacity_count <= v->capacity) {
325 : return true;
326 : }
327 :
328 249 : void *orig = v->data_ptr;
329 249 : v->data_ptr = ccol__mem_realloc(v->m_procs, v->data_ptr,
330 : refined_new_capacity_count * v->elem_size);
331 249 : if (!v->data_ptr) {
332 1 : v->data_ptr = orig;
333 1 : return false;
334 : }
335 :
336 248 : v->capacity = refined_new_capacity_count;
337 248 : return true;
338 : }
339 :
340 : #ifdef RUNNING_UNIT_TESTS
341 : // Counts the memmove branch of cvector_copy_into_tail, so a white-box test can
342 : // see that an overlapping copy really took the safe path. The copied data alone
343 : // can't tell, since memcpy may happen to work on an overlap.
344 : size_t cvector_overlap_copy_count_for_tests = 0;
345 : #endif
346 :
347 : // Copies n bytes from src to the slot after the last element; elem_count isn't
348 : // changed. It exists because an aliasing source can overlap the destination:
349 : // we use memmove when the ranges overlap and memcpy otherwise. In test builds
350 : // the memmove branch bumps cvector_overlap_copy_count_for_tests.
351 21 : static void cvector_copy_into_tail(cvec v, const void *src, size_t n) {
352 21 : char *dst = (char *)v->data_ptr + v->elem_count * v->elem_size;
353 21 : const char *s = (const char *)src;
354 21 : bool may_overlap = dst < s + n && s < dst + n;
355 21 : if (may_overlap) {
356 : #ifdef RUNNING_UNIT_TESTS
357 2 : cvector_overlap_copy_count_for_tests++;
358 : #endif
359 2 : memmove(dst, src, n);
360 : } else {
361 19 : memcpy(dst, src, n);
362 : }
363 21 : }
364 :
365 : // Appends elem_count elements from arr_ptr with at most one reallocation, which
366 : // is cheaper than a push per element. It gives false on an overflow or a failed
367 : // reservation. arr_ptr may point into v's own buffer: Such a source must stay
368 : // inside the reserved capacity (past it is a heap over-read), and it's turned
369 : // into an offset before cvector_reserve so the realloc can't leave it
370 : // dangling.
371 29 : bool cvector_append_array(cvec v, const void *arr_ptr, size_t elem_count) {
372 29 : if (!v) {
373 1 : ccol__assert(false);
374 : }
375 :
376 28 : if (elem_count == 0) {
377 : return true;
378 : }
379 :
380 26 : if (!arr_ptr) {
381 : return false;
382 : }
383 :
384 25 : size_t new_elem_count = v->elem_count + elem_count;
385 25 : if (new_elem_count < v->elem_count || new_elem_count > ccol_max_elem_count) {
386 : return false;
387 : }
388 :
389 23 : const char *buf_start = (const char *)v->data_ptr;
390 23 : const char *buf_end = buf_start + v->capacity * v->elem_size;
391 23 : bool arr_ptr_aliases_self =
392 23 : (const char *)arr_ptr >= buf_start && (const char *)arr_ptr < buf_end;
393 54 : size_t arr_ptr_offset =
394 23 : arr_ptr_aliases_self ? (size_t)((const char *)arr_ptr - buf_start) : 0;
395 :
396 10 : if (arr_ptr_aliases_self &&
397 10 : elem_count > (size_t)(buf_end - (const char *)arr_ptr) / v->elem_size) {
398 : return false;
399 : }
400 :
401 21 : if (new_elem_count <= v->capacity) {
402 9 : cvector_copy_into_tail(v, arr_ptr, elem_count * v->elem_size);
403 9 : v->elem_count += elem_count;
404 9 : return true;
405 : }
406 :
407 12 : if (!cvector_reserve(v, new_elem_count)) {
408 : return false;
409 : }
410 :
411 12 : if (arr_ptr_aliases_self) {
412 3 : arr_ptr = (const char *)v->data_ptr + arr_ptr_offset;
413 : }
414 :
415 12 : cvector_copy_into_tail(v, arr_ptr, elem_count * v->elem_size);
416 12 : v->elem_count += elem_count;
417 12 : return true;
418 : }
419 :
420 : // Appends every element of v_from to v_to, so joining two vectors needs no
421 : // loop. Asserts when either is NULL or the element sizes differ. Appending a
422 : // vector to itself reserves the room first, so the copy never reallocates
423 : // while it reads from the same buffer.
424 9 : bool cvector_append_cvector(cvec v_to, cvec v_from) {
425 9 : if (!v_to || !v_from || v_to->elem_size != v_from->elem_size) {
426 2 : ccol__assert(false);
427 : }
428 :
429 7 : if (v_to == v_from) {
430 2 : size_t count = v_from->elem_count;
431 2 : size_t new_count = v_to->elem_count + count;
432 2 : if (new_count < v_to->elem_count || new_count > ccol_max_elem_count) {
433 : return false;
434 : }
435 2 : if (!cvector_reserve(v_to, new_count)) {
436 : return false;
437 : }
438 : }
439 :
440 7 : return cvector_append_array(v_to, v_from->data_ptr, v_from->elem_count);
441 : }
442 :
443 : // Gives the start of the element buffer, for code that wants a plain C array.
444 : // Asserts on a NULL v.
445 2015 : void *cvector_data_ptr(cvec v) {
446 2015 : if (!v) {
447 1 : ccol__assert(false);
448 : }
449 :
450 2014 : return v->data_ptr;
451 : }
452 :
453 : // Gives the address of element index, or NULL when it's out of range, so a
454 : // caller can read or change an element in place. Asserts on a NULL v.
455 7928848 : void *cvector_at(cvec v, size_t index) {
456 7928848 : if (!v) {
457 1 : ccol__assert(false);
458 : }
459 :
460 7928847 : if (v->elem_count > 0 && index < v->elem_count) {
461 7928806 : return (void *)((char *)v->data_ptr + index * v->elem_size);
462 : }
463 :
464 : return NULL;
465 : }
466 :
467 : // Gives the number of elements; the struct is opaque, so this is the only way
468 : // to read it. Asserts on a NULL v.
469 4575235 : size_t cvector_size(cvec v) {
470 4575235 : if (!v) {
471 1 : ccol__assert(false);
472 : }
473 :
474 4575234 : return v->elem_count;
475 : }
476 :
477 : // Empties the vector and shrinks the buffer to the minimum capacity, so a
478 : // vector can be reused without a new create. When that realloc fails the
479 : // capacity stays as it is, and the count is zero anyway.
480 709 : void cvector_reset(cvec v) {
481 709 : if (!v) {
482 1 : ccol__assert(false);
483 : }
484 :
485 708 : if (v->capacity != ccol__cvector_minimum_capacity) {
486 26 : void *orig = v->data_ptr;
487 26 : v->data_ptr = ccol__mem_realloc(
488 : v->m_procs, v->data_ptr, ccol__cvector_minimum_capacity * v->elem_size);
489 26 : if (!v->data_ptr) {
490 1 : v->data_ptr = orig;
491 : } else {
492 25 : v->capacity = ccol__cvector_minimum_capacity;
493 : }
494 : }
495 708 : v->elem_count = 0;
496 708 : }
497 :
498 : // Sorts the elements in place with csort's stable mergesort; the two sort
499 : // macros need a function that sees the buffer, which the opaque struct hides
500 : // from them. The temporary buffer comes from v's allocator. It gives false,
501 : // with v unchanged, when that allocation fails.
502 141 : bool cvector__sort(cvec v, ccol_comparison_proc comparison_proc) {
503 141 : if (!v) {
504 0 : ccol__assert(false);
505 : }
506 141 : return csort__merge_sort_contiguous(v->data_ptr, v->elem_count, v->elem_size,
507 : comparison_proc, v->m_procs);
508 : }
509 :
510 : // Gives the index of the first element equal to *elem, or ccol_invalid_size,
511 : // so a caller can search without writing the loop. A NULL cmp means memcmp
512 : // over elem_size bytes.
513 151 : size_t cvector_find(cvec v, const void *elem, ccol_comparison_proc cmp) {
514 151 : if (!v) {
515 1 : ccol__assert(false);
516 : }
517 :
518 150 : if (!elem) {
519 : return ccol_invalid_size;
520 : }
521 :
522 5165 : for (size_t i = 0; i < v->elem_count; i++) {
523 5151 : const void *current = (const char *)v->data_ptr + i * v->elem_size;
524 75 : bool match = cmp ? (cmp(current, elem) == 0)
525 5226 : : (memcmp(current, elem, v->elem_size) == 0);
526 5151 : if (match) {
527 135 : return i;
528 : }
529 : }
530 :
531 : return ccol_invalid_size;
532 : }
533 :
534 : // The vector's iterator. base must be the first member, so a cmap_iterator *
535 : // is also a cvec_iter_impl *. key_pair points at index and val_pair at the
536 : // current element.
537 : typedef struct {
538 : cmap_iterator base;
539 : cmap_pair key_pair_storage;
540 : cmap_pair val_pair_storage;
541 : size_t index;
542 : cvec vec;
543 : // The allocator's free function (NULL for the default). The iterator keeps
544 : // its own copy because a scope-exit cleanup can free it after the vector, and
545 : // the vector's procs, are gone.
546 : ccol_free free_fn;
547 : } cvec_iter_impl;
548 :
549 : // Frees an iterator with the free function it saved. It never goes through
550 : // the vector, because the scope cleanup can run after the vector is gone.
551 10 : static void cvec_iter_impl_free(cmap_iterator *it) {
552 10 : cvec_iter_impl *impl = (cvec_iter_impl *)it;
553 10 : ccol_free free_fn = impl->free_fn;
554 5 : if (free_fn) {
555 2 : free_fn(impl);
556 : } else {
557 8 : ccol__default_mem_free(impl);
558 : }
559 5 : }
560 :
561 : // Moves the iterator to the next element, or frees it and gives NULL after the
562 : // last one; it's the next function that ccol_iter_next() calls.
563 29 : static cmap_iterator *cvec_cmap_iter_next(cmap_iterator *it) {
564 29 : if (!it) {
565 0 : ccol__assert(false);
566 : }
567 29 : cvec_iter_impl *impl = (cvec_iter_impl *)it;
568 29 : size_t next_index = impl->index + 1;
569 29 : if (next_index >= impl->vec->elem_count) {
570 5 : cvec_iter_impl_free(it);
571 5 : return NULL;
572 : }
573 24 : impl->index = next_index;
574 24 : impl->val_pair_storage.ptr =
575 24 : (char *)impl->vec->data_ptr + next_index * impl->vec->elem_size;
576 24 : return it;
577 : }
578 :
579 : // Creates an iterator at the first element, so a vector works with the common
580 : // ccol_begin() API. It's allocated with v's allocator. It gives NULL for a
581 : // NULL or empty vector (with *err NULL) and for a failed allocation (with
582 : // *err set).
583 14 : cmap_iterator *cvector_begin_iter(cvec v, char **err) {
584 14 : if (err) {
585 13 : *err = NULL;
586 : }
587 14 : if (!v) {
588 : return NULL;
589 : }
590 12 : if (v->elem_count == 0) {
591 : return NULL;
592 : }
593 22 : cvec_iter_impl *impl =
594 11 : ccol__mem_calloc(v->m_procs, 1, sizeof(cvec_iter_impl));
595 11 : if (!impl) {
596 1 : if (err) {
597 1 : *err = CCOL__ERR_STR("Failed to allocate iterator");
598 : }
599 1 : return NULL;
600 : }
601 10 : impl->index = 0;
602 10 : impl->vec = v;
603 10 : impl->free_fn = v->m_procs ? v->m_procs->free : NULL;
604 10 : impl->key_pair_storage.ptr = &impl->index;
605 10 : impl->key_pair_storage.size = sizeof(size_t);
606 10 : impl->val_pair_storage.ptr = v->data_ptr;
607 10 : impl->val_pair_storage.size = v->elem_size;
608 10 : impl->base.key_pair = &impl->key_pair_storage;
609 10 : impl->base.val_pair = &impl->val_pair_storage;
610 10 : impl->base.ccol__next_fn = cvec_cmap_iter_next;
611 10 : impl->base.ccol__free_fn = cvec_iter_impl_free;
612 10 : impl->base.ccol__direct_ptr = true;
613 10 : return &impl->base;
614 : }
615 :
616 : #ifdef RUNNING_UNIT_TESTS
617 : // Test hook: Gives the capacity of the buffer, so the tests can check the
618 : // growth and shrink thresholds that the public API doesn't show.
619 2114 : size_t cvector_get_capacity(cvec v) {
620 2114 : if (!v) {
621 0 : ccol__assert(false);
622 : }
623 :
624 2114 : return v->capacity;
625 : }
626 : #endif
|