ffi: cleanup pymem

This commit is contained in:
Dean Li 2021-08-23 20:28:37 +08:00
parent 1eeeb1fa32
commit 0a50b42352
No known key found for this signature in database
GPG key ID: 4F55BB69D480672A
2 changed files with 46 additions and 41 deletions

46
src/ffi/cpython/pymem.rs Normal file
View file

@ -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();
}

View file

@ -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;
@ -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();
}