Merge pull request #1544 from mkvoya/dynamic-allocator
Allow to change the allocator dynamically
This commit is contained in:
commit
e01ea9ff51
4 changed files with 104 additions and 59 deletions
76
lib/src/alloc.c
Normal file
76
lib/src/alloc.c
Normal 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;
|
||||
}
|
||||
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#define _POSIX_C_SOURCE 200112L
|
||||
|
||||
#include "./alloc.c"
|
||||
#include "./get_changed_ranges.c"
|
||||
#include "./language.c"
|
||||
#include "./lexer.c"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue