From 3859e52198468c6328cb7508f747f51b4aef13be Mon Sep 17 00:00:00 2001 From: ikrima Date: Fri, 16 Oct 2020 12:42:26 -0700 Subject: [PATCH 1/4] add custom allocation override --- lib/src/alloc.h | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/src/alloc.h b/lib/src/alloc.h index 0e0927a9..52e5ad3d 100644 --- a/lib/src/alloc.h +++ b/lib/src/alloc.h @@ -42,7 +42,20 @@ static inline bool ts_toggle_allocation_recording(bool value) { return false; } -static inline void *ts_malloc(size_t size) { +#ifndef ts_malloc +#define ts_malloc(_sz) ts_malloc_dflt(_sz) +#endif +#ifndef ts_calloc +#define ts_calloc(_cnt,_sz) ts_calloc_dflt(_cnt,_sz) +#endif +#ifndef ts_realloc +#define ts_realloc(_ptr,_sz) ts_realloc_dflt(_ptr,_sz) +#endif +#ifndef ts_free +#define ts_free(_ptr) ts_free_dflt(_ptr) +#endif + +static inline void *ts_malloc_dflt(size_t size) { void *result = malloc(size); if (size > 0 && !result) { fprintf(stderr, "tree-sitter failed to allocate %zu bytes", size); @@ -51,7 +64,7 @@ static inline void *ts_malloc(size_t size) { return result; } -static inline void *ts_calloc(size_t count, size_t size) { +static inline void *ts_calloc_dflt(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); @@ -60,7 +73,7 @@ static inline void *ts_calloc(size_t count, size_t size) { return result; } -static inline void *ts_realloc(void *buffer, size_t size) { +static inline void *ts_realloc_dflt(void *buffer, size_t size) { void *result = realloc(buffer, size); if (size > 0 && !result) { fprintf(stderr, "tree-sitter failed to reallocate %zu bytes", size); @@ -69,7 +82,7 @@ static inline void *ts_realloc(void *buffer, size_t size) { return result; } -static inline void ts_free(void *buffer) { +static inline void ts_free_dflt(void *buffer) { free(buffer); } From 336517fdc92fbc8ffcba199e3a4cd55e1c516181 Mon Sep 17 00:00:00 2001 From: ikrima Date: Fri, 16 Oct 2020 15:18:54 -0700 Subject: [PATCH 2/4] address CR comments - replace _dflt with _default - allow override in TREE_SITTER_TEST path --- lib/src/alloc.h | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/lib/src/alloc.h b/lib/src/alloc.h index 52e5ad3d..c6a3331b 100644 --- a/lib/src/alloc.h +++ b/lib/src/alloc.h @@ -9,6 +9,21 @@ extern "C" { #include #include +// Allow clients to override allocation functions + +#ifndef ts_malloc +#define ts_malloc(size) ts_malloc_default(size) +#endif +#ifndef ts_calloc +#define ts_calloc(count,size) ts_calloc_default(count,size) +#endif +#ifndef ts_realloc +#define ts_realloc(buffer,size) ts_realloc_default(buffer,size) +#endif +#ifndef ts_free +#define ts_free(buffer) ts_free_default(buffer) +#endif + #if defined(TREE_SITTER_TEST) void *ts_record_malloc(size_t); @@ -17,19 +32,19 @@ void *ts_record_realloc(void *, size_t); void ts_record_free(void *); bool ts_toggle_allocation_recording(bool); -static inline void *ts_malloc(size_t size) { +static inline void *ts_malloc_default(size_t size) { return ts_record_malloc(size); } -static inline void *ts_calloc(size_t count, size_t size) { +static inline void *ts_calloc_default(size_t count, size_t size) { return ts_record_calloc(count, size); } -static inline void *ts_realloc(void *buffer, size_t size) { +static inline void *ts_realloc_default(void *buffer, size_t size) { return ts_record_realloc(buffer, size); } -static inline void ts_free(void *buffer) { +static inline void ts_free_default(void *buffer) { ts_record_free(buffer); } @@ -42,20 +57,8 @@ static inline bool ts_toggle_allocation_recording(bool value) { return false; } -#ifndef ts_malloc -#define ts_malloc(_sz) ts_malloc_dflt(_sz) -#endif -#ifndef ts_calloc -#define ts_calloc(_cnt,_sz) ts_calloc_dflt(_cnt,_sz) -#endif -#ifndef ts_realloc -#define ts_realloc(_ptr,_sz) ts_realloc_dflt(_ptr,_sz) -#endif -#ifndef ts_free -#define ts_free(_ptr) ts_free_dflt(_ptr) -#endif -static inline void *ts_malloc_dflt(size_t size) { +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); @@ -64,7 +67,7 @@ static inline void *ts_malloc_dflt(size_t size) { return result; } -static inline void *ts_calloc_dflt(size_t count, size_t size) { +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); @@ -73,7 +76,7 @@ static inline void *ts_calloc_dflt(size_t count, size_t size) { return result; } -static inline void *ts_realloc_dflt(void *buffer, size_t size) { +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); @@ -82,7 +85,7 @@ static inline void *ts_realloc_dflt(void *buffer, size_t size) { return result; } -static inline void ts_free_dflt(void *buffer) { +static inline void ts_free_default(void *buffer) { free(buffer); } From 23530ca599758a4d1d4c1393238b74830256e2db Mon Sep 17 00:00:00 2001 From: ikrima Date: Thu, 29 Oct 2020 09:23:58 -0700 Subject: [PATCH 3/4] CR fixes: don't allow override of allocfn during testing --- lib/src/alloc.h | 48 ++++++++++++++++++------------------------------ 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/lib/src/alloc.h b/lib/src/alloc.h index c6a3331b..cbedb71b 100644 --- a/lib/src/alloc.h +++ b/lib/src/alloc.h @@ -9,21 +9,6 @@ extern "C" { #include #include -// Allow clients to override allocation functions - -#ifndef ts_malloc -#define ts_malloc(size) ts_malloc_default(size) -#endif -#ifndef ts_calloc -#define ts_calloc(count,size) ts_calloc_default(count,size) -#endif -#ifndef ts_realloc -#define ts_realloc(buffer,size) ts_realloc_default(buffer,size) -#endif -#ifndef ts_free -#define ts_free(buffer) ts_free_default(buffer) -#endif - #if defined(TREE_SITTER_TEST) void *ts_record_malloc(size_t); @@ -32,24 +17,27 @@ void *ts_record_realloc(void *, size_t); void ts_record_free(void *); bool ts_toggle_allocation_recording(bool); -static inline void *ts_malloc_default(size_t size) { - return ts_record_malloc(size); -} - -static inline void *ts_calloc_default(size_t count, size_t size) { - return ts_record_calloc(count, size); -} - -static inline void *ts_realloc_default(void *buffer, size_t size) { - return ts_record_realloc(buffer, size); -} - -static inline void ts_free_default(void *buffer) { - ts_record_free(buffer); -} +#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 +// Allow clients to override allocation functions +#ifndef ts_malloc +#define ts_malloc ts_malloc_default +#endif +#ifndef ts_calloc +#define ts_calloc ts_calloc_default +#endif +#ifndef ts_realloc +#define ts_realloc ts_realloc_default +#endif +#ifndef ts_free +#define ts_free ts_free_default +#endif + #include static inline bool ts_toggle_allocation_recording(bool value) { From a99676282f1f18c8187bb02171ad1f261ea3c9ca Mon Sep 17 00:00:00 2001 From: ikrima Date: Thu, 29 Oct 2020 09:36:44 -0700 Subject: [PATCH 4/4] noop: touch file to retrigger github checks --- lib/src/alloc.h | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/src/alloc.h b/lib/src/alloc.h index cbedb71b..6e22a0ab 100644 --- a/lib/src/alloc.h +++ b/lib/src/alloc.h @@ -25,6 +25,7 @@ bool ts_toggle_allocation_recording(bool); #else // Allow clients to override allocation functions + #ifndef ts_malloc #define ts_malloc ts_malloc_default #endif