[NTOS/MM]
[reactos.git] / reactos / ntoskrnl / mm / balance.c
index a82906b..e4b108a 100644 (file)
@@ -59,7 +59,7 @@ MmInitializeBalancer(ULONG NrAvailablePages, ULONG NrSystemPages)
     MiNrTotalPages = NrAvailablePages;
 
     /* Set up targets. */
     MiNrTotalPages = NrAvailablePages;
 
     /* Set up targets. */
-    MiMinimumAvailablePages = 128;
+    MiMinimumAvailablePages = 256;
     MiMinimumPagesPerRun = 256;
     if ((NrAvailablePages + NrSystemPages) >= 8192)
     {
     MiMinimumPagesPerRun = 256;
     if ((NrAvailablePages + NrSystemPages) >= 8192)
     {
@@ -96,9 +96,6 @@ NTSTATUS
 NTAPI
 MmReleasePageMemoryConsumer(ULONG Consumer, PFN_NUMBER Page)
 {
 NTAPI
 MmReleasePageMemoryConsumer(ULONG Consumer, PFN_NUMBER Page)
 {
-    PMM_ALLOCATION_REQUEST Request;
-    PLIST_ENTRY Entry;
-
     if (Page == 0)
     {
         DPRINT1("Tried to release page zero.\n");
     if (Page == 0)
     {
         DPRINT1("Tried to release page zero.\n");
@@ -109,23 +106,10 @@ MmReleasePageMemoryConsumer(ULONG Consumer, PFN_NUMBER Page)
     {
         if(Consumer == MC_USER) MmRemoveLRUUserPage(Page);
         (void)InterlockedDecrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
     {
         if(Consumer == MC_USER) MmRemoveLRUUserPage(Page);
         (void)InterlockedDecrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
-        if ((Entry = ExInterlockedRemoveHeadList(&AllocationListHead, &AllocationListLock)) == NULL)
-        {
-            MmDereferencePage(Page);
-        }
-        else
-        {
-            Request = CONTAINING_RECORD(Entry, MM_ALLOCATION_REQUEST, ListEntry);
-            MiZeroPhysicalPage(Page);
-            Request->Page = Page;
-            KeSetEvent(&Request->Event, IO_NO_INCREMENT, FALSE);
-        }
-    }
-    else
-    {
-        MmDereferencePage(Page);
     }
 
     }
 
+    MmDereferencePage(Page);
+
     return(STATUS_SUCCESS);
 }
 
     return(STATUS_SUCCESS);
 }
 
@@ -261,7 +245,7 @@ MmRequestPageMemoryConsumer(ULONG Consumer, BOOLEAN CanWait,
     /*
      * Allocate always memory for the non paged pool and for the pager thread.
      */
     /*
      * Allocate always memory for the non paged pool and for the pager thread.
      */
-    if ((Consumer == MC_SYSTEM) || MiIsBalancerThread())
+    if ((Consumer == MC_SYSTEM) /* || MiIsBalancerThread() */)
     {
         Page = MmAllocPage(Consumer);
         if (Page == 0)
     {
         Page = MmAllocPage(Consumer);
         if (Page == 0)
@@ -278,7 +262,8 @@ MmRequestPageMemoryConsumer(ULONG Consumer, BOOLEAN CanWait,
     /*
      * Make sure we don't exceed global targets.
      */
     /*
      * Make sure we don't exceed global targets.
      */
-    if (MmAvailablePages < MiMinimumAvailablePages)
+    if (((MmAvailablePages < MiMinimumAvailablePages) && !MiIsBalancerThread())
+            || (MmAvailablePages < (MiMinimumAvailablePages / 2)))
     {
         MM_ALLOCATION_REQUEST Request;
 
     {
         MM_ALLOCATION_REQUEST Request;
 
@@ -418,6 +403,46 @@ MiBalancerThread(PVOID Unused)
     }
 }
 
     }
 }
 
+BOOLEAN MmRosNotifyAvailablePage(PFN_NUMBER Page)
+{
+    PLIST_ENTRY Entry;
+    PMM_ALLOCATION_REQUEST Request;
+    PMMPFN Pfn1;
+
+    /* Make sure the PFN lock is held */
+    ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
+
+    if (!MiMinimumAvailablePages)
+    {
+        /* Dirty way to know if we were initialized. */
+        return FALSE;
+    }
+
+    Entry = ExInterlockedRemoveHeadList(&AllocationListHead, &AllocationListLock);
+    if (!Entry)
+        return FALSE;
+
+    Request = CONTAINING_RECORD(Entry, MM_ALLOCATION_REQUEST, ListEntry);
+    MiZeroPhysicalPage(Page);
+    Request->Page = Page;
+
+    Pfn1 = MiGetPfnEntry(Page);
+    ASSERT(Pfn1->u3.e2.ReferenceCount == 0);
+    Pfn1->u3.e2.ReferenceCount = 1;
+    Pfn1->u3.e1.PageLocation = ActiveAndValid;
+
+    /* This marks the PFN as a ReactOS PFN */
+    Pfn1->u4.AweAllocation = TRUE;
+
+    /* Allocate the extra ReactOS Data and zero it out */
+    Pfn1->u1.SwapEntry = 0;
+    Pfn1->RmapListHead = NULL;
+
+    KeSetEvent(&Request->Event, IO_NO_INCREMENT, FALSE);
+
+    return TRUE;
+}
+
 VOID
 INIT_FUNCTION
 NTAPI
 VOID
 INIT_FUNCTION
 NTAPI