]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/UefiMemoryLib/MemLibGeneric.c
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / UefiMemoryLib / MemLibGeneric.c
index bda1f4992f7945ffa063a3578651339e25a22711..73ef0d92d1ca12426b4d694950c5ed1b26b56b53 100644 (file)
@@ -1,23 +1,14 @@
 /** @file\r
   Architecture Independent Base Memory Library Implementation.\r
 \r
-  Copyright (c) 2006, Intel Corporation<BR>\r
-  All rights reserved. This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions of the BSD License\r
-  which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-  Module Name:  MemLibGeneric.c\r
-\r
-  The following BaseMemoryLib instances share the same version of this file:\r
-\r
+  The following BaseMemoryLib instances contain the same copy of this file:\r
     BaseMemoryLib\r
     PeiMemoryLib\r
     UefiMemoryLib\r
 \r
+  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
 **/\r
 \r
 #include "MemLibInternals.h"\r
@@ -25,9 +16,9 @@
 /**\r
   Fills a target buffer with a 16-bit value, and returns the target buffer.\r
 \r
-  @param  Buffer  Pointer to the target buffer to fill.\r
-  @param  Length  Number of bytes in Buffer to fill.\r
-  @param  Value   Value with which to fill Length bytes of Buffer.\r
+  @param  Buffer  The pointer to the target buffer to fill.\r
+  @param  Length  The count of 16-bit value to fill.\r
+  @param  Value   The value with which to fill Length bytes of Buffer.\r
 \r
   @return Buffer\r
 \r
@@ -40,18 +31,18 @@ InternalMemSetMem16 (
   IN      UINT16                    Value\r
   )\r
 {\r
-  do {\r
-    ((UINT16*)Buffer)[--Length] = Value;\r
-  } while (Length != 0);\r
+  for (; Length != 0; Length--) {\r
+    ((UINT16*)Buffer)[Length - 1] = Value;\r
+  }\r
   return Buffer;\r
 }\r
 \r
 /**\r
   Fills a target buffer with a 32-bit value, and returns the target buffer.\r
 \r
-  @param  Buffer  Pointer to the target buffer to fill.\r
-  @param  Length  Number of bytes in Buffer to fill.\r
-  @param  Value   Value with which to fill Length bytes of Buffer.\r
+  @param  Buffer  The pointer to the target buffer to fill.\r
+  @param  Length  The count of 32-bit value to fill.\r
+  @param  Value   The value with which to fill Length bytes of Buffer.\r
 \r
   @return Buffer\r
 \r
@@ -64,18 +55,18 @@ InternalMemSetMem32 (
   IN      UINT32                    Value\r
   )\r
 {\r
-  do {\r
-    ((UINT32*)Buffer)[--Length] = Value;\r
-  } while (Length != 0);\r
+  for (; Length != 0; Length--) {\r
+    ((UINT32*)Buffer)[Length - 1] = Value;\r
+  }\r
   return Buffer;\r
 }\r
 \r
 /**\r
   Fills a target buffer with a 64-bit value, and returns the target buffer.\r
 \r
-  @param  Buffer  Pointer to the target buffer to fill.\r
-  @param  Length  Number of bytes in Buffer to fill.\r
-  @param  Value   Value with which to fill Length bytes of Buffer.\r
+  @param  Buffer  The pointer to the target buffer to fill.\r
+  @param  Length  The count of 64-bit value to fill.\r
+  @param  Value   The value with which to fill Length bytes of Buffer.\r
 \r
   @return Buffer\r
 \r
@@ -88,9 +79,9 @@ InternalMemSetMem64 (
   IN      UINT64                    Value\r
   )\r
 {\r
-  do {\r
-    ((UINT64*)Buffer)[--Length] = Value;\r
-  } while (Length != 0);\r
+  for (; Length != 0; Length--) {\r
+    ((UINT64*)Buffer)[Length - 1] = Value;\r
+  }\r
   return Buffer;\r
 }\r
 \r
@@ -98,7 +89,7 @@ InternalMemSetMem64 (
   Set Buffer to 0 for Size bytes.\r
 \r
   @param  Buffer Memory to set.\r
-  @param  Size Number of bytes to set\r
+  @param  Length The number of bytes to set\r
 \r
   @return Buffer\r
 \r
@@ -116,12 +107,14 @@ InternalMemZeroMem (
 /**\r
   Compares two memory buffers of a given length.\r
 \r
-  @param  DestinationBuffer First memory buffer\r
-  @param  SourceBuffer      Second memory buffer\r
-  @param  Length            Length of DestinationBuffer and SourceBuffer memory\r
+  @param  DestinationBuffer The first memory buffer\r
+  @param  SourceBuffer      The second memory buffer\r
+  @param  Length            The length of DestinationBuffer and SourceBuffer memory\r
                             regions to compare. Must be non-zero.\r
 \r
-  @retval 0     if MemOne == MemTwo\r
+  @return 0                 All Length bytes of the two buffers are identical.\r
+  @retval Non-zero          The first mismatched byte in SourceBuffer subtracted from the first\r
+                            mismatched byte in DestinationBuffer.\r
 \r
 **/\r
 INTN\r
@@ -132,7 +125,6 @@ InternalMemCompareMem (
   IN      UINTN                     Length\r
   )\r
 {\r
-  ASSERT (Length > 0);\r
   while ((--Length != 0) &&\r
          (*(INT8*)DestinationBuffer == *(INT8*)SourceBuffer)) {\r
     DestinationBuffer = (INT8*)DestinationBuffer + 1;\r
@@ -145,11 +137,11 @@ InternalMemCompareMem (
   Scans a target buffer for an 8-bit value, and returns a pointer to the\r
   matching 8-bit value in the target buffer.\r
 \r
-  @param  Buffer  Pointer to the target buffer to scan.\r
-  @param  Length  Number of bytes in Buffer to scan. Must be non-zero.\r
-  @param  Value   Value to search for in the target buffer.\r
+  @param  Buffer  The pointer to the target buffer to scan.\r
+  @param  Length  The count of 8-bit value to scan. Must be non-zero.\r
+  @param  Value   The value to search for in the target buffer.\r
 \r
-  @return Pointer to the first occurrence or NULL if not found.\r
+  @return The pointer to the first occurrence or NULL if not found.\r
 \r
 **/\r
 CONST VOID *\r
@@ -162,11 +154,10 @@ InternalMemScanMem8 (
 {\r
   CONST UINT8                       *Pointer;\r
 \r
-  ASSERT (Length > 0);\r
   Pointer = (CONST UINT8*)Buffer;\r
   do {\r
     if (*(Pointer++) == Value) {\r
-      return Pointer;\r
+      return --Pointer;\r
     }\r
   } while (--Length != 0);\r
   return NULL;\r
@@ -176,11 +167,11 @@ InternalMemScanMem8 (
   Scans a target buffer for a 16-bit value, and returns a pointer to the\r
   matching 16-bit value in the target buffer.\r
 \r
-  @param  Buffer  Pointer to the target buffer to scan.\r
-  @param  Length  Number of bytes in Buffer to scan. Must be non-zero.\r
-  @param  Value   Value to search for in the target buffer.\r
+  @param  Buffer  The pointer to the target buffer to scan.\r
+  @param  Length  The count of 16-bit value to scan. Must be non-zero.\r
+  @param  Value   The value to search for in the target buffer.\r
 \r
-  @return Pointer to the first occurrence or NULL if not found.\r
+  @return The pointer to the first occurrence or NULL if not found.\r
 \r
 **/\r
 CONST VOID *\r
@@ -193,11 +184,10 @@ InternalMemScanMem16 (
 {\r
   CONST UINT16                      *Pointer;\r
 \r
-  ASSERT (Length > 0);\r
   Pointer = (CONST UINT16*)Buffer;\r
   do {\r
     if (*(Pointer++) == Value) {\r
-      return Pointer;\r
+      return --Pointer;\r
     }\r
   } while (--Length != 0);\r
   return NULL;\r
@@ -207,11 +197,11 @@ InternalMemScanMem16 (
   Scans a target buffer for a 32-bit value, and returns a pointer to the\r
   matching 32-bit value in the target buffer.\r
 \r
-  @param  Buffer  Pointer to the target buffer to scan.\r
-  @param  Length  Number of bytes in Buffer to scan. Must be non-zero.\r
-  @param  Value   Value to search for in the target buffer.\r
+  @param  Buffer  The pointer to the target buffer to scan.\r
+  @param  Length  The count of 32-bit value to scan. Must be non-zero.\r
+  @param  Value   The value to search for in the target buffer.\r
 \r
-  @return Pointer to the first occurrence or NULL if not found.\r
+  @return The pointer to the first occurrence or NULL if not found.\r
 \r
 **/\r
 CONST VOID *\r
@@ -224,11 +214,10 @@ InternalMemScanMem32 (
 {\r
   CONST UINT32                      *Pointer;\r
 \r
-  ASSERT (Length > 0);\r
   Pointer = (CONST UINT32*)Buffer;\r
   do {\r
     if (*(Pointer++) == Value) {\r
-      return Pointer;\r
+      return --Pointer;\r
     }\r
   } while (--Length != 0);\r
   return NULL;\r
@@ -238,11 +227,11 @@ InternalMemScanMem32 (
   Scans a target buffer for a 64-bit value, and returns a pointer to the\r
   matching 64-bit value in the target buffer.\r
 \r
-  @param  Buffer  Pointer to the target buffer to scan.\r
-  @param  Length  Number of bytes in Buffer to scan. Must be non-zero.\r
-  @param  Value   Value to search for in the target buffer.\r
+  @param  Buffer  The pointer to the target buffer to scan.\r
+  @param  Length  The count of 64-bit value to scan. Must be non-zero.\r
+  @param  Value   The value to search for in the target buffer.\r
 \r
-  @return Pointer to the first occurrence or NULL if not found.\r
+  @return The pointer to the first occurrence or NULL if not found.\r
 \r
 **/\r
 CONST VOID *\r
@@ -255,12 +244,40 @@ InternalMemScanMem64 (
 {\r
   CONST UINT64                      *Pointer;\r
 \r
-  ASSERT (Length > 0);\r
   Pointer = (CONST UINT64*)Buffer;\r
   do {\r
     if (*(Pointer++) == Value) {\r
-      return Pointer;\r
+      return --Pointer;\r
     }\r
   } while (--Length != 0);\r
   return NULL;\r
 }\r
+\r
+/**\r
+  Checks whether the contents of a buffer are all zeros.\r
+\r
+  @param  Buffer  The pointer to the buffer to be checked.\r
+  @param  Length  The size of the buffer (in bytes) to be checked.\r
+\r
+  @retval TRUE    Contents of the buffer are all zeros.\r
+  @retval FALSE   Contents of the buffer are not all zeros.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+InternalMemIsZeroBuffer (\r
+  IN CONST VOID  *Buffer,\r
+  IN UINTN       Length\r
+  )\r
+{\r
+  CONST UINT8 *BufferData;\r
+  UINTN       Index;\r
+\r
+  BufferData = Buffer;\r
+  for (Index = 0; Index < Length; Index++) {\r
+    if (BufferData[Index] != 0) {\r
+      return FALSE;\r
+    }\r
+  }\r
+  return TRUE;\r
+}\r