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