implement DbgPrintEx, vDbgPrintEx, and vDbgPrintExWithPrefix
[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 (HANDLE)((ULONG_PTR)Entry->HandleIndex << 2));
250 }
251
252 BOOLEAN
253 RtlpCreateAtomHandle(PRTL_ATOM_TABLE AtomTable, PRTL_ATOM_TABLE_ENTRY Entry)
254 {
255 HANDLE_TABLE_ENTRY ExEntry;
256 HANDLE Handle;
257 USHORT HandleIndex;
258
259 ExEntry.u1.Object = Entry;
260 ExEntry.u2.GrantedAccess = 0x1; /* FIXME - valid handle */
261
262 Handle = ExCreateHandle(AtomTable->ExHandleTable,
263 &ExEntry);
264 if (Handle != NULL)
265 {
266 HandleIndex = (USHORT)((ULONG_PTR)Handle >> 2);
267 /* FIXME - Handle Indexes >= 0xC000 ?! */
268 if ((ULONG_PTR)HandleIndex >> 2 < 0xC000)
269 {
270 Entry->HandleIndex = HandleIndex;
271 Entry->Atom = 0xC000 + HandleIndex;
272
273 return TRUE;
274 }
275 else
276 ExDestroyHandle(AtomTable->ExHandleTable,
277 Handle);
278 }
279
280 return FALSE;
281 }
282
283 PRTL_ATOM_TABLE_ENTRY
284 RtlpGetAtomEntry(PRTL_ATOM_TABLE AtomTable, ULONG Index)
285 {
286 PHANDLE_TABLE_ENTRY ExEntry;
287 PRTL_ATOM_TABLE_ENTRY Entry = NULL;
288
289 /* NOTE: There's no need to explicitly enter a critical region because it's
290 guaranteed that we're in a critical region right now (as we hold
291 the atom table lock) */
292
293 ExEntry = ExMapHandleToPointer(AtomTable->ExHandleTable,
294 (HANDLE)((ULONG_PTR)Index << 2));
295 if (ExEntry != NULL)
296 {
297 Entry = ExEntry->u1.Object;
298
299 ExUnlockHandleTableEntry(AtomTable->ExHandleTable,
300 ExEntry);
301 }
302
303 return Entry;
304 }
305
306 /* FIXME - RtlpCreateUnicodeString is obsolete and should be removed ASAP! */
307 BOOLEAN FASTCALL
308 RtlpCreateUnicodeString(
309 IN OUT PUNICODE_STRING UniDest,
310 IN PCWSTR Source,
311 IN POOL_TYPE PoolType)
312 {
313 ULONG Length;
314
315 Length = (wcslen (Source) + 1) * sizeof(WCHAR);
316 UniDest->Buffer = ExAllocatePoolWithTag(PoolType, Length, TAG('U', 'S', 'T', 'R'));
317 if (UniDest->Buffer == NULL)
318 return FALSE;
319
320 RtlCopyMemory (UniDest->Buffer,
321 Source,
322 Length);
323
324 UniDest->MaximumLength = Length;
325 UniDest->Length = Length - sizeof (WCHAR);
326
327 return TRUE;
328 }
329
330 /* EOF */