Avoid allocator from being switched more than once

This commit is contained in:
Mingkai Dong 2021-12-18 16:45:18 +08:00
parent b9b051e933
commit 486ea2569d
2 changed files with 17 additions and 7 deletions

View file

@ -70,10 +70,15 @@ TSAllocator *ts_allocator = &ts_default_allocator;
#endif // defined(TREE_SITTER_ALLOCATION_TRACKING)
TSAllocator *ts_set_allocator(TSAllocator *new_alloc)
bool ts_set_allocator(TSAllocator *new_alloc)
{
TSAllocator *old = ts_allocator;
ts_allocator = new_alloc;
return old;
static bool using_default_allocator = true;
if (!using_default_allocator) {
fprintf(stderr, "tree-sitter's allocator can only be set once!");
return false;
}
using_default_allocator = false;
ts_allocator = new_alloc;
return true;
}