stm32f1_hal/common/
simplest_heap.rs

1use crate::common::critical_section::Mutex;
2use core::{
3    alloc::{GlobalAlloc, Layout},
4    cell::UnsafeCell,
5    mem::MaybeUninit,
6    ptr,
7};
8
9/// The simplest possible heap.
10///
11/// # Safety
12///
13/// Because it's the simplest implementation, it does **NOT** free memory.
14/// Any memory you drop cannot be reused (it's leaked), so avoid dropping anything whenever possible.
15///
16/// It is recommended that you use [embedded-alloc](https://crates.io/crates/embedded-alloc)
17pub struct Heap<const SIZE: usize> {
18    heap: Mutex<UnsafeCell<SimplestHeap<SIZE>>>,
19}
20
21impl<const SIZE: usize> Heap<SIZE> {
22    /// Create a new heap allocator
23    pub const fn new() -> Self {
24        Self {
25            heap: Mutex::new(UnsafeCell::new(SimplestHeap::new())),
26        }
27    }
28
29    /// Returns an estimate of the amount of bytes in use.
30    pub fn used(&self) -> usize {
31        critical_section::with(|cs| unsafe { &*self.heap.borrow(cs).get() }.used())
32    }
33}
34
35unsafe impl<const SIZE: usize> GlobalAlloc for Heap<SIZE> {
36    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
37        critical_section::with(|cs| unsafe { &mut *self.heap.borrow(cs).get() }.alloc(layout))
38    }
39
40    unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
41}
42
43struct SimplestHeap<const SIZE: usize> {
44    arena: [MaybeUninit<u8>; SIZE],
45    remaining: usize,
46}
47
48unsafe impl<const SIZE: usize> Send for SimplestHeap<SIZE> {}
49
50impl<const SIZE: usize> SimplestHeap<SIZE> {
51    const fn new() -> Self {
52        Self {
53            arena: [MaybeUninit::uninit(); SIZE],
54            remaining: SIZE,
55        }
56    }
57
58    fn used(&self) -> usize {
59        SIZE - self.remaining
60    }
61
62    fn alloc(&mut self, layout: Layout) -> *mut u8 {
63        if layout.size() > self.remaining {
64            return ptr::null_mut();
65        }
66
67        // `Layout` contract forbids making a `Layout` with align=0, or align not power of 2.
68        // So we can safely use a mask to ensure alignment without worrying about UB.
69        let align_mask_to_round_down = !(layout.align() - 1);
70
71        self.remaining -= layout.size();
72        self.remaining &= align_mask_to_round_down;
73        (self.arena.as_mut_ptr() as *mut u8).wrapping_add(self.remaining)
74    }
75}