Author: tkreuzer
Date: Sat Oct  4 21:17:27 2014
New Revision: 64528

URL: http://svn.reactos.org/svn/reactos?rev=64528&view=rev
Log:
[NTOSKRNL]
- Implement MiIncrementAvailablePages and MiDecrementAvailablePages factoring 
out duplicated code from a number of locations
- Get rid of MiAllocatePfn. Any further comment would result in excessive use 
of curse words.

Modified:
    trunk/reactos/ntoskrnl/mm/ARM3/miarm.h
    trunk/reactos/ntoskrnl/mm/ARM3/pfnlist.c
    trunk/reactos/ntoskrnl/mm/ARM3/sysldr.c

Modified: trunk/reactos/ntoskrnl/mm/ARM3/miarm.h
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/mm/ARM3/miarm.h?rev=64528&r1=64527&r2=64528&view=diff
==============================================================================
--- trunk/reactos/ntoskrnl/mm/ARM3/miarm.h      [iso-8859-1] (original)
+++ trunk/reactos/ntoskrnl/mm/ARM3/miarm.h      [iso-8859-1] Sat Oct  4 
21:17:27 2014
@@ -1973,13 +1973,6 @@
     IN PMMPFN Pfn
 );
 
-PFN_NUMBER
-NTAPI
-MiAllocatePfn(
-    IN PMMPTE PointerPte,
-    IN ULONG Protection
-);
-
 VOID
 NTAPI
 MiInitializePfn(

Modified: trunk/reactos/ntoskrnl/mm/ARM3/pfnlist.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/mm/ARM3/pfnlist.c?rev=64528&r1=64527&r2=64528&view=diff
==============================================================================
--- trunk/reactos/ntoskrnl/mm/ARM3/pfnlist.c    [iso-8859-1] (original)
+++ trunk/reactos/ntoskrnl/mm/ARM3/pfnlist.c    [iso-8859-1] Sat Oct  4 
21:17:27 2014
@@ -65,6 +65,59 @@
 
 /* FUNCTIONS 
******************************************************************/
 
+static
+VOID
+MiIncrementAvailablePages(
+    VOID)
+{
+    /* Increment available pages */
+    MmAvailablePages++;
+
+    /* Check if we've reached the configured low memory threshold */
+    if (MmAvailablePages == MmLowMemoryThreshold)
+    {
+        /* Clear the event, because now we're ABOVE the threshold */
+        KeClearEvent(MiLowMemoryEvent);
+    }
+    else if (MmAvailablePages == MmHighMemoryThreshold)
+    {
+        /* Otherwise check if we reached the high threshold and signal the 
event */
+        KeSetEvent(MiHighMemoryEvent, 0, FALSE);
+    }
+}
+
+static
+VOID
+MiDecrementAvailablePages(
+    VOID)
+{
+    ASSERT(MmAvailablePages > 0);
+
+    /* See if we hit any thresholds */
+    if (MmAvailablePages == MmHighMemoryThreshold)
+    {
+        /* Clear the high memory event */
+        KeClearEvent(MiHighMemoryEvent);
+    }
+    else if (MmAvailablePages == MmLowMemoryThreshold)
+    {
+        /* Signal the low memory event */
+        KeSetEvent(MiLowMemoryEvent, 0, FALSE);
+    }
+
+    /* One less page */
+    MmAvailablePages--;
+    if (MmAvailablePages < MmMinimumFreePages)
+    {
+        /* FIXME: Should wake up the MPW and working set manager, if we had 
one */
+
+        DPRINT1("Running low on pages: %lu remaining\n", MmAvailablePages);
+
+        /* Call RosMm and see if it can release any pages for us */
+        MmRebalanceMemoryConsumers();
+    }
+}
+
 VOID
 NTAPI
 MiZeroPhysicalPage(IN PFN_NUMBER PageFrameIndex)
@@ -195,28 +248,8 @@
     Entry->u1.Flink = Entry->u2.Blink = 0;
     ASSERT_LIST_INVARIANT(ListHead);
 
-    /* See if we hit any thresholds */
-    if (MmAvailablePages == MmHighMemoryThreshold)
-    {
-        /* Clear the high memory event */
-        KeClearEvent(MiHighMemoryEvent);
-    }
-    else if (MmAvailablePages == MmLowMemoryThreshold)
-    {
-        /* Signal the low memory event */
-        KeSetEvent(MiLowMemoryEvent, 0, FALSE);
-    }
-
-    /* One less page */
-    if (--MmAvailablePages < MmMinimumFreePages)
-    {
-        /* FIXME: Should wake up the MPW and working set manager, if we had 
one */
-
-        DPRINT1("Running low on pages: %lu remaining\n", MmAvailablePages);
-
-        /* Call RosMm and see if it can release any pages for us */
-        MmRebalanceMemoryConsumers();
-    }
+    /* Decrement number of available pages */
+    MiDecrementAvailablePages();
 
 #if MI_TRACE_PFNS
     ASSERT(MI_PFN_CURRENT_USAGE != MI_USAGE_NOT_SET);
@@ -253,31 +286,12 @@
         /* Get the exact list */
         ListHead = &MmStandbyPageListByPriority[Pfn->u4.Priority];
 
-        /* See if we hit any thresholds */
-        if (MmAvailablePages == MmHighMemoryThreshold)
-        {
-            /* Clear the high memory event */
-            KeClearEvent(MiHighMemoryEvent);
-        }
-        else if (MmAvailablePages == MmLowMemoryThreshold)
-        {
-            /* Signal the low memory event */
-            KeSetEvent(MiLowMemoryEvent, 0, FALSE);
-        }
+        /* Decrement number of available pages */
+        MiDecrementAvailablePages();
 
         /* Decrease transition page counter */
         ASSERT(Pfn->u3.e1.PrototypePte == 1); /* Only supported ARM3 case */
         MmTransitionSharedPages--;
-
-        /* One less page */
-        if (--MmAvailablePages < MmMinimumFreePages)
-        {
-            /* FIXME: Should wake up the MPW and working set manager, if we 
had one */
-            DPRINT1("Running low on pages: %lu remaining\n", MmAvailablePages);
-
-            /* Call RosMm and see if it can release any pages for us */
-            MmRebalanceMemoryConsumers();
-        }
     }
     else if (ListHead == &MmModifiedPageListHead)
     {
@@ -442,28 +456,8 @@
     /* ReactOS Hack */
     Pfn1->OriginalPte.u.Long = 0;
 
-    /* See if we hit any thresholds */
-    if (MmAvailablePages == MmHighMemoryThreshold)
-    {
-        /* Clear the high memory event */
-        KeClearEvent(MiHighMemoryEvent);
-    }
-    else if (MmAvailablePages == MmLowMemoryThreshold)
-    {
-        /* Signal the low memory event */
-        KeSetEvent(MiLowMemoryEvent, 0, FALSE);
-    }
-
-    /* One less page */
-    if (--MmAvailablePages < MmMinimumFreePages)
-    {
-        /* FIXME: Should wake up the MPW and working set manager, if we had 
one */
-
-        DPRINT1("Running low on pages: %lu remaining\n", MmAvailablePages);
-
-        /* Call RosMm and see if it can release any pages for us */
-        MmRebalanceMemoryConsumers();
-    }
+    /* Decrement number of available pages */
+    MiDecrementAvailablePages();
 
 #if MI_TRACE_PFNS
     //ASSERT(MI_PFN_CURRENT_USAGE != MI_USAGE_NOT_SET);
@@ -664,20 +658,8 @@
     Pfn1->u4.InPageError = 0;
     Pfn1->u4.AweAllocation = 0;
 
-    /* Increase available pages */
-    MmAvailablePages++;
-
-    /* Check if we've reached the configured low memory threshold */
-    if (MmAvailablePages == MmLowMemoryThreshold)
-    {
-        /* Clear the event, because now we're ABOVE the threshold */
-        KeClearEvent(MiLowMemoryEvent);
-    }
-    else if (MmAvailablePages == MmHighMemoryThreshold)
-    {
-        /* Otherwise check if we reached the high threshold and signal the 
event */
-        KeSetEvent(MiHighMemoryEvent, 0, FALSE);
-    }
+    /* Increment number of available pages */
+    MiIncrementAvailablePages();
 
     /* Get the page color */
     Color = PageFrameIndex & MmSecondaryColorMask;
@@ -779,20 +761,8 @@
     /* Move the page onto its new location */
     Pfn1->u3.e1.PageLocation = StandbyPageList;
 
-    /* One more page on the system */
-    MmAvailablePages++;
-
-    /* Check if we've reached the configured low memory threshold */
-    if (MmAvailablePages == MmLowMemoryThreshold)
-    {
-        /* Clear the event, because now we're ABOVE the threshold */
-        KeClearEvent(MiLowMemoryEvent);
-    }
-    else if (MmAvailablePages == MmHighMemoryThreshold)
-    {
-        /* Otherwise check if we reached the high threshold and signal the 
event */
-        KeSetEvent(MiHighMemoryEvent, 0, FALSE);
-    }
+    /* Increment number of available pages */
+    MiIncrementAvailablePages();
 }
 
 VOID
@@ -919,20 +889,8 @@
     /* For zero/free pages, we also have to handle the colored lists */
     if (ListName <= StandbyPageList)
     {
-        /* One more page on the system */
-        MmAvailablePages++;
-
-        /* Check if we've reached the configured low memory threshold */
-        if (MmAvailablePages == MmLowMemoryThreshold)
-        {
-            /* Clear the event, because now we're ABOVE the threshold */
-            KeClearEvent(MiLowMemoryEvent);
-        }
-        else if (MmAvailablePages == MmHighMemoryThreshold)
-        {
-            /* Otherwise check if we reached the high threshold and signal the 
event */
-            KeSetEvent(MiHighMemoryEvent, 0, FALSE);
-        }
+        /* Increment number of available pages */
+        MiIncrementAvailablePages();
 
         /* Sanity checks */
         ASSERT(ListName == ZeroedPageList);
@@ -1160,54 +1118,6 @@
     /* Release the lock and return success */
     KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
     return STATUS_SUCCESS;
-}
-
-PFN_NUMBER
-NTAPI
-MiAllocatePfn(IN PMMPTE PointerPte,
-              IN ULONG Protection)
-{
-    KIRQL OldIrql;
-    PFN_NUMBER PageFrameIndex;
-    MMPTE TempPte;
-
-    /* Sanity check that we aren't passed a valid PTE */
-    ASSERT(PointerPte->u.Hard.Valid == 0);
-
-    /* Make an empty software PTE */
-    MI_MAKE_SOFTWARE_PTE(&TempPte, MM_READWRITE);
-
-    /* Lock the PFN database */
-    OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
-
-    /* Check if we're running low on pages */
-    if (MmAvailablePages < 128)
-    {
-        DPRINT1("Warning, running low on memory: %lu pages left\n", 
MmAvailablePages);
-
-        //MiEnsureAvailablePageOrWait(NULL, OldIrql);
-
-        /* Call RosMm and see if it can release any pages for us */
-        MmRebalanceMemoryConsumers();
-    }
-
-    /* Grab a page */
-    ASSERT_LIST_INVARIANT(&MmFreePageListHead);
-    ASSERT_LIST_INVARIANT(&MmZeroedPageListHead);
-    PageFrameIndex = MiRemoveAnyPage(MI_GET_NEXT_COLOR());
-
-    /* Write the software PTE */
-    MI_WRITE_INVALID_PTE(PointerPte, TempPte);
-    PointerPte->u.Soft.Protection |= Protection;
-
-    /* Initialize its PFN entry */
-    MiInitializePfn(PageFrameIndex, PointerPte, TRUE);
-
-    /* Release the PFN lock and return the page */
-    ASSERT_LIST_INVARIANT(&MmFreePageListHead);
-    ASSERT_LIST_INVARIANT(&MmZeroedPageListHead);
-    KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
-    return PageFrameIndex;
 }
 
 VOID

Modified: trunk/reactos/ntoskrnl/mm/ARM3/sysldr.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/mm/ARM3/sysldr.c?rev=64528&r1=64527&r2=64528&view=diff
==============================================================================
--- trunk/reactos/ntoskrnl/mm/ARM3/sysldr.c     [iso-8859-1] (original)
+++ trunk/reactos/ntoskrnl/mm/ARM3/sysldr.c     [iso-8859-1] Sat Oct  4 
21:17:27 2014
@@ -99,6 +99,8 @@
     PMMPTE PointerPte, LastPte;
     PVOID DriverBase;
     MMPTE TempPte;
+    KIRQL OldIrql;
+    PFN_NUMBER PageFrameIndex;
     PAGED_CODE();
 
     /* Detect session load */
@@ -173,30 +175,45 @@
     *ImageBase = DriverBase;
     DPRINT1("Loading: %wZ at %p with %lx pages\n", FileName, DriverBase, 
PteCount);
 
+    /* Lock the PFN database */
+    OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
+
+    /* Some debug stuff */
+    MI_SET_USAGE(MI_USAGE_DRIVER_PAGE);
+#if MI_TRACE_PFNS
+    PWCHAR pos = NULL;
+    ULONG len = 0;
+    if (FileName->Buffer)
+    {
+        pos = wcsrchr(FileName->Buffer, '\\');
+        len = wcslen(pos) * sizeof(WCHAR);
+        if (pos) snprintf(MI_PFN_CURRENT_PROCESS_NAME, min(16, len), "%S", 
pos);
+    }
+#endif
+
     /* Loop the new driver PTEs */
     TempPte = ValidKernelPte;
     while (PointerPte < LastPte)
     {
-        /* Allocate a page */
-        MI_SET_USAGE(MI_USAGE_DRIVER_PAGE);
-#if MI_TRACE_PFNS
-        PWCHAR pos = NULL;
-        ULONG len = 0;
-        if (FileName->Buffer)
-        {
-            pos = wcsrchr(FileName->Buffer, '\\');
-            len = wcslen(pos) * sizeof(WCHAR);
-            if (pos) snprintf(MI_PFN_CURRENT_PROCESS_NAME, min(16, len), "%S", 
pos);
-        }
-#endif
-        TempPte.u.Hard.PageFrameNumber = MiAllocatePfn(PointerPte, MM_EXECUTE);
-
-        /* Write it */
+        /* Make sure the PTE is not valid for whatever reason */
+        ASSERT(PointerPte->u.Hard.Valid == 0);
+
+        /* Grab a page */
+        PageFrameIndex = MiRemoveAnyPage(MI_GET_NEXT_COLOR());
+
+        /* Initialize its PFN entry */
+        MiInitializePfn(PageFrameIndex, PointerPte, TRUE);
+
+        /* Write the PTE */
+        TempPte.u.Hard.PageFrameNumber = PageFrameIndex;
         MI_WRITE_VALID_PTE(PointerPte, TempPte);
 
         /* Move on */
         PointerPte++;
     }
+
+    /* Release the PFN lock */
+    KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
 
     /* Copy the image */
     RtlCopyMemory(DriverBase, Base, PteCount << PAGE_SHIFT);


Reply via email to