Skip to content

Commit 1fd87e0

Browse files
committed
fix: only allocate via calloc once on create and free when done
1 parent aaf899e commit 1fd87e0

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

tree-sitter-markdown/src/scanner.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ static unsigned serialize(Scanner *s, char *buffer) {
260260
static void deserialize(Scanner *s, const char *buffer, unsigned length) {
261261
s->open_blocks.size = 0;
262262
s->open_blocks.capacity = 0;
263-
s->open_blocks.items = NULL;
264263
s->state = 0;
265264
s->matched = 0;
266265
s->indentation = 0;
@@ -277,7 +276,6 @@ static void deserialize(Scanner *s, const char *buffer, unsigned length) {
277276
if (blocks_size > 0) {
278277
size_t blocks_count = blocks_size / sizeof(Block);
279278
s->open_blocks.capacity = roundup_32(blocks_count);
280-
s->open_blocks.items = (Block*) malloc(sizeof(Block) * s->open_blocks.capacity);
281279
memcpy(s->open_blocks.items, &buffer[i], blocks_size);
282280
s->open_blocks.size = blocks_count;
283281
}
@@ -1454,6 +1452,7 @@ static bool scan(Scanner *s, TSLexer *lexer, const bool *valid_symbols) {
14541452

14551453
void *tree_sitter_markdown_external_scanner_create() {
14561454
Scanner *s = (Scanner *)malloc(sizeof(Scanner));
1455+
s->open_blocks.items = (Block *)calloc(1, sizeof(Block));
14571456

14581457
assert(ATX_H6_MARKER == ATX_H1_MARKER + 5);
14591458
deserialize(s, NULL, 0);
@@ -1490,5 +1489,6 @@ void tree_sitter_markdown_external_scanner_deserialize(
14901489

14911490
void tree_sitter_markdown_external_scanner_destroy(void *payload) {
14921491
Scanner *scanner = (Scanner *)payload;
1492+
free(scanner->open_blocks.items);
14931493
free(scanner);
14941494
}

0 commit comments

Comments
 (0)