Use automatic allocation for sections

This commit is contained in:
Rangi42
2024-02-22 15:04:52 -05:00
committed by Sylvie
parent e022adf4a0
commit 9140180c85
3 changed files with 52 additions and 58 deletions

View File

@@ -12,7 +12,7 @@ struct Expression;
struct FileStackNode; struct FileStackNode;
extern const char *objectName; extern const char *objectName;
extern std::deque<struct Section *> sectionList; extern std::deque<struct Section> sectionList;
void out_RegisterNode(struct FileStackNode *node); void out_RegisterNode(struct FileStackNode *node);
void out_ReplaceNode(struct FileStackNode *node); void out_ReplaceNode(struct FileStackNode *node);

View File

@@ -46,7 +46,7 @@ struct Assertion {
const char *objectName; const char *objectName;
std::deque<struct Section *> sectionList; std::deque<struct Section> sectionList;
// List of symbols to put in the object file // List of symbols to put in the object file
static std::vector<struct Symbol *> objectSymbols; static std::vector<struct Symbol *> objectSymbols;
@@ -102,20 +102,18 @@ This is code intended to replace a node, which is pretty useless until ref count
#endif #endif
} }
// Return a section's ID // Return a section's ID, or -1 if the section is not in the list
static uint32_t getsectid(struct Section const *sect) static uint32_t getSectIDIfAny(struct Section *sect)
{ {
auto search = std::find(RANGE(sectionList), sect); if (!sect)
return (uint32_t)-1;
for (auto it = sectionList.begin(); it != sectionList.end(); it++) {
if (&*it == sect)
return it - sectionList.begin();
}
if (search == sectionList.end())
fatalerror("Unknown section '%s'\n", sect->name); fatalerror("Unknown section '%s'\n", sect->name);
return search - sectionList.begin();
}
static uint32_t getSectIDIfAny(struct Section const *sect)
{
return sect ? getsectid(sect) : (uint32_t)-1;
} }
// Write a patch to a file // Write a patch to a file
@@ -133,39 +131,39 @@ static void writepatch(struct Patch const *patch, FILE *f)
} }
// Write a section to a file // Write a section to a file
static void writesection(struct Section const *sect, FILE *f) static void writesection(struct Section const &sect, FILE *f)
{ {
putstring(sect->name, f); putstring(sect.name, f);
putlong(sect->size, f); putlong(sect.size, f);
bool isUnion = sect->modifier == SECTION_UNION; bool isUnion = sect.modifier == SECTION_UNION;
bool isFragment = sect->modifier == SECTION_FRAGMENT; bool isFragment = sect.modifier == SECTION_FRAGMENT;
putc(sect->type | isUnion << 7 | isFragment << 6, f); putc(sect.type | isUnion << 7 | isFragment << 6, f);
putlong(sect->org, f); putlong(sect.org, f);
putlong(sect->bank, f); putlong(sect.bank, f);
putc(sect->align, f); putc(sect.align, f);
putlong(sect->alignOfs, f); putlong(sect.alignOfs, f);
if (sect_HasData(sect->type)) { if (sect_HasData(sect.type)) {
fwrite(sect->data, 1, sect->size, f); fwrite(sect.data, 1, sect.size, f);
putlong(sect->patches->size(), f); putlong(sect.patches->size(), f);
for (struct Patch const *patch : *sect->patches) for (struct Patch const *patch : *sect.patches)
writepatch(patch, f); writepatch(patch, f);
} }
} }
static void freesection(struct Section const *sect) static void freesection(struct Section const &sect)
{ {
if (sect_HasData(sect->type)) { if (sect_HasData(sect.type)) {
for (struct Patch *patch : *sect->patches) { for (struct Patch *patch : *sect.patches) {
free(patch->rpn); free(patch->rpn);
free(patch); free(patch);
} }
delete sect->patches; delete sect.patches;
} }
} }
@@ -439,7 +437,7 @@ void out_WriteObject(void)
for (struct Symbol const *sym : objectSymbols) for (struct Symbol const *sym : objectSymbols)
writesymbol(sym, f); writesymbol(sym, f);
for (struct Section *sect : sectionList) { for (struct Section &sect : sectionList) {
writesection(sect, f); writesection(sect, f);
freesection(sect); freesection(sect);
} }

View File

@@ -110,9 +110,9 @@ attr_(warn_unused_result) static bool reserveSpace(uint32_t delta_size)
struct Section *sect_FindSectionByName(char const *name) struct Section *sect_FindSectionByName(char const *name)
{ {
for (struct Section *sect : sectionList) { for (struct Section &sect : sectionList) {
if (strcmp(name, sect->name) == 0) if (strcmp(name, sect.name) == 0)
return sect; return &sect;
} }
return NULL; return NULL;
} }
@@ -267,38 +267,36 @@ static struct Section *createSection(char const *name, enum SectionType type,
uint32_t org, uint32_t bank, uint8_t alignment, uint32_t org, uint32_t bank, uint8_t alignment,
uint16_t alignOffset, enum SectionModifier mod) uint16_t alignOffset, enum SectionModifier mod)
{ {
struct Section *sect = (struct Section *)malloc(sizeof(*sect)); // Add the new section to the list (order doesn't matter)
struct Section &sect = sectionList.emplace_front();
if (sect == NULL) sect.name = strdup(name);
fatalerror("Not enough memory for section: %s\n", strerror(errno)); if (sect.name == NULL)
sect->name = strdup(name);
if (sect->name == NULL)
fatalerror("Not enough memory for section name: %s\n", strerror(errno)); fatalerror("Not enough memory for section name: %s\n", strerror(errno));
sect->type = type; sect.type = type;
sect->modifier = mod; sect.modifier = mod;
sect->src = fstk_GetFileStack(); sect.src = fstk_GetFileStack();
sect->fileLine = lexer_GetLineNo(); sect.fileLine = lexer_GetLineNo();
sect->size = 0; sect.size = 0;
sect->org = org; sect.org = org;
sect->bank = bank; sect.bank = bank;
sect->align = alignment; sect.align = alignment;
sect->alignOfs = alignOffset; sect.alignOfs = alignOffset;
sect->patches = new(std::nothrow) std::deque<struct Patch *>(); sect.patches = new(std::nothrow) std::deque<struct Patch *>();
if (sect->patches == NULL) if (sect.patches == NULL)
fatalerror("Not enough memory for section patches: %s\n", strerror(errno)); fatalerror("Not enough memory for section patches: %s\n", strerror(errno));
// It is only needed to allocate memory for ROM sections. // It is only needed to allocate memory for ROM sections.
if (sect_HasData(type)) { if (sect_HasData(type)) {
sect->data = (uint8_t *)malloc(sectionTypeInfo[type].size); sect.data = (uint8_t *)malloc(sectionTypeInfo[type].size);
if (sect->data == NULL) if (sect.data == NULL)
fatalerror("Not enough memory for section: %s\n", strerror(errno)); fatalerror("Not enough memory for section: %s\n", strerror(errno));
} else { } else {
sect->data = NULL; sect.data = NULL;
} }
return sect; return &sect;
} }
// Find a section by name and type. If it doesn't exist, create it. // Find a section by name and type. If it doesn't exist, create it.
@@ -371,8 +369,6 @@ static struct Section *getSection(char const *name, enum SectionType type, uint3
mergeSections(sect, type, org, bank, alignment, alignOffset, mod); mergeSections(sect, type, org, bank, alignment, alignOffset, mod);
} else { } else {
sect = createSection(name, type, org, bank, alignment, alignOffset, mod); sect = createSection(name, type, org, bank, alignment, alignOffset, mod);
// Add the new section to the list (order doesn't matter)
sectionList.push_front(sect);
} }
return sect; return sect;