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