88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11- #![ unstable( feature = "allocator_api" ,
12- reason = "the precise API and guarantees it provides may be tweaked \
13- slightly, especially to possibly take into account the \
14- types being stored to make room for a future \
15- tracing garbage collector",
16- issue = "32838" ) ]
11+ //! Memory allocation APIs
12+
13+ #![ stable( feature = "alloc_module" , since = "1.28.0" ) ]
1714
1815use core:: intrinsics:: { min_align_of_val, size_of_val} ;
1916use core:: ptr:: { NonNull , Unique } ;
2017use core:: usize;
2118
19+ #[ stable( feature = "alloc_module" , since = "1.28.0" ) ]
2220#[ doc( inline) ]
2321pub use core:: alloc:: * ;
2422
@@ -37,67 +35,112 @@ extern "Rust" {
3735 fn __rust_alloc_zeroed ( size : usize , align : usize ) -> * mut u8 ;
3836}
3937
38+ /// The global memory allocator.
39+ ///
40+ /// This type implements the [`Alloc`] trait by forwarding calls
41+ /// to the allocator registered with the `#[global_allocator]` attribute
42+ /// if there is one, or the `std` crate’s default.
43+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
4044#[ derive( Copy , Clone , Default , Debug ) ]
4145pub struct Global ;
4246
43- #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
44- #[ rustc_deprecated( since = "1.27.0" , reason = "type renamed to `Global`" ) ]
45- pub type Heap = Global ;
46-
47- #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
48- #[ rustc_deprecated( since = "1.27.0" , reason = "type renamed to `Global`" ) ]
49- #[ allow( non_upper_case_globals) ]
50- pub const Heap : Global = Global ;
51-
52- unsafe impl GlobalAlloc for Global {
53- #[ inline]
54- unsafe fn alloc ( & self , layout : Layout ) -> * mut Opaque {
55- let ptr = __rust_alloc ( layout. size ( ) , layout. align ( ) ) ;
56- ptr as * mut Opaque
57- }
47+ /// Allocate memory with the global allocator.
48+ ///
49+ /// This function forwards calls to the [`GlobalAlloc::alloc`] method
50+ /// of the allocator registered with the `#[global_allocator]` attribute
51+ /// if there is one, or the `std` crate’s default.
52+ ///
53+ /// This function is expected to be deprecated in favor of the `alloc` method
54+ /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
55+ ///
56+ /// # Safety
57+ ///
58+ /// See [`GlobalAlloc::alloc`].
59+ #[ stable( feature = "global_alloc" , since = "1.28.0" ) ]
60+ #[ inline]
61+ pub unsafe fn alloc ( layout : Layout ) -> * mut u8 {
62+ __rust_alloc ( layout. size ( ) , layout. align ( ) )
63+ }
5864
59- #[ inline]
60- unsafe fn dealloc ( & self , ptr : * mut Opaque , layout : Layout ) {
61- __rust_dealloc ( ptr as * mut u8 , layout. size ( ) , layout. align ( ) )
62- }
65+ /// Deallocate memory with the global allocator.
66+ ///
67+ /// This function forwards calls to the [`GlobalAlloc::dealloc`] method
68+ /// of the allocator registered with the `#[global_allocator]` attribute
69+ /// if there is one, or the `std` crate’s default.
70+ ///
71+ /// This function is expected to be deprecated in favor of the `dealloc` method
72+ /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
73+ ///
74+ /// # Safety
75+ ///
76+ /// See [`GlobalAlloc::dealloc`].
77+ #[ stable( feature = "global_alloc" , since = "1.28.0" ) ]
78+ #[ inline]
79+ pub unsafe fn dealloc ( ptr : * mut u8 , layout : Layout ) {
80+ __rust_dealloc ( ptr, layout. size ( ) , layout. align ( ) )
81+ }
6382
64- #[ inline]
65- unsafe fn realloc ( & self , ptr : * mut Opaque , layout : Layout , new_size : usize ) -> * mut Opaque {
66- let ptr = __rust_realloc ( ptr as * mut u8 , layout. size ( ) , layout. align ( ) , new_size) ;
67- ptr as * mut Opaque
68- }
83+ /// Reallocate memory with the global allocator.
84+ ///
85+ /// This function forwards calls to the [`GlobalAlloc::realloc`] method
86+ /// of the allocator registered with the `#[global_allocator]` attribute
87+ /// if there is one, or the `std` crate’s default.
88+ ///
89+ /// This function is expected to be deprecated in favor of the `realloc` method
90+ /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
91+ ///
92+ /// # Safety
93+ ///
94+ /// See [`GlobalAlloc::realloc`].
95+ #[ stable( feature = "global_alloc" , since = "1.28.0" ) ]
96+ #[ inline]
97+ pub unsafe fn realloc ( ptr : * mut u8 , layout : Layout , new_size : usize ) -> * mut u8 {
98+ __rust_realloc ( ptr, layout. size ( ) , layout. align ( ) , new_size)
99+ }
69100
70- #[ inline]
71- unsafe fn alloc_zeroed ( & self , layout : Layout ) -> * mut Opaque {
72- let ptr = __rust_alloc_zeroed ( layout. size ( ) , layout. align ( ) ) ;
73- ptr as * mut Opaque
74- }
101+ /// Allocate zero-initialized memory with the global allocator.
102+ ///
103+ /// This function forwards calls to the [`GlobalAlloc::alloc_zeroed`] method
104+ /// of the allocator registered with the `#[global_allocator]` attribute
105+ /// if there is one, or the `std` crate’s default.
106+ ///
107+ /// This function is expected to be deprecated in favor of the `alloc_zeroed` method
108+ /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
109+ ///
110+ /// # Safety
111+ ///
112+ /// See [`GlobalAlloc::alloc_zeroed`].
113+ #[ stable( feature = "global_alloc" , since = "1.28.0" ) ]
114+ #[ inline]
115+ pub unsafe fn alloc_zeroed ( layout : Layout ) -> * mut u8 {
116+ __rust_alloc_zeroed ( layout. size ( ) , layout. align ( ) )
75117}
76118
119+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
77120unsafe impl Alloc for Global {
78121 #[ inline]
79- unsafe fn alloc ( & mut self , layout : Layout ) -> Result < NonNull < Opaque > , AllocErr > {
80- NonNull :: new ( GlobalAlloc :: alloc ( self , layout) ) . ok_or ( AllocErr )
122+ unsafe fn alloc ( & mut self , layout : Layout ) -> Result < NonNull < u8 > , AllocErr > {
123+ NonNull :: new ( alloc ( layout) ) . ok_or ( AllocErr )
81124 }
82125
83126 #[ inline]
84- unsafe fn dealloc ( & mut self , ptr : NonNull < Opaque > , layout : Layout ) {
85- GlobalAlloc :: dealloc ( self , ptr. as_ptr ( ) , layout)
127+ unsafe fn dealloc ( & mut self , ptr : NonNull < u8 > , layout : Layout ) {
128+ dealloc ( ptr. as_ptr ( ) , layout)
86129 }
87130
88131 #[ inline]
89132 unsafe fn realloc ( & mut self ,
90- ptr : NonNull < Opaque > ,
133+ ptr : NonNull < u8 > ,
91134 layout : Layout ,
92135 new_size : usize )
93- -> Result < NonNull < Opaque > , AllocErr >
136+ -> Result < NonNull < u8 > , AllocErr >
94137 {
95- NonNull :: new ( GlobalAlloc :: realloc ( self , ptr. as_ptr ( ) , layout, new_size) ) . ok_or ( AllocErr )
138+ NonNull :: new ( realloc ( ptr. as_ptr ( ) , layout, new_size) ) . ok_or ( AllocErr )
96139 }
97140
98141 #[ inline]
99- unsafe fn alloc_zeroed ( & mut self , layout : Layout ) -> Result < NonNull < Opaque > , AllocErr > {
100- NonNull :: new ( GlobalAlloc :: alloc_zeroed ( self , layout) ) . ok_or ( AllocErr )
142+ unsafe fn alloc_zeroed ( & mut self , layout : Layout ) -> Result < NonNull < u8 > , AllocErr > {
143+ NonNull :: new ( alloc_zeroed ( layout) ) . ok_or ( AllocErr )
101144 }
102145}
103146
@@ -111,9 +154,9 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
111154 align as * mut u8
112155 } else {
113156 let layout = Layout :: from_size_align_unchecked ( size, align) ;
114- let ptr = Global . alloc ( layout) ;
157+ let ptr = alloc ( layout) ;
115158 if !ptr. is_null ( ) {
116- ptr as * mut u8
159+ ptr
117160 } else {
118161 oom ( layout)
119162 }
@@ -129,10 +172,23 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
129172 // We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
130173 if size != 0 {
131174 let layout = Layout :: from_size_align_unchecked ( size, align) ;
132- Global . dealloc ( ptr as * mut Opaque , layout) ;
175+ dealloc ( ptr as * mut u8 , layout) ;
133176 }
134177}
135178
179+ /// Abort on memory allocation error or failure.
180+ ///
181+ /// Callers of memory allocation APIs wishing to abort computation
182+ /// in response to an allocation error are encouraged to call this function,
183+ /// rather than directly invoking `panic!` or similar.
184+ ///
185+ /// The default behavior of this function is to print a message to standard error
186+ /// and abort the process.
187+ /// It can be replaced with [`set_oom_hook`] and [`take_oom_hook`].
188+ ///
189+ /// [`set_oom_hook`]: ../../std/alloc/fn.set_oom_hook.html
190+ /// [`take_oom_hook`]: ../../std/alloc/fn.take_oom_hook.html
191+ #[ stable( feature = "global_alloc" , since = "1.28.0" ) ]
136192#[ rustc_allocator_nounwind]
137193pub fn oom ( layout : Layout ) -> ! {
138194 #[ allow( improper_ctypes) ]
0 commit comments