@@ -42,35 +42,14 @@ pub enum AllocInit {
4242}
4343
4444/// Represents a block of allocated memory returned by an allocator.
45- #[ derive( Debug ) ]
45+ #[ derive( Debug , Copy , Clone ) ]
4646#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
4747pub struct MemoryBlock {
48- ptr : NonNull < u8 > ,
49- size : usize ,
48+ pub ptr : NonNull < u8 > ,
49+ pub size : usize ,
5050}
5151
5252impl MemoryBlock {
53- /// Creates a new `MemoryBlock` from the specified `ptr` and `size`.
54- #[ inline]
55- #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
56- pub const fn new ( ptr : NonNull < u8 > , size : usize ) -> Self {
57- Self { ptr, size }
58- }
59-
60- /// Acquires the underlying `NonNull<u8>` pointer.
61- #[ inline]
62- #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
63- pub const fn ptr ( & self ) -> NonNull < u8 > {
64- self . ptr
65- }
66-
67- /// Returns the size of the memory block.
68- #[ inline]
69- #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
70- pub const fn size ( & self ) -> usize {
71- self . size
72- }
73-
7453 /// Initialize the memory block like specified by `init`.
7554 ///
7655 /// This behaves like calling [`MemoryBlock::initialize_offset(ptr, layout, 0)`][off].
@@ -98,12 +77,10 @@ impl MemoryBlock {
9877 #[ inline]
9978 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
10079 pub unsafe fn init_offset ( & mut self , init : AllocInit , offset : usize ) {
101- debug_assert ! ( offset <= self . size( ) , "`offset` must be smaller than or equal to `size()`" ) ;
80+ debug_assert ! ( offset <= self . size, "`offset` must be smaller than or equal to `size()`" ) ;
10281 match init {
10382 AllocInit :: Uninitialized => ( ) ,
104- AllocInit :: Zeroed => {
105- self . ptr ( ) . as_ptr ( ) . add ( offset) . write_bytes ( 0 , self . size ( ) - offset)
106- }
83+ AllocInit :: Zeroed => self . ptr . as_ptr ( ) . add ( offset) . write_bytes ( 0 , self . size - offset) ,
10784 }
10885 }
10986}
@@ -246,9 +223,9 @@ pub unsafe trait AllocRef {
246223 ///
247224 /// * `ptr` must be [*currently allocated*] via this allocator,
248225 /// * `layout` must [*fit*] the `ptr`. (The `new_size` argument need not fit it.)
249- // We can't require that `new_size` is strictly greater than `memory.size() ` because of ZSTs.
226+ // We can't require that `new_size` is strictly greater than `memory.size` because of ZSTs.
250227 // An alternative would be
251- // * `new_size must be strictly greater than `memory.size() ` or both are zero
228+ // * `new_size must be strictly greater than `memory.size` or both are zero
252229 /// * `new_size` must be greater than or equal to `layout.size()`
253230 /// * `new_size`, when rounded up to the nearest multiple of `layout.align()`, must not overflow
254231 /// (i.e., the rounded value must be less than `usize::MAX`).
@@ -280,19 +257,19 @@ pub unsafe trait AllocRef {
280257 match placement {
281258 ReallocPlacement :: InPlace => Err ( AllocErr ) ,
282259 ReallocPlacement :: MayMove => {
283- let old_size = layout. size ( ) ;
260+ let size = layout. size ( ) ;
284261 debug_assert ! (
285- new_size >= old_size ,
262+ new_size >= size ,
286263 "`new_size` must be greater than or equal to `layout.size()`"
287264 ) ;
288265
289- if new_size == old_size {
290- return Ok ( MemoryBlock :: new ( ptr, old_size ) ) ;
266+ if new_size == size {
267+ return Ok ( MemoryBlock { ptr, size } ) ;
291268 }
292269
293270 let new_layout = Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ;
294271 let new_memory = self . alloc ( new_layout, init) ?;
295- ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_memory. ptr ( ) . as_ptr ( ) , old_size ) ;
272+ ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_memory. ptr . as_ptr ( ) , size ) ;
296273 self . dealloc ( ptr, layout) ;
297274 Ok ( new_memory)
298275 }
@@ -324,10 +301,10 @@ pub unsafe trait AllocRef {
324301 ///
325302 /// * `ptr` must be [*currently allocated*] via this allocator,
326303 /// * `layout` must [*fit*] the `ptr`. (The `new_size` argument need not fit it.)
327- // We can't require that `new_size` is strictly smaller than `memory.size() ` because of ZSTs.
304+ // We can't require that `new_size` is strictly smaller than `memory.size` because of ZSTs.
328305 // An alternative would be
329- // * `new_size must be strictly smaller than `memory.size() ` or both are zero
330- /// * `new_size` must be smaller than or equal to `memory .size()`
306+ // * `new_size must be strictly smaller than `memory.size` or both are zero
307+ /// * `new_size` must be smaller than or equal to `layout .size()`
331308 ///
332309 /// [*currently allocated*]: #currently-allocated-memory
333310 /// [*fit*]: #memory-fitting
@@ -355,19 +332,19 @@ pub unsafe trait AllocRef {
355332 match placement {
356333 ReallocPlacement :: InPlace => Err ( AllocErr ) ,
357334 ReallocPlacement :: MayMove => {
358- let old_size = layout. size ( ) ;
335+ let size = layout. size ( ) ;
359336 debug_assert ! (
360- new_size <= old_size ,
337+ new_size <= size ,
361338 "`new_size` must be smaller than or equal to `layout.size()`"
362339 ) ;
363340
364- if new_size == old_size {
365- return Ok ( MemoryBlock :: new ( ptr, old_size ) ) ;
341+ if new_size == size {
342+ return Ok ( MemoryBlock { ptr, size } ) ;
366343 }
367344
368345 let new_layout = Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ;
369346 let new_memory = self . alloc ( new_layout, AllocInit :: Uninitialized ) ?;
370- ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_memory. ptr ( ) . as_ptr ( ) , new_size) ;
347+ ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_memory. ptr . as_ptr ( ) , new_size) ;
371348 self . dealloc ( ptr, layout) ;
372349 Ok ( new_memory)
373350 }
0 commit comments