File tree 1 file changed +20
-6
lines changed
1 file changed +20
-6
lines changed Original file line number Diff line number Diff line change 6
6
//@ run-fail
7
7
//@ compile-flags: -Cprefer-dynamic=no
8
8
9
- pub struct AbortingAllocator ;
9
+ use std:: { sync:: atomic:: { AtomicBool , Ordering } , alloc:: System } ;
10
+
11
+ static ABORT : AtomicBool = AtomicBool :: new ( true ) ;
12
+
13
+ pub struct AbortingAllocator ( System ) ;
10
14
11
15
unsafe impl std:: alloc:: GlobalAlloc for AbortingAllocator {
12
- unsafe fn alloc ( & self , _: std:: alloc:: Layout ) -> * mut u8 {
13
- std:: process:: abort ( )
16
+ unsafe fn alloc ( & self , layout : std:: alloc:: Layout ) -> * mut u8 {
17
+ if ABORT . swap ( false , Ordering :: SeqCst ) {
18
+ println ! ( "{}" , std:: backtrace:: Backtrace :: force_capture( ) ) ;
19
+ std:: process:: abort ( ) ;
20
+ }
21
+
22
+ self . 0 . alloc ( layout)
14
23
}
15
24
16
- unsafe fn dealloc ( & self , _: * mut u8 , _: std:: alloc:: Layout ) {
17
- std:: process:: abort ( )
25
+ unsafe fn dealloc ( & self , ptr : * mut u8 , layout : std:: alloc:: Layout ) {
26
+ if ABORT . swap ( false , Ordering :: SeqCst ) {
27
+ println ! ( "{}" , std:: backtrace:: Backtrace :: force_capture( ) ) ;
28
+ std:: process:: abort ( ) ;
29
+ }
30
+
31
+ self . 0 . dealloc ( ptr, layout)
18
32
}
19
33
}
20
34
21
35
#[ global_allocator]
22
- static ALLOCATOR : AbortingAllocator = AbortingAllocator ;
36
+ static ALLOCATOR : AbortingAllocator = AbortingAllocator ( System ) ;
23
37
24
38
fn main ( ) {
25
39
std:: hint:: black_box ( String :: from ( "An allocation" ) ) ;
You can’t perform that action at this time.
0 commit comments