]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Include/Library/MemoryAllocationLib.h
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Include / Library / MemoryAllocationLib.h
CommitLineData
fb3df220 1/** @file\r
50a64e5b 2 Provides services to allocate and free memory buffers of various memory types and alignments.\r
9095d37b
LG
3\r
4 The Memory Allocation Library abstracts various common memory allocation operations. This library\r
5 allows code to be written in a phase-independent manner because the allocation of memory in PEI, DXE,\r
6 and SMM (for example) is done via a different mechanism. Using a common library interface makes it\r
7 much easier to port algorithms from phase to phase.\r
8\r
9Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
9344f092 10SPDX-License-Identifier: BSD-2-Clause-Patent\r
fb3df220 11\r
fb3df220 12**/\r
13\r
14#ifndef __MEMORY_ALLOCATION_LIB_H__\r
15#define __MEMORY_ALLOCATION_LIB_H__\r
16\r
17/**\r
18 Allocates one or more 4KB pages of type EfiBootServicesData.\r
19\r
20 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the\r
21 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
22 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
23 returned.\r
24\r
25 @param Pages The number of 4 KB pages to allocate.\r
26\r
27 @return A pointer to the allocated buffer or NULL if allocation fails.\r
28\r
29**/\r
30VOID *\r
31EFIAPI\r
32AllocatePages (\r
33 IN UINTN Pages\r
34 );\r
35\r
36/**\r
37 Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r
38\r
39 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
40 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
41 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
42 returned.\r
43\r
44 @param Pages The number of 4 KB pages to allocate.\r
45\r
46 @return A pointer to the allocated buffer or NULL if allocation fails.\r
47\r
48**/\r
49VOID *\r
50EFIAPI\r
51AllocateRuntimePages (\r
52 IN UINTN Pages\r
53 );\r
54\r
55/**\r
56 Allocates one or more 4KB pages of type EfiReservedMemoryType.\r
57\r
58 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the\r
59 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
60 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
61 returned.\r
62\r
63 @param Pages The number of 4 KB pages to allocate.\r
64\r
65 @return A pointer to the allocated buffer or NULL if allocation fails.\r
66\r
67**/\r
68VOID *\r
69EFIAPI\r
70AllocateReservedPages (\r
71 IN UINTN Pages\r
72 );\r
73\r
74/**\r
75 Frees one or more 4KB pages that were previously allocated with one of the page allocation\r
76 functions in the Memory Allocation Library.\r
77\r
78 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
79 must have been allocated on a previous call to the page allocation services of the Memory\r
6e10b70a 80 Allocation Library. If it is not possible to free allocated pages, then this function will\r
446b94b0 81 perform no actions.\r
9095d37b 82\r
fb3df220 83 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r
84 then ASSERT().\r
85 If Pages is zero, then ASSERT().\r
9095d37b 86\r
fb3df220 87 @param Buffer Pointer to the buffer of pages to free.\r
88 @param Pages The number of 4 KB pages to free.\r
89\r
90**/\r
91VOID\r
92EFIAPI\r
93FreePages (\r
94 IN VOID *Buffer,\r
95 IN UINTN Pages\r
96 );\r
97\r
98/**\r
99 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
100\r
101 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
102 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
103 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
104 request, then NULL is returned.\r
9095d37b 105\r
fb3df220 106 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
1346352d 107 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
fb3df220 108\r
109 @param Pages The number of 4 KB pages to allocate.\r
110 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
111 If Alignment is zero, then byte alignment is used.\r
112\r
113 @return A pointer to the allocated buffer or NULL if allocation fails.\r
114\r
115**/\r
116VOID *\r
117EFIAPI\r
118AllocateAlignedPages (\r
119 IN UINTN Pages,\r
120 IN UINTN Alignment\r
121 );\r
122\r
123/**\r
124 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.\r
125\r
126 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an\r
127 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
128 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
129 request, then NULL is returned.\r
9095d37b 130\r
fb3df220 131 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
1346352d 132 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
fb3df220 133\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 A pointer to the allocated buffer or NULL if allocation fails.\r
139\r
140**/\r
141VOID *\r
142EFIAPI\r
143AllocateAlignedRuntimePages (\r
144 IN UINTN Pages,\r
145 IN UINTN Alignment\r
146 );\r
147\r
148/**\r
149 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.\r
150\r
151 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an\r
152 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
153 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
154 request, then NULL is returned.\r
9095d37b 155\r
fb3df220 156 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
1346352d 157 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
fb3df220 158\r
159 @param Pages The number of 4 KB pages to allocate.\r
160 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
161 If Alignment is zero, then byte alignment is used.\r
162\r
163 @return A pointer to the allocated buffer or NULL if allocation fails.\r
164\r
165**/\r
166VOID *\r
167EFIAPI\r
168AllocateAlignedReservedPages (\r
169 IN UINTN Pages,\r
170 IN UINTN Alignment\r
171 );\r
172\r
173/**\r
174 Frees one or more 4KB pages that were previously allocated with one of the aligned page\r
175 allocation functions in the Memory Allocation Library.\r
176\r
177 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
178 must have been allocated on a previous call to the aligned page allocation services of the Memory\r
9095d37b 179 Allocation Library. If it is not possible to free allocated pages, then this function will\r
446b94b0 180 perform no actions.\r
9095d37b 181\r
fb3df220 182 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation\r
183 Library, then ASSERT().\r
184 If Pages is zero, then ASSERT().\r
9095d37b 185\r
fb3df220 186 @param Buffer Pointer to the buffer of pages to free.\r
187 @param Pages The number of 4 KB pages to free.\r
188\r
189**/\r
190VOID\r
191EFIAPI\r
192FreeAlignedPages (\r
193 IN VOID *Buffer,\r
194 IN UINTN Pages\r
195 );\r
196\r
197/**\r
198 Allocates a buffer of type EfiBootServicesData.\r
199\r
200 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
201 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
202 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
203\r
204 @param AllocationSize The number of bytes to allocate.\r
205\r
206 @return A pointer to the allocated buffer or NULL if allocation fails.\r
207\r
208**/\r
209VOID *\r
210EFIAPI\r
211AllocatePool (\r
212 IN UINTN AllocationSize\r
213 );\r
214\r
215/**\r
216 Allocates a buffer of type EfiRuntimeServicesData.\r
217\r
218 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns\r
219 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
220 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
221\r
222 @param AllocationSize The number of bytes to allocate.\r
223\r
224 @return A pointer to the allocated buffer or NULL if allocation fails.\r
225\r
226**/\r
227VOID *\r
228EFIAPI\r
229AllocateRuntimePool (\r
230 IN UINTN AllocationSize\r
231 );\r
232\r
233/**\r
8789c5e0 234 Allocates a buffer of type EfiReservedMemoryType.\r
fb3df220 235\r
8789c5e0 236 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns\r
fb3df220 237 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
238 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
239\r
240 @param AllocationSize The number of bytes to allocate.\r
241\r
242 @return A pointer to the allocated buffer or NULL if allocation fails.\r
243\r
244**/\r
245VOID *\r
246EFIAPI\r
247AllocateReservedPool (\r
248 IN UINTN AllocationSize\r
249 );\r
250\r
251/**\r
252 Allocates and zeros a buffer of type EfiBootServicesData.\r
253\r
254 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
255 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
256 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
257 request, then NULL is returned.\r
258\r
259 @param AllocationSize The number of bytes to allocate and zero.\r
260\r
261 @return A pointer to the allocated buffer or NULL if allocation fails.\r
262\r
263**/\r
264VOID *\r
265EFIAPI\r
266AllocateZeroPool (\r
267 IN UINTN AllocationSize\r
268 );\r
269\r
270/**\r
271 Allocates and zeros a buffer of type EfiRuntimeServicesData.\r
272\r
273 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the\r
274 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
275 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
276 request, then NULL is returned.\r
277\r
278 @param AllocationSize The number of bytes to allocate and zero.\r
279\r
280 @return A pointer to the allocated buffer or NULL if allocation fails.\r
281\r
282**/\r
283VOID *\r
284EFIAPI\r
285AllocateRuntimeZeroPool (\r
286 IN UINTN AllocationSize\r
287 );\r
288\r
289/**\r
290 Allocates and zeros a buffer of type EfiReservedMemoryType.\r
291\r
292 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the\r
293 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
294 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
295 request, then NULL is returned.\r
296\r
297 @param AllocationSize The number of bytes to allocate and zero.\r
298\r
299 @return A pointer to the allocated buffer or NULL if allocation fails.\r
300\r
301**/\r
302VOID *\r
303EFIAPI\r
304AllocateReservedZeroPool (\r
305 IN UINTN AllocationSize\r
306 );\r
307\r
308/**\r
309 Copies a buffer to an allocated buffer of type EfiBootServicesData.\r
310\r
311 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r
312 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
313 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
314 is not enough memory remaining to satisfy the request, then NULL is returned.\r
9095d37b 315\r
fb3df220 316 If Buffer is NULL, then ASSERT().\r
9095d37b 317 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
fb3df220 318\r
319 @param AllocationSize The number of bytes to allocate and zero.\r
320 @param Buffer The buffer to copy to the allocated buffer.\r
321\r
322 @return A pointer to the allocated buffer or NULL if allocation fails.\r
323\r
324**/\r
325VOID *\r
326EFIAPI\r
327AllocateCopyPool (\r
328 IN UINTN AllocationSize,\r
329 IN CONST VOID *Buffer\r
330 );\r
331\r
332/**\r
333 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r
334\r
335 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies\r
336 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
337 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
338 is not enough memory remaining to satisfy the request, then NULL is returned.\r
9095d37b 339\r
fb3df220 340 If Buffer is NULL, then ASSERT().\r
9095d37b 341 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
fb3df220 342\r
343 @param AllocationSize The number of bytes to allocate and zero.\r
344 @param Buffer The buffer to copy to the allocated buffer.\r
345\r
346 @return A pointer to the allocated buffer or NULL if allocation fails.\r
347\r
348**/\r
349VOID *\r
350EFIAPI\r
351AllocateRuntimeCopyPool (\r
352 IN UINTN AllocationSize,\r
353 IN CONST VOID *Buffer\r
354 );\r
355\r
356/**\r
357 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.\r
358\r
359 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies\r
360 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
361 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
362 is not enough memory remaining to satisfy the request, then NULL is returned.\r
9095d37b 363\r
fb3df220 364 If Buffer is NULL, then ASSERT().\r
9095d37b 365 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
fb3df220 366\r
367 @param AllocationSize The number of bytes to allocate and zero.\r
368 @param Buffer The buffer to copy to the allocated buffer.\r
369\r
370 @return A pointer to the allocated buffer or NULL if allocation fails.\r
371\r
372**/\r
373VOID *\r
374EFIAPI\r
375AllocateReservedCopyPool (\r
376 IN UINTN AllocationSize,\r
377 IN CONST VOID *Buffer\r
378 );\r
379\r
c2390431 380/**\r
381 Reallocates a buffer of type EfiBootServicesData.\r
382\r
ab95d5f2 383 Allocates and zeros the number bytes specified by NewSize from memory of type\r
9095d37b
LG
384 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and\r
385 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
386 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
387 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
c2390431 388 enough memory remaining to satisfy the request, then NULL is returned.\r
9095d37b 389\r
56304569 390 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
391 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
c2390431 392\r
393 @param OldSize The size, in bytes, of OldBuffer.\r
394 @param NewSize The size, in bytes, of the buffer to reallocate.\r
9095d37b 395 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
c2390431 396 parameter that may be NULL.\r
397\r
398 @return A pointer to the allocated buffer or NULL if allocation fails.\r
399\r
400**/\r
401VOID *\r
402EFIAPI\r
403ReallocatePool (\r
404 IN UINTN OldSize,\r
405 IN UINTN NewSize,\r
406 IN VOID *OldBuffer OPTIONAL\r
407 );\r
408\r
409/**\r
410 Reallocates a buffer of type EfiRuntimeServicesData.\r
411\r
ab95d5f2 412 Allocates and zeros the number bytes specified by NewSize from memory of type\r
9095d37b
LG
413 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and\r
414 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
415 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
416 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
c2390431 417 enough memory remaining to satisfy the request, then NULL is returned.\r
2297186d 418\r
56304569 419 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
420 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
c2390431 421\r
422 @param OldSize The size, in bytes, of OldBuffer.\r
423 @param NewSize The size, in bytes, of the buffer to reallocate.\r
9095d37b 424 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
c2390431 425 parameter that may be NULL.\r
426\r
427 @return A pointer to the allocated buffer or NULL if allocation fails.\r
428\r
429**/\r
430VOID *\r
431EFIAPI\r
432ReallocateRuntimePool (\r
433 IN UINTN OldSize,\r
434 IN UINTN NewSize,\r
435 IN VOID *OldBuffer OPTIONAL\r
436 );\r
437\r
438/**\r
439 Reallocates a buffer of type EfiReservedMemoryType.\r
440\r
ab95d5f2 441 Allocates and zeros the number bytes specified by NewSize from memory of type\r
9095d37b
LG
442 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and\r
443 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
444 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
445 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
c2390431 446 enough memory remaining to satisfy the request, then NULL is returned.\r
2297186d 447\r
56304569 448 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
449 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
c2390431 450\r
451 @param OldSize The size, in bytes, of OldBuffer.\r
452 @param NewSize The size, in bytes, of the buffer to reallocate.\r
9095d37b 453 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
c2390431 454 parameter that may be NULL.\r
455\r
456 @return A pointer to the allocated buffer or NULL if allocation fails.\r
457\r
458**/\r
459VOID *\r
460EFIAPI\r
461ReallocateReservedPool (\r
462 IN UINTN OldSize,\r
463 IN UINTN NewSize,\r
464 IN VOID *OldBuffer OPTIONAL\r
465 );\r
466\r
fb3df220 467/**\r
468 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
469 Memory Allocation Library.\r
470\r
471 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
6e10b70a 472 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
446b94b0 473 resources, then this function will perform no actions.\r
9095d37b 474\r
fb3df220 475 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
476 then ASSERT().\r
477\r
478 @param Buffer Pointer to the buffer to free.\r
479\r
480**/\r
481VOID\r
482EFIAPI\r
483FreePool (\r
484 IN VOID *Buffer\r
485 );\r
486\r
fb3df220 487#endif\r