2019-02-11 04:23:21 +09:00
|
|
|
use crate::alloc::{GlobalAlloc, Layout, System};
|
|
|
|
|
use crate::ptr;
|
2021-02-24 19:16:24 +01:00
|
|
|
use crate::sys::common::alloc::{realloc_fallback, MIN_ALIGN};
|
2018-11-03 11:15:48 -07:00
|
|
|
|
|
|
|
|
#[stable(feature = "alloc_system_type", since = "1.28.0")]
|
|
|
|
|
unsafe impl GlobalAlloc for System {
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
2019-07-02 09:17:38 +02:00
|
|
|
// jemalloc provides alignment less than MIN_ALIGN for small allocations.
|
|
|
|
|
// So only rely on MIN_ALIGN if size >= align.
|
2019-07-02 12:51:00 +02:00
|
|
|
// Also see <https://github.com/rust-lang/rust/issues/45955> and
|
|
|
|
|
// <https://github.com/rust-lang/rust/issues/62251#issuecomment-507580914>.
|
2018-11-03 11:15:48 -07:00
|
|
|
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
|
|
|
|
|
libc::malloc(layout.size()) as *mut u8
|
|
|
|
|
} else {
|
2024-05-06 07:56:41 +02:00
|
|
|
// `posix_memalign` returns a non-aligned value if supplied a very
|
|
|
|
|
// large alignment on older versions of Apple's platforms (unknown
|
|
|
|
|
// exactly which version range, but the issue is definitely
|
|
|
|
|
// present in macOS 10.14 and iOS 13.3).
|
|
|
|
|
//
|
|
|
|
|
// <https://github.com/rust-lang/rust/issues/30170>
|
|
|
|
|
#[cfg(target_vendor = "apple")]
|
2018-11-03 11:15:48 -07:00
|
|
|
{
|
|
|
|
|
if layout.align() > (1 << 31) {
|
|
|
|
|
return ptr::null_mut();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
aligned_malloc(&layout)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
|
2019-07-02 12:51:00 +02:00
|
|
|
// See the comment above in `alloc` for why this check looks the way it does.
|
2018-11-03 11:15:48 -07:00
|
|
|
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
|
|
|
|
|
libc::calloc(layout.size(), 1) as *mut u8
|
|
|
|
|
} else {
|
2019-07-18 14:16:04 +02:00
|
|
|
let ptr = self.alloc(layout);
|
2018-11-03 11:15:48 -07:00
|
|
|
if !ptr.is_null() {
|
|
|
|
|
ptr::write_bytes(ptr, 0, layout.size());
|
|
|
|
|
}
|
|
|
|
|
ptr
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
|
|
|
|
|
libc::free(ptr as *mut libc::c_void)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 11:00:41 -07:00
|
|
|
cfg_if::cfg_if! {
|
2024-05-18 12:49:01 +02:00
|
|
|
// We use posix_memalign wherever possible, but not all targets have that function.
|
2020-08-20 11:00:41 -07:00
|
|
|
if #[cfg(any(
|
|
|
|
|
target_os = "redox",
|
2021-10-19 17:59:59 +02:00
|
|
|
target_os = "espidf",
|
2023-05-07 11:20:01 +03:00
|
|
|
target_os = "horizon",
|
|
|
|
|
target_os = "vita",
|
2020-08-20 11:00:41 -07:00
|
|
|
))] {
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
|
|
|
|
|
libc::memalign(layout.align(), layout.size()) as *mut u8
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
|
|
|
|
|
let mut out = ptr::null_mut();
|
2024-05-14 19:32:11 +02:00
|
|
|
// We prefer posix_memalign over aligned_malloc since with aligned_malloc,
|
|
|
|
|
// implementations are making almost arbitrary choices for which alignments are
|
|
|
|
|
// "supported", making it hard to use. For instance, some implementations require the
|
|
|
|
|
// size to be a multiple of the alignment (wasi emmalloc), while others require the
|
|
|
|
|
// alignment to be at least the pointer size (Illumos, macOS) -- which may or may not be
|
|
|
|
|
// standards-compliant, but that does not help us.
|
|
|
|
|
// posix_memalign only has one, clear requirement: that the alignment be a multiple of
|
|
|
|
|
// `sizeof(void*)`. Since these are all powers of 2, we can just use max.
|
2020-08-20 11:00:41 -07:00
|
|
|
let align = layout.align().max(crate::mem::size_of::<usize>());
|
|
|
|
|
let ret = libc::posix_memalign(&mut out, align, layout.size());
|
|
|
|
|
if ret != 0 { ptr::null_mut() } else { out as *mut u8 }
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-03 11:15:48 -07:00
|
|
|
}
|