]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiMemoryLib/MemLibGeneric.c
MdePkg: Replace BSD License with BSD+Patent License
[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
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
dd51a993 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
2fc59a00 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
dd51a993 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
dd51a993 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
2fc59a00 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
dd51a993 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
dd51a993 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
dd51a993 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
58380e9c 110 @param DestinationBuffer The first memory buffer\r
111 @param SourceBuffer The second memory buffer\r
112 @param Length The length of DestinationBuffer and SourceBuffer memory\r
dd51a993 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
dd51a993 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
2fc59a00 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
dd51a993 143\r
2fc59a00 144 @return The pointer to the first occurrence or NULL if not found.\r
dd51a993 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
159 if (*(Pointer++) == Value) {\r
00dbccf2 160 return --Pointer;\r
dd51a993 161 }\r
162 } while (--Length != 0);\r
163 return NULL;\r
164}\r
165\r
166/**\r
167 Scans a target buffer for a 16-bit value, and returns a pointer to the\r
168 matching 16-bit value in the target buffer.\r
169\r
2fc59a00 170 @param Buffer The pointer to the target buffer to scan.\r
171 @param Length The count of 16-bit value to scan. Must be non-zero.\r
172 @param Value The value to search for in the target buffer.\r
dd51a993 173\r
2fc59a00 174 @return The pointer to the first occurrence or NULL if not found.\r
dd51a993 175\r
176**/\r
177CONST VOID *\r
178EFIAPI\r
179InternalMemScanMem16 (\r
180 IN CONST VOID *Buffer,\r
181 IN UINTN Length,\r
182 IN UINT16 Value\r
183 )\r
184{\r
185 CONST UINT16 *Pointer;\r
186\r
187 Pointer = (CONST UINT16*)Buffer;\r
188 do {\r
189 if (*(Pointer++) == Value) {\r
00dbccf2 190 return --Pointer;\r
dd51a993 191 }\r
192 } while (--Length != 0);\r
193 return NULL;\r
194}\r
195\r
196/**\r
197 Scans a target buffer for a 32-bit value, and returns a pointer to the\r
198 matching 32-bit value in the target buffer.\r
199\r
2fc59a00 200 @param Buffer The pointer to the target buffer to scan.\r
201 @param Length The count of 32-bit value to scan. Must be non-zero.\r
202 @param Value The value to search for in the target buffer.\r
dd51a993 203\r
2fc59a00 204 @return The pointer to the first occurrence or NULL if not found.\r
dd51a993 205\r
206**/\r
207CONST VOID *\r
208EFIAPI\r
209InternalMemScanMem32 (\r
210 IN CONST VOID *Buffer,\r
211 IN UINTN Length,\r
212 IN UINT32 Value\r
213 )\r
214{\r
215 CONST UINT32 *Pointer;\r
216\r
217 Pointer = (CONST UINT32*)Buffer;\r
218 do {\r
219 if (*(Pointer++) == Value) {\r
00dbccf2 220 return --Pointer;\r
dd51a993 221 }\r
222 } while (--Length != 0);\r
223 return NULL;\r
224}\r
225\r
226/**\r
227 Scans a target buffer for a 64-bit value, and returns a pointer to the\r
228 matching 64-bit value in the target buffer.\r
229\r
2fc59a00 230 @param Buffer The pointer to the target buffer to scan.\r
231 @param Length The count of 64-bit value to scan. Must be non-zero.\r
232 @param Value The value to search for in the target buffer.\r
dd51a993 233\r
2fc59a00 234 @return The pointer to the first occurrence or NULL if not found.\r
dd51a993 235\r
236**/\r
237CONST VOID *\r
238EFIAPI\r
239InternalMemScanMem64 (\r
240 IN CONST VOID *Buffer,\r
241 IN UINTN Length,\r
242 IN UINT64 Value\r
243 )\r
244{\r
245 CONST UINT64 *Pointer;\r
246\r
247 Pointer = (CONST UINT64*)Buffer;\r
248 do {\r
249 if (*(Pointer++) == Value) {\r
00dbccf2 250 return --Pointer;\r
dd51a993 251 }\r
252 } while (--Length != 0);\r
253 return NULL;\r
254}\r
1944b02b
HW
255\r
256/**\r
257 Checks whether the contents of a buffer are all zeros.\r
258\r
259 @param Buffer The pointer to the buffer to be checked.\r
260 @param Length The size of the buffer (in bytes) to be checked.\r
261\r
262 @retval TRUE Contents of the buffer are all zeros.\r
263 @retval FALSE Contents of the buffer are not all zeros.\r
264\r
265**/\r
266BOOLEAN\r
267EFIAPI\r
268InternalMemIsZeroBuffer (\r
269 IN CONST VOID *Buffer,\r
270 IN UINTN Length\r
271 )\r
272{\r
273 CONST UINT8 *BufferData;\r
274 UINTN Index;\r
275\r
276 BufferData = Buffer;\r
277 for (Index = 0; Index < Length; Index++) {\r
278 if (BufferData[Index] != 0) {\r
279 return FALSE;\r
280 }\r
281 }\r
282 return TRUE;\r
283}\r