]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseMemoryLib/MemLibGeneric.c
c6d109a0bb9a26bb362c476c1dc4011b40f438a1
[mirror_edk2.git] / MdePkg / Library / BaseMemoryLib / MemLibGeneric.c
1 /** @file
2 Architecture Independent Base Memory Library Implementation.
3
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 The following BaseMemoryLib instances share the same version of this file:
14
15 BaseMemoryLib
16 PeiMemoryLib
17 DxeMemoryLib
18
19 **/
20
21 //
22 // Include common header file for this module.
23 //
24
25
26 #include "MemLibInternals.h"
27
28 /**
29 Fills a target buffer with a 16-bit value, and returns the target buffer.
30
31 @param Buffer Pointer to the target buffer to fill.
32 @param Length Number of bytes in Buffer to fill.
33 @param Value Value with which to fill Length bytes of Buffer.
34
35 @return Buffer
36
37 **/
38 VOID *
39 EFIAPI
40 InternalMemSetMem16 (
41 OUT VOID *Buffer,
42 IN UINTN Length,
43 IN UINT16 Value
44 )
45 {
46 do {
47 ((UINT16*)Buffer)[--Length] = Value;
48 } while (Length != 0);
49 return Buffer;
50 }
51
52 /**
53 Fills a target buffer with a 32-bit value, and returns the target buffer.
54
55 @param Buffer Pointer to the target buffer to fill.
56 @param Length Number of bytes in Buffer to fill.
57 @param Value Value with which to fill Length bytes of Buffer.
58
59 @return Buffer
60
61 **/
62 VOID *
63 EFIAPI
64 InternalMemSetMem32 (
65 OUT VOID *Buffer,
66 IN UINTN Length,
67 IN UINT32 Value
68 )
69 {
70 do {
71 ((UINT32*)Buffer)[--Length] = Value;
72 } while (Length != 0);
73 return Buffer;
74 }
75
76 /**
77 Fills a target buffer with a 64-bit value, and returns the target buffer.
78
79 @param Buffer Pointer to the target buffer to fill.
80 @param Length Number of bytes in Buffer to fill.
81 @param Value Value with which to fill Length bytes of Buffer.
82
83 @return Buffer
84
85 **/
86 VOID *
87 EFIAPI
88 InternalMemSetMem64 (
89 OUT VOID *Buffer,
90 IN UINTN Length,
91 IN UINT64 Value
92 )
93 {
94 do {
95 ((UINT64*)Buffer)[--Length] = Value;
96 } while (Length != 0);
97 return Buffer;
98 }
99
100 /**
101 Set Buffer to 0 for Size bytes.
102
103 @param Buffer Memory to set.
104 @param Length Number of bytes to set
105
106 @return Buffer
107
108 **/
109 VOID *
110 EFIAPI
111 InternalMemZeroMem (
112 OUT VOID *Buffer,
113 IN UINTN Length
114 )
115 {
116 return InternalMemSetMem (Buffer, Length, 0);
117 }
118
119 /**
120 Compares two memory buffers of a given length.
121
122 @param DestinationBuffer First memory buffer
123 @param SourceBuffer Second memory buffer
124 @param Length Length of DestinationBuffer and SourceBuffer memory
125 regions to compare. Must be non-zero.
126
127 @retval 0 if MemOne == MemTwo
128
129 **/
130 INTN
131 EFIAPI
132 InternalMemCompareMem (
133 IN CONST VOID *DestinationBuffer,
134 IN CONST VOID *SourceBuffer,
135 IN UINTN Length
136 )
137 {
138 while ((--Length != 0) &&
139 (*(INT8*)DestinationBuffer == *(INT8*)SourceBuffer)) {
140 DestinationBuffer = (INT8*)DestinationBuffer + 1;
141 SourceBuffer = (INT8*)SourceBuffer + 1;
142 }
143 return (INTN)*(UINT8*)DestinationBuffer - (INTN)*(UINT8*)SourceBuffer;
144 }
145
146 /**
147 Scans a target buffer for an 8-bit value, and returns a pointer to the
148 matching 8-bit value in the target buffer.
149
150 @param Buffer Pointer to the target buffer to scan.
151 @param Length Number of bytes in Buffer to scan. Must be non-zero.
152 @param Value Value to search for in the target buffer.
153
154 @return Pointer to the first occurrence or NULL if not found.
155
156 **/
157 CONST VOID *
158 EFIAPI
159 InternalMemScanMem8 (
160 IN CONST VOID *Buffer,
161 IN UINTN Length,
162 IN UINT8 Value
163 )
164 {
165 CONST UINT8 *Pointer;
166
167 Pointer = (CONST UINT8*)Buffer;
168 do {
169 if (*(Pointer++) == Value) {
170 return Pointer;
171 }
172 } while (--Length != 0);
173 return NULL;
174 }
175
176 /**
177 Scans a target buffer for a 16-bit value, and returns a pointer to the
178 matching 16-bit value in the target buffer.
179
180 @param Buffer Pointer to the target buffer to scan.
181 @param Length Number of bytes in Buffer to scan. Must be non-zero.
182 @param Value Value to search for in the target buffer.
183
184 @return Pointer to the first occurrence or NULL if not found.
185
186 **/
187 CONST VOID *
188 EFIAPI
189 InternalMemScanMem16 (
190 IN CONST VOID *Buffer,
191 IN UINTN Length,
192 IN UINT16 Value
193 )
194 {
195 CONST UINT16 *Pointer;
196
197 Pointer = (CONST UINT16*)Buffer;
198 do {
199 if (*(Pointer++) == Value) {
200 return Pointer;
201 }
202 } while (--Length != 0);
203 return NULL;
204 }
205
206 /**
207 Scans a target buffer for a 32-bit value, and returns a pointer to the
208 matching 32-bit value in the target buffer.
209
210 @param Buffer Pointer to the target buffer to scan.
211 @param Length Number of bytes in Buffer to scan. Must be non-zero.
212 @param Value Value to search for in the target buffer.
213
214 @return Pointer to the first occurrence or NULL if not found.
215
216 **/
217 CONST VOID *
218 EFIAPI
219 InternalMemScanMem32 (
220 IN CONST VOID *Buffer,
221 IN UINTN Length,
222 IN UINT32 Value
223 )
224 {
225 CONST UINT32 *Pointer;
226
227 Pointer = (CONST UINT32*)Buffer;
228 do {
229 if (*(Pointer++) == Value) {
230 return Pointer;
231 }
232 } while (--Length != 0);
233 return NULL;
234 }
235
236 /**
237 Scans a target buffer for a 64-bit value, and returns a pointer to the
238 matching 64-bit value in the target buffer.
239
240 @param Buffer Pointer to the target buffer to scan.
241 @param Length Number of bytes in Buffer to scan. Must be non-zero.
242 @param Value Value to search for in the target buffer.
243
244 @return Pointer to the first occurrence or NULL if not found.
245
246 **/
247 CONST VOID *
248 EFIAPI
249 InternalMemScanMem64 (
250 IN CONST VOID *Buffer,
251 IN UINTN Length,
252 IN UINT64 Value
253 )
254 {
255 CONST UINT64 *Pointer;
256
257 Pointer = (CONST UINT64*)Buffer;
258 do {
259 if (*(Pointer++) == Value) {
260 return Pointer;
261 }
262 } while (--Length != 0);
263 return NULL;
264 }