2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2021 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)
54 // If this is true, we might overflow.
55 if (len > SIZE_MAX / 2) cap = len;
58 // Keep doubling until larger.
67 v->v = bc_vm_realloc(v->v, bc_vm_arraySize(cap, v->size));
70 BC_SIG_TRYUNLOCK(lock);
74 bc_vec_init(BcVec* restrict v, size_t esize, BcDtorType dtor)
78 assert(v != NULL && esize);
80 v->v = bc_vm_malloc(bc_vm_arraySize(BC_VEC_START_CAP, esize));
82 v->size = (BcSize) esize;
83 v->cap = BC_VEC_START_CAP;
85 v->dtor = (BcSize) dtor;
89 bc_vec_expand(BcVec* restrict v, size_t req)
93 // Only expand if necessary.
100 v->v = bc_vm_realloc(v->v, bc_vm_arraySize(req, v->size));
103 BC_SIG_TRYUNLOCK(lock);
108 bc_vec_npop(BcVec* restrict v, size_t n)
112 assert(v != NULL && n <= v->len);
114 BC_SIG_TRYLOCK(lock);
116 if (!v->dtor) v->len -= n;
119 const BcVecFree d = bc_vec_dtors[v->dtor];
120 size_t esize = v->size;
121 size_t len = v->len - n;
123 // Loop through and manually destruct every element.
126 d(v->v + (esize * --v->len));
130 BC_SIG_TRYUNLOCK(lock);
134 bc_vec_npopAt(BcVec* restrict v, size_t n, size_t idx)
141 assert(idx + n < v->len);
143 // Grab start and end pointers.
144 ptr = bc_vec_item(v, idx);
145 data = bc_vec_item(v, idx + n);
147 BC_SIG_TRYLOCK(lock);
152 const BcVecFree d = bc_vec_dtors[v->dtor];
154 // Destroy every popped item.
155 for (i = 0; i < n; ++i)
157 d(bc_vec_item(v, idx + i));
163 memmove(ptr, data, (v->len - idx) * v->size);
165 BC_SIG_TRYUNLOCK(lock);
169 bc_vec_npush(BcVec* restrict v, size_t n, const void* data)
174 assert(v != NULL && data != NULL);
176 BC_SIG_TRYLOCK(lock);
178 // Grow if necessary.
179 if (v->len + n > v->cap) bc_vec_grow(v, n);
183 // Copy the elements in.
185 memcpy(v->v + (esize * v->len), data, esize * n);
188 BC_SIG_TRYUNLOCK(lock);
192 bc_vec_push(BcVec* restrict v, const void* data)
194 bc_vec_npush(v, 1, data);
198 bc_vec_pushEmpty(BcVec* restrict v)
205 BC_SIG_TRYLOCK(lock);
207 // Grow if necessary.
208 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
210 ptr = v->v + v->size * v->len;
213 BC_SIG_TRYUNLOCK(lock);
219 bc_vec_pushByte(BcVec* restrict v, uchar data)
221 assert(v != NULL && v->size == sizeof(uchar));
222 bc_vec_npush(v, 1, &data);
226 bc_vec_pushIndex(BcVec* restrict v, size_t idx)
228 uchar amt, nums[sizeof(size_t) + 1];
231 assert(v->size == sizeof(uchar));
234 for (amt = 0; idx; ++amt)
236 nums[amt + 1] = (uchar) idx;
237 idx &= ((size_t) ~(UCHAR_MAX));
238 idx >>= sizeof(uchar) * CHAR_BIT;
243 // Push the index onto the vector.
244 bc_vec_npush(v, amt + 1, nums);
248 bc_vec_pushAt(BcVec* restrict v, const void* data, size_t idx)
250 assert(v != NULL && data != NULL && idx <= v->len);
252 BC_SIG_ASSERT_LOCKED;
255 if (idx == v->len) bc_vec_push(v, data);
261 // Grow if necessary.
262 if (v->len == v->cap) bc_vec_grow(v, 1);
266 ptr = v->v + esize * idx;
269 memmove(ptr + esize, ptr, esize * (v->len++ - idx));
271 memcpy(ptr, data, esize);
276 bc_vec_string(BcVec* restrict v, size_t len, const char* restrict str)
280 assert(v != NULL && v->size == sizeof(char));
282 assert(!v->len || !v->v[v->len - 1]);
285 BC_SIG_TRYLOCK(lock);
288 bc_vec_expand(v, bc_vm_growSize(len, 1));
290 memcpy(v->v, str, len);
293 bc_vec_pushByte(v, '\0');
295 BC_SIG_TRYUNLOCK(lock);
299 bc_vec_concat(BcVec* restrict v, const char* restrict str)
303 assert(v != NULL && v->size == sizeof(char));
305 assert(!v->len || !v->v[v->len - 1]);
308 BC_SIG_TRYLOCK(lock);
310 // If there is already a string, erase its nul byte.
311 if (v->len) v->len -= 1;
313 bc_vec_npush(v, strlen(str) + 1, str);
315 BC_SIG_TRYUNLOCK(lock);
319 bc_vec_empty(BcVec* restrict v)
323 assert(v != NULL && v->size == sizeof(char));
326 BC_SIG_TRYLOCK(lock);
329 bc_vec_pushByte(v, '\0');
331 BC_SIG_TRYUNLOCK(lock);
334 #if BC_ENABLE_HISTORY
336 bc_vec_replaceAt(BcVec* restrict v, size_t idx, const void* data)
340 BC_SIG_ASSERT_LOCKED;
344 ptr = bc_vec_item(v, idx);
346 if (v->dtor) bc_vec_dtors[v->dtor](ptr);
349 memcpy(ptr, data, v->size);
351 #endif // BC_ENABLE_HISTORY
354 bc_vec_item(const BcVec* restrict v, size_t idx)
356 assert(v != NULL && v->len && idx < v->len);
357 return v->v + v->size * idx;
361 bc_vec_item_rev(const BcVec* restrict v, size_t idx)
363 assert(v != NULL && v->len && idx < v->len);
364 return v->v + v->size * (v->len - idx - 1);
368 bc_vec_clear(BcVec* restrict v)
370 BC_SIG_ASSERT_LOCKED;
373 v->dtor = BC_DTOR_NONE;
377 bc_vec_free(void* vec)
379 BcVec* v = (BcVec*) vec;
380 BC_SIG_ASSERT_LOCKED;
385 #if !BC_ENABLE_LIBRARY
388 * Finds a name in a map by binary search. Returns the index where the item
389 * *would* be if it doesn't exist. Callers are responsible for checking that the
390 * item exists at the index.
392 * @param name The name to find.
393 * @return The index of the item with @a name, or where the item would be
394 * if it does not exist.
397 bc_map_find(const BcVec* restrict v, const char* name)
399 size_t low = 0, high = v->len;
403 size_t mid = (low + high) / 2;
404 const BcId* id = bc_vec_item(v, mid);
405 int result = strcmp(name, id->name);
407 if (!result) return mid;
408 else if (result < 0) high = mid;
416 bc_map_insert(BcVec* restrict v, const char* name, size_t idx,
422 BC_SIG_ASSERT_LOCKED;
424 assert(v != NULL && name != NULL && i != NULL);
426 *i = bc_map_find(v, name);
428 assert(*i <= v->len);
430 if (*i != v->len && !strcmp(name, ((BcId*) bc_vec_item(v, *i))->name))
436 slabs = BC_IS_DC ? &vm.main_slabs : &vm.other_slabs;
438 slabs = &vm.main_slabs;
441 id.name = bc_slabvec_strdup(slabs, name);
444 bc_vec_pushAt(v, &id, *i);
450 bc_map_index(const BcVec* restrict v, const char* name)
454 assert(v != NULL && name != NULL);
456 i = bc_map_find(v, name);
458 // If out of range, return invalid.
459 if (i >= v->len) return BC_VEC_INVALID_IDX;
461 // Make sure the item exists.
462 return strcmp(name, ((BcId*) bc_vec_item(v, i))->name) ?
469 bc_map_name(const BcVec* restrict v, size_t idx)
471 size_t i, len = v->len;
473 for (i = 0; i < len; ++i)
475 BcId* id = (BcId*) bc_vec_item(v, i);
476 if (id->idx == idx) return id->name;
486 * Initializes a single slab.
487 * @param s The slab to initialize.
490 bc_slab_init(BcSlab* s)
492 s->s = bc_vm_malloc(BC_SLAB_SIZE);
497 * Adds a string to a slab and returns a pointer to it, or NULL if it could not
499 * @param s The slab to add to.
500 * @param str The string to add.
501 * @param len The length of the string, including its nul byte.
502 * @return A pointer to the new string in the slab, or NULL if it could not
506 bc_slab_add(BcSlab* s, const char* str, size_t len)
512 assert(len == strlen(str) + 1);
514 if (s->len + len > BC_SLAB_SIZE) return NULL;
516 ptr = (char*) (s->s + s->len);
519 bc_strcpy(ptr, len, str);
527 bc_slab_free(void* slab)
529 free(((BcSlab*) slab)->s);
533 bc_slabvec_init(BcVec* v)
539 bc_vec_init(v, sizeof(BcSlab), BC_DTOR_SLAB);
541 // We always want to have at least one slab.
542 slab = bc_vec_pushEmpty(v);
547 bc_slabvec_strdup(BcVec* v, const char* str)
554 BC_SIG_ASSERT_LOCKED;
556 assert(v != NULL && v->len);
560 len = strlen(str) + 1;
562 // If the len is greater than 128, then just allocate it with malloc.
563 if (BC_UNLIKELY(len >= BC_SLAB_SIZE))
565 // SIZE_MAX is a marker for these standalone allocations.
567 slab.s = bc_vm_strdup(str);
569 // Push the standalone slab.
570 bc_vec_pushAt(v, &slab, v->len - 1);
576 slab_ptr = bc_vec_top(v);
577 s = bc_slab_add(slab_ptr, str, len);
579 // If it couldn't be added, add a slab and try again.
580 if (BC_UNLIKELY(s == NULL))
582 slab_ptr = bc_vec_pushEmpty(v);
583 bc_slab_init(slab_ptr);
585 s = bc_slab_add(slab_ptr, str, len);
594 bc_slabvec_clear(BcVec* v)
599 // This complicated loop exists because of standalone allocations over 128
603 // Get the first slab.
604 s = bc_vec_item(v, 0);
606 // Either the slab must be valid (not standalone), or there must be
608 assert(s->len != SIZE_MAX || v->len > 1);
610 // Do we have to loop again? We do if it's a standalone allocation.
611 again = (s->len == SIZE_MAX);
613 // Pop the standalone allocation, not the one after it.
614 if (again) bc_vec_npopAt(v, 1, 0);
618 // If we get here, we know that the first slab is a valid slab. We want to
619 // pop all of the other slabs.
620 if (v->len > 1) bc_vec_npop(v, v->len - 1);
622 // Empty the first slab.
625 #endif // !BC_ENABLE_LIBRARY
630 bc_slabvec_print(BcVec* v, const char* func)
635 bc_file_printf(&vm.ferr, "%s\n", func);
637 for (i = 0; i < v->len; ++i)
639 s = bc_vec_item(v, i);
640 bc_file_printf(&vm.ferr, "%zu { s = %zu, len = %zu }\n", i,
641 (uintptr_t) s->s, s->len);
644 bc_file_puts(&vm.ferr, bc_flush_none, "\n");
645 bc_file_flush(&vm.ferr, bc_flush_none);
648 #endif // BC_DEBUG_CODE