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