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
use std::{
io::{BufReader, Read, Seek, SeekFrom},
mem::size_of,
path::Path,
};
use crate::{process::Pfn, FileWrapper};
use super::ProcResult;
pub struct KPageCount {
reader: BufReader<FileWrapper>,
}
impl KPageCount {
pub fn new() -> ProcResult<Self> {
Self::from_custom_root("/proc")
}
pub fn from_custom_root<P: AsRef<Path>>(root: P) -> ProcResult<Self> {
let mut path = root.as_ref().to_path_buf();
path.push("kpagecount");
let reader = BufReader::new(FileWrapper::open(path)?);
Ok(Self { reader })
}
pub fn get_count_at_pfn(&mut self, pfn: Pfn) -> ProcResult<u64> {
self.get_count_in_range(pfn, Pfn(pfn.0 + 1))
.map(|mut vec| vec.pop().unwrap())
}
pub fn get_count_in_range(&mut self, start: Pfn, end: Pfn) -> ProcResult<Vec<u64>> {
let mut result = Vec::with_capacity((end.0 - start.0) as usize);
let start_position = start.0 * size_of::<u64>() as u64;
self.reader.seek(SeekFrom::Start(start_position))?;
for _pfn in start.0..end.0 {
let mut buf = [0; size_of::<u64>()];
self.reader.read_exact(&mut buf)?;
let page_references: u64 = u64::from_le_bytes(buf);
result.push(page_references);
}
Ok(result)
}
}