]> git.proxmox.com Git - mirror_edk2.git/blame - StandaloneMmPkg/Library/StandaloneMmMemoryAllocationLib/StandaloneMmMemoryAllocationLib.c
StandaloneMmPkg: add MM_STANDALONE MemoryAllocationLib implementation
[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
7 This program and the accompanying materials\r
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include <PiMm.h>\r
18\r
19#include <Library/BaseMemoryLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/MemoryAllocationLib.h>\r
22#include <Library/MmServicesTableLib.h>\r
23\r
24/**\r
25 Allocates one or more 4KB pages of a certain memory type.\r
26\r
27 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated\r
28 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.\r
29 If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
30\r
31 @param MemoryType The type of memory to allocate.\r
32 @param Pages The number of 4 KB pages to allocate.\r
33\r
34 @return A pointer to the allocated buffer or NULL if allocation fails.\r
35\r
36**/\r
37VOID *\r
38InternalAllocatePages (\r
39 IN EFI_MEMORY_TYPE MemoryType,\r
40 IN UINTN Pages\r
41 )\r
42{\r
43 EFI_STATUS Status;\r
44 EFI_PHYSICAL_ADDRESS Memory;\r
45\r
46 if (Pages == 0) {\r
47 return NULL;\r
48 }\r
49\r
50 Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);\r
51 if (EFI_ERROR (Status)) {\r
52 return NULL;\r
53 }\r
54 return (VOID *)(UINTN)Memory;\r
55}\r
56\r
57/**\r
58 Allocates one or more 4KB pages of type EfiBootServicesData.\r
59\r
60 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the\r
61 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
62 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
63 returned.\r
64\r
65 @param Pages The number of 4 KB pages to allocate.\r
66\r
67 @return A pointer to the allocated buffer or NULL if allocation fails.\r
68\r
69**/\r
70VOID *\r
71EFIAPI\r
72AllocatePages (\r
73 IN UINTN Pages\r
74 )\r
75{\r
76 return InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
77}\r
78\r
79/**\r
80 Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r
81\r
82 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
83 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
84 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
85 returned.\r
86\r
87 @param Pages The number of 4 KB pages to allocate.\r
88\r
89 @return A pointer to the allocated buffer or NULL if allocation fails.\r
90\r
91**/\r
92VOID *\r
93EFIAPI\r
94AllocateRuntimePages (\r
95 IN UINTN Pages\r
96 )\r
97{\r
98 return InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
99}\r
100\r
101/**\r
102 Allocates one or more 4KB pages of type EfiReservedMemoryType.\r
103\r
104 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the\r
105 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
106 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
107 returned.\r
108\r
109 @param Pages The number of 4 KB pages to allocate.\r
110\r
111 @return A pointer to the allocated buffer or NULL if allocation fails.\r
112\r
113**/\r
114VOID *\r
115EFIAPI\r
116AllocateReservedPages (\r
117 IN UINTN Pages\r
118 )\r
119{\r
120 return NULL;\r
121}\r
122\r
123/**\r
124 Frees one or more 4KB pages that were previously allocated with one of the page allocation\r
125 functions in the Memory Allocation Library.\r
126\r
127 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
128 must have been allocated on a previous call to the page allocation services of the Memory\r
129 Allocation Library. If it is not possible to free allocated pages, then this function will\r
130 perform no actions.\r
131\r
132 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r
133 then ASSERT().\r
134 If Pages is zero, then ASSERT().\r
135\r
136 @param Buffer Pointer to the buffer of pages to free.\r
137 @param Pages The number of 4 KB pages to free.\r
138\r
139**/\r
140VOID\r
141EFIAPI\r
142FreePages (\r
143 IN VOID *Buffer,\r
144 IN UINTN Pages\r
145 )\r
146{\r
147 EFI_STATUS Status;\r
148\r
149 ASSERT (Pages != 0);\r
150 Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN)Buffer, Pages);\r
151 ASSERT_EFI_ERROR (Status);\r
152}\r
153\r
154/**\r
155 Allocates one or more 4KB pages of a certain memory type at a specified alignment.\r
156\r
157 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment\r
158 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.\r
159 If there is not enough memory at the specified alignment remaining to satisfy the request, then\r
160 NULL is returned.\r
161 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
162 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
163\r
164 @param MemoryType The type of memory to allocate.\r
165 @param Pages The number of 4 KB pages to allocate.\r
166 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
167 If Alignment is zero, then byte alignment is used.\r
168\r
169 @return A pointer to the allocated buffer or NULL if allocation fails.\r
170\r
171**/\r
172VOID *\r
173InternalAllocateAlignedPages (\r
174 IN EFI_MEMORY_TYPE MemoryType,\r
175 IN UINTN Pages,\r
176 IN UINTN Alignment\r
177 )\r
178{\r
179 EFI_STATUS Status;\r
180 EFI_PHYSICAL_ADDRESS Memory;\r
181 UINTN AlignedMemory;\r
182 UINTN AlignmentMask;\r
183 UINTN UnalignedPages;\r
184 UINTN RealPages;\r
185\r
186 //\r
187 // Alignment must be a power of two or zero.\r
188 //\r
189 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
190\r
191 if (Pages == 0) {\r
192 return NULL;\r
193 }\r
194 if (Alignment > EFI_PAGE_SIZE) {\r
195 //\r
196 // Calculate the total number of pages since alignment is larger than page size.\r
197 //\r
198 AlignmentMask = Alignment - 1;\r
199 RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
200 //\r
201 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
202 //\r
203 ASSERT (RealPages > Pages);\r
204\r
205 Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);\r
206 if (EFI_ERROR (Status)) {\r
207 return NULL;\r
208 }\r
209 AlignedMemory = ((UINTN)Memory + AlignmentMask) & ~AlignmentMask;\r
210 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);\r
211 if (UnalignedPages > 0) {\r
212 //\r
213 // Free first unaligned page(s).\r
214 //\r
215 Status = gMmst->MmFreePages (Memory, UnalignedPages);\r
216 ASSERT_EFI_ERROR (Status);\r
217 }\r
218 Memory = (EFI_PHYSICAL_ADDRESS)(AlignedMemory + EFI_PAGES_TO_SIZE (Pages));\r
219 UnalignedPages = RealPages - Pages - UnalignedPages;\r
220 if (UnalignedPages > 0) {\r
221 //\r
222 // Free last unaligned page(s).\r
223 //\r
224 Status = gMmst->MmFreePages (Memory, UnalignedPages);\r
225 ASSERT_EFI_ERROR (Status);\r
226 }\r
227 } else {\r
228 //\r
229 // Do not over-allocate pages in this case.\r
230 //\r
231 Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);\r
232 if (EFI_ERROR (Status)) {\r
233 return NULL;\r
234 }\r
235 AlignedMemory = (UINTN) Memory;\r
236 }\r
237 return (VOID *) AlignedMemory;\r
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
383 return Memory;\r
384}\r
385\r
386/**\r
387 Allocates a buffer of type EfiBootServicesData.\r
388\r
389 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
390 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
391 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
392\r
393 @param AllocationSize The number of bytes to allocate.\r
394\r
395 @return A pointer to the allocated buffer or NULL if allocation fails.\r
396\r
397**/\r
398VOID *\r
399EFIAPI\r
400AllocatePool (\r
401 IN UINTN AllocationSize\r
402 )\r
403{\r
404 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
405}\r
406\r
407/**\r
408 Allocates a buffer of type EfiRuntimeServicesData.\r
409\r
410 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns\r
411 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
412 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
413\r
414 @param AllocationSize The number of bytes to allocate.\r
415\r
416 @return A pointer to the allocated buffer or NULL if allocation fails.\r
417\r
418**/\r
419VOID *\r
420EFIAPI\r
421AllocateRuntimePool (\r
422 IN UINTN AllocationSize\r
423 )\r
424{\r
425 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
426}\r
427\r
428/**\r
429 Allocates a buffer of type EfiReservedMemoryType.\r
430\r
431 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns\r
432 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
433 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
434\r
435 @param AllocationSize The number of bytes to allocate.\r
436\r
437 @return A pointer to the allocated buffer or NULL if allocation fails.\r
438\r
439**/\r
440VOID *\r
441EFIAPI\r
442AllocateReservedPool (\r
443 IN UINTN AllocationSize\r
444 )\r
445{\r
446 return NULL;\r
447}\r
448\r
449/**\r
450 Allocates and zeros a buffer of a certain pool type.\r
451\r
452 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer\r
453 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid\r
454 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,\r
455 then NULL is returned.\r
456\r
457 @param PoolType The type of memory to allocate.\r
458 @param AllocationSize The number of bytes to allocate and zero.\r
459\r
460 @return A pointer to the allocated buffer or NULL if allocation fails.\r
461\r
462**/\r
463VOID *\r
464InternalAllocateZeroPool (\r
465 IN EFI_MEMORY_TYPE PoolType,\r
466 IN UINTN AllocationSize\r
467 )\r
468{\r
469 VOID *Memory;\r
470\r
471 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
472 if (Memory != NULL) {\r
473 Memory = ZeroMem (Memory, AllocationSize);\r
474 }\r
475 return Memory;\r
476}\r
477\r
478/**\r
479 Allocates and zeros a buffer of type EfiBootServicesData.\r
480\r
481 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
482 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
483 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
484 request, then NULL is returned.\r
485\r
486 @param AllocationSize The number of bytes to allocate and zero.\r
487\r
488 @return A pointer to the allocated buffer or NULL if allocation fails.\r
489\r
490**/\r
491VOID *\r
492EFIAPI\r
493AllocateZeroPool (\r
494 IN UINTN AllocationSize\r
495 )\r
496{\r
497 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
498}\r
499\r
500/**\r
501 Allocates and zeros a buffer of type EfiRuntimeServicesData.\r
502\r
503 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the\r
504 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
505 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
506 request, then NULL is returned.\r
507\r
508 @param AllocationSize The number of bytes to allocate and zero.\r
509\r
510 @return A pointer to the allocated buffer or NULL if allocation fails.\r
511\r
512**/\r
513VOID *\r
514EFIAPI\r
515AllocateRuntimeZeroPool (\r
516 IN UINTN AllocationSize\r
517 )\r
518{\r
519 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
520}\r
521\r
522/**\r
523 Allocates and zeros a buffer of type EfiReservedMemoryType.\r
524\r
525 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the\r
526 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
527 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
528 request, then NULL is returned.\r
529\r
530 @param AllocationSize The number of bytes to allocate and zero.\r
531\r
532 @return A pointer to the allocated buffer or NULL if allocation fails.\r
533\r
534**/\r
535VOID *\r
536EFIAPI\r
537AllocateReservedZeroPool (\r
538 IN UINTN AllocationSize\r
539 )\r
540{\r
541 return NULL;\r
542}\r
543\r
544/**\r
545 Copies a buffer to an allocated buffer of a certain pool type.\r
546\r
547 Allocates the number bytes specified by AllocationSize of a certain pool type, copies\r
548 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
549 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
550 is not enough memory remaining to satisfy the request, then NULL is returned.\r
551 If Buffer is NULL, then ASSERT().\r
552 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
553\r
554 @param PoolType The type of pool to allocate.\r
555 @param AllocationSize The number of bytes to allocate and zero.\r
556 @param Buffer The buffer to copy to the allocated buffer.\r
557\r
558 @return A pointer to the allocated buffer or NULL if allocation fails.\r
559\r
560**/\r
561VOID *\r
562InternalAllocateCopyPool (\r
563 IN EFI_MEMORY_TYPE PoolType,\r
564 IN UINTN AllocationSize,\r
565 IN CONST VOID *Buffer\r
566 )\r
567{\r
568 VOID *Memory;\r
569\r
570 ASSERT (Buffer != NULL);\r
571 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r
572\r
573 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
574 if (Memory != NULL) {\r
575 Memory = CopyMem (Memory, Buffer, AllocationSize);\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
597VOID *\r
598EFIAPI\r
599AllocateCopyPool (\r
600 IN UINTN AllocationSize,\r
601 IN CONST VOID *Buffer\r
602 )\r
603{\r
604 return InternalAllocateCopyPool (EfiRuntimeServicesData, 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
624VOID *\r
625EFIAPI\r
626AllocateRuntimeCopyPool (\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
651VOID *\r
652EFIAPI\r
653AllocateReservedCopyPool (\r
654 IN UINTN AllocationSize,\r
655 IN CONST VOID *Buffer\r
656 )\r
657{\r
658 return NULL;\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
683VOID *\r
684InternalReallocatePool (\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 return NewBuffer;\r
699}\r
700\r
701/**\r
702 Reallocates a buffer of type EfiBootServicesData.\r
703\r
704 Allocates and zeros the number bytes specified by NewSize from memory of type\r
705 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and\r
706 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
707 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
708 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
709 enough memory remaining to satisfy the request, then NULL is returned.\r
710\r
711 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
712 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
713\r
714 @param OldSize The size, in bytes, of OldBuffer.\r
715 @param NewSize The size, in bytes, of the buffer to reallocate.\r
716 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
717 parameter that may be NULL.\r
718\r
719 @return A pointer to the allocated buffer or NULL if allocation fails.\r
720\r
721**/\r
722VOID *\r
723EFIAPI\r
724ReallocatePool (\r
725 IN UINTN OldSize,\r
726 IN UINTN NewSize,\r
727 IN VOID *OldBuffer OPTIONAL\r
728 )\r
729{\r
730 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
731}\r
732\r
733/**\r
734 Reallocates a buffer of type EfiRuntimeServicesData.\r
735\r
736 Allocates and zeros the number bytes specified by NewSize from memory of type\r
737 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and\r
738 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
739 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
740 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
741 enough memory remaining to satisfy the request, then NULL is returned.\r
742\r
743 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
744 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
745\r
746 @param OldSize The size, in bytes, of OldBuffer.\r
747 @param NewSize The size, in bytes, of the buffer to reallocate.\r
748 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
749 parameter that may be NULL.\r
750\r
751 @return A pointer to the allocated buffer or NULL if allocation fails.\r
752\r
753**/\r
754VOID *\r
755EFIAPI\r
756ReallocateRuntimePool (\r
757 IN UINTN OldSize,\r
758 IN UINTN NewSize,\r
759 IN VOID *OldBuffer OPTIONAL\r
760 )\r
761{\r
762 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
763}\r
764\r
765/**\r
766 Reallocates a buffer of type EfiReservedMemoryType.\r
767\r
768 Allocates and zeros the number bytes specified by NewSize from memory of type\r
769 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and\r
770 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
771 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
772 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
773 enough memory remaining to satisfy the request, then NULL is returned.\r
774\r
775 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
776 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
777\r
778 @param OldSize The size, in bytes, of OldBuffer.\r
779 @param NewSize The size, in bytes, of the buffer to reallocate.\r
780 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
781 parameter that may be NULL.\r
782\r
783 @return A pointer to the allocated buffer or NULL if allocation fails.\r
784\r
785**/\r
786VOID *\r
787EFIAPI\r
788ReallocateReservedPool (\r
789 IN UINTN OldSize,\r
790 IN UINTN NewSize,\r
791 IN VOID *OldBuffer OPTIONAL\r
792 )\r
793{\r
794 return NULL;\r
795}\r
796\r
797/**\r
798 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
799 Memory Allocation Library.\r
800\r
801 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
802 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
803 resources, then this function will perform no actions.\r
804\r
805 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
806 then ASSERT().\r
807\r
808 @param Buffer Pointer to the buffer to free.\r
809\r
810**/\r
811VOID\r
812EFIAPI\r
813FreePool (\r
814 IN VOID *Buffer\r
815 )\r
816{\r
817 EFI_STATUS Status;\r
818\r
819 Status = gMmst->MmFreePool (Buffer);\r
820 ASSERT_EFI_ERROR (Status);\r
821}\r