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