pub struct BumpAllocator { /* private fields */ }Expand description
A thread-safe, lock-free bump allocator backed by a static byte buffer.
Allocations are fulfilled by atomically advancing an internal offset;
no individual deallocation is possible. Call reset (or
reset_zeroed) to reclaim the entire buffer at once.
§Thread Safety
BumpAllocator is Sync + Send. Concurrent alloc calls use a
compare-and-swap loop to guarantee each thread receives a non-overlapping
slice of the backing buffer.
§Invariants
startalways points to the first byte of the backing buffer.0 <= offset <= sizeat all times.- Memory in
[start, start + offset)has been “handed out” and must not be written by the allocator itself untilresetis called.
Implementations§
Source§impl BumpAllocator
impl BumpAllocator
Sourcepub fn new(buf: &'static mut [u8]) -> Self
pub fn new(buf: &'static mut [u8]) -> Self
Sourcepub fn used(&self) -> usize
pub fn used(&self) -> usize
Returns the number of bytes that have been allocated from the buffer.
Examples found in repository?
examples/bump_stack.rs (line 26)
15fn main() {
16 let bump_mutex = get_bump();
17 let mut bump = bump_mutex.lock().unwrap();
18
19 {
20 let mut stack_vec = ExVec::new(&*bump);
21
22 stack_vec.push(10);
23 stack_vec.push(20);
24 stack_vec.push(30);
25
26 println!("Bump usage: {}/1024", bump.used());
27 }
28
29 bump.reset();
30
31 println!("Bump reset. Usage: {}", bump.used());
32}Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the allocator, making the entire buffer available again.
§Safety
All pointers previously returned by alloc or
alloc_slice become invalid after this call.
Any subsequent access to those pointers is undefined behaviour.
Examples found in repository?
examples/bump_stack.rs (line 29)
15fn main() {
16 let bump_mutex = get_bump();
17 let mut bump = bump_mutex.lock().unwrap();
18
19 {
20 let mut stack_vec = ExVec::new(&*bump);
21
22 stack_vec.push(10);
23 stack_vec.push(20);
24 stack_vec.push(30);
25
26 println!("Bump usage: {}/1024", bump.used());
27 }
28
29 bump.reset();
30
31 println!("Bump reset. Usage: {}", bump.used());
32}Sourcepub fn reset_zeroed(&mut self)
pub fn reset_zeroed(&mut self)
Trait Implementations§
Source§impl Allocator for BumpAllocator
impl Allocator for BumpAllocator
impl Send for BumpAllocator
impl Sync for BumpAllocator
Auto Trait Implementations§
impl !Freeze for BumpAllocator
impl RefUnwindSafe for BumpAllocator
impl Unpin for BumpAllocator
impl UnsafeUnpin for BumpAllocator
impl UnwindSafe for BumpAllocator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more