diff options
| author | Tim Diekmann <tim.diekmann@3dvision.de> | 2020-03-29 01:47:05 +0100 |
|---|---|---|
| committer | Tim Diekmann <tim.diekmann@3dvision.de> | 2020-03-29 01:47:05 +0100 |
| commit | 3ade8ae6608a9d371580e5e8d68c26a4e3e897fb (patch) | |
| tree | 49607964c87f5f8fe647160c42b178226e2b570a /src/libcore/alloc | |
| parent | bf6a46db3129b0bf31dc67f06af2e52ece52701a (diff) | |
| download | rust-3ade8ae6608a9d371580e5e8d68c26a4e3e897fb.tar.gz rust-3ade8ae6608a9d371580e5e8d68c26a4e3e897fb.zip | |
Implement `init` and `init_offset` on `AllocInit` and mark it unsafe
Diffstat (limited to 'src/libcore/alloc')
| -rw-r--r-- | src/libcore/alloc/mod.rs | 53 |
1 files changed, 31 insertions, 22 deletions
diff --git a/src/libcore/alloc/mod.rs b/src/libcore/alloc/mod.rs index f2f12a98fa6..cc8c730b63a 100644 --- a/src/libcore/alloc/mod.rs +++ b/src/libcore/alloc/mod.rs @@ -41,27 +41,22 @@ pub enum AllocInit { Zeroed, } -/// Represents a block of allocated memory returned by an allocator. -#[derive(Debug, Copy, Clone)] -#[unstable(feature = "allocator_api", issue = "32838")] -pub struct MemoryBlock { - pub ptr: NonNull<u8>, - pub size: usize, -} - -impl MemoryBlock { - /// Initialize the memory block like specified by `init`. +impl AllocInit { + /// Initialize the specified memory block. + /// + /// This behaves like calling [`AllocInit::initialize_offset(ptr, layout, 0)`][off]. + /// + /// [off]: AllocInit::init_offset /// - /// This behaves like calling [`MemoryBlock::initialize_offset(ptr, layout, 0)`][off]. + /// # Safety /// - /// [off]: MemoryBlock::init_offset + /// * `memory.ptr` must be [valid] for writes of `memory.size` bytes. /// - /// [*fit*]: trait.AllocRef.html#memory-fitting + /// [valid]: ../ptr/index.html#safety #[inline] #[unstable(feature = "allocator_api", issue = "32838")] - pub fn init(&mut self, init: AllocInit) { - // SAFETY: 0 is always smaller or equal to the size - unsafe { self.init_offset(init, 0) } + pub unsafe fn init(self, memory: MemoryBlock) { + self.init_offset(memory, 0) } /// Initialize the memory block like specified by `init` at the specified `offset`. @@ -71,20 +66,34 @@ impl MemoryBlock { /// /// # Safety /// - /// * `offset` must be smaller than or equal to `size()` + /// * `memory.ptr` must be [valid] for writes of `memory.size` bytes. + /// * `offset` must be smaller than or equal to `memory.size` /// - /// [*fit*]: trait.AllocRef.html#memory-fitting + /// [valid]: ../ptr/index.html#safety #[inline] #[unstable(feature = "allocator_api", issue = "32838")] - pub unsafe fn init_offset(&mut self, init: AllocInit, offset: usize) { - debug_assert!(offset <= self.size, "`offset` must be smaller than or equal to `size()`"); - match init { + pub unsafe fn init_offset(self, memory: MemoryBlock, offset: usize) { + debug_assert!( + offset <= memory.size, + "`offset` must be smaller than or equal to `memory.size`" + ); + match self { AllocInit::Uninitialized => (), - AllocInit::Zeroed => self.ptr.as_ptr().add(offset).write_bytes(0, self.size - offset), + AllocInit::Zeroed => { + memory.ptr.as_ptr().add(offset).write_bytes(0, memory.size - offset) + } } } } +/// Represents a block of allocated memory returned by an allocator. +#[derive(Debug, Copy, Clone)] +#[unstable(feature = "allocator_api", issue = "32838")] +pub struct MemoryBlock { + pub ptr: NonNull<u8>, + pub size: usize, +} + /// A placement constraint when growing or shrinking an existing allocation. #[derive(Debug, Copy, Clone, PartialEq, Eq)] #[unstable(feature = "allocator_api", issue = "32838")] |
