message: Put heap on stack
diff: @@ -34,57 +34,55 @@ use alloc::allocator::AllocErr;
  use alloc::allocator::Layout;
  use alloc::string::String;
  use console::Console;
  -#[cfg(target_os = "tock")]
  -use core::mem::{align_of, size_of};
  +use core::mem;
  use core::ptr;
  #[cfg(target_os = "tock")]
  -use linked_list_allocator::{align_up, Heap};
  +use linked_list_allocator::Heap;
  // None-threaded heap wrapper based on `r9` register instead of global variable
  #[cfg(target_os = "tock")]
  -struct BaseHeap;
  +struct StackOwnedHeap;
  #[cfg(target_os = "tock")]
  -impl BaseHeap {
  - pub unsafe fn heap(&self) -> &mut Heap {
  - let heap: *mut Heap;
  - asm!("mov $0, r9" : "=r"(heap) : : : "volatile");
  - &mut *heap
  +impl StackOwnedHeap {
  + unsafe fn heap(&self) -> &mut Heap {
  + let heap_location: *mut Heap;
  + asm!("mov $0, r9" : "=r"(heap_location) : : : "volatile");
  + &mut *heap_location
  }
  /// Initializes an empty heap
  ///
  - /// Returns the end of the heap
  - ///
  /// # Unsafety
  ///
  - /// This function must be called at most once and must only be used on an
  - /// empty heap.
  + /// This function must be called at most once. The heap_buffer must remain valid until the end of the process.
  #[inline(never)]
  - pub unsafe fn init(&mut self, heap_size: usize) -> usize {
  - let heap_bottom = align_up(
  - self as *mut _ as usize + size_of::<Heap>(),
  - align_of::<Heap>(),
  - );
  - self.heap().init(heap_bottom, heap_size);
  - heap_bottom + heap_size
  + unsafe fn init(&mut self, heap_buffer: &[u8]) {
  + let heap_location = heap_buffer.as_ptr() as usize;
  + asm!("mov r9, $0" : : "r"(heap_location) : : "volatile");
  +
  + let heap_bottom = heap_location + mem::size_of::<Heap>();
  + let heap_top = heap_location + heap_buffer.len();
  + *self.heap() = Heap::new(heap_bottom, heap_top - heap_bottom);
  }
  }
  #[cfg(target_os = "tock")]
  -unsafe impl<'a> Alloc for &'a BaseHeap {
  +unsafe impl<'a> Alloc for &'a StackOwnedHeap {
  unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
  - self.heap().allocate_first_fit(layout)
  + self.heap().alloc(layout)
  }
  unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
  - self.heap().deallocate(ptr, layout)
  + self.heap().dealloc(ptr, layout)
  }
  }
  #[cfg(target_os = "tock")]
  #[global_allocator]
  -static ALLOCATOR: BaseHeap = BaseHeap;
  +static ALLOCATOR: StackOwnedHeap = StackOwnedHeap;
  +
  +const STACK_SIZE: usize = 0x200;
  /// Tock programs' entry point
  #[cfg(target_os = "tock")]
  @@ -103,10 +101,8 @@ pub extern "C" fn _start(
  }
  unsafe {
  - // Setup heap
  - asm!("mov r9, $0" : : "r"(mem_start) : : "volatile"); // Removing this line will result in a crash that requires reflashing the ROM...
  -
  - BaseHeap.init(1024);
  + let heap = [0; STACK_SIZE];
  + StackOwnedHeap.init(&heap);
  let mut console = Console::new();
  console.write(String::from("\nProcess started\n==============="));
  