Skip to content

Commit dda5516

Browse files
Add allocator functions to c-api (#8168)
1 parent 4d3ddac commit dda5516

5 files changed

Lines changed: 50 additions & 0 deletions

File tree

.cspell.dict/cpython.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ pyerrors
192192
Pyfunc
193193
pylifecycle
194194
pymain
195+
pymem
195196
pyrepl
196197
pystate
197198
PYTHONTRACEMALLOC

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/capi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ crate-type = ["cdylib", "rlib"]
1414
[dependencies]
1515
bitflags = { workspace = true }
1616
itertools = { workspace = true }
17+
libc = { workspace = true }
1718
num-complex = { workspace = true }
1819
rustpython-vm = { workspace = true, features = ["threading", "compiler", "importlib", "host_env"] }
1920
rustpython-stdlib = {workspace = true, features = ["threading"] }

crates/capi/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub mod object;
2626
pub mod pycapsule;
2727
pub mod pyerrors;
2828
pub mod pylifecycle;
29+
pub mod pymem;
2930
pub mod pystate;
3031
pub mod refcount;
3132
pub mod setobject;

crates/capi/src/pymem.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use core::ffi::c_void;
2+
3+
#[unsafe(no_mangle)]
4+
pub unsafe extern "C" fn PyMem_Malloc(n: usize) -> *mut c_void {
5+
unsafe { libc::malloc(if n == 0 { 1 } else { n }) }
6+
}
7+
8+
#[unsafe(no_mangle)]
9+
pub unsafe extern "C" fn PyMem_Calloc(nelem: usize, elsize: usize) -> *mut c_void {
10+
unsafe {
11+
libc::calloc(
12+
if nelem == 0 || elsize == 0 { 1 } else { nelem },
13+
if nelem == 0 || elsize == 0 { 1 } else { elsize },
14+
)
15+
}
16+
}
17+
18+
#[unsafe(no_mangle)]
19+
pub unsafe extern "C" fn PyMem_Realloc(ptr: *mut c_void, new_size: usize) -> *mut c_void {
20+
unsafe { libc::realloc(ptr, if new_size == 0 { 1 } else { new_size }) }
21+
}
22+
23+
#[unsafe(no_mangle)]
24+
pub unsafe extern "C" fn PyMem_Free(ptr: *mut c_void) {
25+
unsafe { libc::free(ptr) }
26+
}
27+
28+
#[unsafe(no_mangle)]
29+
pub unsafe extern "C" fn PyMem_RawMalloc(n: usize) -> *mut c_void {
30+
unsafe { libc::malloc(n) }
31+
}
32+
33+
#[unsafe(no_mangle)]
34+
pub unsafe extern "C" fn PyMem_RawCalloc(nelem: usize, elsize: usize) -> *mut c_void {
35+
unsafe { libc::calloc(nelem, elsize) }
36+
}
37+
38+
#[unsafe(no_mangle)]
39+
pub unsafe extern "C" fn PyMem_RawRealloc(ptr: *mut c_void, new_size: usize) -> *mut c_void {
40+
unsafe { libc::realloc(ptr, new_size) }
41+
}
42+
43+
#[unsafe(no_mangle)]
44+
pub unsafe extern "C" fn PyMem_RawFree(ptr: *mut c_void) {
45+
unsafe { libc::free(ptr) }
46+
}

0 commit comments

Comments
 (0)