Use std::vector for reading object file sections

This commit is contained in:
Rangi42
2024-02-27 10:12:07 -05:00
committed by Sylvie
parent af055ecd27
commit 3d23f5bbb3

View File

@@ -171,7 +171,7 @@ static void readFileStackNode(FILE *file, struct FileStackNode fileNodes[], uint
tryReadlong(parentID, file, tryReadlong(parentID, file,
"%s: Cannot read node #%" PRIu32 "'s parent ID: %s", fileName, i); "%s: Cannot read node #%" PRIu32 "'s parent ID: %s", fileName, i);
fileNodes[i].parent = parentID == (uint32_t)-1 ? NULL : &fileNodes[parentID]; fileNodes[i].parent = parentID != (uint32_t)-1 ? &fileNodes[parentID] : NULL;
tryReadlong(fileNodes[i].lineNo, file, tryReadlong(fileNodes[i].lineNo, file,
"%s: Cannot read node #%" PRIu32 "'s line number: %s", fileName, i); "%s: Cannot read node #%" PRIu32 "'s line number: %s", fileName, i);
tryGetc(enum FileStackNodeType, fileNodes[i].type, file, tryGetc(enum FileStackNodeType, fileNodes[i].type, file,
@@ -289,10 +289,10 @@ static void readPatch(FILE *file, struct Patch *patch, char const *fileName, cha
* Sets a patch's pcSection from its pcSectionID. * Sets a patch's pcSection from its pcSectionID.
* @param patch The struct to fix * @param patch The struct to fix
*/ */
static void linkPatchToPCSect(struct Patch *patch, struct Section *fileSections[]) static void linkPatchToPCSect(struct Patch *patch, std::vector<struct Section *> const &fileSections)
{ {
patch->pcSection = patch->pcSectionID == (uint32_t)-1 ? NULL patch->pcSection = patch->pcSectionID != (uint32_t)-1 ? fileSections[patch->pcSectionID]
: fileSections[patch->pcSectionID]; : NULL;
} }
/* /*
@@ -534,8 +534,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
symbolLists.push_front({ .nbSymbols = nbSymbols, .symbolList = fileSymbols }); symbolLists.push_front({ .nbSymbols = nbSymbols, .symbolList = fileSymbols });
uint32_t *nbSymPerSect = (uint32_t *)calloc(nbSections ? nbSections : 1, std::vector<uint32_t> nbSymPerSect(nbSections, 0);
sizeof(*nbSymPerSect));
verbosePrint("Reading %" PRIu32 " symbols...\n", nbSymbols); verbosePrint("Reading %" PRIu32 " symbols...\n", nbSymbols);
for (uint32_t i = 0; i < nbSymbols; i++) { for (uint32_t i = 0; i < nbSymbols; i++) {
@@ -554,8 +553,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
} }
// This file's sections, stored in a table to link symbols to them // This file's sections, stored in a table to link symbols to them
struct Section **fileSections = std::vector<struct Section *> fileSections(nbSections, NULL);
(struct Section **)malloc(sizeof(*fileSections) * (nbSections ? nbSections : 1));
verbosePrint("Reading %" PRIu32 " sections...\n", nbSections); verbosePrint("Reading %" PRIu32 " sections...\n", nbSections);
for (uint32_t i = 0; i < nbSections; i++) { for (uint32_t i = 0; i < nbSections; i++) {
@@ -580,8 +578,6 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
sect_AddSection(fileSections[i]); sect_AddSection(fileSections[i]);
} }
free(nbSymPerSect);
// Give patches' PC section pointers to their sections // Give patches' PC section pointers to their sections
for (uint32_t i = 0; i < nbSections; i++) { for (uint32_t i = 0; i < nbSections; i++) {
if (sect_HasData(fileSections[i]->type)) { if (sect_HasData(fileSections[i]->type)) {
@@ -624,7 +620,6 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
assertion.fileSymbols = fileSymbols; assertion.fileSymbols = fileSymbols;
} }
free(fileSections);
fclose(file); fclose(file);
} }