Avoid allocator from being switched more than once
This commit is contained in:
parent
b9b051e933
commit
486ea2569d
2 changed files with 17 additions and 7 deletions
|
|
@ -148,10 +148,15 @@ typedef struct {
|
|||
/**
|
||||
* Switch to a new allocator.
|
||||
*
|
||||
* Returns the old allocator. The caller needs to take care of any allocated
|
||||
* memory managed by the old allocator.
|
||||
* Returns true iff the switch successes.
|
||||
*
|
||||
* This function can only be invoked *once*, before tree-sitter has allocated
|
||||
* any memory via malloc/calloc/realloc. Otherwise, memory bugs could occur
|
||||
* since some memory is allocated by the old allocator, but freed by the new
|
||||
* one.
|
||||
*
|
||||
*/
|
||||
TSAllocator *ts_set_allocator(TSAllocator *new_alloc);
|
||||
bool ts_set_allocator(TSAllocator *new_alloc);
|
||||
|
||||
/**
|
||||
* Create a new parser.
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue