Skip to main content

iceoryx2_bb_testing/
allocator.rs

1// Copyright (c) 2026 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13extern 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}