Simplify setup for enabling/disabling allocation recording in the C lib

This commit is contained in:
Max Brunsfeld 2020-12-02 13:17:13 -08:00
parent 00d25e8298
commit b661050a61
10 changed files with 72 additions and 64 deletions

View file

@ -21,9 +21,9 @@ fn main() {
let mut config = cc::Build::new();
println!("cargo:rerun-if-env-changed=PROFILE");
if env::var("PROFILE").map_or(false, |s| s == "debug") {
config.define("TREE_SITTER_TEST", "");
println!("cargo:rerun-if-env-changed=CARGO_FEATURE_ALLOCATION_TRACKING");
if env::var("CARGO_FEATURE_ALLOCATION_TRACKING").is_ok() {
config.define("TREE_SITTER_ALLOCATION_TRACKING", "");
}
let src_path = Path::new("src");

View file

@ -1,71 +1,22 @@
use std::os::raw::c_void;
extern "C" {
/// In *Release* builds, the C library links directly against `malloc` and `free`.
///
/// When freeing memory that was allocated by C code, use `free` directly.
#[cfg(not(debug_assertions))]
/// Normally, use `free(1)` to free memory allocated from C.
#[cfg(not(feature = "allocation-tracking"))]
#[link_name = "free"]
pub fn free_ptr(ptr: *mut c_void);
/// In *Test* builds, 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. These symbols are defined
/// in the `tree_sitter_cli::tests::helpers::allocations` module.
///
/// When freeing memory that was allocated by C code, use the `free` function
/// from that module.
#[cfg(debug_assertions)]
/// 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")]
#[link_name = "ts_record_free"]
pub fn free_ptr(ptr: *mut c_void);
/// In *Debug* builds, the C library is compiled the same as in test builds: using
/// the wrapper functions. This prevents the C library from having to be recompiled
/// constantly when switching between running tests and compiling with RLS.
///
/// But we don't want to actually record allocations when running the library in
/// debug mode, so we define symbols like `ts_record_malloc` to just delegate to
/// the normal `malloc` functions.
#[cfg(all(debug_assertions, not(test)))]
fn malloc(size: usize) -> *mut c_void;
#[cfg(all(debug_assertions, not(test)))]
fn calloc(count: usize, size: usize) -> *mut c_void;
#[cfg(all(debug_assertions, not(test)))]
fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void;
#[cfg(all(debug_assertions, not(test)))]
fn free(ptr: *mut c_void);
}
#[cfg(all(debug_assertions, not(test)))]
#[no_mangle]
unsafe extern "C" fn ts_record_malloc(size: usize) -> *const c_void {
malloc(size)
}
#[cfg(all(debug_assertions, not(test)))]
#[no_mangle]
unsafe extern "C" fn ts_record_calloc(count: usize, size: usize) -> *const c_void {
calloc(count, size)
}
#[cfg(all(debug_assertions, not(test)))]
#[no_mangle]
unsafe extern "C" fn ts_record_realloc(ptr: *mut c_void, size: usize) -> *const c_void {
realloc(ptr, size)
}
#[cfg(all(debug_assertions, not(test)))]
#[no_mangle]
unsafe extern "C" fn ts_record_free(ptr: *mut c_void) {
free(ptr)
}
#[cfg(all(debug_assertions, not(test)))]
#[no_mangle]
extern "C" fn ts_toggle_allocation_recording(_: bool) -> bool {
false
}
/// A raw pointer and a length, exposed as an iterator.
pub struct CBufferIter<T> {
ptr: *mut T,
count: usize,