From 96f354026a65932997fdb3c3ce17c8aefabeb630 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Wed, 28 Feb 2024 20:54:33 -0500 Subject: [PATCH] Use automatic allocation for section data --- include/link/section.hpp | 2 +- src/link/object.cpp | 19 +++++-------------- src/link/output.cpp | 2 +- src/link/patch.cpp | 4 ++-- src/link/sdas_obj.cpp | 14 ++++---------- src/link/section.cpp | 15 +++------------ 6 files changed, 16 insertions(+), 40 deletions(-) diff --git a/include/link/section.hpp b/include/link/section.hpp index 8d4da7bf..c354c930 100644 --- a/include/link/section.hpp +++ b/include/link/section.hpp @@ -44,7 +44,7 @@ struct Section { bool isAlignFixed; uint16_t alignMask; uint16_t alignOfs; - std::vector *data; // Array of size `size` + std::vector data; // Array of size `size`, or 0 if `type` does not have data std::vector patches; // Extra info computed during linking std::vector *fileSymbols; diff --git a/src/link/object.cpp b/src/link/object.cpp index 264d337f..aec2e737 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -304,11 +304,9 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam section->alignOfs = tmp; if (sect_HasData(section->type)) { - section->data = new(std::nothrow) std::vector(section->size); - if (!section->data) - err("%s: Unable to read \"%s\"'s data", fileName, section->name.c_str()); if (section->size) { - if (size_t nbRead = fread(&(*section->data)[0], 1, section->size, file); + section->data.resize(section->size); + if (size_t nbRead = fread(§ion->data[0], 1, section->size, file); nbRead != section->size) errx("%s: Cannot read \"%s\"'s data: %s", fileName, section->name.c_str(), feof(file) ? "Unexpected end of file" : strerror(errno)); @@ -324,8 +322,6 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam for (uint32_t i = 0; i < nbPatches; i++) readPatch(file, §ion->patches[i], fileName, section->name, i, fileNodes); - } else { - section->data = NULL; // `mergeSections()` expects to be able to always read the ptr } } @@ -546,15 +542,10 @@ void obj_Setup(unsigned int nbFiles) static void freeSection(struct Section *section) { - do { - struct Section *next = section->nextu; - - if (sect_HasData(section->type)) - delete section->data; + for (struct Section *next; section; section = next) { + next = section->nextu; delete section; - - section = next; - } while (section); + }; } void obj_Cleanup(void) diff --git a/src/link/output.cpp b/src/link/output.cpp index 5df18d30..fdcc66be 100644 --- a/src/link/output.cpp +++ b/src/link/output.cpp @@ -174,7 +174,7 @@ static void writeBank(std::deque *bankSections, uint16_t } // Output the section itself - fwrite(&(*section->data)[0], 1, section->size, outputFile); + fwrite(§ion->data[0], 1, section->size, outputFile); if (overlayFile) { // Skip bytes even with pipes for (uint16_t i = 0; i < section->size; i++) diff --git a/src/link/patch.cpp b/src/link/patch.cpp index 0ba328c5..5cd6e819 100644 --- a/src/link/patch.cpp +++ b/src/link/patch.cpp @@ -466,7 +466,7 @@ static void applyFilePatches(struct Section *section, struct Section *dataSectio error(patch.src, patch.lineNo, "jr target out of reach (expected -129 < %" PRId16 " < 128)", jumpOffset); - (*dataSection->data)[offset] = jumpOffset & 0xFF; + dataSection->data[offset] = jumpOffset & 0xFF; } else { // Patch a certain number of bytes struct { @@ -486,7 +486,7 @@ static void applyFilePatches(struct Section *section, struct Section *dataSectio value, value < 0 ? " (maybe negative?)" : "", types[patch.type].size * 8U); for (uint8_t i = 0; i < types[patch.type].size; i++) { - (*dataSection->data)[offset + i] = value & 0xFF; + dataSection->data[offset + i] = value & 0xFF; value >>= 8; } } diff --git a/src/link/sdas_obj.cpp b/src/link/sdas_obj.cpp index b78e0682..776084a2 100644 --- a/src/link/sdas_obj.cpp +++ b/src/link/sdas_obj.cpp @@ -337,8 +337,6 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectortype = SECTTYPE_INVALID; // This means "indeterminate" } curSection->isAlignFixed = false; // No such concept! - // The array will be allocated if the section does contain data - curSection->data = NULL; curSection->fileSymbols = &fileSymbols; // IDs are instead per-section curSection->nextu = NULL; break; @@ -469,12 +467,9 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectordata) { + if (section->data.empty()) { assert(section->size != 0); - section->data = new(std::nothrow) std::vector(section->size); - if (!section->data) - fatal(where, lineNo, "Failed to alloc data for \"%s\": %s", - section->name.c_str(), strerror(errno)); + section->data.resize(section->size); } } @@ -637,7 +632,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector %" PRIu16 ")", section->name.c_str(), *writeIndex + (offset - writtenOfs), section->size); // Copy all bytes up to those (plus the byte that we'll overwrite) - memcpy(&(*section->data)[*writeIndex], &data[writtenOfs], offset - writtenOfs + 1); + memcpy(§ion->data[*writeIndex], &data[writtenOfs], offset - writtenOfs + 1); *writeIndex += offset - writtenOfs + 1; writtenOfs = offset + 3; // Skip all three `baseValue` bytes, though } @@ -685,7 +680,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector section->size) fatal(where, lineNo, "'T' line writes past \"%s\"'s end (%zu > %" PRIu16 ")", section->name.c_str(), *writeIndex + (nbBytes - writtenOfs), section->size); - memcpy(&(*section->data)[*writeIndex], &data[writtenOfs], nbBytes - writtenOfs); + memcpy(§ion->data[*writeIndex], &data[writtenOfs], nbBytes - writtenOfs); *writeIndex += nbBytes - writtenOfs; } @@ -719,7 +714,6 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectorname.c_str(), entry.writeIndex, section->size); - // This must be done last, so that `->data` is not NULL anymore sect_AddSection(section); if (section->modifier == SECTION_FRAGMENT) { diff --git a/src/link/section.cpp b/src/link/section.cpp index 336350c3..8a7333cc 100644 --- a/src/link/section.cpp +++ b/src/link/section.cpp @@ -141,21 +141,12 @@ static void mergeSections(struct Section *target, struct Section *other, enum Se other->offset = target->size; target->size += other->size; // Normally we'd check that `sect_HasData`, but SDCC areas may be `_INVALID` here - // Note that if either fragment has data (= a non-NULL `data` pointer), then it's - // assumed that both fragments "have data", and thus should either have a non-NULL - // `data` pointer, or a size of 0. - if (other->data) { - if (target->data) { - target->data->insert(target->data->end(), RANGE(*other->data)); - } else { - assert(target->size == other->size); // It has been increased just above - target->data = other->data; - other->data = NULL; // Prevent a double `delete` - } + if (!other->data.empty()) { + target->data.insert(target->data.end(), RANGE(other->data)); // Adjust patches' PC offsets for (struct Patch &patch : other->patches) patch.pcOffset += other->offset; - } else if (target->data) { + } else if (!target->data.empty()) { assert(other->size == 0); } break;