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