From 006a931ab8967f6c9c89f668d4608ec119065b24 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 14 Mar 2019 13:59:09 -0700 Subject: [PATCH] Tests: Prevent array reallocations during ts_stack_print_dot_graph When debugging a test with 'script/test -D', the DOT-graph generation code was sometimes causing reallocations that were not captured by the allocation tracker, because we explicitly disable allocation-tracking for that method in order to reduce noise when debugging memory leaks. By growing the relevant array *prior* to turning off allocation tracking, we can ensure that it is not reallocated within that function, avoiding false positive memory leak errors. Fixes #302 --- cli/src/tests/helpers/allocations.rs | 6 ++++-- lib/src/alloc.h | 6 +----- lib/src/stack.c | 4 ++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/cli/src/tests/helpers/allocations.rs b/cli/src/tests/helpers/allocations.rs index ae246c40..c64762bd 100644 --- a/cli/src/tests/helpers/allocations.rs +++ b/cli/src/tests/helpers/allocations.rs @@ -98,7 +98,9 @@ extern "C" fn ts_record_free(ptr: *mut c_void) { } #[no_mangle] -extern "C" fn ts_record_allocations_toggle() { +extern "C" fn ts_toggle_allocation_recording(enabled: bool) -> bool { let mut recorder = RECORDER.lock(); - recorder.enabled = !recorder.enabled; + let was_enabled = recorder.enabled; + recorder.enabled = enabled; + was_enabled } diff --git a/lib/src/alloc.h b/lib/src/alloc.h index 8e027a99..c8fe6c6e 100644 --- a/lib/src/alloc.h +++ b/lib/src/alloc.h @@ -15,7 +15,7 @@ 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_record_allocations_toggle(bool); +bool ts_toggle_allocation_recording(bool); static inline void *ts_malloc(size_t size) { return ts_record_malloc(size); @@ -33,10 +33,6 @@ static inline void ts_free(void *buffer) { ts_record_free(buffer); } -static inline bool ts_toggle_allocation_recording(bool value) { - return ts_record_allocations_toggle(value); -} - #else #include diff --git a/lib/src/stack.c b/lib/src/stack.c index e3a1f22d..9e351d4e 100644 --- a/lib/src/stack.c +++ b/lib/src/stack.c @@ -712,9 +712,9 @@ void ts_stack_clear(Stack *self) { } bool ts_stack_print_dot_graph(Stack *self, const TSLanguage *language, FILE *f) { + array_reserve(&self->iterators, 32); bool was_recording_allocations = ts_toggle_allocation_recording(false); - if (!f) - f = stderr; + if (!f) f = stderr; fprintf(f, "digraph stack {\n"); fprintf(f, "rankdir=\"RL\";\n");