2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/rtl/libsupp.c
5 * PURPOSE: RTL Support Routines
6 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
10 /* INCLUDES ******************************************************************/
14 #include <internal/debug.h>
16 extern ULONG NtGlobalFlag
;
18 /* FUNCTIONS *****************************************************************/
22 RtlpCheckForActiveDebugger(BOOLEAN Type
)
24 /* This check is meaningless in kernel-mode */
30 RtlpSetInDbgPrint(IN BOOLEAN NewValue
)
32 /* This check is meaningless in kernel-mode */
45 RtlpAllocateMemory(UINT Bytes
,
48 return ExAllocatePoolWithTag(PagedPool
,
56 RtlpFreeMemory(PVOID Mem
,
59 ExFreePoolWithTag(Mem
,
67 RtlAcquirePebLock(VOID
)
76 RtlReleasePebLock(VOID
)
83 LdrShutdownThread(VOID
)
85 return STATUS_SUCCESS
;
93 return ((PEPROCESS
)(KeGetCurrentThread()->ApcState
.Process
))->Peb
;
99 PRTL_CRITICAL_SECTION CriticalSection
)
102 return STATUS_SUCCESS
;
108 PRTL_CRITICAL_SECTION CriticalSection
)
111 return STATUS_SUCCESS
;
116 RtlInitializeHeapLock(
117 PRTL_CRITICAL_SECTION CriticalSection
)
120 return STATUS_SUCCESS
;
126 PRTL_CRITICAL_SECTION CriticalSection
)
129 return STATUS_SUCCESS
;
134 CHECK_PAGED_CODE_RTL(char *file
, int line
)
136 if(KeGetCurrentIrql() > APC_LEVEL
)
138 DbgPrint("%s:%i: Pagable code called at IRQL > APC_LEVEL (%d)\n", file
, line
, KeGetCurrentIrql());
146 RtlpCheckLogException(IN PEXCEPTION_RECORD ExceptionRecord
,
147 IN PCONTEXT ContextRecord
,
148 IN PVOID ContextData
,
151 /* Check the global flag */
152 if (NtGlobalFlag
& FLG_ENABLE_EXCEPTION_LOGGING
)
154 /* FIXME: Log this exception */
160 RtlpHandleDpcStackException(IN PEXCEPTION_REGISTRATION_RECORD RegistrationFrame
,
161 IN ULONG_PTR RegistrationFrameEnd
,
162 IN OUT PULONG_PTR StackLow
,
163 IN OUT PULONG_PTR StackHigh
)
168 /* Check if we are at DISPATCH or higher */
169 if (KeGetCurrentIrql() >= DISPATCH_LEVEL
)
171 /* Get the PRCB and DPC Stack */
172 Prcb
= KeGetCurrentPrcb();
173 DpcStack
= (ULONG_PTR
)Prcb
->DpcStack
;
175 /* Check if we are in a DPC and the stack matches */
176 if ((Prcb
->DpcRoutineActive
) &&
177 (RegistrationFrameEnd
<= DpcStack
) &&
178 ((ULONG_PTR
)RegistrationFrame
>= DpcStack
- 4096))
180 /* Update the limits to the DPC Stack's */
181 *StackHigh
= DpcStack
;
182 *StackLow
= DpcStack
- 4096;
187 /* Not in DPC stack */
191 /* RTL Atom Tables ************************************************************/
194 RtlpInitAtomTableLock(PRTL_ATOM_TABLE AtomTable
)
196 ExInitializeFastMutex(&AtomTable
->FastMutex
);
198 return STATUS_SUCCESS
;
203 RtlpDestroyAtomTableLock(PRTL_ATOM_TABLE AtomTable
)
209 RtlpLockAtomTable(PRTL_ATOM_TABLE AtomTable
)
211 ExAcquireFastMutex(&AtomTable
->FastMutex
);
216 RtlpUnlockAtomTable(PRTL_ATOM_TABLE AtomTable
)
218 ExReleaseFastMutex(&AtomTable
->FastMutex
);
222 RtlpCreateAtomHandleTable(PRTL_ATOM_TABLE AtomTable
)
224 AtomTable
->ExHandleTable
= ExCreateHandleTable(NULL
);
225 return (AtomTable
->ExHandleTable
!= NULL
);
229 AtomDeleteHandleCallback(PHANDLE_TABLE HandleTable
,
238 RtlpDestroyAtomHandleTable(PRTL_ATOM_TABLE AtomTable
)
240 if (AtomTable
->ExHandleTable
)
242 ExDestroyHandleTable(AtomTable
->ExHandleTable
,
243 AtomDeleteHandleCallback
,
245 AtomTable
->ExHandleTable
= NULL
;
250 RtlpAllocAtomTable(ULONG Size
)
252 PRTL_ATOM_TABLE Table
= ExAllocatePool(NonPagedPool
,
264 RtlpFreeAtomTable(PRTL_ATOM_TABLE AtomTable
)
266 ExFreePool(AtomTable
);
269 PRTL_ATOM_TABLE_ENTRY
270 RtlpAllocAtomTableEntry(ULONG Size
)
272 PRTL_ATOM_TABLE_ENTRY Entry
= ExAllocatePool(NonPagedPool
,
284 RtlpFreeAtomTableEntry(PRTL_ATOM_TABLE_ENTRY Entry
)
290 RtlpFreeAtomHandle(PRTL_ATOM_TABLE AtomTable
, PRTL_ATOM_TABLE_ENTRY Entry
)
292 ExDestroyHandle(AtomTable
->ExHandleTable
,
293 (HANDLE
)((ULONG_PTR
)Entry
->HandleIndex
<< 2));
297 RtlpCreateAtomHandle(PRTL_ATOM_TABLE AtomTable
, PRTL_ATOM_TABLE_ENTRY Entry
)
299 HANDLE_TABLE_ENTRY ExEntry
;
303 ExEntry
.u1
.Object
= Entry
;
304 ExEntry
.u2
.GrantedAccess
= 0x1; /* FIXME - valid handle */
306 Handle
= ExCreateHandle(AtomTable
->ExHandleTable
,
310 HandleIndex
= (USHORT
)((ULONG_PTR
)Handle
>> 2);
311 /* FIXME - Handle Indexes >= 0xC000 ?! */
312 if ((ULONG_PTR
)HandleIndex
>> 2 < 0xC000)
314 Entry
->HandleIndex
= HandleIndex
;
315 Entry
->Atom
= 0xC000 + HandleIndex
;
320 ExDestroyHandle(AtomTable
->ExHandleTable
,
327 PRTL_ATOM_TABLE_ENTRY
328 RtlpGetAtomEntry(PRTL_ATOM_TABLE AtomTable
, ULONG Index
)
330 PHANDLE_TABLE_ENTRY ExEntry
;
331 PRTL_ATOM_TABLE_ENTRY Entry
= NULL
;
333 /* NOTE: There's no need to explicitly enter a critical region because it's
334 guaranteed that we're in a critical region right now (as we hold
335 the atom table lock) */
337 ExEntry
= ExMapHandleToPointer(AtomTable
->ExHandleTable
,
338 (HANDLE
)((ULONG_PTR
)Index
<< 2));
341 Entry
= ExEntry
->u1
.Object
;
343 ExUnlockHandleTableEntry(AtomTable
->ExHandleTable
,
350 /* FIXME - RtlpCreateUnicodeString is obsolete and should be removed ASAP! */
352 RtlpCreateUnicodeString(
353 IN OUT PUNICODE_STRING UniDest
,
355 IN POOL_TYPE PoolType
)
359 Length
= (wcslen (Source
) + 1) * sizeof(WCHAR
);
360 UniDest
->Buffer
= ExAllocatePoolWithTag(PoolType
, Length
, TAG('U', 'S', 'T', 'R'));
361 if (UniDest
->Buffer
== NULL
)
364 RtlCopyMemory (UniDest
->Buffer
,
368 UniDest
->MaximumLength
= Length
;
369 UniDest
->Length
= Length
- sizeof (WCHAR
);