Skip to content

Commit 7c08f3d

Browse files
committed
Add a C API to query MMTk plan name.
1 parent bccd11e commit 7c08f3d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

mmtk/src/api.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ pub extern "C" fn mmtk_enable_collection() {
9797
memory_manager::enable_collection(&SINGLETON)
9898
}
9999

100+
#[no_mangle]
101+
pub extern "C" fn mmtk_plan_name() -> *const libc::c_char {
102+
crate::binding().get_plan_name_c()
103+
}
104+
100105
#[no_mangle]
101106
pub extern "C" fn mmtk_used_bytes() -> usize {
102107
memory_manager::used_bytes(&SINGLETON)

mmtk/src/binding.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
use std::ffi::CString;
12
use std::ptr::null;
3+
use std::sync::Mutex;
24

5+
use crate::SINGLETON;
36
use crate::abi;
47
use crate::finalize;
58

69
pub struct RubyBinding {
710
pub upcalls: *const abi::RubyUpcalls,
811
pub finalizer_processor: finalize::FinalizerProcessor,
12+
pub plan_name: Mutex<Option<CString>>,
913
}
1014

1115
unsafe impl Sync for RubyBinding {}
@@ -15,6 +19,7 @@ impl RubyBinding {
1519
Self {
1620
upcalls: null(),
1721
finalizer_processor: finalize::FinalizerProcessor::new(),
22+
plan_name: Mutex::new(None),
1823
}
1924
}
2025

@@ -25,4 +30,17 @@ impl RubyBinding {
2530
pub fn upcalls(&self) -> &'static abi::RubyUpcalls {
2631
unsafe { &*self.upcalls as &'static abi::RubyUpcalls }
2732
}
33+
34+
pub fn get_plan_name_c(&self) -> *const libc::c_char {
35+
let mut plan_name = self.plan_name.lock().unwrap();
36+
if plan_name.is_none() {
37+
let name_string = format!("{:?}", SINGLETON.get_options().plan.value);
38+
let c_string = CString::new(name_string).unwrap_or_else(|e| {
39+
panic!("Failed converting plan name to CString: {}",
40+
e)
41+
});
42+
*plan_name = Some(c_string);
43+
}
44+
plan_name.as_deref().unwrap().as_ptr()
45+
}
2846
}

0 commit comments

Comments
 (0)