@@ -41,27 +41,22 @@ pub enum AllocInit {
41
41
Zeroed ,
42
42
}
43
43
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
54
50
///
55
- /// This behaves like calling [`MemoryBlock::initialize_offset(ptr, layout, 0)`][off].
51
+ /// # Safety
56
52
///
57
- /// [off]: MemoryBlock::init_offset
53
+ /// * `memory.ptr` must be [valid] for writes of `memory.size` bytes.
58
54
///
59
- /// [*fit* ]: trait.AllocRef. html#memory-fitting
55
+ /// [valid ]: ../ptr/index. html#safety
60
56
#[ inline]
61
57
#[ 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 )
65
60
}
66
61
67
62
/// Initialize the memory block like specified by `init` at the specified `offset`.
@@ -71,20 +66,34 @@ impl MemoryBlock {
71
66
///
72
67
/// # Safety
73
68
///
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`
75
71
///
76
- /// [*fit* ]: trait.AllocRef. html#memory-fitting
72
+ /// [valid ]: ../ptr/index. html#safety
77
73
#[ inline]
78
74
#[ 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 {
82
81
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
+ }
84
85
}
85
86
}
86
87
}
87
88
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
+
88
97
/// A placement constraint when growing or shrinking an existing allocation.
89
98
#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
90
99
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
0 commit comments