@@ -164,27 +164,27 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
164
164
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
165
165
unsafe impl AllocRef for Global {
166
166
#[ inline]
167
- fn alloc ( & mut self , layout : Layout ) -> Result < MemoryBlock , AllocErr > {
167
+ fn alloc ( & mut self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocErr > {
168
168
let size = layout. size ( ) ;
169
169
let ptr = if size == 0 {
170
170
layout. dangling ( )
171
171
} else {
172
172
// SAFETY: `layout` is non-zero in size,
173
173
unsafe { NonNull :: new ( alloc ( layout) ) . ok_or ( AllocErr ) ? }
174
174
} ;
175
- Ok ( MemoryBlock { ptr, size } )
175
+ Ok ( NonNull :: slice_from_raw_parts ( ptr, size) )
176
176
}
177
177
178
178
#[ inline]
179
- fn alloc_zeroed ( & mut self , layout : Layout ) -> Result < MemoryBlock , AllocErr > {
179
+ fn alloc_zeroed ( & mut self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocErr > {
180
180
let size = layout. size ( ) ;
181
181
let ptr = if size == 0 {
182
182
layout. dangling ( )
183
183
} else {
184
184
// SAFETY: `layout` is non-zero in size,
185
185
unsafe { NonNull :: new ( alloc_zeroed ( layout) ) . ok_or ( AllocErr ) ? }
186
186
} ;
187
- Ok ( MemoryBlock { ptr, size } )
187
+ Ok ( NonNull :: slice_from_raw_parts ( ptr, size) )
188
188
}
189
189
190
190
#[ inline]
@@ -202,7 +202,7 @@ unsafe impl AllocRef for Global {
202
202
ptr : NonNull < u8 > ,
203
203
layout : Layout ,
204
204
new_size : usize ,
205
- ) -> Result < MemoryBlock , AllocErr > {
205
+ ) -> Result < NonNull < [ u8 ] > , AllocErr > {
206
206
debug_assert ! (
207
207
new_size >= layout. size( ) ,
208
208
"`new_size` must be greater than or equal to `layout.size()`"
@@ -212,14 +212,16 @@ unsafe impl AllocRef for Global {
212
212
// Other conditions must be upheld by the caller
213
213
unsafe {
214
214
match layout. size ( ) {
215
- old_size if old_size == new_size => Ok ( MemoryBlock { ptr, size : new_size } ) ,
215
+ old_size if old_size == new_size => {
216
+ Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
217
+ }
216
218
0 => self . alloc ( Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ) ,
217
219
old_size => {
218
220
// `realloc` probably checks for `new_size > size` or something similar.
219
221
intrinsics:: assume ( new_size > old_size) ;
220
222
let raw_ptr = realloc ( ptr. as_ptr ( ) , layout, new_size) ;
221
223
let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
222
- Ok ( MemoryBlock { ptr, size : new_size } )
224
+ Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
223
225
}
224
226
}
225
227
}
@@ -231,7 +233,7 @@ unsafe impl AllocRef for Global {
231
233
ptr : NonNull < u8 > ,
232
234
layout : Layout ,
233
235
new_size : usize ,
234
- ) -> Result < MemoryBlock , AllocErr > {
236
+ ) -> Result < NonNull < [ u8 ] > , AllocErr > {
235
237
debug_assert ! (
236
238
new_size >= layout. size( ) ,
237
239
"`new_size` must be greater than or equal to `layout.size()`"
@@ -241,15 +243,17 @@ unsafe impl AllocRef for Global {
241
243
// Other conditions must be upheld by the caller
242
244
unsafe {
243
245
match layout. size ( ) {
244
- old_size if old_size == new_size => Ok ( MemoryBlock { ptr, size : new_size } ) ,
246
+ old_size if old_size == new_size => {
247
+ Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
248
+ }
245
249
0 => self . alloc_zeroed ( Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ) ,
246
250
old_size => {
247
251
// `realloc` probably checks for `new_size > size` or something similar.
248
252
intrinsics:: assume ( new_size > old_size) ;
249
253
let raw_ptr = realloc ( ptr. as_ptr ( ) , layout, new_size) ;
250
254
raw_ptr. add ( old_size) . write_bytes ( 0 , new_size - old_size) ;
251
255
let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
252
- Ok ( MemoryBlock { ptr, size : new_size } )
256
+ Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
253
257
}
254
258
}
255
259
}
@@ -261,7 +265,7 @@ unsafe impl AllocRef for Global {
261
265
ptr : NonNull < u8 > ,
262
266
layout : Layout ,
263
267
new_size : usize ,
264
- ) -> Result < MemoryBlock , AllocErr > {
268
+ ) -> Result < NonNull < [ u8 ] > , AllocErr > {
265
269
let old_size = layout. size ( ) ;
266
270
debug_assert ! (
267
271
new_size <= old_size,
@@ -288,7 +292,7 @@ unsafe impl AllocRef for Global {
288
292
NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?
289
293
} ;
290
294
291
- Ok ( MemoryBlock { ptr, size : new_size } )
295
+ Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
292
296
}
293
297
}
294
298
@@ -300,7 +304,7 @@ unsafe impl AllocRef for Global {
300
304
unsafe fn exchange_malloc ( size : usize , align : usize ) -> * mut u8 {
301
305
let layout = unsafe { Layout :: from_size_align_unchecked ( size, align) } ;
302
306
match Global . alloc ( layout) {
303
- Ok ( memory ) => memory . ptr . as_ptr ( ) ,
307
+ Ok ( ptr ) => ptr. as_non_null_ptr ( ) . as_ptr ( ) ,
304
308
Err ( _) => handle_alloc_error ( layout) ,
305
309
}
306
310
}
0 commit comments