@@ -73,62 +73,42 @@ pub type Heap = Global;
73
73
#[ allow( non_upper_case_globals) ]
74
74
pub const Heap : Global = Global ;
75
75
76
- unsafe impl Alloc for Global {
76
+ unsafe impl GlobalAlloc for Global {
77
77
#[ inline]
78
- unsafe fn alloc ( & mut self , layout : Layout ) -> Result < * mut u8 , AllocErr > {
78
+ unsafe fn alloc ( & self , layout : Layout ) -> * mut Void {
79
79
#[ cfg( not( stage0) ) ]
80
80
let ptr = __rust_alloc ( layout. size ( ) , layout. align ( ) ) ;
81
81
#[ cfg( stage0) ]
82
82
let ptr = __rust_alloc ( layout. size ( ) , layout. align ( ) , & mut 0 ) ;
83
-
84
- if !ptr. is_null ( ) {
85
- Ok ( ptr)
86
- } else {
87
- Err ( AllocErr )
88
- }
83
+ ptr as * mut Void
89
84
}
90
85
91
86
#[ inline]
92
- unsafe fn dealloc ( & mut self , ptr : * mut u8 , layout : Layout ) {
93
- __rust_dealloc ( ptr, layout. size ( ) , layout. align ( ) )
87
+ unsafe fn dealloc ( & self , ptr : * mut Void , layout : Layout ) {
88
+ __rust_dealloc ( ptr as * mut u8 , layout. size ( ) , layout. align ( ) )
94
89
}
95
90
96
91
#[ inline]
97
- unsafe fn realloc ( & mut self ,
98
- ptr : * mut u8 ,
99
- layout : Layout ,
100
- new_size : usize )
101
- -> Result < * mut u8 , AllocErr >
102
- {
92
+ unsafe fn realloc ( & self , ptr : * mut Void , layout : Layout , new_size : usize ) -> * mut Void {
103
93
#[ cfg( not( stage0) ) ]
104
- let ptr = __rust_realloc ( ptr, layout. size ( ) , layout. align ( ) , new_size) ;
94
+ let ptr = __rust_realloc ( ptr as * mut u8 , layout. size ( ) , layout. align ( ) , new_size) ;
105
95
#[ cfg( stage0) ]
106
- let ptr = __rust_realloc ( ptr, layout. size ( ) , layout. align ( ) ,
96
+ let ptr = __rust_realloc ( ptr as * mut u8 , layout. size ( ) , layout. align ( ) ,
107
97
new_size, layout. align ( ) , & mut 0 ) ;
108
-
109
- if !ptr. is_null ( ) {
110
- Ok ( ptr)
111
- } else {
112
- Err ( AllocErr )
113
- }
98
+ ptr as * mut Void
114
99
}
115
100
116
101
#[ inline]
117
- unsafe fn alloc_zeroed ( & mut self , layout : Layout ) -> Result < * mut u8 , AllocErr > {
102
+ unsafe fn alloc_zeroed ( & self , layout : Layout ) -> * mut Void {
118
103
#[ cfg( not( stage0) ) ]
119
104
let ptr = __rust_alloc_zeroed ( layout. size ( ) , layout. align ( ) ) ;
120
105
#[ cfg( stage0) ]
121
106
let ptr = __rust_alloc_zeroed ( layout. size ( ) , layout. align ( ) , & mut 0 ) ;
122
-
123
- if !ptr. is_null ( ) {
124
- Ok ( ptr)
125
- } else {
126
- Err ( AllocErr )
127
- }
107
+ ptr as * mut Void
128
108
}
129
109
130
110
#[ inline]
131
- fn oom ( & mut self ) -> ! {
111
+ fn oom ( & self ) -> ! {
132
112
unsafe {
133
113
#[ cfg( not( stage0) ) ]
134
114
__rust_oom ( ) ;
@@ -138,6 +118,38 @@ unsafe impl Alloc for Global {
138
118
}
139
119
}
140
120
121
+ unsafe impl Alloc for Global {
122
+ #[ inline]
123
+ unsafe fn alloc ( & mut self , layout : Layout ) -> Result < * mut u8 , AllocErr > {
124
+ GlobalAlloc :: alloc ( self , layout) . into ( )
125
+ }
126
+
127
+ #[ inline]
128
+ unsafe fn dealloc ( & mut self , ptr : * mut u8 , layout : Layout ) {
129
+ GlobalAlloc :: dealloc ( self , ptr as * mut Void , layout)
130
+ }
131
+
132
+ #[ inline]
133
+ unsafe fn realloc ( & mut self ,
134
+ ptr : * mut u8 ,
135
+ layout : Layout ,
136
+ new_size : usize )
137
+ -> Result < * mut u8 , AllocErr >
138
+ {
139
+ GlobalAlloc :: realloc ( self , ptr as * mut Void , layout, new_size) . into ( )
140
+ }
141
+
142
+ #[ inline]
143
+ unsafe fn alloc_zeroed ( & mut self , layout : Layout ) -> Result < * mut u8 , AllocErr > {
144
+ GlobalAlloc :: alloc_zeroed ( self , layout) . into ( )
145
+ }
146
+
147
+ #[ inline]
148
+ fn oom ( & mut self ) -> ! {
149
+ GlobalAlloc :: oom ( self )
150
+ }
151
+ }
152
+
141
153
/// The allocator for unique pointers.
142
154
// This function must not unwind. If it does, MIR trans will fail.
143
155
#[ cfg( not( test) ) ]
@@ -148,9 +160,12 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
148
160
align as * mut u8
149
161
} else {
150
162
let layout = Layout :: from_size_align_unchecked ( size, align) ;
151
- Global . alloc ( layout) . unwrap_or_else ( |_| {
163
+ let ptr = Global . alloc ( layout) ;
164
+ if !ptr. is_null ( ) {
165
+ ptr as * mut u8
166
+ } else {
152
167
Global . oom ( )
153
- } )
168
+ }
154
169
}
155
170
}
156
171
@@ -162,7 +177,7 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
162
177
// We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
163
178
if size != 0 {
164
179
let layout = Layout :: from_size_align_unchecked ( size, align) ;
165
- Global . dealloc ( ptr as * mut u8 , layout) ;
180
+ Global . dealloc ( ptr as * mut Void , layout) ;
166
181
}
167
182
}
168
183
0 commit comments