diff options
Diffstat (limited to 'vendor/tree-sitter/lib/src/alloc.c')
| -rw-r--r-- | vendor/tree-sitter/lib/src/alloc.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/vendor/tree-sitter/lib/src/alloc.c b/vendor/tree-sitter/lib/src/alloc.c new file mode 100644 index 0000000..78b8057 --- /dev/null +++ b/vendor/tree-sitter/lib/src/alloc.c | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #include "alloc.h" | ||
| 2 | #include <stdlib.h> | ||
| 3 | |||
| 4 | static void *ts_malloc_default(size_t size) { | ||
| 5 | void *result = malloc(size); | ||
| 6 | if (size > 0 && !result) { | ||
| 7 | fprintf(stderr, "tree-sitter failed to allocate %zu bytes", size); | ||
| 8 | abort(); | ||
| 9 | } | ||
| 10 | return result; | ||
| 11 | } | ||
| 12 | |||
| 13 | static void *ts_calloc_default(size_t count, size_t size) { | ||
| 14 | void *result = calloc(count, size); | ||
| 15 | if (count > 0 && !result) { | ||
| 16 | fprintf(stderr, "tree-sitter failed to allocate %zu bytes", count * size); | ||
| 17 | abort(); | ||
| 18 | } | ||
| 19 | return result; | ||
| 20 | } | ||
| 21 | |||
| 22 | static void *ts_realloc_default(void *buffer, size_t size) { | ||
| 23 | void *result = realloc(buffer, size); | ||
| 24 | if (size > 0 && !result) { | ||
| 25 | fprintf(stderr, "tree-sitter failed to reallocate %zu bytes", size); | ||
| 26 | abort(); | ||
| 27 | } | ||
| 28 | return result; | ||
| 29 | } | ||
| 30 | |||
| 31 | // Allow clients to override allocation functions dynamically | ||
| 32 | void *(*ts_current_malloc)(size_t) = ts_malloc_default; | ||
| 33 | void *(*ts_current_calloc)(size_t, size_t) = ts_calloc_default; | ||
| 34 | void *(*ts_current_realloc)(void *, size_t) = ts_realloc_default; | ||
| 35 | void (*ts_current_free)(void *) = free; | ||
| 36 | |||
| 37 | void ts_set_allocator( | ||
| 38 | void *(*new_malloc)(size_t size), | ||
| 39 | void *(*new_calloc)(size_t count, size_t size), | ||
| 40 | void *(*new_realloc)(void *ptr, size_t size), | ||
| 41 | void (*new_free)(void *ptr) | ||
| 42 | ) { | ||
| 43 | ts_current_malloc = new_malloc ? new_malloc : ts_malloc_default; | ||
| 44 | ts_current_calloc = new_calloc ? new_calloc : ts_calloc_default; | ||
| 45 | ts_current_realloc = new_realloc ? new_realloc : ts_realloc_default; | ||
| 46 | ts_current_free = new_free ? new_free : free; | ||
| 47 | } | ||
| 48 | |||
