- Changed MmGetContinuousPages from using an alignment to using a boundary.
- Removed MmAllocateContiguousAlignedMemory.
Modified: trunk/reactos/ntoskrnl/include/internal/mm.h
Modified: trunk/reactos/ntoskrnl/mm/cont.c
Modified: trunk/reactos/ntoskrnl/mm/freelist.c
Modified: trunk/reactos/ntoskrnl/ntoskrnl.def

Modified: trunk/reactos/ntoskrnl/include/internal/mm.h
--- trunk/reactos/ntoskrnl/include/internal/mm.h	2005-08-07 23:05:57 UTC (rev 17184)
+++ trunk/reactos/ntoskrnl/include/internal/mm.h	2005-08-07 23:07:17 UTC (rev 17185)
@@ -667,7 +667,7 @@
 PFN_TYPE MmGetContinuousPages(ULONG NumberOfBytes,
 			      PHYSICAL_ADDRESS LowestAcceptableAddress,
 			      PHYSICAL_ADDRESS HighestAcceptableAddress,
-			      ULONG Alignment);
+			      PHYSICAL_ADDRESS BoundaryAddressMultiple);
 
 NTSTATUS MmInitZeroPageThread(VOID);
 
@@ -795,12 +795,11 @@
 /* cont.c ********************************************************************/
 
 PVOID STDCALL
-MmAllocateContiguousAlignedMemory(IN ULONG NumberOfBytes,
-					  IN PHYSICAL_ADDRESS LowestAcceptableAddress,
-			          IN PHYSICAL_ADDRESS HighestAcceptableAddress,
-			          IN PHYSICAL_ADDRESS BoundaryAddressMultiple OPTIONAL,
-			          IN MEMORY_CACHING_TYPE CacheType OPTIONAL,
-					  IN ULONG Alignment);
+MmAllocateContiguousMemorySpecifyCache(IN SIZE_T NumberOfBytes,
+				       IN PHYSICAL_ADDRESS LowestAcceptableAddress,
+			               IN PHYSICAL_ADDRESS HighestAcceptableAddress,
+			               IN PHYSICAL_ADDRESS BoundaryAddressMultiple OPTIONAL,
+			               IN MEMORY_CACHING_TYPE CacheType OPTIONAL);
                       
 /* region.c ************************************************************/
 

Modified: trunk/reactos/ntoskrnl/mm/cont.c
--- trunk/reactos/ntoskrnl/mm/cont.c	2005-08-07 23:05:57 UTC (rev 17184)
+++ trunk/reactos/ntoskrnl/mm/cont.c	2005-08-07 23:07:17 UTC (rev 17185)
@@ -28,16 +28,44 @@
    }
 }
 
-/*
+/**********************************************************************
+ * NAME       EXPORTED
+ * MmAllocateContiguousMemorySpecifyCache@32
+ *
+ * DESCRIPTION
+  *  Allocates a range of physically contiguous memory
+ * with a cache parameter.
+ *
+ * ARGUMENTS
+ * NumberOfBytes
+ *  Size of the memory block to allocate;
+ *
+ * LowestAcceptableAddress
+ *  Lowest address valid for the caller.
+ *
+ * HighestAcceptableAddress
+ *  Highest address valid for the caller.
+ *
+ * BoundaryAddressMultiple
+ *  Address multiple not to be crossed by allocated buffer (optional).
+ *
+ * CacheType
+ *  Type of caching to use.
+ *
+ * RETURN VALUE
+ *  The virtual address of the memory block on success;
+ * NULL on error.
+ *
+ * REVISIONS
+ *
  * @implemented
  */
 PVOID STDCALL
-MmAllocateContiguousAlignedMemory(IN ULONG NumberOfBytes,
-                                  IN PHYSICAL_ADDRESS LowestAcceptableAddress OPTIONAL,
-                                  IN PHYSICAL_ADDRESS HighestAcceptableAddress,
-                                  IN PHYSICAL_ADDRESS BoundaryAddressMultiple OPTIONAL,
-                                  IN MEMORY_CACHING_TYPE CacheType OPTIONAL,
-                                  IN ULONG Alignment)
+MmAllocateContiguousMemorySpecifyCache(IN SIZE_T NumberOfBytes,
+                                       IN PHYSICAL_ADDRESS LowestAcceptableAddress OPTIONAL,
+                                       IN PHYSICAL_ADDRESS HighestAcceptableAddress,
+                                       IN PHYSICAL_ADDRESS BoundaryAddressMultiple OPTIONAL,
+                                       IN MEMORY_CACHING_TYPE CacheType OPTIONAL)
 {
    PMEMORY_AREA MArea;
    NTSTATUS Status;
@@ -66,7 +94,7 @@
                                &MArea,
                                FALSE,
                                FALSE,
-                               BoundaryAddressMultiple);
+                               (PHYSICAL_ADDRESS)0LL);
    MmUnlockAddressSpace(MmGetKernelAddressSpace());
 
    if (!NT_SUCCESS(Status))
@@ -77,7 +105,7 @@
    PBase = MmGetContinuousPages(NumberOfBytes,
                                 LowestAcceptableAddress,
                                 HighestAcceptableAddress,
-                                Alignment);
+                                BoundaryAddressMultiple);
    if (PBase == 0)
    {
       MmLockAddressSpace(MmGetKernelAddressSpace());
@@ -91,7 +119,7 @@
    for (i = 0; i < (PAGE_ROUND_UP(NumberOfBytes) / PAGE_SIZE); i++, PBase++)
    {
       MmCreateVirtualMapping(NULL,
-                             (char*)BaseAddress + (i * 4096),
+                             (char*)BaseAddress + (i * PAGE_SIZE),
                              Attributes,
                              &PBase,
 			     1);
@@ -130,18 +158,11 @@
 MmAllocateContiguousMemory (IN ULONG NumberOfBytes,
                             IN PHYSICAL_ADDRESS HighestAcceptableAddress)
 {
-   PHYSICAL_ADDRESS LowestAcceptableAddress;
-   PHYSICAL_ADDRESS BoundaryAddressMultiple;
-
-   LowestAcceptableAddress.QuadPart = 0;
-   BoundaryAddressMultiple.QuadPart = 0;
-
-   return(MmAllocateContiguousAlignedMemory(NumberOfBytes,
-          LowestAcceptableAddress,
-          HighestAcceptableAddress,
-          BoundaryAddressMultiple,
-          MmCached,
-          PAGE_SIZE));
+   return MmAllocateContiguousMemorySpecifyCache(NumberOfBytes,
+                                                 (PHYSICAL_ADDRESS)0LL,
+                                                 HighestAcceptableAddress,
+                                                 (PHYSICAL_ADDRESS)0LL,
+                                                 MmCached);
 }
 
 
@@ -181,53 +202,6 @@
 
 /**********************************************************************
  * NAME       EXPORTED
- * MmAllocateContiguousMemorySpecifyCache@32
- *
- * DESCRIPTION
-  *  Allocates a range of physically contiguous memory
- * with a cache parameter.
- *
- * ARGUMENTS
- * NumberOfBytes
- *  Size of the memory block to allocate;
- *
- * LowestAcceptableAddress
- *  Lowest address valid for the caller.
- *
- * HighestAcceptableAddress
- *  Highest address valid for the caller.
- *
- * BoundaryAddressMultiple
- *  Address multiple not to be crossed by allocated buffer (optional).
- *
- * CacheType
- *  Type of caching to use.
- *
- * RETURN VALUE
- *  The virtual address of the memory block on success;
- * NULL on error.
- *
- * REVISIONS
- *
- * @implemented
- */
-PVOID STDCALL
-MmAllocateContiguousMemorySpecifyCache (IN ULONG NumberOfBytes,
-                                        IN PHYSICAL_ADDRESS LowestAcceptableAddress,
-                                        IN PHYSICAL_ADDRESS HighestAcceptableAddress,
-                                        IN PHYSICAL_ADDRESS BoundaryAddressMultiple OPTIONAL,
-                                        IN MEMORY_CACHING_TYPE CacheType)
-{
-   return(MmAllocateContiguousAlignedMemory(NumberOfBytes,
-          LowestAcceptableAddress,
-          HighestAcceptableAddress,
-          BoundaryAddressMultiple,
-          CacheType,
-          PAGE_SIZE));
-}
-
-/**********************************************************************
- * NAME       EXPORTED
  * MmFreeContiguousMemorySpecifyCache@12
  *
  * DESCRIPTION

Modified: trunk/reactos/ntoskrnl/mm/freelist.c
--- trunk/reactos/ntoskrnl/mm/freelist.c	2005-08-07 23:05:57 UTC (rev 17184)
+++ trunk/reactos/ntoskrnl/mm/freelist.c	2005-08-07 23:07:17 UTC (rev 17185)
@@ -161,85 +161,102 @@
 MmGetContinuousPages(ULONG NumberOfBytes,
                      PHYSICAL_ADDRESS LowestAcceptableAddress,
                      PHYSICAL_ADDRESS HighestAcceptableAddress,
-                     ULONG Alignment)
+                     PHYSICAL_ADDRESS BoundaryAddressMultiple)
 {
    ULONG NrPages;
-   ULONG i;
+   ULONG i, j;
    ULONG start;
+   ULONG last;
    ULONG length;
+   ULONG boundary;
    KIRQL oldIrql;
 
    NrPages = PAGE_ROUND_UP(NumberOfBytes) / PAGE_SIZE;
 
    KeAcquireSpinLock(&PageListLock, &oldIrql);
 
-   start = -1;
-   length = 0;
-   for (i = (LowestAcceptableAddress.QuadPart / PAGE_SIZE); i < (HighestAcceptableAddress.QuadPart / PAGE_SIZE); )
+   last = min(HighestAcceptableAddress.QuadPart / PAGE_SIZE, MmPageArraySize - 1);
+   boundary = BoundaryAddressMultiple.QuadPart / PAGE_SIZE;
+
+   for (j = 0; j < 2; j++)
    {
-      if (MmPageArray[i].Flags.Type ==  MM_PHYSICAL_PAGE_FREE)
+      start = -1;
+      length = 0;
+      /* First try to allocate the pages above the 16MB area. This may fail 
+       * because there are not enough continuous pages or we cannot allocate 
+       * pages above the 16MB area because the caller has specify an upper limit. 
+       * The second try uses the specified lower limit.
+       */
+      for (i = j == 0 ? 0x100000 / PAGE_SIZE : LowestAcceptableAddress.QuadPart / PAGE_SIZE; i <= last; )
       {
-         if (start == (ULONG)-1)
+         if (MmPageArray[i].Flags.Type == MM_PHYSICAL_PAGE_FREE)
          {
-            start = i;
-            length = 1;
+            if (start == (ULONG)-1)
+            {
+               start = i;
+               length = 1;
+            }
+            else
+            {
+               length++;
+               if (boundary)
+               {
+                  if (start / boundary != i / boundary)
+                  {
+                      start = i;
+                      length = 1;
+                  }
+               }
+            }
+            if (length == NrPages)
+            {
+               break;
+            }
          }
          else
          {
-            length++;
+            start = (ULONG)-1;
          }
          i++;
-         if (length == NrPages)
+      }
+
+      if (start != (ULONG)-1 && length == NrPages)
+      {
+         for (i = start; i < (start + length); i++)
          {
-            break;
+            RemoveEntryList(&MmPageArray[i].ListEntry);
+            if (MmPageArray[i].Flags.Zero == 0)
+            {
+               UnzeroedPageCount--;
+            }
+            MmStats.NrFreePages--;
+            MmStats.NrSystemPages++;
+            MmPageArray[i].Flags.Type = MM_PHYSICAL_PAGE_USED;
+            MmPageArray[i].Flags.Consumer = MC_NPPOOL;
+            MmPageArray[i].ReferenceCount = 1;
+            MmPageArray[i].LockCount = 0;
+            MmPageArray[i].MapCount = 0;
+            MmPageArray[i].SavedSwapEntry = 0;
+            InsertTailList(&UsedPageListHeads[MC_NPPOOL],
+                           &MmPageArray[i].ListEntry);
          }
+         KeReleaseSpinLock(&PageListLock, oldIrql);
+         for (i = start; i < (start + length); i++)
+         {
+            if (MmPageArray[i].Flags.Zero == 0)
+            {
+	       MiZeroPage(i);
+            }
+            else
+            {
+      	       MmPageArray[i].Flags.Zero = 0;
+            }
+         }
+         return start;
       }
-      else
-      {
-         start = -1;
-         /*
-          * Fast forward to the base of the next aligned region
-          */
-         i = ROUND_UP((i + 1), (Alignment / PAGE_SIZE));
-      }
    }
-   if (start == (ULONG)-1 || length != NrPages)
-   {
-      KeReleaseSpinLock(&PageListLock, oldIrql);
-      return 0;
-   }
-   for (i = start; i < (start + length); i++)
-   {
-      RemoveEntryList(&MmPageArray[i].ListEntry);
-      if (MmPageArray[i].Flags.Zero == 0)
-      {
-         UnzeroedPageCount--;
-      }
-      MmStats.NrFreePages--;
-      MmStats.NrSystemPages++;
-      MmPageArray[i].Flags.Type = MM_PHYSICAL_PAGE_USED;
-      MmPageArray[i].Flags.Consumer = MC_NPPOOL;
-      MmPageArray[i].ReferenceCount = 1;
-      MmPageArray[i].LockCount = 0;
-      MmPageArray[i].MapCount = 0;
-      MmPageArray[i].SavedSwapEntry = 0;
-      InsertTailList(&UsedPageListHeads[MC_NPPOOL],
-                     &MmPageArray[i].ListEntry);
-   }
    KeReleaseSpinLock(&PageListLock, oldIrql);
-   for (i = start; i < (start + length); i++)
-   {
-      if (MmPageArray[i].Flags.Zero == 0)
-      {
-	 MiZeroPage(i);
-      }
-      else
-      {
-      	 MmPageArray[i].Flags.Zero = 0;
-      }
-   }
-
-   return start;
+   return 0;
 }
 
 

Modified: trunk/reactos/ntoskrnl/ntoskrnl.def
--- trunk/reactos/ntoskrnl/ntoskrnl.def	2005-08-07 23:05:57 UTC (rev 17184)
+++ trunk/reactos/ntoskrnl/ntoskrnl.def	2005-08-07 23:07:17 UTC (rev 17185)
@@ -680,7 +680,6 @@
 MmAddVerifierThunks@8
 MmAdjustWorkingSetSize@12
 MmAdvanceMdl@8
-MmAllocateContiguousAlignedMemory@36
 MmAllocateContiguousMemory@12
 MmAllocateContiguousMemorySpecifyCache@32
 MmAllocateMappingAddress@8