Skip to content

Commit 940f613

Browse files
committed
Replace hand-rolled realloc
1 parent 5eb8845 commit 940f613

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/parser.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3195,12 +3195,20 @@ static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *c
31953195
}
31963196

31973197
if (com->line_count == com->line_size) {
3198-
com->line_size += 10;
3198+
if (com->line_size == 0) com->line_size = 10; // Don't get stuck multiplying by 0 forever
31993199

32003200
if (com->tokens) {
3201-
rbs_token_t *p = com->tokens;
3202-
com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
3203-
memcpy(com->tokens, p, sizeof(rbs_token_t) * com->line_count);
3201+
size_t old_size = com->line_size;
3202+
size_t new_size = old_size * 2;
3203+
com->line_size = new_size;
3204+
3205+
com->tokens = rbs_allocator_realloc(
3206+
allocator,
3207+
com->tokens,
3208+
sizeof(rbs_token_t) * old_size,
3209+
sizeof(rbs_token_t) * new_size,
3210+
rbs_token_t
3211+
);
32043212
} else {
32053213
com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
32063214
}

0 commit comments

Comments
 (0)