1use crate::config::{PAGE_MASK, PAGE_SHIFT, PAGE_SIZE};
2use crate::context::{CpuLocal, current_thread, uma};
3use crate::uma::{Alloc, SlabFlags, UmaFlags, UmaZone};
4use crate::vm::{PageObj, Vm, kaddr_to_phys};
5use alloc::string::ToString;
6use alloc::sync::Arc;
7use alloc::vec::Vec;
8use core::alloc::Layout;
9use core::cell::RefCell;
10use core::num::NonZero;
11
12pub struct VmHeap {
15 vm: &'static Vm,
16 zones: [Vec<Arc<UmaZone>>; PAGE_SHIFT + 1], stats: CpuLocal<RefCell<Stats>>, }
19
20impl VmHeap {
21 const KMEM_ZSHIFT: usize = 4;
22 const KMEM_ZBASE: usize = 16;
23 const KMEM_ZMASK: usize = Self::KMEM_ZBASE - 1;
24 const KMEM_ZSIZE: usize = PAGE_SIZE.get() >> Self::KMEM_ZSHIFT;
25
26 pub fn new(vm: &'static Vm) -> Self {
33 let uma = uma().unwrap();
37 let zones = core::array::from_fn(|align| {
38 let mut zones = Vec::with_capacity(Self::KMEM_ZSIZE + 1);
39 let mut last = 0;
40 let align = align
41 .try_into()
42 .ok()
43 .and_then(|align| 1usize.checked_shl(align))
44 .unwrap();
45
46 for i in Self::KMEM_ZSHIFT.. {
47 let size = NonZero::new(1usize << i).unwrap();
49
50 if size > PAGE_SIZE {
51 break;
52 }
53
54 let zone = Arc::new(uma.into_owned().create_zone(
56 size.to_string(),
57 size,
58 Some(align - 1),
59 None,
60 UmaFlags::Malloc,
61 ));
62
63 while last <= size.get() {
64 zones.push(zone.clone());
65 last += Self::KMEM_ZBASE;
66 }
67 }
68
69 zones
70 });
71
72 Self {
73 vm,
74 zones,
75 stats: CpuLocal::new(|_| RefCell::default()),
76 }
77 }
78
79 pub unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
91 let td = current_thread();
93
94 if !td.can_sleep() {
95 panic!("heap allocation in a non-sleeping context is not supported");
96 }
97
98 let lock = td.disable_vm_heap();
100 let size = layout.size();
101 let mem = if size <= PAGE_SIZE.get() {
102 let align = layout.align().trailing_zeros() as usize;
104 let size = if (size & Self::KMEM_ZMASK) != 0 {
105 (size + Self::KMEM_ZBASE) & !Self::KMEM_ZMASK
107 } else {
108 size
109 };
110
111 let zone = &self.zones[align][size >> Self::KMEM_ZSHIFT];
113 let mem = zone.alloc(Alloc::Wait | Alloc::Zero);
114
115 let stats = self.stats.lock();
117 let mut stats = stats.borrow_mut();
118 let size = if mem.is_null() { 0 } else { zone.size().get() };
119
120 if size != 0 {
121 stats.alloc_bytes = stats
122 .alloc_bytes
123 .checked_add(size.try_into().unwrap())
124 .unwrap();
125 stats.alloc_count += 1;
126 }
127
128 mem
130 } else {
131 todo!()
132 };
133
134 drop(lock);
135
136 mem
137 }
138
139 pub unsafe fn dealloc(&self, ptr: *mut u8, _: Layout) {
150 let page = (ptr as usize) & !PAGE_MASK.get();
151 let page = unsafe { kaddr_to_phys(page) };
152 let page = self.vm.phys_to_page(page).unwrap(); let ps = page.state.lock();
154 let obj = ps.object.as_ref().unwrap(); let slab = match obj {
156 PageObj::Slab(s) => unsafe { s.as_ref() },
157 };
158
159 if slab.flags().has_any(SlabFlags::Malloc) {
160 todo!()
161 } else {
162 todo!()
163 }
164 }
165}
166
167#[derive(Default)]
169struct Stats {
170 alloc_bytes: u64, alloc_count: u64, }