tree-sitter/lib/binding_rust/util.rs
Douglas Creager a29c8d9264 Move allocation tracking into lib crate
We have several test cases defined in the `cli` crate that depend on the
`lib` crate's `allocation-tracking` feature.  The implementation of the
actual allocation tracker used to live in the `cli` crate, close to the
test cases that use it.  The `allocation-tracking` feature in the `lib`
crate was just used to tell the tree-sitter implementation to expect
that the allocation tracker exists, and to use it.

That pattern meant that we had a circular dependency: `cli` depends on
`lib`, but `lib` required some code that was implemented in `cli`.
That, in turn, caused linker errors — but only when compiling in certain
configurations! [1]

This patch moves all of the allocation tracking implementation into the
`lib` crate, gated on the existing `allocation-tracking` feature, which
fixes the circular dependency.

Note that this patch does **not** fix the fact that feature unification
causes the `lib` crate to be built with the `allocation-tracking`
feature enabled, even though it's not a default.  Fixing that depends on
the forthcoming version 2 feature resolver [2], or using the `dev_dep`
workaround [3] in the meantime.

[1] https://github.com/tree-sitter/tree-sitter/issues/919
[2] https://doc.rust-lang.org/nightly/cargo/reference/features.html#feature-resolver-version-2
[3] https://github.com/tree-sitter/tree-sitter/issues/919#issuecomment-777107086
2021-02-23 09:16:37 -05:00

57 lines
1.5 KiB
Rust

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,
count: usize,
i: usize,
}
impl<T> CBufferIter<T> {
pub unsafe fn new(ptr: *mut T, count: usize) -> Self {
Self { ptr, count, i: 0 }
}
}
impl<T: Copy> Iterator for CBufferIter<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
let i = self.i;
if i >= self.count {
None
} else {
self.i += 1;
Some(unsafe { *self.ptr.offset(i as isize) })
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.count - self.i;
(remaining, Some(remaining))
}
}
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);
}
}
}