Skip to content

Commit 1dbb362

Browse files
committed
Move first allocation into alloc_comment()
1 parent 479feef commit 1dbb362

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
@@ -3160,28 +3160,18 @@ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) {
31603160
}
31613161

31623162
static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, rbs_token_t comment_token) {
3163-
if (com->line_count == 0) {
3164-
com->start = comment_token.range.start;
3165-
}
3166-
31673163
if (com->line_count == com->line_size) {
3168-
if (com->line_size == 0) com->line_size = 10; // Don't get stuck multiplying by 0 forever
3169-
3170-
if (com->tokens) {
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-
);
3182-
} else {
3183-
com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
3184-
}
3164+
size_t old_size = com->line_size;
3165+
size_t new_size = old_size * 2;
3166+
com->line_size = new_size;
3167+
3168+
com->tokens = rbs_allocator_realloc(
3169+
allocator,
3170+
com->tokens,
3171+
sizeof(rbs_token_t) * old_size,
3172+
sizeof(rbs_token_t) * new_size,
3173+
rbs_token_t
3174+
);
31853175
}
31863176

31873177
com->tokens[com->line_count++] = comment_token;
@@ -3191,19 +3181,22 @@ static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *c
31913181
static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, rbs_token_t comment_token, rbs_comment_t *last_comment) {
31923182
rbs_comment_t *new_comment = rbs_allocator_alloc(allocator, rbs_comment_t);
31933183

3184+
size_t initial_line_size = 10;
3185+
3186+
rbs_token_t *tokens = rbs_allocator_calloc(allocator, initial_line_size, rbs_token_t);
3187+
tokens[0] = comment_token;
3188+
31943189
*new_comment = (rbs_comment_t) {
31953190
.start = comment_token.range.start,
31963191
.end = comment_token.range.end,
31973192

3198-
.line_size = 0,
3199-
.line_count = 0,
3200-
.tokens = NULL,
3193+
.line_size = initial_line_size,
3194+
.line_count = 1,
3195+
.tokens = tokens,
32013196

32023197
.next_comment = last_comment,
32033198
};
32043199

3205-
comment_insert_new_line(allocator, new_comment, comment_token);
3206-
32073200
return new_comment;
32083201
}
32093202

0 commit comments

Comments
 (0)