]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/UefiMemoryLib/MemLibWrapper.c
Split wrapper functions into separate source files
[mirror_edk2.git] / MdePkg / Library / UefiMemoryLib / MemLibWrapper.c
1 /** @file
2 Wrapper functions for Base Memory Library.
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: MemLibWrapper.c
14
15 The following BaseMemoryLib instances share the same version of this file:
16
17 BaseMemoryLib
18 BaseMemoryLibMmx
19 BaseMemoryLibSse2
20 BaseMemoryLibRepStr
21 PeiMemoryLib
22 UefiMemoryLib
23
24 **/
25
26 /**
27 Copy Length bytes from Source to Destination.
28
29 @param Destination Target of copy
30 @param Source Place to copy from
31 @param Length Number of bytes to copy
32
33 @return Destination
34
35 **/
36 VOID *
37 EFIAPI
38 InternalMemCopyMem (
39 OUT VOID *DestinationBuffer,
40 IN CONST VOID *SourceBuffer,
41 IN UINTN Length
42 );
43
44 /**
45 Set Buffer to Value for Size bytes.
46
47 @param Buffer Memory to set.
48 @param Size Number of bytes to set
49 @param Value Value of the set operation.
50
51 @return Buffer
52
53 **/
54 VOID *
55 EFIAPI
56 InternalMemSetMem (
57 OUT VOID *Buffer,
58 IN UINTN Length,
59 IN UINT8 Value
60 );
61
62 /**
63 Fills a target buffer with a 16-bit value, and returns the target buffer.
64
65 @param Buffer Pointer to the target buffer to fill.
66 @param Length Number of bytes in Buffer to fill.
67 @param Value Value with which to fill Length bytes of Buffer.
68
69 @return Buffer
70
71 **/
72 VOID *
73 EFIAPI
74 InternalMemSetMem16 (
75 OUT VOID *Buffer,
76 IN UINTN Length,
77 IN UINT16 Value
78 );
79
80 /**
81 Fills a target buffer with a 32-bit value, and returns the target buffer.
82
83 @param Buffer Pointer to the target buffer to fill.
84 @param Length Number of bytes in Buffer to fill.
85 @param Value Value with which to fill Length bytes of Buffer.
86
87 @return Buffer
88
89 **/
90 VOID *
91 EFIAPI
92 InternalMemSetMem32 (
93 OUT VOID *Buffer,
94 IN UINTN Length,
95 IN UINT32 Value
96 );
97
98 /**
99 Fills a target buffer with a 64-bit value, and returns the target buffer.
100
101 @param Buffer Pointer to the target buffer to fill.
102 @param Length Number of bytes in Buffer to fill.
103 @param Value Value with which to fill Length bytes of Buffer.
104
105 @return Buffer
106
107 **/
108 VOID *
109 EFIAPI
110 InternalMemSetMem64 (
111 OUT VOID *Buffer,
112 IN UINTN Length,
113 IN UINT64 Value
114 );
115
116 /**
117 Set Buffer to 0 for Size bytes.
118
119 @param Buffer Memory to set.
120 @param Size Number of bytes to set
121
122 @return Buffer
123
124 **/
125 VOID *
126 EFIAPI
127 InternalMemZeroMem (
128 OUT VOID *Buffer,
129 IN UINTN Length
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 /**
152 Scans a target buffer for an 8-bit value, and returns a pointer to the
153 matching 8-bit value in the target buffer.
154
155 @param Buffer Pointer to the target buffer to scan.
156 @param Length Number of bytes in Buffer to scan. Must be non-zero.
157 @param Value Value to search for in the target buffer.
158
159 @return Pointer to the first occurrence or NULL if not found.
160
161 **/
162 CONST VOID *
163 EFIAPI
164 InternalMemScanMem8 (
165 IN CONST VOID *Buffer,
166 IN UINTN Length,
167 IN UINT8 Value
168 );
169
170 /**
171 Scans a target buffer for a 16-bit value, and returns a pointer to the
172 matching 16-bit value in the target buffer.
173
174 @param Buffer Pointer to the target buffer to scan.
175 @param Length Number of bytes in Buffer to scan. Must be non-zero.
176 @param Value Value to search for in the target buffer.
177
178 @return Pointer to the first occurrence or NULL if not found.
179
180 **/
181 CONST VOID *
182 EFIAPI
183 InternalMemScanMem16 (
184 IN CONST VOID *Buffer,
185 IN UINTN Length,
186 IN UINT16 Value
187 );
188
189 /**
190 Scans a target buffer for a 32-bit value, and returns a pointer to the
191 matching 32-bit value in the target buffer.
192
193 @param Buffer Pointer to the target buffer to scan.
194 @param Length Number of bytes in Buffer to scan. Must be non-zero.
195 @param Value Value to search for in the target buffer.
196
197 @return Pointer to the first occurrence or NULL if not found.
198
199 **/
200 CONST VOID *
201 EFIAPI
202 InternalMemScanMem32 (
203 IN CONST VOID *Buffer,
204 IN UINTN Length,
205 IN UINT32 Value
206 );
207
208 /**
209 Scans a target buffer for a 64-bit value, and returns a pointer to the
210 matching 64-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 InternalMemScanMem64 (
222 IN CONST VOID *Buffer,
223 IN UINTN Length,
224 IN UINT64 Value
225 );
226
227 /**
228 Copy Length bytes from Source to Destination.
229
230 This function copies Length bytes from SourceBuffer to DestinationBuffer, and
231 returns DestinationBuffer. The implementation must be reentrant, and it must
232 handle the case where SourceBuffer overlaps DestinationBuffer.
233
234 If Length is greater than (MAX_ADDRESS - DestinationBuffer + 1), then
235 ASSERT().
236 If Length is greater than (MAX_ADDRESS - SourceBuffer + 1), then ASSERT().
237
238 @param Destination Target of copy
239 @param Source Place to copy from
240 @param Length Number of bytes to copy
241
242 @return Destination
243
244 **/
245 VOID *
246 EFIAPI
247 CopyMem (
248 OUT VOID *Destination,
249 IN CONST VOID *Source,
250 IN UINTN Length
251 )
252 {
253 ASSERT (Length <= MAX_ADDRESS - (UINTN)Destination + 1);
254 ASSERT (Length <= MAX_ADDRESS - (UINTN)Source + 1);
255 return InternalMemCopyMem (Destination, Source, Length);
256 }
257
258 /**
259 Set Buffer to Value for Size bytes.
260
261 This function fills Length bytes of Buffer with Value, and returns Buffer.
262
263 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
264
265 @param Buffer Memory to set.
266 @param Size Number of bytes to set
267 @param Value Value of the set operation.
268
269 @return Buffer
270
271 **/
272 VOID *
273 EFIAPI
274 SetMem (
275 IN VOID *Buffer,
276 IN UINTN Size,
277 IN UINT8 Value
278 )
279 {
280 ASSERT (Size <= MAX_ADDRESS - (UINTN)Buffer + 1);
281 return InternalMemSetMem (Buffer, Size, Value);
282 }
283
284 /**
285 Fills a target buffer with a 16-bit value, and returns the target buffer.
286
287 This function fills Length bytes of Buffer with the 16-bit value specified by
288 Value, and returns Buffer. Value is repeated every 16-bits in for Length
289 bytes of Buffer.
290
291 If Buffer is NULL and Length > 0, then ASSERT().
292 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
293 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
294 If Length is not aligned on a 16-bit boundary, then ASSERT().
295
296 @param Buffer Pointer to the target buffer to fill.
297 @param Length Number of bytes in Buffer to fill.
298 @param Value Value with which to fill Length bytes of Buffer.
299
300 @return Buffer
301
302 **/
303 VOID *
304 EFIAPI
305 SetMem16 (
306 OUT VOID *Buffer,
307 IN UINTN Length,
308 IN UINT16 Value
309 )
310 {
311 ASSERT (Buffer != NULL);
312 ASSERT (Length <= MAX_ADDRESS - (UINTN)Buffer + 1);
313 ASSERT ((((UINTN)Buffer) & 1) != 0);
314 ASSERT ((Length & 1) != 0);
315
316 if ((Length /= sizeof (Value)) == 0) {
317 return Buffer;
318 }
319 return InternalMemSetMem16 (Buffer, Length, Value);
320 }
321
322 /**
323 Fills a target buffer with a 32-bit value, and returns the target buffer.
324
325 This function fills Length bytes of Buffer with the 32-bit value specified by
326 Value, and returns Buffer. Value is repeated every 32-bits in for Length
327 bytes of Buffer.
328
329 If Buffer is NULL and Length > 0, then ASSERT().
330 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
331 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
332 If Length is not aligned on a 32-bit boundary, then ASSERT().
333
334 @param Buffer Pointer to the target buffer to fill.
335 @param Length Number of bytes in Buffer to fill.
336 @param Value Value with which to fill Length bytes of Buffer.
337
338 @return Buffer
339
340 **/
341 VOID *
342 EFIAPI
343 SetMem32 (
344 OUT VOID *Buffer,
345 IN UINTN Length,
346 IN UINT32 Value
347 )
348 {
349 ASSERT (Buffer != NULL);
350 ASSERT (Length <= MAX_ADDRESS - (UINTN)Buffer + 1);
351 ASSERT ((((UINTN)Buffer) & 1) != 0);
352 ASSERT ((Length & 1) != 0);
353
354 if ((Length /= sizeof (Value)) == 0) {
355 return Buffer;
356 }
357 return InternalMemSetMem32 (Buffer, Length, Value);
358 }
359
360 /**
361 Fills a target buffer with a 64-bit value, and returns the target buffer.
362
363 This function fills Length bytes of Buffer with the 64-bit value specified by
364 Value, and returns Buffer. Value is repeated every 64-bits in for Length
365 bytes of Buffer.
366
367 If Buffer is NULL and Length > 0, then ASSERT().
368 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
369 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
370 If Length is not aligned on a 64-bit boundary, then ASSERT().
371
372 @param Buffer Pointer to the target buffer to fill.
373 @param Length Number of bytes in Buffer to fill.
374 @param Value Value with which to fill Length bytes of Buffer.
375
376 @return Buffer
377
378 **/
379 VOID *
380 EFIAPI
381 SetMem64 (
382 OUT VOID *Buffer,
383 IN UINTN Length,
384 IN UINT64 Value
385 )
386 {
387 ASSERT (Buffer != NULL);
388 ASSERT (Length <= MAX_ADDRESS - (UINTN)Buffer + 1);
389 ASSERT ((((UINTN)Buffer) & 1) != 0);
390 ASSERT ((Length & 1) != 0);
391
392 if ((Length /= sizeof (Value)) == 0) {
393 return Buffer;
394 }
395 return InternalMemSetMem64 (Buffer, Length, Value);
396 }
397
398 /**
399 Set Buffer to 0 for Size bytes.
400
401 This function fills Length bytes of Buffer with zeros, and returns Buffer.
402
403 If Buffer is NULL and Length > 0, then ASSERT().
404 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
405
406 @param Buffer Memory to set.
407 @param Size Number of bytes to set
408
409 @return Buffer
410
411 **/
412 VOID *
413 EFIAPI
414 ZeroMem (
415 IN VOID *Buffer,
416 IN UINTN Size
417 )
418 {
419 ASSERT (Buffer != NULL);
420 return InternalMemSetMem (Buffer, Size, 0);
421 }
422
423 /**
424 Compares two memory buffers of a given length.
425
426 This function compares Length bytes of SourceBuffer to Length bytes of
427 DestinationBuffer. If all Length bytes of the two buffers are identical, then
428 0 is returned. Otherwise, the value returned is the first mismatched byte in
429 SourceBuffer subtracted from the first mismatched byte in DestinationBuffer.
430
431 If DestinationBuffer is NULL and Length > 0, then ASSERT().
432 If SourceBuffer is NULL and Length > 0, then ASSERT().
433 If Length is greater than (MAX_ADDRESS - DestinationBuffer + 1), then
434 ASSERT().
435 If Length is greater than (MAX_ADDRESS - SourceBuffer + 1), then ASSERT().
436
437 @param DestinationBuffer First memory buffer
438 @param SourceBuffer Second memory buffer
439 @param Length Length of DestinationBuffer and SourceBuffer memory
440 regions to compare
441
442 @retval 0 if DestinationBuffer == SourceBuffer
443 @retval Non-zero if DestinationBuffer != SourceBuffer
444
445 **/
446 INTN
447 EFIAPI
448 CompareMem (
449 IN CONST VOID *DestinationBuffer,
450 IN CONST VOID *SourceBuffer,
451 IN UINTN Length
452 )
453 {
454 ASSERT (DestinationBuffer != NULL);
455 ASSERT (SourceBuffer != NULL);
456 ASSERT (Length <= MAX_ADDRESS - (UINTN)DestinationBuffer + 1);
457 ASSERT (Length <= MAX_ADDRESS - (UINTN)SourceBuffer + 1);
458 if (Length == 0) {
459 return 0;
460 }
461 return InternalMemCompareMem (DestinationBuffer, SourceBuffer, Length);
462 }
463
464 /**
465 Scans a target buffer for an 8-bit value, and returns a pointer to the
466 matching 8-bit value in the target buffer.
467
468 This function searches target the buffer specified by Buffer and Length from
469 the lowest address to the highest address for an 8-bit value that matches
470 Value. If a match is found, then a pointer to the matching byte in the target
471 buffer is returned. If no match is found, then NULL is returned. If Length is
472 0, then NULL is returned.
473
474 If Buffer is NULL, then ASSERT().
475 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
476
477 @param Buffer Pointer to the target buffer to scan.
478 @param Length Number of bytes in Buffer to scan.
479 @param Value Value to search for in the target buffer.
480
481 @return Pointer to the first occurrence or NULL if not found.
482 @retval NULL if Length == 0 or Value was not found.
483
484 **/
485 VOID *
486 EFIAPI
487 ScanMem8 (
488 IN CONST VOID *Buffer,
489 IN UINTN Length,
490 IN UINT8 Value
491 )
492 {
493 ASSERT (Buffer != NULL);
494 ASSERT (Length <= MAX_ADDRESS + (UINTN)Buffer + 1);
495
496 if ((Length /= sizeof (Value)) == 0) {
497 return NULL;
498 }
499 return (VOID*)InternalMemScanMem8 (Buffer, Length, Value);
500 }
501
502 /**
503 Scans a target buffer for a 16-bit value, and returns a pointer to the
504 matching 16-bit value in the target buffer.
505
506 This function searches target the buffer specified by Buffer and Length from
507 the lowest address to the highest address at 16-bit increments for a 16-bit
508 value that matches Value. If a match is found, then a pointer to the matching
509 value in the target buffer is returned. If no match is found, then NULL is
510 returned. If Length is 0, then NULL is returned.
511
512 If Buffer is NULL, then ASSERT().
513 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
514 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
515
516 @param Buffer Pointer to the target buffer to scan.
517 @param Length Number of bytes in Buffer to scan.
518 @param Value Value to search for in the target buffer.
519
520 @return Pointer to the first occurrence.
521 @retval NULL if Length == 0 or Value was not found.
522
523 **/
524 VOID *
525 EFIAPI
526 ScanMem16 (
527 IN CONST VOID *Buffer,
528 IN UINTN Length,
529 IN UINT16 Value
530 )
531 {
532 ASSERT (Buffer != NULL);
533 ASSERT (((UINTN)Buffer & (sizeof (Value) - 1)) == 0);
534 ASSERT (Length <= MAX_ADDRESS + (UINTN)Buffer + 1);
535
536 if ((Length /= sizeof (Value)) == 0) {
537 return NULL;
538 }
539 return (VOID*)InternalMemScanMem16 (Buffer, Length, Value);
540 }
541
542 /**
543 Scans a target buffer for a 32-bit value, and returns a pointer to the
544 matching 32-bit value in the target buffer.
545
546 This function searches target the buffer specified by Buffer and Length from
547 the lowest address to the highest address at 32-bit increments for a 32-bit
548 value that matches Value. If a match is found, then a pointer to the matching
549 value in the target buffer is returned. If no match is found, then NULL is
550 returned. If Length is 0, then NULL is returned.
551
552 If Buffer is NULL, then ASSERT().
553 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
554 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
555
556 @param Buffer Pointer to the target buffer to scan.
557 @param Length Number of bytes in Buffer to scan.
558 @param Value Value to search for in the target buffer.
559
560 @return Pointer to the first occurrence or NULL if not found.
561 @retval NULL if Length == 0 or Value was not found.
562
563 **/
564 VOID *
565 EFIAPI
566 ScanMem32 (
567 IN CONST VOID *Buffer,
568 IN UINTN Length,
569 IN UINT32 Value
570 )
571 {
572 ASSERT (Buffer != NULL);
573 ASSERT (((UINTN)Buffer & (sizeof (Value) - 1)) == 0);
574 ASSERT (Length <= MAX_ADDRESS + (UINTN)Buffer + 1);
575
576 if ((Length /= sizeof (Value)) == 0) {
577 return NULL;
578 }
579 return (VOID*)InternalMemScanMem32 (Buffer, Length, Value);
580 }
581
582 /**
583 Scans a target buffer for a 64-bit value, and returns a pointer to the
584 matching 64-bit value in the target buffer.
585
586 This function searches target the buffer specified by Buffer and Length from
587 the lowest address to the highest address at 64-bit increments for a 64-bit
588 value that matches Value. If a match is found, then a pointer to the matching
589 value in the target buffer is returned. If no match is found, then NULL is
590 returned. If Length is 0, then NULL is returned.
591
592 If Buffer is NULL, then ASSERT().
593 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
594 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
595
596 @param Buffer Pointer to the target buffer to scan.
597 @param Length Number of bytes in Buffer to scan.
598 @param Value Value to search for in the target buffer.
599
600 @return Pointer to the first occurrence or NULL if not found.
601 @retval NULL if Length == 0 or Value was not found.
602
603 **/
604 VOID *
605 EFIAPI
606 ScanMem64 (
607 IN CONST VOID *Buffer,
608 IN UINTN Length,
609 IN UINT64 Value
610 )
611 {
612 ASSERT (Buffer != NULL);
613 ASSERT (((UINTN)Buffer & (sizeof (Value) - 1)) == 0);
614 ASSERT (Length <= MAX_ADDRESS + (UINTN)Buffer + 1);
615
616 if ((Length /= sizeof (Value)) == 0) {
617 return NULL;
618 }
619 return (VOID*)InternalMemScanMem64 (Buffer, Length, Value);
620 }