|
| 1 | +use core::alloc::{GlobalAlloc, Layout}; |
| 2 | +use core::cell::RefCell; |
| 3 | +use core::ptr; |
| 4 | +use critical_section::Mutex; |
| 5 | + |
| 6 | +/// The simplest possible heap. |
| 7 | +/// |
| 8 | +/// # Safety |
| 9 | +/// |
| 10 | +/// This heap does **NOT** free allocated memory. |
| 11 | +pub struct Heap { |
| 12 | + heap: Mutex<RefCell<SimplestHeap>>, |
| 13 | +} |
| 14 | + |
| 15 | +impl Heap { |
| 16 | + /// Create a new UNINITIALIZED heap allocator |
| 17 | + /// |
| 18 | + /// You must initialize this heap using the |
| 19 | + /// [`init`](Self::init) method before using the allocator. |
| 20 | + pub const fn empty() -> Heap { |
| 21 | + Heap { |
| 22 | + heap: Mutex::new(RefCell::new(SimplestHeap::empty())), |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /// Initializes the heap |
| 27 | + /// |
| 28 | + /// This function must be called BEFORE you run any code that makes use of the |
| 29 | + /// allocator. |
| 30 | + /// |
| 31 | + /// `start_addr` is the address where the heap will be located. |
| 32 | + /// |
| 33 | + /// `size` is the size of the heap in bytes. |
| 34 | + /// |
| 35 | + /// # Safety |
| 36 | + /// |
| 37 | + /// Obey these or Bad Stuff will happen. |
| 38 | + /// |
| 39 | + /// - This function must be called exactly ONCE. |
| 40 | + /// - `size > 0` |
| 41 | + pub unsafe fn init(&self, start_addr: usize, size: usize) { |
| 42 | + critical_section::with(|cs| { |
| 43 | + self.heap |
| 44 | + .borrow(cs) |
| 45 | + .borrow_mut() |
| 46 | + .init(start_addr as *mut u8, size); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + /// Returns an estimate of the amount of bytes in use. |
| 51 | + pub fn used(&self) -> usize { |
| 52 | + critical_section::with(|cs| self.heap.borrow(cs).borrow().used()) |
| 53 | + } |
| 54 | + |
| 55 | + /// Returns an estimate of the amount of bytes available. |
| 56 | + pub fn free(&self) -> usize { |
| 57 | + critical_section::with(|cs| self.heap.borrow(cs).borrow().free()) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +unsafe impl GlobalAlloc for Heap { |
| 62 | + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { |
| 63 | + critical_section::with(|cs| self.heap.borrow(cs).borrow_mut().alloc(layout)) |
| 64 | + } |
| 65 | + |
| 66 | + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} |
| 67 | +} |
| 68 | + |
| 69 | +#[cfg(feature = "allocator_api")] |
| 70 | +mod allocator_api { |
| 71 | + use super::*; |
| 72 | + use core::alloc::{AllocError, Allocator}; |
| 73 | + use core::ptr::NonNull; |
| 74 | + |
| 75 | + unsafe impl Allocator for Heap { |
| 76 | + fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { |
| 77 | + match layout.size() { |
| 78 | + 0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)), |
| 79 | + size => critical_section::with(|cs| { |
| 80 | + let rst = self.heap.borrow(cs).borrow_mut().alloc(layout); |
| 81 | + if rst == ptr::null_mut() { |
| 82 | + Err(AllocError) |
| 83 | + } else { |
| 84 | + Ok(NonNull::new(rst)) |
| 85 | + } |
| 86 | + }), |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {} |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +struct SimplestHeap { |
| 95 | + arena: *mut u8, |
| 96 | + remaining: usize, |
| 97 | + size: usize, |
| 98 | +} |
| 99 | + |
| 100 | +unsafe impl Send for SimplestHeap {} |
| 101 | + |
| 102 | +impl SimplestHeap { |
| 103 | + const fn empty() -> Self { |
| 104 | + Self { |
| 105 | + arena: ptr::null_mut(), |
| 106 | + remaining: 0, |
| 107 | + size: 0, |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + fn init(&mut self, start_addr: *mut u8, size: usize) -> Self { |
| 112 | + Self { |
| 113 | + arena: start_addr, |
| 114 | + remaining: size, |
| 115 | + size, |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + fn free(&self) -> usize { |
| 120 | + self.remaining |
| 121 | + } |
| 122 | + |
| 123 | + fn used(&self) -> usize { |
| 124 | + self.size - self.remaining |
| 125 | + } |
| 126 | + |
| 127 | + fn alloc(&mut self, layout: Layout) -> *mut u8 { |
| 128 | + if layout.size() > self.remaining { |
| 129 | + return ptr::null_mut(); |
| 130 | + } |
| 131 | + |
| 132 | + // `Layout` contract forbids making a `Layout` with align=0, or align not power of 2. |
| 133 | + // So we can safely use a mask to ensure alignment without worrying about UB. |
| 134 | + let align_mask_to_round_down = !(layout.align() - 1); |
| 135 | + |
| 136 | + self.remaining -= layout.size(); |
| 137 | + self.remaining &= align_mask_to_round_down; |
| 138 | + self.arena.wrapping_add(self.remaining) |
| 139 | + } |
| 140 | +} |
0 commit comments