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