Author: jgardou
Date: Sun Jul 10 17:21:38 2011
New Revision: 52614

URL: http://svn.reactos.org/svn/reactos?rev=52614&view=rev
Log:
[NTOSKRNL/MM]
- fix broken logic
- simplify calculation of remainig space to alter
- call ExFreePoolWithTag instead of ExFreePool
- return error on failure

Modified:
    trunk/reactos/ntoskrnl/mm/region.c

Modified: trunk/reactos/ntoskrnl/mm/region.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/mm/region.c?rev=52614&r1=52613&r2=52614&view=diff
==============================================================================
--- trunk/reactos/ntoskrnl/mm/region.c [iso-8859-1] (original)
+++ trunk/reactos/ntoskrnl/mm/region.c [iso-8859-1] Sun Jul 10 17:21:38 2011
@@ -53,7 +53,7 @@
                                       TAG_MM_REGION);
    if (NewRegion1 == NULL)
    {
-      ExFreePool(NewRegion2);
+      ExFreePoolWithTag(NewRegion2, TAG_MM_REGION);
       return(NULL);
    }
    NewRegion1->Type = NewType;
@@ -86,14 +86,14 @@
    }
    else
    {
-      ExFreePool(NewRegion2);
+      ExFreePoolWithTag(NewRegion2, TAG_MM_REGION);
    }
 
    /* Either remove or shrink the initial region. */
    if (InitialBaseAddress == StartAddress)
    {
       RemoveEntryList(&InitialRegion->RegionListEntry);
-      ExFreePool(InitialRegion);
+      ExFreePoolWithTag(InitialRegion, TAG_MM_REGION);
    }
    else
    {
@@ -123,17 +123,6 @@
     */
    InitialRegion = MmFindRegion(BaseAddress, RegionListHead, StartAddress,
                                 &InitialBaseAddress);
-   if (((char*)StartAddress + Length) >
-         ((char*)InitialBaseAddress + InitialRegion->Length))
-   {
-      RemainingLength = ((char*)StartAddress + Length) -
-                        ((char*)InitialBaseAddress + InitialRegion->Length);
-   }
-   else
-   {
-      RemainingLength = 0;
-   }
-
    /*
     * If necessary then split the region into the affected and unaffected 
parts.
     */
@@ -151,6 +140,11 @@
    {
       NewRegion = InitialRegion;
    }
+   
+   if(NewRegion->Length < Length)
+      RemainingLength = Length - NewRegion->Length;
+   else
+      RemainingLength = 0;
 
    /*
     * Free any complete regions that are containing in the range of addresses
@@ -163,7 +157,7 @@
    while (RemainingLength > 0 && CurrentRegion->Length <= RemainingLength &&
           CurrentEntry != RegionListHead)
    {
-      if (CurrentRegion->Type != NewType &&
+      if (CurrentRegion->Type != NewType ||
             CurrentRegion->Protect != NewProtect)
       {
          AlterFunc(AddressSpace, CurrentBaseAddress, CurrentRegion->Length,
@@ -176,7 +170,7 @@
       RemainingLength -= CurrentRegion->Length;
       CurrentEntry = CurrentEntry->Flink;
       RemoveEntryList(&CurrentRegion->RegionListEntry);
-      ExFreePool(CurrentRegion);
+      ExFreePoolWithTag(CurrentRegion, TAG_MM_REGION);
       CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
                                         RegionListEntry);
    }
@@ -188,10 +182,10 @@
    {
       CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
                                         RegionListEntry);
-      if (CurrentRegion->Type != NewType &&
+      if (CurrentRegion->Type != NewType ||
             CurrentRegion->Protect != NewProtect)
       {
-         AlterFunc(AddressSpace, CurrentBaseAddress, CurrentRegion->Length,
+         AlterFunc(AddressSpace, CurrentBaseAddress, RemainingLength,
                    CurrentRegion->Type, CurrentRegion->Protect,
                    NewType, NewProtect);
       }
@@ -212,7 +206,7 @@
       {
          NewRegion->Length += CurrentRegion->Length;
          RemoveEntryList(&CurrentRegion->RegionListEntry);
-         ExFreePool(CurrentRegion);
+         ExFreePoolWithTag(CurrentRegion, TAG_MM_REGION);
       }
    }
 
@@ -229,9 +223,12 @@
       {
          NewRegion->Length += CurrentRegion->Length;
          RemoveEntryList(&CurrentRegion->RegionListEntry);
-         ExFreePool(CurrentRegion);
-      }
-   }
+         ExFreePoolWithTag(CurrentRegion, TAG_MM_REGION);
+      }
+   }
+   
+   if(NewRegion->Length < Length)
+      return(STATUS_NO_MEMORY);
 
    return(STATUS_SUCCESS);
 }


Reply via email to