From 6e7e525847e8a65de5b6d904614d693b7afb370e Mon Sep 17 00:00:00 2001 From: Jake Hughes Date: Mon, 23 Oct 2023 11:09:38 +0100 Subject: [PATCH] Remove GC internal methods from GcAllocator API --- library/alloc/src/gc.rs | 84 +++++++++++------------------------ library/boehm/src/lib.rs | 2 - library/std/src/rt.rs | 6 +-- library/std/src/thread/mod.rs | 5 +-- 4 files changed, 31 insertions(+), 66 deletions(-) diff --git a/library/alloc/src/gc.rs b/library/alloc/src/gc.rs index 4d72ce9c6b190..5fe0d35bf1241 100644 --- a/library/alloc/src/gc.rs +++ b/library/alloc/src/gc.rs @@ -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)] @@ -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, - 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); @@ -376,7 +338,7 @@ impl Gc { } unsafe { - ALLOCATOR.register_finalizer( + boehm::GC_register_finalizer_no_order( self.ptr.as_ptr() as *mut u8, Some(finalizer::), null_mut(), @@ -389,7 +351,15 @@ impl Gc { #[unstable(feature = "gc", issue = "none")] pub fn unregister_finalizer(&mut self) { let ptr = self.ptr.as_ptr() as *mut GcBox 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(), + ); + } } } diff --git a/library/boehm/src/lib.rs b/library/boehm/src/lib.rs index f1024eef4b616..c63a02864b4ec 100644 --- a/library/boehm/src/lib.rs +++ b/library/boehm/src/lib.rs @@ -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); diff --git a/library/std/src/rt.rs b/library/std/src/rt.rs index 9e012e339a579..5bb6b1afeab8e 100644 --- a/library/std/src/rt.rs +++ b/library/std/src/rt.rs @@ -94,8 +94,6 @@ 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. @@ -103,11 +101,11 @@ unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) { // 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); diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs index d84e70723e68f..ae6ecb1453071 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -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}; @@ -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); } } @@ -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