sys: plug in allocator
This commit is contained in:
@@ -8,19 +8,13 @@
|
||||
#ifndef SYS_H
|
||||
#define SYS_H
|
||||
|
||||
#include <alisp/allocator.h>
|
||||
#include <alisp/lisp.h>
|
||||
|
||||
/// System context
|
||||
typedef struct
|
||||
{
|
||||
vec_t conses;
|
||||
vec_t vectors;
|
||||
u64 num_conses, num_vectors;
|
||||
} sys_mem_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
sys_mem_t memory;
|
||||
arena_t memory;
|
||||
sym_table_t symtable;
|
||||
} sys_t;
|
||||
|
||||
|
||||
44
src/sys.c
44
src/sys.c
@@ -20,21 +20,8 @@ lisp_t *sys_alloc(sys_t *sys, tag_t type)
|
||||
switch (type)
|
||||
{
|
||||
case TAG_CONS:
|
||||
{
|
||||
cons_t *cons = calloc(1, sizeof(*cons));
|
||||
lisp_t *lisp = tag_cons(cons);
|
||||
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->memory.vectors, &lisp, sizeof(&lisp));
|
||||
sys->memory.num_vectors++;
|
||||
return lisp;
|
||||
}
|
||||
return arena_make(&sys->memory, type);
|
||||
// Shouldn't be registered
|
||||
case TAG_NIL:
|
||||
case TAG_INT:
|
||||
@@ -47,38 +34,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 < sys->memory.num_vectors; ++i)
|
||||
{
|
||||
lisp_t *vec = VEC_GET(&sys->memory.vectors, i, lisp_t *);
|
||||
vec_capacity += as_vec(vec)->capacity;
|
||||
}
|
||||
return sym_table_cost(&sys->symtable) +
|
||||
(sys->memory.num_conses * sizeof(cons_t)) + vec_capacity;
|
||||
return arena_cost(&sys->memory) + sym_table_cost(&sys->symtable);
|
||||
}
|
||||
|
||||
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->memory.conses, lisp_t **); ++i)
|
||||
{
|
||||
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->memory.vectors, lisp_t **); ++i)
|
||||
{
|
||||
lisp_t *allocated = VEC_GET(&sys->memory.vectors, i, lisp_t *);
|
||||
lisp_free(allocated);
|
||||
}
|
||||
|
||||
// Free the containers
|
||||
vec_free(&sys->memory.conses);
|
||||
vec_free(&sys->memory.vectors);
|
||||
|
||||
arena_free(&sys->memory);
|
||||
// Ensure no one treats this as active in any sense
|
||||
memset(sys, 0, sizeof(*sys));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user