lisp: split memory into its own structure
This commit is contained in:
@@ -71,6 +71,12 @@ typedef struct
|
||||
{
|
||||
vec_t conses;
|
||||
vec_t vectors;
|
||||
u64 num_conses, num_vectors;
|
||||
} sys_mem_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
sys_mem_t memory;
|
||||
sym_table_t symtable;
|
||||
} sys_t;
|
||||
|
||||
|
||||
26
src/lisp.c
26
src/lisp.c
@@ -23,21 +23,22 @@ lisp_t *sys_alloc(sys_t *sys, tag_t type)
|
||||
{
|
||||
cons_t *cons = calloc(1, sizeof(*cons));
|
||||
lisp_t *lisp = tag_cons(cons);
|
||||
vec_append(&sys->conses, &lisp, sizeof(&lisp));
|
||||
vec_append(&sys->memory.conses, &lisp, sizeof(&lisp));
|
||||
sys->memory.num_conses++;
|
||||
return lisp;
|
||||
}
|
||||
case TAG_VEC:
|
||||
{
|
||||
vec_t *vec = calloc(1, sizeof(*vec));
|
||||
lisp_t *lisp = tag_vec(vec);
|
||||
vec_append(&sys->vectors, &lisp, sizeof(&lisp));
|
||||
vec_append(&sys->memory.vectors, &lisp, sizeof(&lisp));
|
||||
sys->memory.num_vectors++;
|
||||
return lisp;
|
||||
}
|
||||
// Shouldn't be registered
|
||||
case TAG_NIL:
|
||||
case TAG_INT:
|
||||
case TAG_SYM:
|
||||
case NUM_TAGS:
|
||||
default:
|
||||
FAIL("Unreachable");
|
||||
}
|
||||
@@ -47,12 +48,13 @@ lisp_t *sys_alloc(sys_t *sys, tag_t type)
|
||||
u64 sys_cost(sys_t *sys)
|
||||
{
|
||||
u64 vec_capacity = 0;
|
||||
for (u64 i = 0; i < VEC_SIZE(&sys->vectors, lisp_t *); ++i)
|
||||
for (u64 i = 0; i < sys->memory.num_vectors; ++i)
|
||||
{
|
||||
lisp_t *vec = VEC_GET(&sys->vectors, i, lisp_t *);
|
||||
lisp_t *vec = VEC_GET(&sys->memory.vectors, i, lisp_t *);
|
||||
vec_capacity += as_vec(vec)->capacity;
|
||||
}
|
||||
return sym_table_cost(&sys->symtable) + sys->conses.capacity + vec_capacity;
|
||||
return sym_table_cost(&sys->symtable) +
|
||||
(sys->memory.num_conses * sizeof(cons_t)) + vec_capacity;
|
||||
}
|
||||
|
||||
void sys_free(sys_t *sys)
|
||||
@@ -60,22 +62,22 @@ void sys_free(sys_t *sys)
|
||||
sym_table_free(&sys->symtable);
|
||||
|
||||
// Iterate through each cell of memory currently allocated and free them
|
||||
for (size_t i = 0; i < VEC_SIZE(&sys->conses, lisp_t **); ++i)
|
||||
for (size_t i = 0; i < VEC_SIZE(&sys->memory.conses, lisp_t **); ++i)
|
||||
{
|
||||
lisp_t *allocated = VEC_GET(&sys->conses, i, lisp_t *);
|
||||
lisp_t *allocated = VEC_GET(&sys->memory.conses, i, lisp_t *);
|
||||
lisp_free(allocated);
|
||||
}
|
||||
|
||||
// Iterate through each cell of memory currently allocated and free them
|
||||
for (size_t i = 0; i < VEC_SIZE(&sys->vectors, lisp_t **); ++i)
|
||||
for (size_t i = 0; i < VEC_SIZE(&sys->memory.vectors, lisp_t **); ++i)
|
||||
{
|
||||
lisp_t *allocated = VEC_GET(&sys->vectors, i, lisp_t *);
|
||||
lisp_t *allocated = VEC_GET(&sys->memory.vectors, i, lisp_t *);
|
||||
lisp_free(allocated);
|
||||
}
|
||||
|
||||
// Free the containers
|
||||
vec_free(&sys->conses);
|
||||
vec_free(&sys->vectors);
|
||||
vec_free(&sys->memory.conses);
|
||||
vec_free(&sys->memory.vectors);
|
||||
|
||||
// Ensure no one treats this as active in any sense
|
||||
memset(sys, 0, sizeof(*sys));
|
||||
|
||||
Reference in New Issue
Block a user