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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#![no_std]
#![feature(alloc_error_handler)]
extern crate alloc;
use crate::nt::{ExFreePool, PoolType};
use alloc::alloc::handle_alloc_error;
use core::alloc::{GlobalAlloc, Layout};
#[doc(hidden)] pub mod nt;
#[cfg(feature = "pool-tag")]
const POOL_TAG: u32 = u32::from_ne_bytes(*b"tsuR");
pub struct KernelAlloc;
unsafe impl GlobalAlloc for KernelAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
#[cfg(feature = "pool-tag")]
let pool = nt::ExAllocatePoolWithTag(PoolType::NonPagedPool, layout.size(), POOL_TAG);
#[cfg(not(feature = "pool-tag"))]
let pool = nt::ExAllocatePool(PoolType::NonPagedPool, layout.size());
if pool.is_null() {
handle_alloc_error(layout);
}
pool as _
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { ExFreePool(ptr as _); }
}
#[alloc_error_handler]
fn alloc_error(layout: Layout) -> ! {
panic!("allocation failed: {:?}", layout);
}