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