garbage collection
This commit is contained in:
parent
c02e8aa59a
commit
e6165f3af8
6 changed files with 44 additions and 21 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
25
src/main.c
25
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;
|
||||
|
|
10
src/map.c
10
src/map.c
|
@ -5,6 +5,7 @@
|
|||
//
|
||||
|
||||
#include "map.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue