]> git.proxmox.com Git - mirror_edk2.git/blame - EmulatorPkg/Unix/Host/MemoryAllocationLib.c
EmulatorPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / EmulatorPkg / Unix / Host / MemoryAllocationLib.c
CommitLineData
79e4f2a5
RN
1/*++ @file\r
2\r
3 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
4\r
e3ba31da 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
79e4f2a5
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/**\r
118 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
119 Memory Allocation Library.\r
120\r
121 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
122 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
123 resources, then this function will perform no actions.\r
124\r
125 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
126 then ASSERT().\r
127\r
128 @param Buffer Pointer to the buffer to free.\r
129\r
130**/\r
131VOID\r
132EFIAPI\r
133FreePool (\r
134 IN VOID *Buffer\r
135 )\r
136{\r
137 free ((void *) Buffer);\r
138}\r
139\r