use crate::{allocator::BaseAllocator, math::align};
use core::sync::atomic::Ordering;
use iceoryx2_pal_concurrency_sync::iox_atomic::IoxAtomicUsize;
pub struct BumpAllocator {
start: usize,
pos: IoxAtomicUsize,
}
impl BumpAllocator {
pub fn new(start: usize) -> Self {
Self {
start,
pos: IoxAtomicUsize::new(start),
}
}
}
impl BaseAllocator for BumpAllocator {
fn allocate(
&self,
layout: core::alloc::Layout,
) -> Result<core::ptr::NonNull<[u8]>, crate::allocator::AllocationError> {
let mem = align(self.pos.load(Ordering::Relaxed), layout.align());
self.pos.store(mem + layout.size(), Ordering::Relaxed);
unsafe {
Ok(core::ptr::NonNull::new_unchecked(
core::slice::from_raw_parts_mut(mem as *mut u8, layout.size()),
))
}
}
unsafe fn deallocate(&self, _ptr: core::ptr::NonNull<u8>, _layout: core::alloc::Layout) {
self.pos.store(self.start, Ordering::Relaxed);
}
}