Merge pull request #1544 from mkvoya/dynamic-allocator

Allow to change the allocator dynamically
This commit is contained in:
Max Brunsfeld 2021-12-28 13:39:11 -08:00 committed by GitHub
commit e01ea9ff51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 104 additions and 59 deletions

View file

@ -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.
*/

76
lib/src/alloc.c Normal file
View file

@ -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 <stdlib.h>
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;
}

View file

@ -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 <stdbool.h>
#include <stdio.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);
#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 <stdlib.h>
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
}

View file

@ -5,6 +5,7 @@
#define _POSIX_C_SOURCE 200112L
#include "./alloc.c"
#include "./get_changed_ranges.c"
#include "./language.c"
#include "./lexer.c"