diff --git a/include/asm/lexer.hpp b/include/asm/lexer.hpp index 0da3aafd..940595d3 100644 --- a/include/asm/lexer.hpp +++ b/include/asm/lexer.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "platform.hpp" // SSIZE_MAX @@ -82,8 +83,7 @@ struct LexerState { bool capturing; // Whether the text being lexed should be captured size_t captureSize; // Amount of text captured - char *captureBuf; // Buffer to send the captured text to if non-null - size_t captureCapacity; // Size of the buffer above + std::vector *captureBuf; // Buffer to send the captured text to if non-null bool disableMacroArgs; bool disableInterpolation; diff --git a/src/asm/charmap.cpp b/src/asm/charmap.cpp index 9c196c06..b6429482 100644 --- a/src/asm/charmap.cpp +++ b/src/asm/charmap.cpp @@ -24,7 +24,7 @@ struct CharmapNode { bool isTerminal; // Whether there exists a mapping that ends here uint8_t value; // If the above is true, its corresponding value - // This MUST be indexes and not pointers, because pointers get invalidated by `realloc`! + // This MUST be indexes and not pointers, because pointers get invalidated by reallocation! size_t next[255]; // Indexes of where to go next, 0 = nowhere }; diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index 61240de4..5780d506 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #ifndef _MSC_VER #include #endif @@ -487,19 +488,6 @@ void lexer_ToggleStringExpansion(bool enable) // Functions for the actual lexer to obtain characters -static void reallocCaptureBuf() -{ - if (lexerState->captureCapacity == SIZE_MAX) - fatalerror("Cannot grow capture buffer past %zu bytes\n", SIZE_MAX); - else if (lexerState->captureCapacity > SIZE_MAX / 2) - lexerState->captureCapacity = SIZE_MAX; - else - lexerState->captureCapacity *= 2; - lexerState->captureBuf = (char *)realloc(lexerState->captureBuf, lexerState->captureCapacity); - if (!lexerState->captureBuf) - fatalerror("realloc error while resizing capture buffer: %s\n", strerror(errno)); -} - static void beginExpansion(char const *str, bool owned, char const *name) { size_t size = strlen(str); @@ -763,12 +751,8 @@ static int peek() static void shiftChar() { if (lexerState->capturing) { - if (lexerState->captureBuf) { - if (lexerState->captureSize + 1 >= lexerState->captureCapacity) - reallocCaptureBuf(); - // TODO: improve this? - lexerState->captureBuf[lexerState->captureSize] = peek(); - } + if (lexerState->captureBuf) + lexerState->captureBuf->push_back(peek()); lexerState->captureSize++; } @@ -2293,19 +2277,20 @@ static void startCapture(CaptureBody *capture) if (lexerState->isMmapped && lexerState->expansions.empty()) { capture->body = &lexerState->mmap.ptr.unreferenced[lexerState->mmap.offset]; } else { - lexerState->captureCapacity = 128; // The initial size will be twice that assert(lexerState->captureBuf == nullptr); - reallocCaptureBuf(); + lexerState->captureBuf = new(std::nothrow) std::vector(); + if (!lexerState->captureBuf) + fatalerror("Failed to allocate capture buffer: %s\n", strerror(errno)); capture->body = nullptr; // Indicate to retrieve the capture buffer when done capturing } } static void endCapture(CaptureBody *capture) { - // This being `nullptr` means we're capturing from the capture buf, which is `realloc`ed + // This being `nullptr` means we're capturing from the capture buf, which is reallocated // during the whole capture process, and so MUST be retrieved at the end if (!capture->body) - capture->body = lexerState->captureBuf; + capture->body = lexerState->captureBuf->data(); capture->size = lexerState->captureSize; lexerState->capturing = false;