vec -> ivec
I'm going to implement a normal stable vector instead of an inline vector so we have both options for use. I think that's smarter than just sticking to one.
This commit is contained in:
26
alisp.h
26
alisp.h
@@ -46,25 +46,25 @@ typedef struct
|
|||||||
|
|
||||||
sv_t sv_copy(sv_t old);
|
sv_t sv_copy(sv_t old);
|
||||||
|
|
||||||
/// Dynamic arrays
|
/// Inlined Dynamic arrays
|
||||||
|
|
||||||
typedef struct
|
typedef struct InlineVector
|
||||||
{
|
{
|
||||||
u64 size, capacity;
|
u64 size, capacity;
|
||||||
u8 bytes[];
|
u8 bytes[];
|
||||||
} vec_t;
|
} ivec_t;
|
||||||
|
|
||||||
#define VEC_GET(P) (((vec_t *)(P)) - 1)
|
#define IVEC_GET(P) (((ivec_t *)(P)) - 1)
|
||||||
#define VEC_SIZE(P) (VEC_GET(P)->size)
|
#define IVEC_SIZE(P) (IVEC_GET(P)->size)
|
||||||
#define VEC_CAP(P) (VEC_GET(P)->capacity)
|
#define IVEC_CAP(P) (IVEC_GET(P)->capacity)
|
||||||
#define VEC_MULT 2
|
#define IVEC_MULT 2
|
||||||
|
|
||||||
void vec_make(void **ptr, u64 size);
|
void ivec_make(void **ptr, u64 size);
|
||||||
void vec_free(void **data);
|
void ivec_free(void **data);
|
||||||
void vec_ensure_remaining(void **ptr, u64 space);
|
void ivec_ensure_remaining(void **ptr, u64 space);
|
||||||
void vec_append_byte(void **ptr, u8 byte);
|
void ivec_append_byte(void **ptr, u8 byte);
|
||||||
void vec_append(void **ptr, void *data, u64 size);
|
void ivec_append(void **ptr, void *data, u64 size);
|
||||||
void vec_clone(void **dest, void **src);
|
void ivec_clone(void **dest, void **src);
|
||||||
|
|
||||||
/// Symbol table
|
/// Symbol table
|
||||||
typedef struct
|
typedef struct
|
||||||
|
|||||||
2
build.sh
2
build.sh
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env sh
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
CFLAGS="-Wall -Wextra -std=c11 -ggdb -fsanitize=address -fsanitize=undefined"
|
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"
|
OUT="alisp.out"
|
||||||
|
|
||||||
set -xe
|
set -xe
|
||||||
|
|||||||
@@ -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)
|
lisp_t *make_vec(sys_t *sys, u64 capacity)
|
||||||
{
|
{
|
||||||
lvec_t *lvec = calloc(1, sizeof(*lvec));
|
lvec_t *lvec = calloc(1, sizeof(*lvec));
|
||||||
vec_make(&lvec->data, capacity);
|
ivec_make(&lvec->data, capacity);
|
||||||
lisp_t *ptr = tag_vec(lvec);
|
lisp_t *ptr = tag_vec(lvec);
|
||||||
sys_register(sys, ptr);
|
sys_register(sys, ptr);
|
||||||
return ptr;
|
return ptr;
|
||||||
|
|||||||
76
ivec.c
Normal file
76
ivec.c
Normal file
@@ -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);
|
||||||
|
}
|
||||||
@@ -29,7 +29,8 @@ void sym_table_init(sym_table_t *table)
|
|||||||
{
|
{
|
||||||
table->capacity = MAX(table->capacity, SYM_TABLE_INIT_SIZE);
|
table->capacity = MAX(table->capacity, SYM_TABLE_INIT_SIZE);
|
||||||
table->count = 0;
|
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)
|
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)
|
if (table->entries[i].data)
|
||||||
free(table->entries[i].data);
|
free(table->entries[i].data);
|
||||||
// kill the container
|
// kill the container
|
||||||
vec_free((void **)&table->entries);
|
ivec_free((void **)&table->entries);
|
||||||
memset(table, 0, sizeof(*table));
|
memset(table, 0, sizeof(*table));
|
||||||
}
|
}
|
||||||
|
|||||||
2
sys.c
2
sys.c
@@ -58,7 +58,7 @@ void sys_cleanup(sys_t *sys)
|
|||||||
case TAG_VEC:
|
case TAG_VEC:
|
||||||
{
|
{
|
||||||
lvec_t *lvec = as_vec(allocated);
|
lvec_t *lvec = as_vec(allocated);
|
||||||
vec_free(&lvec->data);
|
ivec_free(&lvec->data);
|
||||||
free(lvec);
|
free(lvec);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
76
vec.c
76
vec.c
@@ -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);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user