refactor!: expose the allocator family of functions for consumption in scanners

This commit is contained in:
Amaan Qureshi 2024-02-05 19:51:19 -05:00
parent e6d67ecde0
commit b66b1a7a92
No known key found for this signature in database
GPG key ID: E67890ADC4227273
2 changed files with 33 additions and 4 deletions

View file

@ -1,6 +1,12 @@
#include "alloc.h"
#include <stdlib.h>
#ifdef _WIN32
#define PUBLIC __declspec(dllexport)
#else
#define PUBLIC __attribute__((visibility("default")))
#endif
static void *ts_malloc_default(size_t size) {
void *result = malloc(size);
if (size > 0 && !result) {
@ -29,10 +35,10 @@ static void *ts_realloc_default(void *buffer, size_t size) {
}
// 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 *) = free;
PUBLIC void *(*ts_current_malloc)(size_t) = ts_malloc_default;
PUBLIC void *(*ts_current_calloc)(size_t, size_t) = ts_calloc_default;
PUBLIC void *(*ts_current_realloc)(void *, size_t) = ts_realloc_default;
PUBLIC void (*ts_current_free)(void *) = free;
void ts_set_allocator(
void *(*new_malloc)(size_t size),

View file

@ -20,6 +20,29 @@ typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage;
#endif
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 consumers to use allocation functions
#ifndef ts_malloc
#define ts_malloc ts_current_malloc
#define malloc ts_current_malloc
#endif
#ifndef ts_calloc
#define ts_calloc ts_current_calloc
#define calloc ts_current_calloc
#endif
#ifndef ts_realloc
#define ts_realloc ts_current_realloc
#define realloc ts_current_realloc
#endif
#ifndef ts_free
#define ts_free ts_current_free
#define free ts_current_free
#endif
typedef struct {
TSFieldId field_id;
uint8_t child_index;