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
8 * PROGRAMMERS: No programmer listed.
11 /* INCLUDES ******************************************************************/
15 #include <internal/debug.h>
17 /* FUNCTIONS *****************************************************************/
27 RtlpAllocateMemory(UINT Bytes
,
30 return ExAllocatePoolWithTag(PagedPool
,
37 RtlpFreeMemory(PVOID Mem
,
40 ExFreePoolWithTag(Mem
,
48 RtlAcquirePebLock(VOID
)
57 RtlReleasePebLock(VOID
)
67 return ((PEPROCESS
)(KeGetCurrentThread()->ApcState
.Process
))->Peb
;
72 RtlDeleteCriticalSection(
73 PRTL_CRITICAL_SECTION CriticalSection
)
75 return STATUS_SUCCESS
;
80 RtlSetCriticalSectionSpinCount(
81 PRTL_CRITICAL_SECTION CriticalSection
,
90 RtlEnterCriticalSection(
91 PRTL_CRITICAL_SECTION CriticalSection
)
93 ExAcquireFastMutex((PFAST_MUTEX
) CriticalSection
);
94 return STATUS_SUCCESS
;
99 RtlInitializeCriticalSection(
100 PRTL_CRITICAL_SECTION CriticalSection
)
102 ExInitializeFastMutex((PFAST_MUTEX
)CriticalSection
);
103 return STATUS_SUCCESS
;
108 RtlLeaveCriticalSection(
109 PRTL_CRITICAL_SECTION CriticalSection
)
111 ExReleaseFastMutex((PFAST_MUTEX
) CriticalSection
);
112 return STATUS_SUCCESS
;
117 RtlTryEnterCriticalSection(
118 PRTL_CRITICAL_SECTION CriticalSection
)
120 return ExTryToAcquireFastMutex((PFAST_MUTEX
) CriticalSection
);
126 RtlInitializeCriticalSectionAndSpinCount(
127 PRTL_CRITICAL_SECTION CriticalSection
,
130 ExInitializeFastMutex((PFAST_MUTEX
)CriticalSection
);
131 return STATUS_SUCCESS
;
137 CHECK_PAGED_CODE_RTL(char *file
, int line
)
139 if(KeGetCurrentIrql() > APC_LEVEL
)
141 DbgPrint("%s:%i: Pagable code called at IRQL > APC_LEVEL (%d)\n", file
, line
, KeGetCurrentIrql());
147 /* RTL Atom Tables ************************************************************/
150 RtlpInitAtomTableLock(PRTL_ATOM_TABLE AtomTable
)
152 ExInitializeFastMutex(&AtomTable
->FastMutex
);
154 return STATUS_SUCCESS
;
159 RtlpDestroyAtomTableLock(PRTL_ATOM_TABLE AtomTable
)
165 RtlpLockAtomTable(PRTL_ATOM_TABLE AtomTable
)
167 ExAcquireFastMutex(&AtomTable
->FastMutex
);
172 RtlpUnlockAtomTable(PRTL_ATOM_TABLE AtomTable
)
174 ExReleaseFastMutex(&AtomTable
->FastMutex
);
178 RtlpCreateAtomHandleTable(PRTL_ATOM_TABLE AtomTable
)
180 AtomTable
->ExHandleTable
= ExCreateHandleTable(NULL
);
181 return (AtomTable
->ExHandleTable
!= NULL
);
185 AtomDeleteHandleCallback(PHANDLE_TABLE HandleTable
,
194 RtlpDestroyAtomHandleTable(PRTL_ATOM_TABLE AtomTable
)
196 if (AtomTable
->ExHandleTable
)
198 ExDestroyHandleTable(AtomTable
->ExHandleTable
,
199 AtomDeleteHandleCallback
,
201 AtomTable
->ExHandleTable
= NULL
;
206 RtlpAllocAtomTable(ULONG Size
)
208 PRTL_ATOM_TABLE Table
= ExAllocatePool(NonPagedPool
,
220 RtlpFreeAtomTable(PRTL_ATOM_TABLE AtomTable
)
222 ExFreePool(AtomTable
);
225 PRTL_ATOM_TABLE_ENTRY
226 RtlpAllocAtomTableEntry(ULONG Size
)
228 PRTL_ATOM_TABLE_ENTRY Entry
= ExAllocatePool(NonPagedPool
,
240 RtlpFreeAtomTableEntry(PRTL_ATOM_TABLE_ENTRY Entry
)
246 RtlpFreeAtomHandle(PRTL_ATOM_TABLE AtomTable
, PRTL_ATOM_TABLE_ENTRY Entry
)
248 ExDestroyHandle(AtomTable
->ExHandleTable
,
249 (HANDLE
)((ULONG_PTR
)Entry
->HandleIndex
<< 2));
253 RtlpCreateAtomHandle(PRTL_ATOM_TABLE AtomTable
, PRTL_ATOM_TABLE_ENTRY Entry
)
255 HANDLE_TABLE_ENTRY ExEntry
;
259 ExEntry
.u1
.Object
= Entry
;
260 ExEntry
.u2
.GrantedAccess
= 0x1; /* FIXME - valid handle */
262 Handle
= ExCreateHandle(AtomTable
->ExHandleTable
,
266 HandleIndex
= (USHORT
)((ULONG_PTR
)Handle
>> 2);
267 /* FIXME - Handle Indexes >= 0xC000 ?! */
268 if ((ULONG_PTR
)HandleIndex
>> 2 < 0xC000)
270 Entry
->HandleIndex
= HandleIndex
;
271 Entry
->Atom
= 0xC000 + HandleIndex
;
276 ExDestroyHandle(AtomTable
->ExHandleTable
,
283 PRTL_ATOM_TABLE_ENTRY
284 RtlpGetAtomEntry(PRTL_ATOM_TABLE AtomTable
, ULONG Index
)
286 PHANDLE_TABLE_ENTRY ExEntry
;
287 PRTL_ATOM_TABLE_ENTRY Entry
= NULL
;
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) */
293 ExEntry
= ExMapHandleToPointer(AtomTable
->ExHandleTable
,
294 (HANDLE
)((ULONG_PTR
)Index
<< 2));
297 Entry
= ExEntry
->u1
.Object
;
299 ExUnlockHandleTableEntry(AtomTable
->ExHandleTable
,
306 /* FIXME - RtlpCreateUnicodeString is obsolete and should be removed ASAP! */
308 RtlpCreateUnicodeString(
309 IN OUT PUNICODE_STRING UniDest
,
311 IN POOL_TYPE PoolType
)
315 Length
= (wcslen (Source
) + 1) * sizeof(WCHAR
);
316 UniDest
->Buffer
= ExAllocatePoolWithTag(PoolType
, Length
, TAG('U', 'S', 'T', 'R'));
317 if (UniDest
->Buffer
== NULL
)
320 RtlCopyMemory (UniDest
->Buffer
,
324 UniDest
->MaximumLength
= Length
;
325 UniDest
->Length
= Length
- sizeof (WCHAR
);