Skip to content

Commit 4ee222b

Browse files
committed
uefi: MemoryMapSize -> MemoryMapMeta
This prepares the following changes.
1 parent e8853d2 commit 4ee222b

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

@@ -1615,14 +1617,30 @@ impl Align for MemoryDescriptor {
16151617
#[repr(C)]
16161618
pub struct MemoryMapKey(usize);
16171619

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

16281646
/// An accessory to the memory map that can be either iterated or
@@ -1640,8 +1658,6 @@ pub struct MemoryMapSize {
16401658
/// always use `entry_size` as step-size when interfacing with the memory map on
16411659
/// a low level.
16421660
///
1643-
///
1644-
///
16451661
/// [0]: https://github.com/tianocore/edk2/blob/7142e648416ff5d3eac6c6d607874805f5de0ca8/MdeModulePkg/Core/PiSmmCore/Page.c#L1059
16461662
#[derive(Debug)]
16471663
pub struct MemoryMap<'buf> {

0 commit comments

Comments
 (0)