diff options
| author | bors <bors@rust-lang.org> | 2024-08-27 13:51:39 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-08-27 13:51:39 +0000 |
| commit | 600edc948ab5de7a92538bcc2f49cb8d47925e2d (patch) | |
| tree | d4de701336264eaff7e4e2b389af9e4588f7d13e /library/std/src/sys/alloc/solid.rs | |
| parent | ae9f5019f9ce6eb3ecd96206ade4a612efe20fd5 (diff) | |
| parent | e9566ce9e92df5fbdf87260cef7f3aa8d096e1bd (diff) | |
| download | rust-600edc948ab5de7a92538bcc2f49cb8d47925e2d.tar.gz rust-600edc948ab5de7a92538bcc2f49cb8d47925e2d.zip | |
Auto merge of #128134 - joboet:move_pal_alloc, r=cupiver
std: move allocators to `sys` Part of #117276.
Diffstat (limited to 'library/std/src/sys/alloc/solid.rs')
| -rw-r--r-- | library/std/src/sys/alloc/solid.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/library/std/src/sys/alloc/solid.rs b/library/std/src/sys/alloc/solid.rs new file mode 100644 index 00000000000..abb534a1c5c --- /dev/null +++ b/library/std/src/sys/alloc/solid.rs @@ -0,0 +1,30 @@ +use super::{realloc_fallback, MIN_ALIGN}; +use crate::alloc::{GlobalAlloc, Layout, System}; + +#[stable(feature = "alloc_system_type", since = "1.28.0")] +unsafe impl GlobalAlloc for System { + #[inline] + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { + unsafe { libc::malloc(layout.size()) as *mut u8 } + } else { + unsafe { libc::memalign(layout.align(), layout.size()) as *mut u8 } + } + } + + #[inline] + unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { + unsafe { libc::free(ptr as *mut libc::c_void) } + } + + #[inline] + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { + unsafe { + if layout.align() <= MIN_ALIGN && layout.align() <= new_size { + libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 + } else { + realloc_fallback(self, ptr, layout, new_size) + } + } + } +} |
