Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub mod longobject;
pub mod methodobject;
pub mod moduleobject;
pub mod object;
pub mod objimpl;
pub mod osmodule;
pub mod pycapsule;
pub mod pyerrors;
Expand Down
82 changes: 82 additions & 0 deletions crates/capi/src/objimpl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use crate::PyObject;
use crate::pymem::{PyMem_Calloc, PyMem_Free, PyMem_Malloc, PyMem_Realloc};
use crate::pystate::with_vm;
use core::ffi::{c_int, c_void};
use rustpython_vm::gc_state;

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyObject_GC_Track(op: *mut PyObject) {
with_vm(|_vm| {
let obj = unsafe { &*op };
if !obj.is_gc_tracked() {
unsafe { gc_state::gc_state().track_object(obj.into()) };
}
})
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyObject_GC_UnTrack(op: *mut PyObject) {
with_vm(|_vm| {
let obj = unsafe { &*op };
if obj.is_gc_tracked() {
unsafe { gc_state::gc_state().untrack_object(obj.into()) };
}
})
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyObject_GC_IsTracked(op: *mut PyObject) -> c_int {
with_vm(|_vm| unsafe { (&*op).is_gc_tracked() })
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyObject_GC_IsFinalized(op: *mut PyObject) -> c_int {
with_vm(|_vm| unsafe { (&*op).gc_finalized() })
}

#[unsafe(no_mangle)]
pub extern "C" fn PyGC_Collect() -> isize {
let result = gc_state::gc_state().collect(2);
(result.collected + result.uncollectable) as isize
}

#[unsafe(no_mangle)]
pub extern "C" fn PyGC_Enable() -> c_int {
let gc = gc_state::gc_state();
let was_enabled = gc.is_enabled();
gc.enable();
was_enabled.into()
}

#[unsafe(no_mangle)]
pub extern "C" fn PyGC_Disable() -> c_int {
let gc = gc_state::gc_state();
let was_enabled = gc.is_enabled();
gc.disable();
was_enabled.into()
}

#[unsafe(no_mangle)]
pub extern "C" fn PyGC_IsEnabled() -> c_int {
gc_state::gc_state().is_enabled().into()
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyObject_Malloc(size: usize) -> *mut c_void {
unsafe { PyMem_Malloc(size) }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyObject_Calloc(nelem: usize, elsize: usize) -> *mut c_void {
unsafe { PyMem_Calloc(nelem, elsize) }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyObject_Realloc(ptr: *mut c_void, new_size: usize) -> *mut c_void {
unsafe { PyMem_Realloc(ptr, new_size) }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyObject_Free(ptr: *mut c_void) {
unsafe { PyMem_Free(ptr) }
}
2 changes: 1 addition & 1 deletion crates/vm/src/object/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ impl PyObject {
/// Check if the object has been finalized (__del__ already called).
/// _PyGC_FINALIZED in Py_GIL_DISABLED mode.
#[inline]
pub(crate) fn gc_finalized(&self) -> bool {
pub fn gc_finalized(&self) -> bool {
GcBits::from_bits_retain(self.0.gc_bits.load(Ordering::Relaxed)).contains(GcBits::FINALIZED)
}

Expand Down
Loading