Skip to main content

iceoryx2_pal_testing/
memory.rs

1// Copyright (c) 2024 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
13use alloc::alloc::{alloc, dealloc};
14use core::alloc::Layout;
15
16/// Allocates uninitialized memory on the heap. When it goes out of scope the memory is released.
17/// The user has to ensure that the memory is initialized.
18pub struct RawMemory<T> {
19    memory: *mut T,
20}
21
22impl<T> Drop for RawMemory<T> {
23    fn drop(&mut self) {
24        unsafe { dealloc(self.memory.cast(), Layout::new::<T>()) }
25    }
26}
27
28impl<T> RawMemory<T> {
29    /// Allocates memory to fit in a T and fills the memory with values provided by the fill
30    /// callback.
31    pub fn new<F: FnMut(usize) -> u8>(mut fill: F) -> Self {
32        let layout = Layout::new::<T>();
33        let memory = unsafe { alloc(layout) };
34
35        for i in 0..layout.size() {
36            unsafe { memory.add(i).write(fill(i)) }
37        }
38
39        Self {
40            memory: memory.cast(),
41        }
42    }
43
44    /// Allocates zeroed memory to fit in a T.
45    pub fn new_zeroed() -> Self {
46        Self::new(|_| 0u8)
47    }
48
49    /// Allocates memory filled with a given value to fit in a T.
50    pub fn new_filled(value: u8) -> Self {
51        Self::new(|_| value)
52    }
53
54    /// Returns a const pointer to T
55    pub fn as_ptr(&self) -> *const T {
56        self.memory
57    }
58
59    /// Returns a mutable pointer to T
60    pub fn as_mut_ptr(&self) -> *mut T {
61        self.memory
62    }
63
64    /// Returns a const reference to T
65    ///
66    /// # Safety
67    ///
68    /// * T must have been constructed manually before calling this function.
69    pub unsafe fn assume_init(&self) -> &T {
70        unsafe { &*self.memory }
71    }
72
73    /// Returns a mutable reference to T
74    ///
75    /// # Safety
76    ///
77    /// * T must have been constructed manually before calling this function.
78    pub unsafe fn assume_init_mut(&mut self) -> &mut T {
79        unsafe { &mut *self.memory }
80    }
81
82    /// Returns a slice to the underlying memory
83    pub fn as_slice(&self) -> &[u8] {
84        unsafe { core::slice::from_raw_parts(self.memory.cast(), core::mem::size_of::<T>()) }
85    }
86
87    /// Returns a mutable slice to the underlying memory
88    pub fn as_slice_mut(&mut self) -> &mut [u8] {
89        unsafe { core::slice::from_raw_parts_mut(self.memory.cast(), core::mem::size_of::<T>()) }
90    }
91}