1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use core::alloc::{GlobalAlloc, Layout};
use spin::{Mutex, MutexGuard};
use core::ptr::NonNull;
use linked_list_allocator::{Heap, align_up};
use crate::syscalls::set_heap_size;
use crate::error::KernelError;
#[allow(missing_debug_implementations)]
pub struct Allocator(Mutex<Heap>);
impl Allocator {
fn expand(heap: &mut MutexGuard<'_, Heap>, by: usize) -> Result<(), KernelError> {
let total = heap.size() + align_up(by, 0x200_000);
let heap_bottom = unsafe { set_heap_size(total)? };
if heap.bottom() == 0 {
unsafe { **heap = Heap::new(heap_bottom, total) };
} else {
unsafe { heap.extend(align_up(by, 0x200_000)) };
}
Ok(())
}
pub const fn new() -> Allocator {
Allocator(Mutex::new(Heap::empty()))
}
}
unsafe impl GlobalAlloc for Allocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let mut heap = self.0.lock();
let allocation_result = heap.allocate_first_fit(layout);
match allocation_result {
Err(_) => {
if Self::expand(&mut heap, layout.size()).is_ok() {
heap.allocate_first_fit(layout)
} else {
allocation_result
}
}
Ok(_) => allocation_result
}.ok().map_or(core::ptr::null_mut(), |allocation| allocation.as_ptr())
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
self.0.lock().deallocate(NonNull::new(ptr).unwrap(), layout)
}
}