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 static NTSTATUS APIENTRY
27 IntUserHeapCommitRoutine(IN PVOID Base
,
28 IN OUT PVOID
*CommitAddress
,
29 IN OUT PSIZE_T CommitSize
)
31 PPROCESSINFO W32Process
;
32 PW32HEAP_USER_MAPPING Mapping
;
33 PVOID UserBase
= NULL
;
36 PVOID UserCommitAddress
;
38 W32Process
= PsGetCurrentProcessWin32Process();
40 if (W32Process
!= NULL
)
42 /* search for the mapping */
43 Mapping
= &W32Process
->HeapMappings
;
44 while (Mapping
!= NULL
)
46 if (Mapping
->KernelMapping
== Base
)
48 UserBase
= Mapping
->UserMapping
;
52 Mapping
= Mapping
->Next
;
55 ASSERT(UserBase
!= NULL
);
61 extern PSECTION_OBJECT GlobalUserHeapSection
;
63 /* HACK: This needs to be handled during startup only... */
64 ASSERT(Base
== (PVOID
)GlobalUserHeap
);
66 /* temporarily map it into user space */
68 Status
= MmMapViewOfSection(GlobalUserHeapSection
,
69 PsGetCurrentProcess(),
77 PAGE_EXECUTE_READ
); /* would prefer PAGE_READONLY, but thanks to RTL heaps... */
79 if (!NT_SUCCESS(Status
))
83 /* Apply the commit address offset to the user base address */
84 Delta
= (SIZE_T
) ((ULONG_PTR
) (*CommitAddress
) - (ULONG_PTR
) (Base
));
85 UserCommitAddress
= (PVOID
) ((ULONG_PTR
) (UserBase
) + Delta
);
87 /* Perform the actual commit */
88 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
95 if (NT_SUCCESS(Status
))
97 /* Determine the address to return */
98 Delta
= (SIZE_T
) ((ULONG_PTR
) (UserCommitAddress
) - (ULONG_PTR
) (UserBase
));
99 *CommitAddress
= (PVOID
) ((ULONG_PTR
) (Base
) + Delta
);
102 if (W32Process
== NULL
)
104 MmUnmapViewOfSection(PsGetCurrentProcess(),
112 IntUserHeapCreate(IN PSECTION_OBJECT SectionObject
,
113 IN PVOID
*SystemMappedBase
,
116 PVOID MappedView
= NULL
;
117 LARGE_INTEGER Offset
;
118 SIZE_T ViewSize
= PAGE_SIZE
;
119 RTL_HEAP_PARAMETERS Parameters
= {0};
125 /* Commit the first page before creating the heap! */
126 Status
= MmMapViewOfSection(SectionObject
,
127 PsGetCurrentProcess(),
135 PAGE_EXECUTE_READ
); /* would prefer PAGE_READONLY, but thanks to RTL heaps... */
136 if (!NT_SUCCESS(Status
))
139 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
144 PAGE_EXECUTE_READ
); /* would prefer PAGE_READONLY, but thanks to RTL heaps... */
146 MmUnmapViewOfSection(PsGetCurrentProcess(),
149 if (!NT_SUCCESS(Status
))
152 /* Create the heap, don't serialize in kmode! The caller is responsible
153 to synchronize the heap! */
154 Parameters
.Length
= sizeof(Parameters
);
155 Parameters
.InitialCommit
= ViewSize
;
156 Parameters
.InitialReserve
= (SIZE_T
)HeapSize
;
157 Parameters
.CommitRoutine
= IntUserHeapCommitRoutine
;
159 pHeap
= RtlCreateHeap(HEAP_ZERO_MEMORY
| HEAP_NO_SERIALIZE
,
170 UserCreateHeap(OUT PSECTION_OBJECT
*SectionObject
,
171 IN OUT PVOID
*SystemBase
,
174 LARGE_INTEGER SizeHeap
;
175 PWIN32HEAP pHeap
= NULL
;
178 SizeHeap
.QuadPart
= HeapSize
;
180 /* create the section and map it into session space */
181 Status
= MmCreateSection((PVOID
*)SectionObject
,
185 PAGE_EXECUTE_READWRITE
, /* would prefer PAGE_READWRITE, but thanks to RTL heaps... */
190 if (!NT_SUCCESS(Status
))
192 SetLastNtError(Status
);
196 Status
= MmMapViewInSystemSpace(*SectionObject
,
199 if (!NT_SUCCESS(Status
))
201 ObDereferenceObject(*SectionObject
);
202 *SectionObject
= NULL
;
204 SetLastNtError(Status
);
208 /* create the heap */
209 pHeap
= IntUserHeapCreate(*SectionObject
,
215 ObDereferenceObject(*SectionObject
);
216 *SectionObject
= NULL
;
218 SetLastNtError(STATUS_UNSUCCESSFUL
);