Skip to content

Commit

Permalink
Remove GC internal methods from GcAllocator API
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-hughes committed Oct 23, 2023
1 parent c9efd21 commit 6e7e525
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 66 deletions.
84 changes: 27 additions & 57 deletions library/alloc/src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ use core::gc::ReferenceFree;
#[cfg(test)]
mod tests;

#[unstable(feature = "gc", issue = "none")]
static ALLOCATOR: GcAllocator = GcAllocator;

#[cfg(profile_gc)]
static FINALIZERS_REGISTERED: AtomicU64 = AtomicU64::new(0);
#[cfg(profile_gc)]
Expand Down Expand Up @@ -113,63 +110,28 @@ impl GcAllocator {
pub fn force_gc() {
unsafe { boehm::GC_gcollect() }
}
}

pub unsafe fn register_finalizer(
&self,
obj: *mut u8,
finalizer: Option<unsafe extern "C" fn(*mut u8, *mut u8)>,
client_data: *mut u8,
old_finalizer: *mut extern "C" fn(*mut u8, *mut u8),
old_client_data: *mut *mut u8,
) {
unsafe {
boehm::GC_register_finalizer_no_order(
obj,
finalizer,
client_data,
old_finalizer,
old_client_data,
)
}
}

pub fn unregister_finalizer(&self, gcbox: *mut u8) {
unsafe {
boehm::GC_register_finalizer(
gcbox,
None,
::core::ptr::null_mut(),
::core::ptr::null_mut(),
::core::ptr::null_mut(),
);
}
}

pub fn init() {
unsafe { boehm::GC_init() }
}

/// Returns true if thread was successfully registered.
pub unsafe fn register_thread(stack_base: *mut u8) -> bool {
unsafe { boehm::GC_register_my_thread(stack_base) == 0 }
}
pub fn init_gc() {
unsafe { boehm::GC_init() }
}

/// Returns true if thread was successfully unregistered.
pub unsafe fn unregister_thread() -> bool {
unsafe { boehm::GC_unregister_my_thread() == 0 }
}
/// Returns true if thread was successfully registered.
pub unsafe fn register_thread(stack_base: *mut u8) -> bool {
unsafe { boehm::GC_register_my_thread(stack_base) == 0 }
}

pub fn thread_registered() -> bool {
unsafe { boehm::GC_thread_is_registered() != 0 }
}
/// Returns true if thread was successfully unregistered.
pub unsafe fn unregister_thread() -> bool {
unsafe { boehm::GC_unregister_my_thread() == 0 }
}

pub fn allow_register_threads() {
unsafe { boehm::GC_allow_register_threads() }
}
pub fn suppress_gc_warnings() {
unsafe { boehm::GC_set_warn_proc(&boehm::GC_ignore_warn_proc as *const _ as *mut u8) };
}

pub fn suppress_warnings() {
unsafe { boehm::GC_set_warn_proc(&boehm::GC_ignore_warn_proc as *const _ as *mut u8) };
}
pub fn thread_registered() -> bool {
unsafe { boehm::GC_thread_is_registered() != 0 }
}

struct GcBox<T: ?Sized>(T);
Expand Down Expand Up @@ -376,7 +338,7 @@ impl<T> Gc<T> {
}

unsafe {
ALLOCATOR.register_finalizer(
boehm::GC_register_finalizer_no_order(
self.ptr.as_ptr() as *mut u8,
Some(finalizer::<T>),
null_mut(),
Expand All @@ -389,7 +351,15 @@ impl<T> Gc<T> {
#[unstable(feature = "gc", issue = "none")]
pub fn unregister_finalizer(&mut self) {
let ptr = self.ptr.as_ptr() as *mut GcBox<T> as *mut u8;
ALLOCATOR.unregister_finalizer(ptr);
unsafe {
boehm::GC_register_finalizer(
ptr,
None,
::core::ptr::null_mut(),
::core::ptr::null_mut(),
::core::ptr::null_mut(),
);
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions library/boehm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ extern "C" {

pub fn GC_unregister_my_thread() -> i32;

pub fn GC_allow_register_threads();

pub fn GC_init();

pub fn GC_set_warn_proc(level: *mut u8);
Expand Down
6 changes: 2 additions & 4 deletions library/std/src/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,18 @@ macro_rules! rtunwrap {
#[cfg_attr(test, allow(dead_code))]
unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
unsafe {
use crate::alloc::GcAllocator;

// Internally, this registers a SIGSEGV handler to compute the start and
// end bounds of the data segment. This means it *MUST* be called before
// rustc registers its own SIGSEGV stack overflow handler.
//
// Rust's stack overflow handler will unregister and return if there is
// no stack overflow, allowing the fault to "fall-through" to Boehm's
// handler next time. The is not true in the reverse case.
GcAllocator::init();
crate::gc::init();

// Boehm GC prints OOM warnings which are useful for debugging, but
// annoying when building the compiler in release mode.
GcAllocator::suppress_warnings();
crate::gc::suppress_warnings();

sys::init(argc, argv, sigpipe);

Expand Down
5 changes: 2 additions & 3 deletions library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@
#[cfg(all(test, not(target_os = "emscripten")))]
mod tests;

use crate::alloc::GcAllocator;
use crate::any::Any;
use crate::cell::UnsafeCell;
use crate::ffi::{CStr, CString};
Expand Down Expand Up @@ -523,7 +522,7 @@ impl Builder {
let stack_start = unsafe { imp::guard::get_stack_start().unwrap() };
if stack_start != crate::ptr::null_mut() {
unsafe {
GcAllocator::register_thread(&stack_start as *const _ as *mut u8);
crate::gc::register_thread(&stack_start as *const _ as *mut u8);
}
}

Expand All @@ -541,7 +540,7 @@ impl Builder {

// SAFETY: The thread has no more work to do, so can be unregisterd.
unsafe {
GcAllocator::unregister_thread();
crate::gc::unregister_thread();
}

// SAFETY: `their_packet` as been built just above and moved by the
Expand Down

0 comments on commit 6e7e525

Please sign in to comment.