diff options
| author | joboet <jonasboettiger@icloud.com> | 2024-07-23 16:10:08 +0200 |
|---|---|---|
| committer | joboet <jonasboettiger@icloud.com> | 2024-08-27 11:58:19 +0200 |
| commit | d456814842e65a153a1de67960b892897a02ed14 (patch) | |
| tree | 2b7f252cd52ba3e785fedea291fe3f78c1edf206 /library/std/src/sys/alloc/solid.rs | |
| parent | a60a9e567a7319b33619f6551dc29522c6f58687 (diff) | |
| download | rust-d456814842e65a153a1de67960b892897a02ed14.tar.gz rust-d456814842e65a153a1de67960b892897a02ed14.zip | |
std: move allocators to `sys`
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) + } + } + } +} |
