#![no_std]
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
pub use kernel_mode::*;
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
mod kernel_mode {
use core::alloc::{GlobalAlloc, Layout};
use wdk_sys::{
POOL_FLAG_NON_PAGED,
SIZE_T,
ULONG,
ntddk::{ExAllocatePool2, ExFreePool},
};
pub struct WdkAllocator;
const RUST_TAG: ULONG = u32::from_ne_bytes(*b"rust");
unsafe impl GlobalAlloc for WdkAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let ptr =
unsafe {
ExAllocatePool2(POOL_FLAG_NON_PAGED, layout.size() as SIZE_T, RUST_TAG)
};
if ptr.is_null() {
return core::ptr::null_mut();
}
ptr.cast()
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
unsafe {
ExFreePool(ptr.cast());
}
}
}
}