@@ -41,27 +41,22 @@ pub enum AllocInit {
4141 Zeroed ,
4242}
4343
44- /// Represents a block of allocated memory returned by an allocator.
45- #[ derive( Debug , Copy , Clone ) ]
46- #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
47- pub struct MemoryBlock {
48- pub ptr : NonNull < u8 > ,
49- pub size : usize ,
50- }
51-
52- impl MemoryBlock {
53- /// Initialize the memory block like specified by `init`.
44+ impl AllocInit {
45+ /// Initialize the specified memory block.
46+ ///
47+ /// This behaves like calling [`AllocInit::initialize_offset(ptr, layout, 0)`][off].
48+ ///
49+ /// [off]: AllocInit::init_offset
5450 ///
55- /// This behaves like calling [`MemoryBlock::initialize_offset(ptr, layout, 0)`][off].
51+ /// # Safety
5652 ///
57- /// [off]: MemoryBlock::init_offset
53+ /// * `memory.ptr` must be [valid] for writes of `memory.size` bytes.
5854 ///
59- /// [*fit* ]: trait.AllocRef. html#memory-fitting
55+ /// [valid ]: ../ptr/index. html#safety
6056 #[ inline]
6157 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
62- pub fn init ( & mut self , init : AllocInit ) {
63- // SAFETY: 0 is always smaller or equal to the size
64- unsafe { self . init_offset ( init, 0 ) }
58+ pub unsafe fn init ( self , memory : MemoryBlock ) {
59+ self . init_offset ( memory, 0 )
6560 }
6661
6762 /// Initialize the memory block like specified by `init` at the specified `offset`.
@@ -71,20 +66,34 @@ impl MemoryBlock {
7166 ///
7267 /// # Safety
7368 ///
74- /// * `offset` must be smaller than or equal to `size()`
69+ /// * `memory.ptr` must be [valid] for writes of `memory.size` bytes.
70+ /// * `offset` must be smaller than or equal to `memory.size`
7571 ///
76- /// [*fit* ]: trait.AllocRef. html#memory-fitting
72+ /// [valid ]: ../ptr/index. html#safety
7773 #[ inline]
7874 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
79- pub unsafe fn init_offset ( & mut self , init : AllocInit , offset : usize ) {
80- debug_assert ! ( offset <= self . size, "`offset` must be smaller than or equal to `size()`" ) ;
81- match init {
75+ pub unsafe fn init_offset ( self , memory : MemoryBlock , offset : usize ) {
76+ debug_assert ! (
77+ offset <= memory. size,
78+ "`offset` must be smaller than or equal to `memory.size`"
79+ ) ;
80+ match self {
8281 AllocInit :: Uninitialized => ( ) ,
83- AllocInit :: Zeroed => self . ptr . as_ptr ( ) . add ( offset) . write_bytes ( 0 , self . size - offset) ,
82+ AllocInit :: Zeroed => {
83+ memory. ptr . as_ptr ( ) . add ( offset) . write_bytes ( 0 , memory. size - offset)
84+ }
8485 }
8586 }
8687}
8788
89+ /// Represents a block of allocated memory returned by an allocator.
90+ #[ derive( Debug , Copy , Clone ) ]
91+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
92+ pub struct MemoryBlock {
93+ pub ptr : NonNull < u8 > ,
94+ pub size : usize ,
95+ }
96+
8897/// A placement constraint when growing or shrinking an existing allocation.
8998#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
9099#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
0 commit comments