@@ -121,28 +121,43 @@ pub unsafe fn raise_tpl(tpl: Tpl) -> TplGuard {
121121 }
122122}
123123
124- /// Allocates memory pages from the system .
124+ /// Allocates a consecutive set of memory pages using the UEFI allocator .
125125///
126- /// UEFI OS loaders should allocate memory of the type `LoaderData`.
126+ /// The caller is responsible to free the memory using [`free_pages`].
127+ ///
128+ /// # Arguments
129+ /// - `allocation_type`: The [`AllocateType`] to alter the allocation strategy.
130+ /// - `memory_type`: The [`MemoryType`] used to persist the allocation in the
131+ /// UEFI memory map. Typically, UEFI OS loaders should allocate memory of
132+ /// type [`MemoryType::LOADER_DATA`].
133+ ///- `size`: Amount of bytes to allocate.
134+ ///
135+ /// # Safety
136+ /// Using this function is safe but reading on initialized memory is not.
137+ /// Please look into the example code.
127138///
128139/// # Errors
129140///
130141/// * [`Status::OUT_OF_RESOURCES`]: allocation failed.
131142/// * [`Status::INVALID_PARAMETER`]: `mem_ty` is [`MemoryType::PERSISTENT_MEMORY`],
132143/// [`MemoryType::UNACCEPTED`], or in the range [`MemoryType::MAX`]`..=0x6fff_ffff`.
133144/// * [`Status::NOT_FOUND`]: the requested pages could not be found.
134- pub fn allocate_pages ( ty : AllocateType , mem_ty : MemoryType , count : usize ) -> Result < NonNull < u8 > > {
145+ pub fn allocate_pages (
146+ allocation_type : AllocateType ,
147+ memory_type : MemoryType ,
148+ count : usize ,
149+ ) -> Result < NonNull < u8 > > {
135150 let bt = boot_services_raw_panicking ( ) ;
136151 let bt = unsafe { bt. as_ref ( ) } ;
137152
138- let ( ty, initial_addr) = match ty {
153+ let ( ty, initial_addr) = match allocation_type {
139154 AllocateType :: AnyPages => ( 0 , 0 ) ,
140155 AllocateType :: MaxAddress ( addr) => ( 1 , addr) ,
141156 AllocateType :: Address ( addr) => ( 2 , addr) ,
142157 } ;
143158
144159 let mut addr1 = initial_addr;
145- unsafe { ( bt. allocate_pages ) ( ty, mem_ty , count, & mut addr1) } . to_result ( ) ?;
160+ unsafe { ( bt. allocate_pages ) ( ty, memory_type , count, & mut addr1) } . to_result ( ) ?;
146161
147162 // The UEFI spec allows `allocate_pages` to return a valid allocation at
148163 // address zero. Rust does not allow writes through a null pointer (which
@@ -156,7 +171,7 @@ pub fn allocate_pages(ty: AllocateType, mem_ty: MemoryType, count: usize) -> Res
156171 // not yet been freed, so if this allocation succeeds it should be at a
157172 // non-zero address.
158173 let mut addr2 = initial_addr;
159- let r = unsafe { ( bt. allocate_pages ) ( ty, mem_ty , count, & mut addr2) } . to_result ( ) ;
174+ let r = unsafe { ( bt. allocate_pages ) ( ty, memory_type , count, & mut addr2) } . to_result ( ) ;
160175
161176 // Free the original allocation (ignoring errors).
162177 let _unused = unsafe { ( bt. free_pages ) ( addr1, count) } ;
@@ -190,22 +205,31 @@ pub unsafe fn free_pages(ptr: NonNull<u8>, count: usize) -> Result {
190205 unsafe { ( bt. free_pages ) ( addr, count) } . to_result ( )
191206}
192207
193- /// Allocates from a memory pool. The pointer will be 8-byte aligned.
208+ /// Allocates a consecutive region of bytes using the UEFI allocator. The buffer
209+ /// will be 8-byte aligned.
210+ ///
211+ /// The caller is responsible to free the memory using [`free_pool`].
212+ ///
213+ /// # Arguments
214+ /// - `memory_type`: The [`MemoryType`] used to persist the allocation in the
215+ /// UEFI memory map. Typically, UEFI OS loaders should allocate memory of
216+ /// type [`MemoryType::LOADER_DATA`].
217+ ///- `size`: Amount of bytes to allocate.
194218///
195219/// # Errors
196220///
197221/// * [`Status::OUT_OF_RESOURCES`]: allocation failed.
198222/// * [`Status::INVALID_PARAMETER`]: `mem_ty` is [`MemoryType::PERSISTENT_MEMORY`],
199223/// [`MemoryType::UNACCEPTED`], or in the range [`MemoryType::MAX`]`..=0x6fff_ffff`.
200- pub fn allocate_pool ( mem_ty : MemoryType , size : usize ) -> Result < NonNull < u8 > > {
224+ pub fn allocate_pool ( memory_type : MemoryType , size : usize ) -> Result < NonNull < u8 > > {
201225 let bt = boot_services_raw_panicking ( ) ;
202226 let bt = unsafe { bt. as_ref ( ) } ;
203227
204228 let mut buffer = ptr:: null_mut ( ) ;
205- let ptr =
206- unsafe { ( bt . allocate_pool ) ( mem_ty , size , & mut buffer ) } . to_result_with_val ( || buffer) ?;
229+ let ptr = unsafe { ( bt . allocate_pool ) ( memory_type , size , & mut buffer ) }
230+ . to_result_with_val ( || buffer) ?;
207231
208- Ok ( NonNull :: new ( ptr) . expect ( "allocate_pool must not return a null pointer if successful" ) )
232+ NonNull :: new ( ptr) . ok_or ( Status :: OUT_OF_RESOURCES . into ( ) )
209233}
210234
211235/// Frees memory allocated by [`allocate_pool`].
0 commit comments