From 3c0af94c5c4300d7eff14f482ec9f86262a20809 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Wed, 28 Feb 2024 18:49:33 -0500 Subject: [PATCH] Use automatic allocation for patches --- include/link/section.hpp | 5 ++--- src/link/object.cpp | 17 ++++++----------- src/link/patch.cpp | 2 +- src/link/sdas_obj.cpp | 12 ++++-------- src/link/section.cpp | 2 +- 5 files changed, 14 insertions(+), 24 deletions(-) diff --git a/include/link/section.hpp b/include/link/section.hpp index cdcec6af..00094a73 100644 --- a/include/link/section.hpp +++ b/include/link/section.hpp @@ -21,12 +21,11 @@ struct Patch { struct FileStackNode const *src; uint32_t lineNo; uint32_t offset; + struct Section const *pcSection; uint32_t pcSectionID; uint32_t pcOffset; enum PatchType type; std::vector rpnExpression; - - struct Section const *pcSection; }; struct Section { @@ -46,7 +45,7 @@ struct Section { uint16_t alignMask; uint16_t alignOfs; std::vector *data; // Array of size `size` - std::vector *patches; + std::vector patches; // Extra info computed during linking std::vector *fileSymbols; std::vector *symbols; diff --git a/src/link/object.cpp b/src/link/object.cpp index da1e5f68..91c6c1d0 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -337,12 +337,9 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam "%s: Cannot read \"%s\"'s number of patches: %s", fileName, section->name->c_str()); - section->patches = new(std::nothrow) std::vector(); - if (!section->patches) - err("%s: Unable to read \"%s\"'s patches", fileName, section->name->c_str()); - section->patches->resize(nbPatches); + section->patches.resize(nbPatches); for (uint32_t i = 0; i < nbPatches; i++) - readPatch(file, &(*section->patches)[i], fileName, section->name->c_str(), + readPatch(file, §ion->patches[i], fileName, section->name->c_str(), i, fileNodes); } else { section->data = NULL; // `mergeSections()` expects to be able to always read the ptr @@ -492,7 +489,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) verbosePrint("Reading %" PRIu32 " sections...\n", nbSections); for (uint32_t i = 0; i < nbSections; i++) { // Read section - fileSections[i] = (struct Section *)malloc(sizeof(*fileSections[i])); + fileSections[i] = new(std::nothrow) struct Section(); if (!fileSections[i]) err("%s: Failed to create new section", fileName); @@ -510,7 +507,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) // Give patches' PC section pointers to their sections for (uint32_t i = 0; i < nbSections; i++) { if (sect_HasData(fileSections[i]->type)) { - for (struct Patch &patch : *fileSections[i]->patches) + for (struct Patch &patch : fileSections[i]->patches) linkPatchToPCSect(&patch, fileSections); } } @@ -573,12 +570,10 @@ static void freeSection(struct Section *section) struct Section *next = section->nextu; delete section->name; - if (sect_HasData(section->type)) { + if (sect_HasData(section->type)) delete section->data; - delete section->patches; - } delete section->symbols; - free(section); + delete section; section = next; } while (section); diff --git a/src/link/patch.cpp b/src/link/patch.cpp index b2b1a2c0..5c2389bc 100644 --- a/src/link/patch.cpp +++ b/src/link/patch.cpp @@ -452,7 +452,7 @@ void patch_CheckAssertions(std::deque &assertions) static void applyFilePatches(struct Section *section, struct Section *dataSection) { verbosePrint("Patching section \"%s\"...\n", section->name->c_str()); - for (struct Patch &patch : *section->patches) { + for (struct Patch &patch : section->patches) { int32_t value = computeRPNExpr(&patch, *section->fileSymbols); uint16_t offset = patch.offset + section->offset; diff --git a/src/link/sdas_obj.cpp b/src/link/sdas_obj.cpp index 7d49f01b..cefa598d 100644 --- a/src/link/sdas_obj.cpp +++ b/src/link/sdas_obj.cpp @@ -255,7 +255,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectorisAlignFixed = false; // No such concept! // The array will be allocated if the section does contain data curSection->data = NULL; - curSection->patches = new(std::nothrow) std::vector(); - if (!curSection->patches) - fatal(where, lineNo, "Failed to alloc new area's patches: %s", - strerror(errno)); curSection->fileSymbols = &fileSymbols; // IDs are instead per-section curSection->symbols = new(std::nothrow) std::vector(); if (!curSection->symbols) @@ -536,13 +532,13 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectorpatches->emplace_back(); + struct Patch &patch = section->patches.emplace_back(); patch.lineNo = lineNo; patch.src = where; patch.offset = offset - writtenOfs + *writeIndex; - if (section->patches->size() > 1) { - uint32_t prevOffset = (*section->patches)[section->patches->size() - 2].offset; + if (section->patches.size() > 1) { + uint32_t prevOffset = section->patches[section->patches.size() - 2].offset; if (prevOffset>= patch.offset) fatal(where, lineNo, "Relocs not sorted by offset are not supported (%" PRIu32 " >= %" PRIu32 ")", prevOffset, patch.offset); diff --git a/src/link/section.cpp b/src/link/section.cpp index 8bc52ceb..606e28c2 100644 --- a/src/link/section.cpp +++ b/src/link/section.cpp @@ -153,7 +153,7 @@ static void mergeSections(struct Section *target, struct Section *other, enum Se other->data = NULL; // Prevent a double `delete` } // Adjust patches' PC offsets - for (struct Patch &patch : *other->patches) + for (struct Patch &patch : other->patches) patch.pcOffset += other->offset; } else if (target->data) { assert(other->size == 0);