2005-09-08 00:09:32 +00:00
|
|
|
/* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS system libraries
|
|
|
|
* PURPOSE: Handle table
|
|
|
|
* FILE: lib/rtl/handle.c
|
2012-12-15 13:58:24 +00:00
|
|
|
* PROGRAMMER: Eric Kohl
|
2001-05-26 16:52:31 +00:00
|
|
|
*/
|
|
|
|
|
2005-09-08 00:09:32 +00:00
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
|
|
|
#include <rtl.h>
|
2001-05-26 16:52:31 +00:00
|
|
|
|
2005-06-19 22:50:59 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
2001-05-26 16:52:31 +00:00
|
|
|
|
2005-09-08 00:09:32 +00:00
|
|
|
/* GLOBALS ******************************************************************/
|
2001-05-26 16:52:31 +00:00
|
|
|
|
2012-12-15 13:58:24 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
RtlInitializeHandleTable(
|
|
|
|
ULONG TableSize,
|
|
|
|
ULONG HandleSize,
|
|
|
|
PRTL_HANDLE_TABLE HandleTable)
|
2001-05-26 16:52:31 +00:00
|
|
|
{
|
2012-12-15 13:58:24 +00:00
|
|
|
/* Initialize handle table */
|
|
|
|
memset(HandleTable, 0, sizeof(RTL_HANDLE_TABLE));
|
|
|
|
HandleTable->MaximumNumberOfHandles = TableSize;
|
|
|
|
HandleTable->SizeOfHandleTableEntry = HandleSize;
|
2001-05-26 16:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-11 13:50:23 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2012-12-15 13:58:24 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
RtlDestroyHandleTable(
|
|
|
|
PRTL_HANDLE_TABLE HandleTable)
|
2001-05-26 16:52:31 +00:00
|
|
|
{
|
2012-12-15 13:58:24 +00:00
|
|
|
PVOID ArrayPointer;
|
|
|
|
SIZE_T ArraySize = 0;
|
2001-05-26 16:52:31 +00:00
|
|
|
|
2012-12-15 13:58:24 +00:00
|
|
|
/* free handle array */
|
|
|
|
if (HandleTable->CommittedHandles)
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
ArrayPointer = (PVOID)HandleTable->CommittedHandles;
|
|
|
|
NtFreeVirtualMemory(NtCurrentProcess(),
|
2012-12-15 13:58:24 +00:00
|
|
|
&ArrayPointer,
|
|
|
|
&ArraySize,
|
|
|
|
MEM_RELEASE);
|
|
|
|
}
|
2001-05-26 16:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-11 13:50:23 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2012-12-15 13:58:24 +00:00
|
|
|
PRTL_HANDLE_TABLE_ENTRY
|
|
|
|
NTAPI
|
|
|
|
RtlAllocateHandle(
|
|
|
|
PRTL_HANDLE_TABLE HandleTable,
|
|
|
|
PULONG Index)
|
2001-05-26 16:52:31 +00:00
|
|
|
{
|
2012-12-18 10:10:34 +00:00
|
|
|
PRTL_HANDLE_TABLE_ENTRY CurrentEntry, NextEntry;
|
2012-12-15 13:58:24 +00:00
|
|
|
NTSTATUS Status;
|
2012-12-18 10:10:34 +00:00
|
|
|
PRTL_HANDLE_TABLE_ENTRY HandleEntry;
|
2012-12-15 13:58:24 +00:00
|
|
|
PVOID ArrayPointer;
|
|
|
|
SIZE_T ArraySize;
|
2012-12-18 10:10:34 +00:00
|
|
|
ULONG i, NumberOfEntries;
|
2012-12-15 13:58:24 +00:00
|
|
|
|
2012-12-18 10:10:34 +00:00
|
|
|
/* Check if we are out of free handles entries */
|
2012-12-15 13:58:24 +00:00
|
|
|
if (HandleTable->FreeHandles == NULL)
|
|
|
|
{
|
2012-12-18 10:10:34 +00:00
|
|
|
/* Check if we don't have uncomitted handle entries yet */
|
2012-12-15 13:58:24 +00:00
|
|
|
if (HandleTable->UnCommittedHandles == NULL)
|
|
|
|
{
|
2012-12-18 10:10:34 +00:00
|
|
|
/* Use the maximum number of handle entries */
|
2012-12-15 13:58:24 +00:00
|
|
|
ArraySize = HandleTable->SizeOfHandleTableEntry * HandleTable->MaximumNumberOfHandles;
|
|
|
|
ArrayPointer = NULL;
|
|
|
|
|
2012-12-18 10:10:34 +00:00
|
|
|
/* Reserve memory */
|
|
|
|
Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
|
|
|
|
&ArrayPointer,
|
2012-12-15 13:58:24 +00:00
|
|
|
0,
|
|
|
|
&ArraySize,
|
2012-12-18 10:10:34 +00:00
|
|
|
MEM_RESERVE,
|
2012-12-15 13:58:24 +00:00
|
|
|
PAGE_READWRITE);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return NULL;
|
|
|
|
|
2012-12-18 10:10:34 +00:00
|
|
|
/* Update handle array pointers */
|
2012-12-15 13:58:24 +00:00
|
|
|
HandleTable->UnCommittedHandles = (PRTL_HANDLE_TABLE_ENTRY)ArrayPointer;
|
2012-12-18 10:10:34 +00:00
|
|
|
HandleTable->MaxReservedHandles = (PRTL_HANDLE_TABLE_ENTRY)((ULONG_PTR)ArrayPointer + ArraySize);
|
2012-12-15 13:58:24 +00:00
|
|
|
}
|
2001-05-26 16:52:31 +00:00
|
|
|
|
2012-12-18 10:10:34 +00:00
|
|
|
/* Commit one reserved handle entry page */
|
|
|
|
ArraySize = PAGE_SIZE;
|
|
|
|
ArrayPointer = HandleTable->UnCommittedHandles;
|
|
|
|
Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
|
|
|
|
&ArrayPointer,
|
|
|
|
0,
|
|
|
|
&ArraySize,
|
|
|
|
MEM_COMMIT,
|
|
|
|
PAGE_READWRITE);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Update handle array pointers */
|
|
|
|
HandleTable->FreeHandles = (PRTL_HANDLE_TABLE_ENTRY)ArrayPointer;
|
|
|
|
HandleTable->CommittedHandles = (PRTL_HANDLE_TABLE_ENTRY)ArrayPointer;
|
|
|
|
HandleTable->UnCommittedHandles = (PRTL_HANDLE_TABLE_ENTRY)((ULONG_PTR)ArrayPointer + ArraySize);
|
|
|
|
|
|
|
|
/* Calculate the number of entries we can store in the array */
|
|
|
|
NumberOfEntries = ArraySize / HandleTable->SizeOfHandleTableEntry;
|
|
|
|
|
|
|
|
/* Loop all entries, except the last one */
|
|
|
|
CurrentEntry = HandleTable->FreeHandles;
|
|
|
|
for (i = 0; i < NumberOfEntries - 1; i++)
|
2012-12-15 13:58:24 +00:00
|
|
|
{
|
2012-12-18 10:10:34 +00:00
|
|
|
/* Calculate the address of the next handle entry */
|
|
|
|
NextEntry = (PRTL_HANDLE_TABLE_ENTRY)((ULONG_PTR)CurrentEntry +
|
|
|
|
HandleTable->SizeOfHandleTableEntry);
|
|
|
|
|
|
|
|
/* Link the next entry */
|
|
|
|
CurrentEntry->NextFree = NextEntry;
|
|
|
|
|
|
|
|
/* Continue with the next entry */
|
|
|
|
CurrentEntry = NextEntry;
|
2012-12-15 13:58:24 +00:00
|
|
|
}
|
2012-12-18 10:10:34 +00:00
|
|
|
|
|
|
|
/* CurrentEntry now points to the last entry, terminate the list here */
|
|
|
|
CurrentEntry->NextFree = NULL;
|
2012-12-15 13:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* remove handle from free list */
|
2012-12-18 10:10:34 +00:00
|
|
|
HandleEntry = HandleTable->FreeHandles;
|
|
|
|
HandleTable->FreeHandles = HandleEntry->NextFree;
|
|
|
|
HandleEntry->NextFree = NULL;
|
2012-12-15 13:58:24 +00:00
|
|
|
|
|
|
|
if (Index)
|
|
|
|
{
|
2012-12-18 10:10:34 +00:00
|
|
|
*Index = ((ULONG)((ULONG_PTR)HandleEntry - (ULONG_PTR)HandleTable->CommittedHandles) /
|
2012-12-15 13:58:24 +00:00
|
|
|
HandleTable->SizeOfHandleTableEntry);
|
|
|
|
}
|
|
|
|
|
2012-12-18 10:10:34 +00:00
|
|
|
return HandleEntry;
|
2001-05-26 16:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-11 13:50:23 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2012-12-15 13:58:24 +00:00
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
RtlFreeHandle(
|
|
|
|
PRTL_HANDLE_TABLE HandleTable,
|
|
|
|
PRTL_HANDLE_TABLE_ENTRY Handle)
|
2001-05-26 16:52:31 +00:00
|
|
|
{
|
2009-06-17 12:44:05 +00:00
|
|
|
#if DBG
|
2012-12-15 13:58:24 +00:00
|
|
|
/* check if handle is valid */
|
|
|
|
if (!RtlIsValidHandle(HandleTable, Handle))
|
|
|
|
{
|
|
|
|
DPRINT1("Invalid Handle! HandleTable=0x%p, Handle=0x%p, Handle->Flags=0x%x\n",
|
|
|
|
HandleTable, Handle, Handle ? Handle->Flags : 0);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-08-07 23:19:15 +00:00
|
|
|
#endif
|
2005-05-09 01:43:25 +00:00
|
|
|
|
2012-12-15 13:58:24 +00:00
|
|
|
/* clear handle */
|
|
|
|
memset(Handle, 0, HandleTable->SizeOfHandleTableEntry);
|
2005-05-09 01:43:25 +00:00
|
|
|
|
2012-12-15 13:58:24 +00:00
|
|
|
/* add handle to free list */
|
|
|
|
Handle->NextFree = HandleTable->FreeHandles;
|
|
|
|
HandleTable->FreeHandles = Handle;
|
2005-05-09 01:43:25 +00:00
|
|
|
|
2012-12-15 13:58:24 +00:00
|
|
|
return TRUE;
|
2001-05-26 16:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-11 13:50:23 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2012-12-15 13:58:24 +00:00
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
RtlIsValidHandle(
|
|
|
|
PRTL_HANDLE_TABLE HandleTable,
|
|
|
|
PRTL_HANDLE_TABLE_ENTRY Handle)
|
2001-05-26 16:52:31 +00:00
|
|
|
{
|
2012-12-15 13:58:24 +00:00
|
|
|
if ((HandleTable != NULL)
|
|
|
|
&& (Handle >= HandleTable->CommittedHandles)
|
|
|
|
&& (Handle < HandleTable->MaxReservedHandles)
|
|
|
|
&& (Handle->Flags & RTL_HANDLE_VALID))
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
2001-05-26 16:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-11 13:50:23 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2012-12-15 13:58:24 +00:00
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
RtlIsValidIndexHandle(
|
|
|
|
IN PRTL_HANDLE_TABLE HandleTable,
|
|
|
|
IN ULONG Index,
|
|
|
|
OUT PRTL_HANDLE_TABLE_ENTRY *Handle)
|
2001-05-26 16:52:31 +00:00
|
|
|
{
|
2012-12-15 13:58:24 +00:00
|
|
|
PRTL_HANDLE_TABLE_ENTRY InternalHandle;
|
2001-05-26 16:52:31 +00:00
|
|
|
|
2012-12-15 13:58:24 +00:00
|
|
|
DPRINT("RtlIsValidIndexHandle(HandleTable %p Index 0x%lx Handle %p)\n", HandleTable, Index, Handle);
|
2001-05-26 16:52:31 +00:00
|
|
|
|
2012-12-15 13:58:24 +00:00
|
|
|
if (HandleTable == NULL)
|
|
|
|
return FALSE;
|
2001-05-26 16:52:31 +00:00
|
|
|
|
2012-12-15 13:58:24 +00:00
|
|
|
DPRINT("Handles %p HandleSize 0x%lx\n",
|
|
|
|
HandleTable->CommittedHandles, HandleTable->SizeOfHandleTableEntry);
|
2001-05-26 16:52:31 +00:00
|
|
|
|
2012-12-15 13:58:24 +00:00
|
|
|
InternalHandle = (PRTL_HANDLE_TABLE_ENTRY)((ULONG_PTR)HandleTable->CommittedHandles +
|
|
|
|
(HandleTable->SizeOfHandleTableEntry * Index));
|
|
|
|
if (!RtlIsValidHandle(HandleTable, InternalHandle))
|
|
|
|
return FALSE;
|
2001-05-26 16:52:31 +00:00
|
|
|
|
2012-12-15 13:58:24 +00:00
|
|
|
DPRINT("InternalHandle %p\n", InternalHandle);
|
2001-05-26 16:52:31 +00:00
|
|
|
|
2012-12-15 13:58:24 +00:00
|
|
|
if (Handle != NULL)
|
|
|
|
*Handle = InternalHandle;
|
2001-05-26 16:52:31 +00:00
|
|
|
|
2012-12-15 13:58:24 +00:00
|
|
|
return TRUE;
|
2001-05-26 16:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|