Skip to main content

MmapBacked

Struct MmapBacked 

Source
pub struct MmapBacked { /* private fields */ }
Expand description

OS-mapped anonymous region.

len is rounded up to a multiple of the page size at construction. The Allocator impl serves requests bump-style from the mapping.

§Thread safety

Send: yes — the mapping is identified by (ptr, len), both Send-safe values; we restore Send via an unsafe impl since NonNull<u8> is !Send by default. Sync: NO. The cursor uses UnsafeCell for &self allocation; concurrent &self allocators would race. UnsafeCell is !Sync, which gives us the right behavior without any extra marker field. Cross-thread allocation belongs to higher layers (SharedBumpArena, SlabRemote).

Implementations§

Source§

impl MmapBacked

Source

pub fn new(size: usize) -> Result<Self, AllocError>

Allocate an anonymous OS-mapped region of at least size bytes (rounded up to the page size).

Source

pub fn with_huge_pages(size: usize) -> Result<Self, AllocError>

Allocate with huge-pages requested. This layer ignores the hint; HugePageAligned enforces 2 MiB / 32 MiB alignment.

Source

pub fn new_lazy(size: usize) -> Result<Self, AllocError>

Reserve an anonymous OS-mapped region of at least size bytes without committing it up front (see MmapFlags::lazy_commit).

Windows-only effect. On Windows the region is MEM_RESERVE-only and consumes no commit charge until FixedRange::commit commits pages on demand; on Unix this is identical to new because mmap is already demand-paged.

§Safety / usage contract

The returned region hands back reserved-but-uncommitted pages on Windows. It is safe under BumpArena / StackAlloc (true demand-commit), under any pass-through FixedRange wrapper over those, and under Slab / SizeClassed / direct Allocator::allocate (safe, but committed eagerly). It faults under SharedBumpArena and GuardPage. See MmapFlags::lazy_commit for the full contract.

Source

pub fn with_flags(size: usize, flags: MmapFlags) -> Result<Self, AllocError>

Allocate with the supplied MmapFlags.

Source

pub fn allocated(&self) -> usize

Bytes already allocated from this backing.

Source

pub const fn capacity(&self) -> usize

Total size of the OS-mapped region (page-aligned).

Source

pub fn remaining(&self) -> usize

Bytes remaining for allocation.

Trait Implementations§

Source§

impl Allocator for MmapBacked

Source§

fn allocate(&self, layout: NonZeroLayout) -> Result<NonNull<[u8]>, AllocError>

Allocate a block satisfying layout. The returned slice’s length is at least layout.size() but may be larger.
Source§

fn capacity_bytes(&self) -> Option<usize>

Total bytes this allocator can issue, if bounded. None for unbounded allocators like System. Used by Watermark to compute thresholds.
Source§

fn allocate_zeroed( &self, layout: NonZeroLayout, ) -> Result<NonNull<[u8]>, AllocError>

Allocate a zero-initialized block.
Source§

unsafe fn grow( &self, ptr: NonNull<u8>, old: NonZeroLayout, new: NonZeroLayout, ) -> Result<NonNull<[u8]>, AllocError>

Grow an allocation in place if possible, otherwise allocate-copy-free. Read more
Source§

unsafe fn shrink( &self, ptr: NonNull<u8>, old: NonZeroLayout, new: NonZeroLayout, ) -> Result<NonNull<[u8]>, AllocError>

Shrink an allocation in place if possible, otherwise allocate-copy-free. Read more
Source§

fn reset(&mut self) -> Result<(), AllocError>

Reclaim everything previously allocated. Default impl returns AllocError — only arena-style allocators implement a meaningful reset. Read more
Source§

unsafe fn usable_size( &self, _ptr: NonNull<u8>, _layout: NonZeroLayout, ) -> Option<usize>

Usable size of an existing allocation, if the allocator can report it. Defaults to None — implementors that track usable size override. Read more
Source§

fn corruption_events(&self) -> u64

Detected freelist / metadata corruption events observed by this allocator since construction. Read more
Source§

impl Deallocator for MmapBacked

Source§

unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: NonZeroLayout)

Release a previously allocated block. Read more
Source§

impl Drop for MmapBacked

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl FixedRange for MmapBacked

Source§

fn base(&self) -> NonNull<u8>

First byte of the owned address range. Read more
Source§

fn size(&self) -> usize

Length in bytes of the owned address range. Read more
Source§

fn commit(&self, offset: usize, len: usize) -> Result<(), AllocError>

Ensure the bytes [offset, offset + len) (relative to base) are backed by committed, writable memory before a consumer writes through them. Read more
Source§

fn contains(&self, ptr: NonNull<u8>) -> bool

Whether ptr lies within [base, base + size). Read more
Source§

impl OsBacked for MmapBacked

Source§

unsafe fn release_pages(&self, ptr: NonNull<u8>, size: usize)

§Caveat (shared with commit)

On Windows this reads the committed high-water mark through &self under the !Sync single-writer contract. Do NOT call release_pages on a &MmapBacked that is shared while an allocator (or commit) advances the watermark — that races the UnsafeCell, exactly as commit’s own caveat warns.

Source§

fn base_ptr(&self) -> NonNull<u8>

First byte of the OS-managed region.
Source§

fn region_size(&self) -> usize

Length in bytes of the OS-managed region.
Source§

unsafe fn protect(&self, ptr: NonNull<u8>, size: usize, flags: ProtectFlags)

Change memory protection flags on [ptr, ptr + size). Read more
Source§

impl Send for MmapBacked

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.