]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseMemoryLib/MemLibGeneric.c
MdePkg/UefiDebugLibDebugPortProtocol: Add destructor to CloseEvent
[mirror_edk2.git] / MdePkg / Library / BaseMemoryLib / MemLibGeneric.c
CommitLineData
e1f414b6 1/** @file\r
2 Architecture Independent Base Memory Library Implementation.\r
3\r
d531bfee 4 The following BaseMemoryLib instances contain the same copy of this file:\r
5 BaseMemoryLib\r
6 PeiMemoryLib\r
1fef058f 7 UefiMemoryLib\r
d531bfee 8\r
1944b02b 9 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
9344f092 10 SPDX-License-Identifier: BSD-2-Clause-Patent\r
e1f414b6 11\r
e1f414b6 12**/\r
13\r
e1f414b6 14#include "MemLibInternals.h"\r
15\r
16/**\r
17 Fills a target buffer with a 16-bit value, and returns the target buffer.\r
18\r
15c952e7 19 @param Buffer The pointer to the target buffer to fill.\r
20 @param Length The count of 16-bit value to fill.\r
21 @param Value The value with which to fill Length bytes of Buffer.\r
e1f414b6 22\r
23 @return Buffer\r
24\r
25**/\r
26VOID *\r
27EFIAPI\r
28InternalMemSetMem16 (\r
29 OUT VOID *Buffer,\r
30 IN UINTN Length,\r
31 IN UINT16 Value\r
32 )\r
33{\r
9088c61e
HW
34 for (; Length != 0; Length--) {\r
35 ((UINT16*)Buffer)[Length - 1] = Value;\r
36 }\r
e1f414b6 37 return Buffer;\r
38}\r
39\r
40/**\r
41 Fills a target buffer with a 32-bit value, and returns the target buffer.\r
42\r
15c952e7 43 @param Buffer The pointer to the target buffer to fill.\r
44 @param Length The count of 32-bit value to fill.\r
45 @param Value The value with which to fill Length bytes of Buffer.\r
e1f414b6 46\r
47 @return Buffer\r
48\r
49**/\r
50VOID *\r
51EFIAPI\r
52InternalMemSetMem32 (\r
53 OUT VOID *Buffer,\r
54 IN UINTN Length,\r
55 IN UINT32 Value\r
56 )\r
57{\r
9088c61e
HW
58 for (; Length != 0; Length--) {\r
59 ((UINT32*)Buffer)[Length - 1] = Value;\r
60 }\r
e1f414b6 61 return Buffer;\r
62}\r
63\r
64/**\r
65 Fills a target buffer with a 64-bit value, and returns the target buffer.\r
66\r
15c952e7 67 @param Buffer The pointer to the target buffer to fill.\r
68 @param Length The count of 64-bit value to fill.\r
69 @param Value The value with which to fill Length bytes of Buffer.\r
e1f414b6 70\r
71 @return Buffer\r
72\r
73**/\r
74VOID *\r
75EFIAPI\r
76InternalMemSetMem64 (\r
77 OUT VOID *Buffer,\r
78 IN UINTN Length,\r
79 IN UINT64 Value\r
80 )\r
81{\r
9088c61e
HW
82 for (; Length != 0; Length--) {\r
83 ((UINT64*)Buffer)[Length - 1] = Value;\r
84 }\r
e1f414b6 85 return Buffer;\r
86}\r
87\r
88/**\r
89 Set Buffer to 0 for Size bytes.\r
90\r
91 @param Buffer Memory to set.\r
2fc59a00 92 @param Length The number of bytes to set.\r
e1f414b6 93\r
94 @return Buffer\r
95\r
96**/\r
97VOID *\r
98EFIAPI\r
99InternalMemZeroMem (\r
100 OUT VOID *Buffer,\r
101 IN UINTN Length\r
102 )\r
103{\r
104 return InternalMemSetMem (Buffer, Length, 0);\r
105}\r
106\r
107/**\r
108 Compares two memory buffers of a given length.\r
109\r
15c952e7 110 @param DestinationBuffer The first memory buffer.\r
111 @param SourceBuffer The second memory buffer.\r
e1f414b6 112 @param Length Length of DestinationBuffer and SourceBuffer memory\r
113 regions to compare. Must be non-zero.\r
114\r
d531bfee 115 @return 0 All Length bytes of the two buffers are identical.\r
116 @retval Non-zero The first mismatched byte in SourceBuffer subtracted from the first\r
117 mismatched byte in DestinationBuffer.\r
e1f414b6 118\r
119**/\r
120INTN\r
121EFIAPI\r
122InternalMemCompareMem (\r
123 IN CONST VOID *DestinationBuffer,\r
124 IN CONST VOID *SourceBuffer,\r
125 IN UINTN Length\r
126 )\r
127{\r
128 while ((--Length != 0) &&\r
129 (*(INT8*)DestinationBuffer == *(INT8*)SourceBuffer)) {\r
130 DestinationBuffer = (INT8*)DestinationBuffer + 1;\r
131 SourceBuffer = (INT8*)SourceBuffer + 1;\r
132 }\r
133 return (INTN)*(UINT8*)DestinationBuffer - (INTN)*(UINT8*)SourceBuffer;\r
134}\r
135\r
136/**\r
137 Scans a target buffer for an 8-bit value, and returns a pointer to the\r
138 matching 8-bit value in the target buffer.\r
139\r
15c952e7 140 @param Buffer The pointer to the target buffer to scan.\r
141 @param Length The count of 8-bit value to scan. Must be non-zero.\r
142 @param Value The value to search for in the target buffer.\r
e1f414b6 143\r
15c952e7 144 @return The pointer to the first occurrence, or NULL if not found.\r
e1f414b6 145\r
146**/\r
147CONST VOID *\r
148EFIAPI\r
149InternalMemScanMem8 (\r
150 IN CONST VOID *Buffer,\r
151 IN UINTN Length,\r
152 IN UINT8 Value\r
153 )\r
154{\r
155 CONST UINT8 *Pointer;\r
156\r
157 Pointer = (CONST UINT8*)Buffer;\r
158 do {\r
af072124 159 if (*Pointer == Value) {\r
160 return Pointer;\r
e1f414b6 161 }\r
af072124 162 ++Pointer;\r
e1f414b6 163 } while (--Length != 0);\r
164 return NULL;\r
165}\r
166\r
167/**\r
168 Scans a target buffer for a 16-bit value, and returns a pointer to the\r
169 matching 16-bit value in the target buffer.\r
170\r
15c952e7 171 @param Buffer The pointer to the target buffer to scan.\r
172 @param Length The count of 16-bit value to scan. Must be non-zero.\r
173 @param Value The value to search for in the target buffer.\r
e1f414b6 174\r
15c952e7 175 @return The pointer to the first occurrence, or NULL if not found.\r
e1f414b6 176\r
177**/\r
178CONST VOID *\r
179EFIAPI\r
180InternalMemScanMem16 (\r
181 IN CONST VOID *Buffer,\r
182 IN UINTN Length,\r
183 IN UINT16 Value\r
184 )\r
185{\r
186 CONST UINT16 *Pointer;\r
187\r
188 Pointer = (CONST UINT16*)Buffer;\r
189 do {\r
af072124 190 if (*Pointer == Value) {\r
191 return Pointer;\r
e1f414b6 192 }\r
af072124 193 ++Pointer;\r
e1f414b6 194 } while (--Length != 0);\r
195 return NULL;\r
196}\r
197\r
198/**\r
199 Scans a target buffer for a 32-bit value, and returns a pointer to the\r
200 matching 32-bit value in the target buffer.\r
201\r
15c952e7 202 @param Buffer The pointer to the target buffer to scan.\r
203 @param Length The count of 32-bit value to scan. Must be non-zero.\r
204 @param Value The value to search for in the target buffer.\r
e1f414b6 205\r
15c952e7 206 @return The pointer to the first occurrence, or NULL if not found.\r
e1f414b6 207\r
208**/\r
209CONST VOID *\r
210EFIAPI\r
211InternalMemScanMem32 (\r
212 IN CONST VOID *Buffer,\r
213 IN UINTN Length,\r
214 IN UINT32 Value\r
215 )\r
216{\r
217 CONST UINT32 *Pointer;\r
218\r
219 Pointer = (CONST UINT32*)Buffer;\r
220 do {\r
af072124 221 if (*Pointer == Value) {\r
222 return Pointer;\r
e1f414b6 223 }\r
af072124 224 ++Pointer;\r
e1f414b6 225 } while (--Length != 0);\r
226 return NULL;\r
227}\r
228\r
229/**\r
230 Scans a target buffer for a 64-bit value, and returns a pointer to the\r
231 matching 64-bit value in the target buffer.\r
232\r
15c952e7 233 @param Buffer The pointer to the target buffer to scan.\r
234 @param Length The count of 64-bit value to scan. Must be non-zero.\r
235 @param Value The value to search for in the target buffer.\r
e1f414b6 236\r
15c952e7 237 @return The pointer to the first occurrence, or NULL if not found.\r
e1f414b6 238\r
239**/\r
240CONST VOID *\r
241EFIAPI\r
242InternalMemScanMem64 (\r
243 IN CONST VOID *Buffer,\r
244 IN UINTN Length,\r
245 IN UINT64 Value\r
246 )\r
247{\r
248 CONST UINT64 *Pointer;\r
249\r
250 Pointer = (CONST UINT64*)Buffer;\r
251 do {\r
af072124 252 if (*Pointer == Value) {\r
253 return Pointer;\r
e1f414b6 254 }\r
af072124 255 ++Pointer;\r
e1f414b6 256 } while (--Length != 0);\r
257 return NULL;\r
258}\r
1944b02b
HW
259\r
260/**\r
261 Checks whether the contents of a buffer are all zeros.\r
262\r
263 @param Buffer The pointer to the buffer to be checked.\r
264 @param Length The size of the buffer (in bytes) to be checked.\r
265\r
266 @retval TRUE Contents of the buffer are all zeros.\r
267 @retval FALSE Contents of the buffer are not all zeros.\r
268\r
269**/\r
270BOOLEAN\r
271EFIAPI\r
272InternalMemIsZeroBuffer (\r
273 IN CONST VOID *Buffer,\r
274 IN UINTN Length\r
275 )\r
276{\r
277 CONST UINT8 *BufferData;\r
278 UINTN Index;\r
279\r
280 BufferData = Buffer;\r
281 for (Index = 0; Index < Length; Index++) {\r
282 if (BufferData[Index] != 0) {\r
283 return FALSE;\r
284 }\r
285 }\r
286 return TRUE;\r
287}\r