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