From 4a7d333891c69bb77fa7eecced1c3ec924961e18 Mon Sep 17 00:00:00 2001 From: Sylvie <35663410+Rangi42@users.noreply.github.com> Date: Sat, 9 Mar 2024 11:12:01 -0500 Subject: [PATCH] Use `std::unique_ptr` for rgblink sections (#1337) --- include/link/section.hpp | 5 ++- src/link/assign.cpp | 2 +- src/link/main.cpp | 17 -------- src/link/object.cpp | 85 +++++++++++++++++++++------------------- src/link/output.cpp | 6 +-- src/link/patch.cpp | 2 +- src/link/script.y | 2 +- src/link/sdas_obj.cpp | 24 ++++++------ src/link/section.cpp | 73 +++++++++++++++++----------------- 9 files changed, 102 insertions(+), 114 deletions(-) diff --git a/include/link/section.hpp b/include/link/section.hpp index e134743a..5e817e62 100644 --- a/include/link/section.hpp +++ b/include/link/section.hpp @@ -6,6 +6,7 @@ // GUIDELINE: external code MUST NOT BE AWARE of the data structure used! +#include #include #include #include @@ -50,7 +51,7 @@ struct Section { // Extra info computed during linking std::vector *fileSymbols; std::vector symbols; - Section *nextu; // The next "component" of this unionized sect + std::unique_ptr
nextu; // The next "component" of this unionized sect }; /* @@ -64,7 +65,7 @@ void sect_ForEach(void (*callback)(Section &)); * Registers a section to be processed. * @param section The section to register. */ -void sect_AddSection(Section §ion); +void sect_AddSection(std::unique_ptr
&§ion); /* * Finds a section by its name. diff --git a/src/link/assign.cpp b/src/link/assign.cpp index b72fa3e7..2006c502 100644 --- a/src/link/assign.cpp +++ b/src/link/assign.cpp @@ -57,7 +57,7 @@ static void initFreeSpace() { static void assignSection(Section §ion, MemoryLocation const &location) { // Propagate the assigned location to all UNIONs/FRAGMENTs // so `jr` patches in them will have the correct offset - for (Section *next = §ion; next != nullptr; next = next->nextu) { + for (Section *next = §ion; next != nullptr; next = next->nextu.get()) { next->org = location.address; next->bank = location.bank; } diff --git a/src/link/main.cpp b/src/link/main.cpp index 1d3353bf..4f0d733c 100644 --- a/src/link/main.cpp +++ b/src/link/main.cpp @@ -346,19 +346,6 @@ next: exit(1); } -static void freeSection(Section §ion) { - Section *next = §ion; - - for (Section *nextu; next; next = nextu) { - nextu = next->nextu; - delete next; - }; -} - -static void freeSections() { - sect_ForEach(freeSection); -} - int main(int argc, char *argv[]) { // Parse options for (int ch; (ch = musl_getopt_long_only(argc, argv, optstring, longopts, nullptr)) != -1;) { @@ -462,10 +449,6 @@ 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(freeSections); - // Read all object files first, for (obj_Setup(argc - curArgIndex); curArgIndex < argc; curArgIndex++) obj_ReadFile(argv[curArgIndex], argc - curArgIndex - 1); diff --git a/src/link/object.cpp b/src/link/object.cpp index afb6045d..7b752999 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include @@ -329,8 +329,10 @@ static void readPatch( * Sets a patch's pcSection from its pcSectionID. * @param patch The patch to fix */ -static void linkPatchToPCSect(Patch &patch, std::vector
const &fileSections) { - patch.pcSection = patch.pcSectionID != (uint32_t)-1 ? fileSections[patch.pcSectionID] : nullptr; +static void + linkPatchToPCSect(Patch &patch, std::vector> const &fileSections) { + patch.pcSection = + patch.pcSectionID != (uint32_t)-1 ? fileSections[patch.pcSectionID].get() : nullptr; } /* @@ -469,10 +471,6 @@ static void readAssertion( tryReadstring(assert.message, file, "%s: Cannot read assertion's message: %s", fileName); } -static Section *getMainSection(Section §ion) { - return section.modifier != SECTION_NORMAL ? sect_GetSection(section.name) : §ion; -} - void obj_ReadFile(char const *fileName, unsigned int fileID) { FILE *file; @@ -571,47 +569,16 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) { } // This file's sections, stored in a table to link symbols to them - std::vector
fileSections(nbSections, nullptr); + std::vector> fileSections(nbSections); verbosePrint("Reading %" PRIu32 " sections...\n", nbSections); for (uint32_t i = 0; i < nbSections; i++) { // Read section - fileSections[i] = new (std::nothrow) Section(); - if (!fileSections[i]) - err("%s: Failed to create new section", fileName); - + fileSections[i] = std::make_unique
(); fileSections[i]->nextu = nullptr; readSection(file, *fileSections[i], fileName, nodes[fileID]); fileSections[i]->fileSymbols = &fileSymbols; fileSections[i]->symbols.reserve(nbSymPerSect[i]); - - sect_AddSection(*fileSections[i]); - } - - // Give patches' PC section pointers to their sections - for (uint32_t i = 0; i < nbSections; i++) { - if (sect_HasData(fileSections[i]->type)) { - for (Patch &patch : fileSections[i]->patches) - linkPatchToPCSect(patch, fileSections); - } - } - - // Give symbols' section pointers to their sections - for (uint32_t i = 0; i < nbSymbols; i++) { - if (Label *label = std::get_if