Skip to content

Commit 479feef

Browse files
committed
Replace hand-rolled realloc
1 parent e566460 commit 479feef

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
@@ -3165,12 +3165,20 @@ static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *c
31653165
}
31663166

31673167
if (com->line_count == com->line_size) {
3168-
com->line_size += 10;
3168+
if (com->line_size == 0) com->line_size = 10; // Don't get stuck multiplying by 0 forever
31693169

31703170
if (com->tokens) {
3171-
rbs_token_t *p = com->tokens;
3172-
com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
3173-
memcpy(com->tokens, p, sizeof(rbs_token_t) * com->line_count);
3171+
size_t old_size = com->line_size;
3172+
size_t new_size = old_size * 2;
3173+
com->line_size = new_size;
3174+
3175+
com->tokens = rbs_allocator_realloc(
3176+
allocator,
3177+
com->tokens,
3178+
sizeof(rbs_token_t) * old_size,
3179+
sizeof(rbs_token_t) * new_size,
3180+
rbs_token_t
3181+
);
31743182
} else {
31753183
com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
31763184
}

0 commit comments

Comments
 (0)