- NDK 0.98, now with versionned headers. Too many changes to list, see the TinyKRNL...
[reactos.git] / reactos / ntoskrnl / mm / marea.c
index c56f487..a031f64 100644 (file)
@@ -1,11 +1,43 @@
 /* $Id$
- * 
- * COPYRIGHT:       See COPYING in the top level directory
+ *
+ * Copyright (C) 1998-2005 ReactOS Team (and the authors from the programmers section)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *
  * PROJECT:         ReactOS kernel
  * FILE:            ntoskrnl/mm/marea.c
  * PURPOSE:         Implements memory areas
- * 
- * PROGRAMMERS:     David Welch (welch@mcmail.com)
+ *
+ * PROGRAMMERS:     Rex Jolliff
+ *                  David Welch
+ *                  Eric Kohl
+ *                  Philip Susi
+ *                  Casper Hornstrup
+ *                  Eric Kohl
+ *                  Ge van Geldorp
+ *                  Royce Mitchell III
+ *                  Aleksey Bragin 
+ *                  Jason Filby
+ *                  Thomas Weidenmueller
+ *                  Gunnar Andre' Dalsnes
+ *                  Mike Nordell
+ *                  Alex Ionescu
+ *                  Filip Navara
+ *                  Herve Poussineau
+ *                  Steven Edwards
  */
 
 /* INCLUDES *****************************************************************/
@@ -14,9 +46,9 @@
 #define NDEBUG
 #include <internal/debug.h>
 
-/* GLOBALS *******************************************************************/
-
-#define TAG_MAREA   TAG('M', 'A', 'R', 'E')
+#if defined (ALLOC_PRAGMA)
+#pragma alloc_text(INIT, MmInitMemoryAreas)
+#endif
 
 /* #define VALIDATE_MEMORY_AREAS */
 
@@ -76,7 +108,7 @@ static PMEMORY_AREA MmIterateNextNode(PMEMORY_AREA Node)
 }
 
 /**
- * @name MmIterateFirstNode
+ * @name MmIterateLastNode
  *
  * @param Node
  *        Head node of the MEMORY_AREA tree.
@@ -94,7 +126,7 @@ static PMEMORY_AREA MmIterateLastNode(PMEMORY_AREA Node)
 }
 
 /**
- * @name MmIterateNextNode
+ * @name MmIteratePreviousNode
  *
  * @param Node
  *        Current node in the tree.
@@ -171,9 +203,9 @@ MmDumpMemoryAreas(PMADDRESS_SPACE AddressSpace)
         Node != NULL;
         Node = MmIterateNextNode(Node))
    {
-      DbgPrint("Start %p End %p Attributes %x\n",
+      DbgPrint("Start %p End %p Protect %x Flags %x\n",
                Node->StartingAddress, Node->EndingAddress,
-               Node->Attributes);
+               Node->Protect, Node->Flags);
    }
 
    DbgPrint("Finished MmDumpMemoryAreas()\n");
@@ -440,8 +472,8 @@ MmFindGapBottomUp(
    ULONG_PTR Length,
    ULONG_PTR Granularity)
 {
-   PVOID HighestAddress = AddressSpace->LowestAddress < (PVOID)KERNEL_BASE ?
-                          (PVOID)(KERNEL_BASE - 1) : (PVOID)MAXULONG_PTR;
+   PVOID HighestAddress = AddressSpace->LowestAddress < MmSystemRangeStart ?
+                          (PVOID)((ULONG_PTR)MmSystemRangeStart - 1) : (PVOID)MAXULONG_PTR;
    PVOID AlignedAddress;
    PMEMORY_AREA Node;
    PMEMORY_AREA FirstNode;
@@ -517,8 +549,8 @@ MmFindGapTopDown(
    ULONG_PTR Length,
    ULONG_PTR Granularity)
 {
-   PVOID HighestAddress = AddressSpace->LowestAddress < (PVOID)KERNEL_BASE ?
-                          (PVOID)(KERNEL_BASE - 1) : (PVOID)MAXULONG_PTR;
+   PVOID HighestAddress = AddressSpace->LowestAddress < MmSystemRangeStart ?
+                          (PVOID)((ULONG_PTR)MmSystemRangeStart - 1) : (PVOID)MAXULONG_PTR;
    PVOID AlignedAddress;
    PMEMORY_AREA Node;
    PMEMORY_AREA PreviousNode;
@@ -616,16 +648,16 @@ MmFindGapAtAddress(
 {
    PMEMORY_AREA Node = AddressSpace->MemoryAreaRoot;
    PMEMORY_AREA RightNeighbour = NULL;
-   PVOID HighestAddress = AddressSpace->LowestAddress < (PVOID)KERNEL_BASE ?
-                          (PVOID)(KERNEL_BASE - 1) : (PVOID)MAXULONG_PTR;
+   PVOID HighestAddress = AddressSpace->LowestAddress < MmSystemRangeStart ?
+                          (PVOID)((ULONG_PTR)MmSystemRangeStart - 1) : (PVOID)MAXULONG_PTR;
 
    MmVerifyMemoryAreas(AddressSpace);
 
    Address = MM_ROUND_DOWN(Address, PAGE_SIZE);
 
-   if (AddressSpace->LowestAddress < (PVOID)KERNEL_BASE)
+   if (AddressSpace->LowestAddress < MmSystemRangeStart)
    {
-      if (Address >= (PVOID)KERNEL_BASE)
+      if (Address >= MmSystemRangeStart)
       {
          return 0;
       }
@@ -676,7 +708,9 @@ MmFindGapAtAddress(
  * Initialize the memory area list implementation.
  */
 
-NTSTATUS INIT_FUNCTION
+NTSTATUS
+INIT_FUNCTION
+NTAPI
 MmInitMemoryAreas(VOID)
 {
    DPRINT("MmInitMemoryAreas()\n",0);
@@ -713,7 +747,7 @@ MmFreeMemoryArea(
    PMEMORY_AREA *ParentReplace;
    ULONG_PTR Address;
    PVOID EndAddress;
-   PEPROCESS CurrentProcess = PsGetCurrentProcess();
+   PROS_EPROCESS CurrentProcess = (PROS_EPROCESS)PsGetCurrentProcess();
 
    if (AddressSpace->Process != NULL &&
        AddressSpace->Process != CurrentProcess)
@@ -732,7 +766,7 @@ MmFreeMemoryArea(
       }
       else
       {
-         BOOL Dirty = FALSE;
+         BOOLEAN Dirty = FALSE;
          SWAPENTRY SwapEntry = 0;
          PFN_TYPE Page = 0;
 
@@ -812,7 +846,7 @@ MmFreeMemoryArea(
       }
    }
 
-   ExFreePool(MemoryArea);
+   ExFreePoolWithTag(MemoryArea, TAG_MAREA);
 
    DPRINT("MmFreeMemoryAreaByNode() succeeded\n");
 
@@ -897,15 +931,14 @@ MmFreeMemoryAreaByPtr(
  */
 
 NTSTATUS STDCALL
-MmCreateMemoryArea(PEPROCESS Process,
-                   PMADDRESS_SPACE AddressSpace,
+MmCreateMemoryArea(PMADDRESS_SPACE AddressSpace,
                    ULONG Type,
                    PVOID *BaseAddress,
                    ULONG_PTR Length,
-                   ULONG Attributes,
+                   ULONG Protect,
                    PMEMORY_AREA *Result,
                    BOOLEAN FixedAddress,
-                   BOOLEAN TopDown,
+                   ULONG AllocationFlags,
                    PHYSICAL_ADDRESS BoundaryAddressMultiple)
 {
    PVOID EndAddress;
@@ -928,7 +961,7 @@ MmCreateMemoryArea(PEPROCESS Process,
       *BaseAddress = MmFindGap(AddressSpace,
                                tmpLength,
                                Granularity,
-                               TopDown != 0);
+                               (AllocationFlags & MEM_TOP_DOWN) == MEM_TOP_DOWN);
       if ((*BaseAddress) == 0)
       {
          DPRINT("No suitable gap\n");
@@ -941,15 +974,15 @@ MmCreateMemoryArea(PEPROCESS Process,
                          - (ULONG_PTR) MM_ROUND_DOWN(*BaseAddress, Granularity));
       *BaseAddress = MM_ROUND_DOWN(*BaseAddress, Granularity);
 
-      if (AddressSpace->LowestAddress == (PVOID)KERNEL_BASE &&
-          *BaseAddress < (PVOID)KERNEL_BASE)
+      if (AddressSpace->LowestAddress == MmSystemRangeStart &&
+          *BaseAddress < MmSystemRangeStart)
       {
          CHECKPOINT;
          return STATUS_ACCESS_VIOLATION;
       }
 
-      if (AddressSpace->LowestAddress < (PVOID)KERNEL_BASE &&
-          (ULONG_PTR)(*BaseAddress) + tmpLength > KERNEL_BASE)
+      if (AddressSpace->LowestAddress < MmSystemRangeStart &&
+          (ULONG_PTR)(*BaseAddress) + tmpLength > (ULONG_PTR)MmSystemRangeStart)
       {
          CHECKPOINT;
          return STATUS_ACCESS_VIOLATION;
@@ -976,9 +1009,9 @@ MmCreateMemoryArea(PEPROCESS Process,
    MemoryArea->Type = Type;
    MemoryArea->StartingAddress = *BaseAddress;
    MemoryArea->EndingAddress = (PVOID)((ULONG_PTR)*BaseAddress + tmpLength);
-   MemoryArea->Attributes = Attributes;
+   MemoryArea->Protect = Protect;
+   MemoryArea->Flags = AllocationFlags;
    MemoryArea->LockCount = 0;
-   MemoryArea->Process = Process;
    MemoryArea->PageOpCount = 0;
    MemoryArea->DeleteInProgress = FALSE;
 
@@ -992,7 +1025,7 @@ MmCreateMemoryArea(PEPROCESS Process,
 
 
 VOID STDCALL
-MmReleaseMemoryAreaIfDecommitted(PEPROCESS Process,
+MmReleaseMemoryAreaIfDecommitted(PROS_EPROCESS Process,
                                  PMADDRESS_SPACE AddressSpace,
                                  PVOID BaseAddress)
 {