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