stm32f1_hal/common/
simplest_heap.rs1use crate::common::critical_section::Mutex;
2use core::{
3 alloc::{GlobalAlloc, Layout},
4 cell::UnsafeCell,
5 mem::MaybeUninit,
6 ptr,
7};
8
9pub struct Heap<const SIZE: usize> {
18 heap: Mutex<UnsafeCell<SimplestHeap<SIZE>>>,
19}
20
21impl<const SIZE: usize> Heap<SIZE> {
22 pub const fn new() -> Self {
24 Self {
25 heap: Mutex::new(UnsafeCell::new(SimplestHeap::new())),
26 }
27 }
28
29 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 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}