From 52ac98c294fc2f2eeadfde52ef660eedd3a09273 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Tue, 27 Feb 2024 14:54:00 -0500 Subject: [PATCH] Use `std::vector` for section data --- include/link/section.hpp | 2 +- src/link/object.cpp | 26 +++++++++----------------- src/link/output.cpp | 2 +- src/link/patch.cpp | 4 ++-- src/link/sdas_obj.cpp | 8 ++++---- src/link/section.cpp | 9 ++------- 6 files changed, 19 insertions(+), 32 deletions(-) diff --git a/include/link/section.hpp b/include/link/section.hpp index 3fbf6b7c..c56edf65 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; - uint8_t *data; // Array of size `size` + std::vector *data; // Array of size `size` std::vector *patches; // Extra info computed during linking std::vector *fileSymbols; diff --git a/src/link/object.cpp b/src/link/object.cpp index d68b91ae..4d9c6f40 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -268,8 +268,7 @@ static void readPatch(FILE *file, struct Patch *patch, char const *fileName, cha fileName, sectName, i); patch->rpnExpression.resize(rpnSize); - size_t nbElementsRead = fread(&patch->rpnExpression[0], sizeof(patch->rpnExpression[0]), - rpnSize, file); + size_t nbElementsRead = fread(&patch->rpnExpression[0], 1, rpnSize, file); if (nbElementsRead != rpnSize) errx("%s: Cannot read \"%s\"'s patch #%" PRIu32 "'s RPN expression: %s", @@ -346,22 +345,15 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam section->alignOfs = tmp; if (sect_HasData(section->type)) { - // Ensure we never allocate 0 bytes - uint8_t *data = (uint8_t *)malloc(sizeof(*data) * section->size + 1); - - if (!data) - err("%s: Unable to read \"%s\"'s data", fileName, - section->name); + section->data = new(std::nothrow) std::vector(section->size); + if (!section->data) + err("%s: Unable to read \"%s\"'s data", fileName, section->name); if (section->size) { - size_t nbElementsRead = fread(data, sizeof(*data), - section->size, file); - if (nbElementsRead != section->size) - errx("%s: Cannot read \"%s\"'s data: %s", - fileName, section->name, - feof(file) ? "Unexpected end of file" - : strerror(errno)); + if (size_t nbRead = fread(&(*section->data)[0], 1, section->size, file); + nbRead != section->size) + errx("%s: Cannot read \"%s\"'s data: %s", fileName, section->name, + feof(file) ? "Unexpected end of file" : strerror(errno)); } - section->data = data; uint32_t nbPatches; @@ -617,7 +609,7 @@ static void freeSection(struct Section *section) free(section->name); if (sect_HasData(section->type)) { - free(section->data); + delete section->data; delete section->patches; } delete section->symbols; diff --git a/src/link/output.cpp b/src/link/output.cpp index c8cdf1f6..54372567 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, sizeof(*section->data), section->size, outputFile); + fwrite(&(*section->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 39f3883b..484b88fa 100644 --- a/src/link/patch.cpp +++ b/src/link/patch.cpp @@ -467,7 +467,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 { @@ -487,7 +487,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 c735f0a3..d5ba351e 100644 --- a/src/link/sdas_obj.cpp +++ b/src/link/sdas_obj.cpp @@ -479,7 +479,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectordata + uint8_t writtenOfs = ADDR_SIZE; // Bytes before this have been written to `->data` uint16_t addr = data[0] | data[1] << 8; if (section->isAddressFixed) { @@ -496,7 +496,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectordata) { assert(section->size != 0); - section->data = (uint8_t *)malloc(section->size); + section->data = new(std::nothrow) std::vector(section->size); if (!section->data) fatal(where, lineNo, "Failed to alloc data for \"%s\": %s", section->name, strerror(errno)); @@ -661,7 +661,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector %" PRIu16 ")", section->name, *writeIndex + (offset - writtenOfs), section->size); // Copy all bytes up to those (plus the byte that we'll overwrite) - memcpy(§ion->data[*writeIndex], &data[writtenOfs], offset - writtenOfs + 1); + memcpy(&(*section->data)[*writeIndex], &data[writtenOfs], offset - writtenOfs + 1); *writeIndex += offset - writtenOfs + 1; writtenOfs = offset + 3; // Skip all three `baseValue` bytes, though } @@ -709,7 +709,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, *writeIndex + (nbBytes - writtenOfs), section->size); - memcpy(§ion->data[*writeIndex], &data[writtenOfs], nbBytes - writtenOfs); + memcpy(&(*section->data)[*writeIndex], &data[writtenOfs], nbBytes - writtenOfs); *writeIndex += nbBytes - writtenOfs; } diff --git a/src/link/section.cpp b/src/link/section.cpp index cf3d24ff..2960e921 100644 --- a/src/link/section.cpp +++ b/src/link/section.cpp @@ -148,16 +148,11 @@ static void mergeSections(struct Section *target, struct Section *other, enum Se // `data` pointer, or a size of 0. if (other->data) { if (target->data) { - // Ensure we're not allocating 0 bytes - target->data = (uint8_t *)realloc(target->data, - sizeof(*target->data) * target->size + 1); - if (!target->data) - errx("Failed to concatenate \"%s\"'s fragments", target->name); - memcpy(&target->data[other->offset], other->data, other->size); + 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 free() + other->data = NULL; // Prevent a double `delete` } // Adjust patches' PC offsets for (struct Patch &patch : *other->patches)