Skip to content

Commit ceb472c

Browse files
committed
Replace hand-rolled realloc
1 parent 2f6829d commit ceb472c

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

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_tokens_count == com->line_tokens_capacity) {
3168-
com->line_tokens_capacity += 10;
3168+
if (com->line_tokens_capacity == 0) com->line_tokens_capacity = 10; // Don't get stuck multiplying by 0 forever
31693169

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

0 commit comments

Comments
 (0)