| 1 |
#[cfg(not(all(Py_3_11, any(PyPy, GraalPy))))] |
| 2 |
use libc::size_t; |
| 3 |
use std::ffi::c_int; |
| 4 |
|
| 5 |
#[cfg(not(any(PyPy, GraalPy)))] |
| 6 |
use std::ffi::c_void; |
| 7 |
|
| 8 |
use crate::object::*; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#[cfg(not(Py_3_11))] |
| 14 |
extern "C" { |
| 15 |
pub fn _Py_GetAllocatedBlocks() -> crate::Py_ssize_t; |
| 16 |
} |
| 17 |
|
| 18 |
#[cfg(not(any(PyPy, GraalPy)))] |
| 19 |
#[repr(C)] |
| 20 |
#[derive(Copy, Clone)] |
| 21 |
pub struct PyObjectArenaAllocator { |
| 22 |
pub ctx: *mut c_void, |
| 23 |
pub alloc: Option<extern "C" fn(ctx: *mut c_void, size: size_t) -> *mut c_void>, |
| 24 |
pub free: Option<extern "C" fn(ctx: *mut c_void, ptr: *mut c_void, size: size_t)>, |
| 25 |
} |
| 26 |
|
| 27 |
#[cfg(not(any(PyPy, GraalPy)))] |
| 28 |
impl Default for PyObjectArenaAllocator { |
| 29 |
#[inline] |
| 30 |
fn default() -> Self { |
| 31 |
unsafe { std::mem::zeroed() } |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
extern "C" { |
| 36 |
#[cfg(not(any(PyPy, GraalPy)))] |
| 37 |
pub fn PyObject_GetArenaAllocator(allocator: *mut PyObjectArenaAllocator); |
| 38 |
#[cfg(not(any(PyPy, GraalPy)))] |
| 39 |
pub fn PyObject_SetArenaAllocator(allocator: *mut PyObjectArenaAllocator); |
| 40 |
|
| 41 |
#[cfg(Py_3_9)] |
| 42 |
pub fn PyObject_IS_GC(o: *mut PyObject) -> c_int; |
| 43 |
} |
| 44 |
|
| 45 |
#[inline] |
| 46 |
#[cfg(not(Py_3_9))] |
| 47 |
pub unsafe fn PyObject_IS_GC(o: *mut PyObject) -> c_int { |
| 48 |
(crate::PyType_IS_GC(Py_TYPE(o)) != 0 |
| 49 |
&& match (*Py_TYPE(o)).tp_is_gc { |
| 50 |
Some(tp_is_gc) => tp_is_gc(o) != 0, |
| 51 |
None => true, |
| 52 |
}) as c_int |
| 53 |
} |
| 54 |
|
| 55 |
#[cfg(not(Py_3_11))] |
| 56 |
extern "C" { |
| 57 |
pub fn _PyObject_GC_Malloc(size: size_t) -> *mut PyObject; |
| 58 |
pub fn _PyObject_GC_Calloc(size: size_t) -> *mut PyObject; |
| 59 |
} |
| 60 |
|
| 61 |
#[inline] |
| 62 |
pub unsafe fn PyType_SUPPORTS_WEAKREFS(t: *mut PyTypeObject) -> c_int { |
| 63 |
((*t).tp_weaklistoffset > 0) as c_int |
| 64 |
} |
| 65 |
|
| 66 |
#[inline] |
| 67 |
pub unsafe fn PyObject_GET_WEAKREFS_LISTPTR(o: *mut PyObject) -> *mut *mut PyObject { |
| 68 |
let weaklistoffset = (*Py_TYPE(o)).tp_weaklistoffset; |
| 69 |
o.offset(weaklistoffset) as *mut *mut PyObject |
| 70 |
} |
| 71 |
|
| 72 |
|