iceoryx2_bb_testing/
allocator.rs1extern crate alloc;
14
15use alloc::alloc::{alloc, dealloc};
16use core::alloc::Layout;
17use core::ptr::NonNull;
18
19use iceoryx2_bb_elementary_traits::allocator::{Allocate, AllocationError, Deallocate};
20
21pub struct Allocator {}
22
23impl Default for Allocator {
24 fn default() -> Self {
25 Self::new()
26 }
27}
28
29impl Allocator {
30 pub fn new() -> Self {
31 Self {}
32 }
33}
34
35impl Allocate<NonNull<u8>> for Allocator {
36 fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, AllocationError> {
37 let ptr = unsafe { alloc(layout) };
38 NonNull::new(ptr).ok_or(AllocationError::OutOfMemory)
39 }
40}
41
42impl Deallocate<NonNull<u8>> for Allocator {
43 unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
44 unsafe { dealloc(ptr.as_ptr(), layout) };
45 }
46}