@@ -218,22 +218,53 @@ pub unsafe fn free_pages(ptr: NonNull<u8>, count: usize) -> Result {
218
218
unsafe { ( bt. free_pages ) ( addr, count) } . to_result ( )
219
219
}
220
220
221
- /// Allocates from a memory pool. The pointer will be 8-byte aligned.
221
+ /// Allocates a consecutive region of bytes using the UEFI allocator. The buffer
222
+ /// will be 8-byte aligned.
223
+ ///
224
+ /// The caller is responsible to free the memory using [`free_pool`].
225
+ ///
226
+ /// # Arguments
227
+ /// - `memory_type`: The [`MemoryType`] used to persist the allocation in the
228
+ /// UEFI memory map. Typically, UEFI OS loaders should allocate memory of
229
+ /// type [`MemoryType::LOADER_DATA`].
230
+ ///- `size`: Amount of bytes to allocate.
231
+ ///
232
+ /// # Example
233
+ ///```rust,no_run
234
+ /// use uefi::boot::{self, AllocateType};
235
+ /// use uefi_raw::table::boot::MemoryType;
236
+ ///
237
+ /// let mut ptr = boot::allocate_pool(
238
+ /// MemoryType::LOADER_DATA,
239
+ /// 42
240
+ /// ).unwrap();
241
+ ///
242
+ /// let buffer: &mut [u8] = unsafe { ptr.as_mut() };
243
+ /// // now do something with your buffer
244
+ ///
245
+ /// // free the allocation
246
+ /// unsafe { boot::free_pool(ptr.cast()) }.unwrap();
247
+ /// ```
222
248
///
223
249
/// # Errors
224
250
///
225
251
/// * [`Status::OUT_OF_RESOURCES`]: allocation failed.
226
252
/// * [`Status::INVALID_PARAMETER`]: `mem_ty` is [`MemoryType::PERSISTENT_MEMORY`],
227
253
/// [`MemoryType::UNACCEPTED`], or in the range [`MemoryType::MAX`]`..=0x6fff_ffff`.
228
- pub fn allocate_pool ( mem_ty : MemoryType , size : usize ) -> Result < NonNull < u8 > > {
254
+ pub fn allocate_pool ( memory_type : MemoryType , size : usize ) -> Result < NonNull < [ u8 ] > > {
229
255
let bt = boot_services_raw_panicking ( ) ;
230
256
let bt = unsafe { bt. as_ref ( ) } ;
231
257
232
258
let mut buffer = ptr:: null_mut ( ) ;
233
- let ptr =
234
- unsafe { ( bt . allocate_pool ) ( mem_ty , size , & mut buffer ) } . to_result_with_val ( || buffer) ?;
259
+ let ptr = unsafe { ( bt . allocate_pool ) ( memory_type , size , & mut buffer ) }
260
+ . to_result_with_val ( || buffer) ?;
235
261
236
- Ok ( NonNull :: new ( ptr) . expect ( "allocate_pool must not return a null pointer if successful" ) )
262
+ if let Some ( ptr) = NonNull :: new ( ptr) {
263
+ let slice = NonNull :: slice_from_raw_parts ( ptr, size) ;
264
+ Ok ( slice)
265
+ } else {
266
+ Err ( Status :: OUT_OF_RESOURCES . into ( ) )
267
+ }
237
268
}
238
269
239
270
/// Frees memory allocated by [`allocate_pool`].
0 commit comments