aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--alisp.h30
-rw-r--r--build.sh2
-rw-r--r--constructor.c2
-rw-r--r--ivec.c76
-rw-r--r--symtable.c5
-rw-r--r--sys.c2
-rw-r--r--vec.c76
7 files changed, 97 insertions, 96 deletions
diff --git a/alisp.h b/alisp.h
index 72635b8..a0e84c9 100644
--- a/alisp.h
+++ b/alisp.h
@@ -46,25 +46,25 @@ typedef struct
sv_t sv_copy(sv_t old);
-/// Dynamic arrays
+/// Inlined Dynamic arrays
-typedef struct
+typedef struct InlineVector
{
u64 size, capacity;
u8 bytes[];
-} vec_t;
-
-#define VEC_GET(P) (((vec_t *)(P)) - 1)
-#define VEC_SIZE(P) (VEC_GET(P)->size)
-#define VEC_CAP(P) (VEC_GET(P)->capacity)
-#define VEC_MULT 2
-
-void vec_make(void **ptr, u64 size);
-void vec_free(void **data);
-void vec_ensure_remaining(void **ptr, u64 space);
-void vec_append_byte(void **ptr, u8 byte);
-void vec_append(void **ptr, void *data, u64 size);
-void vec_clone(void **dest, void **src);
+} ivec_t;
+
+#define IVEC_GET(P) (((ivec_t *)(P)) - 1)
+#define IVEC_SIZE(P) (IVEC_GET(P)->size)
+#define IVEC_CAP(P) (IVEC_GET(P)->capacity)
+#define IVEC_MULT 2
+
+void ivec_make(void **ptr, u64 size);
+void ivec_free(void **data);
+void ivec_ensure_remaining(void **ptr, u64 space);
+void ivec_append_byte(void **ptr, u8 byte);
+void ivec_append(void **ptr, void *data, u64 size);
+void ivec_clone(void **dest, void **src);
/// Symbol table
typedef struct
diff --git a/build.sh b/build.sh
index 878c28c..a5c8f0a 100644
--- a/build.sh
+++ b/build.sh
@@ -1,7 +1,7 @@
#!/usr/bin/env sh
CFLAGS="-Wall -Wextra -std=c11 -ggdb -fsanitize=address -fsanitize=undefined"
-SRC="vec.c symtable.c tag.c constructor.c sys.c main.c"
+SRC="ivec.c symtable.c tag.c constructor.c sys.c main.c"
OUT="alisp.out"
set -xe
diff --git a/constructor.c b/constructor.c
index a2669ee..a20e66a 100644
--- a/constructor.c
+++ b/constructor.c
@@ -35,7 +35,7 @@ lisp_t *cons(sys_t *sys, lisp_t *car, lisp_t *cdr)
lisp_t *make_vec(sys_t *sys, u64 capacity)
{
lvec_t *lvec = calloc(1, sizeof(*lvec));
- vec_make(&lvec->data, capacity);
+ ivec_make(&lvec->data, capacity);
lisp_t *ptr = tag_vec(lvec);
sys_register(sys, ptr);
return ptr;
diff --git a/ivec.c b/ivec.c
new file mode 100644
index 0000000..d780adc
--- /dev/null
+++ b/ivec.c
@@ -0,0 +1,76 @@
+/* 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-19
+ * Description: Inline Vector implementation
+ */
+
+#include <malloc.h>
+#include <string.h>
+
+#include "./alisp.h"
+
+void ivec_make(void **ptr, u64 size)
+{
+ if (!ptr)
+ return;
+ ivec_t *ivector = calloc(1, sizeof(*ivector) + size);
+ ivector->size = 0;
+ ivector->capacity = size;
+ *ptr = (ivector + 1);
+}
+
+void ivec_free(void **data)
+{
+ if (!data || !*data)
+ return;
+ free(IVEC_GET(*data));
+ *data = NULL;
+}
+
+void ivec_ensure_remaining(void **ptr, u64 space)
+{
+ if (!ptr || !*ptr)
+ return;
+ ivec_t *ivec = IVEC_GET(*ptr);
+ if (ivec->capacity - ivec->size < space)
+ {
+ void *new_ivec = NULL;
+ ivec_make(&new_ivec, MAX(ivec->capacity * IVEC_MULT, ivec->size + space));
+ IVEC_SIZE(new_ivec) = ivec->size;
+ memcpy(new_ivec, *ptr, ivec->size);
+ ivec_free(ptr);
+ *ptr = new_ivec;
+ }
+}
+
+void ivec_append_byte(void **ptr, u8 byte)
+{
+ ivec_ensure_remaining(ptr, 1);
+ ivec_t *ivec = IVEC_GET(*ptr);
+ ivec->bytes[ivec->size++] = byte;
+}
+
+void ivec_append(void **ptr, void *data, u64 size)
+{
+ ivec_ensure_remaining(ptr, size);
+ ivec_t *ivec = IVEC_GET(*ptr);
+ memcpy(*ptr + ivec->size, data, size);
+ ivec->size += size;
+}
+
+void ivec_clone(void **dest, void **src)
+{
+ if (!dest || !src || !*src)
+ return;
+ ivec_make(dest, IVEC_SIZE(*src));
+ memcpy(*dest, *src, IVEC_SIZE(*src));
+ IVEC_SIZE(*dest) = IVEC_SIZE(*src);
+}
diff --git a/symtable.c b/symtable.c
index e704a27..1358b5e 100644
--- a/symtable.c
+++ b/symtable.c
@@ -29,7 +29,8 @@ void sym_table_init(sym_table_t *table)
{
table->capacity = MAX(table->capacity, SYM_TABLE_INIT_SIZE);
table->count = 0;
- vec_make((void **)&table->entries, table->capacity * sizeof(*table->entries));
+ ivec_make((void **)&table->entries,
+ table->capacity * sizeof(*table->entries));
}
sv_t *sym_table_find(sym_table_t *table, sv_t sv)
@@ -61,6 +62,6 @@ void sym_table_cleanup(sym_table_t *table)
if (table->entries[i].data)
free(table->entries[i].data);
// kill the container
- vec_free((void **)&table->entries);
+ ivec_free((void **)&table->entries);
memset(table, 0, sizeof(*table));
}
diff --git a/sys.c b/sys.c
index d56c152..195b2a1 100644
--- a/sys.c
+++ b/sys.c
@@ -58,7 +58,7 @@ void sys_cleanup(sys_t *sys)
case TAG_VEC:
{
lvec_t *lvec = as_vec(allocated);
- vec_free(&lvec->data);
+ ivec_free(&lvec->data);
free(lvec);
break;
}
diff --git a/vec.c b/vec.c
deleted file mode 100644
index 811fe9a..0000000
--- a/vec.c
+++ /dev/null
@@ -1,76 +0,0 @@
-/* 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-19
- * Description: Vector implementation
- */
-
-#include <malloc.h>
-#include <string.h>
-
-#include "./alisp.h"
-
-void vec_make(void **ptr, u64 size)
-{
- if (!ptr)
- return;
- vec_t *vector = calloc(1, sizeof(*vector) + size);
- vector->size = 0;
- vector->capacity = size;
- *ptr = (vector + 1);
-}
-
-void vec_free(void **data)
-{
- if (!data || !*data)
- return;
- free(VEC_GET(*data));
- *data = NULL;
-}
-
-void vec_ensure_remaining(void **ptr, u64 space)
-{
- if (!ptr || !*ptr)
- return;
- vec_t *vec = VEC_GET(*ptr);
- if (vec->capacity - vec->size < space)
- {
- void *new_vec = NULL;
- vec_make(&new_vec, MAX(vec->capacity * VEC_MULT, vec->size + space));
- VEC_SIZE(new_vec) = vec->size;
- memcpy(new_vec, *ptr, vec->size);
- vec_free(ptr);
- *ptr = new_vec;
- }
-}
-
-void vec_append_byte(void **ptr, u8 byte)
-{
- vec_ensure_remaining(ptr, 1);
- vec_t *vec = VEC_GET(*ptr);
- vec->bytes[vec->size++] = byte;
-}
-
-void vec_append(void **ptr, void *data, u64 size)
-{
- vec_ensure_remaining(ptr, size);
- vec_t *vec = VEC_GET(*ptr);
- memcpy(*ptr + vec->size, data, size);
- vec->size += size;
-}
-
-void vec_clone(void **dest, void **src)
-{
- if (!dest || !src || !*src)
- return;
- vec_make(dest, VEC_SIZE(*src));
- memcpy(*dest, *src, VEC_SIZE(*src));
- VEC_SIZE(*dest) = VEC_SIZE(*src);
-}