]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Foundation/Library/EdkIIGlueLib/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c
Update the copyright notice format
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / EdkIIGlueLib / Library / PeiMemoryAllocationLib / MemoryAllocationLib.c
CommitLineData
3eb9473e 1/*++\r
2\r
2c7e5c2f
HT
3Copyright (c) 2004 - 2006, Intel Corporation. All rights reserved.<BR>\r
4This program and the accompanying materials \r
3eb9473e 5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12\r
13Module Name:\r
14\r
15 MemoryAllocationLib.c\r
16 \r
17Abstract: \r
18\r
19 Support routines for memory allocation routines for use with drivers.\r
20\r
21--*/\r
22\r
23#include "EdkIIGluePeim.h"\r
24\r
25/**\r
26 Allocates one or more 4KB pages of a certain memory type.\r
27\r
28 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated\r
29 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.\r
30 If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
31\r
32 @param MemoryType The type of memory to allocate.\r
33 @param Pages The number of 4 KB pages to allocate.\r
34\r
35 @return A pointer to the allocated buffer or NULL if allocation fails.\r
36\r
37**/\r
38VOID *\r
39InternalAllocatePages (\r
40 IN EFI_MEMORY_TYPE MemoryType, \r
41 IN UINTN Pages\r
42 )\r
43{\r
44 EFI_STATUS Status;\r
45 EFI_PHYSICAL_ADDRESS Memory; \r
3eb9473e 46\r
47 if (Pages == 0) {\r
48 return NULL;\r
49 }\r
50\r
bfe03264 51 Status = PeiServicesAllocatePages (MemoryType, Pages, &Memory);\r
3eb9473e 52 if (EFI_ERROR (Status)) {\r
53 Memory = 0;\r
54 }\r
55 return (VOID *) (UINTN) Memory;\r
56}\r
57\r
58/**\r
59 Allocates one or more 4KB pages of type EfiBootServicesData.\r
60\r
61 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the\r
62 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
63 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
64 returned.\r
65\r
66 @param Pages The number of 4 KB pages to allocate.\r
67\r
68 @return A pointer to the allocated buffer or NULL if allocation fails.\r
69\r
70**/\r
71VOID *\r
72EFIAPI\r
73GlueAllocatePages (\r
74 IN UINTN Pages\r
75 )\r
76{\r
77 return InternalAllocatePages (EfiBootServicesData, Pages);\r
78}\r
79\r
80/**\r
81 Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r
82\r
83 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
84 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
85 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
86 returned.\r
87\r
88 @param Pages The number of 4 KB pages to allocate.\r
89\r
90 @return A pointer to the allocated buffer or NULL if allocation fails.\r
91\r
92**/\r
93VOID *\r
94EFIAPI\r
95AllocateRuntimePages (\r
96 IN UINTN Pages\r
97 )\r
98{\r
99 return InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
100}\r
101\r
102/**\r
103 Allocates one or more 4KB pages of type EfiReservedMemoryType.\r
104\r
105 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the\r
106 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
107 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
108 returned.\r
109\r
110 @param Pages The number of 4 KB pages to allocate.\r
111\r
112 @return A pointer to the allocated buffer or NULL if allocation fails.\r
113\r
114**/\r
115VOID *\r
116EFIAPI\r
117AllocateReservedPages (\r
118 IN UINTN Pages\r
119 )\r
120{\r
121 return InternalAllocatePages (EfiReservedMemoryType, Pages);\r
122}\r
123\r
124/**\r
125 Frees one or more 4KB pages that were previously allocated with one of the page allocation\r
126 functions in the Memory Allocation Library.\r
127\r
128 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
129 must have been allocated on a previous call to the page allocation services of the Memory\r
130 Allocation Library.\r
131 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r
132 then ASSERT().\r
133 If Pages is zero, then ASSERT().\r
134 \r
135 @param Buffer Pointer to the buffer of pages to free.\r
136 @param Pages The number of 4 KB pages to free.\r
137\r
138**/\r
139VOID\r
140EFIAPI\r
141GlueFreePages (\r
142 IN VOID *Buffer,\r
143 IN UINTN Pages\r
144 )\r
145{\r
146 //\r
147 // PEI phase does not support to free pages, so leave it as NOP.\r
148 //\r
149}\r
150\r
151/**\r
152 Allocates one or more 4KB pages of a certain memory type at a specified alignment.\r
153\r
154 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment\r
155 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.\r
156 If there is not enough memory at the specified alignment remaining to satisfy the request, then\r
157 NULL is returned.\r
158 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
159\r
160 @param MemoryType The type of memory to allocate.\r
161 @param Pages The number of 4 KB pages to allocate.\r
162 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
163 If Alignment is zero, then byte alignment is used.\r
164\r
165 @return A pointer to the allocated buffer or NULL if allocation fails.\r
166\r
167**/\r
168VOID *\r
169InternalAllocateAlignedPages (\r
170 IN EFI_MEMORY_TYPE MemoryType, \r
171 IN UINTN Pages,\r
172 IN UINTN Alignment\r
173 )\r
174{\r
175 VOID *Memory;\r
176 UINTN AlignmentMask;\r
177\r
178 //\r
179 // Alignment must be a power of two or zero.\r
180 //\r
181 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
182\r
183 if (Pages == 0) {\r
184 return NULL;\r
185 }\r
186 //\r
187 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
188 //\r
189 ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));\r
190 //\r
191 // We would rather waste some memory to save PEI code size.\r
192 //\r
193 Memory = InternalAllocatePages (MemoryType, Pages + EFI_SIZE_TO_PAGES (Alignment));\r
194 if (Alignment == 0) {\r
195 AlignmentMask = Alignment;\r
196 } else {\r
197 AlignmentMask = Alignment - 1; \r
198 }\r
199 return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask);\r
200}\r
201\r
202/**\r
203 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
204\r
205 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
206 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
207 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
208 request, then NULL is returned.\r
209 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
210\r
211 @param Pages The number of 4 KB pages to allocate.\r
212 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
213 If Alignment is zero, then byte alignment is used.\r
214\r
215 @return A pointer to the allocated buffer or NULL if allocation fails.\r
216\r
217**/\r
218VOID *\r
219EFIAPI\r
220AllocateAlignedPages (\r
221 IN UINTN Pages,\r
222 IN UINTN Alignment\r
223 )\r
224{\r
225 return InternalAllocateAlignedPages (EfiBootServicesData, Pages, Alignment);\r
226}\r
227\r
228/**\r
229 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.\r
230\r
231 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an\r
232 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
233 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
234 request, then NULL is returned.\r
235 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
236\r
237 @param Pages The number of 4 KB pages to allocate.\r
238 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
239 If Alignment is zero, then byte alignment is used.\r
240\r
241 @return A pointer to the allocated buffer or NULL if allocation fails.\r
242\r
243**/\r
244VOID *\r
245EFIAPI\r
246AllocateAlignedRuntimePages (\r
247 IN UINTN Pages,\r
248 IN UINTN Alignment\r
249 )\r
250{\r
251 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
252}\r
253\r
254/**\r
255 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.\r
256\r
257 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an\r
258 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
259 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
260 request, then NULL is returned.\r
261 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
262\r
263 @param Pages The number of 4 KB pages to allocate.\r
264 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
265 If Alignment is zero, then byte alignment is used.\r
266\r
267 @return A pointer to the allocated buffer or NULL if allocation fails.\r
268\r
269**/\r
270VOID *\r
271EFIAPI\r
272AllocateAlignedReservedPages (\r
273 IN UINTN Pages,\r
274 IN UINTN Alignment\r
275 )\r
276{\r
277 return InternalAllocateAlignedPages (EfiReservedMemoryType, Pages, Alignment);\r
278}\r
279\r
280/**\r
281 Frees one or more 4KB pages that were previously allocated with one of the aligned page\r
282 allocation functions in the Memory Allocation Library.\r
283\r
284 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
285 must have been allocated on a previous call to the aligned page allocation services of the Memory\r
286 Allocation Library.\r
287 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation\r
288 Library, then ASSERT().\r
289 If Pages is zero, then ASSERT().\r
290 \r
291 @param Buffer Pointer to the buffer of pages to free.\r
292 @param Pages The number of 4 KB pages to free.\r
293\r
294**/\r
295VOID\r
296EFIAPI\r
297FreeAlignedPages (\r
298 IN VOID *Buffer,\r
299 IN UINTN Pages\r
300 )\r
301{\r
302 //\r
303 // PEI phase does not support to free pages, so leave it as NOP.\r
304 //\r
305}\r
306\r
307/**\r
308 Allocates a buffer of a certain pool type.\r
309\r
310 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a\r
311 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
312 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
313\r
314 @param MemoryType The type of memory to allocate.\r
315 @param AllocationSize The number of bytes to allocate.\r
316\r
317 @return A pointer to the allocated buffer or NULL if allocation fails.\r
318\r
319**/\r
320VOID *\r
321InternalAllocatePool (\r
322 IN EFI_MEMORY_TYPE MemoryType, \r
323 IN UINTN AllocationSize\r
324 )\r
325{\r
326 //\r
327 // If we need lots of small runtime/reserved memory type from PEI in the future, \r
328 // we can consider providing a more complex algorithm that allocates runtime pages and \r
329 // provide pool allocations from those pages. \r
330 //\r
331 return InternalAllocatePages (MemoryType, EFI_SIZE_TO_PAGES (AllocationSize));\r
332}\r
333\r
334/**\r
335 Allocates a buffer of type EfiBootServicesData.\r
336\r
337 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
338 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
339 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
340\r
341 @param AllocationSize The number of bytes to allocate.\r
342\r
343 @return A pointer to the allocated buffer or NULL if allocation fails.\r
344\r
345**/\r
346VOID *\r
347EFIAPI\r
348GlueAllocatePool (\r
349 IN UINTN AllocationSize\r
350 )\r
351{\r
352 EFI_STATUS Status;\r
3eb9473e 353 VOID *Buffer;\r
354 \r
bfe03264 355 Status = PeiServicesAllocatePool (AllocationSize, &Buffer);\r
3eb9473e 356 if (EFI_ERROR (Status)) {\r
357 Buffer = NULL;\r
358 }\r
359 return Buffer;\r
360}\r
361\r
362/**\r
363 Allocates a buffer of type EfiRuntimeServicesData.\r
364\r
365 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns\r
366 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
367 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
368\r
369 @param AllocationSize The number of bytes to allocate.\r
370\r
371 @return A pointer to the allocated buffer or NULL if allocation fails.\r
372\r
373**/\r
374VOID *\r
375EFIAPI\r
376AllocateRuntimePool (\r
377 IN UINTN AllocationSize\r
378 )\r
379{\r
380 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
381}\r
382\r
383/**\r
384 Allocates a buffer of type EfieservedMemoryType.\r
385\r
386 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType and returns\r
387 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
388 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
389\r
390 @param AllocationSize The number of bytes to allocate.\r
391\r
392 @return A pointer to the allocated buffer or NULL if allocation fails.\r
393\r
394**/\r
395VOID *\r
396EFIAPI\r
397AllocateReservedPool (\r
398 IN UINTN AllocationSize\r
399 )\r
400{\r
401 return InternalAllocatePool (EfiReservedMemoryType, AllocationSize);\r
402}\r
403\r
404/**\r
405 Allocates and zeros a buffer of a certian pool type.\r
406\r
407 Allocates the number bytes specified by AllocationSize of a certian pool type, clears the buffer\r
408 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid\r
409 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,\r
410 then NULL is returned.\r
411\r
412 @param PoolType The type of memory to allocate.\r
413 @param AllocationSize The number of bytes to allocate and zero.\r
414\r
415 @return A pointer to the allocated buffer or NULL if allocation fails.\r
416\r
417**/\r
418VOID *\r
419InternalAllocateZeroPool (\r
420 IN EFI_MEMORY_TYPE PoolType, \r
421 IN UINTN AllocationSize\r
422 ) \r
423{\r
424 VOID *Memory;\r
425\r
426 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
427 if (Memory != NULL) {\r
428 Memory = ZeroMem (Memory, AllocationSize);\r
429 }\r
430 return Memory;\r
431}\r
432\r
433/**\r
434 Allocates and zeros a buffer of type EfiBootServicesData.\r
435\r
436 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
437 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
438 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
439 request, then NULL is returned.\r
440\r
441 @param AllocationSize The number of bytes to allocate and zero.\r
442\r
443 @return A pointer to the allocated buffer or NULL if allocation fails.\r
444\r
445**/\r
446VOID *\r
447EFIAPI\r
448GlueAllocateZeroPool (\r
449 IN UINTN AllocationSize\r
450 )\r
451{\r
452 VOID *Memory;\r
453\r
454 Memory = AllocatePool (AllocationSize);\r
455 if (Memory != NULL) {\r
456 Memory = ZeroMem (Memory, AllocationSize);\r
457 }\r
458 return Memory;\r
459}\r
460\r
461/**\r
462 Allocates and zeros a buffer of type EfiRuntimeServicesData.\r
463\r
464 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the\r
465 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
466 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
467 request, then NULL is returned.\r
468\r
469 @param AllocationSize The number of bytes to allocate and zero.\r
470\r
471 @return A pointer to the allocated buffer or NULL if allocation fails.\r
472\r
473**/\r
474VOID *\r
475EFIAPI\r
476AllocateRuntimeZeroPool (\r
477 IN UINTN AllocationSize\r
478 )\r
479{\r
480 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
481}\r
482\r
483/**\r
484 Allocates and zeros a buffer of type EfiReservedMemoryType.\r
485\r
486 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the\r
487 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
488 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
489 request, then NULL is returned.\r
490\r
491 @param AllocationSize The number of bytes to allocate and zero.\r
492\r
493 @return A pointer to the allocated buffer or NULL if allocation fails.\r
494\r
495**/\r
496VOID *\r
497EFIAPI\r
498AllocateReservedZeroPool (\r
499 IN UINTN AllocationSize\r
500 )\r
501{\r
502 return InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize);\r
503}\r
504\r
505/**\r
506 Copies a buffer to an allocated buffer of a certian pool type.\r
507\r
508 Allocates the number bytes specified by AllocationSize of a certian pool type, copies\r
509 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
510 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
511 is not enough memory remaining to satisfy the request, then NULL is returned.\r
512 If Buffer is NULL, then ASSERT().\r
513 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT(). \r
514\r
515 @param PoolType The type of pool to allocate.\r
516 @param AllocationSize The number of bytes to allocate and zero.\r
517 @param Buffer The buffer to copy to the allocated buffer.\r
518\r
519 @return A pointer to the allocated buffer or NULL if allocation fails.\r
520\r
521**/\r
522VOID *\r
523InternalAllocateCopyPool (\r
524 IN EFI_MEMORY_TYPE PoolType, \r
525 IN UINTN AllocationSize,\r
526 IN CONST VOID *Buffer\r
527 ) \r
528{\r
529 VOID *Memory;\r
530\r
531 ASSERT (Buffer != NULL);\r
532 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r
533\r
534 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
535 if (Memory != NULL) {\r
536 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
537 }\r
538 return Memory;\r
539} \r
540\r
541/**\r
542 Copies a buffer to an allocated buffer of type EfiBootServicesData.\r
543\r
544 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r
545 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
546 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
547 is not enough memory remaining to satisfy the request, then NULL is returned.\r
548 If Buffer is NULL, then ASSERT().\r
549 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT(). \r
550\r
551 @param AllocationSize The number of bytes to allocate and zero.\r
552 @param Buffer The buffer to copy to the allocated buffer.\r
553\r
554 @return A pointer to the allocated buffer or NULL if allocation fails.\r
555\r
556**/\r
557VOID *\r
558EFIAPI\r
559GlueAllocateCopyPool (\r
560 IN UINTN AllocationSize,\r
561 IN CONST VOID *Buffer\r
562 )\r
563{\r
564 VOID *Memory;\r
565\r
566 ASSERT (Buffer != NULL);\r
567 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r
568\r
569 Memory = AllocatePool (AllocationSize);\r
570 if (Memory != NULL) {\r
571 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
572 }\r
573 return Memory;\r
574}\r
575\r
576/**\r
577 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r
578\r
579 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies\r
580 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
581 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
582 is not enough memory remaining to satisfy the request, then NULL is returned.\r
583 If Buffer is NULL, then ASSERT().\r
584 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT(). \r
585\r
586 @param AllocationSize The number of bytes to allocate and zero.\r
587 @param Buffer The buffer to copy to the allocated buffer.\r
588\r
589 @return A pointer to the allocated buffer or NULL if allocation fails.\r
590\r
591**/\r
592VOID *\r
593EFIAPI\r
594AllocateRuntimeCopyPool (\r
595 IN UINTN AllocationSize,\r
596 IN CONST VOID *Buffer\r
597 )\r
598{\r
599 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
600}\r
601\r
602/**\r
603 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.\r
604\r
605 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies\r
606 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
607 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
608 is not enough memory remaining to satisfy the request, then NULL is returned.\r
609 If Buffer is NULL, then ASSERT().\r
610 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT(). \r
611\r
612 @param AllocationSize The number of bytes to allocate and zero.\r
613 @param Buffer The buffer to copy to the allocated buffer.\r
614\r
615 @return A pointer to the allocated buffer or NULL if allocation fails.\r
616\r
617**/\r
618VOID *\r
619EFIAPI\r
620AllocateReservedCopyPool (\r
621 IN UINTN AllocationSize,\r
622 IN CONST VOID *Buffer\r
623 )\r
624{\r
625 return InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, Buffer);\r
626}\r
627\r
628/**\r
629 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
630 Memory Allocation Library.\r
631\r
632 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
633 pool allocation services of the Memory Allocation Library.\r
634 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
635 then ASSERT().\r
636\r
637 @param Buffer Pointer to the buffer to free.\r
638\r
639**/\r
640VOID\r
641EFIAPI\r
642GlueFreePool (\r
643 IN VOID *Buffer\r
644 )\r
645{\r
646 //\r
647 // PEI phase does not support to free pool, so leave it as NOP.\r
648 //\r
649}\r
650\r
651/**\r
652 Allocates a buffer of a certain pool type at a specified alignment.\r
653\r
654 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment\r
655 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid\r
656 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining\r
657 to satisfy the request, then NULL is returned.\r
658 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
659\r
660 @param PoolType The type of pool to allocate.\r
661 @param AllocationSize The number of bytes to allocate.\r
662 @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used.\r
663 If Alignment is zero, then byte alignment is used.\r
664\r
665 @return A pointer to the allocated buffer or NULL if allocation fails.\r
666\r
667**/\r
668VOID *\r
669InternalAllocateAlignedPool (\r
670 IN EFI_MEMORY_TYPE PoolType,\r
671 IN UINTN AllocationSize,\r
672 IN UINTN Alignment\r
673 )\r
674{\r
675 VOID *RawAddress;\r
676 UINTN AlignedAddress;\r
677 UINTN AlignmentMask;\r
678\r
679 //\r
680 // Alignment must be a power of two or zero.\r
681 //\r
682 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
683 \r
684 if (Alignment == 0) {\r
685 AlignmentMask = Alignment;\r
686 } else {\r
687 AlignmentMask = Alignment - 1;\r
688 }\r
689 //\r
690 // Make sure that AllocationSize plus AlignmentMask does not overflow.\r
691 //\r
692 ASSERT (AllocationSize <= (MAX_ADDRESS - AlignmentMask));\r
693\r
694 RawAddress = InternalAllocatePool (PoolType, AllocationSize + AlignmentMask);\r
695 \r
696 AlignedAddress = ((UINTN) RawAddress + AlignmentMask) & ~AlignmentMask;\r
697 \r
698 return (VOID *) AlignedAddress;\r
699}\r
700\r
701/**\r
702 Allocates a buffer of type EfiBootServicesData at a specified alignment.\r
703\r
704 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an\r
705 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,\r
706 then a valid buffer of 0 size is returned. If there is not enough memory at the specified\r
707 alignment remaining to satisfy the request, then NULL is returned.\r
708 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
709\r
710 @param AllocationSize The number of bytes to allocate.\r
711 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
712 If Alignment is zero, then byte alignment is used.\r
713\r
714 @return A pointer to the allocated buffer or NULL if allocation fails.\r
715\r
716**/\r
717VOID *\r
718EFIAPI\r
719AllocateAlignedPool (\r
720 IN UINTN AllocationSize,\r
721 IN UINTN Alignment\r
722 )\r
723{\r
724 VOID *RawAddress;\r
725 UINTN AlignedAddress;\r
726 UINTN AlignmentMask;\r
727\r
728 //\r
729 // Alignment must be a power of two or zero.\r
730 //\r
731 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
732\r
733 if (Alignment == 0) {\r
734 AlignmentMask = Alignment;\r
735 } else {\r
736 AlignmentMask = Alignment - 1;\r
737 }\r
738\r
739 //\r
740 // Make sure that AllocationSize plus AlignmentMask does not overflow.\r
741 //\r
742 ASSERT (AllocationSize <= (MAX_ADDRESS - AlignmentMask));\r
743\r
744 RawAddress = AllocatePool (AllocationSize + AlignmentMask);\r
745 \r
746 AlignedAddress = ((UINTN) RawAddress + AlignmentMask) & ~AlignmentMask;\r
747 \r
748 return (VOID *) AlignedAddress;\r
749}\r
750\r
751/**\r
752 Allocates a buffer of type EfiRuntimeServicesData at a specified alignment.\r
753\r
754 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an\r
755 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,\r
756 then a valid buffer of 0 size is returned. If there is not enough memory at the specified\r
757 alignment remaining to satisfy the request, then NULL is returned.\r
758 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
759\r
760 @param AllocationSize The number of bytes to allocate.\r
761 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
762 If Alignment is zero, then byte alignment is used.\r
763\r
764 @return A pointer to the allocated buffer or NULL if allocation fails.\r
765\r
766**/\r
767VOID *\r
768EFIAPI\r
769AllocateAlignedRuntimePool (\r
770 IN UINTN AllocationSize,\r
771 IN UINTN Alignment\r
772 )\r
773{\r
774 return InternalAllocateAlignedPool (EfiRuntimeServicesData, AllocationSize, Alignment);\r
775}\r
776\r
777/**\r
778 Allocates a buffer of type EfieservedMemoryType at a specified alignment.\r
779\r
780 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an\r
781 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,\r
782 then a valid buffer of 0 size is returned. If there is not enough memory at the specified\r
783 alignment remaining to satisfy the request, then NULL is returned.\r
784 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
785\r
786 @param AllocationSize The number of bytes to allocate.\r
787 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
788 If Alignment is zero, then byte alignment is used.\r
789\r
790 @return A pointer to the allocated buffer or NULL if allocation fails.\r
791\r
792**/\r
793VOID *\r
794EFIAPI\r
795AllocateAlignedReservedPool (\r
796 IN UINTN AllocationSize,\r
797 IN UINTN Alignment\r
798 )\r
799{\r
800 return InternalAllocateAlignedPool (EfiReservedMemoryType, AllocationSize, Alignment);\r
801}\r
802\r
803/**\r
804 Allocates and zeros a buffer of a certain pool type at a specified alignment.\r
805\r
806 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment\r
807 specified by Alignment, clears the buffer with zeros, and returns a pointer to the allocated\r
808 buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not\r
809 enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.\r
810 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
811\r
812 @param PoolType The type of pool to allocate.\r
813 @param AllocationSize The number of bytes to allocate.\r
814 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
815 If Alignment is zero, then byte alignment is used.\r
816\r
817 @return A pointer to the allocated buffer or NULL if allocation fails.\r
818\r
819**/\r
820VOID *\r
821InternalAllocateAlignedZeroPool (\r
822 IN EFI_MEMORY_TYPE PoolType,\r
823 IN UINTN AllocationSize,\r
824 IN UINTN Alignment\r
825 )\r
826{\r
827 VOID *Memory;\r
828\r
829 Memory = InternalAllocateAlignedPool (PoolType, AllocationSize, Alignment);\r
830 if (Memory != NULL) {\r
831 Memory = ZeroMem (Memory, AllocationSize);\r
832 }\r
833 return Memory;\r
834}\r
835\r
836/**\r
837 Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.\r
838\r
839 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an\r
840 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the\r
841 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
842 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is\r
843 returned.\r
844 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
845\r
846 @param AllocationSize The number of bytes to allocate.\r
847 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
848 If Alignment is zero, then byte alignment is used.\r
849\r
850 @return A pointer to the allocated buffer or NULL if allocation fails.\r
851\r
852**/\r
853VOID *\r
854EFIAPI\r
855AllocateAlignedZeroPool (\r
856 IN UINTN AllocationSize,\r
857 IN UINTN Alignment\r
858 )\r
859{\r
860 VOID *Memory;\r
861\r
862 Memory = AllocateAlignedPool (AllocationSize, Alignment);\r
863 if (Memory != NULL) {\r
864 Memory = ZeroMem (Memory, AllocationSize);\r
865 }\r
866 return Memory;\r
867}\r
868\r
869/**\r
870 Allocates and zeros a buffer of type EfiRuntimeServicesData at a specified alignment.\r
871\r
872 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an\r
873 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the\r
874 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
875 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is\r
876 returned.\r
877 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
878\r
879 @param AllocationSize The number of bytes to allocate.\r
880 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
881 If Alignment is zero, then byte alignment is used.\r
882\r
883 @return A pointer to the allocated buffer or NULL if allocation fails.\r
884\r
885**/\r
886VOID *\r
887EFIAPI\r
888AllocateAlignedRuntimeZeroPool (\r
889 IN UINTN AllocationSize,\r
890 IN UINTN Alignment\r
891 )\r
892{\r
893 return InternalAllocateAlignedZeroPool (EfiRuntimeServicesData, AllocationSize, Alignment);\r
894}\r
895\r
896/**\r
897 Allocates and zeros a buffer of type EfieservedMemoryType at a specified alignment.\r
898\r
899 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an\r
900 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the\r
901 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
902 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is\r
903 returned.\r
904 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
905\r
906 @param AllocationSize The number of bytes to allocate.\r
907 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
908 If Alignment is zero, then byte alignment is used.\r
909\r
910 @return A pointer to the allocated buffer or NULL if allocation fails.\r
911\r
912**/\r
913VOID *\r
914EFIAPI\r
915AllocateAlignedReservedZeroPool (\r
916 IN UINTN AllocationSize,\r
917 IN UINTN Alignment\r
918 )\r
919{\r
920 return InternalAllocateAlignedZeroPool (EfiReservedMemoryType, AllocationSize, Alignment);\r
921}\r
922\r
923/**\r
924 Copies a buffer to an allocated buffer of a certain pool type at a specified alignment.\r
925\r
926 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment\r
927 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid\r
928 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining\r
929 to satisfy the request, then NULL is returned.\r
930 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
931\r
932 @param PoolType The type of pool to allocate.\r
933 @param AllocationSize The number of bytes to allocate.\r
934 @param Buffer The buffer to copy to the allocated buffer.\r
935 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
936 If Alignment is zero, then byte alignment is used.\r
937\r
938 @return A pointer to the allocated buffer or NULL if allocation fails.\r
939\r
940**/\r
941VOID *\r
942InternalAllocateAlignedCopyPool (\r
943 IN EFI_MEMORY_TYPE PoolType,\r
944 IN UINTN AllocationSize,\r
945 IN CONST VOID *Buffer,\r
946 IN UINTN Alignment\r
947 )\r
948{\r
949 VOID *Memory;\r
950 \r
951 ASSERT (Buffer != NULL);\r
952 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r
953\r
954 Memory = InternalAllocateAlignedPool (PoolType, AllocationSize, Alignment);\r
955 if (Memory != NULL) {\r
956 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
957 }\r
958 return Memory;\r
959}\r
960\r
961/**\r
962 Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment.\r
963\r
964 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData type with an\r
965 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,\r
966 then a valid buffer of 0 size is returned. If there is not enough memory at the specified\r
967 alignment remaining to satisfy the request, then NULL is returned.\r
968 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
969\r
970 @param AllocationSize The number of bytes to allocate.\r
971 @param Buffer The buffer to copy to the allocated buffer.\r
972 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
973 If Alignment is zero, then byte alignment is used.\r
974\r
975 @return A pointer to the allocated buffer or NULL if allocation fails.\r
976\r
977**/\r
978VOID *\r
979EFIAPI\r
980AllocateAlignedCopyPool (\r
981 IN UINTN AllocationSize,\r
982 IN CONST VOID *Buffer,\r
983 IN UINTN Alignment\r
984 )\r
985{\r
986 VOID *Memory;\r
987 \r
988 ASSERT (Buffer != NULL);\r
989 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r
990\r
991 Memory = AllocateAlignedPool (AllocationSize, Alignment);\r
992 if (Memory != NULL) {\r
993 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
994 }\r
995 return Memory;\r
996}\r
997\r
998/**\r
999 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData at a specified alignment.\r
1000\r
1001 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData type with an\r
1002 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,\r
1003 then a valid buffer of 0 size is returned. If there is not enough memory at the specified\r
1004 alignment remaining to satisfy the request, then NULL is returned.\r
1005 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
1006\r
1007 @param AllocationSize The number of bytes to allocate.\r
1008 @param Buffer The buffer to copy to the allocated buffer.\r
1009 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
1010 If Alignment is zero, then byte alignment is used.\r
1011\r
1012 @return A pointer to the allocated buffer or NULL if allocation fails.\r
1013\r
1014**/\r
1015VOID *\r
1016EFIAPI\r
1017AllocateAlignedRuntimeCopyPool (\r
1018 IN UINTN AllocationSize,\r
1019 IN CONST VOID *Buffer,\r
1020 IN UINTN Alignment\r
1021 )\r
1022{\r
1023 return InternalAllocateAlignedCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer, Alignment);\r
1024}\r
1025\r
1026/**\r
1027 Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment.\r
1028\r
1029 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType type with an\r
1030 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,\r
1031 then a valid buffer of 0 size is returned. If there is not enough memory at the specified\r
1032 alignment remaining to satisfy the request, then NULL is returned.\r
1033 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
1034\r
1035 @param AllocationSize The number of bytes to allocate.\r
1036 @param Buffer The buffer to copy to the allocated buffer.\r
1037 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
1038 If Alignment is zero, then byte alignment is used.\r
1039\r
1040 @return A pointer to the allocated buffer or NULL if allocation fails.\r
1041\r
1042**/\r
1043VOID *\r
1044EFIAPI\r
1045AllocateAlignedReservedCopyPool (\r
1046 IN UINTN AllocationSize,\r
1047 IN CONST VOID *Buffer,\r
1048 IN UINTN Alignment\r
1049 )\r
1050{\r
1051 return InternalAllocateAlignedCopyPool (EfiReservedMemoryType, AllocationSize, Buffer, Alignment);\r
1052}\r
1053\r
1054/**\r
1055 Frees a buffer that was previously allocated with one of the aligned pool allocation functions \r
1056 in the Memory Allocation Library.\r
1057\r
1058 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
1059 aligned pool allocation services of the Memory Allocation Library.\r
1060 If Buffer was not allocated with an aligned pool allocation function in the Memory Allocation\r
1061 Library, then ASSERT().\r
1062\r
1063 @param Buffer Pointer to the buffer to free.\r
1064\r
1065**/\r
1066VOID\r
1067EFIAPI\r
1068FreeAlignedPool (\r
1069 IN VOID *Buffer\r
1070 )\r
1071{\r
1072 //\r
1073 // PEI phase does not support to free pool, so leave it as NOP.\r
1074 //\r
1075}\r