]> git.proxmox.com Git - mirror_edk2.git/blame - EmulatorPkg/Win/Host/WinMemoryAllocationLib.c
StandaloneMmPkg/Core/Dispatcher: don't copy dispatched image twice
[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
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <Base.h>\r
16#include <Library/BaseMemoryLib.h>\r
17#include <Library/MemoryAllocationLib.h>\r
18\r
19#include <stdlib.h>\r
20\r
21/**\r
22 Allocates a buffer of type EfiBootServicesData.\r
23\r
24 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
25 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
26 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
27\r
28 @param AllocationSize The number of bytes to allocate.\r
29\r
30 @return A pointer to the allocated buffer or NULL if allocation fails.\r
31\r
32**/\r
33VOID *\r
34EFIAPI\r
35AllocatePool (\r
36 IN UINTN AllocationSize\r
37 )\r
38{\r
39 return (VOID*) malloc (AllocationSize);\r
40}\r
41\r
42\r
43/**\r
44 Allocates and zeros a buffer of type EfiBootServicesData.\r
45\r
46 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
47 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
48 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
49 request, then NULL is returned.\r
50\r
51 @param AllocationSize The number of bytes to allocate and zero.\r
52\r
53 @return A pointer to the allocated buffer or NULL if allocation fails.\r
54\r
55**/\r
56VOID *\r
57EFIAPI\r
58AllocateZeroPool (\r
59 IN UINTN AllocationSize\r
60 )\r
61{\r
62 VOID *Buffer;\r
63\r
64 Buffer = AllocatePool (AllocationSize);\r
65 if (Buffer == NULL) {\r
66 return NULL;\r
67 }\r
68\r
69 ZeroMem (Buffer, AllocationSize);\r
70\r
71 return Buffer;\r
72}\r
73\r
74\r
75/**\r
76 Reallocates a buffer of type EfiBootServicesData.\r
77\r
78 Allocates and zeros the number bytes specified by NewSize from memory of type\r
79 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and\r
80 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
81 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
82 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
83 enough memory remaining to satisfy the request, then NULL is returned.\r
84\r
85 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
86 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
87\r
88 @param OldSize The size, in bytes, of OldBuffer.\r
89 @param NewSize The size, in bytes, of the buffer to reallocate.\r
90 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
91 parameter that may be NULL.\r
92\r
93 @return A pointer to the allocated buffer or NULL if allocation fails.\r
94\r
95**/\r
96VOID *\r
97EFIAPI\r
98ReallocatePool (\r
99 IN UINTN OldSize,\r
100 IN UINTN NewSize,\r
101 IN VOID *OldBuffer OPTIONAL\r
102 )\r
103{\r
104 VOID *NewBuffer;\r
105\r
106 NewBuffer = AllocatePool (NewSize);\r
107 if (NewBuffer == NULL) {\r
108 return NULL;\r
109 }\r
110\r
111 if (OldBuffer != NULL) {\r
112 if (OldSize > 0) {\r
113 CopyMem (NewBuffer, OldBuffer, OldSize);\r
114 }\r
115\r
116 FreePool (OldBuffer);\r
117 }\r
118\r
119 return NewBuffer;\r
120}\r
121\r
122/**\r
123 Copies a buffer to an allocated buffer of type EfiBootServicesData.\r
124\r
125 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r
126 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
127 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
128 is not enough memory remaining to satisfy the request, then NULL is returned.\r
129\r
130 If Buffer is NULL, then ASSERT().\r
131 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
132\r
133 @param AllocationSize The number of bytes to allocate and zero.\r
134 @param Buffer The buffer to copy to the allocated buffer.\r
135\r
136 @return A pointer to the allocated buffer or NULL if allocation fails.\r
137\r
138**/\r
139VOID *\r
140EFIAPI\r
141AllocateCopyPool (\r
142 IN UINTN AllocationSize,\r
143 IN CONST VOID *Buffer\r
144 )\r
145{\r
146 VOID *Memory;\r
147\r
148 Memory = AllocatePool (AllocationSize);\r
149 if (Memory != NULL) {\r
150 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
151 }\r
152 return Memory;\r
153}\r
154\r
155\r
156/**\r
157 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
158 Memory Allocation Library.\r
159\r
160 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
161 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
162 resources, then this function will perform no actions.\r
163\r
164 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
165 then ASSERT().\r
166\r
167 @param Buffer Pointer to the buffer to free.\r
168\r
169**/\r
170VOID\r
171EFIAPI\r
172FreePool (\r
173 IN VOID *Buffer\r
174 )\r
175{\r
176 free ((void *) Buffer);\r
177}\r
178\r