Simplify setup for enabling/disabling allocation recording in the C lib
This commit is contained in:
parent
00d25e8298
commit
b661050a61
10 changed files with 72 additions and 64 deletions
40
cli/src/allocations_stubs.rs
Normal file
40
cli/src/allocations_stubs.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// In all dev builds, the tree-sitter library is built with the `allocation-tracking`
|
||||
// feature enabled. This causes the library to link against a set of externally
|
||||
// defined C functions like `ts_record_malloc` and `ts_record_free`. In tests, these
|
||||
// are defined to actually keep track of outstanding allocations. But when not running
|
||||
// tests, the symbols still need to be defined. This file provides pass-through
|
||||
// implementations of all of these functions.
|
||||
|
||||
use std::os::raw::c_void;
|
||||
|
||||
extern "C" {
|
||||
fn malloc(size: usize) -> *mut c_void;
|
||||
fn calloc(count: usize, size: usize) -> *mut c_void;
|
||||
fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void;
|
||||
fn free(ptr: *mut c_void);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
unsafe extern "C" fn ts_record_malloc(size: usize) -> *const c_void {
|
||||
malloc(size)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
unsafe extern "C" fn ts_record_calloc(count: usize, size: usize) -> *const c_void {
|
||||
calloc(count, size)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
unsafe extern "C" fn ts_record_realloc(ptr: *mut c_void, size: usize) -> *const c_void {
|
||||
realloc(ptr, size)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
unsafe extern "C" fn ts_record_free(ptr: *mut c_void) {
|
||||
free(ptr)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
extern "C" fn ts_toggle_allocation_recording(_: bool) -> bool {
|
||||
false
|
||||
}
|
||||
|
|
@ -16,3 +16,6 @@ pub mod web_ui;
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[cfg(not(test))]
|
||||
mod allocations_stubs;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
use lazy_static::lazy_static;
|
||||
use spin::Mutex;
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use std::os::raw::{c_ulong, c_void};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
|
|
@ -31,9 +32,14 @@ extern "C" {
|
|||
|
||||
pub fn start_recording() {
|
||||
let mut recorder = RECORDER.lock();
|
||||
recorder.enabled = true;
|
||||
recorder.allocation_count = 0;
|
||||
recorder.outstanding_allocations.clear();
|
||||
|
||||
if env::var("RUST_TEST_THREADS").map_or(false, |s| s == "1") {
|
||||
recorder.enabled = true;
|
||||
} else {
|
||||
panic!("This test must be run with RUST_TEST_THREADS=1. Use script/test.");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stop_recording() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue