// option. This file may not be copied, modified, or distributed
// except according to those terms.
+use core::{isize, usize};
+
+#[inline(always)]
+fn check_size_and_alignment(size: usize, align: usize) {
+ debug_assert!(size != 0);
+ debug_assert!(size <= isize::MAX as usize, "Tried to allocate too much: {} bytes", size);
+ debug_assert!(usize::is_power_of_two(align), "Invalid alignment of allocation: {}", align);
+}
+
// FIXME: #13996: mark the `allocate` and `reallocate` return value as `noalias`
/// Return a pointer to `size` bytes of memory aligned to `align`.
/// size on the platform.
#[inline]
pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
+ check_size_and_alignment(size, align);
imp::allocate(size, align)
}
/// any value in range_inclusive(requested_size, usable_size).
#[inline]
pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
+ check_size_and_alignment(size, align);
imp::reallocate(ptr, old_size, size, align)
}
#[inline]
pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: usize, size: usize,
align: usize) -> usize {
+ check_size_and_alignment(size, align);
imp::reallocate_inplace(ptr, old_size, size, align)
}
/// The allocator for unique pointers.
#[cfg(not(test))]
-#[lang="exchange_malloc"]
+#[lang = "exchange_malloc"]
#[inline]
unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
if size == 0 {
}
#[cfg(not(test))]
-#[lang="exchange_free"]
+#[lang = "exchange_free"]
#[inline]
unsafe fn exchange_free(ptr: *mut u8, old_size: usize, align: usize) {
deallocate(ptr, old_size, align);
#[cfg(feature = "external_funcs")]
mod imp {
+ #[allow(improper_ctypes)]
extern {
fn rust_allocate(size: usize, align: usize) -> *mut u8;
fn rust_deallocate(ptr: *mut u8, old_size: usize, align: usize);
}
// -lpthread needs to occur after -ljemalloc, the earlier argument isn't enough
- #[cfg(all(not(windows), not(target_os = "android")))]
+ #[cfg(all(not(windows),
+ not(target_os = "android"),
+ not(target_env = "musl")))]
#[link(name = "pthread")]
extern {}
}
#[cfg(test)]
-mod test {
+mod tests {
extern crate test;
use self::test::Bencher;
use boxed::Box;