From ea2c2b92839f18b2d4e09666e9dbb79f9067c069 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Wed, 2 Jul 2025 10:21:34 -0400 Subject: [PATCH 1/5] Use designated initialiser for `rbs_buffer_t` --- src/util/rbs_buffer.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/util/rbs_buffer.c b/src/util/rbs_buffer.c index 71421b975..95a8cbb04 100644 --- a/src/util/rbs_buffer.c +++ b/src/util/rbs_buffer.c @@ -2,12 +2,12 @@ #include "rbs/util/rbs_assert.h" bool rbs_buffer_init(rbs_allocator_t *allocator, rbs_buffer_t *buffer) { - size_t capacity = RBS_BUFFER_DEFAULT_CAPACITY; + *buffer = (rbs_buffer_t) { + .length = 0, + .capacity = RBS_BUFFER_DEFAULT_CAPACITY, + .value = rbs_allocator_calloc(allocator, RBS_BUFFER_DEFAULT_CAPACITY, char), + }; - buffer->length = 0; - buffer->capacity = capacity; - - buffer->value = rbs_allocator_calloc(allocator, capacity, char); return buffer->value != NULL; } From 791d944162c17a6de78e278891d33f1754aafda6 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Wed, 2 Jul 2025 10:21:45 -0400 Subject: [PATCH 2/5] Hide `RBS_BUFFER_DEFAULT_CAPACITY` --- include/rbs/util/rbs_buffer.h | 6 ------ src/util/rbs_buffer.c | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/rbs/util/rbs_buffer.h b/include/rbs/util/rbs_buffer.h index 7cc001ecc..3018b009f 100644 --- a/include/rbs/util/rbs_buffer.h +++ b/include/rbs/util/rbs_buffer.h @@ -8,12 +8,6 @@ #include #include -/** - * The default capacity of a rbs_buffer_t. - * If the buffer needs to grow beyond this capacity, it will be doubled. - */ -#define RBS_BUFFER_DEFAULT_CAPACITY 128 - /** * A rbs_buffer_t is a simple memory buffer that stores data in a contiguous block of memory. */ diff --git a/src/util/rbs_buffer.c b/src/util/rbs_buffer.c index 95a8cbb04..5911aabf7 100644 --- a/src/util/rbs_buffer.c +++ b/src/util/rbs_buffer.c @@ -1,6 +1,12 @@ #include "rbs/util/rbs_buffer.h" #include "rbs/util/rbs_assert.h" +/** + * The default capacity of a rbs_buffer_t. + * If the buffer needs to grow beyond this capacity, it will be doubled. + */ +#define RBS_BUFFER_DEFAULT_CAPACITY 128 + bool rbs_buffer_init(rbs_allocator_t *allocator, rbs_buffer_t *buffer) { *buffer = (rbs_buffer_t) { .length = 0, From 6855f492e7e66bb4c260d8fb56e60475fba2c13c Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Wed, 2 Jul 2025 10:22:11 -0400 Subject: [PATCH 3/5] Add `rbs_buffer_init_with_capacity` --- include/rbs/util/rbs_buffer.h | 2 ++ src/util/rbs_buffer.c | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/rbs/util/rbs_buffer.h b/include/rbs/util/rbs_buffer.h index 3018b009f..e50746c0f 100644 --- a/include/rbs/util/rbs_buffer.h +++ b/include/rbs/util/rbs_buffer.h @@ -31,6 +31,8 @@ typedef struct { */ bool rbs_buffer_init(rbs_allocator_t *, rbs_buffer_t *buffer); +bool rbs_buffer_init_with_capacity(rbs_allocator_t *allocator, rbs_buffer_t *buffer, size_t capacity); + /** * Return the value of the buffer. * diff --git a/src/util/rbs_buffer.c b/src/util/rbs_buffer.c index 5911aabf7..165a39876 100644 --- a/src/util/rbs_buffer.c +++ b/src/util/rbs_buffer.c @@ -8,10 +8,14 @@ #define RBS_BUFFER_DEFAULT_CAPACITY 128 bool rbs_buffer_init(rbs_allocator_t *allocator, rbs_buffer_t *buffer) { + return rbs_buffer_init_with_capacity(allocator, buffer, RBS_BUFFER_DEFAULT_CAPACITY); +} + +bool rbs_buffer_init_with_capacity(rbs_allocator_t *allocator, rbs_buffer_t *buffer, size_t capacity) { *buffer = (rbs_buffer_t) { .length = 0, - .capacity = RBS_BUFFER_DEFAULT_CAPACITY, - .value = rbs_allocator_calloc(allocator, RBS_BUFFER_DEFAULT_CAPACITY, char), + .capacity = capacity, + .value = rbs_allocator_calloc(allocator, capacity, char), }; return buffer->value != NULL; From abef4085709a320b1a4a3901d85b27d47310a3d7 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Wed, 2 Jul 2025 10:22:44 -0400 Subject: [PATCH 4/5] Reuse `rbs_buffer` for comment tokens --- include/rbs/parser.h | 4 ++-- include/rbs/util/rbs_buffer.h | 25 +++++++++++++++++++++++++ src/parser.c | 32 +++++++++----------------------- 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/include/rbs/parser.h b/include/rbs/parser.h index 339235deb..5e4e58a6f 100644 --- a/include/rbs/parser.h +++ b/include/rbs/parser.h @@ -4,6 +4,7 @@ #include "rbs/defines.h" #include "rbs/util/rbs_allocator.h" #include "rbs/util/rbs_constant_pool.h" +#include "rbs/util/rbs_buffer.h" #include "rbs/lexer.h" #include "rbs/ast.h" @@ -27,9 +28,8 @@ typedef struct rbs_comment_t { rbs_position_t start; rbs_position_t end; - size_t line_tokens_capacity; size_t line_tokens_count; - rbs_token_t *line_tokens; + rbs_buffer_t /* of rbs_token_t */ line_tokens; struct rbs_comment_t *next_comment; } rbs_comment_t; diff --git a/include/rbs/util/rbs_buffer.h b/include/rbs/util/rbs_buffer.h index e50746c0f..8a88bddb6 100644 --- a/include/rbs/util/rbs_buffer.h +++ b/include/rbs/util/rbs_buffer.h @@ -76,4 +76,29 @@ void rbs_buffer_append_string(rbs_allocator_t *, rbs_buffer_t *buffer, const cha */ rbs_string_t rbs_buffer_to_string(rbs_buffer_t *buffer); +/** + * Append a value to the buffer. + * + * @param allocator The allocator to use. + * @param buffer The buffer to append to. + * @param value The value to append. + * @param type The type of the value to append, which determines how many bytes to append. + */ +#define rbs_buffer_append_value(allocator, buffer, value, type) \ + rbs_buffer_append_string((allocator), (buffer), (char *) (value), sizeof(type)) + +/** + * Returns a copy of a `type` from the `buffer` at the given `index`. + * + * This cast is unchecked, so it's up to caller to ensure the type is correct. + * + * @param buffer The buffer to get the value from. + * @param index The index of the element to retrieve. + * @param type The element type that the data will be cast to. + * @returns The value at the specified index, cast to the specified type. + */ +#define rbs_buffer_get(buffer, index, type) ( \ + ((type *) (buffer).value)[index] \ +) + #endif diff --git a/src/parser.c b/src/parser.c index 0cbb4bf12..e533cb607 100644 --- a/src/parser.c +++ b/src/parser.c @@ -3208,7 +3208,7 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_ rbs_buffer_init(ALLOCATOR(), &rbs_buffer); for (size_t i = 0; i < com->line_tokens_count; i++) { - rbs_token_t tok = com->line_tokens[i]; + rbs_token_t tok = rbs_buffer_get(com->line_tokens, i, rbs_token_t); const char *comment_start = parser->rbs_lexer_t->string.start + tok.range.start.byte_pos + hash_bytes; size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes; @@ -3252,43 +3252,29 @@ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) { } static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, rbs_token_t comment_token) { - if (com->line_tokens_count == com->line_tokens_capacity) { - size_t old_size = com->line_tokens_capacity; - size_t new_size = old_size * 2; - com->line_tokens_capacity = new_size; - - com->line_tokens = rbs_allocator_realloc( - allocator, - com->line_tokens, - sizeof(rbs_token_t) * old_size, - sizeof(rbs_token_t) * new_size, - rbs_token_t - ); - } + rbs_buffer_append_value(allocator, &com->line_tokens, &comment_token, rbs_token_t); - com->line_tokens[com->line_tokens_count++] = comment_token; + com->line_tokens_count++; com->end = comment_token.range.end; } static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, rbs_token_t comment_token, rbs_comment_t *last_comment) { rbs_comment_t *new_comment = rbs_allocator_alloc(allocator, rbs_comment_t); - size_t initial_line_capacity = 10; - - rbs_token_t *tokens = rbs_allocator_calloc(allocator, initial_line_capacity, rbs_token_t); - tokens[0] = comment_token; - *new_comment = (rbs_comment_t) { .start = comment_token.range.start, .end = comment_token.range.end, - .line_tokens_capacity = initial_line_capacity, - .line_tokens_count = 1, - .line_tokens = tokens, + .line_tokens_count = 0, + .line_tokens = { 0 }, .next_comment = last_comment, }; + size_t initial_line_capacity = 10; + rbs_buffer_init_with_capacity(allocator, &new_comment->line_tokens, initial_line_capacity * sizeof(rbs_token_t)); + comment_insert_new_line(allocator, new_comment, comment_token); + return new_comment; } From 345351e972251796c2ad8bfc32e0c177857fcab5 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 9 Sep 2025 00:51:23 +0300 Subject: [PATCH 5/5] Test valgrind in CI --- include/rbs/util/rbs_buffer.h | 1 + src/parser.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/include/rbs/util/rbs_buffer.h b/include/rbs/util/rbs_buffer.h index 8a88bddb6..15d0f11c2 100644 --- a/include/rbs/util/rbs_buffer.h +++ b/include/rbs/util/rbs_buffer.h @@ -91,6 +91,7 @@ rbs_string_t rbs_buffer_to_string(rbs_buffer_t *buffer); * Returns a copy of a `type` from the `buffer` at the given `index`. * * This cast is unchecked, so it's up to caller to ensure the type is correct. + * Note: This assumes the buffer contains only elements of the specified type. * * @param buffer The buffer to get the value from. * @param index The index of the element to retrieve. diff --git a/src/parser.c b/src/parser.c index e533cb607..ae9ebf310 100644 --- a/src/parser.c +++ b/src/parser.c @@ -3254,7 +3254,7 @@ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) { static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, rbs_token_t comment_token) { rbs_buffer_append_value(allocator, &com->line_tokens, &comment_token, rbs_token_t); - com->line_tokens_count++; + com->line_tokens_count = com->line_tokens.length / sizeof(rbs_token_t); com->end = comment_token.range.end; }