buddy_slab_allocator/slab/
mod.rs1pub mod cache;
10pub mod page;
11pub mod size_class;
12
13use core::{alloc::Layout, ptr::NonNull};
14
15use ax_kspin::SpinRaw as SpinMutex;
16use cache::{CacheDeallocResult, SlabCache};
17pub use page::SlabPageHeader;
18pub use size_class::SizeClass;
19
20use crate::error::{AllocError, AllocResult};
21
22pub enum SlabAllocResult {
24 Allocated(NonNull<u8>),
26 NeedsSlab { size_class: SizeClass, pages: usize },
30}
31
32pub enum SlabDeallocResult {
34 Done,
36 FreeSlab { base: usize, pages: usize },
38}
39
40pub enum SlabPoolDeallocResult {
42 Done,
44 RemoteQueued,
46 FreeSlab { base: usize, pages: usize },
48}
49
50pub trait SlabTrait: Sync {
52 fn cpu_id(&self) -> usize;
54
55 fn page_size(&self) -> usize;
57
58 fn alloc(&self, layout: Layout) -> AllocResult<SlabAllocResult>;
60
61 fn add_slab(&self, size_class: SizeClass, base: usize, bytes: usize);
63
64 fn dealloc_local(&self, ptr: NonNull<u8>, layout: Layout) -> SlabDeallocResult;
66
67 fn dealloc_remote(&self, ptr: NonNull<u8>) {
69 let owner_cpu = u16::try_from(self.cpu_id()).expect("CPU id exceeds slab owner range");
70 unsafe { SlabPageHeader::remote_free_object(ptr, owner_cpu, self.page_size()) };
71 }
72}
73
74pub trait SlabPoolTrait: Sync {
76 fn current_slab(&self) -> &dyn SlabTrait;
78
79 fn owner_slab(&self, cpu_idx: usize) -> &dyn SlabTrait;
81
82 fn current_cpu_id(&self) -> usize {
84 self.current_slab().cpu_id()
85 }
86
87 fn alloc(&self, layout: Layout) -> AllocResult<SlabAllocResult> {
89 self.current_slab().alloc(layout)
90 }
91
92 fn add_slab(&self, size_class: SizeClass, base: usize, bytes: usize) {
94 self.current_slab().add_slab(size_class, base, bytes)
95 }
96
97 fn dealloc(&self, ptr: NonNull<u8>, layout: Layout, owner_cpu: usize) -> SlabPoolDeallocResult {
99 if owner_cpu == self.current_cpu_id() {
100 match self.current_slab().dealloc_local(ptr, layout) {
101 SlabDeallocResult::Done => SlabPoolDeallocResult::Done,
102 SlabDeallocResult::FreeSlab { base, pages } => {
103 SlabPoolDeallocResult::FreeSlab { base, pages }
104 }
105 }
106 } else {
107 self.owner_slab(owner_cpu).dealloc_remote(ptr);
108 SlabPoolDeallocResult::RemoteQueued
109 }
110 }
111}
112
113pub trait SlabPoolExt: SlabPoolTrait {
115 fn with_current_slab<R>(&self, f: impl FnOnce(&dyn SlabTrait) -> R) -> R {
117 f(self.current_slab())
118 }
119
120 fn with_owner_slab<R>(&self, cpu_idx: usize, f: impl FnOnce(&dyn SlabTrait) -> R) -> R {
122 f(self.owner_slab(cpu_idx))
123 }
124}
125
126impl<T: ?Sized + SlabPoolTrait> SlabPoolExt for T {}
127
128pub struct SlabAllocator<const PAGE_SIZE: usize = 0x1000> {
130 caches: [SlabCache; SizeClass::COUNT],
131}
132
133pub struct PerCpuSlab<const PAGE_SIZE: usize = 0x1000> {
135 cpu_id: u16,
136 inner: SpinMutex<SlabAllocator<PAGE_SIZE>>,
137}
138
139pub struct StaticSlabPool<const PAGE_SIZE: usize = 0x1000, const N: usize = 1> {
141 slabs: [PerCpuSlab<PAGE_SIZE>; N],
142 current_cpu_id: fn() -> usize,
143}
144
145impl<const PAGE_SIZE: usize> PerCpuSlab<PAGE_SIZE> {
146 pub const fn new(cpu_id: u16) -> Self {
148 Self {
149 cpu_id,
150 inner: SpinMutex::new(SlabAllocator::new()),
151 }
152 }
153
154 pub fn reset(&self) {
156 *self.inner.lock() = SlabAllocator::new();
157 }
158
159 pub const fn cpu_id(&self) -> usize {
161 self.cpu_id as usize
162 }
163
164 pub fn alloc(&self, layout: Layout) -> AllocResult<SlabAllocResult> {
166 self.inner.lock().alloc(layout)
167 }
168
169 pub fn add_slab(&self, size_class: SizeClass, base: usize, bytes: usize) {
171 self.inner
172 .lock()
173 .add_slab(size_class, base, bytes, self.cpu_id);
174 }
175
176 pub fn dealloc_local(&self, ptr: NonNull<u8>, layout: Layout) -> SlabDeallocResult {
178 self.inner.lock().dealloc(ptr, layout)
179 }
180
181 pub fn dealloc_remote(&self, ptr: NonNull<u8>) {
183 unsafe { SlabPageHeader::remote_free_object(ptr, self.cpu_id, PAGE_SIZE) };
184 }
185}
186
187impl<const PAGE_SIZE: usize, const N: usize> StaticSlabPool<PAGE_SIZE, N> {
188 pub const fn new(slabs: [PerCpuSlab<PAGE_SIZE>; N], current_cpu_id: fn() -> usize) -> Self {
190 Self {
191 slabs,
192 current_cpu_id,
193 }
194 }
195}
196
197impl<const PAGE_SIZE: usize> SlabAllocator<PAGE_SIZE> {
198 pub const fn new() -> Self {
200 Self {
201 caches: [
202 SlabCache::new(SizeClass::Bytes8),
203 SlabCache::new(SizeClass::Bytes16),
204 SlabCache::new(SizeClass::Bytes32),
205 SlabCache::new(SizeClass::Bytes64),
206 SlabCache::new(SizeClass::Bytes128),
207 SlabCache::new(SizeClass::Bytes256),
208 SlabCache::new(SizeClass::Bytes512),
209 SlabCache::new(SizeClass::Bytes1024),
210 SlabCache::new(SizeClass::Bytes2048),
211 ],
212 }
213 }
214}
215
216impl<const PAGE_SIZE: usize> Default for SlabAllocator<PAGE_SIZE> {
217 fn default() -> Self {
218 Self::new()
219 }
220}
221
222impl<const PAGE_SIZE: usize> SlabAllocator<PAGE_SIZE> {
223 pub fn alloc(&mut self, layout: Layout) -> AllocResult<SlabAllocResult> {
228 let sc = SizeClass::from_layout(layout).ok_or(AllocError::InvalidParam)?;
229 let cache = &mut self.caches[sc.index()];
230
231 match cache.alloc_object::<PAGE_SIZE>() {
232 Some(addr) => {
233 let ptr = unsafe { NonNull::new_unchecked(addr as *mut u8) };
235 Ok(SlabAllocResult::Allocated(ptr))
236 }
237 None => Ok(SlabAllocResult::NeedsSlab {
238 size_class: sc,
239 pages: sc.slab_pages(PAGE_SIZE),
240 }),
241 }
242 }
243
244 pub fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) -> SlabDeallocResult {
249 let sc = SizeClass::from_layout(layout).expect("layout exceeds slab size");
250 let cache = &mut self.caches[sc.index()];
251
252 match cache.dealloc_object::<PAGE_SIZE>(ptr.as_ptr() as usize) {
253 CacheDeallocResult::Done => SlabDeallocResult::Done,
254 CacheDeallocResult::FreeSlab { base, pages } => {
255 SlabDeallocResult::FreeSlab { base, pages }
256 }
257 }
258 }
259
260 pub fn add_slab(&mut self, size_class: SizeClass, base: usize, bytes: usize, owner_cpu: u16) {
264 self.caches[size_class.index()].add_slab(base, bytes, owner_cpu);
265 }
266}
267
268impl<const PAGE_SIZE: usize> SlabTrait for PerCpuSlab<PAGE_SIZE> {
269 fn cpu_id(&self) -> usize {
270 PerCpuSlab::cpu_id(self)
271 }
272
273 fn page_size(&self) -> usize {
274 PAGE_SIZE
275 }
276
277 fn alloc(&self, layout: Layout) -> AllocResult<SlabAllocResult> {
278 PerCpuSlab::alloc(self, layout)
279 }
280
281 fn add_slab(&self, size_class: SizeClass, base: usize, bytes: usize) {
282 PerCpuSlab::add_slab(self, size_class, base, bytes)
283 }
284
285 fn dealloc_local(&self, ptr: NonNull<u8>, layout: Layout) -> SlabDeallocResult {
286 PerCpuSlab::dealloc_local(self, ptr, layout)
287 }
288}
289
290impl<const PAGE_SIZE: usize, const N: usize> SlabPoolTrait for StaticSlabPool<PAGE_SIZE, N> {
291 fn current_slab(&self) -> &dyn SlabTrait {
292 &self.slabs[(self.current_cpu_id)()]
293 }
294
295 fn owner_slab(&self, cpu_idx: usize) -> &dyn SlabTrait {
296 &self.slabs[cpu_idx]
297 }
298}