From e6165f3af8d2df949528478b72e621b3208f7421 Mon Sep 17 00:00:00 2001 From: biglyderv Date: Wed, 26 Mar 2025 01:20:38 -0400 Subject: [PATCH] garbage collection --- src/command.c | 18 ++++++++++-------- src/loader.c | 8 +++++--- src/main.c | 25 +++++++++++++++++-------- src/map.c | 10 +++++++++- src/map.h | 4 +++- src/writer.c | 0 6 files changed, 44 insertions(+), 21 deletions(-) delete mode 100644 src/writer.c diff --git a/src/command.c b/src/command.c index 36d64dd..2a8b217 100644 --- a/src/command.c +++ b/src/command.c @@ -74,17 +74,19 @@ int slash_command(char *command, hashmap *inv) { char *invs = "inv "; + int page; + if (strncmp(command, invs, strlen(invs)) == 0) { - int page = stoi(&command[strlen(invs)]); - printf("Your inventory (page %i):\n", page); - struct pager i = {.page = page - 1, .i = -1}; - hashmap_iterate(inv, inv_handler, &i); + page = stoi(&command[strlen(invs)]); } else if (strncmp(command, invs, strlen(invs) - 1) == 0) { - int page = 1; - printf("Your inventory (page %i):\n", page); - struct pager i = {.page = page - 1, .i = -1}; - hashmap_iterate(inv, inv_handler, &i); + page = 1; + } else { + return 0; } + + printf("Your inventory (page %i):\n", page); + struct pager i = {.page = page - 1, .i = -1}; + hashmap_iterate(inv, inv_handler, &i); return 1; } diff --git a/src/loader.c b/src/loader.c index 91a01ae..77e4ea7 100644 --- a/src/loader.c +++ b/src/loader.c @@ -20,13 +20,15 @@ int load_elements(hashmap *m, char *table, int use_inv) { while (1) { str = calloc(MAX_FILE_SIZE, sizeof(char)); - if (!fgets(str, MAX_FILE_SIZE, fptr)) + if (!fgets(str, MAX_FILE_SIZE, fptr)) { + free(str); break; + } did_something = 1; if (use_inv) { - hashmap_set(m, str, strlen(str) - 1, (uintptr_t)1); + hashmap_set(m, str, strlen(str) - 1, (uintptr_t)1, 0); continue; } @@ -34,7 +36,7 @@ int load_elements(hashmap *m, char *table, int use_inv) { combo[0] = '\0'; combo++; - hashmap_set(m, combo, strlen(combo) - 1, (uintptr_t)str); + hashmap_set(m, combo, strlen(combo) - 1, (uintptr_t)str, 1); } if (!did_something) { diff --git a/src/main.c b/src/main.c index a74ee7d..7ebb4d1 100644 --- a/src/main.c +++ b/src/main.c @@ -12,6 +12,20 @@ #define MAX_BUF_LENGTH 1024 #define MAX_COMBO_LENGTH 1024 +void init_tables(hashmap *elements, hashmap *inv, int do_inv) { + + load_elements(elements, "../elem_data/" combo_file, 0) || + load_elements(elements, combo_file, 0) || + load_elements(elements, "bin/" combo_file, 0); + + if (!do_inv) + return; + + load_elements(elements, "../elem_data/" inv_base_file, 1) || + load_elements(inv, inv_base_file, 1) || + load_elements(inv, "bin/" inv_base_file, 1); +} + int main(int argc, char *argv[]) { hashmap *elements = hashmap_create(); hashmap *inv = hashmap_create(); @@ -20,10 +34,7 @@ int main(int argc, char *argv[]) { char *command_re = calloc(MAX_BUF_LENGTH, sizeof(char)); char **sort_tmp = calloc(MAX_COMBO_LENGTH, sizeof(char **)); - load_elements(elements, combo_file, 0) || - load_elements(elements, "bin/" combo_file, 0); - load_elements(inv, inv_base_file, 1) || - load_elements(inv, "bin/" inv_base_file, 1); + init_tables(elements, inv, 1); load_elements(inv, inv_file, 1); @@ -56,10 +67,8 @@ int main(int argc, char *argv[]) { uintptr_t result; hashmap_get(elements, command_re, strlen(command_re), &result); - // todo: better if (result == 0) { - load_elements(elements, combo_file, 0) || - load_elements(elements, "bin/" combo_file, 0); + init_tables(elements, inv, 0); hashmap_get(elements, command_re, strlen(command_re), &result); } @@ -71,7 +80,7 @@ int main(int argc, char *argv[]) { char *res_str = (char *)result; - hashmap_set(inv, res_str, strlen(res_str), (uintptr_t)1); + hashmap_set(inv, res_str, strlen(res_str), (uintptr_t)1, 0); printf("You made %s!\n", res_str); FILE *fptr; diff --git a/src/map.c b/src/map.c index a93b2b4..59deeb2 100644 --- a/src/map.c +++ b/src/map.c @@ -5,6 +5,7 @@ // #include "map.h" +#include #include #include @@ -167,7 +168,8 @@ static struct bucket *find_entry(hashmap *m, const void *key, size_t ksize, } } -int hashmap_set(hashmap *m, const void *key, size_t ksize, uintptr_t val) { +int hashmap_set(hashmap *m, const void *key, size_t ksize, uintptr_t val, + int free_old) { if (m->count + 1 > HASHMAP_MAX_LOAD * m->capacity) { if (hashmap_resize(m) == -1) return -1; @@ -186,6 +188,12 @@ int hashmap_set(hashmap *m, const void *key, size_t ksize, uintptr_t val) { entry->ksize = ksize; entry->hash = hash; } + + if (free_old && entry->value != 0 && entry->value != val && + strcmp((char *)entry->value, (char *)val) == 0) { + free((char *)val); + return 0; + } entry->value = val; return 0; } diff --git a/src/map.h b/src/map.h index b6543a9..775b62d 100644 --- a/src/map.h +++ b/src/map.h @@ -2,6 +2,7 @@ // map.h // // Created by Mashpoe on 1/15/21. +// Modifications by BiglyDerv // #ifndef map_h @@ -63,7 +64,8 @@ void hashmap_free(hashmap *map); // you must copy it yourself if you want to guarantee its lifetime, // or if you intend to call `hashmap_key_free`. // returns -1 on error. -int hashmap_set(hashmap *map, const void *key, size_t ksize, uintptr_t value); +int hashmap_set(hashmap *map, const void *key, size_t ksize, uintptr_t value, + int free_old); // adds an entry if it doesn't exist, using the value of `*out_in`. // if it does exist, it sets value in `*out_in`, meaning the value diff --git a/src/writer.c b/src/writer.c deleted file mode 100644 index e69de29..0000000