Linux Audio

Check our new training course

Loading...
 1// SPDX-License-Identifier: GPL-2.0
 2
 3//! Allocator support.
 4
 5use core::alloc::{GlobalAlloc, Layout};
 6use core::ptr;
 7
 8use crate::bindings;
 9
10struct KernelAllocator;
11
12unsafe impl GlobalAlloc for KernelAllocator {
13    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
14        // `krealloc()` is used instead of `kmalloc()` because the latter is
15        // an inline function and cannot be bound to as a result.
16        unsafe { bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *mut u8 }
17    }
18
19    unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
20        unsafe {
21            bindings::kfree(ptr as *const core::ffi::c_void);
22        }
23    }
24}
25
26#[global_allocator]
27static ALLOCATOR: KernelAllocator = KernelAllocator;
28
29// `rustc` only generates these for some crate types. Even then, we would need
30// to extract the object file that has them from the archive. For the moment,
31// let's generate them ourselves instead.
32//
33// Note that `#[no_mangle]` implies exported too, nowadays.
34#[no_mangle]
35fn __rust_alloc(size: usize, _align: usize) -> *mut u8 {
36    unsafe { bindings::krealloc(core::ptr::null(), size, bindings::GFP_KERNEL) as *mut u8 }
37}
38
39#[no_mangle]
40fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
41    unsafe { bindings::kfree(ptr as *const core::ffi::c_void) };
42}
43
44#[no_mangle]
45fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: usize) -> *mut u8 {
46    unsafe {
47        bindings::krealloc(
48            ptr as *const core::ffi::c_void,
49            new_size,
50            bindings::GFP_KERNEL,
51        ) as *mut u8
52    }
53}
54
55#[no_mangle]
56fn __rust_alloc_zeroed(size: usize, _align: usize) -> *mut u8 {
57    unsafe {
58        bindings::krealloc(
59            core::ptr::null(),
60            size,
61            bindings::GFP_KERNEL | bindings::__GFP_ZERO,
62        ) as *mut u8
63    }
64}