16
16
issue = "32838" ) ]
17
17
18
18
use core:: intrinsics:: { min_align_of_val, size_of_val} ;
19
- use core:: mem;
20
19
use core:: usize;
21
20
22
21
#[ doc( inline) ]
23
22
pub use core:: alloc:: * ;
24
23
24
+ #[ cfg( stage0) ]
25
25
extern "Rust" {
26
26
#[ allocator]
27
27
#[ rustc_allocator_nounwind]
28
28
fn __rust_alloc ( size : usize , align : usize , err : * mut u8 ) -> * mut u8 ;
29
- #[ cold]
30
- #[ rustc_allocator_nounwind]
31
- fn __rust_oom ( err : * const u8 ) -> !;
32
29
#[ rustc_allocator_nounwind]
33
30
fn __rust_dealloc ( ptr : * mut u8 , size : usize , align : usize ) ;
34
31
#[ rustc_allocator_nounwind]
35
- fn __rust_usable_size ( layout : * const u8 ,
36
- min : * mut usize ,
37
- max : * mut usize ) ;
38
- #[ rustc_allocator_nounwind]
39
32
fn __rust_realloc ( ptr : * mut u8 ,
40
33
old_size : usize ,
41
34
old_align : usize ,
@@ -44,31 +37,22 @@ extern "Rust" {
44
37
err : * mut u8 ) -> * mut u8 ;
45
38
#[ rustc_allocator_nounwind]
46
39
fn __rust_alloc_zeroed ( size : usize , align : usize , err : * mut u8 ) -> * mut u8 ;
40
+ }
41
+
42
+ #[ cfg( not( stage0) ) ]
43
+ extern "Rust" {
44
+ #[ allocator]
47
45
#[ rustc_allocator_nounwind]
48
- fn __rust_alloc_excess ( size : usize ,
49
- align : usize ,
50
- excess : * mut usize ,
51
- err : * mut u8 ) -> * mut u8 ;
46
+ fn __rust_alloc ( size : usize , align : usize ) -> * mut u8 ;
52
47
#[ rustc_allocator_nounwind]
53
- fn __rust_realloc_excess ( ptr : * mut u8 ,
54
- old_size : usize ,
55
- old_align : usize ,
56
- new_size : usize ,
57
- new_align : usize ,
58
- excess : * mut usize ,
59
- err : * mut u8 ) -> * mut u8 ;
48
+ fn __rust_dealloc ( ptr : * mut u8 , size : usize , align : usize ) ;
60
49
#[ rustc_allocator_nounwind]
61
- fn __rust_grow_in_place ( ptr : * mut u8 ,
62
- old_size : usize ,
63
- old_align : usize ,
64
- new_size : usize ,
65
- new_align : usize ) -> u8 ;
50
+ fn __rust_realloc ( ptr : * mut u8 ,
51
+ old_size : usize ,
52
+ align : usize ,
53
+ new_size : usize ) -> * mut u8 ;
66
54
#[ rustc_allocator_nounwind]
67
- fn __rust_shrink_in_place ( ptr : * mut u8 ,
68
- old_size : usize ,
69
- old_align : usize ,
70
- new_size : usize ,
71
- new_align : usize ) -> u8 ;
55
+ fn __rust_alloc_zeroed ( size : usize , align : usize ) -> * mut u8 ;
72
56
}
73
57
74
58
#[ derive( Copy , Clone , Default , Debug ) ]
@@ -86,22 +70,15 @@ pub const Heap: Global = Global;
86
70
unsafe impl Alloc for Global {
87
71
#[ inline]
88
72
unsafe fn alloc ( & mut self , layout : Layout ) -> Result < * mut u8 , AllocErr > {
89
- let mut err = AllocErr ;
90
- let ptr = __rust_alloc ( layout. size ( ) ,
91
- layout. align ( ) ,
92
- & mut err as * mut AllocErr as * mut u8 ) ;
93
- if ptr. is_null ( ) {
94
- Err ( AllocErr )
95
- } else {
96
- Ok ( ptr)
97
- }
98
- }
73
+ #[ cfg( not( stage0) ) ]
74
+ let ptr = __rust_alloc ( layout. size ( ) , layout. align ( ) ) ;
75
+ #[ cfg( stage0) ]
76
+ let ptr = __rust_alloc ( layout. size ( ) , layout. align ( ) , & mut 0 ) ;
99
77
100
- #[ inline]
101
- #[ cold]
102
- fn oom ( & mut self , err : AllocErr ) -> ! {
103
- unsafe {
104
- __rust_oom ( & err as * const AllocErr as * const u8 )
78
+ if !ptr. is_null ( ) {
79
+ Ok ( ptr)
80
+ } else {
81
+ Err ( AllocErr )
105
82
}
106
83
}
107
84
@@ -110,126 +87,41 @@ unsafe impl Alloc for Global {
110
87
__rust_dealloc ( ptr, layout. size ( ) , layout. align ( ) )
111
88
}
112
89
113
- #[ inline]
114
- fn usable_size ( & self , layout : & Layout ) -> ( usize , usize ) {
115
- let mut min = 0 ;
116
- let mut max = 0 ;
117
- unsafe {
118
- __rust_usable_size ( layout as * const Layout as * const u8 ,
119
- & mut min,
120
- & mut max) ;
121
- }
122
- ( min, max)
123
- }
124
-
125
90
#[ inline]
126
91
unsafe fn realloc ( & mut self ,
127
92
ptr : * mut u8 ,
128
93
layout : Layout ,
129
94
new_layout : Layout )
130
95
-> Result < * mut u8 , AllocErr >
131
96
{
132
- let mut err = AllocErr ;
133
- let ptr = __rust_realloc ( ptr,
134
- layout. size ( ) ,
135
- layout. align ( ) ,
136
- new_layout. size ( ) ,
137
- new_layout. align ( ) ,
138
- & mut err as * mut AllocErr as * mut u8 ) ;
139
- if ptr. is_null ( ) {
140
- Err ( AllocErr )
97
+ if layout. align ( ) == new_layout. align ( ) {
98
+ #[ cfg( not( stage0) ) ]
99
+ let ptr = __rust_realloc ( ptr, layout. size ( ) , layout. align ( ) , new_layout. size ( ) ) ;
100
+ #[ cfg( stage0) ]
101
+ let ptr = __rust_realloc ( ptr, layout. size ( ) , layout. align ( ) ,
102
+ new_layout. size ( ) , new_layout. align ( ) , & mut 0 ) ;
103
+
104
+ if !ptr. is_null ( ) {
105
+ Ok ( ptr)
106
+ } else {
107
+ Err ( AllocErr )
108
+ }
141
109
} else {
142
- mem:: forget ( err) ;
143
- Ok ( ptr)
110
+ Err ( AllocErr )
144
111
}
145
112
}
146
113
147
114
#[ inline]
148
115
unsafe fn alloc_zeroed ( & mut self , layout : Layout ) -> Result < * mut u8 , AllocErr > {
149
- let mut err = AllocErr ;
150
- let ptr = __rust_alloc_zeroed ( layout. size ( ) ,
151
- layout. align ( ) ,
152
- & mut err as * mut AllocErr as * mut u8 ) ;
153
- if ptr. is_null ( ) {
154
- Err ( AllocErr )
155
- } else {
156
- Ok ( ptr)
157
- }
158
- }
116
+ #[ cfg( not( stage0) ) ]
117
+ let ptr = __rust_alloc_zeroed ( layout. size ( ) , layout. align ( ) ) ;
118
+ #[ cfg( stage0) ]
119
+ let ptr = __rust_alloc_zeroed ( layout. size ( ) , layout. align ( ) , & mut 0 ) ;
159
120
160
- #[ inline]
161
- unsafe fn alloc_excess ( & mut self , layout : Layout ) -> Result < Excess , AllocErr > {
162
- let mut err = AllocErr ;
163
- let mut size = 0 ;
164
- let ptr = __rust_alloc_excess ( layout. size ( ) ,
165
- layout. align ( ) ,
166
- & mut size,
167
- & mut err as * mut AllocErr as * mut u8 ) ;
168
- if ptr. is_null ( ) {
169
- Err ( AllocErr )
121
+ if !ptr. is_null ( ) {
122
+ Ok ( ptr)
170
123
} else {
171
- Ok ( Excess ( ptr, size) )
172
- }
173
- }
174
-
175
- #[ inline]
176
- unsafe fn realloc_excess ( & mut self ,
177
- ptr : * mut u8 ,
178
- layout : Layout ,
179
- new_layout : Layout ) -> Result < Excess , AllocErr > {
180
- let mut err = AllocErr ;
181
- let mut size = 0 ;
182
- let ptr = __rust_realloc_excess ( ptr,
183
- layout. size ( ) ,
184
- layout. align ( ) ,
185
- new_layout. size ( ) ,
186
- new_layout. align ( ) ,
187
- & mut size,
188
- & mut err as * mut AllocErr as * mut u8 ) ;
189
- if ptr. is_null ( ) {
190
124
Err ( AllocErr )
191
- } else {
192
- Ok ( Excess ( ptr, size) )
193
- }
194
- }
195
-
196
- #[ inline]
197
- unsafe fn grow_in_place ( & mut self ,
198
- ptr : * mut u8 ,
199
- layout : Layout ,
200
- new_layout : Layout )
201
- -> Result < ( ) , CannotReallocInPlace >
202
- {
203
- debug_assert ! ( new_layout. size( ) >= layout. size( ) ) ;
204
- debug_assert ! ( new_layout. align( ) == layout. align( ) ) ;
205
- let ret = __rust_grow_in_place ( ptr,
206
- layout. size ( ) ,
207
- layout. align ( ) ,
208
- new_layout. size ( ) ,
209
- new_layout. align ( ) ) ;
210
- if ret != 0 {
211
- Ok ( ( ) )
212
- } else {
213
- Err ( CannotReallocInPlace )
214
- }
215
- }
216
-
217
- #[ inline]
218
- unsafe fn shrink_in_place ( & mut self ,
219
- ptr : * mut u8 ,
220
- layout : Layout ,
221
- new_layout : Layout ) -> Result < ( ) , CannotReallocInPlace > {
222
- debug_assert ! ( new_layout. size( ) <= layout. size( ) ) ;
223
- debug_assert ! ( new_layout. align( ) == layout. align( ) ) ;
224
- let ret = __rust_shrink_in_place ( ptr,
225
- layout. size ( ) ,
226
- layout. align ( ) ,
227
- new_layout. size ( ) ,
228
- new_layout. align ( ) ) ;
229
- if ret != 0 {
230
- Ok ( ( ) )
231
- } else {
232
- Err ( CannotReallocInPlace )
233
125
}
234
126
}
235
127
}
0 commit comments