diff --git a/lib/include/tree_sitter/api.h b/lib/include/tree_sitter/api.h index 66ceaea1..4d558a5c 100644 --- a/lib/include/tree_sitter/api.h +++ b/lib/include/tree_sitter/api.h @@ -138,6 +138,22 @@ typedef enum { /* Section - Parser */ /********************/ +/** + * Switch to a new allocator. + * + * 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. + */ +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. */ diff --git a/lib/src/alloc.c b/lib/src/alloc.c new file mode 100644 index 00000000..9c0458ff --- /dev/null +++ b/lib/src/alloc.c @@ -0,0 +1,76 @@ +#include "alloc.h" + +#if defined(TREE_SITTER_ALLOCATION_TRACKING) + +void *ts_record_malloc(size_t); +void *ts_record_calloc(size_t, size_t); +void *ts_record_realloc(void *, size_t); +void ts_record_free(void *); +bool ts_toggle_allocation_recording(bool); + +void *(*ts_current_malloc)(size_t) = ts_record_malloc; +void *(*ts_current_calloc)(size_t, size_t) = ts_record_calloc; +void *(*ts_current_realloc)(void *, size_t) = ts_record_realloc; +void (*ts_current_free)(void *) = ts_record_free; + +#else + +#include + +bool ts_toggle_allocation_recording(bool value) { + (void)value; + return false; +} + + +static inline void *ts_malloc_default(size_t size) { + void *result = malloc(size); + if (size > 0 && !result) { + fprintf(stderr, "tree-sitter failed to allocate %zu bytes", size); + exit(1); + } + return result; +} + +static inline void *ts_calloc_default(size_t count, size_t size) { + void *result = calloc(count, size); + if (count > 0 && !result) { + fprintf(stderr, "tree-sitter failed to allocate %zu bytes", count * size); + exit(1); + } + return result; +} + +static inline void *ts_realloc_default(void *buffer, size_t size) { + void *result = realloc(buffer, size); + if (size > 0 && !result) { + fprintf(stderr, "tree-sitter failed to reallocate %zu bytes", size); + exit(1); + } + return result; +} + +static inline void ts_free_default(void *buffer) { + free(buffer); +} + + +// Allow clients to override allocation functions dynamically +void *(*ts_current_malloc)(size_t) = ts_malloc_default; +void *(*ts_current_calloc)(size_t, size_t) = ts_calloc_default; +void *(*ts_current_realloc)(void *, size_t) = ts_realloc_default; +void (*ts_current_free)(void *) = ts_free_default; + +#endif // defined(TREE_SITTER_ALLOCATION_TRACKING) + +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 *)) +{ + ts_current_malloc = new_malloc; + ts_current_calloc = new_calloc; + ts_current_realloc = new_realloc; + ts_current_free = new_free; +} + diff --git a/lib/src/alloc.h b/lib/src/alloc.h index dd487ca2..faf7c4ef 100644 --- a/lib/src/alloc.h +++ b/lib/src/alloc.h @@ -1,6 +1,8 @@ #ifndef TREE_SITTER_ALLOC_H_ #define TREE_SITTER_ALLOC_H_ +#include "tree_sitter/api.h" + #ifdef __cplusplus extern "C" { #endif @@ -9,76 +11,26 @@ extern "C" { #include #include -#if defined(TREE_SITTER_ALLOCATION_TRACKING) - -void *ts_record_malloc(size_t); -void *ts_record_calloc(size_t, size_t); -void *ts_record_realloc(void *, size_t); -void ts_record_free(void *); -bool ts_toggle_allocation_recording(bool); - -#define ts_malloc ts_record_malloc -#define ts_calloc ts_record_calloc -#define ts_realloc ts_record_realloc -#define ts_free ts_record_free - -#else +extern void *(*ts_current_malloc)(size_t); +extern void *(*ts_current_calloc)(size_t, size_t); +extern void *(*ts_current_realloc)(void *, size_t); +extern void (*ts_current_free)(void *); // Allow clients to override allocation functions - #ifndef ts_malloc -#define ts_malloc ts_malloc_default +#define ts_malloc ts_current_malloc #endif #ifndef ts_calloc -#define ts_calloc ts_calloc_default +#define ts_calloc ts_current_calloc #endif #ifndef ts_realloc -#define ts_realloc ts_realloc_default +#define ts_realloc ts_current_realloc #endif #ifndef ts_free -#define ts_free ts_free_default +#define ts_free ts_current_free #endif -#include - -static inline bool ts_toggle_allocation_recording(bool value) { - (void)value; - return false; -} - - -static inline void *ts_malloc_default(size_t size) { - void *result = malloc(size); - if (size > 0 && !result) { - fprintf(stderr, "tree-sitter failed to allocate %zu bytes", size); - exit(1); - } - return result; -} - -static inline void *ts_calloc_default(size_t count, size_t size) { - void *result = calloc(count, size); - if (count > 0 && !result) { - fprintf(stderr, "tree-sitter failed to allocate %zu bytes", count * size); - exit(1); - } - return result; -} - -static inline void *ts_realloc_default(void *buffer, size_t size) { - void *result = realloc(buffer, size); - if (size > 0 && !result) { - fprintf(stderr, "tree-sitter failed to reallocate %zu bytes", size); - exit(1); - } - return result; -} - -static inline void ts_free_default(void *buffer) { - free(buffer); -} - -#endif +bool ts_toggle_allocation_recording(bool); #ifdef __cplusplus } diff --git a/lib/src/lib.c b/lib/src/lib.c index 289d32f4..5aab20d5 100644 --- a/lib/src/lib.c +++ b/lib/src/lib.c @@ -5,6 +5,7 @@ #define _POSIX_C_SOURCE 200112L +#include "./alloc.c" #include "./get_changed_ranges.c" #include "./language.c" #include "./lexer.c"