@@ -164,16 +164,26 @@ 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 , init : AllocInit ) -> Result < MemoryBlock , AllocErr > {
167
+ fn alloc ( & mut self , layout : Layout ) -> Result < MemoryBlock , AllocErr > {
168
168
unsafe {
169
169
let size = layout. size ( ) ;
170
170
if size == 0 {
171
171
Ok ( MemoryBlock { ptr : layout. dangling ( ) , size : 0 } )
172
172
} else {
173
- let raw_ptr = match init {
174
- AllocInit :: Uninitialized => alloc ( layout) ,
175
- AllocInit :: Zeroed => alloc_zeroed ( layout) ,
176
- } ;
173
+ let raw_ptr = alloc ( layout) ;
174
+ let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
175
+ Ok ( MemoryBlock { ptr, size } )
176
+ }
177
+ }
178
+ }
179
+
180
+ fn alloc_zeroed ( & mut self , layout : Layout ) -> Result < MemoryBlock , AllocErr > {
181
+ unsafe {
182
+ let size = layout. size ( ) ;
183
+ if size == 0 {
184
+ Ok ( MemoryBlock { ptr : layout. dangling ( ) , size : 0 } )
185
+ } else {
186
+ let raw_ptr = alloc_zeroed ( layout) ;
177
187
let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
178
188
Ok ( MemoryBlock { ptr, size } )
179
189
}
@@ -193,8 +203,6 @@ unsafe impl AllocRef for Global {
193
203
ptr : NonNull < u8 > ,
194
204
layout : Layout ,
195
205
new_size : usize ,
196
- placement : ReallocPlacement ,
197
- init : AllocInit ,
198
206
) -> Result < MemoryBlock , AllocErr > {
199
207
let size = layout. size ( ) ;
200
208
debug_assert ! (
@@ -206,26 +214,49 @@ unsafe impl AllocRef for Global {
206
214
return Ok ( MemoryBlock { ptr, size } ) ;
207
215
}
208
216
209
- match placement {
210
- ReallocPlacement :: InPlace => Err ( AllocErr ) ,
211
- ReallocPlacement :: MayMove if layout. size ( ) == 0 => {
212
- let new_layout =
213
- unsafe { Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) } ;
214
- self . alloc ( new_layout, init)
215
- }
216
- ReallocPlacement :: MayMove => {
217
- // `realloc` probably checks for `new_size > size` or something similar.
218
- let ptr = unsafe {
219
- intrinsics:: assume ( new_size > size) ;
220
- realloc ( ptr. as_ptr ( ) , layout, new_size)
221
- } ;
222
- let memory =
223
- MemoryBlock { ptr : NonNull :: new ( ptr) . ok_or ( AllocErr ) ?, size : new_size } ;
224
- unsafe {
225
- init. init_offset ( memory, size) ;
226
- }
227
- Ok ( memory)
217
+ if layout. size ( ) == 0 {
218
+ let new_layout = unsafe { Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) } ;
219
+ self . alloc ( new_layout)
220
+ } else {
221
+ // `realloc` probably checks for `new_size > size` or something similar.
222
+ let ptr = unsafe {
223
+ intrinsics:: assume ( new_size > size) ;
224
+ realloc ( ptr. as_ptr ( ) , layout, new_size)
225
+ } ;
226
+ Ok ( MemoryBlock { ptr : NonNull :: new ( ptr) . ok_or ( AllocErr ) ?, size : new_size } )
227
+ }
228
+ }
229
+
230
+ unsafe fn grow_zeroed (
231
+ & mut self ,
232
+ ptr : NonNull < u8 > ,
233
+ layout : Layout ,
234
+ new_size : usize ,
235
+ ) -> Result < MemoryBlock , AllocErr > {
236
+ let size = layout. size ( ) ;
237
+ debug_assert ! (
238
+ new_size >= size,
239
+ "`new_size` must be greater than or equal to `memory.size()`"
240
+ ) ;
241
+
242
+ if size == new_size {
243
+ return Ok ( MemoryBlock { ptr, size } ) ;
244
+ }
245
+
246
+ if layout. size ( ) == 0 {
247
+ let new_layout = unsafe { Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) } ;
248
+ self . alloc ( new_layout)
249
+ } else {
250
+ // `realloc` probably checks for `new_size > size` or something similar.
251
+ let ptr = unsafe {
252
+ intrinsics:: assume ( new_size > size) ;
253
+ realloc ( ptr. as_ptr ( ) , layout, new_size)
254
+ } ;
255
+ let memory = MemoryBlock { ptr : NonNull :: new ( ptr) . ok_or ( AllocErr ) ?, size : new_size } ;
256
+ unsafe {
257
+ memory. ptr . as_ptr ( ) . add ( size) . write_bytes ( 0 , memory. size - size) ;
228
258
}
259
+ Ok ( memory)
229
260
}
230
261
}
231
262
@@ -235,7 +266,6 @@ unsafe impl AllocRef for Global {
235
266
ptr : NonNull < u8 > ,
236
267
layout : Layout ,
237
268
new_size : usize ,
238
- placement : ReallocPlacement ,
239
269
) -> Result < MemoryBlock , AllocErr > {
240
270
let size = layout. size ( ) ;
241
271
debug_assert ! (
@@ -247,22 +277,18 @@ unsafe impl AllocRef for Global {
247
277
return Ok ( MemoryBlock { ptr, size } ) ;
248
278
}
249
279
250
- match placement {
251
- ReallocPlacement :: InPlace => Err ( AllocErr ) ,
252
- ReallocPlacement :: MayMove if new_size == 0 => {
253
- unsafe {
254
- self . dealloc ( ptr, layout) ;
255
- }
256
- Ok ( MemoryBlock { ptr : layout. dangling ( ) , size : 0 } )
257
- }
258
- ReallocPlacement :: MayMove => {
259
- // `realloc` probably checks for `new_size < size` or something similar.
260
- let ptr = unsafe {
261
- intrinsics:: assume ( new_size < size) ;
262
- realloc ( ptr. as_ptr ( ) , layout, new_size)
263
- } ;
264
- Ok ( MemoryBlock { ptr : NonNull :: new ( ptr) . ok_or ( AllocErr ) ?, size : new_size } )
280
+ if new_size == 0 {
281
+ unsafe {
282
+ self . dealloc ( ptr, layout) ;
265
283
}
284
+ Ok ( MemoryBlock { ptr : layout. dangling ( ) , size : 0 } )
285
+ } else {
286
+ // `realloc` probably checks for `new_size < size` or something similar.
287
+ let ptr = unsafe {
288
+ intrinsics:: assume ( new_size < size) ;
289
+ realloc ( ptr. as_ptr ( ) , layout, new_size)
290
+ } ;
291
+ Ok ( MemoryBlock { ptr : NonNull :: new ( ptr) . ok_or ( AllocErr ) ?, size : new_size } )
266
292
}
267
293
}
268
294
}
@@ -274,7 +300,7 @@ unsafe impl AllocRef for Global {
274
300
#[ inline]
275
301
unsafe fn exchange_malloc ( size : usize , align : usize ) -> * mut u8 {
276
302
let layout = unsafe { Layout :: from_size_align_unchecked ( size, align) } ;
277
- match Global . alloc ( layout, AllocInit :: Uninitialized ) {
303
+ match Global . alloc ( layout) {
278
304
Ok ( memory) => memory. ptr . as_ptr ( ) ,
279
305
Err ( _) => handle_alloc_error ( layout) ,
280
306
}
0 commit comments