|
2 | 2 | #![allow(static_mut_refs)] |
3 | 3 |
|
4 | 4 | use crate::alloc::{GlobalAlloc, Layout, System}; |
| 5 | +use crate::ptr; |
| 6 | +use crate::sync::atomic::{AtomicBool, Ordering}; |
5 | 7 |
|
6 | | -static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::Dlmalloc::new(); |
| 8 | +// symbols defined in the target linkerscript |
| 9 | +unsafe extern "C" { |
| 10 | + static mut __heap_start: u8; |
| 11 | + static mut __heap_end: u8; |
| 12 | +} |
| 13 | + |
| 14 | +static mut DLMALLOC: dlmalloc::Dlmalloc<Vexos> = dlmalloc::Dlmalloc::new_with_allocator(Vexos); |
| 15 | + |
| 16 | +struct Vexos; |
| 17 | + |
| 18 | +unsafe impl dlmalloc::Allocator for Vexos { |
| 19 | + /// Allocs system resources |
| 20 | + fn alloc(&self, _size: usize) -> (*mut u8, usize, u32) { |
| 21 | + static INIT: AtomicBool = AtomicBool::new(false); |
| 22 | + |
| 23 | + if !INIT.swap(true, Ordering::Relaxed) { |
| 24 | + unsafe { |
| 25 | + ( |
| 26 | + (&raw mut __heap_start).cast(), |
| 27 | + (&raw const __heap_end).byte_offset_from(ptr::addr_of!(__heap_start)) as _, |
| 28 | + 0, |
| 29 | + ) |
| 30 | + } |
| 31 | + } else { |
| 32 | + (ptr::null_mut(), 0, 0) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + fn remap(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 { |
| 37 | + ptr::null_mut() |
| 38 | + } |
| 39 | + |
| 40 | + fn free_part(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize) -> bool { |
| 41 | + false |
| 42 | + } |
| 43 | + |
| 44 | + fn free(&self, _ptr: *mut u8, _size: usize) -> bool { |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + fn can_release_part(&self, _flags: u32) -> bool { |
| 49 | + false |
| 50 | + } |
| 51 | + |
| 52 | + fn allocates_zeros(&self) -> bool { |
| 53 | + false |
| 54 | + } |
| 55 | + |
| 56 | + fn page_size(&self) -> usize { |
| 57 | + 0x1000 |
| 58 | + } |
| 59 | +} |
7 | 60 |
|
8 | 61 | #[stable(feature = "alloc_system_type", since = "1.28.0")] |
9 | 62 | unsafe impl GlobalAlloc for System { |
|
0 commit comments