@@ -16,34 +16,28 @@ use core::ptr::{self, NonNull};
16
16
#[ doc( inline) ]
17
17
pub use core:: alloc:: * ;
18
18
19
- use core:: marker:: Destruct ;
20
-
21
19
#[ cfg( test) ]
22
20
mod tests;
23
21
24
22
extern "Rust" {
25
- // These are the magic symbols to call the global allocator. rustc generates
23
+ // These are the magic symbols to call the global allocator. rustc generates
26
24
// them to call `__rg_alloc` etc. if there is a `#[global_allocator]` attribute
27
25
// (the code expanding that attribute macro generates those functions), or to call
28
- // the default implementations in libstd (`__rdl_alloc` etc. in `library/std/src/alloc.rs`)
26
+ // the default implementations in std (`__rdl_alloc` etc. in `library/std/src/alloc.rs`)
29
27
// otherwise.
30
28
// The rustc fork of LLVM 14 and earlier also special-cases these function names to be able to optimize them
31
29
// like `malloc`, `realloc`, and `free`, respectively.
32
30
#[ rustc_allocator]
33
- #[ cfg_attr( not( bootstrap) , rustc_nounwind) ]
34
- #[ cfg_attr( bootstrap, rustc_allocator_nounwind) ]
31
+ #[ rustc_nounwind]
35
32
fn __rust_alloc ( size : usize , align : usize ) -> * mut u8 ;
36
33
#[ rustc_deallocator]
37
- #[ cfg_attr( not( bootstrap) , rustc_nounwind) ]
38
- #[ cfg_attr( bootstrap, rustc_allocator_nounwind) ]
34
+ #[ rustc_nounwind]
39
35
fn __rust_dealloc ( ptr : * mut u8 , size : usize , align : usize ) ;
40
36
#[ rustc_reallocator]
41
- #[ cfg_attr( not( bootstrap) , rustc_nounwind) ]
42
- #[ cfg_attr( bootstrap, rustc_allocator_nounwind) ]
37
+ #[ rustc_nounwind]
43
38
fn __rust_realloc ( ptr : * mut u8 , old_size : usize , align : usize , new_size : usize ) -> * mut u8 ;
44
39
#[ rustc_allocator_zeroed]
45
- #[ cfg_attr( not( bootstrap) , rustc_nounwind) ]
46
- #[ cfg_attr( bootstrap, rustc_allocator_nounwind) ]
40
+ #[ rustc_nounwind]
47
41
fn __rust_alloc_zeroed ( size : usize , align : usize ) -> * mut u8 ;
48
42
}
49
43
@@ -337,16 +331,12 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
337
331
338
332
#[ cfg_attr( not( test) , lang = "box_free" ) ]
339
333
#[ inline]
340
- #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
341
334
// This signature has to be the same as `Box`, otherwise an ICE will happen.
342
335
// When an additional parameter to `Box` is added (like `A: Allocator`), this has to be added here as
343
336
// well.
344
337
// For example if `Box` is changed to `struct Box<T: ?Sized, A: Allocator>(Unique<T>, A)`,
345
338
// this function has to be changed to `fn box_free<T: ?Sized, A: Allocator>(Unique<T>, A)` as well.
346
- pub ( crate ) const unsafe fn box_free < T : ?Sized , A : ~const Allocator + ~const Destruct > (
347
- ptr : Unique < T > ,
348
- alloc : A ,
349
- ) {
339
+ pub ( crate ) unsafe fn box_free < T : ?Sized , A : Allocator > ( ptr : Unique < T > , alloc : A ) {
350
340
unsafe {
351
341
let size = size_of_val ( ptr. as_ref ( ) ) ;
352
342
let align = min_align_of_val ( ptr. as_ref ( ) ) ;
@@ -359,7 +349,7 @@ pub(crate) const unsafe fn box_free<T: ?Sized, A: ~const Allocator + ~const Dest
359
349
360
350
#[ cfg( not( no_global_oom_handling) ) ]
361
351
extern "Rust" {
362
- // This is the magic symbol to call the global alloc error handler. rustc generates
352
+ // This is the magic symbol to call the global alloc error handler. rustc generates
363
353
// it to call `__rg_oom` if there is a `#[alloc_error_handler]`, or to call the
364
354
// default implementations below (`__rdl_oom`) otherwise.
365
355
fn __rust_alloc_error_handler ( size : usize , align : usize ) -> !;
@@ -404,25 +394,24 @@ pub use std::alloc::handle_alloc_error;
404
394
#[ allow( unused_attributes) ]
405
395
#[ unstable( feature = "alloc_internals" , issue = "none" ) ]
406
396
pub mod __alloc_error_handler {
407
- use crate :: alloc:: Layout ;
408
-
409
- // called via generated `__rust_alloc_error_handler`
410
-
411
- // if there is no `#[alloc_error_handler]`
397
+ // called via generated `__rust_alloc_error_handler` if there is no
398
+ // `#[alloc_error_handler]`.
412
399
#[ rustc_std_internal_symbol]
413
400
pub unsafe fn __rdl_oom ( size : usize , _align : usize ) -> ! {
414
- panic ! ( "memory allocation of {size} bytes failed" )
415
- }
416
-
417
- // if there is an `#[alloc_error_handler]`
418
- #[ rustc_std_internal_symbol]
419
- pub unsafe fn __rg_oom ( size : usize , align : usize ) -> ! {
420
- let layout = unsafe { Layout :: from_size_align_unchecked ( size, align) } ;
421
401
extern "Rust" {
422
- #[ lang = "oom" ]
423
- fn oom_impl ( layout : Layout ) -> !;
402
+ // This symbol is emitted by rustc next to __rust_alloc_error_handler.
403
+ // Its value depends on the -Zoom={panic,abort} compiler option.
404
+ static __rust_alloc_error_handler_should_panic: u8 ;
405
+ }
406
+
407
+ #[ allow( unused_unsafe) ]
408
+ if unsafe { __rust_alloc_error_handler_should_panic != 0 } {
409
+ panic ! ( "memory allocation of {size} bytes failed" )
410
+ } else {
411
+ core:: panicking:: panic_nounwind_fmt ( format_args ! (
412
+ "memory allocation of {size} bytes failed"
413
+ ) )
424
414
}
425
- unsafe { oom_impl ( layout) }
426
415
}
427
416
}
428
417
0 commit comments