Split alisp.h into several header files, in /include folder.
This commit is contained in:
@@ -7,7 +7,8 @@
|
||||
|
||||
#include <malloc.h>
|
||||
|
||||
#include "../alisp.h"
|
||||
#include <alisp/lisp.h>
|
||||
#include <alisp/tag.h>
|
||||
|
||||
lisp_t *make_int(i64 i)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../alisp.h"
|
||||
#include <alisp/stream.h>
|
||||
|
||||
stream_err_t stream_init_string(stream_t *stream, char *name, sv_t contents)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../alisp.h"
|
||||
#include <alisp/sv.h>
|
||||
|
||||
sv_t sv_copy(sv_t old)
|
||||
{
|
||||
|
||||
@@ -5,13 +5,17 @@
|
||||
* Commentary:
|
||||
*/
|
||||
|
||||
#include "../alisp.h"
|
||||
#include <alisp/symtable.h>
|
||||
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ENTRY_GET(TABLE, INDEX) (VEC_GET(&(TABLE)->entries, (INDEX), sv_t))
|
||||
|
||||
u64 djb2(sv_t string)
|
||||
{
|
||||
// magic number (see
|
||||
// https://stackoverflow.com/questions/10696223/reason-for-the-number-5381-in-the-djb-hash-function)
|
||||
u64 hash = 5381;
|
||||
for (u64 i = 0; i < string.size; ++i)
|
||||
hash = string.data[i] + (hash + (hash << 5));
|
||||
@@ -31,38 +35,36 @@ char *sym_table_find(sym_table_t *table, sv_t sv)
|
||||
if (table->entries.capacity == 0)
|
||||
sym_table_init(table);
|
||||
|
||||
// TODO: Deal with resizing this when table->count > table->size / 2
|
||||
// TODO: Consider resizing table when count >= capacity / 2
|
||||
u64 index = djb2(sv) & (table->capacity - 1);
|
||||
|
||||
for (sv_t comp = VEC_GET(&table->entries, index, sv_t); comp.data; index += 1,
|
||||
index = index & (table->capacity - 1),
|
||||
comp = VEC_GET(&table->entries, index, sv_t))
|
||||
// Is it present in the table?
|
||||
// FIXME: Since we don't implement resizing currently, this will infinite loop
|
||||
// when the table capacity SYM_TABLE_INIT_SIZE=(1 << 10)=1024 is filled.
|
||||
for (sv_t comp = ENTRY_GET(table, index); comp.data;
|
||||
index = (index + 1) & (table->capacity - 1),
|
||||
comp = ENTRY_GET(table, index))
|
||||
// If present in the table already, stop.
|
||||
if (sv.size == comp.size && strncmp(sv.data, comp.data, sv.size) == 0)
|
||||
break;
|
||||
return comp.data;
|
||||
|
||||
// we couldn't find it in our linear search (worst case scenario)
|
||||
if (!VEC_GET(&table->entries, index, sv_t).data)
|
||||
{
|
||||
sv_t newsv = sv_copy(sv);
|
||||
VEC_GET(&table->entries, index, sv_t) = newsv;
|
||||
++table->count;
|
||||
}
|
||||
|
||||
return VEC_GET(&table->entries, index, sv_t).data;
|
||||
// Worst case: not present in the table already. Since index is certainly
|
||||
// free (based on loop condition before) we can fill it.
|
||||
ENTRY_GET(table, index) = sv_copy(sv);
|
||||
++table->count;
|
||||
return ENTRY_GET(table, index).data;
|
||||
}
|
||||
|
||||
void sym_table_cleanup(sym_table_t *table)
|
||||
{
|
||||
// kill the data
|
||||
// Iterate through the strings and free each of them.
|
||||
sv_t current = {0};
|
||||
for (u64 i = 0; i < table->capacity; ++i)
|
||||
{
|
||||
current = VEC_GET(&table->entries, i, sv_t);
|
||||
current = ENTRY_GET(table, i);
|
||||
if (current.data)
|
||||
free(current.data);
|
||||
}
|
||||
// kill the container
|
||||
// Free the underlying container
|
||||
vec_free(&table->entries);
|
||||
memset(table, 0, sizeof(*table));
|
||||
}
|
||||
|
||||
@@ -5,22 +5,22 @@
|
||||
* Commentary:
|
||||
*/
|
||||
|
||||
#include "../alisp.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <alisp/lisp.h>
|
||||
#include <alisp/tag.h>
|
||||
|
||||
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->memory, &ptr, sizeof(&ptr));
|
||||
}
|
||||
|
||||
void sys_cleanup(sys_t *sys)
|
||||
@@ -28,13 +28,13 @@ void sys_cleanup(sys_t *sys)
|
||||
static_assert(NUM_TAGS == 5);
|
||||
|
||||
sym_table_cleanup(&sys->symtable);
|
||||
if (sys->conses.size == 0)
|
||||
if (sys->memory.size == 0)
|
||||
return;
|
||||
|
||||
// Iterate through each cons currently allocated and free them
|
||||
for (size_t i = 0; i < VEC_SIZE(&sys->conses, lisp_t **); ++i)
|
||||
// Iterate through each cell of memory currently allocated and free them
|
||||
for (size_t i = 0; i < VEC_SIZE(&sys->memory, lisp_t **); ++i)
|
||||
{
|
||||
lisp_t *allocated = VEC_GET(&sys->conses, i, lisp_t *);
|
||||
lisp_t *allocated = VEC_GET(&sys->memory, i, lisp_t *);
|
||||
switch (get_tag(allocated))
|
||||
{
|
||||
case TAG_CONS:
|
||||
@@ -58,7 +58,7 @@ void sys_cleanup(sys_t *sys)
|
||||
}
|
||||
|
||||
// Free the container
|
||||
vec_free(&sys->conses);
|
||||
vec_free(&sys->memory);
|
||||
|
||||
// Ensure no one treats this as active in any sense
|
||||
memset(sys, 0, sizeof(*sys));
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../alisp.h"
|
||||
#include <alisp/tag.h>
|
||||
|
||||
lisp_t *tag_int(i64 i)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../alisp.h"
|
||||
#include <alisp/vec.h>
|
||||
|
||||
void vec_init(vec_t *vec, u64 size)
|
||||
{
|
||||
@@ -47,6 +47,14 @@ void vec_ensure_free(vec_t *vec, u64 size)
|
||||
{
|
||||
if (!vec)
|
||||
return;
|
||||
if (vec->capacity == 0)
|
||||
{
|
||||
// We need to initialise this ourselves.
|
||||
vec->capacity = VEC_INLINE_CAPACITY;
|
||||
vec->size = 0;
|
||||
memset(vec->inlined, 0, sizeof(vec->inlined));
|
||||
}
|
||||
|
||||
if (vec->capacity - vec->size < size)
|
||||
{
|
||||
vec->capacity = MAX(vec->capacity * VEC_MULT, vec->size + size);
|
||||
|
||||
Reference in New Issue
Block a user