fixed success check in RtlpCreateAtomHandle, ExCreateHandle returns EX_INVALID_HANDLE...
[reactos.git] / reactos / ntoskrnl / rtl / libsupp.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/rtl/libsupp.c
6 * PURPOSE: Rtl library support routines
7 *
8 * PROGRAMMERS: No programmer listed.
9 */
10
11 /* INCLUDES ******************************************************************/
12
13 #include <ntoskrnl.h>
14 #define NDEBUG
15 #include <internal/debug.h>
16
17 /* FUNCTIONS *****************************************************************/
18
19
20 KPROCESSOR_MODE
21 RtlpGetMode()
22 {
23 return KernelMode;
24 }
25
26 PVOID
27 RtlpAllocateMemory(UINT Bytes,
28 ULONG Tag)
29 {
30 return ExAllocatePoolWithTag(PagedPool,
31 (SIZE_T)Bytes,
32 Tag);
33 }
34
35
36 VOID
37 RtlpFreeMemory(PVOID Mem,
38 ULONG Tag)
39 {
40 ExFreePoolWithTag(Mem,
41 Tag);
42 }
43
44 /*
45 * @implemented
46 */
47 VOID STDCALL
48 RtlAcquirePebLock(VOID)
49 {
50
51 }
52
53 /*
54 * @implemented
55 */
56 VOID STDCALL
57 RtlReleasePebLock(VOID)
58 {
59
60 }
61
62
63 PPEB
64 STDCALL
65 RtlpCurrentPeb(VOID)
66 {
67 return ((PEPROCESS)(KeGetCurrentThread()->ApcState.Process))->Peb;
68 }
69
70 NTSTATUS
71 STDCALL
72 RtlDeleteCriticalSection(
73 PRTL_CRITICAL_SECTION CriticalSection)
74 {
75 return STATUS_SUCCESS;
76 }
77
78 DWORD
79 STDCALL
80 RtlSetCriticalSectionSpinCount(
81 PRTL_CRITICAL_SECTION CriticalSection,
82 DWORD SpinCount
83 )
84 {
85 return 0;
86 }
87
88 NTSTATUS
89 STDCALL
90 RtlEnterCriticalSection(
91 PRTL_CRITICAL_SECTION CriticalSection)
92 {
93 ExAcquireFastMutex((PFAST_MUTEX) CriticalSection);
94 return STATUS_SUCCESS;
95 }
96
97 NTSTATUS
98 STDCALL
99 RtlInitializeCriticalSection(
100 PRTL_CRITICAL_SECTION CriticalSection)
101 {
102 ExInitializeFastMutex((PFAST_MUTEX)CriticalSection );
103 return STATUS_SUCCESS;
104 }
105
106 NTSTATUS
107 STDCALL
108 RtlLeaveCriticalSection(
109 PRTL_CRITICAL_SECTION CriticalSection)
110 {
111 ExReleaseFastMutex((PFAST_MUTEX) CriticalSection );
112 return STATUS_SUCCESS;
113 }
114
115 BOOLEAN
116 STDCALL
117 RtlTryEnterCriticalSection(
118 PRTL_CRITICAL_SECTION CriticalSection)
119 {
120 return ExTryToAcquireFastMutex((PFAST_MUTEX) CriticalSection );
121 }
122
123
124 NTSTATUS
125 STDCALL
126 RtlInitializeCriticalSectionAndSpinCount(
127 PRTL_CRITICAL_SECTION CriticalSection,
128 ULONG SpinCount)
129 {
130 ExInitializeFastMutex((PFAST_MUTEX)CriticalSection );
131 return STATUS_SUCCESS;
132 }
133
134
135 #ifdef DBG
136 VOID FASTCALL
137 CHECK_PAGED_CODE_RTL(char *file, int line)
138 {
139 if(KeGetCurrentIrql() > APC_LEVEL)
140 {
141 DbgPrint("%s:%i: Pagable code called at IRQL > APC_LEVEL (%d)\n", file, line, KeGetCurrentIrql());
142 KEBUGCHECK(0);
143 }
144 }
145 #endif
146
147 /* RTL Atom Tables ************************************************************/
148
149 NTSTATUS
150 RtlpInitAtomTableLock(PRTL_ATOM_TABLE AtomTable)
151 {
152 ExInitializeFastMutex(&AtomTable->FastMutex);
153
154 return STATUS_SUCCESS;
155 }
156
157
158 VOID
159 RtlpDestroyAtomTableLock(PRTL_ATOM_TABLE AtomTable)
160 {
161 }
162
163
164 BOOLEAN
165 RtlpLockAtomTable(PRTL_ATOM_TABLE AtomTable)
166 {
167 ExAcquireFastMutex(&AtomTable->FastMutex);
168 return TRUE;
169 }
170
171 VOID
172 RtlpUnlockAtomTable(PRTL_ATOM_TABLE AtomTable)
173 {
174 ExReleaseFastMutex(&AtomTable->FastMutex);
175 }
176
177 BOOLEAN
178 RtlpCreateAtomHandleTable(PRTL_ATOM_TABLE AtomTable)
179 {
180 AtomTable->ExHandleTable = ExCreateHandleTable(NULL);
181 return (AtomTable->ExHandleTable != NULL);
182 }
183
184 static VOID STDCALL
185 AtomDeleteHandleCallback(PHANDLE_TABLE HandleTable,
186 PVOID Object,
187 ULONG GrantedAccess,
188 PVOID Context)
189 {
190 return;
191 }
192
193 VOID
194 RtlpDestroyAtomHandleTable(PRTL_ATOM_TABLE AtomTable)
195 {
196 if (AtomTable->ExHandleTable)
197 {
198 ExDestroyHandleTable(AtomTable->ExHandleTable,
199 AtomDeleteHandleCallback,
200 AtomTable);
201 AtomTable->ExHandleTable = NULL;
202 }
203 }
204
205 PRTL_ATOM_TABLE
206 RtlpAllocAtomTable(ULONG Size)
207 {
208 PRTL_ATOM_TABLE Table = ExAllocatePool(NonPagedPool,
209 Size);
210 if (Table != NULL)
211 {
212 RtlZeroMemory(Table,
213 Size);
214 }
215
216 return Table;
217 }
218
219 VOID
220 RtlpFreeAtomTable(PRTL_ATOM_TABLE AtomTable)
221 {
222 ExFreePool(AtomTable);
223 }
224
225 PRTL_ATOM_TABLE_ENTRY
226 RtlpAllocAtomTableEntry(ULONG Size)
227 {
228 PRTL_ATOM_TABLE_ENTRY Entry = ExAllocatePool(NonPagedPool,
229 Size);
230 if (Entry != NULL)
231 {
232 RtlZeroMemory(Entry,
233 Size);
234 }
235
236 return Entry;
237 }
238
239 VOID
240 RtlpFreeAtomTableEntry(PRTL_ATOM_TABLE_ENTRY Entry)
241 {
242 ExFreePool(Entry);
243 }
244
245 VOID
246 RtlpFreeAtomHandle(PRTL_ATOM_TABLE AtomTable, PRTL_ATOM_TABLE_ENTRY Entry)
247 {
248 ExDestroyHandle(AtomTable->ExHandleTable,
249 (LONG)Entry->HandleIndex);
250 }
251
252 BOOLEAN
253 RtlpCreateAtomHandle(PRTL_ATOM_TABLE AtomTable, PRTL_ATOM_TABLE_ENTRY Entry)
254 {
255 HANDLE_TABLE_ENTRY ExEntry;
256 LONG HandleIndex;
257
258 ExEntry.u1.Object = Entry;
259 ExEntry.u2.GrantedAccess = 0x1; /* FIXME - valid handle */
260
261 HandleIndex = ExCreateHandle(AtomTable->ExHandleTable,
262 &ExEntry);
263 if (HandleIndex != EX_INVALID_HANDLE)
264 {
265 /* FIXME - Handle Indexes >= 0xC000 ?! */
266 if (HandleIndex < 0xC000)
267 {
268 Entry->HandleIndex = (USHORT)HandleIndex;
269 Entry->Atom = 0xC000 + (USHORT)HandleIndex;
270
271 return TRUE;
272 }
273 else
274 ExDestroyHandle(AtomTable->ExHandleTable,
275 HandleIndex);
276 }
277
278 return FALSE;
279 }
280
281 PRTL_ATOM_TABLE_ENTRY
282 RtlpGetAtomEntry(PRTL_ATOM_TABLE AtomTable, ULONG Index)
283 {
284 PHANDLE_TABLE_ENTRY ExEntry;
285
286 ExEntry = ExMapHandleToPointer(AtomTable->ExHandleTable,
287 (LONG)Index);
288 if (ExEntry != NULL)
289 {
290 PRTL_ATOM_TABLE_ENTRY Entry;
291
292 Entry = ExEntry->u1.Object;
293
294 ExUnlockHandleTableEntry(AtomTable->ExHandleTable,
295 ExEntry);
296 return Entry;
297 }
298
299 return NULL;
300 }
301
302 /* FIXME - RtlpCreateUnicodeString is obsolete and should be removed ASAP! */
303 BOOLEAN FASTCALL
304 RtlpCreateUnicodeString(
305 IN OUT PUNICODE_STRING UniDest,
306 IN PCWSTR Source,
307 IN POOL_TYPE PoolType)
308 {
309 ULONG Length;
310
311 Length = (wcslen (Source) + 1) * sizeof(WCHAR);
312 UniDest->Buffer = ExAllocatePoolWithTag(PoolType, Length, TAG('U', 'S', 'T', 'R'));
313 if (UniDest->Buffer == NULL)
314 return FALSE;
315
316 RtlCopyMemory (UniDest->Buffer,
317 Source,
318 Length);
319
320 UniDest->MaximumLength = Length;
321 UniDest->Length = Length - sizeof (WCHAR);
322
323 return TRUE;
324 }
325
326 /* EOF */