diff --git a/spec/integration/corpus_specs.cc b/spec/integration/corpus_specs.cc index a986a2d3..9d9cd191 100644 --- a/spec/integration/corpus_specs.cc +++ b/spec/integration/corpus_specs.cc @@ -1,16 +1,18 @@ #include "spec_helper.h" +#include "runtime/alloc.h" #include "helpers/test_languages.h" #include "helpers/read_test_entries.h" #include "helpers/spy_input.h" #include "helpers/log_debugger.h" #include "helpers/point_helpers.h" #include "helpers/encoding_helpers.h" +#include "helpers/record_alloc.h" #include static void expect_the_correct_tree(TSNode node, TSDocument *document, string tree_string) { const char *node_string = ts_node_string(node, document); AssertThat(node_string, Equals(tree_string)); - free((void *)node_string); + ts_free((void *)node_string); } static void expect_a_consistent_tree(TSNode node, TSDocument *document) { @@ -105,6 +107,7 @@ describe("The Corpus", []() { TSDocument *document; before_each([&]() { + record_alloc::start(); document = ts_document_make(); ts_document_set_language(document, get_test_language(language_name)); // ts_document_set_debugger(document, log_debugger_make(true)); @@ -112,6 +115,7 @@ describe("The Corpus", []() { after_each([&]() { ts_document_free(document); + AssertThat(record_alloc::outstanding_allocation_indices(), IsEmpty()); }); for (auto &entry : read_corpus_entries(language_dir + "/grammar_test")) { diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 10124325..76507e78 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -605,28 +605,35 @@ static ParseActionResult ts_parser__accept(TSParser *self, int head) { TSTree *root = pop_result->trees[i]; size_t leading_extra_count = i; size_t trailing_extra_count = pop_result->tree_count - 1 - i; - TSTree **new_children = ts_calloc( - root->child_count + leading_extra_count + trailing_extra_count, - sizeof(TSTree *)); - if (!new_children) - goto error; - - if (leading_extra_count > 0) - memcpy(new_children, pop_result->trees, - leading_extra_count * sizeof(TSTree *)); - if (root->child_count > 0) - memcpy(new_children + leading_extra_count, root->children, - root->child_count * sizeof(TSTree *)); - if (trailing_extra_count > 0) - memcpy(new_children + leading_extra_count + root->child_count, - pop_result->trees + leading_extra_count + 1, - trailing_extra_count * sizeof(TSTree *)); size_t new_count = root->child_count + leading_extra_count + trailing_extra_count; - ts_tree_set_children(root, new_count, new_children); + + if (new_count > 0) { + TSTree **new_children = ts_calloc(new_count, sizeof(TSTree *)); + if (!new_children) + goto error; + if (leading_extra_count > 0) + memcpy(new_children, pop_result->trees, + leading_extra_count * sizeof(TSTree *)); + if (root->child_count > 0) + memcpy(new_children + leading_extra_count, root->children, + root->child_count * sizeof(TSTree *)); + if (trailing_extra_count > 0) + memcpy(new_children + leading_extra_count + root->child_count, + pop_result->trees + leading_extra_count + 1, + trailing_extra_count * sizeof(TSTree *)); + ts_tree_set_children(root, new_count, new_children); + } + ts_parser__remove_head(self, pop_result->head_index); - self->finished_tree = - ts_parser__select_tree(self, self->finished_tree, root); + TSTree *tree = ts_parser__select_tree(self, self->finished_tree, root); + if (tree == root) { + ts_tree_release(self->finished_tree); + self->finished_tree = root; + } else { + ts_tree_release(root); + } + ts_free(pop_result->trees); break; } @@ -834,10 +841,8 @@ TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *previous_tree) { ts_stack_head_count(self->stack), ts_stack_top_state(self->stack, head), position.chars); - if (position.chars == last_position.chars && - ts_parser__can_reuse(self, head, lookahead)) { - ts_tree_retain(lookahead); - } else { + if (position.chars != last_position.chars || + !ts_parser__can_reuse(self, head, lookahead)) { ts_tree_release(lookahead); lookahead = ts_parser__get_next_lookahead(self, head); if (!lookahead) diff --git a/src/runtime/vector.h b/src/runtime/vector.h index 6bc2ed20..4c33c9d8 100644 --- a/src/runtime/vector.h +++ b/src/runtime/vector.h @@ -28,6 +28,9 @@ static inline Vector vector_new(size_t element_size) { } static inline bool vector_grow(Vector *self, size_t capacity) { + if (capacity == 0) + return true; + void *new_contents; if (self->contents) new_contents = ts_realloc(self->contents, capacity * self->element_size);