Skip to content

Commit 82178a2

Browse files
committed
uefi: MemoryMapSize -> MemoryMapMeta
This prepares the following changes.
1 parent 1b7a9bd commit 82178a2

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

uefi/src/table/boot.rs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -179,26 +179,26 @@ impl BootServices {
179179
unsafe { (self.0.free_pages)(addr, count) }.to_result()
180180
}
181181

182-
/// Returns struct which contains the size of a single memory descriptor
183-
/// as well as the size of the current memory map.
182+
/// Queries the `get_memory_map` function of UEFI to retrieve the current
183+
/// size of the map. Returns a [`MemoryMapMeta`].
184184
///
185-
/// Note that the size of the memory map can increase any time an allocation happens,
186-
/// so when creating a buffer to put the memory map into, it's recommended to allocate a few extra
187-
/// elements worth of space above the size of the current memory map.
185+
/// It is recommended to add a few more bytes for a subsequent allocation
186+
/// for the memory map, as the memory map itself also needs heap memory,
187+
/// and other allocations might occur before that call.
188188
#[must_use]
189-
pub fn memory_map_size(&self) -> MemoryMapSize {
189+
pub fn memory_map_size(&self) -> MemoryMapMeta {
190190
let mut map_size = 0;
191191
let mut map_key = MemoryMapKey(0);
192192
let mut desc_size = 0;
193-
let mut entry_version = 0;
193+
let mut desc_version = 0;
194194

195195
let status = unsafe {
196196
(self.0.get_memory_map)(
197197
&mut map_size,
198198
ptr::null_mut(),
199199
&mut map_key.0,
200200
&mut desc_size,
201-
&mut entry_version,
201+
&mut desc_version,
202202
)
203203
};
204204
assert_eq!(status, Status::BUFFER_TOO_SMALL);
@@ -209,9 +209,11 @@ impl BootServices {
209209
"Memory map must be a multiple of the reported descriptor size."
210210
);
211211

212-
MemoryMapSize {
212+
MemoryMapMeta {
213213
desc_size,
214214
map_size,
215+
map_key,
216+
desc_version,
215217
}
216218
}
217219

@@ -1619,14 +1621,30 @@ impl Align for MemoryDescriptor {
16191621
#[repr(C)]
16201622
pub struct MemoryMapKey(usize);
16211623

1622-
/// A structure containing the size of a memory descriptor and the size of the
1623-
/// memory map.
1624-
#[derive(Debug)]
1625-
pub struct MemoryMapSize {
1626-
/// Size of a single memory descriptor in bytes
1627-
pub desc_size: usize,
1628-
/// Size of the entire memory map in bytes
1624+
/// A structure containing the meta attributes associated with a call to
1625+
/// `GetMemoryMap` of UEFI. Note that all values refer to the time this was
1626+
/// called. All following invocations (hidden, subtle, and asynchronous ones)
1627+
/// will likely invalidate this.
1628+
#[derive(Copy, Clone, Debug)]
1629+
pub struct MemoryMapMeta {
1630+
/// The actual size of the map.
16291631
pub map_size: usize,
1632+
/// The reported memory descriptor size. Note that this is the reference
1633+
/// and never `size_of::<MemoryDescriptor>()`!
1634+
pub desc_size: usize,
1635+
/// A unique memory key bound to a specific memory map version/state.
1636+
pub map_key: MemoryMapKey,
1637+
/// The version of the descriptor struct.
1638+
pub desc_version: u32,
1639+
}
1640+
1641+
impl MemoryMapMeta {
1642+
/// Returns the amount of entries in the map.
1643+
#[must_use]
1644+
pub fn entry_count(&self) -> usize {
1645+
assert_eq!(self.map_size % self.desc_size, 0);
1646+
self.map_size / self.desc_size
1647+
}
16301648
}
16311649

16321650
/// An accessory to the memory map that can be either iterated or
@@ -1644,8 +1662,6 @@ pub struct MemoryMapSize {
16441662
/// always use `entry_size` as step-size when interfacing with the memory map on
16451663
/// a low level.
16461664
///
1647-
///
1648-
///
16491665
/// [0]: https://github.com/tianocore/edk2/blob/7142e648416ff5d3eac6c6d607874805f5de0ca8/MdeModulePkg/Core/PiSmmCore/Page.c#L1059
16501666
#[derive(Debug)]
16511667
pub struct MemoryMap<'buf> {

0 commit comments

Comments
 (0)