| 21 | // and remain valid until reset, which is called before any allocation. |
| 22 | unsafe impl GlobalAlloc for Allocator { |
| 23 | unsafe fn alloc(&self, layout: Layout) -> *mut u8 { |
| 24 | if layout.align() > align_of::<Heap>() { |
| 25 | return null_mut(); |
| 26 | } |
| 27 | |
| 28 | // SAFETY: Only the current single-threaded analysis accesses NEXT. |
| 29 | let start = unsafe { (NEXT + layout.align() - 1) & !(layout.align() - 1) }; |
| 30 | let Some(end) = start.checked_add(layout.size()) else { |
| 31 | return null_mut(); |
| 32 | }; |
| 33 | if end > HEAP_SIZE { |
| 34 | return null_mut(); |
| 35 | } |
| 36 | |
| 37 | // SAFETY: The allocation is aligned and within HEAP; advancing NEXT |
| 38 | // prevents overlap with earlier allocations. |
| 39 | unsafe { |
| 40 | NEXT = end; |
| 41 | addr_of_mut!(HEAP.0).cast::<u8>().add(start) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} |
| 46 | } |