aboutsummaryrefslogtreecommitdiff
path: root/impl/vec.c
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2025-09-01 21:26:01 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2025-09-01 21:26:01 +0100
commit700c3b1d1b3ed835ffab3fd502ab91baba8e2d1f (patch)
tree69029747231d8fa60d3cb05f965e796d2211f021 /impl/vec.c
parentcc56a2ee2b5703f9ea5ac63a86870af188845c30 (diff)
downloadalisp-700c3b1d1b3ed835ffab3fd502ab91baba8e2d1f.tar.gz
alisp-700c3b1d1b3ed835ffab3fd502ab91baba8e2d1f.tar.bz2
alisp-700c3b1d1b3ed835ffab3fd502ab91baba8e2d1f.zip
Move implementation files into their own folder
main.c and test.c generate binary executables so they can stay in the main folder, but the rest can go into their own dedicated folder to make it look nicer
Diffstat (limited to 'impl/vec.c')
-rw-r--r--impl/vec.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/impl/vec.c b/impl/vec.c
new file mode 100644
index 0000000..12f577c
--- /dev/null
+++ b/impl/vec.c
@@ -0,0 +1,94 @@
+/* Copyright (C) 2025 Aryadev Chavali
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Unlicense
+ * for details.
+
+ * You may distribute and modify this code under the terms of the
+ * Unlicense, which you should have received a copy of along with this
+ * program. If not, please go to <https://unlicense.org/>.
+
+ * Created: 2025-08-20
+ * Description: Stable Vector implementation
+ */
+
+#include <malloc.h>
+#include <string.h>
+
+#include <alisp.h>
+
+void vec_init(vec_t *vec, u64 size)
+{
+ memset(vec, 0, sizeof(*vec));
+ if (!vec)
+ return;
+ else if (size <= VEC_INLINE_CAPACITY)
+ {
+ vec->is_inlined = 1;
+ vec->capacity = VEC_INLINE_CAPACITY;
+ vec->ptr = NULL;
+ }
+ else
+ {
+ vec->is_inlined = 0;
+ vec->capacity = size;
+ vec->ptr = calloc(1, vec->capacity);
+ }
+}
+
+void vec_free(vec_t *vec)
+{
+ if (!vec)
+ return;
+ if (!vec->is_inlined && vec->ptr)
+ free(vec->ptr);
+ memset(vec, 0, sizeof(*vec));
+}
+
+void *vec_data(vec_t *vec)
+{
+ return vec->is_inlined ? vec->inlined : vec->ptr;
+}
+
+void vec_ensure_free(vec_t *vec, u64 size)
+{
+ if (!vec)
+ return;
+ if (vec->capacity - vec->size < size)
+ {
+ vec->capacity = MAX(vec->capacity * VEC_MULT, vec->size + size);
+ if (vec->is_inlined)
+ {
+ // If we're inlined, we need to allocate on the heap now. So let's copy
+ // vec->inlined over to vec->ptr, then turn off inlining.
+
+ // We need to do a two-way swap since vec->ptr and vec->inlined are taking
+ // up the same space.
+ u8 buffer[VEC_INLINE_CAPACITY];
+ memcpy(buffer, vec->inlined, vec->size);
+ vec->ptr = calloc(1, vec->capacity);
+ memcpy(vec->ptr, buffer, vec->size);
+ vec->is_inlined = 0;
+ }
+ else
+ vec->ptr = realloc(vec->ptr, vec->capacity);
+ }
+}
+
+void vec_append(vec_t *vec, const void *const ptr, u64 size)
+{
+ if (!vec)
+ return;
+ vec_ensure_free(vec, size);
+ memcpy(vec_data(vec) + vec->size, ptr, size);
+ vec->size += size;
+}
+
+void vec_clone(vec_t *dest, vec_t *src)
+{
+ if (!src || !dest)
+ return;
+ vec_init(dest, src->capacity);
+ memcpy(vec_data(dest), vec_data(src), src->size);
+}