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