From 6ec0108566b235fd7e61e072a11196d79c866a3f Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Thu, 22 Jan 2026 16:37:58 +0000 Subject: [PATCH] vec: is_inlined -> not_inlined If you allocate a vector on the stack and default initialise it (i.e. {0}), then by default "is_inlined" = 0. This is bad, as technically speaking we should expect the vector to attempt to use it's small inline buffer for vector operations before going onto the heap - this will mess up our functions. So here I adjust the name to make default stack based allocation a bit easier. --- alisp.h | 2 +- runtime/sys.c | 3 ++- runtime/vec.c | 20 ++++++++++---------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/alisp.h b/alisp.h index 78a0377..49e03ec 100644 --- a/alisp.h +++ b/alisp.h @@ -66,7 +66,7 @@ typedef struct Vector { u64 size, capacity; // Small buffer optimisation - u8 is_inlined; + u8 not_inlined; union { void *ptr; diff --git a/runtime/sys.c b/runtime/sys.c index bd7148d..3b63bc5 100644 --- a/runtime/sys.c +++ b/runtime/sys.c @@ -21,12 +21,13 @@ void sys_init(sys_t *sys) { memset(sys, 0, sizeof(*sys)); + vec_init(&sys->conses, 0); } void sys_register(sys_t *sys, lisp_t *ptr) { // Simply append it to the list of currently active conses - vec_append(&sys->conses, ptr, sizeof(ptr)); + vec_append(&sys->conses, &ptr, sizeof(&ptr)); } void sys_cleanup(sys_t *sys) diff --git a/runtime/vec.c b/runtime/vec.c index 644d904..c1d2228 100644 --- a/runtime/vec.c +++ b/runtime/vec.c @@ -25,15 +25,15 @@ void vec_init(vec_t *vec, u64 size) return; else if (size <= VEC_INLINE_CAPACITY) { - vec->is_inlined = 1; - vec->capacity = VEC_INLINE_CAPACITY; - vec->ptr = NULL; + vec->not_inlined = 0; + vec->capacity = VEC_INLINE_CAPACITY; + vec->ptr = NULL; } else { - vec->is_inlined = 0; - vec->capacity = size; - vec->ptr = calloc(1, vec->capacity); + vec->not_inlined = 1; + vec->capacity = size; + vec->ptr = calloc(1, vec->capacity); } } @@ -41,14 +41,14 @@ void vec_free(vec_t *vec) { if (!vec) return; - if (!vec->is_inlined && vec->ptr) + if (vec->not_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; + return vec->not_inlined ? vec->ptr : vec->inlined; } void vec_ensure_free(vec_t *vec, u64 size) @@ -58,7 +58,7 @@ void vec_ensure_free(vec_t *vec, u64 size) if (vec->capacity - vec->size < size) { vec->capacity = MAX(vec->capacity * VEC_MULT, vec->size + size); - if (vec->is_inlined) + if (!vec->not_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. @@ -69,7 +69,7 @@ void vec_ensure_free(vec_t *vec, u64 size) memcpy(buffer, vec->inlined, vec->size); vec->ptr = calloc(1, vec->capacity); memcpy(vec->ptr, buffer, vec->size); - vec->is_inlined = 0; + vec->not_inlined = 1; } else vec->ptr = realloc(vec->ptr, vec->capacity);