mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-21 10:42:07 +00:00
Use std::vector for section data
This commit is contained in:
@@ -44,7 +44,7 @@ struct Section {
|
|||||||
bool isAlignFixed;
|
bool isAlignFixed;
|
||||||
uint16_t alignMask;
|
uint16_t alignMask;
|
||||||
uint16_t alignOfs;
|
uint16_t alignOfs;
|
||||||
uint8_t *data; // Array of size `size`
|
std::vector<uint8_t> *data; // Array of size `size`
|
||||||
std::vector<struct Patch> *patches;
|
std::vector<struct Patch> *patches;
|
||||||
// Extra info computed during linking
|
// Extra info computed during linking
|
||||||
std::vector<struct Symbol> *fileSymbols;
|
std::vector<struct Symbol> *fileSymbols;
|
||||||
|
|||||||
@@ -268,8 +268,7 @@ static void readPatch(FILE *file, struct Patch *patch, char const *fileName, cha
|
|||||||
fileName, sectName, i);
|
fileName, sectName, i);
|
||||||
|
|
||||||
patch->rpnExpression.resize(rpnSize);
|
patch->rpnExpression.resize(rpnSize);
|
||||||
size_t nbElementsRead = fread(&patch->rpnExpression[0], sizeof(patch->rpnExpression[0]),
|
size_t nbElementsRead = fread(&patch->rpnExpression[0], 1, rpnSize, file);
|
||||||
rpnSize, file);
|
|
||||||
|
|
||||||
if (nbElementsRead != rpnSize)
|
if (nbElementsRead != rpnSize)
|
||||||
errx("%s: Cannot read \"%s\"'s patch #%" PRIu32 "'s RPN expression: %s",
|
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;
|
section->alignOfs = tmp;
|
||||||
|
|
||||||
if (sect_HasData(section->type)) {
|
if (sect_HasData(section->type)) {
|
||||||
// Ensure we never allocate 0 bytes
|
section->data = new(std::nothrow) std::vector<uint8_t>(section->size);
|
||||||
uint8_t *data = (uint8_t *)malloc(sizeof(*data) * section->size + 1);
|
if (!section->data)
|
||||||
|
err("%s: Unable to read \"%s\"'s data", fileName, section->name);
|
||||||
if (!data)
|
|
||||||
err("%s: Unable to read \"%s\"'s data", fileName,
|
|
||||||
section->name);
|
|
||||||
if (section->size) {
|
if (section->size) {
|
||||||
size_t nbElementsRead = fread(data, sizeof(*data),
|
if (size_t nbRead = fread(&(*section->data)[0], 1, section->size, file);
|
||||||
section->size, file);
|
nbRead != section->size)
|
||||||
if (nbElementsRead != section->size)
|
errx("%s: Cannot read \"%s\"'s data: %s", fileName, section->name,
|
||||||
errx("%s: Cannot read \"%s\"'s data: %s",
|
feof(file) ? "Unexpected end of file" : strerror(errno));
|
||||||
fileName, section->name,
|
|
||||||
feof(file) ? "Unexpected end of file"
|
|
||||||
: strerror(errno));
|
|
||||||
}
|
}
|
||||||
section->data = data;
|
|
||||||
|
|
||||||
uint32_t nbPatches;
|
uint32_t nbPatches;
|
||||||
|
|
||||||
@@ -617,7 +609,7 @@ static void freeSection(struct Section *section)
|
|||||||
|
|
||||||
free(section->name);
|
free(section->name);
|
||||||
if (sect_HasData(section->type)) {
|
if (sect_HasData(section->type)) {
|
||||||
free(section->data);
|
delete section->data;
|
||||||
delete section->patches;
|
delete section->patches;
|
||||||
}
|
}
|
||||||
delete section->symbols;
|
delete section->symbols;
|
||||||
|
|||||||
@@ -174,7 +174,7 @@ static void writeBank(std::deque<struct Section const *> *bankSections, uint16_t
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Output the section itself
|
// Output the section itself
|
||||||
fwrite(section->data, sizeof(*section->data), section->size, outputFile);
|
fwrite(&(*section->data)[0], 1, section->size, outputFile);
|
||||||
if (overlayFile) {
|
if (overlayFile) {
|
||||||
// Skip bytes even with pipes
|
// Skip bytes even with pipes
|
||||||
for (uint16_t i = 0; i < section->size; i++)
|
for (uint16_t i = 0; i < section->size; i++)
|
||||||
|
|||||||
@@ -467,7 +467,7 @@ static void applyFilePatches(struct Section *section, struct Section *dataSectio
|
|||||||
error(patch.src, patch.lineNo,
|
error(patch.src, patch.lineNo,
|
||||||
"jr target out of reach (expected -129 < %" PRId16 " < 128)",
|
"jr target out of reach (expected -129 < %" PRId16 " < 128)",
|
||||||
jumpOffset);
|
jumpOffset);
|
||||||
dataSection->data[offset] = jumpOffset & 0xFF;
|
(*dataSection->data)[offset] = jumpOffset & 0xFF;
|
||||||
} else {
|
} else {
|
||||||
// Patch a certain number of bytes
|
// Patch a certain number of bytes
|
||||||
struct {
|
struct {
|
||||||
@@ -487,7 +487,7 @@ static void applyFilePatches(struct Section *section, struct Section *dataSectio
|
|||||||
value, value < 0 ? " (maybe negative?)" : "",
|
value, value < 0 ? " (maybe negative?)" : "",
|
||||||
types[patch.type].size * 8U);
|
types[patch.type].size * 8U);
|
||||||
for (uint8_t i = 0; i < types[patch.type].size; i++) {
|
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;
|
value >>= 8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
assert(!fileSections.empty()); // There should be at least one, from the above check
|
||||||
struct Section *section = fileSections[areaIdx].section;
|
struct Section *section = fileSections[areaIdx].section;
|
||||||
uint16_t *writeIndex = &fileSections[areaIdx].writeIndex;
|
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;
|
uint16_t addr = data[0] | data[1] << 8;
|
||||||
|
|
||||||
if (section->isAddressFixed) {
|
if (section->isAddressFixed) {
|
||||||
@@ -496,7 +496,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
|
|||||||
addr, *writeIndex);
|
addr, *writeIndex);
|
||||||
if (!section->data) {
|
if (!section->data) {
|
||||||
assert(section->size != 0);
|
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)
|
if (!section->data)
|
||||||
fatal(where, lineNo, "Failed to alloc data for \"%s\": %s",
|
fatal(where, lineNo, "Failed to alloc data for \"%s\": %s",
|
||||||
section->name, strerror(errno));
|
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 ")",
|
fatal(where, lineNo, "'T' line writes past \"%s\"'s end (%u > %" PRIu16 ")",
|
||||||
section->name, *writeIndex + (offset - writtenOfs), section->size);
|
section->name, *writeIndex + (offset - writtenOfs), section->size);
|
||||||
// Copy all bytes up to those (plus the byte that we'll overwrite)
|
// 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;
|
*writeIndex += offset - writtenOfs + 1;
|
||||||
writtenOfs = offset + 3; // Skip all three `baseValue` bytes, though
|
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)
|
if (*writeIndex + (nbBytes - writtenOfs) > section->size)
|
||||||
fatal(where, lineNo, "'T' line writes past \"%s\"'s end (%zu > %" PRIu16 ")",
|
fatal(where, lineNo, "'T' line writes past \"%s\"'s end (%zu > %" PRIu16 ")",
|
||||||
section->name, *writeIndex + (nbBytes - writtenOfs), section->size);
|
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;
|
*writeIndex += nbBytes - writtenOfs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -148,16 +148,11 @@ static void mergeSections(struct Section *target, struct Section *other, enum Se
|
|||||||
// `data` pointer, or a size of 0.
|
// `data` pointer, or a size of 0.
|
||||||
if (other->data) {
|
if (other->data) {
|
||||||
if (target->data) {
|
if (target->data) {
|
||||||
// Ensure we're not allocating 0 bytes
|
target->data->insert(target->data->end(), RANGE(*other->data));
|
||||||
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);
|
|
||||||
} else {
|
} else {
|
||||||
assert(target->size == other->size); // It has been increased just above
|
assert(target->size == other->size); // It has been increased just above
|
||||||
target->data = other->data;
|
target->data = other->data;
|
||||||
other->data = NULL; // Prevent a double free()
|
other->data = NULL; // Prevent a double `delete`
|
||||||
}
|
}
|
||||||
// Adjust patches' PC offsets
|
// Adjust patches' PC offsets
|
||||||
for (struct Patch &patch : *other->patches)
|
for (struct Patch &patch : *other->patches)
|
||||||
|
|||||||
Reference in New Issue
Block a user