Use std::deque for symbol lists

Also fix a memory leak that this reveals
This commit is contained in:
Rangi42
2024-02-21 13:15:30 -05:00
committed by Sylvie
parent bc8cb754c0
commit d5de3fa111
2 changed files with 22 additions and 28 deletions

View File

@@ -422,6 +422,10 @@ int main(int argc, char *argv[])
if (isDmgMode) if (isDmgMode)
sectionTypeInfo[SECTTYPE_VRAM].lastBank = 0; 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, // Read all object files first,
for (obj_Setup(argc - curArgIndex); curArgIndex < argc; curArgIndex++) for (obj_Setup(argc - curArgIndex); curArgIndex < argc; curArgIndex++)
obj_ReadFile(argv[curArgIndex], argc - curArgIndex - 1); obj_ReadFile(argv[curArgIndex], argc - curArgIndex - 1);
@@ -450,8 +454,4 @@ int main(int argc, char *argv[])
if (nbErrors != 0) if (nbErrors != 0)
reportErrors(); reportErrors();
out_WriteFiles(); out_WriteFiles();
// Do cleanup before quitting, though.
// Mostly here to please tools such as `valgrind` so actual errors can be seen
obj_Cleanup();
} }

View File

@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: MIT */ /* SPDX-License-Identifier: MIT */
#include <deque>
#include <errno.h> #include <errno.h>
#include <inttypes.h> #include <inttypes.h>
#include <limits.h> #include <limits.h>
@@ -21,11 +22,12 @@
#include "linkdefs.hpp" #include "linkdefs.hpp"
#include "version.hpp" #include "version.hpp"
static struct SymbolList { struct SymbolList {
size_t nbSymbols; size_t nbSymbols;
struct Symbol **symbolList; struct Symbol **symbolList;
struct SymbolList *next; };
} *symbolLists;
static std::deque<struct SymbolList> symbolLists;
unsigned int nbObjFiles; unsigned int nbObjFiles;
static struct FileStackNodes { static struct FileStackNodes {
@@ -472,7 +474,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
// object file. It's better than nothing. // object file. It's better than nothing.
nodes[fileID].nbNodes = 1; nodes[fileID].nbNodes = 1;
nodes[fileID].nodes = 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) if (!nodes[fileID].nodes)
err("Failed to get memory for %s's nodes", fileName); err("Failed to get memory for %s's nodes", fileName);
struct FileStackNode *where = &nodes[fileID].nodes[0]; struct FileStackNode *where = &nodes[fileID].nodes[0];
@@ -530,14 +532,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
if (!fileSymbols) if (!fileSymbols)
err("Failed to get memory for %s's symbols", fileName); err("Failed to get memory for %s's symbols", fileName);
struct SymbolList *symbolList = (struct SymbolList *)malloc(sizeof(*symbolList)); symbolLists.push_front({ .nbSymbols = nbSymbols, .symbolList = fileSymbols });
if (!symbolList)
err("Failed to register %s's symbol list", fileName);
symbolList->symbolList = fileSymbols;
symbolList->nbSymbols = nbSymbols;
symbolList->next = symbolLists;
symbolLists = symbolList;
uint32_t *nbSymPerSect = (uint32_t *)calloc(nbSections ? nbSections : 1, uint32_t *nbSymPerSect = (uint32_t *)calloc(nbSections ? nbSections : 1,
sizeof(*nbSymPerSect)); sizeof(*nbSymPerSect));
@@ -653,16 +648,18 @@ void obj_Setup(unsigned int nbFiles)
if (nbFiles > SIZE_MAX / sizeof(*nodes)) if (nbFiles > SIZE_MAX / sizeof(*nodes))
fatal(NULL, 0, "Impossible to link more than %zu files!", 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) static void freeNode(struct FileStackNode *node)
{ {
if (node) {
if (node->type == NODE_REPT) if (node->type == NODE_REPT)
free(node->rept.iters); free(node->rept.iters);
else else
free(node->name); free(node->name);
} }
}
static void freeSection(struct Section *section, void *) static void freeSection(struct Section *section, void *)
{ {
@@ -704,12 +701,9 @@ void obj_Cleanup(void)
sect_ForEach(freeSection, NULL); sect_ForEach(freeSection, NULL);
sect_CleanupSections(); sect_CleanupSections();
for (struct SymbolList *list = symbolLists, *next; list; list = next) { for (struct SymbolList &list : symbolLists) {
next = list->next; for (size_t i = 0; i < list.nbSymbols; i++)
freeSymbol(list.symbolList[i]);
for (size_t i = 0; i < list->nbSymbols; i++) free(list.symbolList);
freeSymbol(list->symbolList[i]);
free(list->symbolList);
free(list);
} }
} }