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