2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
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.
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.
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.
26 _Function_class_(RTL_HEAP_COMMIT_ROUTINE
)
31 IntUserHeapCommitRoutine(
33 _Inout_ PVOID
*CommitAddress
,
34 _Inout_ PSIZE_T CommitSize
)
36 PPROCESSINFO W32Process
;
37 PW32HEAP_USER_MAPPING Mapping
;
38 PVOID UserBase
= NULL
;
41 PVOID UserCommitAddress
;
43 W32Process
= PsGetCurrentProcessWin32Process();
45 if (W32Process
!= NULL
)
47 /* Search for the mapping */
48 Mapping
= &W32Process
->HeapMappings
;
49 while (Mapping
!= NULL
)
51 if (Mapping
->KernelMapping
== Base
)
53 UserBase
= Mapping
->UserMapping
;
57 Mapping
= Mapping
->Next
;
60 ASSERT(UserBase
!= NULL
);
66 extern PVOID GlobalUserHeapSection
;
68 /* HACK: This needs to be handled during startup only... */
69 ASSERT(Base
== (PVOID
)GlobalUserHeap
);
71 /* Temporarily map it into user space */
73 Status
= MmMapViewOfSection(GlobalUserHeapSection
,
74 PsGetCurrentProcess(),
82 PAGE_EXECUTE_READ
); /* Would prefer PAGE_READONLY, but thanks to RTL heaps... */
84 if (!NT_SUCCESS(Status
))
88 /* Apply the commit address offset to the user base address */
89 Delta
= (SIZE_T
) ((ULONG_PTR
) (*CommitAddress
) - (ULONG_PTR
) (Base
));
90 UserCommitAddress
= (PVOID
) ((ULONG_PTR
) (UserBase
) + Delta
);
92 /* Perform the actual commit */
93 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
100 if (NT_SUCCESS(Status
))
102 /* Determine the address to return */
103 Delta
= (SIZE_T
) ((ULONG_PTR
) (UserCommitAddress
) - (ULONG_PTR
) (UserBase
));
104 *CommitAddress
= (PVOID
) ((ULONG_PTR
) (Base
) + Delta
);
107 if (W32Process
== NULL
)
109 MmUnmapViewOfSection(PsGetCurrentProcess(),
117 IntUserHeapCreate(IN PVOID SectionObject
,
118 IN PVOID
*SystemMappedBase
,
121 PVOID MappedView
= NULL
;
122 LARGE_INTEGER Offset
;
123 SIZE_T ViewSize
= PAGE_SIZE
;
124 RTL_HEAP_PARAMETERS Parameters
= {0};
130 /* Commit the first page before creating the heap! */
131 Status
= MmMapViewOfSection(SectionObject
,
132 PsGetCurrentProcess(),
140 PAGE_EXECUTE_READ
); /* Would prefer PAGE_READONLY, but thanks to RTL heaps... */
141 if (!NT_SUCCESS(Status
))
144 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
149 PAGE_EXECUTE_READ
); /* Would prefer PAGE_READONLY, but thanks to RTL heaps... */
151 MmUnmapViewOfSection(PsGetCurrentProcess(),
154 if (!NT_SUCCESS(Status
))
157 /* Create the heap, don't serialize in kmode! The caller is responsible
158 to synchronize the heap! */
159 Parameters
.Length
= sizeof(Parameters
);
160 Parameters
.InitialCommit
= ViewSize
;
161 Parameters
.InitialReserve
= (SIZE_T
)HeapSize
;
162 Parameters
.CommitRoutine
= IntUserHeapCommitRoutine
;
164 pHeap
= RtlCreateHeap(HEAP_ZERO_MEMORY
| HEAP_NO_SERIALIZE
,
175 UserCreateHeap(OUT PVOID
*SectionObject
,
176 IN OUT PVOID
*SystemBase
,
179 LARGE_INTEGER SizeHeap
;
180 PWIN32HEAP pHeap
= NULL
;
183 SizeHeap
.QuadPart
= HeapSize
;
185 /* Create the section and map it into session space */
186 Status
= MmCreateSection((PVOID
*)SectionObject
,
190 PAGE_EXECUTE_READWRITE
, /* Would prefer PAGE_READWRITE, but thanks to RTL heaps... */
195 if (!NT_SUCCESS(Status
))
197 SetLastNtError(Status
);
201 Status
= MmMapViewInSessionSpace(*SectionObject
,
204 if (!NT_SUCCESS(Status
))
206 ObDereferenceObject(*SectionObject
);
207 *SectionObject
= NULL
;
209 SetLastNtError(Status
);
213 /* Create the heap */
214 pHeap
= IntUserHeapCreate(*SectionObject
,
220 ObDereferenceObject(*SectionObject
);
221 *SectionObject
= NULL
;
223 SetLastNtError(STATUS_UNSUCCESSFUL
);