Replace allocator struct with function pointers

This commit is contained in:
Mingkai Dong 2021-12-24 09:28:23 +08:00
parent 486ea2569d
commit 8e4d4ef8b9
3 changed files with 34 additions and 43 deletions

View file

@ -134,13 +134,6 @@ typedef enum {
TSQueryErrorLanguage,
} TSQueryError;
typedef struct {
void *(*malloc)(size_t);
void *(*calloc)(size_t, size_t);
void *(*realloc)(void *, size_t);
void (*free)(void *);
} TSAllocator;
/********************/
/* Section - Parser */
/********************/
@ -148,15 +141,18 @@ typedef struct {
/**
* Switch to a new 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.
* This function can be invoked more than once. However, the application needs
* to pay attention to the memory allocated by the old allocator but might be
* freed by the new one.
*
* Specifically, the application either,
* 1. ensures all parsers and trees are freed before calling it;
* 2. provides an allocator that shares its state with the old allocator.
*/
bool ts_set_allocator(TSAllocator *new_alloc);
void ts_set_allocator(void *(*new_malloc)(size_t),
void *(*new_calloc)(size_t, size_t),
void *(*new_realloc)(void *, size_t),
void (*new_free)(void *));
/**
* Create a new parser.