From 52c80c2740cceb5ca84b52b71f11d72267b3b6fb Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Wed, 21 Feb 2024 09:48:55 -0500 Subject: [PATCH] Use `std::stack` for charmaps --- src/asm/charmap.cpp | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/src/asm/charmap.cpp b/src/asm/charmap.cpp index 356d0a18..9b442057 100644 --- a/src/asm/charmap.cpp +++ b/src/asm/charmap.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -37,12 +38,7 @@ static HashMap charmaps; // that gets reallocated. static struct Charmap **currentCharmap; -struct CharmapStackEntry { - struct Charmap **charmap; - struct CharmapStackEntry *next; -}; - -struct CharmapStackEntry *charmapStack; +std::stack charmapStack; static struct Charmap *charmap_Get(char const *name) { @@ -119,29 +115,18 @@ void charmap_Set(char const *name) void charmap_Push(void) { - struct CharmapStackEntry *stackEntry = (struct CharmapStackEntry *)malloc(sizeof(*stackEntry)); - - if (stackEntry == NULL) - fatalerror("Failed to alloc charmap stack entry: %s\n", strerror(errno)); - - stackEntry->charmap = currentCharmap; - stackEntry->next = charmapStack; - - charmapStack = stackEntry; + charmapStack.push(currentCharmap); } void charmap_Pop(void) { - if (charmapStack == NULL) { + if (charmapStack.empty()) { error("No entries in the charmap stack\n"); return; } - struct CharmapStackEntry *top = charmapStack; - - currentCharmap = top->charmap; - charmapStack = top->next; - free(top); + currentCharmap = charmapStack.top(); + charmapStack.pop(); } void charmap_Add(char *mapping, uint8_t value)