#![stable(feature = "alloc_module", since = "1.28.0")]
+use core::ptr::NonNull;
use core::sync::atomic::{AtomicPtr, Ordering};
use core::{mem, ptr};
-use core::ptr::NonNull;
use crate::sys_common::util::dumb_print;
}
#[inline]
- unsafe fn realloc(&mut self,
- ptr: NonNull<u8>,
- layout: Layout,
- new_size: usize) -> Result<NonNull<u8>, AllocErr> {
+ unsafe fn realloc(
+ &mut self,
+ ptr: NonNull<u8>,
+ layout: Layout,
+ new_size: usize,
+ ) -> Result<NonNull<u8>, AllocErr> {
NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
}
}
/// about the allocation that failed.
///
/// The allocation error hook is a global resource.
+///
+/// [`set_alloc_error_hook`]: fn.set_alloc_error_hook.html
+/// [`take_alloc_error_hook`]: fn.take_alloc_error_hook.html
#[unstable(feature = "alloc_error_hook", issue = "51245")]
pub fn set_alloc_error_hook(hook: fn(Layout)) {
HOOK.store(hook as *mut (), Ordering::SeqCst);
/// *See also the function [`set_alloc_error_hook`].*
///
/// If no custom hook is registered, the default hook will be returned.
+///
+/// [`set_alloc_error_hook`]: fn.set_alloc_error_hook.html
#[unstable(feature = "alloc_error_hook", issue = "51245")]
pub fn take_alloc_error_hook() -> fn(Layout) {
let hook = HOOK.swap(ptr::null_mut(), Ordering::SeqCst);
- if hook.is_null() {
- default_alloc_error_hook
- } else {
- unsafe { mem::transmute(hook) }
- }
+ if hook.is_null() { default_alloc_error_hook } else { unsafe { mem::transmute(hook) } }
}
fn default_alloc_error_hook(layout: Layout) {
#[cfg(not(test))]
#[doc(hidden)]
#[alloc_error_handler]
-#[unstable(feature = "alloc_internals", issue = "0")]
+#[unstable(feature = "alloc_internals", issue = "none")]
pub fn rust_oom(layout: Layout) -> ! {
let hook = HOOK.load(Ordering::SeqCst);
- let hook: fn(Layout) = if hook.is_null() {
- default_alloc_error_hook
- } else {
- unsafe { mem::transmute(hook) }
- };
+ let hook: fn(Layout) =
+ if hook.is_null() { default_alloc_error_hook } else { unsafe { mem::transmute(hook) } };
hook(layout);
- unsafe { crate::sys::abort_internal(); }
+ unsafe {
+ crate::sys::abort_internal();
+ }
}
#[cfg(not(test))]
#[doc(hidden)]
#[allow(unused_attributes)]
-#[unstable(feature = "alloc_internals", issue = "0")]
+#[unstable(feature = "alloc_internals", issue = "none")]
pub mod __default_lib_allocator {
- use super::{System, Layout, GlobalAlloc};
+ use super::{GlobalAlloc, Layout, System};
// These magic symbol names are used as a fallback for implementing the
// `__rust_alloc` etc symbols (see `src/liballoc/alloc.rs) when there is
// no `#[global_allocator]` attribute.
// ABI
#[rustc_std_internal_symbol]
- pub unsafe extern fn __rdl_alloc(size: usize, align: usize) -> *mut u8 {
+ pub unsafe extern "C" fn __rdl_alloc(size: usize, align: usize) -> *mut u8 {
let layout = Layout::from_size_align_unchecked(size, align);
System.alloc(layout)
}
#[rustc_std_internal_symbol]
- pub unsafe extern fn __rdl_dealloc(ptr: *mut u8,
- size: usize,
- align: usize) {
+ pub unsafe extern "C" fn __rdl_dealloc(ptr: *mut u8, size: usize, align: usize) {
System.dealloc(ptr, Layout::from_size_align_unchecked(size, align))
}
#[rustc_std_internal_symbol]
- pub unsafe extern fn __rdl_realloc(ptr: *mut u8,
- old_size: usize,
- align: usize,
- new_size: usize) -> *mut u8 {
+ pub unsafe extern "C" fn __rdl_realloc(
+ ptr: *mut u8,
+ old_size: usize,
+ align: usize,
+ new_size: usize,
+ ) -> *mut u8 {
let old_layout = Layout::from_size_align_unchecked(old_size, align);
System.realloc(ptr, old_layout, new_size)
}
#[rustc_std_internal_symbol]
- pub unsafe extern fn __rdl_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
+ pub unsafe extern "C" fn __rdl_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
let layout = Layout::from_size_align_unchecked(size, align);
System.alloc_zeroed(layout)
}