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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#![allow(non_camel_case_types)]
use common::*;
use std;
use std::error::Error;
use std::fmt;
use win_memory::*;
pub trait Memory {
fn as_slice_mut(&mut self) -> &mut [u8];
fn as_ptr(&self) -> *const VOID;
fn get_size(&self) -> usize;
}
#[derive(Debug)]
pub struct MemoryError {}
impl MemoryError {
pub fn new() -> MemoryError {
MemoryError {}
}
}
impl Error for MemoryError {
fn description(&self) -> &str {
"Memory error"
}
}
impl fmt::Display for MemoryError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Failed to allocate memory")
}
}
pub struct VirtualMemory {
address: *mut VOID,
size: usize,
}
impl VirtualMemory {
pub fn new(size: usize) -> Result<VirtualMemory, MemoryError> {
let address = unsafe {
VirtualAlloc(
std::ptr::null_mut(),
size as SIZE_T,
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE,
)
};
match address as u64 {
0 => Err(MemoryError::new()),
_ => Ok(VirtualMemory {
address: address,
size: size,
}),
}
}
}
impl Memory for VirtualMemory {
fn as_slice_mut(&mut self) -> &mut [u8] {
unsafe { std::slice::from_raw_parts_mut(self.address as *mut u8, self.size) }
}
fn as_ptr(&self) -> *const VOID {
self.address
}
fn get_size(&self) -> usize {
self.size
}
}
impl Drop for VirtualMemory {
fn drop(&mut self) {
unsafe { VirtualFree(self.address, 0, MEM_RELEASE) };
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_virtual_alloc_free() {
const SIZE: usize = 0x1000;
let mut mem = VirtualMemory::new(SIZE).unwrap();
let addr = mem.address;
assert_eq!(mem.get_size(), SIZE);
assert_eq!(mem.as_ptr(), addr);
let slice = mem.as_slice_mut();
assert_eq!(slice.as_ptr() as *const VOID, addr);
assert_eq!(slice.len(), SIZE);
}
}