a49e4fe5cab37360d805d2d5293652d618bc158c
[reactos.git] / reactos / subsystems / win32 / win32k / eng / mem.c
1 /*
2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /* $Id$
20 *
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * PURPOSE: GDI Driver Memory Management Functions
24 * FILE: subsys/win32k/eng/mem.c
25 * PROGRAMER: Jason Filby
26 * REVISION HISTORY:
27 * 3/7/1999: Created
28 */
29
30 #include <win32k.h>
31
32 #define NDEBUG
33 #include <debug.h>
34
35 /*
36 * @implemented
37 */
38 PVOID APIENTRY
39 EngAllocMem(ULONG Flags,
40 ULONG MemSize,
41 ULONG Tag)
42 {
43 PVOID newMem;
44
45 newMem = ExAllocatePoolWithTag(PagedPool, MemSize, Tag);
46
47 if (Flags == FL_ZERO_MEMORY && NULL != newMem)
48 {
49 RtlZeroMemory(newMem, MemSize);
50 }
51
52 return newMem;
53 }
54
55 /*
56 * @implemented
57 */
58 VOID APIENTRY
59 EngFreeMem(PVOID Mem)
60 {
61 ExFreePool(Mem);
62 }
63
64 /*
65 * @implemented
66 */
67 PVOID APIENTRY
68 EngAllocUserMem(SIZE_T cj, ULONG Tag)
69 {
70 PVOID NewMem = NULL;
71 NTSTATUS Status;
72 SIZE_T MemSize = cj;
73
74 Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &NewMem, 0, &MemSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
75
76 if (! NT_SUCCESS(Status))
77 {
78 return NULL;
79 }
80
81 /* TODO: Add allocation info to AVL tree (stored inside W32PROCESS structure) */
82
83 return NewMem;
84 }
85
86 /*
87 * @implemented
88 */
89 VOID APIENTRY
90 EngFreeUserMem(PVOID pv)
91 {
92 PVOID BaseAddress = pv;
93 SIZE_T MemSize = 0;
94
95 ZwFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &MemSize, MEM_RELEASE);
96
97 /* TODO: Remove allocation info from AVL tree */
98 }
99
100
101
102 PVOID
103 APIENTRY
104 HackSecureVirtualMemory(
105 IN PVOID Address,
106 IN SIZE_T Size,
107 IN ULONG ProbeMode,
108 OUT PVOID *SafeAddress)
109 {
110 NTSTATUS Status = STATUS_SUCCESS;
111 PMDL mdl;
112 LOCK_OPERATION Operation;
113
114 if (ProbeMode == PAGE_READONLY) Operation = IoReadAccess;
115 else if (ProbeMode == PAGE_READWRITE) Operation = IoModifyAccess;
116 else return NULL;
117
118 mdl = IoAllocateMdl(Address, Size, FALSE, TRUE, NULL);
119 if (mdl == NULL)
120 {
121 return NULL;
122 }
123
124 _SEH2_TRY
125 {
126 MmProbeAndLockPages(mdl, UserMode, Operation);
127 }
128 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
129 {
130 Status = _SEH2_GetExceptionCode();
131 }
132 _SEH2_END
133
134 if (!NT_SUCCESS(Status))
135 {
136 IoFreeMdl(mdl);
137 return NULL;
138 }
139
140 *SafeAddress = MmGetSystemAddressForMdlSafe(mdl, NormalPagePriority);
141
142 if(!*SafeAddress)
143 {
144 MmUnlockPages(mdl);
145 IoFreeMdl(mdl);
146 return NULL;
147 }
148
149 return mdl;
150 }
151
152 VOID
153 APIENTRY
154 HackUnsecureVirtualMemory(
155 IN PVOID SecureHandle)
156 {
157 PMDL mdl = (PMDL)SecureHandle;
158
159 MmUnlockPages(mdl);
160 IoFreeMdl(mdl);
161 }
162
163 /*
164 * @implemented
165 */
166 HANDLE APIENTRY
167 EngSecureMem(PVOID Address, ULONG Length)
168 {
169 return MmSecureVirtualMemory(Address, Length, PAGE_READWRITE);
170 }
171
172 /*
173 * @implemented
174 */
175 VOID APIENTRY
176 EngUnsecureMem(HANDLE Mem)
177 {
178 MmUnsecureVirtualMemory((PVOID) Mem);
179 }
180
181 /* EOF */