Repository navigation
Expand file tree
/
Copy pathobjimpl.rs
More file actions
87 lines (75 loc) · 2.39 KB
/
Copy pathobjimpl.rs
File metadata and controls
87 lines (75 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::PyObject;
use crate::pymem::{PyMem_Calloc, PyMem_Free, PyMem_Malloc, PyMem_Realloc};
use crate::pystate::with_vm;
use crate::util::FfiPtrExt;
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.assume_borrowed() };
if !obj.is_gc_tracked() {
unsafe { gc_state::gc_state().track_object(obj.into(), gc_state::current_owner()) };
}
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyObject_GC_UnTrack(op: *mut PyObject) {
with_vm(|_vm| {
let obj = unsafe { op.assume_borrowed() };
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.assume_borrowed() }.is_gc_tracked())
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyObject_GC_IsFinalized(op: *mut PyObject) -> c_int {
with_vm(|_vm| unsafe { op.assume_borrowed() }.gc_finalized())
}
#[unsafe(no_mangle)]
pub extern "C" fn PyGC_Collect() -> isize {
with_vm(|vm| {
let result = vm.state.gc.collect(2);
(result.collected + result.uncollectable) as isize
})
}
#[unsafe(no_mangle)]
pub extern "C" fn PyGC_Enable() -> c_int {
with_vm(|vm| {
let was_enabled: c_int = vm.state.gc.is_enabled().into();
vm.state.gc.enable();
was_enabled
})
}
#[unsafe(no_mangle)]
pub extern "C" fn PyGC_Disable() -> c_int {
with_vm(|vm| {
let was_enabled: c_int = vm.state.gc.is_enabled().into();
vm.state.gc.disable();
was_enabled
})
}
#[unsafe(no_mangle)]
pub extern "C" fn PyGC_IsEnabled() -> c_int {
with_vm(|vm| -> c_int { vm.state.gc.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) }
}