- Support INIT section pragmas for msvc. Patch by Brezenbak.
[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 #if defined (ALLOC_PRAGMA)
17 #pragma alloc_text(INIT, MmInitializeKernelAddressSpace)
18 #endif
19
20
21 /* GLOBALS ******************************************************************/
22
23 STATIC MADDRESS_SPACE KernelAddressSpace;
24
25 /* FUNCTIONS *****************************************************************/
26
27 VOID
28 NTAPI
29 MmLockAddressSpace(PMADDRESS_SPACE AddressSpace)
30 {
31 /*
32 * Don't bother with locking if we are the first thread.
33 */
34 if (KeGetCurrentThread() == NULL)
35 {
36 return;
37 }
38 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(&AddressSpace->Lock);
39 }
40
41 VOID
42 NTAPI
43 MmUnlockAddressSpace(PMADDRESS_SPACE AddressSpace)
44 {
45 /*
46 * Don't bother locking if we are the first thread.
47 */
48 if (KeGetCurrentThread() == NULL)
49 {
50 return;
51 }
52 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(&AddressSpace->Lock);
53 }
54
55 VOID
56 INIT_FUNCTION
57 NTAPI
58 MmInitializeKernelAddressSpace(VOID)
59 {
60 MmInitializeAddressSpace(NULL, &KernelAddressSpace);
61 }
62
63 PMADDRESS_SPACE
64 NTAPI
65 MmGetCurrentAddressSpace(VOID)
66 {
67 return(&PsGetCurrentProcess()->AddressSpace);
68 }
69
70 PMADDRESS_SPACE
71 NTAPI
72 MmGetKernelAddressSpace(VOID)
73 {
74 return(&KernelAddressSpace);
75 }
76
77 NTSTATUS
78 NTAPI
79 MmInitializeAddressSpace(PEPROCESS Process,
80 PMADDRESS_SPACE AddressSpace)
81 {
82 AddressSpace->MemoryAreaRoot = NULL;
83 ExInitializeFastMutex(&AddressSpace->Lock);
84 if (Process != NULL)
85 {
86 AddressSpace->LowestAddress = MM_LOWEST_USER_ADDRESS;
87 }
88 else
89 {
90 AddressSpace->LowestAddress = MmSystemRangeStart;
91 }
92 AddressSpace->Process = Process;
93 if (Process != NULL)
94 {
95 ULONG Count;
96 Count = MiGetUserPageDirectoryCount();
97 AddressSpace->PageTableRefCountTable =
98 ExAllocatePoolWithTag(NonPagedPool, Count * sizeof(USHORT),
99 TAG_PTRC);
100 RtlZeroMemory(AddressSpace->PageTableRefCountTable, Count * sizeof(USHORT));
101 AddressSpace->PageTableRefCountTableSize = Count;
102 }
103 else
104 {
105 AddressSpace->PageTableRefCountTable = NULL;
106 AddressSpace->PageTableRefCountTableSize = 0;
107 }
108 return(STATUS_SUCCESS);
109 }
110
111 NTSTATUS
112 NTAPI
113 MmDestroyAddressSpace(PMADDRESS_SPACE AddressSpace)
114 {
115 if (AddressSpace->PageTableRefCountTable != NULL)
116 {
117 ExFreePool(AddressSpace->PageTableRefCountTable);
118 }
119 return(STATUS_SUCCESS);
120 }
121
122 /* EOF */