Skip to content

Reuse rbs_buffer_t for storing comment tokens #2585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replace hand-rolled realloc
  • Loading branch information
amomchilov committed Jul 2, 2025
commit ceb472c9e19ce86bbed624fb4ba7d6cfdd0f6688
16 changes: 12 additions & 4 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -3165,12 +3165,20 @@ static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *c
}

if (com->line_tokens_count == com->line_tokens_capacity) {
com->line_tokens_capacity += 10;
if (com->line_tokens_capacity == 0) com->line_tokens_capacity = 10; // Don't get stuck multiplying by 0 forever

if (com->line_tokens) {
rbs_token_t *p = com->line_tokens;
com->line_tokens = rbs_allocator_calloc(allocator, com->line_tokens_capacity, rbs_token_t);
memcpy(com->line_tokens, p, sizeof(rbs_token_t) * com->line_tokens_count);
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
);
} else {
com->line_tokens = rbs_allocator_calloc(allocator, com->line_tokens_capacity, rbs_token_t);
}
Expand Down