]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - EmulatorPkg/Unix/Host/MemoryAllocationLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmulatorPkg / Unix / Host / MemoryAllocationLib.c
... / ...
CommitLineData
1/*++ @file\r
2\r
3 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
4\r
5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7**/\r
8\r
9#include "Base.h"\r
10#include "Library/BaseMemoryLib.h"\r
11#include "Library/MemoryAllocationLib.h"\r
12\r
13#include <stdlib.h>\r
14\r
15/**\r
16 Allocates a buffer of type EfiBootServicesData.\r
17\r
18 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
19 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
20 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
21\r
22 @param AllocationSize The number of bytes to allocate.\r
23\r
24 @return A pointer to the allocated buffer or NULL if allocation fails.\r
25\r
26**/\r
27VOID *\r
28EFIAPI\r
29AllocatePool (\r
30 IN UINTN AllocationSize\r
31 )\r
32{\r
33 return (VOID *)malloc (AllocationSize);\r
34}\r
35\r
36/**\r
37 Allocates and zeros a buffer of type EfiBootServicesData.\r
38\r
39 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
40 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
41 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
42 request, then NULL is returned.\r
43\r
44 @param AllocationSize The number of bytes to allocate and zero.\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
51AllocateZeroPool (\r
52 IN UINTN AllocationSize\r
53 )\r
54{\r
55 VOID *Buffer;\r
56\r
57 Buffer = AllocatePool (AllocationSize);\r
58 if (Buffer == NULL) {\r
59 return NULL;\r
60 }\r
61\r
62 ZeroMem (Buffer, AllocationSize);\r
63\r
64 return Buffer;\r
65}\r
66\r
67/**\r
68 Reallocates a buffer of type EfiBootServicesData.\r
69\r
70 Allocates and zeros the number bytes specified by NewSize from memory of type\r
71 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and\r
72 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
73 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
74 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
75 enough memory remaining to satisfy the request, then NULL is returned.\r
76\r
77 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
78 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
79\r
80 @param OldSize The size, in bytes, of OldBuffer.\r
81 @param NewSize The size, in bytes, of the buffer to reallocate.\r
82 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
83 parameter that may be NULL.\r
84\r
85 @return A pointer to the allocated buffer or NULL if allocation fails.\r
86\r
87**/\r
88VOID *\r
89EFIAPI\r
90ReallocatePool (\r
91 IN UINTN OldSize,\r
92 IN UINTN NewSize,\r
93 IN VOID *OldBuffer OPTIONAL\r
94 )\r
95{\r
96 VOID *NewBuffer;\r
97\r
98 NewBuffer = AllocatePool (NewSize);\r
99 if (NewBuffer == NULL) {\r
100 return NULL;\r
101 }\r
102\r
103 if (OldBuffer != NULL) {\r
104 if (OldSize > 0) {\r
105 CopyMem (NewBuffer, OldBuffer, OldSize);\r
106 }\r
107\r
108 FreePool (OldBuffer);\r
109 }\r
110\r
111 return NewBuffer;\r
112}\r
113\r
114/**\r
115 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
116 Memory Allocation Library.\r
117\r
118 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
119 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
120 resources, then this function will perform no actions.\r
121\r
122 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
123 then ASSERT().\r
124\r
125 @param Buffer Pointer to the buffer to free.\r
126\r
127**/\r
128VOID\r
129EFIAPI\r
130FreePool (\r
131 IN VOID *Buffer\r
132 )\r
133{\r
134 free ((void *)Buffer);\r
135}\r