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