@@ -179,26 +179,26 @@ impl BootServices {
179
179
unsafe { ( self . 0 . free_pages ) ( addr, count) } . to_result ( )
180
180
}
181
181
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`] .
184
184
///
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 .
188
188
#[ must_use]
189
- pub fn memory_map_size ( & self ) -> MemoryMapSize {
189
+ pub fn memory_map_size ( & self ) -> MemoryMapMeta {
190
190
let mut map_size = 0 ;
191
191
let mut map_key = MemoryMapKey ( 0 ) ;
192
192
let mut desc_size = 0 ;
193
- let mut entry_version = 0 ;
193
+ let mut desc_version = 0 ;
194
194
195
195
let status = unsafe {
196
196
( self . 0 . get_memory_map ) (
197
197
& mut map_size,
198
198
ptr:: null_mut ( ) ,
199
199
& mut map_key. 0 ,
200
200
& mut desc_size,
201
- & mut entry_version ,
201
+ & mut desc_version ,
202
202
)
203
203
} ;
204
204
assert_eq ! ( status, Status :: BUFFER_TOO_SMALL ) ;
@@ -209,9 +209,11 @@ impl BootServices {
209
209
"Memory map must be a multiple of the reported descriptor size."
210
210
) ;
211
211
212
- MemoryMapSize {
212
+ MemoryMapMeta {
213
213
desc_size,
214
214
map_size,
215
+ map_key,
216
+ desc_version,
215
217
}
216
218
}
217
219
@@ -1615,14 +1617,30 @@ impl Align for MemoryDescriptor {
1615
1617
#[ repr( C ) ]
1616
1618
pub struct MemoryMapKey ( usize ) ;
1617
1619
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.
1625
1627
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
+ }
1626
1644
}
1627
1645
1628
1646
/// An accessory to the memory map that can be either iterated or
@@ -1640,8 +1658,6 @@ pub struct MemoryMapSize {
1640
1658
/// always use `entry_size` as step-size when interfacing with the memory map on
1641
1659
/// a low level.
1642
1660
///
1643
- ///
1644
- ///
1645
1661
/// [0]: https://github.com/tianocore/edk2/blob/7142e648416ff5d3eac6c6d607874805f5de0ca8/MdeModulePkg/Core/PiSmmCore/Page.c#L1059
1646
1662
#[ derive( Debug ) ]
1647
1663
pub struct MemoryMap < ' buf > {
0 commit comments