Add allocator functions to c-api (#8168)

This commit is contained in:
Bas Schoenmaeckers authored and GitHub committed 2026-06-25 22:34:14 +09:00
1 parent 4d3ddac7c3
commit dda5516449
5 files changed
+50

No files matched your search

+1
View File
@@ -192,6 +192,7 @@ pyerrors
Pyfunc
pylifecycle
pymain
pymem
pyrepl
pystate
PYTHONTRACEMALLOC
Generated
+1
View File
@@ -3447,6 +3447,7 @@ version = "0.5.0"
dependencies = [
"bitflags 2.13.0",
"itertools 0.14.0",
"libc",
"num-complex",
"pyo3",
"rustpython-pylib",
+1
View File
@@ -14,6 +14,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
bitflags = { workspace = true }
itertools = { workspace = true }
libc = { workspace = true }
num-complex = { workspace = true }
rustpython-vm = { workspace = true, features = ["threading", "compiler", "importlib", "host_env"] }
rustpython-stdlib = {workspace = true, features = ["threading"] }
+1
View File
@@ -26,6 +26,7 @@ pub mod object;
pub mod pycapsule;
pub mod pyerrors;
pub mod pylifecycle;
pub mod pymem;
pub mod pystate;
pub mod refcount;
pub mod setobject;
+46
View File
@@ -0,0 +1,46 @@
use core::ffi::c_void;
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyMem_Malloc(n: usize) -> *mut c_void {
unsafe { libc::malloc(if n == 0 { 1 } else { n }) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyMem_Calloc(nelem: usize, elsize: usize) -> *mut c_void {
unsafe {
libc::calloc(
if nelem == 0 || elsize == 0 { 1 } else { nelem },
if nelem == 0 || elsize == 0 { 1 } else { elsize },
)
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyMem_Realloc(ptr: *mut c_void, new_size: usize) -> *mut c_void {
unsafe { libc::realloc(ptr, if new_size == 0 { 1 } else { new_size }) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyMem_Free(ptr: *mut c_void) {
unsafe { libc::free(ptr) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyMem_RawMalloc(n: usize) -> *mut c_void {
unsafe { libc::malloc(n) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyMem_RawCalloc(nelem: usize, elsize: usize) -> *mut c_void {
unsafe { libc::calloc(nelem, elsize) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyMem_RawRealloc(ptr: *mut c_void, new_size: usize) -> *mut c_void {
unsafe { libc::realloc(ptr, new_size) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyMem_RawFree(ptr: *mut c_void) {
unsafe { libc::free(ptr) }
}