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