Make conses a vector, add VEC_SIZE macro
Easier to iterate through, O(1) amortized registering just like the Linked List. VEC_SIZE just makes it easier to iterate through a vector of specially typed elements.
This commit is contained in:
5
alisp.h
5
alisp.h
@@ -76,7 +76,8 @@ typedef struct Vector
|
|||||||
|
|
||||||
static_assert(sizeof(vec_t) == 64, "vec_t has to be 64 bytes as part of SBO");
|
static_assert(sizeof(vec_t) == 64, "vec_t has to be 64 bytes as part of SBO");
|
||||||
|
|
||||||
#define VEC_GET(V, T) ((T *)vec_data(V))
|
#define VEC_GET(V, T) ((T *)vec_data(V))
|
||||||
|
#define VEC_SIZE(V, T) ((V)->size / (sizeof(T)))
|
||||||
|
|
||||||
void vec_init(vec_t *, u64);
|
void vec_init(vec_t *, u64);
|
||||||
void vec_free(vec_t *);
|
void vec_free(vec_t *);
|
||||||
@@ -183,7 +184,7 @@ typedef struct
|
|||||||
/// System context
|
/// System context
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
lisp_t *conses;
|
vec_t conses;
|
||||||
sym_table_t symtable;
|
sym_table_t symtable;
|
||||||
} sys_t;
|
} sys_t;
|
||||||
|
|
||||||
|
|||||||
@@ -25,11 +25,8 @@ void sys_init(sys_t *sys)
|
|||||||
|
|
||||||
void sys_register(sys_t *sys, lisp_t *ptr)
|
void sys_register(sys_t *sys, lisp_t *ptr)
|
||||||
{
|
{
|
||||||
// Generate an unmanaged cons
|
// Simply append it to the list of currently active conses
|
||||||
cons_t *cons = calloc(1, sizeof(*cons));
|
vec_append(&sys->conses, ptr, sizeof(ptr));
|
||||||
cons->car = ptr;
|
|
||||||
cons->cdr = sys->conses;
|
|
||||||
sys->conses = tag_cons(cons);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sys_cleanup(sys_t *sys)
|
void sys_cleanup(sys_t *sys)
|
||||||
@@ -37,18 +34,13 @@ void sys_cleanup(sys_t *sys)
|
|||||||
static_assert(NUM_TAGS == 5);
|
static_assert(NUM_TAGS == 5);
|
||||||
|
|
||||||
sym_table_cleanup(&sys->symtable);
|
sym_table_cleanup(&sys->symtable);
|
||||||
|
if (sys->conses.size == 0)
|
||||||
if (!sys->conses)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Iterate through each element of memory
|
// Iterate through each cons currently allocated and free them
|
||||||
for (lisp_t *cell = sys->conses, *next = cdr(cell); cell;
|
for (size_t i = 0; i < VEC_SIZE(&sys->conses, lisp_t **); ++i)
|
||||||
cell = next, next = cdr(next))
|
|
||||||
{
|
{
|
||||||
// Only reason allocated exists is because we had to allocate memory on the
|
lisp_t *allocated = VEC_GET(&sys->conses, lisp_t *)[i];
|
||||||
// heap for it. It's therefore enough to deal with only the allocated
|
|
||||||
// types.
|
|
||||||
lisp_t *allocated = car(cell);
|
|
||||||
switch (get_tag(allocated))
|
switch (get_tag(allocated))
|
||||||
{
|
{
|
||||||
case TAG_CONS:
|
case TAG_CONS:
|
||||||
@@ -69,9 +61,11 @@ void sys_cleanup(sys_t *sys)
|
|||||||
// shouldn't be dealt with (either constant or dealt with elsewhere)
|
// shouldn't be dealt with (either constant or dealt with elsewhere)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then free the current cell
|
|
||||||
free(as_cons(cell));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Free the container
|
||||||
|
vec_free(&sys->conses);
|
||||||
|
|
||||||
|
// Ensure no one treats this as active in any sense
|
||||||
memset(sys, 0, sizeof(*sys));
|
memset(sys, 0, sizeof(*sys));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user