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