577df2beca9b394ef9d23100a0fafc6e8784e97b
[reactos.git] / reactos / ntoskrnl / mm / cont.c
1 /* $Id: cont.c,v 1.17 2002/01/01 00:21:55 dwelch Exp $
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, ULONG PhysAddr,
24 SWAPENTRY SwapEntry, BOOLEAN Dirty)
25 {
26 assert(SwapEntry == 0);
27 if (PhysAddr != 0)
28 {
29 MmDereferencePage((PVOID)PhysAddr);
30 }
31 }
32
33 PVOID STDCALL
34 MmAllocateContiguousAlignedMemory(IN ULONG NumberOfBytes,
35 IN PHYSICAL_ADDRESS HighestAcceptableAddress,
36 IN ULONG Alignment)
37 {
38 PMEMORY_AREA MArea;
39 NTSTATUS Status;
40 PVOID BaseAddress = 0;
41 PVOID PBase;
42 ULONG i;
43
44 MmLockAddressSpace(MmGetKernelAddressSpace());
45 Status = MmCreateMemoryArea(NULL,
46 MmGetKernelAddressSpace(),
47 MEMORY_AREA_CONTINUOUS_MEMORY,
48 &BaseAddress,
49 NumberOfBytes,
50 0,
51 &MArea,
52 FALSE);
53 MmUnlockAddressSpace(MmGetKernelAddressSpace());
54
55 if (!NT_SUCCESS(Status))
56 {
57 return(NULL);
58 }
59 DPRINT( "Base = %x\n", BaseAddress );
60 PBase = MmGetContinuousPages(NumberOfBytes,
61 HighestAcceptableAddress,
62 Alignment);
63 if (PBase == NULL)
64 {
65 MmFreeMemoryArea(MmGetKernelAddressSpace(),
66 BaseAddress,
67 0,
68 NULL,
69 NULL);
70 return(NULL);
71 }
72 for (i = 0; i < (PAGE_ROUND_UP(NumberOfBytes) / 4096); i++)
73 {
74 MmCreateVirtualMapping(NULL,
75 BaseAddress + (i * 4096),
76 PAGE_EXECUTE_READWRITE | PAGE_SYSTEM,
77 (ULONG)(PBase + (i * 4096)),
78 TRUE);
79 }
80 return(BaseAddress);
81 }
82
83 /**********************************************************************
84 * NAME EXPORTED
85 * MmAllocateContiguousMemory@12
86 *
87 * DESCRIPTION
88 * Allocates a range of physically contiguous cache aligned
89 * memory from the non-paged pool.
90 *
91 * ARGUMENTS
92 * NumberOfBytes
93 * Size of the memory block to allocate;
94 *
95 * HighestAcceptableAddress
96 * Highest address valid for the caller.
97 *
98 * RETURN VALUE
99 * The virtual address of the memory block on success;
100 * NULL on error.
101 *
102 * NOTE
103 * Description taken from include/ddk/mmfuncs.h.
104 * Code taken from ntoskrnl/mm/special.c.
105 *
106 * REVISIONS
107 *
108 */
109 PVOID STDCALL
110 MmAllocateContiguousMemory (IN ULONG NumberOfBytes,
111 IN PHYSICAL_ADDRESS HighestAcceptableAddress)
112 {
113 return(MmAllocateContiguousAlignedMemory(NumberOfBytes,
114 HighestAcceptableAddress,
115 PAGESIZE));
116 }
117
118
119 /**********************************************************************
120 * NAME EXPORTED
121 * MmFreeContiguousMemory@4
122 *
123 * DESCRIPTION
124 * Releases a range of physically contiguous memory allocated
125 * with MmAllocateContiguousMemory.
126 *
127 * ARGUMENTS
128 * BaseAddress
129 * Virtual address of the memory to be freed.
130 *
131 * RETURN VALUE
132 * None.
133 *
134 * NOTE
135 * Description taken from include/ddk/mmfuncs.h.
136 * Code taken from ntoskrnl/mm/special.c.
137 *
138 * REVISIONS
139 *
140 */
141 VOID STDCALL
142 MmFreeContiguousMemory(IN PVOID BaseAddress)
143 {
144 MmFreeMemoryArea(MmGetKernelAddressSpace(),
145 BaseAddress,
146 0,
147 MmFreeContinuousPage,
148 NULL);
149 }
150
151
152 /* EOF */