1use libc::{c_int, c_void, size_t};
2
3use crate::object::*;
4use crate::pyport::Py_ssize_t;
5
6#[cfg_attr(windows, link(name = "pythonXY"))]
7extern "C" {
8 #[cfg(not(all(py_sys_config = "Py_DEBUG", not(Py_3_4))))]
9 pub fn PyObject_Malloc(size: size_t) -> *mut c_void;
10 #[cfg(Py_3_5)]
11 pub fn PyObject_Calloc(nelem: size_t, elsize: size_t) -> *mut c_void;
12 #[cfg(not(all(py_sys_config = "Py_DEBUG", not(Py_3_4))))]
13 pub fn PyObject_Realloc(ptr: *mut c_void, new_size: size_t) -> *mut c_void;
14 #[cfg(not(all(py_sys_config = "Py_DEBUG", not(Py_3_4))))]
15 pub fn PyObject_Free(ptr: *mut c_void) -> ();
16
17 #[cfg(all(py_sys_config = "Py_DEBUG", not(Py_3_4)))]
18 pub fn _PyObject_DebugMalloc(arg1: size_t) -> *mut c_void;
19 #[cfg(all(py_sys_config = "Py_DEBUG", not(Py_3_4)))]
20 pub fn _PyObject_DebugRealloc(arg1: *mut c_void, arg2: size_t) -> *mut c_void;
21 #[cfg(all(py_sys_config = "Py_DEBUG", not(Py_3_4)))]
22 pub fn _PyObject_DebugFree(arg1: *mut c_void);
23
24 #[cfg(all(not(Py_LIMITED_API), Py_3_4, not(Py_3_11)))]
25 pub fn _Py_GetAllocatedBlocks() -> Py_ssize_t;
26 pub fn PyObject_Init(arg1: *mut PyObject, arg2: *mut PyTypeObject) -> *mut PyObject;
27 pub fn PyObject_InitVar(
28 arg1: *mut PyVarObject,
29 arg2: *mut PyTypeObject,
30 arg3: Py_ssize_t,
31 ) -> *mut PyVarObject;
32 pub fn _PyObject_New(arg1: *mut PyTypeObject) -> *mut PyObject;
33 pub fn _PyObject_NewVar(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyVarObject;
34
35 pub fn PyGC_Collect() -> Py_ssize_t;
36 #[cfg(Py_3_10)]
37 pub fn PyGC_Enable() -> c_int;
38 #[cfg(Py_3_10)]
39 pub fn PyGC_Disable() -> c_int;
40 #[cfg(Py_3_10)]
41 pub fn PyGC_IsEnabled() -> c_int;
42}
43
44#[cfg(all(py_sys_config = "Py_DEBUG", not(Py_3_4)))]
45pub use self::_PyObject_DebugFree as PyObject_Free;
46#[cfg(all(py_sys_config = "Py_DEBUG", not(Py_3_4)))]
47pub use self::_PyObject_DebugMalloc as PyObject_Malloc;
48#[cfg(all(py_sys_config = "Py_DEBUG", not(Py_3_4)))]
49pub use self::_PyObject_DebugRealloc as PyObject_Realloc;
50
51#[repr(C)]
52#[derive(Copy)]
53#[cfg(all(not(Py_LIMITED_API), Py_3_4))]
54pub struct PyObjectArenaAllocator {
55 pub ctx: *mut c_void,
56 pub alloc: Option<extern "C" fn(ctx: *mut c_void, size: size_t) -> *mut c_void>,
57 pub free: Option<extern "C" fn(ctx: *mut c_void, ptr: *mut c_void, size: size_t) -> ()>,
58}
59#[cfg(all(not(Py_LIMITED_API), Py_3_4))]
60impl Clone for PyObjectArenaAllocator {
61 #[inline]
62 fn clone(&self) -> Self {
63 *self
64 }
65}
66#[cfg(all(not(Py_LIMITED_API), Py_3_4))]
67impl Default for PyObjectArenaAllocator {
68 #[inline]
69 fn default() -> Self {
70 unsafe { ::core::mem::zeroed() }
71 }
72}
73#[cfg(all(not(Py_LIMITED_API), Py_3_4))]
74#[cfg_attr(windows, link(name = "pythonXY"))]
75extern "C" {
76 pub fn PyObject_GetArenaAllocator(allocator: *mut PyObjectArenaAllocator) -> ();
77 pub fn PyObject_SetArenaAllocator(allocator: *mut PyObjectArenaAllocator) -> ();
78}
79
80#[inline(always)]
82pub unsafe fn PyType_IS_GC(t: *mut PyTypeObject) -> c_int {
83 PyType_HasFeature(t, Py_TPFLAGS_HAVE_GC)
84}
85
86#[inline(always)]
88#[cfg(all(not(Py_LIMITED_API), not(Py_3_9)))]
89pub unsafe fn PyObject_IS_GC(o: *mut PyObject) -> c_int {
90 (PyType_IS_GC(Py_TYPE(o)) != 0
91 && match (*Py_TYPE(o)).tp_is_gc {
92 Some(tp_is_gc) => tp_is_gc(o) != 0,
93 None => true,
94 }) as c_int
95}
96
97#[cfg_attr(windows, link(name = "pythonXY"))]
98extern "C" {
99 #[cfg(all(not(Py_LIMITED_API), Py_3_9))]
100 pub fn PyObject_IS_GC(o: *mut PyObject) -> c_int;
101
102 pub fn _PyObject_GC_Resize(arg1: *mut PyVarObject, arg2: Py_ssize_t) -> *mut PyVarObject;
103
104 #[cfg(all(not(Py_LIMITED_API), not(Py_3_11)))]
105 pub fn _PyObject_GC_Malloc(size: size_t) -> *mut PyObject;
106 #[cfg(all(not(Py_LIMITED_API), Py_3_5, not(Py_3_11)))]
107 pub fn _PyObject_GC_Calloc(size: size_t) -> *mut PyObject;
108 pub fn _PyObject_GC_New(arg1: *mut PyTypeObject) -> *mut PyObject;
109 pub fn _PyObject_GC_NewVar(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyVarObject;
110 pub fn PyObject_GC_Track(arg1: *mut c_void) -> ();
111 pub fn PyObject_GC_UnTrack(arg1: *mut c_void) -> ();
112 pub fn PyObject_GC_Del(arg1: *mut c_void) -> ();
113
114 #[cfg(Py_3_9)]
115 pub fn PyObject_GC_IsTracked(o: *mut PyObject) -> c_int;
116 #[cfg(Py_3_9)]
117 pub fn PyObject_GC_IsFinalized(o: *mut PyObject) -> c_int;
118}
119
120#[inline(always)]
122#[cfg(all(not(Py_LIMITED_API), not(Py_3_11)))]
123pub unsafe fn PyType_SUPPORTS_WEAKREFS(t: *mut PyTypeObject) -> c_int {
124 ((*t).tp_weaklistoffset > 0) as c_int
125}
126
127#[inline(always)]
128#[cfg(all(not(Py_LIMITED_API), not(Py_3_9)))]
129pub unsafe fn PyObject_GET_WEAKREFS_LISTPTR(o: *mut PyObject) -> *mut *mut PyObject {
130 let weaklistoffset = (*Py_TYPE(o)).tp_weaklistoffset as isize;
131 (o as *mut u8).offset(weaklistoffset) as *mut *mut PyObject
132}
133
134#[cfg_attr(windows, link(name = "pythonXY"))]
135extern "C" {
136 #[cfg(all(not(Py_LIMITED_API), Py_3_11))]
137 pub fn PyType_SUPPORTS_WEAKREFS(t: *mut PyTypeObject) -> c_int;
138 #[cfg(all(not(Py_LIMITED_API), Py_3_9))]
139 pub fn PyObject_GET_WEAKREFS_LISTPTR(o: *mut PyObject) -> *mut *mut PyObject;
140}