From d5de3fa1115c90470a848dc4e3a218935a14745a Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Wed, 21 Feb 2024 13:15:30 -0500 Subject: [PATCH] Use `std::deque` for symbol lists Also fix a memory leak that this reveals --- src/link/main.cpp | 8 ++++---- src/link/object.cpp | 42 ++++++++++++++++++------------------------ 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/link/main.cpp b/src/link/main.cpp index e74ac225..48a2c3cb 100644 --- a/src/link/main.cpp +++ b/src/link/main.cpp @@ -422,6 +422,10 @@ int main(int argc, char *argv[]) if (isDmgMode) sectionTypeInfo[SECTTYPE_VRAM].lastBank = 0; + // Do cleanup before quitting, though. + // Mostly here to please tools such as `valgrind` so actual errors can be seen + atexit(obj_Cleanup); + // Read all object files first, for (obj_Setup(argc - curArgIndex); curArgIndex < argc; curArgIndex++) obj_ReadFile(argv[curArgIndex], argc - curArgIndex - 1); @@ -450,8 +454,4 @@ int main(int argc, char *argv[]) if (nbErrors != 0) reportErrors(); out_WriteFiles(); - - // Do cleanup before quitting, though. - // Mostly here to please tools such as `valgrind` so actual errors can be seen - obj_Cleanup(); } diff --git a/src/link/object.cpp b/src/link/object.cpp index b898a68d..b2e4d7a0 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: MIT */ +#include #include #include #include @@ -21,11 +22,12 @@ #include "linkdefs.hpp" #include "version.hpp" -static struct SymbolList { +struct SymbolList { size_t nbSymbols; struct Symbol **symbolList; - struct SymbolList *next; -} *symbolLists; +}; + +static std::deque symbolLists; unsigned int nbObjFiles; static struct FileStackNodes { @@ -472,7 +474,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) // object file. It's better than nothing. nodes[fileID].nbNodes = 1; nodes[fileID].nodes = - (struct FileStackNode *)malloc(sizeof(nodes[fileID].nodes[0]) * nodes[fileID].nbNodes); + (struct FileStackNode *)calloc(nodes[fileID].nbNodes, sizeof(nodes[fileID].nodes[0])); if (!nodes[fileID].nodes) err("Failed to get memory for %s's nodes", fileName); struct FileStackNode *where = &nodes[fileID].nodes[0]; @@ -530,14 +532,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) if (!fileSymbols) err("Failed to get memory for %s's symbols", fileName); - struct SymbolList *symbolList = (struct SymbolList *)malloc(sizeof(*symbolList)); - - if (!symbolList) - err("Failed to register %s's symbol list", fileName); - symbolList->symbolList = fileSymbols; - symbolList->nbSymbols = nbSymbols; - symbolList->next = symbolLists; - symbolLists = symbolList; + symbolLists.push_front({ .nbSymbols = nbSymbols, .symbolList = fileSymbols }); uint32_t *nbSymPerSect = (uint32_t *)calloc(nbSections ? nbSections : 1, sizeof(*nbSymPerSect)); @@ -653,15 +648,17 @@ void obj_Setup(unsigned int nbFiles) if (nbFiles > SIZE_MAX / sizeof(*nodes)) fatal(NULL, 0, "Impossible to link more than %zu files!", SIZE_MAX / sizeof(*nodes)); - nodes = (struct FileStackNodes *)malloc(sizeof(*nodes) * nbFiles); + nodes = (struct FileStackNodes *)calloc(nbFiles, sizeof(*nodes)); } static void freeNode(struct FileStackNode *node) { - if (node->type == NODE_REPT) - free(node->rept.iters); - else - free(node->name); + if (node) { + if (node->type == NODE_REPT) + free(node->rept.iters); + else + free(node->name); + } } static void freeSection(struct Section *section, void *) @@ -704,12 +701,9 @@ void obj_Cleanup(void) sect_ForEach(freeSection, NULL); sect_CleanupSections(); - for (struct SymbolList *list = symbolLists, *next; list; list = next) { - next = list->next; - - for (size_t i = 0; i < list->nbSymbols; i++) - freeSymbol(list->symbolList[i]); - free(list->symbolList); - free(list); + for (struct SymbolList &list : symbolLists) { + for (size_t i = 0; i < list.nbSymbols; i++) + freeSymbol(list.symbolList[i]); + free(list.symbolList); } }