Skip to content

Commit 569997c

Browse files
committed
WIP: Strongly-typed MMTk options
After the MMTKBuilder refactoring in mmtk-core, mmtk-ruby now has access to the strongly-typed Options struct. We no longer have to use environment variables or any string-parsing API for setting things like the plan and the number of MMTk threads.
1 parent 320a3cd commit 569997c

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

mmtk/src/api.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,51 @@
11
// All functions here are extern function. There is no point for marking them as unsafe.
22
#![allow(clippy::not_unsafe_ptr_arg_deref)]
33

4+
use std::ffi::CStr;
5+
46
use crate::abi;
57
use crate::Ruby;
68
use crate::mmtk;
79
use crate::binding::RubyBinding;
810
use mmtk::MMTKBuilder;
911
use mmtk::memory_manager;
12+
use mmtk::memory_manager::mmtk_init;
1013
use mmtk::scheduler::{GCController, GCWorker};
1114
use mmtk::util::constants::MIN_OBJECT_SIZE;
15+
use mmtk::util::options::PlanSelector;
1216
use mmtk::util::{Address, ObjectReference};
1317
use mmtk::util::{VMMutatorThread, VMThread, VMWorkerThread};
1418
use mmtk::AllocationSemantics;
1519
use mmtk::Mutator;
20+
use mmtk::util::options::Options;
21+
22+
#[no_mangle]
23+
pub extern "C" fn mmtk_options_default() -> *mut Options {
24+
Box::into_raw(Box::new(Options::default()))
25+
}
1626

1727
#[no_mangle]
18-
pub extern "C" fn mmtk_init_binding(heap_size: usize, upcalls: *const abi::RubyUpcalls) {
19-
let mut builder = MMTKBuilder::default();
20-
builder.options.heap_size.set(heap_size);
21-
let mmtk = builder.build();
22-
let mmtk_static = Box::leak(Box::new(mmtk));
28+
pub extern "C" fn mmtk_options_set_heap_size(options: *mut Options, heap_size: usize) {
29+
let options = unsafe { &mut *options };
30+
options.heap_size.set(heap_size);
31+
}
32+
33+
#[no_mangle]
34+
pub extern "C" fn mmtk_options_set_plan(options: *mut Options, plan_name: *const libc::c_char) {
35+
let options = unsafe { &mut *options };
36+
let plan_name_cstr = unsafe { CStr::from_ptr(plan_name) };
37+
let plan_name_str = plan_name_cstr.to_str().unwrap();
38+
let plan_selector = plan_name_str.parse::<PlanSelector>().unwrap();
39+
options.plan.set(plan_selector);
40+
}
41+
42+
#[no_mangle]
43+
pub extern "C" fn mmtk_init_binding(options: *mut Options, upcalls: *const abi::RubyUpcalls) {
44+
let options = unsafe { Box::from_raw(options) };
45+
let builder = MMTKBuilder { options: *options };
46+
let mmtk_boxed = mmtk_init(&builder);
47+
let mmtk_static = Box::leak(Box::new(mmtk_boxed));
48+
2349
let binding = RubyBinding::new(mmtk_static, upcalls);
2450

2551
crate::BINDING.set(binding).unwrap_or_else(|_| {

0 commit comments

Comments
 (0)