@@ -121,28 +121,43 @@ pub unsafe fn raise_tpl(tpl: Tpl) -> TplGuard {
121
121
}
122
122
}
123
123
124
- /// Allocates memory pages from the system .
124
+ /// Allocates a consecutive set of memory pages using the UEFI allocator .
125
125
///
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.
127
138
///
128
139
/// # Errors
129
140
///
130
141
/// * [`Status::OUT_OF_RESOURCES`]: allocation failed.
131
142
/// * [`Status::INVALID_PARAMETER`]: `mem_ty` is [`MemoryType::PERSISTENT_MEMORY`],
132
143
/// [`MemoryType::UNACCEPTED`], or in the range [`MemoryType::MAX`]`..=0x6fff_ffff`.
133
144
/// * [`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 > > {
135
150
let bt = boot_services_raw_panicking ( ) ;
136
151
let bt = unsafe { bt. as_ref ( ) } ;
137
152
138
- let ( ty, initial_addr) = match ty {
153
+ let ( ty, initial_addr) = match allocation_type {
139
154
AllocateType :: AnyPages => ( 0 , 0 ) ,
140
155
AllocateType :: MaxAddress ( addr) => ( 1 , addr) ,
141
156
AllocateType :: Address ( addr) => ( 2 , addr) ,
142
157
} ;
143
158
144
159
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 ( ) ?;
146
161
147
162
// The UEFI spec allows `allocate_pages` to return a valid allocation at
148
163
// 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
156
171
// not yet been freed, so if this allocation succeeds it should be at a
157
172
// non-zero address.
158
173
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 ( ) ;
160
175
161
176
// Free the original allocation (ignoring errors).
162
177
let _unused = unsafe { ( bt. free_pages ) ( addr1, count) } ;
@@ -190,22 +205,31 @@ pub unsafe fn free_pages(ptr: NonNull<u8>, count: usize) -> Result {
190
205
unsafe { ( bt. free_pages ) ( addr, count) } . to_result ( )
191
206
}
192
207
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.
194
218
///
195
219
/// # Errors
196
220
///
197
221
/// * [`Status::OUT_OF_RESOURCES`]: allocation failed.
198
222
/// * [`Status::INVALID_PARAMETER`]: `mem_ty` is [`MemoryType::PERSISTENT_MEMORY`],
199
223
/// [`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 > > {
201
225
let bt = boot_services_raw_panicking ( ) ;
202
226
let bt = unsafe { bt. as_ref ( ) } ;
203
227
204
228
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) ?;
207
231
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 ( ) )
209
233
}
210
234
211
235
/// Frees memory allocated by [`allocate_pool`].
0 commit comments