:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / ntoskrnl / mm / cont.c
1 /* $Id$
2  * 
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS kernel
5  * FILE:            ntoskrnl/mm/cont.c
6  * PURPOSE:         Manages continuous memory
7  * PROGRAMMER:      David Welch (welch@cwcom.net)
8  * UPDATE HISTORY:
9  *                  Created 22/05/98
10  */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <internal/mm.h>
16
17 #define NDEBUG
18 #include <internal/debug.h>
19
20 /* FUNCTIONS *****************************************************************/
21
22 VOID STATIC
23 MmFreeContinuousPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address, 
24                      PHYSICAL_ADDRESS PhysAddr, SWAPENTRY SwapEntry, 
25                      BOOLEAN Dirty)
26 {
27   assert(SwapEntry == 0);
28   if (PhysAddr.QuadPart != 0)
29     {
30       MmReleasePageMemoryConsumer(MC_NPPOOL, PhysAddr);
31     }
32 }
33
34 PVOID STDCALL
35 MmAllocateContiguousAlignedMemory(IN ULONG NumberOfBytes,
36                                   IN PHYSICAL_ADDRESS HighestAcceptableAddress,
37                                   IN ULONG Alignment)
38 {
39    PMEMORY_AREA MArea;
40    NTSTATUS Status;
41    PVOID BaseAddress = 0;
42    PHYSICAL_ADDRESS PBase;
43    ULONG i;
44    
45    MmLockAddressSpace(MmGetKernelAddressSpace());
46    Status = MmCreateMemoryArea(NULL,
47                                MmGetKernelAddressSpace(),
48                                MEMORY_AREA_CONTINUOUS_MEMORY,
49                                &BaseAddress,
50                                NumberOfBytes,
51                                0,
52                                &MArea,
53                                FALSE);
54    MmUnlockAddressSpace(MmGetKernelAddressSpace());
55
56    if (!NT_SUCCESS(Status))
57      {
58         return(NULL);
59      }
60    DPRINT( "Base = %x\n", BaseAddress );
61    PBase = MmGetContinuousPages(NumberOfBytes,
62                                 HighestAcceptableAddress,
63                                 Alignment);
64    if (PBase.QuadPart == 0LL)
65      {
66        MmFreeMemoryArea(MmGetKernelAddressSpace(),
67                         BaseAddress,
68                         0,
69                         NULL,
70                         NULL);
71        return(NULL);
72      }
73    for (i = 0; i < (PAGE_ROUND_UP(NumberOfBytes) / 4096); i++)
74      {
75         MmCreateVirtualMapping(NULL,
76                                BaseAddress + (i * 4096),
77                                PAGE_EXECUTE_READWRITE | PAGE_SYSTEM,
78                                (LARGE_INTEGER)(PBase.QuadPart + (i * 4096)),
79                                TRUE);
80      }
81    return(BaseAddress);
82 }
83
84 /**********************************************************************
85  * NAME                                                 EXPORTED
86  *      MmAllocateContiguousMemory@12
87  *
88  * DESCRIPTION
89  *      Allocates a range of physically contiguous cache aligned
90  *      memory from the non-paged pool.
91  *      
92  * ARGUMENTS
93  *      NumberOfBytes
94  *              Size of the memory block to allocate;
95  *              
96  *      HighestAcceptableAddress
97  *              Highest address valid for the caller.
98  *              
99  * RETURN VALUE
100  *      The virtual address of the memory block on success;
101  *      NULL on error.
102  *
103  * NOTE
104  *      Description taken from include/ddk/mmfuncs.h.
105  *      Code taken from ntoskrnl/mm/special.c.
106  *
107  * REVISIONS
108  *
109  */
110 PVOID STDCALL 
111 MmAllocateContiguousMemory (IN ULONG NumberOfBytes,
112                             IN PHYSICAL_ADDRESS HighestAcceptableAddress)
113 {
114   return(MmAllocateContiguousAlignedMemory(NumberOfBytes,
115                                            HighestAcceptableAddress,
116                                            PAGE_SIZE));
117 }
118
119
120 /**********************************************************************
121  * NAME                                                 EXPORTED
122  *      MmFreeContiguousMemory@4
123  *
124  * DESCRIPTION
125  *      Releases a range of physically contiguous memory allocated
126  *      with MmAllocateContiguousMemory.
127  *      
128  * ARGUMENTS
129  *      BaseAddress
130  *              Virtual address of the memory to be freed.
131  *
132  * RETURN VALUE
133  *      None.
134  *
135  * NOTE
136  *      Description taken from include/ddk/mmfuncs.h.
137  *      Code taken from ntoskrnl/mm/special.c.
138  *
139  * REVISIONS
140  *
141  */
142 VOID STDCALL 
143 MmFreeContiguousMemory(IN PVOID BaseAddress)
144 {
145    MmFreeMemoryArea(MmGetKernelAddressSpace(),
146                     BaseAddress,
147                     0,
148                     MmFreeContinuousPage,
149                     NULL);
150 }
151
152
153 /* EOF */