1 | ␊ |
2 | #include "MemLibInternals.h"␊ |
3 | ␊ |
4 | /** @file␊ |
5 | Implementation of the InternalMemCopyMem routine. This function is broken␊ |
6 | out into its own source file so that it can be excluded from a build for a␊ |
7 | particular platform easily if an optimized version is desired.␊ |
8 | ␊ |
9 | Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>␊ |
10 | 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 | Copy Length bytes from Source to Destination.␊ |
22 | ␊ |
23 | @param DestinationBuffer The target of the copy request.␊ |
24 | @param SourceBuffer The place to copy from.␊ |
25 | @param Length The number of bytes to copy.␊ |
26 | ␊ |
27 | @return Destination␊ |
28 | ␊ |
29 | **/␊ |
30 | VOID *␊ |
31 | InternalMemCopyMem (␊ |
32 | OUT VOID *DestinationBuffer,␊ |
33 | IN CONST VOID *SourceBuffer,␊ |
34 | IN UINTN Length␊ |
35 | )␊ |
36 | {␊ |
37 | //␊ |
38 | // Declare the local variables that actually move the data elements as␊ |
39 | // volatile to prevent the optimizer from replacing this function with␊ |
40 | // the intrinsic memcpy()␊ |
41 | //␊ |
42 | volatile UINT8 *Destination8;␊ |
43 | CONST UINT8 *Source8;␊ |
44 | ␊ |
45 | if (SourceBuffer > DestinationBuffer) {␊ |
46 | Destination8 = (UINT8*)DestinationBuffer;␊ |
47 | Source8 = (CONST UINT8*)SourceBuffer;␊ |
48 | while (Length-- != 0) {␊ |
49 | *(Destination8++) = *(Source8++);␊ |
50 | }␊ |
51 | } else if (SourceBuffer < DestinationBuffer) {␊ |
52 | Destination8 = (UINT8*)DestinationBuffer + Length;␊ |
53 | Source8 = (CONST UINT8*)SourceBuffer + Length;␊ |
54 | while (Length-- != 0) {␊ |
55 | *(--Destination8) = *(--Source8);␊ |
56 | }␊ |
57 | }␊ |
58 | return DestinationBuffer;␊ |
59 | }␊ |
60 | ␊ |
61 | /**␊ |
62 | Copies a source buffer to a destination buffer, and returns the destination buffer.␊ |
63 | ␊ |
64 | This function copies Length bytes from SourceBuffer to DestinationBuffer, and returns␊ |
65 | DestinationBuffer. The implementation must be reentrant, and it must handle the case␊ |
66 | where SourceBuffer overlaps DestinationBuffer.␊ |
67 | ␊ |
68 | If Length is greater than (MAX_ADDRESS - DestinationBuffer + 1), then ASSERT().␊ |
69 | If Length is greater than (MAX_ADDRESS - SourceBuffer + 1), then ASSERT().␊ |
70 | ␊ |
71 | @param DestinationBuffer A pointer to the destination buffer of the memory copy.␊ |
72 | @param SourceBuffer A pointer to the source buffer of the memory copy.␊ |
73 | @param Length The number of bytes to copy from SourceBuffer to DestinationBuffer.␊ |
74 | ␊ |
75 | @return DestinationBuffer.␊ |
76 | ␊ |
77 | **/␊ |
78 | VOID *␊ |
79 | CopyMem (␊ |
80 | OUT VOID *DestinationBuffer,␊ |
81 | IN CONST VOID *SourceBuffer,␊ |
82 | IN UINTN Length␊ |
83 | )␊ |
84 | {␊ |
85 | if (Length == 0) {␊ |
86 | return DestinationBuffer;␊ |
87 | }␊ |
88 | ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN)DestinationBuffer));␊ |
89 | ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN)SourceBuffer));␊ |
90 | ␊ |
91 | if (DestinationBuffer == SourceBuffer) {␊ |
92 | return DestinationBuffer;␊ |
93 | }␊ |
94 | return InternalMemCopyMem (DestinationBuffer, SourceBuffer, Length);␊ |
95 | }␊ |
96 | ␊ |
97 | ␊ |
98 | /**␊ |
99 | Fills a target buffer with a 16-bit value, and returns the target buffer.␊ |
100 | ␊ |
101 | @param Buffer The pointer to the target buffer to fill.␊ |
102 | @param Length The count of 16-bit value to fill.␊ |
103 | @param Value The value with which to fill Length bytes of Buffer.␊ |
104 | ␊ |
105 | @return Buffer␊ |
106 | ␊ |
107 | **/␊ |
108 | VOID *␊ |
109 | InternalMemSetMem16 (␊ |
110 | OUT VOID *Buffer,␊ |
111 | IN UINTN Length,␊ |
112 | IN UINT16 Value␊ |
113 | )␊ |
114 | {␊ |
115 | ␉ASSERT(Buffer != NULL);␊ |
116 | do {␊ |
117 | ((UINT16*)Buffer)[--Length] = Value;␊ |
118 | } while (Length != 0);␊ |
119 | return Buffer;␊ |
120 | }␊ |
121 | ␊ |
122 | /**␊ |
123 | Fills a target buffer with a 32-bit value, and returns the target buffer.␊ |
124 | ␊ |
125 | @param Buffer The pointer to the target buffer to fill.␊ |
126 | @param Length The count of 32-bit value to fill.␊ |
127 | @param Value The value with which to fill Length bytes of Buffer.␊ |
128 | ␊ |
129 | @return Buffer␊ |
130 | ␊ |
131 | **/␊ |
132 | VOID *␊ |
133 | InternalMemSetMem32 (␊ |
134 | OUT VOID *Buffer,␊ |
135 | IN UINTN Length,␊ |
136 | IN UINT32 Value␊ |
137 | )␊ |
138 | {␊ |
139 | ␉ASSERT(Buffer != NULL);␊ |
140 | do {␊ |
141 | ((UINT32*)Buffer)[--Length] = Value;␊ |
142 | } while (Length != 0);␊ |
143 | return Buffer;␊ |
144 | }␊ |
145 | ␊ |
146 | /**␊ |
147 | Fills a target buffer with a 64-bit value, and returns the target buffer.␊ |
148 | ␊ |
149 | @param Buffer The pointer to the target buffer to fill.␊ |
150 | @param Length The count of 64-bit value to fill.␊ |
151 | @param Value The value with which to fill Length bytes of Buffer.␊ |
152 | ␊ |
153 | @return Buffer␊ |
154 | ␊ |
155 | **/␊ |
156 | VOID *␊ |
157 | InternalMemSetMem64 (␊ |
158 | OUT VOID *Buffer,␊ |
159 | IN UINTN Length,␊ |
160 | IN UINT64 Value␊ |
161 | )␊ |
162 | {␊ |
163 | ␉ASSERT(Buffer != NULL);␊ |
164 | do {␊ |
165 | ((UINT64*)Buffer)[--Length] = Value;␊ |
166 | } while (Length != 0);␊ |
167 | return Buffer;␊ |
168 | }␊ |
169 | ␊ |
170 | /**␊ |
171 | Set Buffer to 0 for Size bytes.␊ |
172 | ␊ |
173 | @param Buffer Memory to set.␊ |
174 | @param Length The number of bytes to set.␊ |
175 | ␊ |
176 | @return Buffer␊ |
177 | ␊ |
178 | **/␊ |
179 | VOID *␊ |
180 | InternalMemZeroMem (␊ |
181 | OUT VOID *Buffer,␊ |
182 | IN UINTN Length␊ |
183 | )␊ |
184 | {␊ |
185 | return InternalMemSetMem (Buffer, Length, 0);␊ |
186 | }␊ |
187 | ␊ |
188 | /**␊ |
189 | Compares two memory buffers of a given length.␊ |
190 | ␊ |
191 | @param DestinationBuffer The first memory buffer.␊ |
192 | @param SourceBuffer The second memory buffer.␊ |
193 | @param Length Length of DestinationBuffer and SourceBuffer memory␊ |
194 | regions to compare. Must be non-zero.␊ |
195 | ␊ |
196 | @return 0 All Length bytes of the two buffers are identical.␊ |
197 | @retval Non-zero The first mismatched byte in SourceBuffer subtracted from the first␊ |
198 | mismatched byte in DestinationBuffer.␊ |
199 | ␊ |
200 | **/␊ |
201 | INTN␊ |
202 | InternalMemCompareMem (␊ |
203 | IN CONST VOID *DestinationBuffer,␊ |
204 | IN CONST VOID *SourceBuffer,␊ |
205 | IN UINTN Length␊ |
206 | )␊ |
207 | {␊ |
208 | while ((--Length != 0) &&␊ |
209 | (*(INT8*)DestinationBuffer == *(INT8*)SourceBuffer)) {␊ |
210 | DestinationBuffer = (INT8*)DestinationBuffer + 1;␊ |
211 | SourceBuffer = (INT8*)SourceBuffer + 1;␊ |
212 | }␊ |
213 | return (INTN)*(UINT8*)DestinationBuffer - (INTN)*(UINT8*)SourceBuffer;␊ |
214 | }␊ |
215 | ␊ |
216 | /**␊ |
217 | Scans a target buffer for an 8-bit value, and returns a pointer to the␊ |
218 | matching 8-bit value in the target buffer.␊ |
219 | ␊ |
220 | @param Buffer The pointer to the target buffer to scan.␊ |
221 | @param Length The count of 8-bit value to scan. Must be non-zero.␊ |
222 | @param Value The value to search for in the target buffer.␊ |
223 | ␊ |
224 | @return The pointer to the first occurrence, or NULL if not found.␊ |
225 | ␊ |
226 | **/␊ |
227 | CONST VOID *␊ |
228 | InternalMemScanMem8 (␊ |
229 | IN CONST VOID *Buffer,␊ |
230 | IN UINTN Length,␊ |
231 | IN UINT8 Value␊ |
232 | )␊ |
233 | {␊ |
234 | CONST UINT8 *Pointer;␊ |
235 | ␊ |
236 | Pointer = (CONST UINT8*)Buffer;␊ |
237 | ␉ASSERT(Pointer != NULL);␊ |
238 | do {␊ |
239 | if (*Pointer == Value) {␊ |
240 | return Pointer;␊ |
241 | }␊ |
242 | ++Pointer;␊ |
243 | } while (--Length != 0);␊ |
244 | return NULL;␊ |
245 | }␊ |
246 | ␊ |
247 | /**␊ |
248 | Scans a target buffer for a 16-bit value, and returns a pointer to the␊ |
249 | matching 16-bit value in the target buffer.␊ |
250 | ␊ |
251 | @param Buffer The pointer to the target buffer to scan.␊ |
252 | @param Length The count of 16-bit value to scan. Must be non-zero.␊ |
253 | @param Value The value to search for in the target buffer.␊ |
254 | ␊ |
255 | @return The pointer to the first occurrence, or NULL if not found.␊ |
256 | ␊ |
257 | **/␊ |
258 | CONST VOID *␊ |
259 | InternalMemScanMem16 (␊ |
260 | IN CONST VOID *Buffer,␊ |
261 | IN UINTN Length,␊ |
262 | IN UINT16 Value␊ |
263 | )␊ |
264 | {␊ |
265 | CONST UINT16 *Pointer;␊ |
266 | ␊ |
267 | Pointer = (CONST UINT16*)Buffer;␊ |
268 | ␉ASSERT(Pointer != NULL);␊ |
269 | do {␊ |
270 | if (*Pointer == Value) {␊ |
271 | return Pointer;␊ |
272 | }␊ |
273 | ++Pointer;␊ |
274 | } while (--Length != 0);␊ |
275 | return NULL;␊ |
276 | }␊ |
277 | ␊ |
278 | /**␊ |
279 | Scans a target buffer for a 32-bit value, and returns a pointer to the␊ |
280 | matching 32-bit value in the target buffer.␊ |
281 | ␊ |
282 | @param Buffer The pointer to the target buffer to scan.␊ |
283 | @param Length The count of 32-bit value to scan. Must be non-zero.␊ |
284 | @param Value The value to search for in the target buffer.␊ |
285 | ␊ |
286 | @return The pointer to the first occurrence, or NULL if not found.␊ |
287 | ␊ |
288 | **/␊ |
289 | CONST VOID *␊ |
290 | InternalMemScanMem32 (␊ |
291 | IN CONST VOID *Buffer,␊ |
292 | IN UINTN Length,␊ |
293 | IN UINT32 Value␊ |
294 | )␊ |
295 | {␊ |
296 | CONST UINT32 *Pointer;␊ |
297 | ␊ |
298 | Pointer = (CONST UINT32*)Buffer;␊ |
299 | ␉ASSERT(Pointer != NULL);␊ |
300 | do {␊ |
301 | if (*Pointer == Value) {␊ |
302 | return Pointer;␊ |
303 | }␊ |
304 | ++Pointer;␊ |
305 | } while (--Length != 0);␊ |
306 | return NULL;␊ |
307 | }␊ |
308 | ␊ |
309 | /**␊ |
310 | Scans a target buffer for a 64-bit value, and returns a pointer to the␊ |
311 | matching 64-bit value in the target buffer.␊ |
312 | ␊ |
313 | @param Buffer The pointer to the target buffer to scan.␊ |
314 | @param Length The count of 64-bit value to scan. Must be non-zero.␊ |
315 | @param Value The value to search for in the target buffer.␊ |
316 | ␊ |
317 | @return The pointer to the first occurrence, or NULL if not found.␊ |
318 | ␊ |
319 | **/␊ |
320 | CONST VOID *␊ |
321 | InternalMemScanMem64 (␊ |
322 | IN CONST VOID *Buffer,␊ |
323 | IN UINTN Length,␊ |
324 | IN UINT64 Value␊ |
325 | )␊ |
326 | {␊ |
327 | CONST UINT64 *Pointer;␊ |
328 | ␊ |
329 | Pointer = (CONST UINT64*)Buffer;␊ |
330 | ␉ASSERT(Pointer != NULL);␉␊ |
331 | do {␊ |
332 | if (*Pointer == Value) {␊ |
333 | return Pointer;␊ |
334 | }␊ |
335 | ++Pointer;␊ |
336 | } while (--Length != 0);␊ |
337 | return NULL;␊ |
338 | }␊ |
339 | ␊ |
340 | /**␊ |
341 | Scans a target buffer for an 8-bit value, and returns a pointer to the matching 8-bit value␊ |
342 | in the target buffer.␊ |
343 | ␊ |
344 | This function searches the target buffer specified by Buffer and Length from the lowest␊ |
345 | address to the highest address for an 8-bit value that matches Value. If a match is found,␊ |
346 | then a pointer to the matching byte in the target buffer is returned. If no match is found,␊ |
347 | then NULL is returned. If Length is 0, then NULL is returned.␊ |
348 | ␊ |
349 | If Length > 0 and Buffer is NULL, then ASSERT().␊ |
350 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().␊ |
351 | ␊ |
352 | @param Buffer The pointer to the target buffer to scan.␊ |
353 | @param Length The number of bytes in Buffer to scan.␊ |
354 | @param Value The value to search for in the target buffer.␊ |
355 | ␊ |
356 | @return A pointer to the matching byte in the target buffer, or NULL otherwise.␊ |
357 | ␊ |
358 | **/␊ |
359 | VOID *␊ |
360 | ScanMem8 (␊ |
361 | IN CONST VOID *Buffer,␊ |
362 | IN UINTN Length,␊ |
363 | IN UINT8 Value␊ |
364 | )␊ |
365 | {␊ |
366 | if (Length == 0) {␊ |
367 | return NULL;␊ |
368 | }␊ |
369 | ASSERT (Buffer != NULL);␊ |
370 | ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN)Buffer));␊ |
371 | ␊ |
372 | return (VOID*)InternalMemScanMem8 (Buffer, Length, Value);␊ |
373 | }␊ |
374 | ␊ |
375 | /**␊ |
376 | Scans a target buffer for a UINTN sized value, and returns a pointer to the matching␊ |
377 | UINTN sized value in the target buffer.␊ |
378 | ␊ |
379 | This function searches the target buffer specified by Buffer and Length from the lowest␊ |
380 | address to the highest address for a UINTN sized value that matches Value. If a match is found,␊ |
381 | then a pointer to the matching byte in the target buffer is returned. If no match is found,␊ |
382 | then NULL is returned. If Length is 0, then NULL is returned.␊ |
383 | ␊ |
384 | If Length > 0 and Buffer is NULL, then ASSERT().␊ |
385 | If Buffer is not aligned on a UINTN boundary, then ASSERT().␊ |
386 | If Length is not aligned on a UINTN boundary, then ASSERT().␊ |
387 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().␊ |
388 | ␊ |
389 | @param Buffer The pointer to the target buffer to scan.␊ |
390 | @param Length The number of bytes in Buffer to scan.␊ |
391 | @param Value The value to search for in the target buffer.␊ |
392 | ␊ |
393 | @return A pointer to the matching byte in the target buffer, or NULL otherwise.␊ |
394 | ␊ |
395 | **/␊ |
396 | VOID *␊ |
397 | ScanMemN (␊ |
398 | IN CONST VOID *Buffer,␊ |
399 | IN UINTN Length,␊ |
400 | IN UINTN Value␊ |
401 | )␊ |
402 | {␊ |
403 | if (sizeof (UINTN) == sizeof (UINT64)) {␊ |
404 | return ScanMem64 (Buffer, Length, (UINT64)Value);␊ |
405 | } else {␊ |
406 | return ScanMem32 (Buffer, Length, (UINT32)Value);␊ |
407 | }␊ |
408 | }␊ |
409 | ␊ |
410 | /**␊ |
411 | Scans a target buffer for a 16-bit value, and returns a pointer to the matching 16-bit value␊ |
412 | in the target buffer.␊ |
413 | ␊ |
414 | This function searches the target buffer specified by Buffer and Length from the lowest␊ |
415 | address to the highest address for a 16-bit value that matches Value. If a match is found,␊ |
416 | then a pointer to the matching byte in the target buffer is returned. If no match is found,␊ |
417 | then NULL is returned. If Length is 0, then NULL is returned.␊ |
418 | ␊ |
419 | If Length > 0 and Buffer is NULL, then ASSERT().␊ |
420 | If Buffer is not aligned on a 16-bit boundary, then ASSERT().␊ |
421 | If Length is not aligned on a 16-bit boundary, then ASSERT().␊ |
422 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().␊ |
423 | ␊ |
424 | @param Buffer The pointer to the target buffer to scan.␊ |
425 | @param Length The number of bytes in Buffer to scan.␊ |
426 | @param Value The value to search for in the target buffer.␊ |
427 | ␊ |
428 | @return A pointer to the matching byte in the target buffer or NULL otherwise.␊ |
429 | ␊ |
430 | **/␊ |
431 | VOID *␊ |
432 | ScanMem16 (␊ |
433 | IN CONST VOID *Buffer,␊ |
434 | IN UINTN Length,␊ |
435 | IN UINT16 Value␊ |
436 | )␊ |
437 | {␊ |
438 | if (Length == 0) {␊ |
439 | return NULL;␊ |
440 | }␊ |
441 | ␊ |
442 | ASSERT (Buffer != NULL);␊ |
443 | ASSERT (((UINTN)Buffer & (sizeof (Value) - 1)) == 0);␊ |
444 | ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN)Buffer));␊ |
445 | ASSERT ((Length & (sizeof (Value) - 1)) == 0);␊ |
446 | ␊ |
447 | return (VOID*)InternalMemScanMem16 (Buffer, Length / sizeof (Value), Value);␊ |
448 | }␊ |
449 | ␊ |
450 | /**␊ |
451 | Scans a target buffer for a 32-bit value, and returns a pointer to the matching 32-bit value␊ |
452 | in the target buffer.␊ |
453 | ␊ |
454 | This function searches the target buffer specified by Buffer and Length from the lowest␊ |
455 | address to the highest address for a 32-bit value that matches Value. If a match is found,␊ |
456 | then a pointer to the matching byte in the target buffer is returned. If no match is found,␊ |
457 | then NULL is returned. If Length is 0, then NULL is returned.␊ |
458 | ␊ |
459 | If Length > 0 and Buffer is NULL, then ASSERT().␊ |
460 | If Buffer is not aligned on a 32-bit boundary, then ASSERT().␊ |
461 | If Length is not aligned on a 32-bit boundary, then ASSERT().␊ |
462 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().␊ |
463 | ␊ |
464 | @param Buffer The pointer to the target buffer to scan.␊ |
465 | @param Length The number of bytes in Buffer to scan.␊ |
466 | @param Value The value to search for in the target buffer.␊ |
467 | ␊ |
468 | @return A pointer to the matching byte in the target buffer or NULL otherwise.␊ |
469 | ␊ |
470 | **/␊ |
471 | VOID *␊ |
472 | ScanMem32 (␊ |
473 | IN CONST VOID *Buffer,␊ |
474 | IN UINTN Length,␊ |
475 | IN UINT32 Value␊ |
476 | )␊ |
477 | {␊ |
478 | if (Length == 0) {␊ |
479 | return NULL;␊ |
480 | }␊ |
481 | ␊ |
482 | ASSERT (Buffer != NULL);␊ |
483 | ASSERT (((UINTN)Buffer & (sizeof (Value) - 1)) == 0);␊ |
484 | ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN)Buffer));␊ |
485 | ASSERT ((Length & (sizeof (Value) - 1)) == 0);␊ |
486 | ␊ |
487 | return (VOID*)InternalMemScanMem32 (Buffer, Length / sizeof (Value), Value);␊ |
488 | }␊ |
489 | ␊ |
490 | /**␊ |
491 | Scans a target buffer for a 64-bit value, and returns a pointer to the matching 64-bit value␊ |
492 | in the target buffer.␊ |
493 | ␊ |
494 | This function searches the target buffer specified by Buffer and Length from the lowest␊ |
495 | address to the highest address for a 64-bit value that matches Value. If a match is found,␊ |
496 | then a pointer to the matching byte in the target buffer is returned. If no match is found,␊ |
497 | then NULL is returned. If Length is 0, then NULL is returned.␊ |
498 | ␊ |
499 | If Length > 0 and Buffer is NULL, then ASSERT().␊ |
500 | If Buffer is not aligned on a 64-bit boundary, then ASSERT().␊ |
501 | If Length is not aligned on a 64-bit boundary, then ASSERT().␊ |
502 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().␊ |
503 | ␊ |
504 | @param Buffer The pointer to the target buffer to scan.␊ |
505 | @param Length The number of bytes in Buffer to scan.␊ |
506 | @param Value The value to search for in the target buffer.␊ |
507 | ␊ |
508 | @return A pointer to the matching byte in the target buffer or NULL otherwise.␊ |
509 | ␊ |
510 | **/␊ |
511 | VOID *␊ |
512 | ScanMem64 (␊ |
513 | IN CONST VOID *Buffer,␊ |
514 | IN UINTN Length,␊ |
515 | IN UINT64 Value␊ |
516 | )␊ |
517 | {␊ |
518 | if (Length == 0) {␊ |
519 | return NULL;␊ |
520 | }␊ |
521 | ␊ |
522 | ASSERT (Buffer != NULL);␊ |
523 | ASSERT (((UINTN)Buffer & (sizeof (Value) - 1)) == 0);␊ |
524 | ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN)Buffer));␊ |
525 | ASSERT ((Length & (sizeof (Value) - 1)) == 0);␊ |
526 | ␊ |
527 | return (VOID*)InternalMemScanMem64 (Buffer, Length / sizeof (Value), Value);␊ |
528 | }␊ |
529 | ␊ |
530 | /**␊ |
531 | Set Buffer to Value for Size bytes.␊ |
532 | ␊ |
533 | @param Buffer The memory to set.␊ |
534 | @param Length The number of bytes to set.␊ |
535 | @param Value The value of the set operation.␊ |
536 | ␊ |
537 | @return Buffer␊ |
538 | ␊ |
539 | **/␊ |
540 | VOID *␊ |
541 | InternalMemSetMem (␊ |
542 | OUT VOID *Buffer,␊ |
543 | IN UINTN Length,␊ |
544 | IN UINT8 Value␊ |
545 | )␊ |
546 | {␊ |
547 | //␊ |
548 | // Declare the local variables that actually move the data elements as␊ |
549 | // volatile to prevent the optimizer from replacing this function with␊ |
550 | // the intrinsic memset()␊ |
551 | //␊ |
552 | ␉ASSERT(Buffer != NULL);␊ |
553 | volatile UINT8 *Pointer; ␊ |
554 | Pointer = (UINT8*)Buffer;␊ |
555 | while (Length-- > 0) {␊ |
556 | *(Pointer++) = Value;␊ |
557 | }␊ |
558 | return Buffer;␊ |
559 | }␊ |
560 | ␊ |
561 | /**␊ |
562 | Fills a target buffer with a 16-bit value, and returns the target buffer.␊ |
563 | ␊ |
564 | This function fills Length bytes of Buffer with the 16-bit value specified by␊ |
565 | Value, and returns Buffer. Value is repeated every 16-bits in for Length␊ |
566 | bytes of Buffer.␊ |
567 | ␊ |
568 | If Length > 0 and Buffer is NULL, then ASSERT().␊ |
569 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().␊ |
570 | If Buffer is not aligned on a 16-bit boundary, then ASSERT().␊ |
571 | If Length is not aligned on a 16-bit boundary, then ASSERT().␊ |
572 | ␊ |
573 | @param Buffer The pointer to the target buffer to fill.␊ |
574 | @param Length The number of bytes in Buffer to fill.␊ |
575 | @param Value The value with which to fill Length bytes of Buffer.␊ |
576 | ␊ |
577 | @return Buffer.␊ |
578 | ␊ |
579 | **/␊ |
580 | VOID *␊ |
581 | SetMem16 (␊ |
582 | OUT VOID *Buffer,␊ |
583 | IN UINTN Length,␊ |
584 | IN UINT16 Value␊ |
585 | )␊ |
586 | {␊ |
587 | if (Length == 0) {␊ |
588 | return Buffer;␊ |
589 | }␊ |
590 | ␊ |
591 | ASSERT (Buffer != NULL);␊ |
592 | ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN)Buffer));␊ |
593 | ASSERT ((((UINTN)Buffer) & (sizeof (Value) - 1)) == 0);␊ |
594 | ASSERT ((Length & (sizeof (Value) - 1)) == 0);␊ |
595 | ␊ |
596 | return InternalMemSetMem16 (Buffer, Length / sizeof (Value), Value);␊ |
597 | }␊ |
598 | ␊ |
599 | /**␊ |
600 | Fills a target buffer with a 32-bit value, and returns the target buffer.␊ |
601 | ␊ |
602 | This function fills Length bytes of Buffer with the 32-bit value specified by␊ |
603 | Value, and returns Buffer. Value is repeated every 32-bits in for Length␊ |
604 | bytes of Buffer.␊ |
605 | ␊ |
606 | If Length > 0 and Buffer is NULL, then ASSERT().␊ |
607 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().␊ |
608 | If Buffer is not aligned on a 32-bit boundary, then ASSERT().␊ |
609 | If Length is not aligned on a 32-bit boundary, then ASSERT().␊ |
610 | ␊ |
611 | @param Buffer The pointer to the target buffer to fill.␊ |
612 | @param Length The number of bytes in Buffer to fill.␊ |
613 | @param Value The value with which to fill Length bytes of Buffer.␊ |
614 | ␊ |
615 | @return Buffer.␊ |
616 | ␊ |
617 | **/␊ |
618 | VOID *␊ |
619 | SetMem32 (␊ |
620 | OUT VOID *Buffer,␊ |
621 | IN UINTN Length,␊ |
622 | IN UINT32 Value␊ |
623 | )␊ |
624 | {␊ |
625 | if (Length == 0) {␊ |
626 | return Buffer;␊ |
627 | }␊ |
628 | ␊ |
629 | ASSERT (Buffer != NULL);␊ |
630 | ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN)Buffer));␊ |
631 | ASSERT ((((UINTN)Buffer) & (sizeof (Value) - 1)) == 0);␊ |
632 | ASSERT ((Length & (sizeof (Value) - 1)) == 0);␊ |
633 | ␊ |
634 | return InternalMemSetMem32 (Buffer, Length / sizeof (Value), Value);␊ |
635 | }␊ |
636 | ␊ |
637 | /**␊ |
638 | Fills a target buffer with a 64-bit value, and returns the target buffer.␊ |
639 | ␊ |
640 | This function fills Length bytes of Buffer with the 64-bit value specified by␊ |
641 | Value, and returns Buffer. Value is repeated every 64-bits in for Length␊ |
642 | bytes of Buffer.␊ |
643 | ␊ |
644 | If Length > 0 and Buffer is NULL, then ASSERT().␊ |
645 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().␊ |
646 | If Buffer is not aligned on a 64-bit boundary, then ASSERT().␊ |
647 | If Length is not aligned on a 64-bit boundary, then ASSERT().␊ |
648 | ␊ |
649 | @param Buffer The pointer to the target buffer to fill.␊ |
650 | @param Length The number of bytes in Buffer to fill.␊ |
651 | @param Value The value with which to fill Length bytes of Buffer.␊ |
652 | ␊ |
653 | @return Buffer.␊ |
654 | ␊ |
655 | **/␊ |
656 | VOID *␊ |
657 | SetMem64 (␊ |
658 | OUT VOID *Buffer,␊ |
659 | IN UINTN Length,␊ |
660 | IN UINT64 Value␊ |
661 | )␊ |
662 | {␊ |
663 | if (Length == 0) {␊ |
664 | return Buffer;␊ |
665 | }␊ |
666 | ␊ |
667 | ASSERT (Buffer != NULL);␊ |
668 | ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN)Buffer));␊ |
669 | ASSERT ((((UINTN)Buffer) & (sizeof (Value) - 1)) == 0);␊ |
670 | ASSERT ((Length & (sizeof (Value) - 1)) == 0);␊ |
671 | ␊ |
672 | return InternalMemSetMem64 (Buffer, Length / sizeof (Value), Value);␊ |
673 | }␊ |
674 | ␊ |
675 | /**␊ |
676 | Fills a target buffer with a byte value, and returns the target buffer.␊ |
677 | ␊ |
678 | This function fills Length bytes of Buffer with Value, and returns Buffer.␊ |
679 | ␊ |
680 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().␊ |
681 | ␊ |
682 | @param Buffer The memory to set.␊ |
683 | @param Length The number of bytes to set.␊ |
684 | @param Value The value with which to fill Length bytes of Buffer.␊ |
685 | ␊ |
686 | @return Buffer.␊ |
687 | ␊ |
688 | **/␊ |
689 | VOID *␊ |
690 | SetMem (␊ |
691 | OUT VOID *Buffer,␊ |
692 | IN UINTN Length,␊ |
693 | IN UINT8 Value␊ |
694 | )␊ |
695 | {␊ |
696 | if (Length == 0) {␊ |
697 | return Buffer;␊ |
698 | }␊ |
699 | ␊ |
700 | ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN)Buffer));␊ |
701 | ␊ |
702 | return InternalMemSetMem (Buffer, Length, Value);␊ |
703 | }␊ |
704 | ␊ |
705 | /**␊ |
706 | Fills a target buffer with a value that is size UINTN, and returns the target buffer.␊ |
707 | ␊ |
708 | This function fills Length bytes of Buffer with the UINTN sized value specified by␊ |
709 | Value, and returns Buffer. Value is repeated every sizeof(UINTN) bytes for Length␊ |
710 | bytes of Buffer.␊ |
711 | ␊ |
712 | If Length > 0 and Buffer is NULL, then ASSERT().␊ |
713 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().␊ |
714 | If Buffer is not aligned on a UINTN boundary, then ASSERT().␊ |
715 | If Length is not aligned on a UINTN boundary, then ASSERT().␊ |
716 | ␊ |
717 | @param Buffer The pointer to the target buffer to fill.␊ |
718 | @param Length The number of bytes in Buffer to fill.␊ |
719 | @param Value The value with which to fill Length bytes of Buffer.␊ |
720 | ␊ |
721 | @return Buffer.␊ |
722 | ␊ |
723 | **/␊ |
724 | VOID *␊ |
725 | SetMemN (␊ |
726 | OUT VOID *Buffer,␊ |
727 | IN UINTN Length,␊ |
728 | IN UINTN Value␊ |
729 | )␊ |
730 | {␊ |
731 | if (sizeof (UINTN) == sizeof (UINT64)) {␊ |
732 | return SetMem64 (Buffer, Length, (UINT64)Value);␊ |
733 | } else {␊ |
734 | return SetMem32 (Buffer, Length, (UINT32)Value);␊ |
735 | }␊ |
736 | }␊ |
737 | ␊ |
738 | /**␊ |
739 | Fills a target buffer with zeros, and returns the target buffer.␊ |
740 | ␊ |
741 | This function fills Length bytes of Buffer with zeros, and returns Buffer.␊ |
742 | ␊ |
743 | If Length > 0 and Buffer is NULL, then ASSERT().␊ |
744 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().␊ |
745 | ␊ |
746 | @param Buffer The pointer to the target buffer to fill with zeros.␊ |
747 | @param Length The number of bytes in Buffer to fill with zeros.␊ |
748 | ␊ |
749 | @return Buffer.␊ |
750 | ␊ |
751 | **/␊ |
752 | VOID *␊ |
753 | ZeroMem (␊ |
754 | OUT VOID *Buffer,␊ |
755 | IN UINTN Length␊ |
756 | )␊ |
757 | {␊ |
758 | ASSERT (!(Buffer == NULL && Length > 0));␊ |
759 | ASSERT (Length <= (MAX_ADDRESS - (UINTN)Buffer + 1));␊ |
760 | return InternalMemZeroMem (Buffer, Length);␊ |
761 | }␊ |
762 | ␊ |
763 | ␊ |
764 | VOID *␊ |
765 | AllocatePool (␊ |
766 | IN UINTN AllocationSize␊ |
767 | )␊ |
768 | {␊ |
769 | ␉ASSERT(AllocationSize != 0);␊ |
770 | return malloc (AllocationSize);␊ |
771 | }␊ |
772 | ␊ |
773 | ␊ |
774 | VOID *␊ |
775 | AllocateZeroPool (␊ |
776 | IN UINTN AllocationSize␊ |
777 | )␊ |
778 | {␊ |
779 | VOID *Memory;␊ |
780 | ␊ |
781 | Memory = AllocatePool (AllocationSize);␊ |
782 | if (Memory != NULL) {␊ |
783 | Memory = ZeroMem (Memory, AllocationSize);␊ |
784 | }␊ |
785 | return Memory;␊ |
786 | }␊ |
787 | ␊ |
788 | VOID *␊ |
789 | AllocateCopyPool (␊ |
790 | IN UINTN AllocationSize,␊ |
791 | IN CONST VOID *Buffer␊ |
792 | )␊ |
793 | {␊ |
794 | VOID *Memory;␊ |
795 | ␊ |
796 | ASSERT (Buffer != NULL);␊ |
797 | ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));␊ |
798 | ␊ |
799 | Memory = AllocatePool ( AllocationSize);␊ |
800 | if (Memory != NULL) {␊ |
801 | Memory = CopyMem (Memory, Buffer, AllocationSize);␊ |
802 | }␊ |
803 | return Memory;␊ |
804 | }␊ |
805 | ␊ |
806 | VOID *␊ |
807 | ReallocatePool (␊ |
808 | IN UINTN OldSize,␊ |
809 | IN UINTN NewSize,␊ |
810 | IN VOID *OldBuffer OPTIONAL␊ |
811 | )␊ |
812 | {␊ |
813 | VOID *NewBuffer;␊ |
814 | ␊ |
815 | NewBuffer = AllocateZeroPool (NewSize);␊ |
816 | if (NewBuffer != NULL && OldBuffer != NULL) {␊ |
817 | CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));␊ |
818 | FreePool (OldBuffer);␊ |
819 | }␊ |
820 | return NewBuffer;␊ |
821 | }␊ |
822 | ␊ |
823 | VOID␊ |
824 | FreePool (␊ |
825 | IN VOID *Buffer␊ |
826 | )␊ |
827 | {␊ |
828 | ASSERT (Buffer != NULL);␊ |
829 | ␊ |
830 | free (Buffer);␊ |
831 | ␊ |
832 | }␊ |
833 | |