Skip to content

Commit e17236a

Browse files
committed
Use a constexpr instead of #define LEXER_BUF_SIZE
1 parent 73ad431 commit e17236a

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

include/asm/lexer.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
// This value is a compromise between `LexerState` allocation performance when `mmap` works, and
1717
// buffering performance when it doesn't/can't (e.g. when piping a file into RGBASM).
18-
#define LEXER_BUF_SIZE 64
18+
static constexpr size_t lexer_buf_size = 64;
1919
// The buffer needs to be large enough for the maximum `lexerState->peek()` lookahead distance
20-
static_assert(LEXER_BUF_SIZE > 1, "Lexer buffer size is too small");
20+
static_assert(lexer_buf_size > 1, "Lexer buffer size is too small");
2121
// This caps the size of buffer reads, and according to POSIX, passing more than SSIZE_MAX is UB
22-
static_assert(LEXER_BUF_SIZE <= SSIZE_MAX, "Lexer buffer size is too large");
22+
static_assert(lexer_buf_size <= SSIZE_MAX, "Lexer buffer size is too large");
2323

2424
enum LexerMode {
2525
LEXER_NORMAL,
@@ -58,7 +58,7 @@ struct ViewedContent {
5858

5959
struct BufferedContent {
6060
int fd; // File from which to read chars
61-
char buf[LEXER_BUF_SIZE] = {}; // Circular buffer of chars
61+
char buf[lexer_buf_size] = {}; // Circular buffer of chars
6262
size_t offset = 0; // Cursor into `buf`
6363
size_t size = 0; // Number of "fresh" chars in `buf`
6464

src/asm/lexer.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <unistd.h>
2020
#endif
2121

22-
#include "helpers.hpp" // assume, QUOTEDSTRLEN
22+
#include "helpers.hpp"
2323
#include "util.hpp"
2424

2525
#include "asm/fixpoint.hpp"
@@ -500,27 +500,27 @@ BufferedContent::~BufferedContent() {
500500
}
501501

502502
void BufferedContent::advance() {
503-
assume(offset < LEXER_BUF_SIZE);
503+
assume(offset < ARRAY_SIZE(buf));
504504
offset++;
505-
if (offset == LEXER_BUF_SIZE)
505+
if (offset == ARRAY_SIZE(buf))
506506
offset = 0; // Wrap around if necessary
507507
assume(size > 0);
508508
size--;
509509
}
510510

511511
void BufferedContent::refill() {
512-
size_t target = LEXER_BUF_SIZE - size; // Aim: making the buf full
512+
size_t target = ARRAY_SIZE(buf) - size; // Aim: making the buf full
513513

514514
// Compute the index we'll start writing to
515-
size_t startIndex = (offset + size) % LEXER_BUF_SIZE;
515+
size_t startIndex = (offset + size) % ARRAY_SIZE(buf);
516516

517517
// If the range to fill passes over the buffer wrapping point, we need two reads
518-
if (startIndex + target > LEXER_BUF_SIZE) {
519-
size_t nbExpectedChars = LEXER_BUF_SIZE - startIndex;
518+
if (startIndex + target > ARRAY_SIZE(buf)) {
519+
size_t nbExpectedChars = ARRAY_SIZE(buf) - startIndex;
520520
size_t nbReadChars = readMore(startIndex, nbExpectedChars);
521521

522522
startIndex += nbReadChars;
523-
if (startIndex == LEXER_BUF_SIZE)
523+
if (startIndex == ARRAY_SIZE(buf))
524524
startIndex = 0;
525525

526526
// If the read was incomplete, don't perform a second read
@@ -534,7 +534,7 @@ void BufferedContent::refill() {
534534

535535
size_t BufferedContent::readMore(size_t startIndex, size_t nbChars) {
536536
// This buffer overflow made me lose WEEKS of my life. Never again.
537-
assume(startIndex + nbChars <= LEXER_BUF_SIZE);
537+
assume(startIndex + nbChars <= ARRAY_SIZE(buf));
538538
ssize_t nbReadChars = read(fd, &buf[startIndex], nbChars);
539539

540540
if (nbReadChars == -1)
@@ -720,7 +720,7 @@ int LexerState::peekChar() {
720720
auto &cbuf = content.get<BufferedContent>();
721721
if (cbuf.size == 0)
722722
cbuf.refill();
723-
assume(cbuf.offset < LEXER_BUF_SIZE);
723+
assume(cbuf.offset < ARRAY_SIZE(cbuf.buf));
724724
if (cbuf.size > 0)
725725
return static_cast<uint8_t>(cbuf.buf[cbuf.offset]);
726726
}
@@ -748,11 +748,11 @@ int LexerState::peekCharAhead() {
748748
return static_cast<uint8_t>(view.span.ptr[view.offset + distance]);
749749
} else {
750750
auto &cbuf = content.get<BufferedContent>();
751-
assume(distance < LEXER_BUF_SIZE);
751+
assume(distance < ARRAY_SIZE(cbuf.buf));
752752
if (cbuf.size <= distance)
753753
cbuf.refill();
754754
if (cbuf.size > distance)
755-
return static_cast<uint8_t>(cbuf.buf[(cbuf.offset + distance) % LEXER_BUF_SIZE]);
755+
return static_cast<uint8_t>(cbuf.buf[(cbuf.offset + distance) % ARRAY_SIZE(cbuf.buf)]);
756756
}
757757

758758
// If there aren't enough chars, give up

test/asm/ff00+c.asm

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SECTION "test", ROM0[0]
22
ldh [ $ff00 + c ], a
3-
; 257 spaces exceeds both LEXER_BUF_SIZE (42) and uint8_t limit (255)
3+
; 257 spaces exceeds both lexer_buf_size (64) and uint8_t limit (255)
44
ldh [ $ff00 + c ], a
55
ldh [ $ff00 + c ], a

0 commit comments

Comments
 (0)