]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | Support routines for memory allocation routines based\r | |
3 | on boot services for Dxe phase drivers.\r | |
4 | \r | |
5 | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r | |
6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
7 | \r | |
8 | **/\r | |
9 | \r | |
10 | #include <Uefi.h>\r | |
11 | \r | |
12 | #include <Library/MemoryAllocationLib.h>\r | |
13 | #include <Library/UefiBootServicesTableLib.h>\r | |
14 | #include <Library/BaseMemoryLib.h>\r | |
15 | #include <Library/DebugLib.h>\r | |
16 | \r | |
17 | /**\r | |
18 | Allocates one or more 4KB pages of a certain memory type.\r | |
19 | \r | |
20 | Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated\r | |
21 | buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.\r | |
22 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
23 | \r | |
24 | @param MemoryType The type of memory to allocate.\r | |
25 | @param Pages The number of 4 KB pages to allocate.\r | |
26 | \r | |
27 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
28 | \r | |
29 | **/\r | |
30 | VOID *\r | |
31 | InternalAllocatePages (\r | |
32 | IN EFI_MEMORY_TYPE MemoryType,\r | |
33 | IN UINTN Pages\r | |
34 | )\r | |
35 | {\r | |
36 | EFI_STATUS Status;\r | |
37 | EFI_PHYSICAL_ADDRESS Memory;\r | |
38 | \r | |
39 | if (Pages == 0) {\r | |
40 | return NULL;\r | |
41 | }\r | |
42 | \r | |
43 | Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);\r | |
44 | if (EFI_ERROR (Status)) {\r | |
45 | return NULL;\r | |
46 | }\r | |
47 | \r | |
48 | return (VOID *)(UINTN)Memory;\r | |
49 | }\r | |
50 | \r | |
51 | /**\r | |
52 | Allocates one or more 4KB pages of type EfiBootServicesData.\r | |
53 | \r | |
54 | Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the\r | |
55 | allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r | |
56 | is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r | |
57 | returned.\r | |
58 | \r | |
59 | @param Pages The number of 4 KB pages to allocate.\r | |
60 | \r | |
61 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
62 | \r | |
63 | **/\r | |
64 | VOID *\r | |
65 | EFIAPI\r | |
66 | AllocatePages (\r | |
67 | IN UINTN Pages\r | |
68 | )\r | |
69 | {\r | |
70 | return InternalAllocatePages (EfiBootServicesData, Pages);\r | |
71 | }\r | |
72 | \r | |
73 | /**\r | |
74 | Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r | |
75 | \r | |
76 | Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r | |
77 | allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r | |
78 | is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r | |
79 | returned.\r | |
80 | \r | |
81 | @param Pages The number of 4 KB pages to allocate.\r | |
82 | \r | |
83 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
84 | \r | |
85 | **/\r | |
86 | VOID *\r | |
87 | EFIAPI\r | |
88 | AllocateRuntimePages (\r | |
89 | IN UINTN Pages\r | |
90 | )\r | |
91 | {\r | |
92 | return InternalAllocatePages (EfiRuntimeServicesData, Pages);\r | |
93 | }\r | |
94 | \r | |
95 | /**\r | |
96 | Allocates one or more 4KB pages of type EfiReservedMemoryType.\r | |
97 | \r | |
98 | Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the\r | |
99 | allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r | |
100 | is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r | |
101 | returned.\r | |
102 | \r | |
103 | @param Pages The number of 4 KB pages to allocate.\r | |
104 | \r | |
105 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
106 | \r | |
107 | **/\r | |
108 | VOID *\r | |
109 | EFIAPI\r | |
110 | AllocateReservedPages (\r | |
111 | IN UINTN Pages\r | |
112 | )\r | |
113 | {\r | |
114 | return InternalAllocatePages (EfiReservedMemoryType, Pages);\r | |
115 | }\r | |
116 | \r | |
117 | /**\r | |
118 | Frees one or more 4KB pages that were previously allocated with one of the page allocation\r | |
119 | functions in the Memory Allocation Library.\r | |
120 | \r | |
121 | Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r | |
122 | must have been allocated on a previous call to the page allocation services of the Memory\r | |
123 | Allocation Library. If it is not possible to free allocated pages, then this function will\r | |
124 | perform no actions.\r | |
125 | \r | |
126 | If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r | |
127 | then ASSERT().\r | |
128 | If Pages is zero, then ASSERT().\r | |
129 | \r | |
130 | @param Buffer The pointer to the buffer of pages to free.\r | |
131 | @param Pages The number of 4 KB pages to free.\r | |
132 | \r | |
133 | **/\r | |
134 | VOID\r | |
135 | EFIAPI\r | |
136 | FreePages (\r | |
137 | IN VOID *Buffer,\r | |
138 | IN UINTN Pages\r | |
139 | )\r | |
140 | {\r | |
141 | EFI_STATUS Status;\r | |
142 | \r | |
143 | ASSERT (Pages != 0);\r | |
144 | Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);\r | |
145 | ASSERT_EFI_ERROR (Status);\r | |
146 | }\r | |
147 | \r | |
148 | /**\r | |
149 | Allocates one or more 4KB pages of a certain memory type at a specified alignment.\r | |
150 | \r | |
151 | Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment\r | |
152 | specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.\r | |
153 | If there is not enough memory at the specified alignment remaining to satisfy the request, then\r | |
154 | NULL is returned.\r | |
155 | If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r | |
156 | If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r | |
157 | \r | |
158 | @param MemoryType The type of memory to allocate.\r | |
159 | @param Pages The number of 4 KB pages to allocate.\r | |
160 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
161 | If Alignment is zero, then byte alignment is used.\r | |
162 | \r | |
163 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
164 | \r | |
165 | **/\r | |
166 | VOID *\r | |
167 | InternalAllocateAlignedPages (\r | |
168 | IN EFI_MEMORY_TYPE MemoryType,\r | |
169 | IN UINTN Pages,\r | |
170 | IN UINTN Alignment\r | |
171 | )\r | |
172 | {\r | |
173 | EFI_STATUS Status;\r | |
174 | EFI_PHYSICAL_ADDRESS Memory;\r | |
175 | UINTN AlignedMemory;\r | |
176 | UINTN AlignmentMask;\r | |
177 | UINTN UnalignedPages;\r | |
178 | UINTN RealPages;\r | |
179 | \r | |
180 | //\r | |
181 | // Alignment must be a power of two or zero.\r | |
182 | //\r | |
183 | ASSERT ((Alignment & (Alignment - 1)) == 0);\r | |
184 | \r | |
185 | if (Pages == 0) {\r | |
186 | return NULL;\r | |
187 | }\r | |
188 | \r | |
189 | if (Alignment > EFI_PAGE_SIZE) {\r | |
190 | //\r | |
191 | // Calculate the total number of pages since alignment is larger than page size.\r | |
192 | //\r | |
193 | AlignmentMask = Alignment - 1;\r | |
194 | RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);\r | |
195 | //\r | |
196 | // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r | |
197 | //\r | |
198 | ASSERT (RealPages > Pages);\r | |
199 | \r | |
200 | Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);\r | |
201 | if (EFI_ERROR (Status)) {\r | |
202 | return NULL;\r | |
203 | }\r | |
204 | \r | |
205 | AlignedMemory = ((UINTN)Memory + AlignmentMask) & ~AlignmentMask;\r | |
206 | UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory);\r | |
207 | if (UnalignedPages > 0) {\r | |
208 | //\r | |
209 | // Free first unaligned page(s).\r | |
210 | //\r | |
211 | Status = gBS->FreePages (Memory, UnalignedPages);\r | |
212 | ASSERT_EFI_ERROR (Status);\r | |
213 | }\r | |
214 | \r | |
215 | Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);\r | |
216 | UnalignedPages = RealPages - Pages - UnalignedPages;\r | |
217 | if (UnalignedPages > 0) {\r | |
218 | //\r | |
219 | // Free last unaligned page(s).\r | |
220 | //\r | |
221 | Status = gBS->FreePages (Memory, UnalignedPages);\r | |
222 | ASSERT_EFI_ERROR (Status);\r | |
223 | }\r | |
224 | } else {\r | |
225 | //\r | |
226 | // Do not over-allocate pages in this case.\r | |
227 | //\r | |
228 | Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);\r | |
229 | if (EFI_ERROR (Status)) {\r | |
230 | return NULL;\r | |
231 | }\r | |
232 | \r | |
233 | AlignedMemory = (UINTN)Memory;\r | |
234 | }\r | |
235 | \r | |
236 | return (VOID *)AlignedMemory;\r | |
237 | }\r | |
238 | \r | |
239 | /**\r | |
240 | Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r | |
241 | \r | |
242 | Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r | |
243 | alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r | |
244 | returned. If there is not enough memory at the specified alignment remaining to satisfy the\r | |
245 | request, then NULL is returned.\r | |
246 | \r | |
247 | If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r | |
248 | If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r | |
249 | \r | |
250 | @param Pages The number of 4 KB pages to allocate.\r | |
251 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
252 | If Alignment is zero, then byte alignment is used.\r | |
253 | \r | |
254 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
255 | \r | |
256 | **/\r | |
257 | VOID *\r | |
258 | EFIAPI\r | |
259 | AllocateAlignedPages (\r | |
260 | IN UINTN Pages,\r | |
261 | IN UINTN Alignment\r | |
262 | )\r | |
263 | {\r | |
264 | return InternalAllocateAlignedPages (EfiBootServicesData, Pages, Alignment);\r | |
265 | }\r | |
266 | \r | |
267 | /**\r | |
268 | Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.\r | |
269 | \r | |
270 | Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an\r | |
271 | alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r | |
272 | returned. If there is not enough memory at the specified alignment remaining to satisfy the\r | |
273 | request, then NULL is returned.\r | |
274 | \r | |
275 | If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r | |
276 | If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r | |
277 | \r | |
278 | @param Pages The number of 4 KB pages to allocate.\r | |
279 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
280 | If Alignment is zero, then byte alignment is used.\r | |
281 | \r | |
282 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
283 | \r | |
284 | **/\r | |
285 | VOID *\r | |
286 | EFIAPI\r | |
287 | AllocateAlignedRuntimePages (\r | |
288 | IN UINTN Pages,\r | |
289 | IN UINTN Alignment\r | |
290 | )\r | |
291 | {\r | |
292 | return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r | |
293 | }\r | |
294 | \r | |
295 | /**\r | |
296 | Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.\r | |
297 | \r | |
298 | Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an\r | |
299 | alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r | |
300 | returned. If there is not enough memory at the specified alignment remaining to satisfy the\r | |
301 | request, then NULL is returned.\r | |
302 | \r | |
303 | If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r | |
304 | If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r | |
305 | \r | |
306 | @param Pages The number of 4 KB pages to allocate.\r | |
307 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
308 | If Alignment is zero, then byte alignment is used.\r | |
309 | \r | |
310 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
311 | \r | |
312 | **/\r | |
313 | VOID *\r | |
314 | EFIAPI\r | |
315 | AllocateAlignedReservedPages (\r | |
316 | IN UINTN Pages,\r | |
317 | IN UINTN Alignment\r | |
318 | )\r | |
319 | {\r | |
320 | return InternalAllocateAlignedPages (EfiReservedMemoryType, Pages, Alignment);\r | |
321 | }\r | |
322 | \r | |
323 | /**\r | |
324 | Frees one or more 4KB pages that were previously allocated with one of the aligned page\r | |
325 | allocation functions in the Memory Allocation Library.\r | |
326 | \r | |
327 | Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r | |
328 | must have been allocated on a previous call to the aligned page allocation services of the Memory\r | |
329 | Allocation Library. If it is not possible to free allocated pages, then this function will\r | |
330 | perform no actions.\r | |
331 | \r | |
332 | If Buffer was not allocated with an aligned page allocation function in the Memory Allocation\r | |
333 | Library, then ASSERT().\r | |
334 | If Pages is zero, then ASSERT().\r | |
335 | \r | |
336 | @param Buffer The pointer to the buffer of pages to free.\r | |
337 | @param Pages The number of 4 KB pages to free.\r | |
338 | \r | |
339 | **/\r | |
340 | VOID\r | |
341 | EFIAPI\r | |
342 | FreeAlignedPages (\r | |
343 | IN VOID *Buffer,\r | |
344 | IN UINTN Pages\r | |
345 | )\r | |
346 | {\r | |
347 | EFI_STATUS Status;\r | |
348 | \r | |
349 | ASSERT (Pages != 0);\r | |
350 | Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);\r | |
351 | ASSERT_EFI_ERROR (Status);\r | |
352 | }\r | |
353 | \r | |
354 | /**\r | |
355 | Allocates a buffer of a certain pool type.\r | |
356 | \r | |
357 | Allocates the number bytes specified by AllocationSize of a certain pool type and returns a\r | |
358 | pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r | |
359 | returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
360 | \r | |
361 | @param MemoryType The type of memory to allocate.\r | |
362 | @param AllocationSize The number of bytes to allocate.\r | |
363 | \r | |
364 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
365 | \r | |
366 | **/\r | |
367 | VOID *\r | |
368 | InternalAllocatePool (\r | |
369 | IN EFI_MEMORY_TYPE MemoryType,\r | |
370 | IN UINTN AllocationSize\r | |
371 | )\r | |
372 | {\r | |
373 | EFI_STATUS Status;\r | |
374 | VOID *Memory;\r | |
375 | \r | |
376 | Status = gBS->AllocatePool (MemoryType, AllocationSize, &Memory);\r | |
377 | if (EFI_ERROR (Status)) {\r | |
378 | Memory = NULL;\r | |
379 | }\r | |
380 | \r | |
381 | return Memory;\r | |
382 | }\r | |
383 | \r | |
384 | /**\r | |
385 | Allocates a buffer of type EfiBootServicesData.\r | |
386 | \r | |
387 | Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r | |
388 | pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r | |
389 | returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
390 | \r | |
391 | @param AllocationSize The number of bytes to allocate.\r | |
392 | \r | |
393 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
394 | \r | |
395 | **/\r | |
396 | VOID *\r | |
397 | EFIAPI\r | |
398 | AllocatePool (\r | |
399 | IN UINTN AllocationSize\r | |
400 | )\r | |
401 | {\r | |
402 | return InternalAllocatePool (EfiBootServicesData, AllocationSize);\r | |
403 | }\r | |
404 | \r | |
405 | /**\r | |
406 | Allocates a buffer of type EfiRuntimeServicesData.\r | |
407 | \r | |
408 | Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns\r | |
409 | a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r | |
410 | returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
411 | \r | |
412 | @param AllocationSize The number of bytes to allocate.\r | |
413 | \r | |
414 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
415 | \r | |
416 | **/\r | |
417 | VOID *\r | |
418 | EFIAPI\r | |
419 | AllocateRuntimePool (\r | |
420 | IN UINTN AllocationSize\r | |
421 | )\r | |
422 | {\r | |
423 | return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r | |
424 | }\r | |
425 | \r | |
426 | /**\r | |
427 | Allocates a buffer of type EfiReservedMemoryType.\r | |
428 | \r | |
429 | Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns\r | |
430 | a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r | |
431 | returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
432 | \r | |
433 | @param AllocationSize The number of bytes to allocate.\r | |
434 | \r | |
435 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
436 | \r | |
437 | **/\r | |
438 | VOID *\r | |
439 | EFIAPI\r | |
440 | AllocateReservedPool (\r | |
441 | IN UINTN AllocationSize\r | |
442 | )\r | |
443 | {\r | |
444 | return InternalAllocatePool (EfiReservedMemoryType, AllocationSize);\r | |
445 | }\r | |
446 | \r | |
447 | /**\r | |
448 | Allocates and zeros a buffer of a certain pool type.\r | |
449 | \r | |
450 | Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer\r | |
451 | with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid\r | |
452 | buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,\r | |
453 | then NULL is returned.\r | |
454 | \r | |
455 | @param PoolType The type of memory to allocate.\r | |
456 | @param AllocationSize The number of bytes to allocate and zero.\r | |
457 | \r | |
458 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
459 | \r | |
460 | **/\r | |
461 | VOID *\r | |
462 | InternalAllocateZeroPool (\r | |
463 | IN EFI_MEMORY_TYPE PoolType,\r | |
464 | IN UINTN AllocationSize\r | |
465 | )\r | |
466 | {\r | |
467 | VOID *Memory;\r | |
468 | \r | |
469 | Memory = InternalAllocatePool (PoolType, AllocationSize);\r | |
470 | if (Memory != NULL) {\r | |
471 | Memory = ZeroMem (Memory, AllocationSize);\r | |
472 | }\r | |
473 | \r | |
474 | return Memory;\r | |
475 | }\r | |
476 | \r | |
477 | /**\r | |
478 | Allocates and zeros a buffer of type EfiBootServicesData.\r | |
479 | \r | |
480 | Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r | |
481 | buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r | |
482 | valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r | |
483 | request, then NULL is returned.\r | |
484 | \r | |
485 | @param AllocationSize The number of bytes to allocate and zero.\r | |
486 | \r | |
487 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
488 | \r | |
489 | **/\r | |
490 | VOID *\r | |
491 | EFIAPI\r | |
492 | AllocateZeroPool (\r | |
493 | IN UINTN AllocationSize\r | |
494 | )\r | |
495 | {\r | |
496 | return InternalAllocateZeroPool (EfiBootServicesData, AllocationSize);\r | |
497 | }\r | |
498 | \r | |
499 | /**\r | |
500 | Allocates and zeros a buffer of type EfiRuntimeServicesData.\r | |
501 | \r | |
502 | Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the\r | |
503 | buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r | |
504 | valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r | |
505 | request, then NULL is returned.\r | |
506 | \r | |
507 | @param AllocationSize The number of bytes to allocate and zero.\r | |
508 | \r | |
509 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
510 | \r | |
511 | **/\r | |
512 | VOID *\r | |
513 | EFIAPI\r | |
514 | AllocateRuntimeZeroPool (\r | |
515 | IN UINTN AllocationSize\r | |
516 | )\r | |
517 | {\r | |
518 | return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r | |
519 | }\r | |
520 | \r | |
521 | /**\r | |
522 | Allocates and zeros a buffer of type EfiReservedMemoryType.\r | |
523 | \r | |
524 | Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the\r | |
525 | buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r | |
526 | valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r | |
527 | request, then NULL is returned.\r | |
528 | \r | |
529 | @param AllocationSize The number of bytes to allocate and zero.\r | |
530 | \r | |
531 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
532 | \r | |
533 | **/\r | |
534 | VOID *\r | |
535 | EFIAPI\r | |
536 | AllocateReservedZeroPool (\r | |
537 | IN UINTN AllocationSize\r | |
538 | )\r | |
539 | {\r | |
540 | return InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize);\r | |
541 | }\r | |
542 | \r | |
543 | /**\r | |
544 | Copies a buffer to an allocated buffer of a certain pool type.\r | |
545 | \r | |
546 | Allocates the number bytes specified by AllocationSize of a certain pool type, copies\r | |
547 | AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r | |
548 | allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r | |
549 | is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
550 | If Buffer is NULL, then ASSERT().\r | |
551 | If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
552 | \r | |
553 | @param PoolType The type of pool to allocate.\r | |
554 | @param AllocationSize The number of bytes to allocate and zero.\r | |
555 | @param Buffer The buffer to copy to the allocated buffer.\r | |
556 | \r | |
557 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
558 | \r | |
559 | **/\r | |
560 | VOID *\r | |
561 | InternalAllocateCopyPool (\r | |
562 | IN EFI_MEMORY_TYPE PoolType,\r | |
563 | IN UINTN AllocationSize,\r | |
564 | IN CONST VOID *Buffer\r | |
565 | )\r | |
566 | {\r | |
567 | VOID *Memory;\r | |
568 | \r | |
569 | ASSERT (Buffer != NULL);\r | |
570 | ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN)Buffer + 1));\r | |
571 | \r | |
572 | Memory = InternalAllocatePool (PoolType, AllocationSize);\r | |
573 | if (Memory != NULL) {\r | |
574 | Memory = CopyMem (Memory, Buffer, AllocationSize);\r | |
575 | }\r | |
576 | \r | |
577 | return Memory;\r | |
578 | }\r | |
579 | \r | |
580 | /**\r | |
581 | Copies a buffer to an allocated buffer of type EfiBootServicesData.\r | |
582 | \r | |
583 | Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r | |
584 | AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r | |
585 | allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r | |
586 | is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
587 | \r | |
588 | If Buffer is NULL, then ASSERT().\r | |
589 | If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
590 | \r | |
591 | @param AllocationSize The number of bytes to allocate and zero.\r | |
592 | @param Buffer The buffer to copy to the allocated buffer.\r | |
593 | \r | |
594 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
595 | \r | |
596 | **/\r | |
597 | VOID *\r | |
598 | EFIAPI\r | |
599 | AllocateCopyPool (\r | |
600 | IN UINTN AllocationSize,\r | |
601 | IN CONST VOID *Buffer\r | |
602 | )\r | |
603 | {\r | |
604 | return InternalAllocateCopyPool (EfiBootServicesData, AllocationSize, Buffer);\r | |
605 | }\r | |
606 | \r | |
607 | /**\r | |
608 | Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r | |
609 | \r | |
610 | Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies\r | |
611 | AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r | |
612 | allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r | |
613 | is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
614 | \r | |
615 | If Buffer is NULL, then ASSERT().\r | |
616 | If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
617 | \r | |
618 | @param AllocationSize The number of bytes to allocate and zero.\r | |
619 | @param Buffer The buffer to copy to the allocated buffer.\r | |
620 | \r | |
621 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
622 | \r | |
623 | **/\r | |
624 | VOID *\r | |
625 | EFIAPI\r | |
626 | AllocateRuntimeCopyPool (\r | |
627 | IN UINTN AllocationSize,\r | |
628 | IN CONST VOID *Buffer\r | |
629 | )\r | |
630 | {\r | |
631 | return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r | |
632 | }\r | |
633 | \r | |
634 | /**\r | |
635 | Copies a buffer to an allocated buffer of type EfiReservedMemoryType.\r | |
636 | \r | |
637 | Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies\r | |
638 | AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r | |
639 | allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r | |
640 | is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
641 | \r | |
642 | If Buffer is NULL, then ASSERT().\r | |
643 | If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
644 | \r | |
645 | @param AllocationSize The number of bytes to allocate and zero.\r | |
646 | @param Buffer The buffer to copy to the allocated buffer.\r | |
647 | \r | |
648 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
649 | \r | |
650 | **/\r | |
651 | VOID *\r | |
652 | EFIAPI\r | |
653 | AllocateReservedCopyPool (\r | |
654 | IN UINTN AllocationSize,\r | |
655 | IN CONST VOID *Buffer\r | |
656 | )\r | |
657 | {\r | |
658 | return InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, Buffer);\r | |
659 | }\r | |
660 | \r | |
661 | /**\r | |
662 | Reallocates a buffer of a specified memory type.\r | |
663 | \r | |
664 | Allocates and zeros the number bytes specified by NewSize from memory of the type\r | |
665 | specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and\r | |
666 | NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r | |
667 | OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r | |
668 | If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r | |
669 | enough memory remaining to satisfy the request, then NULL is returned.\r | |
670 | \r | |
671 | If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r | |
672 | is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r | |
673 | \r | |
674 | @param PoolType The type of pool to allocate.\r | |
675 | @param OldSize The size, in bytes, of OldBuffer.\r | |
676 | @param NewSize The size, in bytes, of the buffer to reallocate.\r | |
677 | @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r | |
678 | parameter that may be NULL.\r | |
679 | \r | |
680 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
681 | \r | |
682 | **/\r | |
683 | VOID *\r | |
684 | InternalReallocatePool (\r | |
685 | IN EFI_MEMORY_TYPE PoolType,\r | |
686 | IN UINTN OldSize,\r | |
687 | IN UINTN NewSize,\r | |
688 | IN VOID *OldBuffer OPTIONAL\r | |
689 | )\r | |
690 | {\r | |
691 | VOID *NewBuffer;\r | |
692 | \r | |
693 | NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);\r | |
694 | if ((NewBuffer != NULL) && (OldBuffer != NULL)) {\r | |
695 | CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));\r | |
696 | FreePool (OldBuffer);\r | |
697 | }\r | |
698 | \r | |
699 | return NewBuffer;\r | |
700 | }\r | |
701 | \r | |
702 | /**\r | |
703 | Reallocates a buffer of type EfiBootServicesData.\r | |
704 | \r | |
705 | Allocates and zeros the number bytes specified by NewSize from memory of type\r | |
706 | EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and\r | |
707 | NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r | |
708 | OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r | |
709 | If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r | |
710 | enough memory remaining to satisfy the request, then NULL is returned.\r | |
711 | \r | |
712 | If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r | |
713 | is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r | |
714 | \r | |
715 | @param OldSize The size, in bytes, of OldBuffer.\r | |
716 | @param NewSize The size, in bytes, of the buffer to reallocate.\r | |
717 | @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r | |
718 | parameter that may be NULL.\r | |
719 | \r | |
720 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
721 | \r | |
722 | **/\r | |
723 | VOID *\r | |
724 | EFIAPI\r | |
725 | ReallocatePool (\r | |
726 | IN UINTN OldSize,\r | |
727 | IN UINTN NewSize,\r | |
728 | IN VOID *OldBuffer OPTIONAL\r | |
729 | )\r | |
730 | {\r | |
731 | return InternalReallocatePool (EfiBootServicesData, OldSize, NewSize, OldBuffer);\r | |
732 | }\r | |
733 | \r | |
734 | /**\r | |
735 | Reallocates a buffer of type EfiRuntimeServicesData.\r | |
736 | \r | |
737 | Allocates and zeros the number bytes specified by NewSize from memory of type\r | |
738 | EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and\r | |
739 | NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r | |
740 | OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r | |
741 | If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r | |
742 | enough memory remaining to satisfy the request, then NULL is returned.\r | |
743 | \r | |
744 | If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r | |
745 | is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r | |
746 | \r | |
747 | @param OldSize The size, in bytes, of OldBuffer.\r | |
748 | @param NewSize The size, in bytes, of the buffer to reallocate.\r | |
749 | @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r | |
750 | parameter that may be NULL.\r | |
751 | \r | |
752 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
753 | \r | |
754 | **/\r | |
755 | VOID *\r | |
756 | EFIAPI\r | |
757 | ReallocateRuntimePool (\r | |
758 | IN UINTN OldSize,\r | |
759 | IN UINTN NewSize,\r | |
760 | IN VOID *OldBuffer OPTIONAL\r | |
761 | )\r | |
762 | {\r | |
763 | return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r | |
764 | }\r | |
765 | \r | |
766 | /**\r | |
767 | Reallocates a buffer of type EfiReservedMemoryType.\r | |
768 | \r | |
769 | Allocates and zeros the number bytes specified by NewSize from memory of type\r | |
770 | EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and\r | |
771 | NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r | |
772 | OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r | |
773 | If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r | |
774 | enough memory remaining to satisfy the request, then NULL is returned.\r | |
775 | \r | |
776 | If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r | |
777 | is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r | |
778 | \r | |
779 | @param OldSize The size, in bytes, of OldBuffer.\r | |
780 | @param NewSize The size, in bytes, of the buffer to reallocate.\r | |
781 | @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r | |
782 | parameter that may be NULL.\r | |
783 | \r | |
784 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
785 | \r | |
786 | **/\r | |
787 | VOID *\r | |
788 | EFIAPI\r | |
789 | ReallocateReservedPool (\r | |
790 | IN UINTN OldSize,\r | |
791 | IN UINTN NewSize,\r | |
792 | IN VOID *OldBuffer OPTIONAL\r | |
793 | )\r | |
794 | {\r | |
795 | return InternalReallocatePool (EfiReservedMemoryType, OldSize, NewSize, OldBuffer);\r | |
796 | }\r | |
797 | \r | |
798 | /**\r | |
799 | Frees a buffer that was previously allocated with one of the pool allocation functions in the\r | |
800 | Memory Allocation Library.\r | |
801 | \r | |
802 | Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r | |
803 | pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r | |
804 | resources, then this function will perform no actions.\r | |
805 | \r | |
806 | If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r | |
807 | then ASSERT().\r | |
808 | \r | |
809 | @param Buffer The pointer to the buffer to free.\r | |
810 | \r | |
811 | **/\r | |
812 | VOID\r | |
813 | EFIAPI\r | |
814 | FreePool (\r | |
815 | IN VOID *Buffer\r | |
816 | )\r | |
817 | {\r | |
818 | EFI_STATUS Status;\r | |
819 | \r | |
820 | Status = gBS->FreePool (Buffer);\r | |
821 | ASSERT_EFI_ERROR (Status);\r | |
822 | }\r |