Allocate and free trees using an object pool

This commit is contained in:
Max Brunsfeld 2017-10-05 17:32:21 -07:00
parent 53152658ce
commit addeb6c4c1
11 changed files with 287 additions and 230 deletions

View file

@ -8,23 +8,17 @@
TSDocument *ts_document_new() {
TSDocument *self = ts_calloc(1, sizeof(TSDocument));
if (!self)
goto error;
if (!parser_init(&self->parser))
goto error;
parser_init(&self->parser);
array_init(&self->tree_path1);
array_init(&self->tree_path2);
return self;
error:
if (self)
ts_free(self);
return NULL;
}
void ts_document_free(TSDocument *self) {
if (self->tree) ts_tree_release(&self->parser.tree_pool, self->tree);
if (self->tree_path1.contents) array_delete(&self->tree_path1);
if (self->tree_path2.contents) array_delete(&self->tree_path2);
parser_destroy(&self->parser);
if (self->tree) ts_tree_release(self->tree);
ts_document_set_input(self, (TSInput){
NULL,
NULL,
@ -43,7 +37,7 @@ void ts_document_set_language(TSDocument *self, const TSLanguage *language) {
ts_document_invalidate(self);
parser_set_language(&self->parser, language);
if (self->tree) {
ts_tree_release(self->tree);
ts_tree_release(&self->parser.tree_pool, self->tree);
self->tree = NULL;
}
}
@ -140,12 +134,12 @@ void ts_document_parse_with_options(TSDocument *self, TSParseOptions options) {
if (options.changed_ranges && options.changed_range_count) {
*options.changed_range_count = ts_tree_get_changed_ranges(
old_tree, tree, &self->parser.tree_path1, &self->parser.tree_path2,
old_tree, tree, &self->tree_path1, &self->tree_path2,
self->parser.language, options.changed_ranges
);
}
ts_tree_release(old_tree);
ts_tree_release(&self->parser.tree_pool, old_tree);
}
self->tree = tree;