Skip to content

Commit 3ade8ae

Browse files
committed
Implement init and init_offset on AllocInit and mark it unsafe
1 parent bf6a46d commit 3ade8ae

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

src/liballoc/alloc.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,12 @@ unsafe impl AllocRef for Global {
214214
self.alloc(new_layout, init)
215215
}
216216
ReallocPlacement::MayMove => {
217-
// `realloc` probably checks for `new_size > old_size` or something similar.
217+
// `realloc` probably checks for `new_size > size` or something similar.
218218
intrinsics::assume(new_size > size);
219219
let ptr = realloc(ptr.as_ptr(), layout, new_size);
220-
let mut memory =
220+
let memory =
221221
MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size };
222-
memory.init_offset(init, size);
222+
init.init_offset(memory, size);
223223
Ok(memory)
224224
}
225225
}
@@ -250,7 +250,7 @@ unsafe impl AllocRef for Global {
250250
Ok(MemoryBlock { ptr: layout.dangling(), size: 0 })
251251
}
252252
ReallocPlacement::MayMove => {
253-
// `realloc` probably checks for `new_size < old_size` or something similar.
253+
// `realloc` probably checks for `new_size < size` or something similar.
254254
intrinsics::assume(new_size < size);
255255
let ptr = realloc(ptr.as_ptr(), layout, new_size);
256256
Ok(MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size })

src/libcore/alloc/mod.rs

+31-22
Original file line numberDiff line numberDiff line change
@@ -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")]

src/libstd/alloc.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,12 @@ unsafe impl AllocRef for System {
188188
self.alloc(new_layout, init)
189189
}
190190
ReallocPlacement::MayMove => {
191-
// `realloc` probably checks for `new_size > old_size` or something similar.
191+
// `realloc` probably checks for `new_size > size` or something similar.
192192
intrinsics::assume(new_size > size);
193193
let ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size);
194-
let mut memory =
194+
let memory =
195195
MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size };
196-
memory.init_offset(init, size);
196+
init.init_offset(memory, size);
197197
Ok(memory)
198198
}
199199
}
@@ -224,7 +224,7 @@ unsafe impl AllocRef for System {
224224
Ok(MemoryBlock { ptr: layout.dangling(), size: 0 })
225225
}
226226
ReallocPlacement::MayMove => {
227-
// `realloc` probably checks for `new_size < old_size` or something similar.
227+
// `realloc` probably checks for `new_size < size` or something similar.
228228
intrinsics::assume(new_size < size);
229229
let ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size);
230230
Ok(MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size })

0 commit comments

Comments
 (0)