2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2023 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * *****************************************************************************
32 * Code to manipulate vectors (resizable arrays).
46 bc_vec_grow(BcVec* restrict v, size_t n)
49 #if !BC_ENABLE_LIBRARY
51 #endif // !BC_ENABLE_LIBRARY
56 // If this is true, we might overflow.
57 if (len > SIZE_MAX / 2) cap = len;
60 // Keep doubling until larger.
69 v->v = bc_vm_realloc(v->v, bc_vm_arraySize(cap, v->size));
72 BC_SIG_TRYUNLOCK(lock);
76 bc_vec_init(BcVec* restrict v, size_t esize, BcDtorType dtor)
80 assert(v != NULL && esize);
82 v->v = bc_vm_malloc(bc_vm_arraySize(BC_VEC_START_CAP, esize));
84 v->size = (BcSize) esize;
85 v->cap = BC_VEC_START_CAP;
87 v->dtor = (BcSize) dtor;
91 bc_vec_expand(BcVec* restrict v, size_t req)
95 // Only expand if necessary.
98 #if !BC_ENABLE_LIBRARY
100 #endif // !BC_ENABLE_LIBRARY
102 BC_SIG_TRYLOCK(lock);
104 v->v = bc_vm_realloc(v->v, bc_vm_arraySize(req, v->size));
107 BC_SIG_TRYUNLOCK(lock);
112 bc_vec_npop(BcVec* restrict v, size_t n)
114 #if !BC_ENABLE_LIBRARY
116 #endif // !BC_ENABLE_LIBRARY
118 assert(v != NULL && n <= v->len);
120 BC_SIG_TRYLOCK(lock);
122 if (!v->dtor) v->len -= n;
125 const BcVecFree d = bc_vec_dtors[v->dtor];
126 size_t esize = v->size;
127 size_t len = v->len - n;
129 // Loop through and manually destruct every element.
132 d(v->v + (esize * --v->len));
136 BC_SIG_TRYUNLOCK(lock);
140 bc_vec_npopAt(BcVec* restrict v, size_t n, size_t idx)
144 #if !BC_ENABLE_LIBRARY
146 #endif // !BC_ENABLE_LIBRARY
149 assert(idx + n < v->len);
151 // Grab start and end pointers.
152 ptr = bc_vec_item(v, idx);
153 data = bc_vec_item(v, idx + n);
155 BC_SIG_TRYLOCK(lock);
160 const BcVecFree d = bc_vec_dtors[v->dtor];
162 // Destroy every popped item.
163 for (i = 0; i < n; ++i)
165 d(bc_vec_item(v, idx + i));
171 memmove(ptr, data, (v->len - idx) * v->size);
173 BC_SIG_TRYUNLOCK(lock);
177 bc_vec_npush(BcVec* restrict v, size_t n, const void* data)
179 #if !BC_ENABLE_LIBRARY
181 #endif // !BC_ENABLE_LIBRARY
184 assert(v != NULL && data != NULL);
186 BC_SIG_TRYLOCK(lock);
188 // Grow if necessary.
189 if (v->len + n > v->cap) bc_vec_grow(v, n);
193 // Copy the elements in.
195 memcpy(v->v + (esize * v->len), data, esize * n);
198 BC_SIG_TRYUNLOCK(lock);
202 bc_vec_push(BcVec* restrict v, const void* data)
204 bc_vec_npush(v, 1, data);
208 bc_vec_pushEmpty(BcVec* restrict v)
210 #if !BC_ENABLE_LIBRARY
212 #endif // !BC_ENABLE_LIBRARY
217 BC_SIG_TRYLOCK(lock);
219 // Grow if necessary.
220 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
222 ptr = v->v + v->size * v->len;
225 BC_SIG_TRYUNLOCK(lock);
231 bc_vec_pushByte(BcVec* restrict v, uchar data)
233 assert(v != NULL && v->size == sizeof(uchar));
234 bc_vec_npush(v, 1, &data);
238 bc_vec_pushIndex(BcVec* restrict v, size_t idx)
240 uchar amt, nums[sizeof(size_t) + 1];
243 assert(v->size == sizeof(uchar));
246 for (amt = 0; idx; ++amt)
248 nums[amt + 1] = (uchar) idx;
249 idx &= ((size_t) ~(UCHAR_MAX));
250 idx >>= sizeof(uchar) * CHAR_BIT;
255 // Push the index onto the vector.
256 bc_vec_npush(v, amt + 1, nums);
260 bc_vec_pushAt(BcVec* restrict v, const void* data, size_t idx)
262 assert(v != NULL && data != NULL && idx <= v->len);
264 BC_SIG_ASSERT_LOCKED;
267 if (idx == v->len) bc_vec_push(v, data);
273 // Grow if necessary.
274 if (v->len == v->cap) bc_vec_grow(v, 1);
278 ptr = v->v + esize * idx;
281 memmove(ptr + esize, ptr, esize * (v->len++ - idx));
283 memcpy(ptr, data, esize);
288 bc_vec_string(BcVec* restrict v, size_t len, const char* restrict str)
290 #if !BC_ENABLE_LIBRARY
292 #endif // !BC_ENABLE_LIBRARY
294 assert(v != NULL && v->size == sizeof(char));
296 assert(!v->len || !v->v[v->len - 1]);
299 BC_SIG_TRYLOCK(lock);
302 bc_vec_expand(v, bc_vm_growSize(len, 1));
304 memcpy(v->v, str, len);
307 bc_vec_pushByte(v, '\0');
309 BC_SIG_TRYUNLOCK(lock);
313 bc_vec_concat(BcVec* restrict v, const char* restrict str)
315 #if !BC_ENABLE_LIBRARY
317 #endif // !BC_ENABLE_LIBRARY
319 assert(v != NULL && v->size == sizeof(char));
321 assert(!v->len || !v->v[v->len - 1]);
324 BC_SIG_TRYLOCK(lock);
326 // If there is already a string, erase its nul byte.
327 if (v->len) v->len -= 1;
329 bc_vec_npush(v, strlen(str) + 1, str);
331 BC_SIG_TRYUNLOCK(lock);
335 bc_vec_empty(BcVec* restrict v)
337 #if !BC_ENABLE_LIBRARY
339 #endif // !BC_ENABLE_LIBRARY
341 assert(v != NULL && v->size == sizeof(char));
344 BC_SIG_TRYLOCK(lock);
347 bc_vec_pushByte(v, '\0');
349 BC_SIG_TRYUNLOCK(lock);
352 #if BC_ENABLE_HISTORY
354 bc_vec_replaceAt(BcVec* restrict v, size_t idx, const void* data)
358 BC_SIG_ASSERT_LOCKED;
362 ptr = bc_vec_item(v, idx);
364 if (v->dtor) bc_vec_dtors[v->dtor](ptr);
367 memcpy(ptr, data, v->size);
369 #endif // BC_ENABLE_HISTORY
372 bc_vec_item(const BcVec* restrict v, size_t idx)
374 assert(v != NULL && v->len && idx < v->len);
375 return v->v + v->size * idx;
379 bc_vec_item_rev(const BcVec* restrict v, size_t idx)
381 assert(v != NULL && v->len && idx < v->len);
382 return v->v + v->size * (v->len - idx - 1);
386 bc_vec_clear(BcVec* restrict v)
388 BC_SIG_ASSERT_LOCKED;
391 v->dtor = BC_DTOR_NONE;
395 bc_vec_free(void* vec)
397 BcVec* v = (BcVec*) vec;
398 BC_SIG_ASSERT_LOCKED;
403 #if !BC_ENABLE_LIBRARY
406 * Finds a name in a map by binary search. Returns the index where the item
407 * *would* be if it doesn't exist. Callers are responsible for checking that the
408 * item exists at the index.
410 * @param name The name to find.
411 * @return The index of the item with @a name, or where the item would be
412 * if it does not exist.
415 bc_map_find(const BcVec* restrict v, const char* name)
417 size_t low = 0, high = v->len;
421 size_t mid = low + (high - low) / 2;
422 const BcId* id = bc_vec_item(v, mid);
423 int result = strcmp(name, id->name);
425 if (!result) return mid;
426 else if (result < 0) high = mid;
434 bc_map_insert(BcVec* restrict v, const char* name, size_t idx,
439 BC_SIG_ASSERT_LOCKED;
441 assert(v != NULL && name != NULL && i != NULL);
443 *i = bc_map_find(v, name);
445 assert(*i <= v->len);
447 if (*i != v->len && !strcmp(name, ((BcId*) bc_vec_item(v, *i))->name))
452 id.name = bc_slabvec_strdup(&vm->slabs, name);
455 bc_vec_pushAt(v, &id, *i);
461 bc_map_index(const BcVec* restrict v, const char* name)
466 assert(v != NULL && name != NULL);
468 i = bc_map_find(v, name);
470 // If out of range, return invalid.
471 if (i >= v->len) return BC_VEC_INVALID_IDX;
473 id = (BcId*) bc_vec_item(v, i);
475 // Make sure the item exists and return appropriately.
476 return strcmp(name, id->name) ? BC_VEC_INVALID_IDX : i;
481 bc_map_name(const BcVec* restrict v, size_t idx)
483 size_t i, len = v->len;
485 for (i = 0; i < len; ++i)
487 BcId* id = (BcId*) bc_vec_item(v, i);
488 if (id->idx == idx) return id->name;
500 * Initializes a single slab.
501 * @param s The slab to initialize.
504 bc_slab_init(BcSlab* s)
506 s->s = bc_vm_malloc(BC_SLAB_SIZE);
511 * Adds a string to a slab and returns a pointer to it, or NULL if it could not
513 * @param s The slab to add to.
514 * @param str The string to add.
515 * @param len The length of the string, including its nul byte.
516 * @return A pointer to the new string in the slab, or NULL if it could not
520 bc_slab_add(BcSlab* s, const char* str, size_t len)
526 assert(len == strlen(str) + 1);
528 if (s->len + len > BC_SLAB_SIZE) return NULL;
530 ptr = (char*) (s->s + s->len);
533 bc_strcpy(ptr, len, str);
541 bc_slab_free(void* slab)
543 free(((BcSlab*) slab)->s);
547 bc_slabvec_init(BcVec* v)
553 bc_vec_init(v, sizeof(BcSlab), BC_DTOR_SLAB);
555 // We always want to have at least one slab.
556 slab = bc_vec_pushEmpty(v);
561 bc_slabvec_strdup(BcVec* v, const char* str)
568 BC_SIG_ASSERT_LOCKED;
570 assert(v != NULL && v->len);
574 len = strlen(str) + 1;
576 // If the len is greater than 128, then just allocate it with malloc.
577 if (BC_UNLIKELY(len >= BC_SLAB_SIZE))
579 // SIZE_MAX is a marker for these standalone allocations.
581 slab.s = bc_vm_strdup(str);
583 // Push the standalone slab.
584 bc_vec_pushAt(v, &slab, v->len - 1);
590 slab_ptr = bc_vec_top(v);
591 s = bc_slab_add(slab_ptr, str, len);
593 // If it couldn't be added, add a slab and try again.
594 if (BC_UNLIKELY(s == NULL))
596 slab_ptr = bc_vec_pushEmpty(v);
597 bc_slab_init(slab_ptr);
599 s = bc_slab_add(slab_ptr, str, len);
608 bc_slabvec_clear(BcVec* v)
613 // This complicated loop exists because of standalone allocations over 128
617 // Get the first slab.
618 s = bc_vec_item(v, 0);
620 // Either the slab must be valid (not standalone), or there must be
622 assert(s->len != SIZE_MAX || v->len > 1);
624 // Do we have to loop again? We do if it's a standalone allocation.
625 again = (s->len == SIZE_MAX);
627 // Pop the standalone allocation, not the one after it.
628 if (again) bc_vec_npopAt(v, 1, 0);
632 // If we get here, we know that the first slab is a valid slab. We want to
633 // pop all of the other slabs.
634 if (v->len > 1) bc_vec_npop(v, v->len - 1);
636 // Empty the first slab.
639 #endif // !BC_ENABLE_LIBRARY
644 bc_slabvec_print(BcVec* v, const char* func)
649 bc_file_printf(&vm->ferr, "%s\n", func);
651 for (i = 0; i < v->len; ++i)
653 s = bc_vec_item(v, i);
654 bc_file_printf(&vm->ferr, "%zu { s = %zu, len = %zu }\n", i,
655 (uintptr_t) s->s, s->len);
658 bc_file_puts(&vm->ferr, bc_flush_none, "\n");
659 bc_file_flush(&vm->ferr, bc_flush_none);
662 #endif // BC_DEBUG_CODE