Merge 14981:15268 from trunk
[reactos.git] / reactos / ntoskrnl / mm / aspace.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/mm/aspace.c
6 * PURPOSE: Manages address spaces
7 *
8 * PROGRAMMERS: David Welch (welch@cwcom.net)
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <ntoskrnl.h>
14 #include <internal/debug.h>
15
16 /* GLOBALS ******************************************************************/
17
18 STATIC MADDRESS_SPACE KernelAddressSpace;
19
20 #define TAG_PTRC TAG('P', 'T', 'R', 'C')
21
22 /* FUNCTIONS *****************************************************************/
23
24 VOID
25 MmLockAddressSpace(PMADDRESS_SPACE AddressSpace)
26 {
27 /*
28 * Don't bother with locking if we are the first thread.
29 */
30 if (KeGetCurrentThread() == NULL)
31 {
32 return;
33 }
34 ExAcquireFastMutex(&AddressSpace->Lock);
35 }
36
37 VOID
38 MmUnlockAddressSpace(PMADDRESS_SPACE AddressSpace)
39 {
40 /*
41 * Don't bother locking if we are the first thread.
42 */
43 if (KeGetCurrentThread() == NULL)
44 {
45 return;
46 }
47 ExReleaseFastMutex(&AddressSpace->Lock);
48 }
49
50 VOID INIT_FUNCTION
51 MmInitializeKernelAddressSpace(VOID)
52 {
53 MmInitializeAddressSpace(NULL, &KernelAddressSpace);
54 }
55
56 PMADDRESS_SPACE MmGetCurrentAddressSpace(VOID)
57 {
58 return(&PsGetCurrentProcess()->AddressSpace);
59 }
60
61 PMADDRESS_SPACE MmGetKernelAddressSpace(VOID)
62 {
63 return(&KernelAddressSpace);
64 }
65
66 NTSTATUS
67 MmInitializeAddressSpace(PEPROCESS Process,
68 PMADDRESS_SPACE AddressSpace)
69 {
70 AddressSpace->MemoryAreaRoot = NULL;
71 ExInitializeFastMutex(&AddressSpace->Lock);
72 if (Process != NULL)
73 {
74 AddressSpace->LowestAddress = MM_LOWEST_USER_ADDRESS;
75 }
76 else
77 {
78 AddressSpace->LowestAddress = (PVOID)KERNEL_BASE;
79 }
80 AddressSpace->Process = Process;
81 if (Process != NULL)
82 {
83 ULONG Count;
84 Count = MiGetUserPageDirectoryCount();
85 AddressSpace->PageTableRefCountTable =
86 ExAllocatePoolWithTag(NonPagedPool, Count * sizeof(USHORT),
87 TAG_PTRC);
88 RtlZeroMemory(AddressSpace->PageTableRefCountTable, Count * sizeof(USHORT));
89 AddressSpace->PageTableRefCountTableSize = Count;
90 }
91 else
92 {
93 AddressSpace->PageTableRefCountTable = NULL;
94 AddressSpace->PageTableRefCountTableSize = 0;
95 }
96 return(STATUS_SUCCESS);
97 }
98
99 NTSTATUS
100 MmDestroyAddressSpace(PMADDRESS_SPACE AddressSpace)
101 {
102 if (AddressSpace->PageTableRefCountTable != NULL)
103 {
104 ExFreePool(AddressSpace->PageTableRefCountTable);
105 }
106 return(STATUS_SUCCESS);
107 }
108
109 /* EOF */