Skip to content
Open
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
56ff758
add `PyMemRawAllocator`
chirizxc Jul 31, 2026
27eab03
docs
chirizxc Jul 31, 2026
6c554a0
docs (x2)
chirizxc Jul 31, 2026
781b2c3
add newsfragments
chirizxc Jul 31, 2026
eadec33
update MAX_ALIGN
chirizxc Jul 31, 2026
707c873
update newsfragments
chirizxc Jul 31, 2026
0ae37cc
fix comment
chirizxc Jul 31, 2026
c430046
update doc
chirizxc Jul 31, 2026
d5dc676
refactor
chirizxc Jul 31, 2026
56dce48
remove unnecessary `unsafe` block
chirizxc Jul 31, 2026
2f1bdba
refactoring
chirizxc Jul 31, 2026
8a1777c
fmt
chirizxc Jul 31, 2026
583d780
rumdl fmt guide/src/features.md
chirizxc Jul 31, 2026
61f76db
fix newsfragments
chirizxc Jul 31, 2026
e3806cb
fix
chirizxc Jul 31, 2026
7ec0df4
try fix
chirizxc Jul 31, 2026
bcc71cb
add tests
chirizxc Jul 31, 2026
e388cec
remove whitespaces
chirizxc Jul 31, 2026
d53be71
more test
chirizxc Jul 31, 2026
24e5953
fix cfg
chirizxc Jul 31, 2026
ddf34d8
GLOBAL -> GLOBAL_ALLOCATOR
chirizxc Jul 31, 2026
81ed81c
remove feature
chirizxc Jul 31, 2026
0aa7a07
fix
chirizxc Jul 31, 2026
429bb8a
add safety comments
chirizxc Jul 31, 2026
1c9b8a3
update safety comments
chirizxc Jul 31, 2026
e416c10
update safety comments, add cpython docs links
chirizxc Jul 31, 2026
6624118
sync `pymem.h` with 3.15 branch
chirizxc Jul 31, 2026
4cc1242
fix clippy
chirizxc Aug 1, 2026
4c5106e
fix `cargo doc`
chirizxc Aug 1, 2026
89b135c
fix
chirizxc Aug 1, 2026
16e9763
fix clippy
chirizxc Aug 1, 2026
cffcdf7
fix test
chirizxc Aug 1, 2026
d9f21b8
fmt
chirizxc Aug 1, 2026
f8dd183
fix clippy
chirizxc Aug 1, 2026
ea4c350
fix MSRV
chirizxc Aug 1, 2026
e56c4a1
fix MSRV (x2)
chirizxc Aug 1, 2026
db5e086
fix MSRV (x3)
chirizxc Aug 1, 2026
5068b35
fix clippy
chirizxc Aug 1, 2026
eec4e73
Merge branch 'main' into pymem-raw-alloc
chirizxc Aug 7, 2026
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
1 change: 1 addition & 0 deletions newsfragments/6279.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `pyo3::pymem_alloc::PyMemRawAllocator`, a `GlobalAlloc` implementation backed by CPython's `PyMem_Raw*` (`PYMEM_DOMAIN_RAW`) allocator functions. Not registered as the global allocator automatically.
2 changes: 2 additions & 0 deletions pyo3-ffi-check/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ const MACRO_EXCLUSIONS: &[(&str, &str)] = &[
("Py_IsFalse", "not(Py_3_10)"),
("Py_IsTrue", "not(Py_3_10)"),
("Py_IsNone", "not(Py_3_10)"),
("PyMem_New", ""),
("PyMem_Resize", ""),
];

// TODO: probably need to clean these up
Expand Down
18 changes: 2 additions & 16 deletions pyo3-ffi/src/cpython/pymem.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
#[cfg(not(any(PyPy, GraalPy)))]
use core::ffi::c_void;
#[cfg(not(any(PyPy, GraalPy)))]
use libc::size_t;

extern_libpython! {
#[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
}

Comment on lines -4 to -19

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[repr(C)]
#[derive(Copy, Clone)]
pub enum PyMemAllocatorDomain {
Expand Down
49 changes: 49 additions & 0 deletions pyo3-ffi/src/pymem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,59 @@ use libc::size_t;
extern_libpython! {
#[cfg_attr(PyPy, link_name = "PyPyMem_Malloc")]
pub fn PyMem_Malloc(size: size_t) -> *mut c_void;

#[cfg_attr(PyPy, link_name = "PyPyMem_Calloc")]
pub fn PyMem_Calloc(nelem: size_t, elsize: size_t) -> *mut c_void;

#[cfg_attr(PyPy, link_name = "PyPyMem_Realloc")]
pub fn PyMem_Realloc(ptr: *mut c_void, new_size: size_t) -> *mut c_void;

#[cfg_attr(PyPy, link_name = "PyPyMem_Free")]
pub fn PyMem_Free(ptr: *mut c_void);
}

#[inline]
pub unsafe fn PyMem_New<T>(n: size_t) -> *mut T {
if n > isize::MAX as size_t / size_of::<T>() as size_t {
core::ptr::null_mut()
} else {
PyMem_Malloc(n * size_of::<T>() as size_t).cast()
}
}

#[inline]
pub unsafe fn PyMem_Resize<T>(p: &mut *mut T, n: size_t) {
*p = if n > isize::MAX as size_t / size_of::<T>() as size_t {
core::ptr::null_mut()
} else {
PyMem_Realloc(p.cast(), n * size_of::<T>() as size_t).cast()
};
}

extern_libpython! {
// Deprecated aliases:
//
// skipped PyMem_MALLOC
// skipped PyMem_NEW
// skipped PyMem_REALLOC
// skipped PyMem_RESIZE
// skipped PyMem_FREE
// skipped PyMem_Del
// skipped PyMem_DEL

#[cfg_attr(PyPy, link_name = "PyPyMem_RawMalloc")]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[cfg(any(Py_3_13, not(Py_LIMITED_API)))]
pub fn PyMem_RawMalloc(size: size_t) -> *mut c_void;

#[cfg_attr(PyPy, link_name = "PyPyMem_RawCalloc")]
#[cfg(any(Py_3_13, not(Py_LIMITED_API)))]
pub fn PyMem_RawCalloc(nelem: size_t, elsize: size_t) -> *mut c_void;

#[cfg_attr(PyPy, link_name = "PyPyMem_RawRealloc")]
#[cfg(any(Py_3_13, not(Py_LIMITED_API)))]
pub fn PyMem_RawRealloc(ptr: *mut c_void, new_size: size_t) -> *mut c_void;

#[cfg_attr(PyPy, link_name = "PyPyMem_RawFree")]
#[cfg(any(Py_3_13, not(Py_LIMITED_API)))]
pub fn PyMem_RawFree(ptr: *mut c_void);
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
//! crate, which is not supported on all platforms.
//!
//! The following features enable interactions with other crates in the Rust ecosystem:
//!
//! - [`anyhow`]: Enables a conversion from [anyhow]’s [`Error`][anyhow_error] type to [`PyErr`].
//! - [`chrono`]: Enables a conversion from [chrono]'s structures to the equivalent Python ones.
//! - [`chrono-tz`]: Enables a conversion from [chrono-tz]'s `Tz` enum. Requires Python 3.9+.
Expand Down Expand Up @@ -480,6 +481,9 @@ pub mod inspect;
// other paths to the same items. (e.g. `pyo3::types::PyAnyMethods` instead of `pyo3::prelude::PyAnyMethods`).
pub mod prelude;

#[cfg(any(Py_3_13, not(Py_LIMITED_API)))]
pub mod pymem_alloc;

/// Test readme and user guide
#[cfg(doctest)]
pub mod doc_test {
Expand Down
Loading