| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | use super::{flags::*, Flags};
|
| 6 | use core::alloc::{GlobalAlloc, Layout};
|
| 7 | use core::ptr;
|
| 8 |
|
| 9 | struct KernelAllocator;
|
| 10 |
|
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: Flags) -> *mut u8 {
|
| 18 | |
| 19 | let layout = new_layout.pad_to_align();
|
| 20 |
|
| 21 | |
| 22 | |
| 23 | |
| 24 | let size = layout.size();
|
| 25 |
|
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, flags.0) as *mut u8 }
|
| 32 | }
|
| 33 |
|
| 34 | unsafe impl GlobalAlloc for KernelAllocator {
|
| 35 | unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
| 36 | |
| 37 | |
| 38 | unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL) }
|
| 39 | }
|
| 40 |
|
| 41 | unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
|
| 42 | unsafe {
|
| 43 | bindings::kfree(ptr as *const core::ffi::c_void);
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | let layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
|
| 53 |
|
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | unsafe { krealloc_aligned(ptr, layout, GFP_KERNEL) }
|
| 60 | }
|
| 61 |
|
| 62 | unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
|
| 63 | |
| 64 | |
| 65 | unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL | __GFP_ZERO) }
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | #[global_allocator]
|
| 70 | static ALLOCATOR: KernelAllocator = KernelAllocator;
|
| 71 |
|
| 72 | |
| 73 | #[no_mangle]
|
| 74 | static __rust_no_alloc_shim_is_unstable: u8 = 0;
|