pie_boot_if/
memregions.rs1use core::{fmt, ops, slice};
2
3#[derive(Clone, Copy)]
4pub struct MemoryRegion {
5 pub start: usize,
7 pub end: usize,
9 pub kind: MemoryRegionKind,
13}
14
15impl fmt::Debug for MemoryRegion {
16 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17 f.debug_struct("MemoryRegion")
18 .field("start", &(self.start as *const u8))
19 .field("end", &(self.end as *const u8))
20 .field("kind", &self.kind)
21 .finish()
22 }
23}
24
25#[derive(Debug, Copy, Clone, Eq, PartialEq)]
27#[non_exhaustive]
28#[repr(C)]
29pub enum MemoryRegionKind {
30 Ram,
32 Reserved,
34 Bootloader,
38 UnknownUefi(u32),
42 UnknownBios(u32),
46}
47
48#[derive(Clone)]
55#[repr(C)]
56pub struct MemoryRegions {
57 pub(crate) ptr: *mut MemoryRegion,
58 pub(crate) len: usize,
59}
60
61impl ops::Deref for MemoryRegions {
62 type Target = [MemoryRegion];
63
64 fn deref(&self) -> &Self::Target {
65 unsafe { slice::from_raw_parts(self.ptr, self.len) }
66 }
67}
68
69impl ops::DerefMut for MemoryRegions {
70 fn deref_mut(&mut self) -> &mut Self::Target {
71 unsafe { slice::from_raw_parts_mut(self.ptr, self.len) }
72 }
73}
74
75impl From<&'static mut [MemoryRegion]> for MemoryRegions {
76 fn from(regions: &'static mut [MemoryRegion]) -> Self {
77 MemoryRegions {
78 ptr: regions.as_mut_ptr(),
79 len: regions.len(),
80 }
81 }
82}
83
84impl From<MemoryRegions> for &'static mut [MemoryRegion] {
85 fn from(regions: MemoryRegions) -> &'static mut [MemoryRegion] {
86 unsafe { slice::from_raw_parts_mut(regions.ptr, regions.len) }
87 }
88}
89
90impl fmt::Debug for MemoryRegions {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 writeln!(f, "MemoryRegions [")?;
93 if !self.ptr.is_null() {
94 for region in self.iter() {
95 writeln!(f, "{region:?}")?;
96 }
97 }
98 writeln!(f, "]")
99 }
100}
101
102unsafe impl Send for MemoryRegions {}
103unsafe impl Sync for MemoryRegions {}