Use std::deque for sections

This commit is contained in:
Rangi42
2024-02-21 12:24:50 -05:00
committed by Sylvie
parent 2ea6de7195
commit bc8cb754c0

View File

@@ -38,11 +38,10 @@ struct SectionStackEntry {
uint32_t offset; uint32_t offset;
int32_t loadOffset; int32_t loadOffset;
std::stack<struct UnionStackEntry> *unionStack; std::stack<struct UnionStackEntry> *unionStack;
struct SectionStackEntry *next;
}; };
std::stack<struct UnionStackEntry> *currentUnionStack = NULL; std::stack<struct UnionStackEntry> *currentUnionStack = NULL;
struct SectionStackEntry *sectionStack; std::deque<struct SectionStackEntry> sectionStack;
uint32_t curOffset; // Offset into the current section (see sect_GetSymbolOffset) uint32_t curOffset; // Offset into the current section (see sect_GetSymbolOffset)
struct Section *currentSection = NULL; struct Section *currentSection = NULL;
static struct Section *currentLoadSection = NULL; static struct Section *currentLoadSection = NULL;
@@ -395,8 +394,8 @@ void sect_NewSection(char const *name, enum SectionType type, uint32_t org,
if (currentLoadSection) if (currentLoadSection)
fatalerror("Cannot change the section within a `LOAD` block\n"); fatalerror("Cannot change the section within a `LOAD` block\n");
for (struct SectionStackEntry *stack = sectionStack; stack; stack = stack->next) { for (struct SectionStackEntry &entry : sectionStack) {
if (stack->section && !strcmp(name, stack->section->name)) if (entry.section && !strcmp(name, entry.section->name))
fatalerror("Section '%s' is already on the stack\n", name); fatalerror("Section '%s' is already on the stack\n", name);
} }
@@ -942,18 +941,14 @@ cleanup:
// Section stack routines // Section stack routines
void sect_PushSection(void) void sect_PushSection(void)
{ {
struct SectionStackEntry *entry = (struct SectionStackEntry *)malloc(sizeof(*entry)); sectionStack.push_front({
.section = currentSection,
if (entry == NULL) .loadSection = currentLoadSection,
fatalerror("No memory for section stack: %s\n", strerror(errno)); .scope = sym_GetCurrentSymbolScope(),
entry->section = currentSection; .offset = curOffset,
entry->loadSection = currentLoadSection; .loadOffset = loadOffset,
entry->scope = sym_GetCurrentSymbolScope(); .unionStack = currentUnionStack,
entry->offset = curOffset; });
entry->loadOffset = loadOffset;
entry->unionStack = currentUnionStack;
entry->next = sectionStack;
sectionStack = entry;
// Reset the section scope // Reset the section scope
currentSection = NULL; currentSection = NULL;
@@ -964,25 +959,23 @@ void sect_PushSection(void)
void sect_PopSection(void) void sect_PopSection(void)
{ {
if (!sectionStack) if (sectionStack.empty())
fatalerror("No entries in the section stack\n"); fatalerror("No entries in the section stack\n");
if (currentLoadSection) if (currentLoadSection)
fatalerror("Cannot change the section within a `LOAD` block\n"); fatalerror("Cannot change the section within a `LOAD` block\n");
struct SectionStackEntry *entry = sectionStack; struct SectionStackEntry entry = sectionStack.front();
sectionStack.pop_front();
changeSection(); changeSection();
currentSection = entry->section; currentSection = entry.section;
currentLoadSection = entry->loadSection; currentLoadSection = entry.loadSection;
sym_SetCurrentSymbolScope(entry->scope); sym_SetCurrentSymbolScope(entry.scope);
curOffset = entry->offset; curOffset = entry.offset;
loadOffset = entry->loadOffset; loadOffset = entry.loadOffset;
delete currentUnionStack; delete currentUnionStack;
currentUnionStack = entry->unionStack; currentUnionStack = entry.unionStack;
sectionStack = entry->next;
free(entry);
} }
void sect_EndSection(void) void sect_EndSection(void)
@@ -1012,8 +1005,8 @@ bool sect_IsSizeKnown(struct Section const NONNULL(sect))
return false; return false;
// Any section on the stack is still growing // Any section on the stack is still growing
for (struct SectionStackEntry *stack = sectionStack; stack; stack = stack->next) { for (struct SectionStackEntry &entry : sectionStack) {
if (stack->section && !strcmp(sect->name, stack->section->name)) if (entry.section && !strcmp(sect->name, entry.section->name))
return false; return false;
} }