aboutsummaryrefslogtreecommitdiff
path: root/vec.c
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2025-08-20 22:33:40 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2025-08-20 22:33:40 +0100
commit6e2db6825d4ff4b57be3086f654a84cc9ff64bcf (patch)
treeb9a7bb71ff55f7ef6d34ab90574ed86cd1f39b43 /vec.c
parent2369185b269164c9fd3cc888dbde117192d08bff (diff)
downloadalisp-6e2db6825d4ff4b57be3086f654a84cc9ff64bcf.tar.gz
alisp-6e2db6825d4ff4b57be3086f654a84cc9ff64bcf.tar.bz2
alisp-6e2db6825d4ff4b57be3086f654a84cc9ff64bcf.zip
Stable vector implementation
Stable vectors will be used in the lisp runtime to implement actual vectors, instead of using the disgusting lvec trick. This way we at least can get attributes about the vector through one pointer hop.
Diffstat (limited to 'vec.c')
-rw-r--r--vec.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/vec.c b/vec.c
new file mode 100644
index 0000000..84f6944
--- /dev/null
+++ b/vec.c
@@ -0,0 +1,57 @@
+/* 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_free(vec_t *vec)
+{
+ if (!vec)
+ return;
+ if (vec->data)
+ free(vec->data);
+ memset(vec, 0, sizeof(*vec));
+}
+
+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);
+ vec->data = realloc(vec->data, vec->capacity);
+ }
+}
+
+void vec_append(vec_t *vec, void *ptr, u64 size)
+{
+ if (!vec)
+ return;
+ vec_ensure_free(vec, size);
+ memcpy(vec->data + vec->size, ptr, size);
+ vec->size += size;
+}
+
+void vec_clone(vec_t *dest, vec_t *src)
+{
+ if (!src || !dest)
+ return;
+ dest = src;
+ dest->data = calloc(1, dest->capacity);
+ memcpy(dest->data, src->data, src->size);
+}