@@ -231,22 +231,67 @@ pub unsafe fn free_pages(ptr: NonNull<u8>, count: usize) -> Result {
231
231
unsafe { ( bt. free_pages ) ( addr, count) } . to_result ( )
232
232
}
233
233
234
- /// Allocates from a memory pool. The pointer will be 8-byte aligned.
234
+ /// Allocates a consecutive region of bytes using the UEFI allocator. The buffer
235
+ /// will be 8-byte aligned.
236
+ ///
237
+ /// The caller is responsible to free the memory using [`free_pool`].
238
+ ///
239
+ /// # Arguments
240
+ /// - `memory_type`: The [`MemoryType`] used to persist the allocation in the
241
+ /// UEFI memory map. Typically, UEFI OS loaders should allocate memory of
242
+ /// type [`MemoryType::LOADER_DATA`].
243
+ ///- `size`: Amount of bytes to allocate.
244
+ ///
245
+ /// # Example
246
+ ///```rust,no_run
247
+ /// use uefi::boot::{self, AllocateType};
248
+ /// use uefi_raw::table::boot::MemoryType;
249
+ ///
250
+ /// let mut ptr = boot::allocate_pool(
251
+ /// MemoryType::LOADER_DATA,
252
+ /// 42
253
+ /// ).unwrap();
254
+ ///
255
+ /// // ⚠️ Creating the reference is safe, but reading the uninitialized memory
256
+ /// // causes Undefined Behavior (UB)! Please make sure to initialize the memory
257
+ /// // first by:
258
+ /// // - using `core::ptr::write`,
259
+ /// // - directly writing to slice indices,
260
+ /// // - zeroing the memory,
261
+ /// // - using `.copy_from_slice()`,
262
+ /// // - or a similar operation.
263
+ /// let buffer: &mut [u8] = unsafe { ptr.as_mut() };
264
+ /// // Now initialize the content of the buffer, cast it, etc.
265
+ /// // Please follow Rust guidelines on safety and UB! ⚠️
266
+ ///
267
+ /// // free the allocation
268
+ /// unsafe { boot::free_pool(ptr.cast()) }.unwrap();
269
+ /// ```
270
+ ///
271
+ /// # Safety
272
+ /// Using this function is safe but reading on initialized memory is not.
273
+ /// Please look into the example code.
274
+ ///
235
275
///
236
276
/// # Errors
237
277
///
238
278
/// * [`Status::OUT_OF_RESOURCES`]: allocation failed.
239
279
/// * [`Status::INVALID_PARAMETER`]: `mem_ty` is [`MemoryType::PERSISTENT_MEMORY`],
240
280
/// [`MemoryType::UNACCEPTED`], or in the range [`MemoryType::MAX`]`..=0x6fff_ffff`.
241
- pub fn allocate_pool ( mem_ty : MemoryType , size : usize ) -> Result < NonNull < u8 > > {
281
+ pub fn allocate_pool ( memory_type : MemoryType , size : usize ) -> Result < NonNull < [ u8 ] > > {
242
282
let bt = boot_services_raw_panicking ( ) ;
243
283
let bt = unsafe { bt. as_ref ( ) } ;
244
284
245
285
let mut buffer = ptr:: null_mut ( ) ;
246
- let ptr =
247
- unsafe { ( bt . allocate_pool ) ( mem_ty , size , & mut buffer ) } . to_result_with_val ( || buffer) ?;
286
+ let ptr = unsafe { ( bt . allocate_pool ) ( memory_type , size , & mut buffer ) }
287
+ . to_result_with_val ( || buffer) ?;
248
288
249
- Ok ( NonNull :: new ( ptr) . expect ( "allocate_pool must not return a null pointer if successful" ) )
289
+ if let Some ( ptr) = NonNull :: new ( ptr) {
290
+ let slice = NonNull :: slice_from_raw_parts ( ptr, size) ;
291
+ Ok ( slice)
292
+ } else {
293
+ Err ( Status :: OUT_OF_RESOURCES . into ( ) )
294
+ }
250
295
}
251
296
252
297
/// Frees memory allocated by [`allocate_pool`].
0 commit comments