From c92c369d2828143182f9223646073dfb6bc1e421 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Sun, 19 Apr 2015 04:20:54 +0000 Subject: [PATCH] Cleanup config memory --- conf.c | 37 ++++++++++++++++++++++--------------- main.c | 1 + 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/conf.c b/conf.c index 889bda6..ff55b13 100644 --- a/conf.c +++ b/conf.c @@ -13,6 +13,7 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF */ +#define _GNU_SOURCE #include #include #include @@ -42,31 +43,36 @@ static char **conf_argv; static char conf_path[256]; /* Helpers */ -static int conf_cmp(const void *_a, const void *_b) +static int entry_cmp(const void *_a, const void *_b) { const entry_t *a = _a; const entry_t *b = _b; return strcmp(a->key, b->key); } -static entry_t *conf_get(const char *key) +static entry_t *entry_get(const char *key) { entry_t try = { .key = (char*)key }; - entry_t **found = tfind(&try, &conf, conf_cmp); + entry_t **found = tfind(&try, &conf, entry_cmp); return found ? *found : NULL; } -static void conf_set(const char *key, int num, const char *str) +static void entry_free(entry_t *entry) +{ + free(entry->key); + if (entry->type == STRING) + free(entry->str); + free(entry); +} + +static void entry_set(const char *key, int num, const char *str) { entry_t *entry; /* Free old item */ - if ((entry = conf_get(key))) { - tdelete(entry, &conf, conf_cmp); - free(entry->key); - if (entry->type == STRING) - free(entry->str); - free(entry); + if ((entry = entry_get(key))) { + tdelete(entry, &conf, entry_cmp); + entry_free(entry); } /* Insert new item */ @@ -81,7 +87,7 @@ static void conf_set(const char *key, int num, const char *str) entry->num = num; //printf("set_num: %s = %d\n", key, num); } - tsearch(entry, &conf, conf_cmp); + tsearch(entry, &conf, entry_cmp); } static char *strtrim(char *str) @@ -225,26 +231,26 @@ static void load_args(int argc, char **argv) /* Configuration file functions */ int conf_get_int(const char *key, int def) { - entry_t *entry = conf_get(key); + entry_t *entry = entry_get(key); return entry && entry->type == NUMBER ? entry->num : def; } void conf_set_int(const char *key, int value) { - conf_set(key, value, NULL); + entry_set(key, value, NULL); } const char *conf_get_str(const char *key, const char *def) { - entry_t *entry = conf_get(key); + entry_t *entry = entry_get(key); return entry && entry->type == STRING ? entry->str : def; } void conf_set_str(const char *key, const char *value) { - conf_set(key, 0, value); + entry_set(key, 0, value); } void conf_reload(void) @@ -265,4 +271,5 @@ void conf_init(int argc, char **argv) void conf_free(void) { + tdestroy(conf, (void(*)(void*))entry_free); } diff --git a/main.c b/main.c index 58150d3..327066e 100644 --- a/main.c +++ b/main.c @@ -41,5 +41,6 @@ int main(int argc, char **argv) wm_free(); sys_free(); + conf_free(); return 0; } -- 2.43.2