shithub: rgbds

Download patch

ref: 7ddbe44b21fdb7e62048776884c9ecbb8ec9de1f
parent: af60e7f74a08e54ebe8ddf24f64a0323519cab91
author: ISSOtm <eldredhabert0@gmail.com>
date: Sat Mar 14 21:09:21 EDT 2020

Use hashmap system for charmap

--- a/Makefile
+++ b/Makefile
@@ -69,6 +69,7 @@
 	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
--- a/include/asm/charmap.h
+++ b/include/asm/charmap.h
@@ -33,7 +33,6 @@
 	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);
--- a/src/asm/charmap.c
+++ b/src/asm/charmap.c
@@ -6,6 +6,7 @@
  * SPDX-License-Identifier: MIT
  */
 
+#include <errno.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -19,6 +20,8 @@
 #include "asm/util.h"
 #include "asm/warning.h"
 
+#include "hashmap.h"
+
 #define CHARMAP_HASH_SIZE (1 << 9)
 
 struct CharmapStackEntry {
@@ -26,7 +29,7 @@
 	struct CharmapStackEntry *next;
 };
 
-static struct Charmap *tHashedCharmaps[CHARMAP_HASH_SIZE];
+static HashMap charmaps;
 
 static struct Charmap *mainCharmap;
 static struct Charmap *currentCharmap;
@@ -33,21 +36,11 @@
 
 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;
+	return hash_GetElement(charmaps, name);
 }
 
-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;
-}
-
 static void CopyNode(struct Charmap *dest,
 		     const struct Charmap *src,
 		     int nodeIdx)
@@ -62,56 +55,54 @@
 
 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");
+	/* 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;
 
-	struct Charmap *pCharmap = *ppCharmap;
-
-	snprintf(pCharmap->name, sizeof(pCharmap->name), "%s", name);
-
-	if (pBase != NULL) {
-		pCharmap->charCount = pBase->charCount;
-		pCharmap->nodeCount = pBase->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)