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