Use move semantics for the union stacks

This commit is contained in:
Rangi42
2024-02-28 11:18:16 -05:00
committed by Sylvie
parent 7daa8759c9
commit 18d4a81954

View File

@@ -38,10 +38,10 @@ struct SectionStackEntry {
char const *scope; // Section's symbol scope char const *scope; // Section's symbol scope
uint32_t offset; uint32_t offset;
int32_t loadOffset; int32_t loadOffset;
std::stack<struct UnionStackEntry> *unionStack; std::stack<struct UnionStackEntry> unionStack;
}; };
std::stack<struct UnionStackEntry> *currentUnionStack = NULL; std::stack<struct UnionStackEntry> currentUnionStack;
std::deque<struct SectionStackEntry> sectionStack; std::deque<struct SectionStackEntry> sectionStack;
std::deque<struct Section> sectionList; std::deque<struct Section> sectionList;
uint32_t curOffset; // Offset into the current section (see sect_GetSymbolOffset) uint32_t curOffset; // Offset into the current section (see sect_GetSymbolOffset)
@@ -371,7 +371,7 @@ static struct Section *getSection(char const *name, enum SectionType type, uint3
// Set the current section // Set the current section
static void changeSection(void) static void changeSection(void)
{ {
if (currentUnionStack && !currentUnionStack->empty()) if (!currentUnionStack.empty())
fatalerror("Cannot change the section within a UNION\n"); fatalerror("Cannot change the section within a UNION\n");
sym_SetCurrentSymbolScope(NULL); sym_SetCurrentSymbolScope(NULL);
@@ -565,17 +565,12 @@ void sect_StartUnion(void)
return; return;
} }
if (!currentUnionStack) currentUnionStack.push({ .start = curOffset, .size = 0 });
currentUnionStack = new(std::nothrow) std::stack<struct UnionStackEntry>();
if (!currentUnionStack)
fatalerror("No memory for union stack: %s\n", strerror(errno));
currentUnionStack->push({ .start = curOffset, .size = 0 });
} }
static void endUnionMember(void) static void endUnionMember(void)
{ {
struct UnionStackEntry &member = currentUnionStack->top(); struct UnionStackEntry &member = currentUnionStack.top();
uint32_t memberSize = curOffset - member.start; uint32_t memberSize = curOffset - member.start;
if (memberSize > member.size) if (memberSize > member.size)
@@ -585,7 +580,7 @@ static void endUnionMember(void)
void sect_NextUnionMember(void) void sect_NextUnionMember(void)
{ {
if (!currentUnionStack || currentUnionStack->empty()) { if (currentUnionStack.empty()) {
error("Found NEXTU outside of a UNION construct\n"); error("Found NEXTU outside of a UNION construct\n");
return; return;
} }
@@ -594,21 +589,19 @@ void sect_NextUnionMember(void)
void sect_EndUnion(void) void sect_EndUnion(void)
{ {
if (!currentUnionStack || currentUnionStack->empty()) { if (currentUnionStack.empty()) {
error("Found ENDU outside of a UNION construct\n"); error("Found ENDU outside of a UNION construct\n");
return; return;
} }
endUnionMember(); endUnionMember();
curOffset += currentUnionStack->top().size; curOffset += currentUnionStack.top().size;
currentUnionStack->pop(); currentUnionStack.pop();
} }
void sect_CheckUnionClosed(void) void sect_CheckUnionClosed(void)
{ {
if (currentUnionStack && !currentUnionStack->empty()) if (!currentUnionStack.empty())
error("Unterminated UNION construct\n"); error("Unterminated UNION construct\n");
if (currentUnionStack)
delete currentUnionStack;
} }
// Output an absolute byte // Output an absolute byte
@@ -937,14 +930,14 @@ void sect_PushSection(void)
.scope = sym_GetCurrentSymbolScope(), .scope = sym_GetCurrentSymbolScope(),
.offset = curOffset, .offset = curOffset,
.loadOffset = loadOffset, .loadOffset = loadOffset,
.unionStack = currentUnionStack, .unionStack = {},
}); });
// Reset the section scope // Reset the section scope
currentSection = NULL; currentSection = NULL;
currentLoadSection = NULL; currentLoadSection = NULL;
sym_SetCurrentSymbolScope(NULL); sym_SetCurrentSymbolScope(NULL);
currentUnionStack = NULL; std::swap(currentUnionStack, sectionStack.front().unionStack);
} }
void sect_PopSection(void) void sect_PopSection(void)
@@ -964,8 +957,7 @@ void sect_PopSection(void)
sym_SetCurrentSymbolScope(entry.scope); sym_SetCurrentSymbolScope(entry.scope);
curOffset = entry.offset; curOffset = entry.offset;
loadOffset = entry.loadOffset; loadOffset = entry.loadOffset;
delete currentUnionStack; std::swap(currentUnionStack, entry.unionStack);
currentUnionStack = entry.unionStack;
} }
void sect_EndSection(void) void sect_EndSection(void)
@@ -976,7 +968,7 @@ void sect_EndSection(void)
if (currentLoadSection) if (currentLoadSection)
fatalerror("Cannot end the section within a `LOAD` block\n"); fatalerror("Cannot end the section within a `LOAD` block\n");
if (currentUnionStack && !currentUnionStack->empty()) if (!currentUnionStack.empty())
fatalerror("Cannot end the section within a UNION\n"); fatalerror("Cannot end the section within a UNION\n");
// Reset the section scope // Reset the section scope