From 5e3b3539872ca8682b7f7a5d7e39a22e4b667e69 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 26 Jun 2020 20:24:05 +0200 Subject: [PATCH] (libretro-db) Avoid callocs when possible --- libretro-db/bintree.c | 24 +++++++++++++++--------- libretro-db/libretrodb.c | 16 ++++++++++++++-- libretro-db/libretrodb_tool.c | 15 +++++++++------ libretro-db/query.c | 10 +++++++--- 4 files changed, 45 insertions(+), 20 deletions(-) diff --git a/libretro-db/bintree.c b/libretro-db/bintree.c index b15b369bd1..fa3509c581 100644 --- a/libretro-db/bintree.c +++ b/libretro-db/bintree.c @@ -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); } diff --git a/libretro-db/libretrodb.c b/libretro-db/libretrodb.c index 4976bdcbf2..31709bbe55 100644 --- a/libretro-db/libretrodb.c +++ b/libretro-db/libretrodb.c @@ -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; } diff --git a/libretro-db/libretrodb_tool.c b/libretro-db/libretrodb_tool.c index ee3c1ad85e..050d7ea229 100644 --- a/libretro-db/libretrodb_tool.c +++ b/libretro-db/libretrodb_tool.c @@ -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; diff --git a/libretro-db/query.c b/libretro-db/query.c index 9cdf5dc529..6b829873c6 100644 --- a/libretro-db/query.c +++ b/libretro-db/query.c @@ -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, "{")) {