|
57 | 57 | #![stable(feature = "alloc_module", since = "1.28.0")] |
58 | 58 |
|
59 | 59 | use core::intrinsics; |
60 | | -use core::ptr; |
61 | 60 | use core::ptr::NonNull; |
| 61 | +use core::sync::atomic::{AtomicPtr, Ordering}; |
| 62 | +use core::{mem, ptr}; |
62 | 63 |
|
63 | 64 | #[stable(feature = "alloc_module", since = "1.28.0")] |
64 | 65 | #[doc(inline)] |
@@ -285,6 +286,76 @@ unsafe impl Allocator for System { |
285 | 286 | } |
286 | 287 | } |
287 | 288 |
|
| 289 | +static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut()); |
| 290 | + |
| 291 | +/// Registers a custom allocation error hook, replacing any that was previously registered. |
| 292 | +/// |
| 293 | +/// The allocation error hook is invoked when an infallible memory allocation fails, before |
| 294 | +/// the runtime aborts. The default hook prints a message to standard error, |
| 295 | +/// but this behavior can be customized with the [`set_alloc_error_hook`] and |
| 296 | +/// [`take_alloc_error_hook`] functions. |
| 297 | +/// |
| 298 | +/// The hook is provided with a `Layout` struct which contains information |
| 299 | +/// about the allocation that failed. |
| 300 | +/// |
| 301 | +/// The allocation error hook is a global resource. |
| 302 | +/// |
| 303 | +/// # Examples |
| 304 | +/// |
| 305 | +/// ``` |
| 306 | +/// #![feature(alloc_error_hook)] |
| 307 | +/// |
| 308 | +/// use std::alloc::{Layout, set_alloc_error_hook}; |
| 309 | +/// |
| 310 | +/// fn custom_alloc_error_hook(layout: Layout) { |
| 311 | +/// panic!("memory allocation of {} bytes failed", layout.size()); |
| 312 | +/// } |
| 313 | +/// |
| 314 | +/// set_alloc_error_hook(custom_alloc_error_hook); |
| 315 | +/// ``` |
| 316 | +#[unstable(feature = "alloc_error_hook", issue = "51245")] |
| 317 | +pub fn set_alloc_error_hook(hook: fn(Layout)) { |
| 318 | + HOOK.store(hook as *mut (), Ordering::SeqCst); |
| 319 | +} |
| 320 | + |
| 321 | +/// Unregisters the current allocation error hook, returning it. |
| 322 | +/// |
| 323 | +/// *See also the function [`set_alloc_error_hook`].* |
| 324 | +/// |
| 325 | +/// If no custom hook is registered, the default hook will be returned. |
| 326 | +#[unstable(feature = "alloc_error_hook", issue = "51245")] |
| 327 | +pub fn take_alloc_error_hook() -> fn(Layout) { |
| 328 | + let hook = HOOK.swap(ptr::null_mut(), Ordering::SeqCst); |
| 329 | + if hook.is_null() { default_alloc_error_hook } else { unsafe { mem::transmute(hook) } } |
| 330 | +} |
| 331 | + |
| 332 | +fn default_alloc_error_hook(layout: Layout) { |
| 333 | + extern "Rust" { |
| 334 | + // This symbol is emitted by rustc next to __rust_alloc_error_handler. |
| 335 | + // Its value depends on the -Zoom={panic,abort} compiler option. |
| 336 | + static __rust_alloc_error_handler_should_panic: u8; |
| 337 | + } |
| 338 | + |
| 339 | + #[allow(unused_unsafe)] |
| 340 | + if unsafe { __rust_alloc_error_handler_should_panic != 0 } { |
| 341 | + panic!("memory allocation of {} bytes failed", layout.size()); |
| 342 | + } else { |
| 343 | + rtprintpanic!("memory allocation of {} bytes failed\n", layout.size()); |
| 344 | + } |
| 345 | +} |
| 346 | + |
| 347 | +#[cfg(not(test))] |
| 348 | +#[doc(hidden)] |
| 349 | +#[alloc_error_handler] |
| 350 | +#[unstable(feature = "alloc_internals", issue = "none")] |
| 351 | +pub fn rust_oom(layout: Layout) -> ! { |
| 352 | + let hook = HOOK.load(Ordering::SeqCst); |
| 353 | + let hook: fn(Layout) = |
| 354 | + if hook.is_null() { default_alloc_error_hook } else { unsafe { mem::transmute(hook) } }; |
| 355 | + hook(layout); |
| 356 | + crate::process::abort() |
| 357 | +} |
| 358 | + |
288 | 359 | #[cfg(not(test))] |
289 | 360 | #[doc(hidden)] |
290 | 361 | #[allow(unused_attributes)] |
|
0 commit comments