windows_win/raw/
memory.rs1use core::{fmt, ptr, mem};
4
5use crate::sys::*;
6use crate::utils::{self, Result};
7
8pub struct Info(pub MEMORY_BASIC_INFORMATION);
10
11impl Info {
12 #[inline]
13 pub fn base_addr(&self) -> usize {
19 self.0.BaseAddress as usize
20 }
21
22 #[inline]
23 pub fn alloc_base(&self) -> usize {
27 self.0.AllocationBase as usize
28 }
29
30 #[inline]
31 pub fn size(&self) -> SIZE_T {
33 self.0.RegionSize
34 }
35
36 #[inline]
37 pub fn is_commit(&self) -> bool {
41 self.0.State == MEM_COMMIT
42 }
43
44 #[inline]
45 pub fn is_free(&self) -> bool {
47 self.0.State == MEM_FREE
48 }
49
50 #[inline]
51 pub fn is_reserved(&self) -> bool {
55 self.0.State == MEM_RESERVE
56 }
57}
58
59impl fmt::Debug for Info {
60 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61 write!(f, "Info {{ BaseAddress={:p}, AllocationBase={:p}, AllocationProtect={}, RegionSize={}, State={}, Protect={}, Type={} }}",
62 self.0.BaseAddress, self.0.AllocationBase, self.0.AllocationProtect, self.0.RegionSize, self.0.State, self.0.Protect, self.0.Type)
63 }
64}
65
66pub struct Virtual {
70 handle: HANDLE,
71 addr: *const u8
72}
73
74impl Virtual {
75 pub fn new(handle: HANDLE) -> Self {
79 Virtual {
80 handle,
81 addr: ptr::null()
82 }
83 }
84}
85
86impl Iterator for Virtual {
87 type Item = Info;
88
89 fn next(&mut self) -> Option<Self::Item> {
90 virtual_query_ex(self.handle, self.addr as *const c_void).ok().map(|info| {
91 self.addr = unsafe { self.addr.add(info.size()) };
92 info
93 })
94 }
95}
96
97pub fn virtual_query_ex(handle: HANDLE, base: *const c_void) -> Result<Info> {
108 let mut info: MEMORY_BASIC_INFORMATION = unsafe { mem::zeroed() };
109
110 if unsafe { VirtualQueryEx(handle, base, &mut info as *mut _, mem::size_of_val(&info) as SIZE_T) } != 0 {
111 Ok(Info(info))
112 }
113 else {
114 Err(utils::get_last_error())
115 }
116}