(libretro-db) Avoid callocs when possible

This commit is contained in:
twinaphex 2020-06-26 20:24:05 +02:00
parent 1ae20ea17c
commit 5e3b353987
4 changed files with 45 additions and 20 deletions

View File

@ -46,26 +46,30 @@ struct bintree
/* TODO/FIXME - static global variable */
static void *NIL_NODE = &NIL_NODE;
static struct bintree_node *bintree_new_nil_node(struct bintree_node *parent)
static INLINE int bintree_is_nil(const struct bintree_node *node)
{
return !node || (node->value == NIL_NODE);
}
static struct bintree_node *bintree_new_nil_node(
struct bintree_node *parent)
{
struct bintree_node *node = (struct bintree_node *)
calloc(1, sizeof(struct bintree_node));
malloc(sizeof(struct bintree_node));
if (!node)
return NULL;
node->value = NIL_NODE;
node->parent = parent;
node->left = NULL;
node->right = NULL;
return node;
}
static INLINE int bintree_is_nil(const struct bintree_node *node)
{
return !node || (node->value == NIL_NODE);
}
static int bintree_insert_internal(bintree_t *t, struct bintree_node *root, void *value)
static int bintree_insert_internal(bintree_t *t,
struct bintree_node *root, void *value)
{
int cmp_res = 0;
@ -135,7 +139,7 @@ int bintree_iterate(const bintree_t *t, bintree_iter_cb cb,
bintree_t *bintree_new(bintree_cmp_func cmp, void *ctx)
{
bintree_t *t = (bintree_t*)calloc(1, sizeof(*t));
bintree_t *t = (bintree_t*)malloc(sizeof(*t));
if (!t)
return NULL;
@ -149,5 +153,7 @@ bintree_t *bintree_new(bintree_cmp_func cmp, void *ctx)
void bintree_free(bintree_t *t)
{
if (!t)
return;
bintree_free_node(t->root);
}

View File

@ -590,11 +590,17 @@ clean:
libretrodb_cursor_t *libretrodb_cursor_new(void)
{
libretrodb_cursor_t *dbc = (libretrodb_cursor_t*)
calloc(1, sizeof(*dbc));
malloc(sizeof(*dbc));
if (!dbc)
return NULL;
dbc->is_valid = 0;
dbc->fd = NULL;
dbc->eof = 0;
dbc->query = NULL;
dbc->db = NULL;
return dbc;
}
@ -608,11 +614,17 @@ void libretrodb_cursor_free(libretrodb_cursor_t *dbc)
libretrodb_t *libretrodb_new(void)
{
libretrodb_t *db = (libretrodb_t*)calloc(1, sizeof(*db));
libretrodb_t *db = (libretrodb_t*)malloc(sizeof(*db));
if (!db)
return NULL;
db->fd = NULL;
db->root = 0;
db->count = 0;
db->first_index_offset = 0;
db->path = NULL;
return db;
}

View File

@ -31,11 +31,14 @@
int main(int argc, char ** argv)
{
int rv;
libretrodb_t *db;
libretrodb_cursor_t *cur;
libretrodb_query_t *q;
struct rmsgpack_dom_value item;
const char *command, *path, *query_exp, *error;
const char *command = NULL;
const char *path = NULL;
const char *query_exp = NULL;
const char *error = NULL;
libretrodb_t *db = NULL;
libretrodb_cursor_t *cur = NULL;
libretrodb_query_t *q = NULL;
if (argc < 3)
{
@ -51,8 +54,8 @@ int main(int argc, char ** argv)
command = argv[2];
path = argv[1];
db = libretrodb_new();
cur = libretrodb_cursor_new();
db = libretrodb_new();
cur = libretrodb_cursor_new();
if (!db || !cur)
goto error;

View File

@ -923,18 +923,22 @@ void *libretrodb_query_compile(libretrodb_t *db,
const char *query, size_t buff_len, const char **error_string)
{
struct buffer buff;
struct query *q = (struct query*)calloc(1, sizeof(*q));
struct query *q = (struct query*)malloc(sizeof(*q));
if (!q)
goto error;
return NULL;
q->ref_count = 1;
q->root.argc = 0;
q->root.func = NULL;
q->root.argv = NULL;
buff.data = query;
buff.len = buff_len;
buff.offset = 0;
*error_string = NULL;
buff = query_chomp(buff);
buff = query_chomp(buff);
if (query_peek(buff, "{"))
{