|
| 1 | +/* |
| 2 | + * Copyright (C) 2021-2023 The Aero Project Developers. |
| 3 | + * |
| 4 | + * This file is part of The Aero Project. |
| 5 | + * |
| 6 | + * Aero is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * Aero is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with Aero. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +use core::ptr::NonNull; |
| 21 | + |
| 22 | +use intrusive_collections::UnsafeRef; |
| 23 | + |
| 24 | +use crate::mem::paging::*; |
| 25 | +use crate::utils::sync::Mutex; |
| 26 | + |
| 27 | +#[repr(C)] |
| 28 | +pub struct SlabHeader { |
| 29 | + pub ptr: UnsafeRef<SmallSlab>, |
| 30 | +} |
| 31 | + |
| 32 | +const_assert_eq!(core::mem::size_of::<SlabHeader>(), 8); |
| 33 | + |
| 34 | +/// For small slabs, the [`BufCtl`]s are stored inline. |
| 35 | +struct BufCtl(Option<NonNull<BufCtl>>); |
| 36 | + |
| 37 | +impl BufCtl { |
| 38 | + const NULL: Self = Self(None); |
| 39 | + |
| 40 | + /// Constructs a [`BufCtl`] from a raw pointer. |
| 41 | + const fn from_ptr(ptr: *mut BufCtl) -> Self { |
| 42 | + assert!(!ptr.is_null()); |
| 43 | + |
| 44 | + // SAFETY: We have verified above that the pointer is non-null. |
| 45 | + Self(Some(unsafe { NonNull::new_unchecked(ptr) })) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +const_assert_eq!(core::mem::size_of::<BufCtl>(), 8); |
| 50 | + |
| 51 | +/// Used for allocations smaller than `1/8` of a page. |
| 52 | +pub struct SmallSlab { |
| 53 | + /// Size of the slab. |
| 54 | + size: usize, |
| 55 | + first_free: Mutex<BufCtl>, |
| 56 | +} |
| 57 | + |
| 58 | +impl SmallSlab { |
| 59 | + pub const fn new(size: usize) -> Self { |
| 60 | + assert!(size.is_power_of_two()); |
| 61 | + |
| 62 | + Self { |
| 63 | + size, |
| 64 | + first_free: Mutex::new(BufCtl::NULL), |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + pub fn alloc(&self) -> *mut u8 { |
| 69 | + let mut first_free = self.first_free.lock_irq(); |
| 70 | + |
| 71 | + if let Some(entry) = first_free.0 { |
| 72 | + *first_free = BufCtl(unsafe { entry.as_ref() }.0); |
| 73 | + entry.as_ptr().cast() |
| 74 | + } else { |
| 75 | + drop(first_free); |
| 76 | + |
| 77 | + self.expand(); |
| 78 | + self.alloc() |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + pub fn dealloc(&self, ptr: *mut u8) { |
| 83 | + assert!(!ptr.is_null()); |
| 84 | + |
| 85 | + let mut first_free = self.first_free.lock_irq(); |
| 86 | + |
| 87 | + let mut new_head = BufCtl::from_ptr(ptr.cast()); |
| 88 | + new_head.0 = first_free.0; |
| 89 | + *first_free = new_head; |
| 90 | + } |
| 91 | + |
| 92 | + fn expand(&self) { |
| 93 | + let frame: PhysFrame<Size4KiB> = FRAME_ALLOCATOR.allocate_frame().expect("slab: OOM"); |
| 94 | + |
| 95 | + let ptr = frame.start_address().as_hhdm_virt().as_mut_ptr::<u8>(); |
| 96 | + let header_size = |
| 97 | + align_up(core::mem::size_of::<SlabHeader>() as u64, self.size as u64) as usize; |
| 98 | + |
| 99 | + let avaliable_size = Size4KiB::SIZE as usize - header_size; |
| 100 | + let slab_ptr = unsafe { &mut *ptr.cast::<SlabHeader>() }; |
| 101 | + |
| 102 | + // SAFETY: We are constructing an [`UnsafeRef`] from ourselves which is a valid reference. |
| 103 | + slab_ptr.ptr = unsafe { UnsafeRef::from_raw(self as *const _) }; |
| 104 | + |
| 105 | + let first_free = unsafe { ptr.add(header_size).cast() }; |
| 106 | + *self.first_free.lock_irq() = BufCtl::from_ptr(first_free); |
| 107 | + |
| 108 | + // Initialize the free-list: |
| 109 | + // |
| 110 | + // For objects smaller than 1/8 of a page, A slab is built by allocating a 4KiB page, |
| 111 | + // placing the slab header at the end, and dividing the rest into equal-size buffers: |
| 112 | + // |
| 113 | + // ------------------------------------------------------ |
| 114 | + // | buffer | buffer | buffer | buffer | slab header |
| 115 | + // ------------------------------------------------------ |
| 116 | + // 4KiB |
| 117 | + let max = (avaliable_size / self.size) - 1; |
| 118 | + let fact = self.size / 8; |
| 119 | + |
| 120 | + for i in 0..max { |
| 121 | + unsafe { |
| 122 | + let entry = first_free.add(i * fact); |
| 123 | + let next = first_free.add((i + 1) * fact); |
| 124 | + |
| 125 | + (&mut *entry).0 = Some(NonNull::new_unchecked(next)); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + unsafe { |
| 130 | + let entry = &mut *first_free.add(max * fact); |
| 131 | + *entry = BufCtl::NULL; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + pub fn size(&self) -> usize { |
| 136 | + self.size |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +unsafe impl Send for SmallSlab {} |
| 141 | +unsafe impl Sync for SmallSlab {} |
0 commit comments