Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ffi cleanup: pylifecycle to pystate #1823

Merged
merged 4 commits into from
Aug 24, 2021
Merged
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
3 changes: 2 additions & 1 deletion src/ffi/cpython/ceval.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ffi::{freefunc, PyObject, Py_tracefunc};
use crate::ffi::cpython::pystate::Py_tracefunc;
use crate::ffi::object::{freefunc, PyObject};
use std::os::raw::c_int;

extern "C" {
Expand Down
46 changes: 46 additions & 0 deletions src/ffi/cpython/pymem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use libc::size_t;
use std::os::raw::c_void;

extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyMem_RawMalloc")]
pub fn PyMem_RawMalloc(size: size_t) -> *mut c_void;
#[cfg_attr(PyPy, link_name = "PyPyMem_RawCalloc")]
pub fn PyMem_RawCalloc(nelem: size_t, elsize: size_t) -> *mut c_void;
#[cfg_attr(PyPy, link_name = "PyPyMem_RawRealloc")]
pub fn PyMem_RawRealloc(ptr: *mut c_void, new_size: size_t) -> *mut c_void;
#[cfg_attr(PyPy, link_name = "PyPyMem_RawFree")]
pub fn PyMem_RawFree(ptr: *mut c_void);

// skipped _PyMem_GetCurrentAllocatorName
// skipped _PyMem_RawStrdup
// skipped _PyMem_Strdup
// skipped _PyMem_RawWcsdup
}

#[repr(C)]
#[derive(Copy, Clone)]
pub enum PyMemAllocatorDomain {
PYMEM_DOMAIN_RAW,
PYMEM_DOMAIN_MEM,
PYMEM_DOMAIN_OBJ,
}

// skipped PyMemAllocatorName

#[repr(C)]
#[derive(Copy, Clone)]
pub struct PyMemAllocatorEx {
pub ctx: *mut c_void,
pub malloc: Option<extern "C" fn(ctx: *mut c_void, size: size_t) -> *mut c_void>,
pub calloc:
Option<extern "C" fn(ctx: *mut c_void, nelem: size_t, elsize: size_t) -> *mut c_void>,
pub realloc:
Option<extern "C" fn(ctx: *mut c_void, ptr: *mut c_void, new_size: size_t) -> *mut c_void>,
pub free: Option<extern "C" fn(ctx: *mut c_void, ptr: *mut c_void)>,
}

extern "C" {
pub fn PyMem_GetAllocator(domain: PyMemAllocatorDomain, allocator: *mut PyMemAllocatorEx);
pub fn PyMem_SetAllocator(domain: PyMemAllocatorDomain, allocator: *mut PyMemAllocatorEx);
pub fn PyMem_SetupDebugHooks();
}
5 changes: 4 additions & 1 deletion src/ffi/cpython/pystate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ extern "C" {
#[cfg(not(PyPy))]
#[cfg_attr(docsrs, doc(cfg(not(PyPy))))]
pub fn PyThreadState_Next(tstate: *mut PyThreadState) -> *mut PyThreadState;
// skipped PyThreadState_DeleteCurrent

#[cfg(py_sys_config = "WITH_THREAD")]
#[cfg_attr(PyPy, link_name = "PyPyThreadState_DeleteCurrent")]
pub fn PyThreadState_DeleteCurrent();
deantvv marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(Py_3_9)]
Expand Down
1 change: 0 additions & 1 deletion src/ffi/genobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub unsafe fn PyGen_CheckExact(op: *mut PyObject) -> c_int {

extern "C" {
pub fn PyGen_New(frame: *mut PyFrameObject) -> *mut PyObject;
// skipped PyGen_New
// skipped PyGen_NewWithQualName
// skipped _PyGen_SetStopIterationValue
// skipped _PyGen_FetchStopIterationValue
Expand Down
2 changes: 2 additions & 0 deletions src/ffi/pylifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ extern "C" {
pub fn Py_GetPath() -> *mut wchar_t;
pub fn Py_SetPath(arg1: *const wchar_t);

// skipped _Py_CheckPython3

#[cfg_attr(PyPy, link_name = "PyPy_GetVersion")]
pub fn Py_GetVersion() -> *const c_char;
pub fn Py_GetPlatform() -> *const c_char;
Expand Down
41 changes: 0 additions & 41 deletions src/ffi/pymem.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
use libc::size_t;
use std::os::raw::c_void;

#[cfg(not(Py_LIMITED_API))]
extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyMem_RawMalloc")]
pub fn PyMem_RawMalloc(size: size_t) -> *mut c_void;
#[cfg_attr(PyPy, link_name = "PyPyMem_RawCalloc")]
pub fn PyMem_RawCalloc(nelem: size_t, elsize: size_t) -> *mut c_void;
#[cfg_attr(PyPy, link_name = "PyPyMem_RawRealloc")]
pub fn PyMem_RawRealloc(ptr: *mut c_void, new_size: size_t) -> *mut c_void;
#[cfg_attr(PyPy, link_name = "PyPyMem_RawFree")]
pub fn PyMem_RawFree(ptr: *mut c_void);
}

extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyMem_Malloc")]
pub fn PyMem_Malloc(size: size_t) -> *mut c_void;
Expand All @@ -23,32 +11,3 @@ extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyMem_Free")]
pub fn PyMem_Free(ptr: *mut c_void);
}

#[cfg(not(Py_LIMITED_API))]
#[repr(C)]
#[derive(Copy, Clone)]
pub enum PyMemAllocatorDomain {
PYMEM_DOMAIN_RAW,
PYMEM_DOMAIN_MEM,
PYMEM_DOMAIN_OBJ,
}

#[repr(C)]
#[derive(Copy, Clone)]
#[cfg(not(Py_LIMITED_API))]
pub struct PyMemAllocatorEx {
pub ctx: *mut c_void,
pub malloc: Option<extern "C" fn(ctx: *mut c_void, size: size_t) -> *mut c_void>,
pub calloc:
Option<extern "C" fn(ctx: *mut c_void, nelem: size_t, elsize: size_t) -> *mut c_void>,
pub realloc:
Option<extern "C" fn(ctx: *mut c_void, ptr: *mut c_void, new_size: size_t) -> *mut c_void>,
pub free: Option<extern "C" fn(ctx: *mut c_void, ptr: *mut c_void)>,
}

#[cfg(not(Py_LIMITED_API))]
extern "C" {
pub fn PyMem_GetAllocator(domain: PyMemAllocatorDomain, allocator: *mut PyMemAllocatorEx);
pub fn PyMem_SetAllocator(domain: PyMemAllocatorDomain, allocator: *mut PyMemAllocatorEx);
pub fn PyMem_SetupDebugHooks();
}
30 changes: 17 additions & 13 deletions src/ffi/pystate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::ffi::moduleobject::PyModuleDef;
use crate::ffi::object::PyObject;
use std::os::raw::c_int;

#[cfg(not(PyPy))]
use std::os::raw::c_long;

Expand All @@ -27,15 +28,16 @@ extern "C" {

#[cfg(all(Py_3_8, not(PyPy)))]
#[cfg_attr(docsrs, doc(all(Py_3_8, not(PyPy))))]
pub fn PyInterpreterState_GetDict() -> *mut PyObject;
pub fn PyInterpreterState_GetDict(arg1: *mut PyInterpreterState) -> *mut PyObject;

#[cfg(all(Py_3_7, not(PyPy)))]
#[cfg_attr(docsrs, doc(all(Py_3_7, not(PyPy))))]
pub fn PyInterpreterState_GetID() -> i64;
pub fn PyInterpreterState_GetID(arg1: *mut PyInterpreterState) -> i64;

#[cfg(not(PyPy))]
#[cfg_attr(docsrs, doc(cfg(not(PyPy))))]
pub fn PyState_AddModule(arg1: *mut PyObject, arg2: *mut PyModuleDef) -> c_int;

#[cfg(not(PyPy))]
#[cfg_attr(docsrs, doc(cfg(not(PyPy))))]
pub fn PyState_RemoveModule(arg1: *mut PyModuleDef) -> c_int;
Expand All @@ -46,18 +48,21 @@ extern "C" {

#[cfg_attr(PyPy, link_name = "PyPyThreadState_New")]
pub fn PyThreadState_New(arg1: *mut PyInterpreterState) -> *mut PyThreadState;
//fn _PyThreadState_Prealloc(arg1: *mut PyInterpreterState)
// -> *mut PyThreadState;
//fn _PyThreadState_Init(arg1: *mut PyThreadState);
#[cfg_attr(PyPy, link_name = "PyPyThreadState_Clear")]
pub fn PyThreadState_Clear(arg1: *mut PyThreadState);
#[cfg_attr(PyPy, link_name = "PyPyThreadState_Delete")]
pub fn PyThreadState_Delete(arg1: *mut PyThreadState);
#[cfg(py_sys_config = "WITH_THREAD")]
#[cfg_attr(PyPy, link_name = "PyPyThreadState_DeleteCurrent")]
pub fn PyThreadState_DeleteCurrent();

#[cfg_attr(PyPy, link_name = "PyPyThreadState_Get")]
pub fn PyThreadState_Get() -> *mut PyThreadState;
}

#[inline]
pub unsafe fn PyThreadState_GET() -> *mut PyThreadState {
PyThreadState_Get()
}

extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyThreadState_Swap")]
pub fn PyThreadState_Swap(arg1: *mut PyThreadState) -> *mut PyThreadState;
#[cfg_attr(PyPy, link_name = "PyPyThreadState_GetDict")]
Expand All @@ -67,6 +72,10 @@ extern "C" {
pub fn PyThreadState_SetAsyncExc(arg1: c_long, arg2: *mut PyObject) -> c_int;
}

// skipped non-limited / 3.9 PyThreadState_GetInterpreter
// skipped non-limited / 3.9 PyThreadState_GetFrame
// skipped non-limited / 3.9 PyThreadState_GetID

#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum PyGILState_STATE {
Expand All @@ -83,8 +92,3 @@ extern "C" {
#[cfg_attr(docsrs, doc(cfg(not(PyPy))))]
pub fn PyGILState_GetThisThreadState() -> *mut PyThreadState;
}

#[inline]
pub unsafe fn PyThreadState_GET() -> *mut PyThreadState {
PyThreadState_Get()
}