forked from immunant/linux-kernel-module-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallocator.rs
More file actions
27 lines (22 loc) · 709 Bytes
/
allocator.rs
File metadata and controls
27 lines (22 loc) · 709 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use core::alloc::{GlobalAlloc, Layout};
use bindings;
use c_types;
pub struct KernelAllocator;
unsafe impl GlobalAlloc for KernelAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
// krealloc is used instead of kmalloc because kmalloc is an inline function and can't be
// bound to as a result
return bindings::krealloc(
0 as *const c_types::c_void,
layout.size(),
bindings::GFP_KERNEL,
) as *mut u8;
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
bindings::kfree(ptr as *const c_types::c_void);
}
}
#[alloc_error_handler]
fn oom(_layout: Layout) -> ! {
panic!("Out of memory!");
}