| 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 | fn aligned_size(new_layout: Layout) -> usize {
|
| 48 | |
| 49 | let layout = new_layout.pad_to_align();
|
| 50 |
|
| 51 | |
| 52 | |
| 53 | |
| 54 | layout.size()
|
| 55 | }
|
| 56 |
|
| 57 | |
| 58 | |
| 59 | |
| 60 | struct ReallocFunc(
|
| 61 | unsafe extern "C" fn(*const crate::ffi::c_void, usize, u32) -> *mut crate::ffi::c_void,
|
| 62 | );
|
| 63 |
|
| 64 | impl ReallocFunc {
|
| 65 | |
| 66 | const KREALLOC: Self = Self(bindings::krealloc);
|
| 67 |
|
| 68 | |
| 69 | const VREALLOC: Self = Self(bindings::vrealloc);
|
| 70 |
|
| 71 | |
| 72 | const KVREALLOC: Self = Self(bindings::kvrealloc);
|
| 73 |
|
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | #[inline]
|
| 84 | unsafe fn call(
|
| 85 | &self,
|
| 86 | ptr: Option<NonNull<u8>>,
|
| 87 | layout: Layout,
|
| 88 | old_layout: Layout,
|
| 89 | flags: Flags,
|
| 90 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 91 | let size = aligned_size(layout);
|
| 92 | let ptr = match ptr {
|
| 93 | Some(ptr) => {
|
| 94 | if old_layout.size() == 0 {
|
| 95 | ptr::null()
|
| 96 | } else {
|
| 97 | ptr.as_ptr()
|
| 98 | }
|
| 99 | }
|
| 100 | None => ptr::null(),
|
| 101 | };
|
| 102 |
|
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | let raw_ptr = unsafe {
|
| 112 | |
| 113 | self.0(ptr.cast(), size, flags.0).cast()
|
| 114 | };
|
| 115 |
|
| 116 | let ptr = if size == 0 {
|
| 117 | crate::alloc::dangling_from_layout(layout)
|
| 118 | } else {
|
| 119 | NonNull::new(raw_ptr).ok_or(AllocError)?
|
| 120 | };
|
| 121 |
|
| 122 | Ok(NonNull::slice_from_raw_parts(ptr, size))
|
| 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 | |
| 139 | unsafe { ReallocFunc::KREALLOC.call(ptr, layout, old_layout, flags) }
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | unsafe impl Allocator for Vmalloc {
|
| 148 | #[inline]
|
| 149 | unsafe fn realloc(
|
| 150 | ptr: Option<NonNull<u8>>,
|
| 151 | layout: Layout,
|
| 152 | old_layout: Layout,
|
| 153 | flags: Flags,
|
| 154 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 155 | |
| 156 | if layout.align() > bindings::PAGE_SIZE {
|
| 157 | pr_warn!("Vmalloc does not support alignments larger than PAGE_SIZE yet.\n");
|
| 158 | return Err(AllocError);
|
| 159 | }
|
| 160 |
|
| 161 | |
| 162 | |
| 163 | unsafe { ReallocFunc::VREALLOC.call(ptr, layout, old_layout, flags) }
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | unsafe impl Allocator for KVmalloc {
|
| 172 | #[inline]
|
| 173 | unsafe fn realloc(
|
| 174 | ptr: Option<NonNull<u8>>,
|
| 175 | layout: Layout,
|
| 176 | old_layout: Layout,
|
| 177 | flags: Flags,
|
| 178 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 179 | |
| 180 | if layout.align() > bindings::PAGE_SIZE {
|
| 181 | pr_warn!("KVmalloc does not support alignments larger than PAGE_SIZE yet.\n");
|
| 182 | return Err(AllocError);
|
| 183 | }
|
| 184 |
|
| 185 | |
| 186 | |
| 187 | unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags) }
|
| 188 | }
|
| 189 | }
|