Skip to main content

Heap

Struct Heap 

Source
pub struct Heap<'r> { /* private fields */ }
Expand description

A claimed heap, which is one thread’s handle for allocating from a Region.

A heap is the unit of single-threaded access. It is Send, so it can move between threads, but it is not Sync. Dropping it releases its slot and its slabs to the next claimant. Get one from Region::heap.

Blocks are not tied to the heap that allocated them. Any heap, or the region itself through Region::dealloc, can free them.

use bcmalloc::{Config, Mapping};
use core::alloc::Layout;

let mapping = Mapping::new(&Config::new().capacity(64 << 20).max_heaps(4))?;
let region = mapping.region();
let heap = region.heap().expect("a free heap");

let layout = Layout::new::<[u64; 8]>();
let block = heap.alloc(layout).expect("memory");
// SAFETY: the block came from this region with this layout and is not used again.
unsafe { heap.dealloc(block, layout) };

Implementations§

Source§

impl<'r> Heap<'r>

Source

pub const fn region(&self) -> &'r Region<'r>

The region this heap allocates from.

Source

pub fn alloc(&self, layout: Layout) -> Option<NonNull<u8>>

Allocates memory for layout, or returns None if the region has no room or the layout is not supported (alignment above MAX_ALIGN).

The memory is uninitialised. Zero-sized layouts are served like one-byte ones.

Source

pub fn alloc_zeroed(&self, layout: Layout) -> Option<NonNull<u8>>

As alloc, but the memory is zeroed.

Source

pub unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout)

Frees a block.

§Safety

ptr must have been returned by alloc, alloc_zeroed or realloc on this region with exactly layout, and must not be used or freed again.

Source

pub unsafe fn free_unsized(&self, ptr: NonNull<u8>)

Frees a block without its layout, and ignores a pointer outside the region.

§Safety

A pointer inside the region must be a live block of it, and must not be used or freed again.

Source

pub unsafe fn realloc( &self, ptr: NonNull<u8>, old: Layout, new_size: usize, ) -> Option<NonNull<u8>>

Resizes a block, returning None (and leaving the block untouched) if there is no room.

A block of a slab stays where it is while the new size fits in it and fills at least half of it. A block of whole chunks that shrinks gives back the chunks past its new end, and one that grows takes the chunks right after it if they are free. Otherwise the contents are copied to a new block.

§Safety

ptr must be a live block of this region allocated with exactly old, and new_size must be non-zero and not overflow when rounded up to old.align().

Source

pub fn flush(&self)

Hands back every block this heap holds outside its slabs, then every empty slab, so that other heaps (and large allocations) can use the memory. Region::trim does this for every idle heap.

Trait Implementations§

Source§

impl<'r> Debug for Heap<'r>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Heap<'_>

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 Send for Heap<'_>

Auto Trait Implementations§

§

impl<'r> !RefUnwindSafe for Heap<'r>

§

impl<'r> !Sync for Heap<'r>

§

impl<'r> !UnwindSafe for Heap<'r>

§

impl<'r> Forget for Heap<'r>

§

impl<'r> Freeze for Heap<'r>

§

impl<'r> Move for Heap<'r>

§

impl<'r> Unpin for Heap<'r>

§

impl<'r> UnsafeUnpin for Heap<'r>

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.