Skip to content

Commit 8c30976

Browse files
committed
Move first allocation into alloc_comment()
1 parent 940f613 commit 8c30976

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

src/parser.c

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3190,28 +3190,18 @@ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) {
31903190
}
31913191

31923192
static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, rbs_token_t comment_token) {
3193-
if (com->line_count == 0) {
3194-
com->start = comment_token.range.start;
3195-
}
3196-
31973193
if (com->line_count == com->line_size) {
3198-
if (com->line_size == 0) com->line_size = 10; // Don't get stuck multiplying by 0 forever
3199-
3200-
if (com->tokens) {
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-
);
3212-
} else {
3213-
com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
3214-
}
3194+
size_t old_size = com->line_size;
3195+
size_t new_size = old_size * 2;
3196+
com->line_size = new_size;
3197+
3198+
com->tokens = rbs_allocator_realloc(
3199+
allocator,
3200+
com->tokens,
3201+
sizeof(rbs_token_t) * old_size,
3202+
sizeof(rbs_token_t) * new_size,
3203+
rbs_token_t
3204+
);
32153205
}
32163206

32173207
com->tokens[com->line_count++] = comment_token;
@@ -3221,19 +3211,22 @@ static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *c
32213211
static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, rbs_token_t comment_token, rbs_comment_t *last_comment) {
32223212
rbs_comment_t *new_comment = rbs_allocator_alloc(allocator, rbs_comment_t);
32233213

3214+
size_t initial_line_size = 10;
3215+
3216+
rbs_token_t *tokens = rbs_allocator_calloc(allocator, initial_line_size, rbs_token_t);
3217+
tokens[0] = comment_token;
3218+
32243219
*new_comment = (rbs_comment_t) {
32253220
.start = comment_token.range.start,
32263221
.end = comment_token.range.end,
32273222

3228-
.line_size = 0,
3229-
.line_count = 0,
3230-
.tokens = NULL,
3223+
.line_size = initial_line_size,
3224+
.line_count = 1,
3225+
.tokens = tokens,
32313226

32323227
.next_comment = last_comment,
32333228
};
32343229

3235-
comment_insert_new_line(allocator, new_comment, comment_token);
3236-
32373230
return new_comment;
32383231
}
32393232

0 commit comments

Comments
 (0)