[RSHELL]
[reactos.git] / ntoskrnl / mm / region.c
index 9c79738..eb71540 100644 (file)
@@ -32,13 +32,13 @@ InsertAfterEntry(PLIST_ENTRY Previous,
 
 static PMM_REGION
 MmSplitRegion(PMM_REGION InitialRegion, PVOID InitialBaseAddress,
-              PVOID StartAddress, ULONG Length, ULONG NewType,
+              PVOID StartAddress, SIZE_T Length, ULONG NewType,
               ULONG NewProtect, PMMSUPPORT AddressSpace,
               PMM_ALTER_REGION_FUNC AlterFunc)
 {
    PMM_REGION NewRegion1;
    PMM_REGION NewRegion2;
-   ULONG InternalLength;
+   SIZE_T InternalLength;
 
    /* Allocate this in front otherwise the failure case is too difficult. */
    NewRegion2 = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_REGION),
@@ -53,7 +53,7 @@ MmSplitRegion(PMM_REGION InitialRegion, PVOID InitialBaseAddress,
                                       TAG_MM_REGION);
    if (NewRegion1 == NULL)
    {
-      ExFreePool(NewRegion2);
+      ExFreePoolWithTag(NewRegion2, TAG_MM_REGION);
       return(NULL);
    }
    NewRegion1->Type = NewType;
@@ -86,14 +86,14 @@ MmSplitRegion(PMM_REGION InitialRegion, PVOID InitialBaseAddress,
    }
    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
    {
@@ -106,7 +106,7 @@ MmSplitRegion(PMM_REGION InitialRegion, PVOID InitialBaseAddress,
 NTSTATUS
 NTAPI
 MmAlterRegion(PMMSUPPORT AddressSpace, PVOID BaseAddress,
-              PLIST_ENTRY RegionListHead, PVOID StartAddress, ULONG Length,
+              PLIST_ENTRY RegionListHead, PVOID StartAddress, SIZE_T Length,
               ULONG NewType, ULONG NewProtect, PMM_ALTER_REGION_FUNC AlterFunc)
 {
    PMM_REGION InitialRegion;
@@ -115,7 +115,7 @@ MmAlterRegion(PMMSUPPORT AddressSpace, PVOID BaseAddress,
    PLIST_ENTRY CurrentEntry;
    PMM_REGION CurrentRegion = NULL;
    PVOID CurrentBaseAddress;
-   ULONG RemainingLength;
+   SIZE_T RemainingLength;
 
    /*
     * Find the first region containing part of the range of addresses to
@@ -123,17 +123,6 @@ MmAlterRegion(PMMSUPPORT AddressSpace, PVOID BaseAddress,
     */
    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.
     */
@@ -146,10 +135,19 @@ MmAlterRegion(PMMSUPPORT AddressSpace, PVOID BaseAddress,
       {
          return(STATUS_NO_MEMORY);
       }
+      if(NewRegion->Length < Length)
+         RemainingLength = Length - NewRegion->Length;
+      else
+         RemainingLength = 0;
    }
    else
    {
       NewRegion = InitialRegion;
+      if(((ULONG_PTR)InitialBaseAddress + NewRegion->Length) <
+            ((ULONG_PTR)StartAddress + Length))
+         RemainingLength = ((ULONG_PTR)StartAddress + Length) - ((ULONG_PTR)InitialBaseAddress + NewRegion->Length);
+      else
+         RemainingLength = 0;
    }
 
    /*
@@ -163,7 +161,7 @@ MmAlterRegion(PMMSUPPORT AddressSpace, PVOID BaseAddress,
    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 +174,7 @@ MmAlterRegion(PMMSUPPORT AddressSpace, PVOID BaseAddress,
       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 +186,10 @@ MmAlterRegion(PMMSUPPORT AddressSpace, PVOID BaseAddress,
    {
       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 +210,7 @@ MmAlterRegion(PMMSUPPORT AddressSpace, PVOID BaseAddress,
       {
          NewRegion->Length += CurrentRegion->Length;
          RemoveEntryList(&CurrentRegion->RegionListEntry);
-         ExFreePool(CurrentRegion);
+         ExFreePoolWithTag(CurrentRegion, TAG_MM_REGION);
       }
    }
 
@@ -229,7 +227,7 @@ MmAlterRegion(PMMSUPPORT AddressSpace, PVOID BaseAddress,
       {
          NewRegion->Length += CurrentRegion->Length;
          RemoveEntryList(&CurrentRegion->RegionListEntry);
-         ExFreePool(CurrentRegion);
+         ExFreePoolWithTag(CurrentRegion, TAG_MM_REGION);
       }
    }