From 59fe9dfa472830cce2e0ce4e94d507e9f118aa64 Mon Sep 17 00:00:00 2001 From: Yutaka HARA Date: Sun, 14 May 2023 16:12:16 +0900 Subject: [PATCH] impl. register_finalizer, force_collect (#248) --- src/lib.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 2b5a1e0..d021af0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,6 +26,14 @@ extern "C" { fn GC_register_my_thread(stack_base: *const GcStackBase) -> c_int; fn GC_set_stackbottom(thread: *const c_void, stack_bottom: *const GcStackBase); fn GC_unregister_my_thread(); + fn GC_gcollect(); + fn GC_register_finalizer( + obj: *const c_void, + f: extern "C" fn(*mut c_void, *mut c_void), + client_data: *const c_void, + opt_old_f: *const c_void, + opt_old_cd: *const c_void, + ) -> *mut c_void; } pub struct Allocator; @@ -70,6 +78,18 @@ impl Allocator { pub unsafe fn unregister_current_thread() { GC_unregister_my_thread() } + + pub fn force_collect() { + unsafe { GC_gcollect() } + } + + pub unsafe fn register_finalizer( + obj: *const c_void, + f: extern "C" fn(*mut c_void, *mut c_void), + client_data: *const c_void, + ) { + GC_register_finalizer(obj, f, client_data, std::ptr::null(), std::ptr::null()); + } } unsafe impl GlobalAlloc for Allocator {