sync with trunk (r49238)
[reactos.git] / ntoskrnl / mm / ARM3 / vadnode.c
index 46222b9..d62a02c 100644 (file)
@@ -100,35 +100,16 @@ MiInsertNode(IN PMM_AVL_TABLE Table,
 {
     /* Insert it into the tree */
     RtlpInsertAvlTreeNode(Table, NewNode, Parent, Result);
-}
 
-VOID
-NTAPI
-MiInsertVad(IN PMMVAD Vad,
-            IN PEPROCESS Process)
-{
-    TABLE_SEARCH_RESULT Result;
-    PMMADDRESS_NODE Parent = NULL;
-    
-    /* Validate the VAD and set it as the current hint */
-    ASSERT(Vad->EndingVpn >= Vad->StartingVpn);
-    Process->VadRoot.NodeHint = Vad;
-    
-    /* Find the parent VAD and where this child should be inserted */
-    Result = RtlpFindAvlTableNodeOrParent(&Process->VadRoot, (PVOID)Vad->StartingVpn, &Parent);
-    ASSERT(Result != TableFoundNode);
-    ASSERT((Parent != NULL) || (Result == TableEmptyTree));
-    
-    /* Do the actual insert operation */
-    MiInsertNode(&Process->VadRoot, (PVOID)Vad, Parent, Result);
-    
     /* Now insert an ARM3 MEMORY_AREA for this node, unless the insert was already from the MEMORY_AREA code */
+    PMMVAD Vad = (PMMVAD)NewNode;
     if (Vad->u.VadFlags.Spare == 0)
     {
         NTSTATUS Status;
         PMEMORY_AREA MemoryArea;
         PHYSICAL_ADDRESS BoundaryAddressMultiple;
         SIZE_T Size;
+        PEPROCESS Process = CONTAINING_RECORD(Table, EPROCESS, VadRoot);
         PVOID AllocatedBase = (PVOID)(Vad->StartingVpn << PAGE_SHIFT);
         BoundaryAddressMultiple.QuadPart = 0;
         Size = ((Vad->EndingVpn + 1) - Vad->StartingVpn) << PAGE_SHIFT;
@@ -142,9 +123,44 @@ MiInsertVad(IN PMMVAD Vad,
                                     0,
                                     BoundaryAddressMultiple);
         ASSERT(NT_SUCCESS(Status));
+
+        /* Check if this is VM VAD */
+        if (Vad->ControlArea == NULL)
+        {
+            /* We store the reactos MEMORY_AREA here */
+            DPRINT("Storing %p in %p\n", MemoryArea, Vad);
+            Vad->FirstPrototypePte = (PMMPTE)MemoryArea;
+        }
+        else
+        {
+            /* This is a section VAD. Store the MAREA here for now */
+            DPRINT("Storing %p in %p\n", MemoryArea, Vad);
+            Vad->ControlArea->WaitingForDeletion = (PVOID)MemoryArea;
+        }
     }
 }
 
+VOID
+NTAPI
+MiInsertVad(IN PMMVAD Vad,
+            IN PEPROCESS Process)
+{
+    TABLE_SEARCH_RESULT Result;
+    PMMADDRESS_NODE Parent = NULL;
+    
+    /* Validate the VAD and set it as the current hint */
+    ASSERT(Vad->EndingVpn >= Vad->StartingVpn);
+    Process->VadRoot.NodeHint = Vad;
+    
+    /* Find the parent VAD and where this child should be inserted */
+    Result = RtlpFindAvlTableNodeOrParent(&Process->VadRoot, (PVOID)Vad->StartingVpn, &Parent);
+    ASSERT(Result != TableFoundNode);
+    ASSERT((Parent != NULL) || (Result == TableEmptyTree));
+    
+    /* Do the actual insert operation */
+    MiInsertNode(&Process->VadRoot, (PVOID)Vad, Parent, Result);
+}
+
 VOID
 NTAPI
 MiRemoveNode(IN PMMADDRESS_NODE Node,
@@ -163,6 +179,40 @@ MiRemoveNode(IN PMMADDRESS_NODE Node,
         if (!Table->NumberGenericTableElements) Table->NodeHint = NULL;
         else Table->NodeHint = Table->BalancedRoot.RightChild;
     }
+
+    /* Free the node from ReactOS view as well */
+    PMMVAD Vad = (PMMVAD)Node;
+    if (Vad->u.VadFlags.Spare == 0)
+    {
+        PMEMORY_AREA MemoryArea;
+        PEPROCESS Process;
+            
+        /* Check if this is VM VAD */
+        if (Vad->ControlArea == NULL)
+        {
+            /* We store the ReactOS MEMORY_AREA here */
+            MemoryArea = (PMEMORY_AREA)Vad->FirstPrototypePte;
+        }
+        else
+        {
+            /* This is a section VAD. We store the ReactOS MEMORY_AREA here */
+            MemoryArea = (PMEMORY_AREA)Vad->ControlArea->WaitingForDeletion;
+        }
+        
+        /* Make sure one actually still exists */
+        if (MemoryArea)
+        {
+            /* Get the process */
+            Process = CONTAINING_RECORD(Table, EPROCESS, VadRoot);
+            
+            /* We only create fake memory-areas for ARM3 VADs */
+            ASSERT(MemoryArea->Type == MEMORY_AREA_OWNED_BY_ARM3);
+            ASSERT(MemoryArea->Vad == NULL);
+        
+            /* Free it */
+            MmFreeMemoryArea(&Process->Vm, MemoryArea, NULL, NULL);
+        }
+    }
 }
 
 PMMADDRESS_NODE