Simplify allocation-recording in test suite using new ts_set_allocator API

This commit is contained in:
Max Brunsfeld 2021-12-30 16:09:07 -08:00
parent e01ea9ff51
commit 622359b400
19 changed files with 245 additions and 254 deletions

View file

@ -1,19 +1,6 @@
use super::FREE_FN;
use std::os::raw::c_void;
#[cfg(not(feature = "allocation-tracking"))]
extern "C" {
/// Normally, use `free(1)` to free memory allocated from C.
#[link_name = "free"]
pub fn free_ptr(ptr: *mut c_void);
}
/// When the `allocation-tracking` feature is enabled, the C library is compiled with
/// the `TREE_SITTER_TEST` macro, so all calls to `malloc`, `free`, etc are linked
/// against wrapper functions called `ts_record_malloc`, `ts_record_free`, etc.
/// When freeing buffers allocated from C, use the wrapper `free` function.
#[cfg(feature = "allocation-tracking")]
pub use crate::allocations::ts_record_free as free_ptr;
/// A raw pointer and a length, exposed as an iterator.
pub struct CBufferIter<T> {
ptr: *mut T,
@ -50,8 +37,6 @@ impl<T: Copy> ExactSizeIterator for CBufferIter<T> {}
impl<T> Drop for CBufferIter<T> {
fn drop(&mut self) {
unsafe {
free_ptr(self.ptr as *mut c_void);
}
unsafe { (FREE_FN)(self.ptr as *mut c_void) };
}
}