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