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