@@ -164,16 +164,26 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
164164#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
165165unsafe impl AllocRef for Global {
166166 #[ inline]
167- fn alloc ( & mut self , layout : Layout , init : AllocInit ) -> Result < MemoryBlock , AllocErr > {
167+ fn alloc ( & mut self , layout : Layout ) -> Result < MemoryBlock , AllocErr > {
168168 unsafe {
169169 let size = layout. size ( ) ;
170170 if size == 0 {
171171 Ok ( MemoryBlock { ptr : layout. dangling ( ) , size : 0 } )
172172 } 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) ;
177187 let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
178188 Ok ( MemoryBlock { ptr, size } )
179189 }
@@ -193,8 +203,6 @@ unsafe impl AllocRef for Global {
193203 ptr : NonNull < u8 > ,
194204 layout : Layout ,
195205 new_size : usize ,
196- placement : ReallocPlacement ,
197- init : AllocInit ,
198206 ) -> Result < MemoryBlock , AllocErr > {
199207 let size = layout. size ( ) ;
200208 debug_assert ! (
@@ -206,26 +214,49 @@ unsafe impl AllocRef for Global {
206214 return Ok ( MemoryBlock { ptr, size } ) ;
207215 }
208216
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) ;
228258 }
259+ Ok ( memory)
229260 }
230261 }
231262
@@ -235,7 +266,6 @@ unsafe impl AllocRef for Global {
235266 ptr : NonNull < u8 > ,
236267 layout : Layout ,
237268 new_size : usize ,
238- placement : ReallocPlacement ,
239269 ) -> Result < MemoryBlock , AllocErr > {
240270 let size = layout. size ( ) ;
241271 debug_assert ! (
@@ -247,22 +277,18 @@ unsafe impl AllocRef for Global {
247277 return Ok ( MemoryBlock { ptr, size } ) ;
248278 }
249279
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) ;
265283 }
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 } )
266292 }
267293 }
268294}
@@ -274,7 +300,7 @@ unsafe impl AllocRef for Global {
274300#[ inline]
275301unsafe fn exchange_malloc ( size : usize , align : usize ) -> * mut u8 {
276302 let layout = unsafe { Layout :: from_size_align_unchecked ( size, align) } ;
277- match Global . alloc ( layout, AllocInit :: Uninitialized ) {
303+ match Global . alloc ( layout) {
278304 Ok ( memory) => memory. ptr . as_ptr ( ) ,
279305 Err ( _) => handle_alloc_error ( layout) ,
280306 }
0 commit comments