UefiCpuPkg/MtrrLib: Avoid running unnecessary code
authorRuiyu Ni <ruiyu.ni@intel.com>
Mon, 17 Apr 2017 03:20:47 +0000 (11:20 +0800)
committerRuiyu Ni <ruiyu.ni@intel.com>
Tue, 18 Apr 2017 02:29:04 +0000 (10:29 +0800)
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Hao A Wu <hao.a.wu@intel.com>
UefiCpuPkg/Library/MtrrLib/MtrrLib.c

index c38191a7efc15a9bab859381e7817d0b4e64141c..9d1927262a4edee650bd18621fac69cba7f73738 100644 (file)
@@ -1536,6 +1536,7 @@ MtrrLibAddVariableMtrr (
 \r
   MTRR_LIB_ASSERT_ALIGNED (BaseAddress, Length);\r
   if (Type == CacheInvalid) {\r
+    ASSERT (Ranges != NULL);\r
     for (Index = 0; Index < RangeCount; Index++) {\r
       if (Ranges[Index].BaseAddress <= BaseAddress && BaseAddress < Ranges[Index].BaseAddress + Ranges[Index].Length) {\r
 \r
@@ -2027,25 +2028,27 @@ MtrrSetMemoryAttributeWorker (
   ASSERT (OriginalVariableMtrrCount - FreeVariableMtrrCount <= FirmwareVariableMtrrCount);\r
 \r
   //\r
-  // Move MTRRs after the FirmwraeVariableMtrrCount position to beginning\r
+  // Move MTRRs after the FirmwareVariableMtrrCount position to beginning\r
   //\r
-  WorkingIndex = FirmwareVariableMtrrCount;\r
-  for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {\r
-    if (!OriginalVariableMtrr[Index].Valid) {\r
-      //\r
-      // Found an empty MTRR in WorkingIndex position\r
-      //\r
-      for (; WorkingIndex < OriginalVariableMtrrCount; WorkingIndex++) {\r
-        if (OriginalVariableMtrr[WorkingIndex].Valid) {\r
-          break;\r
+  if (FirmwareVariableMtrrCount < OriginalVariableMtrrCount) {\r
+    WorkingIndex = FirmwareVariableMtrrCount;\r
+    for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {\r
+      if (!OriginalVariableMtrr[Index].Valid) {\r
+        //\r
+        // Found an empty MTRR in WorkingIndex position\r
+        //\r
+        for (; WorkingIndex < OriginalVariableMtrrCount; WorkingIndex++) {\r
+          if (OriginalVariableMtrr[WorkingIndex].Valid) {\r
+            break;\r
+          }\r
         }\r
-      }\r
 \r
-      if (WorkingIndex != OriginalVariableMtrrCount) {\r
-        CopyMem (&OriginalVariableMtrr[Index], &OriginalVariableMtrr[WorkingIndex], sizeof (VARIABLE_MTRR));\r
-        VariableSettingModified[Index] = TRUE;\r
-        VariableSettingModified[WorkingIndex] = TRUE;\r
-        OriginalVariableMtrr[WorkingIndex].Valid = FALSE;\r
+        if (WorkingIndex != OriginalVariableMtrrCount) {\r
+          CopyMem (&OriginalVariableMtrr[Index], &OriginalVariableMtrr[WorkingIndex], sizeof (VARIABLE_MTRR));\r
+          VariableSettingModified[Index] = TRUE;\r
+          VariableSettingModified[WorkingIndex] = TRUE;\r
+          OriginalVariableMtrr[WorkingIndex].Valid = FALSE;\r
+        }\r
       }\r
     }\r
   }\r