]>
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 | |
a3657e3e | 161 | // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r |
162 | //\r | |
163 | ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));\r | |
164 | //\r | |
878ddf1f | 165 | // We would rather waste some memory to save PEI code size.\r |
166 | //\r | |
167 | Memory = InternalAllocatePages (MemoryType, Pages + EFI_SIZE_TO_PAGES (Alignment));\r | |
168 | if (Alignment == 0) {\r | |
169 | AlignmentMask = Alignment;\r | |
170 | } else {\r | |
171 | AlignmentMask = Alignment - 1; \r | |
172 | }\r | |
173 | return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask);\r | |
174 | }\r | |
175 | \r | |
176 | /**\r | |
177 | Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData\r | |
178 | with an alignment specified by Alignment. \r | |
179 | \r | |
180 | @param Pages The number of 4 KB pages to allocate.\r | |
181 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
182 | If Alignment is zero, then byte alignment is used.\r | |
183 | \r | |
184 | @return The allocated buffer is returned. If Pages is 0, then NULL is returned.\r | |
185 | If there is not enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.\r | |
186 | \r | |
187 | **/\r | |
188 | VOID *\r | |
189 | EFIAPI\r | |
190 | AllocateAlignedPages (\r | |
191 | IN UINTN Pages,\r | |
192 | IN UINTN Alignment\r | |
193 | )\r | |
194 | {\r | |
195 | return InternalAllocateAlignedPages (EfiBootServicesData, Pages, Alignment);\r | |
196 | }\r | |
197 | \r | |
198 | /**\r | |
199 | Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData\r | |
200 | with an alignment specified by Alignment. \r | |
201 | \r | |
202 | @param Pages The number of 4 KB pages to allocate.\r | |
203 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
204 | If Alignment is zero, then byte alignment is used.\r | |
205 | \r | |
206 | @return The allocated buffer is returned. If Pages is 0, then NULL is returned.\r | |
207 | If there is not enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.\r | |
208 | \r | |
209 | **/\r | |
210 | VOID *\r | |
211 | EFIAPI\r | |
212 | AllocateAlignedRuntimePages (\r | |
213 | IN UINTN Pages,\r | |
214 | IN UINTN Alignment\r | |
215 | )\r | |
216 | {\r | |
217 | return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r | |
218 | }\r | |
219 | \r | |
220 | /**\r | |
221 | Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.\r | |
222 | \r | |
223 | @param Pages The number of 4 KB pages to allocate.\r | |
224 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
225 | If Alignment is zero, then byte alignment is used.\r | |
226 | \r | |
227 | @return The allocated buffer is returned. If Pages is 0, then NULL is returned.\r | |
228 | If there is not enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.\r | |
229 | \r | |
230 | **/\r | |
231 | VOID *\r | |
232 | EFIAPI\r | |
233 | AllocateAlignedReservedPages (\r | |
234 | IN UINTN Pages,\r | |
235 | IN UINTN Alignment\r | |
236 | )\r | |
237 | {\r | |
238 | return InternalAllocateAlignedPages (EfiReservedMemoryType, Pages, Alignment);\r | |
239 | }\r | |
240 | \r | |
241 | /**\r | |
242 | Frees one or more 4KB pages that were previously allocated with \r | |
243 | one of the aligned page allocation functions in the Memory Allocation Library.\r | |
244 | \r | |
245 | @param Buffer Pointer to the buffer of pages to free.\r | |
246 | @param Pages The number of 4 KB pages to free.\r | |
247 | \r | |
248 | **/\r | |
249 | VOID\r | |
250 | EFIAPI\r | |
251 | FreeAlignedPages (\r | |
252 | IN VOID *Buffer,\r | |
253 | IN UINTN Pages\r | |
254 | )\r | |
255 | {\r | |
256 | //\r | |
257 | // PEI phase does not support to free pages, so leave it as NOP.\r | |
258 | //\r | |
259 | }\r | |
260 | \r | |
261 | /**\r | |
262 | Allocates a buffer of a certain memory type.\r | |
263 | \r | |
264 | @param MemoryType The type of memory to allocate.\r | |
265 | @param AllocationSize The number of bytes to allocate.\r | |
266 | \r | |
267 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
268 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
269 | \r | |
270 | **/\r | |
271 | VOID *\r | |
272 | InternalAllocatePool (\r | |
273 | IN EFI_MEMORY_TYPE MemoryType, \r | |
274 | IN UINTN AllocationSize\r | |
275 | )\r | |
276 | {\r | |
277 | //\r | |
278 | // If we need lots of small runtime/reserved memory type from PEI in the future, \r | |
279 | // we can consider providing a more complex algorithm that allocates runtime pages and \r | |
280 | // provide pool allocations from those pages. \r | |
281 | //\r | |
282 | return InternalAllocatePages (MemoryType, EFI_SIZE_TO_PAGES (AllocationSize));\r | |
283 | }\r | |
284 | \r | |
285 | /**\r | |
286 | Allocates a buffer of type EfiBootServicesData.\r | |
287 | \r | |
288 | @param AllocationSize The number of bytes to allocate.\r | |
289 | \r | |
290 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
291 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
292 | \r | |
293 | **/\r | |
294 | VOID *\r | |
295 | EFIAPI\r | |
296 | AllocatePool (\r | |
297 | IN UINTN AllocationSize\r | |
298 | )\r | |
299 | {\r | |
300 | EFI_STATUS Status;\r | |
301 | EFI_PEI_SERVICES **PeiServices;\r | |
302 | VOID *Buffer;\r | |
303 | \r | |
304 | PeiServices = GetPeiServicesTablePointer ();\r | |
305 | \r | |
306 | Status = (*PeiServices)->AllocatePool (PeiServices, AllocationSize, &Buffer);\r | |
307 | if (EFI_ERROR (Status)) {\r | |
308 | Buffer = NULL;\r | |
309 | }\r | |
310 | return Buffer;\r | |
311 | }\r | |
312 | \r | |
313 | /**\r | |
314 | Allocates a buffer of type EfiRuntimeServicesData.\r | |
315 | \r | |
316 | @param AllocationSize The number of bytes to allocate.\r | |
317 | \r | |
318 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
319 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
320 | \r | |
321 | **/\r | |
322 | VOID *\r | |
323 | EFIAPI\r | |
324 | AllocateRuntimePool (\r | |
325 | IN UINTN AllocationSize\r | |
326 | )\r | |
327 | {\r | |
328 | return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r | |
329 | }\r | |
330 | \r | |
331 | /**\r | |
332 | Allocates a buffer of type EfiReservedMemoryType.\r | |
333 | \r | |
334 | @param AllocationSize The number of bytes to allocate.\r | |
335 | \r | |
336 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
337 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
338 | \r | |
339 | **/\r | |
340 | VOID *\r | |
341 | EFIAPI\r | |
342 | AllocateReservedPool (\r | |
343 | IN UINTN AllocationSize\r | |
344 | )\r | |
345 | {\r | |
346 | return InternalAllocatePool (EfiReservedMemoryType, AllocationSize);\r | |
347 | }\r | |
348 | \r | |
349 | /**\r | |
350 | Allocates and zeros a buffer of a certian pool type.\r | |
351 | \r | |
352 | @param PoolType The type of memory to allocate.\r | |
353 | @param AllocationSize The number of bytes to allocate and zero.\r | |
354 | \r | |
355 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
356 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
357 | \r | |
358 | **/\r | |
359 | VOID *\r | |
360 | InternalAllocateZeroPool (\r | |
361 | IN EFI_MEMORY_TYPE PoolType, \r | |
362 | IN UINTN AllocationSize\r | |
363 | ) \r | |
364 | {\r | |
365 | VOID *Memory;\r | |
366 | \r | |
367 | Memory = InternalAllocatePool (PoolType, AllocationSize);\r | |
368 | if (Memory != NULL) {\r | |
369 | Memory = ZeroMem (Memory, AllocationSize);\r | |
370 | }\r | |
371 | return Memory;\r | |
372 | }\r | |
373 | \r | |
374 | /**\r | |
375 | Allocates and zeros a buffer of type EfiBootServicesData.\r | |
376 | \r | |
377 | @param AllocationSize The number of bytes to allocate and zero.\r | |
378 | \r | |
379 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
380 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
381 | \r | |
382 | **/\r | |
383 | VOID *\r | |
384 | EFIAPI\r | |
385 | AllocateZeroPool (\r | |
386 | IN UINTN AllocationSize\r | |
387 | )\r | |
388 | {\r | |
389 | VOID *Memory;\r | |
390 | \r | |
391 | Memory = AllocatePool (AllocationSize);\r | |
392 | if (Memory != NULL) {\r | |
393 | Memory = ZeroMem (Memory, AllocationSize);\r | |
394 | }\r | |
395 | return Memory;\r | |
396 | }\r | |
397 | \r | |
398 | /**\r | |
399 | Allocates and zeros a buffer of type EfiRuntimeServicesData.\r | |
400 | \r | |
401 | @param AllocationSize The number of bytes to allocate and zero.\r | |
402 | \r | |
403 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
404 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
405 | \r | |
406 | **/\r | |
407 | VOID *\r | |
408 | EFIAPI\r | |
409 | AllocateRuntimeZeroPool (\r | |
410 | IN UINTN AllocationSize\r | |
411 | )\r | |
412 | {\r | |
413 | return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r | |
414 | }\r | |
415 | \r | |
416 | /**\r | |
417 | Allocates and zeros a buffer of type EfiReservedMemoryType.\r | |
418 | \r | |
419 | @param AllocationSize The number of bytes to allocate and zero.\r | |
420 | \r | |
421 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
422 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
423 | \r | |
424 | **/\r | |
425 | VOID *\r | |
426 | EFIAPI\r | |
427 | AllocateReservedZeroPool (\r | |
428 | IN UINTN AllocationSize\r | |
429 | )\r | |
430 | {\r | |
431 | return InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize);\r | |
432 | }\r | |
433 | \r | |
434 | /**\r | |
435 | Copies a buffer to an allocated buffer of a certian memory type. \r | |
436 | \r | |
437 | @param MemoryType The type of pool to allocate.\r | |
438 | @param AllocationSize The number of bytes to allocate and zero.\r | |
439 | @param Buffer The buffer to copy to the allocated buffer.\r | |
440 | \r | |
441 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
442 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
443 | \r | |
444 | **/\r | |
445 | VOID *\r | |
446 | InternalAllocateCopyPool (\r | |
447 | IN EFI_MEMORY_TYPE PoolType, \r | |
448 | IN UINTN AllocationSize,\r | |
449 | IN CONST VOID *Buffer\r | |
450 | ) \r | |
451 | {\r | |
452 | VOID *Memory;\r | |
453 | \r | |
9a462b41 | 454 | ASSERT (Buffer != NULL);\r |
455 | ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r | |
456 | \r | |
878ddf1f | 457 | Memory = InternalAllocatePool (PoolType, AllocationSize);\r |
458 | if (Memory != NULL) {\r | |
459 | Memory = CopyMem (Memory, Buffer, AllocationSize);\r | |
460 | }\r | |
461 | return Memory;\r | |
462 | } \r | |
463 | \r | |
464 | /**\r | |
465 | Copies a buffer to an allocated buffer of type EfiBootServicesData. \r | |
466 | \r | |
467 | @param AllocationSize The number of bytes to allocate.\r | |
468 | @param Buffer The buffer to copy to the allocated buffer.\r | |
469 | \r | |
470 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
471 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
472 | \r | |
473 | **/\r | |
474 | VOID *\r | |
475 | EFIAPI\r | |
476 | AllocateCopyPool (\r | |
477 | IN UINTN AllocationSize,\r | |
478 | IN CONST VOID *Buffer\r | |
479 | )\r | |
480 | {\r | |
481 | VOID *Memory;\r | |
482 | \r | |
9a462b41 | 483 | ASSERT (Buffer != NULL);\r |
484 | ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r | |
485 | \r | |
878ddf1f | 486 | Memory = AllocatePool (AllocationSize);\r |
487 | if (Memory != NULL) {\r | |
488 | Memory = CopyMem (Memory, Buffer, AllocationSize);\r | |
489 | }\r | |
490 | return Memory;\r | |
491 | }\r | |
492 | \r | |
493 | /**\r | |
494 | Copies a buffer to an allocated buffer of type EfiRuntimeServicesData. \r | |
495 | \r | |
496 | @param AllocationSize The number of bytes to allocate.\r | |
497 | @param Buffer The buffer to copy to the allocated buffer.\r | |
498 | \r | |
499 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
500 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
501 | \r | |
502 | **/\r | |
503 | VOID *\r | |
504 | EFIAPI\r | |
505 | AllocateRuntimeCopyPool (\r | |
506 | IN UINTN AllocationSize,\r | |
507 | IN CONST VOID *Buffer\r | |
508 | )\r | |
509 | {\r | |
510 | return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r | |
511 | }\r | |
512 | \r | |
513 | /**\r | |
514 | Copies a buffer to an allocated buffer of type EfiReservedMemoryType. \r | |
515 | \r | |
516 | @param AllocationSize The number of bytes to allocate.\r | |
517 | @param Buffer The buffer to copy to the allocated buffer.\r | |
518 | \r | |
519 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
520 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
521 | \r | |
522 | **/\r | |
523 | VOID *\r | |
524 | EFIAPI\r | |
525 | AllocateReservedCopyPool (\r | |
526 | IN UINTN AllocationSize,\r | |
527 | IN CONST VOID *Buffer\r | |
528 | )\r | |
529 | {\r | |
530 | return InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, Buffer);\r | |
531 | }\r | |
532 | \r | |
533 | /**\r | |
534 | Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment.\r | |
535 | \r | |
536 | @param Buffer Pointer to the buffer to free.\r | |
537 | \r | |
538 | **/\r | |
539 | VOID\r | |
540 | EFIAPI\r | |
541 | FreePool (\r | |
542 | IN VOID *Buffer\r | |
543 | )\r | |
544 | {\r | |
545 | //\r | |
546 | // PEI phase does not support to free pool, so leave it as NOP.\r | |
547 | //\r | |
548 | }\r | |
549 | \r | |
550 | /**\r | |
551 | Allocates a buffer of a certain pool type at a specified alignment.\r | |
552 | \r | |
553 | @param PoolType The type of pool to allocate.\r | |
554 | @param AllocationSize The number of bytes to allocate.\r | |
555 | @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used.\r | |
556 | \r | |
557 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
558 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
559 | \r | |
560 | **/\r | |
561 | VOID *\r | |
562 | InternalAllocateAlignedPool (\r | |
563 | IN EFI_MEMORY_TYPE PoolType,\r | |
564 | IN UINTN AllocationSize,\r | |
565 | IN UINTN Alignment\r | |
566 | )\r | |
567 | {\r | |
568 | VOID *RawAddress;\r | |
569 | UINTN AlignedAddress;\r | |
570 | UINTN AlignmentMask;\r | |
571 | \r | |
572 | //\r | |
573 | // Alignment must be a power of two or zero.\r | |
574 | //\r | |
575 | ASSERT ((Alignment & (Alignment - 1)) == 0);\r | |
576 | \r | |
577 | if (Alignment == 0) {\r | |
578 | AlignmentMask = Alignment;\r | |
579 | } else {\r | |
580 | AlignmentMask = Alignment - 1;\r | |
581 | }\r | |
a3657e3e | 582 | //\r |
583 | // Make sure that AllocationSize plus AlignmentMask does not overflow.\r | |
584 | //\r | |
585 | ASSERT (AllocationSize <= (MAX_ADDRESS - AlignmentMask));\r | |
878ddf1f | 586 | \r |
587 | RawAddress = InternalAllocatePool (PoolType, AllocationSize + AlignmentMask);\r | |
588 | \r | |
589 | AlignedAddress = ((UINTN) RawAddress + AlignmentMask) & ~AlignmentMask;\r | |
590 | \r | |
591 | return (VOID *) AlignedAddress;\r | |
592 | }\r | |
593 | \r | |
594 | /**\r | |
595 | Allocates a buffer of type EfiBootServicesData at a specified alignment.\r | |
596 | \r | |
597 | @param AllocationSize The number of bytes to allocate.\r | |
598 | @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used.\r | |
599 | \r | |
600 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
601 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
602 | \r | |
603 | **/\r | |
604 | VOID *\r | |
605 | EFIAPI\r | |
606 | AllocateAlignedPool (\r | |
607 | IN UINTN AllocationSize,\r | |
608 | IN UINTN Alignment\r | |
609 | )\r | |
610 | {\r | |
611 | VOID *RawAddress;\r | |
612 | UINTN AlignedAddress;\r | |
613 | UINTN AlignmentMask;\r | |
614 | \r | |
615 | //\r | |
616 | // Alignment must be a power of two or zero.\r | |
617 | //\r | |
618 | ASSERT ((Alignment & (Alignment - 1)) == 0);\r | |
619 | \r | |
620 | if (Alignment == 0) {\r | |
621 | AlignmentMask = Alignment;\r | |
622 | } else {\r | |
623 | AlignmentMask = Alignment - 1;\r | |
624 | }\r | |
625 | \r | |
a3657e3e | 626 | //\r |
627 | // Make sure that AllocationSize plus AlignmentMask does not overflow.\r | |
628 | //\r | |
629 | ASSERT (AllocationSize <= (MAX_ADDRESS - AlignmentMask));\r | |
630 | \r | |
878ddf1f | 631 | RawAddress = AllocatePool (AllocationSize + AlignmentMask);\r |
632 | \r | |
633 | AlignedAddress = ((UINTN) RawAddress + AlignmentMask) & ~AlignmentMask;\r | |
634 | \r | |
635 | return (VOID *) AlignedAddress;\r | |
636 | }\r | |
637 | \r | |
638 | /**\r | |
639 | Allocates a buffer of type EfiRuntimeServicesData at a specified alignment.\r | |
640 | \r | |
641 | @param AllocationSize The number of bytes to allocate.\r | |
642 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
643 | If Alignment is zero, then byte alignment is used.\r | |
644 | \r | |
645 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
646 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
647 | \r | |
648 | **/\r | |
649 | VOID *\r | |
650 | EFIAPI\r | |
651 | AllocateAlignedRuntimePool (\r | |
652 | IN UINTN AllocationSize,\r | |
653 | IN UINTN Alignment\r | |
654 | )\r | |
655 | {\r | |
656 | return InternalAllocateAlignedPool (EfiRuntimeServicesData, AllocationSize, Alignment);\r | |
657 | }\r | |
658 | \r | |
659 | /**\r | |
660 | Allocates a buffer of type EfiReservedMemoryType at a specified alignment.\r | |
661 | \r | |
662 | @param AllocationSize The number of bytes to allocate.\r | |
663 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
664 | If Alignment is zero, then byte alignment is used.\r | |
665 | \r | |
666 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
667 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
668 | \r | |
669 | **/\r | |
670 | VOID *\r | |
671 | EFIAPI\r | |
672 | AllocateAlignedReservedPool (\r | |
673 | IN UINTN AllocationSize,\r | |
674 | IN UINTN Alignment\r | |
675 | )\r | |
676 | {\r | |
677 | return InternalAllocateAlignedPool (EfiReservedMemoryType, AllocationSize, Alignment);\r | |
678 | }\r | |
679 | \r | |
680 | /**\r | |
681 | Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.\r | |
682 | \r | |
683 | @param PoolType The type of pool to allocate.\r | |
684 | @param AllocationSize The number of bytes to allocate.\r | |
685 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
686 | If Alignment is zero, then byte alignment is used.\r | |
687 | \r | |
688 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
689 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
690 | \r | |
691 | **/\r | |
692 | VOID *\r | |
693 | InternalAllocateAlignedZeroPool (\r | |
694 | IN EFI_MEMORY_TYPE PoolType,\r | |
695 | IN UINTN AllocationSize,\r | |
696 | IN UINTN Alignment\r | |
697 | )\r | |
698 | {\r | |
699 | VOID *Memory;\r | |
700 | \r | |
701 | Memory = InternalAllocateAlignedPool (PoolType, AllocationSize, Alignment);\r | |
702 | if (Memory != NULL) {\r | |
703 | Memory = ZeroMem (Memory, AllocationSize);\r | |
704 | }\r | |
705 | return Memory;\r | |
706 | }\r | |
707 | \r | |
708 | /**\r | |
709 | Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.\r | |
710 | \r | |
711 | @param AllocationSize The number of bytes to allocate.\r | |
712 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
713 | If Alignment is zero, then byte alignment is used.\r | |
714 | \r | |
715 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
716 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
717 | \r | |
718 | **/\r | |
719 | VOID *\r | |
720 | EFIAPI\r | |
721 | AllocateAlignedZeroPool (\r | |
722 | IN UINTN AllocationSize,\r | |
723 | IN UINTN Alignment\r | |
724 | )\r | |
725 | {\r | |
726 | VOID *Memory;\r | |
727 | \r | |
728 | Memory = AllocateAlignedPool (AllocationSize, Alignment);\r | |
729 | if (Memory != NULL) {\r | |
730 | Memory = ZeroMem (Memory, AllocationSize);\r | |
731 | }\r | |
732 | return Memory;\r | |
733 | }\r | |
734 | \r | |
735 | /**\r | |
736 | Allocates and zeros a buffer of type EfiRuntimeServicesData at a specified alignment.\r | |
737 | \r | |
738 | @param AllocationSize The number of bytes to allocate.\r | |
739 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
740 | If Alignment is zero, then byte alignment is used.\r | |
741 | \r | |
742 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
743 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
744 | \r | |
745 | **/\r | |
746 | VOID *\r | |
747 | EFIAPI\r | |
748 | AllocateAlignedRuntimeZeroPool (\r | |
749 | IN UINTN AllocationSize,\r | |
750 | IN UINTN Alignment\r | |
751 | )\r | |
752 | {\r | |
753 | return InternalAllocateAlignedZeroPool (EfiRuntimeServicesData, AllocationSize, Alignment);\r | |
754 | }\r | |
755 | \r | |
756 | /**\r | |
757 | Allocates and zeros a buffer of type EfiReservedMemoryType at a specified alignment.\r | |
758 | \r | |
759 | @param AllocationSize The number of bytes to allocate.\r | |
760 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
761 | If Alignment is zero, then byte alignment is used.\r | |
762 | \r | |
763 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
764 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
765 | \r | |
766 | **/\r | |
767 | VOID *\r | |
768 | EFIAPI\r | |
769 | AllocateAlignedReservedZeroPool (\r | |
770 | IN UINTN AllocationSize,\r | |
771 | IN UINTN Alignment\r | |
772 | )\r | |
773 | {\r | |
774 | return InternalAllocateAlignedZeroPool (EfiReservedMemoryType, AllocationSize, Alignment);\r | |
775 | }\r | |
776 | \r | |
777 | /**\r | |
778 | Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment.\r | |
779 | \r | |
780 | @param PoolType The type of pool to allocate.\r | |
781 | @param AllocationSize The number of bytes to allocate.\r | |
782 | @param Buffer The buffer to copy to the allocated buffer.\r | |
783 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
784 | If Alignment is zero, then byte alignment is used.\r | |
785 | \r | |
786 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
787 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
788 | \r | |
789 | **/\r | |
790 | VOID *\r | |
791 | InternalAllocateAlignedCopyPool (\r | |
792 | IN EFI_MEMORY_TYPE PoolType,\r | |
793 | IN UINTN AllocationSize,\r | |
794 | IN CONST VOID *Buffer,\r | |
795 | IN UINTN Alignment\r | |
796 | )\r | |
797 | {\r | |
798 | VOID *Memory;\r | |
799 | \r | |
9a462b41 | 800 | ASSERT (Buffer != NULL);\r |
801 | ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r | |
802 | \r | |
878ddf1f | 803 | Memory = InternalAllocateAlignedPool (PoolType, AllocationSize, Alignment);\r |
804 | if (Memory != NULL) {\r | |
805 | Memory = CopyMem (Memory, Buffer, AllocationSize);\r | |
806 | }\r | |
807 | return Memory;\r | |
808 | }\r | |
809 | \r | |
810 | /**\r | |
811 | Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment.\r | |
812 | \r | |
813 | @param AllocationSize The number of bytes to allocate.\r | |
814 | @param Buffer The buffer to copy to the allocated buffer.\r | |
815 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
816 | If Alignment is zero, then byte alignment is used.\r | |
817 | \r | |
818 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
819 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
820 | \r | |
821 | **/\r | |
822 | VOID *\r | |
823 | EFIAPI\r | |
824 | AllocateAlignedCopyPool (\r | |
825 | IN UINTN AllocationSize,\r | |
826 | IN CONST VOID *Buffer,\r | |
827 | IN UINTN Alignment\r | |
828 | )\r | |
829 | {\r | |
830 | VOID *Memory;\r | |
831 | \r | |
9a462b41 | 832 | ASSERT (Buffer != NULL);\r |
833 | ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r | |
834 | \r | |
878ddf1f | 835 | Memory = AllocateAlignedPool (AllocationSize, Alignment);\r |
836 | if (Memory != NULL) {\r | |
837 | Memory = CopyMem (Memory, Buffer, AllocationSize);\r | |
838 | }\r | |
839 | return Memory;\r | |
840 | }\r | |
841 | \r | |
842 | /**\r | |
843 | Copies a buffer to an allocated buffer of type EfiRuntimeServicesData at a specified alignment.\r | |
844 | \r | |
845 | @param AllocationSize The number of bytes to allocate.\r | |
846 | @param Buffer The buffer to copy to the allocated buffer.\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. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
851 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
852 | \r | |
853 | **/\r | |
854 | VOID *\r | |
855 | EFIAPI\r | |
856 | AllocateAlignedRuntimeCopyPool (\r | |
857 | IN UINTN AllocationSize,\r | |
858 | IN CONST VOID *Buffer,\r | |
859 | IN UINTN Alignment\r | |
860 | )\r | |
861 | {\r | |
862 | return InternalAllocateAlignedCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer, Alignment);\r | |
863 | }\r | |
864 | \r | |
865 | /**\r | |
866 | Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment.\r | |
867 | \r | |
868 | @param AllocationSize The number of bytes to allocate.\r | |
869 | @param Buffer The buffer to copy to the allocated buffer.\r | |
870 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
871 | If Alignment is zero, then byte alignment is used.\r | |
872 | \r | |
873 | @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.\r | |
874 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
875 | \r | |
876 | **/\r | |
877 | VOID *\r | |
878 | EFIAPI\r | |
879 | AllocateAlignedReservedCopyPool (\r | |
880 | IN UINTN AllocationSize,\r | |
881 | IN CONST VOID *Buffer,\r | |
882 | IN UINTN Alignment\r | |
883 | )\r | |
884 | {\r | |
885 | return InternalAllocateAlignedCopyPool (EfiReservedMemoryType, AllocationSize, Buffer, Alignment);\r | |
886 | }\r | |
887 | \r | |
888 | /**\r | |
889 | Frees a buffer that was previously allocated with one of the aligned pool allocation functions \r | |
890 | in the Memory Allocation Library.\r | |
891 | \r | |
892 | @param Buffer Pointer to the buffer to free.\r | |
893 | \r | |
894 | **/\r | |
895 | VOID\r | |
896 | EFIAPI\r | |
897 | FreeAlignedPool (\r | |
898 | IN VOID *Buffer\r | |
899 | )\r | |
900 | {\r | |
901 | //\r | |
902 | // PEI phase does not support to free pool, so leave it as NOP.\r | |
903 | //\r | |
904 | }\r |