| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 |
|
| 11 | use super::Flags;
|
| 12 | use core::alloc::Layout;
|
| 13 | use core::ptr;
|
| 14 | use core::ptr::NonNull;
|
| 15 |
|
| 16 | use crate::alloc::{AllocError, Allocator};
|
| 17 | use crate::bindings;
|
| 18 | use crate::pr_warn;
|
| 19 |
|
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | pub struct Kmalloc;
|
| 27 |
|
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | pub struct Vmalloc;
|
| 36 |
|
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | pub struct KVmalloc;
|
| 45 |
|
| 46 | |
| 47 | |
| 48 | |
| 49 | struct ReallocFunc(
|
| 50 | unsafe extern "C" fn(*const crate::ffi::c_void, usize, u32) -> *mut crate::ffi::c_void,
|
| 51 | );
|
| 52 |
|
| 53 | impl ReallocFunc {
|
| 54 | |
| 55 | const KREALLOC: Self = Self(bindings::krealloc);
|
| 56 |
|
| 57 | |
| 58 | const VREALLOC: Self = Self(bindings::vrealloc);
|
| 59 |
|
| 60 | |
| 61 | const KVREALLOC: Self = Self(bindings::kvrealloc);
|
| 62 |
|
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | #[inline]
|
| 73 | unsafe fn call(
|
| 74 | &self,
|
| 75 | ptr: Option<NonNull<u8>>,
|
| 76 | layout: Layout,
|
| 77 | old_layout: Layout,
|
| 78 | flags: Flags,
|
| 79 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 80 | let size = layout.size();
|
| 81 | let ptr = match ptr {
|
| 82 | Some(ptr) => {
|
| 83 | if old_layout.size() == 0 {
|
| 84 | ptr::null()
|
| 85 | } else {
|
| 86 | ptr.as_ptr()
|
| 87 | }
|
| 88 | }
|
| 89 | None => ptr::null(),
|
| 90 | };
|
| 91 |
|
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | let raw_ptr = unsafe {
|
| 101 | |
| 102 | self.0(ptr.cast(), size, flags.0).cast()
|
| 103 | };
|
| 104 |
|
| 105 | let ptr = if size == 0 {
|
| 106 | crate::alloc::dangling_from_layout(layout)
|
| 107 | } else {
|
| 108 | NonNull::new(raw_ptr).ok_or(AllocError)?
|
| 109 | };
|
| 110 |
|
| 111 | Ok(NonNull::slice_from_raw_parts(ptr, size))
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | impl Kmalloc {
|
| 116 | |
| 117 | |
| 118 | pub fn aligned_layout(layout: Layout) -> Layout {
|
| 119 | |
| 120 | |
| 121 | |
| 122 | layout.pad_to_align()
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | unsafe impl Allocator for Kmalloc {
|
| 131 | #[inline]
|
| 132 | unsafe fn realloc(
|
| 133 | ptr: Option<NonNull<u8>>,
|
| 134 | layout: Layout,
|
| 135 | old_layout: Layout,
|
| 136 | flags: Flags,
|
| 137 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 138 | let layout = Kmalloc::aligned_layout(layout);
|
| 139 |
|
| 140 | |
| 141 | unsafe { ReallocFunc::KREALLOC.call(ptr, layout, old_layout, flags) }
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | unsafe impl Allocator for Vmalloc {
|
| 150 | #[inline]
|
| 151 | unsafe fn realloc(
|
| 152 | ptr: Option<NonNull<u8>>,
|
| 153 | layout: Layout,
|
| 154 | old_layout: Layout,
|
| 155 | flags: Flags,
|
| 156 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 157 | |
| 158 | if layout.align() > bindings::PAGE_SIZE {
|
| 159 | pr_warn!("Vmalloc does not support alignments larger than PAGE_SIZE yet.\n");
|
| 160 | return Err(AllocError);
|
| 161 | }
|
| 162 |
|
| 163 | |
| 164 | |
| 165 | unsafe { ReallocFunc::VREALLOC.call(ptr, layout, old_layout, flags) }
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | unsafe impl Allocator for KVmalloc {
|
| 174 | #[inline]
|
| 175 | unsafe fn realloc(
|
| 176 | ptr: Option<NonNull<u8>>,
|
| 177 | layout: Layout,
|
| 178 | old_layout: Layout,
|
| 179 | flags: Flags,
|
| 180 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 181 | |
| 182 | |
| 183 | let layout = Kmalloc::aligned_layout(layout);
|
| 184 |
|
| 185 | |
| 186 | if layout.align() > bindings::PAGE_SIZE {
|
| 187 | pr_warn!("KVmalloc does not support alignments larger than PAGE_SIZE yet.\n");
|
| 188 | return Err(AllocError);
|
| 189 | }
|
| 190 |
|
| 191 | |
| 192 | |
| 193 | unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags) }
|
| 194 | }
|
| 195 | }
|