@@ -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
@@ -1619,14 +1621,30 @@ impl Align for MemoryDescriptor {
1619
1621
#[ repr( C ) ]
1620
1622
pub struct MemoryMapKey ( usize ) ;
1621
1623
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.
1629
1631
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
+ }
1630
1648
}
1631
1649
1632
1650
/// An accessory to the memory map that can be either iterated or
@@ -1644,8 +1662,6 @@ pub struct MemoryMapSize {
1644
1662
/// always use `entry_size` as step-size when interfacing with the memory map on
1645
1663
/// a low level.
1646
1664
///
1647
- ///
1648
- ///
1649
1665
/// [0]: https://github.com/tianocore/edk2/blob/7142e648416ff5d3eac6c6d607874805f5de0ca8/MdeModulePkg/Core/PiSmmCore/Page.c#L1059
1650
1666
#[ derive( Debug ) ]
1651
1667
pub struct MemoryMap < ' buf > {
0 commit comments