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