Use std::vector for section data

This commit is contained in:
Rangi42
2024-02-27 14:54:00 -05:00
committed by Sylvie
parent 4cd88ade54
commit 52ac98c294
6 changed files with 19 additions and 32 deletions

View File

@@ -44,7 +44,7 @@ struct Section {
bool isAlignFixed;
uint16_t alignMask;
uint16_t alignOfs;
uint8_t *data; // Array of size `size`
std::vector<uint8_t> *data; // Array of size `size`
std::vector<struct Patch> *patches;
// Extra info computed during linking
std::vector<struct Symbol> *fileSymbols;

View File

@@ -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<uint8_t>(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;

View File

@@ -174,7 +174,7 @@ static void writeBank(std::deque<struct Section const *> *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++)

View File

@@ -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;
}
}

View File

@@ -479,7 +479,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
assert(!fileSections.empty()); // There should be at least one, from the above check
struct Section *section = fileSections[areaIdx].section;
uint16_t *writeIndex = &fileSections[areaIdx].writeIndex;
uint8_t writtenOfs = ADDR_SIZE; // Bytes before this have been written to ->data
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::vector<s
addr, *writeIndex);
if (!section->data) {
assert(section->size != 0);
section->data = (uint8_t *)malloc(section->size);
section->data = new(std::nothrow) std::vector<uint8_t>(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<s
fatal(where, lineNo, "'T' line writes past \"%s\"'s end (%u > %" PRIu16 ")",
section->name, *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(&(*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<s
if (*writeIndex + (nbBytes - writtenOfs) > section->size)
fatal(where, lineNo, "'T' line writes past \"%s\"'s end (%zu > %" PRIu16 ")",
section->name, *writeIndex + (nbBytes - writtenOfs), section->size);
memcpy(&section->data[*writeIndex], &data[writtenOfs], nbBytes - writtenOfs);
memcpy(&(*section->data)[*writeIndex], &data[writtenOfs], nbBytes - writtenOfs);
*writeIndex += nbBytes - writtenOfs;
}

View File

@@ -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)