mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-21 10:42:07 +00:00
Use RAII to unmap or close the lexer states' files automatically
This commit is contained in:
@@ -83,6 +83,17 @@ struct LexerState {
|
|||||||
std::deque<Expansion> expansions; // Front is the innermost current expansion
|
std::deque<Expansion> expansions; // Front is the innermost current expansion
|
||||||
|
|
||||||
std::variant<std::monostate, MmappedLexerState, ViewedLexerState, BufferedLexerState> content;
|
std::variant<std::monostate, MmappedLexerState, ViewedLexerState, BufferedLexerState> content;
|
||||||
|
|
||||||
|
LexerState() = default;
|
||||||
|
LexerState(LexerState &&) = default;
|
||||||
|
LexerState(LexerState const &) = delete;
|
||||||
|
|
||||||
|
// This destructor unmaps or closes the content file if applicable.
|
||||||
|
// As such, lexer states should not be copyable.
|
||||||
|
~LexerState();
|
||||||
|
|
||||||
|
LexerState &operator=(LexerState &&) = default;
|
||||||
|
LexerState &operator=(LexerState const &) = delete;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern LexerState *lexerState;
|
extern LexerState *lexerState;
|
||||||
@@ -117,7 +128,6 @@ void lexer_OpenFileView(
|
|||||||
LexerState &state, char const *path, char const *buf, size_t size, uint32_t lineNo
|
LexerState &state, char const *path, char const *buf, size_t size, uint32_t lineNo
|
||||||
);
|
);
|
||||||
void lexer_RestartRept(uint32_t lineNo);
|
void lexer_RestartRept(uint32_t lineNo);
|
||||||
void lexer_CleanupState(LexerState &state);
|
|
||||||
void lexer_Init();
|
void lexer_Init();
|
||||||
void lexer_SetMode(LexerMode mode);
|
void lexer_SetMode(LexerMode mode);
|
||||||
void lexer_ToggleStringExpansion(bool enable);
|
void lexer_ToggleStringExpansion(bool enable);
|
||||||
|
|||||||
@@ -220,7 +220,6 @@ bool yywrap() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
lexer_CleanupState(contextStack.top().lexerState);
|
|
||||||
contextStack.pop();
|
contextStack.pop();
|
||||||
lexer_SetState(&contextStack.top().lexerState);
|
lexer_SetState(&contextStack.top().lexerState);
|
||||||
|
|
||||||
|
|||||||
@@ -459,7 +459,7 @@ void lexer_RestartRept(uint32_t lineNo) {
|
|||||||
lexerState->lineNo = lineNo;
|
lexerState->lineNo = lineNo;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lexer_CleanupState(LexerState &state) {
|
LexerState::~LexerState() {
|
||||||
// A big chunk of the lexer state soundness is the file stack ("fstack").
|
// A big chunk of the lexer state soundness is the file stack ("fstack").
|
||||||
// Each context in the fstack has its own *unique* lexer state; thus, we always guarantee
|
// Each context in the fstack has its own *unique* lexer state; thus, we always guarantee
|
||||||
// that lexer states lifetimes are always properly managed, since they're handled solely
|
// that lexer states lifetimes are always properly managed, since they're handled solely
|
||||||
@@ -471,12 +471,12 @@ void lexer_CleanupState(LexerState &state) {
|
|||||||
// This assertion checks that this doesn't happen again.
|
// This assertion checks that this doesn't happen again.
|
||||||
// It could be argued that deleting a state that's scheduled for EOF could simply clear
|
// It could be argued that deleting a state that's scheduled for EOF could simply clear
|
||||||
// `lexerStateEOL`, but there's currently no situation in which this should happen.
|
// `lexerStateEOL`, but there's currently no situation in which this should happen.
|
||||||
assert(&state != lexerStateEOL);
|
assert(this != lexerStateEOL);
|
||||||
|
|
||||||
if (auto *mmap = std::get_if<MmappedLexerState>(&state.content); mmap) {
|
if (auto *mmap = std::get_if<MmappedLexerState>(&content); mmap) {
|
||||||
if (!mmap->isReferenced)
|
if (!mmap->isReferenced)
|
||||||
munmap(mmap->ptr, mmap->size);
|
munmap(mmap->ptr, mmap->size);
|
||||||
} else if (auto *cbuf = std::get_if<BufferedLexerState>(&state.content); cbuf) {
|
} else if (auto *cbuf = std::get_if<BufferedLexerState>(&content); cbuf) {
|
||||||
close(cbuf->fd);
|
close(cbuf->fd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user