]> git.proxmox.com Git - mirror_edk2.git/blame - EmulatorPkg/Win/Host/WinMemoryAllocationLib.c
CryptoPkg/BaseCryptLib: Make HMAC_CTX size backward compatible
[mirror_edk2.git] / EmulatorPkg / Win / Host / WinMemoryAllocationLib.c
CommitLineData
3c859dfe
RN
1/*++ @file\r
2\r
3 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
4\r
e3ba31da 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
3c859dfe
RN
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/**\r
38 Allocates and zeros a buffer of type EfiBootServicesData.\r
39\r
40 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
41 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
42 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
43 request, then NULL is returned.\r
44\r
45 @param AllocationSize The number of bytes to allocate and zero.\r
46\r
47 @return A pointer to the allocated buffer or NULL if allocation fails.\r
48\r
49**/\r
50VOID *\r
51EFIAPI\r
52AllocateZeroPool (\r
53 IN UINTN AllocationSize\r
54 )\r
55{\r
56 VOID *Buffer;\r
57\r
58 Buffer = AllocatePool (AllocationSize);\r
59 if (Buffer == NULL) {\r
60 return NULL;\r
61 }\r
62\r
63 ZeroMem (Buffer, AllocationSize);\r
64\r
65 return Buffer;\r
66}\r
67\r
68\r
69/**\r
70 Reallocates a buffer of type EfiBootServicesData.\r
71\r
72 Allocates and zeros the number bytes specified by NewSize from memory of type\r
73 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and\r
74 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
75 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
76 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
77 enough memory remaining to satisfy the request, then NULL is returned.\r
78\r
79 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
80 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
81\r
82 @param OldSize The size, in bytes, of OldBuffer.\r
83 @param NewSize The size, in bytes, of the buffer to reallocate.\r
84 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
85 parameter that may be NULL.\r
86\r
87 @return A pointer to the allocated buffer or NULL if allocation fails.\r
88\r
89**/\r
90VOID *\r
91EFIAPI\r
92ReallocatePool (\r
93 IN UINTN OldSize,\r
94 IN UINTN NewSize,\r
95 IN VOID *OldBuffer OPTIONAL\r
96 )\r
97{\r
98 VOID *NewBuffer;\r
99\r
100 NewBuffer = AllocatePool (NewSize);\r
101 if (NewBuffer == NULL) {\r
102 return NULL;\r
103 }\r
104\r
105 if (OldBuffer != NULL) {\r
106 if (OldSize > 0) {\r
107 CopyMem (NewBuffer, OldBuffer, OldSize);\r
108 }\r
109\r
110 FreePool (OldBuffer);\r
111 }\r
112\r
113 return NewBuffer;\r
114}\r
115\r
116/**\r
117 Copies a buffer to an allocated buffer of type EfiBootServicesData.\r
118\r
119 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r
120 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
121 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
122 is not enough memory remaining to satisfy the request, then NULL is returned.\r
123\r
124 If Buffer is NULL, then ASSERT().\r
125 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
126\r
127 @param AllocationSize The number of bytes to allocate and zero.\r
128 @param Buffer The buffer to copy to the allocated buffer.\r
129\r
130 @return A pointer to the allocated buffer or NULL if allocation fails.\r
131\r
132**/\r
133VOID *\r
134EFIAPI\r
135AllocateCopyPool (\r
136 IN UINTN AllocationSize,\r
137 IN CONST VOID *Buffer\r
138 )\r
139{\r
140 VOID *Memory;\r
141\r
142 Memory = AllocatePool (AllocationSize);\r
143 if (Memory != NULL) {\r
144 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
145 }\r
146 return Memory;\r
147}\r
148\r
149\r
150/**\r
151 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
152 Memory Allocation Library.\r
153\r
154 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
155 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
156 resources, then this function will perform no actions.\r
157\r
158 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
159 then ASSERT().\r
160\r
161 @param Buffer Pointer to the buffer to free.\r
162\r
163**/\r
164VOID\r
165EFIAPI\r
166FreePool (\r
167 IN VOID *Buffer\r
168 )\r
169{\r
170 free ((void *) Buffer);\r
171}\r
172\r