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>
impl<'r> Heap<'r>
Sourcepub fn alloc(&self, layout: Layout) -> Option<NonNull<u8>>
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.
Sourcepub fn alloc_zeroed(&self, layout: Layout) -> Option<NonNull<u8>>
pub fn alloc_zeroed(&self, layout: Layout) -> Option<NonNull<u8>>
As alloc, but the memory is zeroed.
Sourcepub unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout)
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.
Sourcepub unsafe fn free_unsized(&self, ptr: NonNull<u8>)
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.
Sourcepub unsafe fn realloc(
&self,
ptr: NonNull<u8>,
old: Layout,
new_size: usize,
) -> Option<NonNull<u8>>
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().
Sourcepub fn flush(&self)
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.