removed libtoml allocator
This commit is contained in:
parent
c49a8cb1e5
commit
52870920c1
@ -63,18 +63,6 @@ struct TomlValue {
|
||||
} value;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
void* (*malloc)(void* context, u64 size);
|
||||
void* (*realloc)(void* context, void* p, u64 size);
|
||||
void (*free)(void* context, void* p);
|
||||
} TomlAllocFuncs;
|
||||
|
||||
void toml_set_allocator(void* context, const TomlAllocFuncs *funcs);
|
||||
|
||||
void* toml_malloc(u64 size);
|
||||
void* toml_realloc(void* p, u64 size);
|
||||
void toml_free(void* p);
|
||||
|
||||
const TomlErr* toml_err(void);
|
||||
void toml_err_clear(void);
|
||||
|
||||
|
||||
85
src/toml.c
85
src/toml.c
@ -12,58 +12,6 @@
|
||||
|
||||
static ATTRIBUTE_THREAD_LOCAL TomlErr g_err = { TOML_OK, NULL };
|
||||
|
||||
static void* toml_default_malloc(void* context, u64 size)
|
||||
{
|
||||
(void)context;
|
||||
void* p = malloc(size);
|
||||
return p;
|
||||
}
|
||||
|
||||
static void* toml_default_realloc(void* context, void* p, u64 size)
|
||||
{
|
||||
(void)context;
|
||||
void* ptr = realloc(p, size);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static void toml_default_free(void* context, void* p)
|
||||
{
|
||||
(void)context;
|
||||
free(p);
|
||||
}
|
||||
|
||||
static TomlAllocFuncs g_default_alloc_funcs = {
|
||||
&toml_default_malloc,
|
||||
&toml_default_realloc,
|
||||
&toml_default_free
|
||||
};
|
||||
|
||||
static void* g_alloc_context = NULL;
|
||||
static const TomlAllocFuncs* g_alloc_funcs = &g_default_alloc_funcs;
|
||||
|
||||
void toml_set_allocator(void* context, const TomlAllocFuncs *funcs)
|
||||
{
|
||||
g_alloc_context = context;
|
||||
g_alloc_funcs = funcs;
|
||||
}
|
||||
|
||||
void* toml_malloc(u64 size)
|
||||
{
|
||||
return g_alloc_funcs->malloc(g_alloc_context, size);
|
||||
}
|
||||
|
||||
void* toml_realloc(void* p, u64 size)
|
||||
{
|
||||
return g_alloc_funcs->realloc(g_alloc_context, p, size);
|
||||
}
|
||||
|
||||
void toml_free(void* p)
|
||||
{
|
||||
if (p != NULL) {
|
||||
g_alloc_funcs->free(g_alloc_context, p);
|
||||
}
|
||||
}
|
||||
|
||||
const TomlErr* toml_err(void)
|
||||
{
|
||||
return &g_err;
|
||||
@ -71,7 +19,7 @@ const TomlErr* toml_err(void)
|
||||
|
||||
void toml_err_clear(void)
|
||||
{
|
||||
toml_free(g_err.message);
|
||||
free(g_err.message);
|
||||
g_err.code = TOML_OK;
|
||||
}
|
||||
|
||||
@ -96,7 +44,7 @@ static inline void toml_err_set(TomlErrCode code, cstr format, ...)
|
||||
|
||||
TomlTable* toml_table_new(void)
|
||||
{
|
||||
TomlTable* table = toml_malloc(sizeof(TomlTable));
|
||||
TomlTable* table = malloc(sizeof(TomlTable));
|
||||
HashMap_construct(table, TomlValue, (FreeFunction)TomlValue_destroy);
|
||||
return table;
|
||||
}
|
||||
@ -180,7 +128,7 @@ bool toml_table_get_bool(const TomlTable* self, str key)
|
||||
|
||||
TomlArray *toml_array_new(void)
|
||||
{
|
||||
TomlArray* array = toml_malloc(sizeof(TomlArray));
|
||||
TomlArray* array = malloc(sizeof(TomlArray));
|
||||
array->elements = NULL;
|
||||
array->len = 0;
|
||||
array->_capacity = 0;
|
||||
@ -189,20 +137,21 @@ TomlArray *toml_array_new(void)
|
||||
|
||||
void toml_array_free(TomlArray* self)
|
||||
{
|
||||
if (self != NULL) {
|
||||
if (self == NULL)
|
||||
return;
|
||||
|
||||
for (u64 i = 0; i < self->len; i++) {
|
||||
TomlValue_destroy(&self->elements[i]);
|
||||
}
|
||||
free(self->elements);
|
||||
free(self);
|
||||
}
|
||||
}
|
||||
|
||||
void toml_array_expand_if_necessary(TomlArray* self)
|
||||
{
|
||||
if (self->len + 1 > self->_capacity) {
|
||||
u64 new_capacity = self->_capacity > 0 ? self->_capacity * 2 : 8;
|
||||
void* p = toml_realloc(self->elements, sizeof(TomlValue) * new_capacity);
|
||||
void* p = realloc(self->elements, sizeof(TomlValue) * new_capacity);
|
||||
self->elements = p;
|
||||
self->_capacity = new_capacity;
|
||||
}
|
||||
@ -241,7 +190,7 @@ TomlValue TomlValue_new(TomlType type)
|
||||
value.value.b = false;
|
||||
break;
|
||||
case TOML_DATETIME:
|
||||
value.value.dt = (TomlDateTime*)toml_malloc(sizeof(TomlDateTime));
|
||||
value.value.dt = (TomlDateTime*)malloc(sizeof(TomlDateTime));
|
||||
memset(value.value.dt, 0, sizeof(TomlDateTime));
|
||||
break;
|
||||
}
|
||||
@ -303,7 +252,9 @@ TomlValue TomlValue_new_bool(bool b)
|
||||
|
||||
void TomlValue_destroy(TomlValue* self)
|
||||
{
|
||||
if (self != NULL) {
|
||||
if (self == NULL)
|
||||
return;
|
||||
|
||||
switch (self->type) {
|
||||
case TOML_STRING:
|
||||
str_destroy(self->value.s);
|
||||
@ -321,7 +272,6 @@ void TomlValue_destroy(TomlValue* self)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct _TomlParser {
|
||||
cstr begin;
|
||||
@ -329,29 +279,28 @@ typedef struct _TomlParser {
|
||||
cstr ptr;
|
||||
i32 lineno;
|
||||
i32 colno;
|
||||
char* filename;
|
||||
cstr filename;
|
||||
} TomlParser;
|
||||
|
||||
TomlParser* toml_parser_new(str s, cstr filename)
|
||||
{
|
||||
TomlParser* parser = toml_malloc(sizeof(TomlParser));
|
||||
TomlParser* parser = malloc(sizeof(TomlParser));
|
||||
parser->begin = s.data;
|
||||
parser->end = s.data + s.size;
|
||||
parser->ptr = s.data;
|
||||
parser->lineno = 1;
|
||||
parser->colno = 1;
|
||||
// TODO: remove copy filename?
|
||||
parser->filename = str_copy(str_from_cstr(filename)).data;
|
||||
parser->filename = filename;
|
||||
return parser;
|
||||
}
|
||||
|
||||
void toml_parser_free(TomlParser* self)
|
||||
{
|
||||
if (self != NULL) {
|
||||
free(self->filename);
|
||||
if (self == NULL)
|
||||
return;
|
||||
|
||||
free(self);
|
||||
}
|
||||
}
|
||||
|
||||
void toml_move_next(TomlParser* self)
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user