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 7c5ae30 commit 846a7f1
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 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 @@ -1318,7 +1316,9 @@ static bool scan(Scanner *s, TSLexer *lexer, const bool *valid_symbols) {
return parse_html_block(s, lexer, valid_symbols);
break;
}
if (lexer->lookahead != '\r' && lexer->lookahead != '\n' && valid_symbols[PIPE_TABLE_START]) {
if (lexer->lookahead != '\r'
&& lexer->lookahead != '\n'
&& valid_symbols[PIPE_TABLE_START]) {
return parse_pipe_table(s, lexer, valid_symbols);
}
} else { // we are in the state of trying to match all currently open blocks
Expand Down Expand Up @@ -1454,6 +1454,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 +1491,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 846a7f1

Please sign in to comment.