[YAROTOWS] Reintegrate the branch. For a brighter future.
[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 //hSecure = EngSecureMem(NewMem, cj);
83
84 return NewMem;
85 }
86
87 /*
88 * @implemented
89 */
90 VOID APIENTRY
91 EngFreeUserMem(PVOID pv)
92 {
93 PVOID BaseAddress = pv;
94 SIZE_T MemSize = 0;
95
96 ZwFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &MemSize, MEM_RELEASE);
97
98 /* TODO: Remove allocation info from AVL tree */
99 }
100
101
102
103 PVOID
104 APIENTRY
105 HackSecureVirtualMemory(
106 IN PVOID Address,
107 IN SIZE_T Size,
108 IN ULONG ProbeMode,
109 OUT PVOID *SafeAddress)
110 {
111 NTSTATUS Status = STATUS_SUCCESS;
112 PMDL mdl;
113 LOCK_OPERATION Operation;
114
115 if (ProbeMode == PAGE_READONLY) Operation = IoReadAccess;
116 else if (ProbeMode == PAGE_READWRITE) Operation = IoModifyAccess;
117 else return NULL;
118
119 mdl = IoAllocateMdl(Address, Size, FALSE, TRUE, NULL);
120 if (mdl == NULL)
121 {
122 return NULL;
123 }
124
125 _SEH2_TRY
126 {
127 MmProbeAndLockPages(mdl, UserMode, Operation);
128 }
129 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
130 {
131 Status = _SEH2_GetExceptionCode();
132 }
133 _SEH2_END
134
135 if (!NT_SUCCESS(Status))
136 {
137 IoFreeMdl(mdl);
138 return NULL;
139 }
140
141 *SafeAddress = MmGetSystemAddressForMdlSafe(mdl, NormalPagePriority);
142
143 if(!*SafeAddress)
144 {
145 MmUnlockPages(mdl);
146 IoFreeMdl(mdl);
147 return NULL;
148 }
149
150 return mdl;
151 }
152
153 VOID
154 APIENTRY
155 HackUnsecureVirtualMemory(
156 IN PVOID SecureHandle)
157 {
158 PMDL mdl = (PMDL)SecureHandle;
159
160 MmUnlockPages(mdl);
161 IoFreeMdl(mdl);
162 }
163
164 /*
165 * @implemented
166 */
167 HANDLE APIENTRY
168 EngSecureMem(PVOID Address, ULONG Length)
169 {
170 return (HANDLE)-1; // HACK!!!
171 return MmSecureVirtualMemory(Address, Length, PAGE_READWRITE);
172 }
173
174 /*
175 * @implemented
176 */
177 VOID APIENTRY
178 EngUnsecureMem(HANDLE Mem)
179 {
180 if (Mem == (HANDLE)-1) return; // HACK!!!
181 MmUnsecureVirtualMemory((PVOID) Mem);
182 }
183
184 /* EOF */