Skip to content

Commit

Permalink
fix: only allocate via calloc once on create and free when done
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Jun 17, 2023
1 parent aaf899e commit 1fd87e0
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tree-sitter-markdown/src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ static unsigned serialize(Scanner *s, char *buffer) {
static void deserialize(Scanner *s, const char *buffer, unsigned length) {
s->open_blocks.size = 0;
s->open_blocks.capacity = 0;
s->open_blocks.items = NULL;
s->state = 0;
s->matched = 0;
s->indentation = 0;
Expand All @@ -277,7 +276,6 @@ static void deserialize(Scanner *s, const char *buffer, unsigned length) {
if (blocks_size > 0) {
size_t blocks_count = blocks_size / sizeof(Block);
s->open_blocks.capacity = roundup_32(blocks_count);
s->open_blocks.items = (Block*) malloc(sizeof(Block) * s->open_blocks.capacity);
memcpy(s->open_blocks.items, &buffer[i], blocks_size);
s->open_blocks.size = blocks_count;
}
Expand Down Expand Up @@ -1454,6 +1452,7 @@ static bool scan(Scanner *s, TSLexer *lexer, const bool *valid_symbols) {

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

assert(ATX_H6_MARKER == ATX_H1_MARKER + 5);
deserialize(s, NULL, 0);
Expand Down Expand Up @@ -1490,5 +1489,6 @@ void tree_sitter_markdown_external_scanner_deserialize(

void tree_sitter_markdown_external_scanner_destroy(void *payload) {
Scanner *scanner = (Scanner *)payload;
free(scanner->open_blocks.items);
free(scanner);
}

0 comments on commit 1fd87e0

Please sign in to comment.