diff --git a/Makefile b/Makefile index 3bbcf8a1..62ccf886 100644 --- a/Makefile +++ b/Makefile @@ -69,6 +69,7 @@ rgbasm_obj := \ src/extern/err.o \ src/extern/getopt.o \ src/extern/utf8decoder.o \ + src/hashmap.o \ src/linkdefs.o src/asm/globlex.o src/asm/lexer.o src/asm/constexpr.o: src/asm/asmy.h diff --git a/include/asm/charmap.h b/include/asm/charmap.h index 857d1a30..08c12336 100644 --- a/include/asm/charmap.h +++ b/include/asm/charmap.h @@ -33,7 +33,6 @@ struct Charmap { struct Charnode nodes[MAXCHARNODES]; /* first node is reserved for the * root node in charmap. */ - struct Charmap *next; /* next charmap in hash table bucket */ }; void charmap_InitMain(void); diff --git a/src/asm/charmap.c b/src/asm/charmap.c index 31a0127f..31b82cc6 100644 --- a/src/asm/charmap.c +++ b/src/asm/charmap.c @@ -6,6 +6,7 @@ * SPDX-License-Identifier: MIT */ +#include #include #include #include @@ -19,6 +20,8 @@ #include "asm/util.h" #include "asm/warning.h" +#include "hashmap.h" + #define CHARMAP_HASH_SIZE (1 << 9) struct CharmapStackEntry { @@ -26,26 +29,16 @@ struct CharmapStackEntry { struct CharmapStackEntry *next; }; -static struct Charmap *tHashedCharmaps[CHARMAP_HASH_SIZE]; +static HashMap charmaps; static struct Charmap *mainCharmap; static struct Charmap *currentCharmap; struct CharmapStackEntry *charmapStack; -static uint32_t charmap_CalcHash(const char *s) +static inline struct Charmap *charmap_Get(const char *name) { - return calchash(s) % CHARMAP_HASH_SIZE; -} - -static struct Charmap **charmap_Get(const char *name) -{ - struct Charmap **ppCharmap = &tHashedCharmaps[charmap_CalcHash(name)]; - - while (*ppCharmap != NULL && strcmp((*ppCharmap)->name, name)) - ppCharmap = &(*ppCharmap)->next; - - return ppCharmap; + return hash_GetElement(charmaps, name); } static void CopyNode(struct Charmap *dest, @@ -62,56 +55,54 @@ static void CopyNode(struct Charmap *dest, struct Charmap *charmap_New(const char *name, const char *baseName) { - struct Charmap *pBase = NULL; + struct Charmap *base = NULL; if (baseName != NULL) { - struct Charmap **ppBase = charmap_Get(baseName); + base = charmap_Get(baseName); - if (*ppBase == NULL) + if (base == NULL) yyerror("Base charmap '%s' doesn't exist", baseName); - else - pBase = *ppBase; } - struct Charmap **ppCharmap = charmap_Get(name); + struct Charmap *charmap = charmap_Get(name); - if (*ppCharmap != NULL) { + if (charmap != NULL) { yyerror("Charmap '%s' already exists", name); return NULL; } - *ppCharmap = calloc(1, sizeof(struct Charmap)); + charmap = malloc(sizeof(*charmap)); + if (charmap == NULL) + fatalerror("Failed to create charmap: %s", strerror(errno)); - if (*ppCharmap == NULL) - fatalerror("Not enough memory for charmap"); - - struct Charmap *pCharmap = *ppCharmap; - - snprintf(pCharmap->name, sizeof(pCharmap->name), "%s", name); - - if (pBase != NULL) { - pCharmap->charCount = pBase->charCount; - pCharmap->nodeCount = pBase->nodeCount; + /* Init the new charmap's fields */ + snprintf(charmap->name, sizeof(charmap->name), "%s", name); + if (base != NULL) { + charmap->charCount = base->charCount; + charmap->nodeCount = base->nodeCount; for (int i = 0; i < MAXCHARNODES; i++) - CopyNode(pCharmap, pBase, i); + CopyNode(charmap, base, i); + } else { + charmap->charCount = 0; + charmap->nodeCount = 0; + memset(charmap->nodes, 0, sizeof(charmap->nodes)); } - currentCharmap = pCharmap; + hash_AddElement(charmaps, charmap->name, charmap); + currentCharmap = charmap; - return pCharmap; + return charmap; } void charmap_Set(const char *name) { - struct Charmap **ppCharmap = charmap_Get(name); + struct Charmap *charmap = charmap_Get(name); - if (*ppCharmap == NULL) { + if (charmap == NULL) yyerror("Charmap '%s' doesn't exist", name); - return; - } - - currentCharmap = *ppCharmap; + else + currentCharmap = charmap; } void charmap_Push(void)