ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

handle.c
Go to the documentation of this file.
00001 /* COPYRIGHT:       See COPYING in the top level directory
00002  * PROJECT:         ReactOS system libraries
00003  * PURPOSE:         Handle table
00004  * FILE:            lib/rtl/handle.c
00005  * PROGRAMER:       Eric Kohl
00006  */
00007 
00008 /* INCLUDES *****************************************************************/
00009 
00010 #include <rtl.h>
00011 
00012 #define NDEBUG
00013 #include <debug.h>
00014 
00015 /* GLOBALS ******************************************************************/
00016 
00017 VOID NTAPI
00018 RtlInitializeHandleTable(ULONG TableSize,
00019              ULONG HandleSize,
00020              PRTL_HANDLE_TABLE HandleTable)
00021 {
00022    /* initialize handle table */
00023    memset(HandleTable,
00024       0,
00025       sizeof(RTL_HANDLE_TABLE));
00026    HandleTable->MaximumNumberOfHandles = TableSize;
00027    HandleTable->SizeOfHandleTableEntry = HandleSize;
00028 }
00029 
00030 
00031 /*
00032  * @implemented
00033  */
00034 VOID NTAPI
00035 RtlDestroyHandleTable(PRTL_HANDLE_TABLE HandleTable)
00036 {
00037    PVOID ArrayPointer;
00038    SIZE_T ArraySize = 0;
00039 
00040    /* free handle array */
00041    if (HandleTable->CommittedHandles)
00042      {
00043         ArrayPointer = (PVOID)HandleTable->CommittedHandles;
00044         NtFreeVirtualMemory(NtCurrentProcess(),
00045                     &ArrayPointer,
00046                     &ArraySize,
00047                     MEM_RELEASE);
00048      }
00049 }
00050 
00051 
00052 /*
00053  * @implemented
00054  */
00055 PRTL_HANDLE_TABLE_ENTRY NTAPI
00056 RtlAllocateHandle(PRTL_HANDLE_TABLE HandleTable,
00057           PULONG Index)
00058 {
00059    PRTL_HANDLE_TABLE_ENTRY *pp_new, *pph, ph;
00060    NTSTATUS Status;
00061    PRTL_HANDLE_TABLE_ENTRY retval;
00062    PVOID ArrayPointer;
00063    SIZE_T ArraySize;
00064 
00065    pp_new = &HandleTable->FreeHandles;
00066 
00067    if (HandleTable->FreeHandles == NULL)
00068      {
00069     /* no free handle available */
00070     if (HandleTable->UnCommittedHandles == NULL)
00071       {
00072          /* allocate handle array */
00073          ArraySize = HandleTable->SizeOfHandleTableEntry * HandleTable->MaximumNumberOfHandles;
00074          ArrayPointer = NULL;
00075 
00076          /* FIXME - only reserve handles here! */
00077          Status = NtAllocateVirtualMemory(NtCurrentProcess(),
00078                           (PVOID*)&ArrayPointer,
00079                           0,
00080                           &ArraySize,
00081                           MEM_RESERVE | MEM_COMMIT,
00082                           PAGE_READWRITE);
00083          if (!NT_SUCCESS(Status))
00084            return NULL;
00085 
00086          /* update handle array pointers */
00087          HandleTable->FreeHandles = (PRTL_HANDLE_TABLE_ENTRY)ArrayPointer;
00088          HandleTable->MaxReservedHandles = (PRTL_HANDLE_TABLE_ENTRY)((ULONG_PTR)ArrayPointer + ArraySize);
00089          HandleTable->CommittedHandles = (PRTL_HANDLE_TABLE_ENTRY)ArrayPointer;
00090          HandleTable->UnCommittedHandles = (PRTL_HANDLE_TABLE_ENTRY)ArrayPointer;
00091       }
00092 
00093         /* FIXME - should check if handles need to be committed */
00094 
00095     /* build free list in handle array */
00096     ph = HandleTable->FreeHandles;
00097     pph = pp_new;
00098     while (ph < HandleTable->MaxReservedHandles)
00099       {
00100          *pph = ph;
00101          pph = &ph->NextFree;
00102          ph = (PRTL_HANDLE_TABLE_ENTRY)((ULONG_PTR)ph + HandleTable->SizeOfHandleTableEntry);
00103       }
00104     *pph = 0;
00105      }
00106 
00107    /* remove handle from free list */
00108    retval = *pp_new;
00109    *pp_new = retval->NextFree;
00110    retval->NextFree = NULL;
00111 
00112    if (Index)
00113      *Index = ((ULONG)((ULONG_PTR)retval - (ULONG_PTR)HandleTable->CommittedHandles) /
00114                HandleTable->SizeOfHandleTableEntry);
00115 
00116    return retval;
00117 }
00118 
00119 
00120 /*
00121  * @implemented
00122  */
00123 BOOLEAN NTAPI
00124 RtlFreeHandle(PRTL_HANDLE_TABLE HandleTable,
00125           PRTL_HANDLE_TABLE_ENTRY Handle)
00126 {
00127 #if DBG
00128    /* check if handle is valid */
00129    if (!RtlIsValidHandle(HandleTable, Handle))
00130    {
00131      DPRINT1("Invalid Handle! HandleTable=0x%p, Handle=0x%p, Handle->Flags=0x%x\n",
00132              HandleTable, Handle, Handle ? Handle->Flags : 0);
00133      return FALSE;
00134    }
00135 #endif
00136 
00137    /* clear handle */
00138    memset(Handle, 0, HandleTable->SizeOfHandleTableEntry);
00139 
00140    /* add handle to free list */
00141    Handle->NextFree = HandleTable->FreeHandles;
00142    HandleTable->FreeHandles = Handle;
00143 
00144    return TRUE;
00145 }
00146 
00147 
00148 /*
00149  * @implemented
00150  */
00151 BOOLEAN NTAPI
00152 RtlIsValidHandle(PRTL_HANDLE_TABLE HandleTable,
00153          PRTL_HANDLE_TABLE_ENTRY Handle)
00154 {
00155    if ((HandleTable != NULL)
00156        && (Handle >= HandleTable->CommittedHandles)
00157        && (Handle < HandleTable->MaxReservedHandles)
00158        && (Handle->Flags & RTL_HANDLE_VALID))
00159      return TRUE;
00160    return FALSE;
00161 }
00162 
00163 
00164 /*
00165  * @implemented
00166  */
00167 BOOLEAN NTAPI
00168 RtlIsValidIndexHandle(IN PRTL_HANDLE_TABLE HandleTable,
00169               IN ULONG Index,
00170               OUT PRTL_HANDLE_TABLE_ENTRY *Handle)
00171 {
00172    PRTL_HANDLE_TABLE_ENTRY InternalHandle;
00173 
00174    DPRINT("RtlIsValidIndexHandle(HandleTable %p Index 0x%lx Handle %p)\n", HandleTable, Index, Handle);
00175 
00176    if (HandleTable == NULL)
00177      return FALSE;
00178 
00179    DPRINT("Handles %p HandleSize 0x%lx\n",
00180       HandleTable->CommittedHandles, HandleTable->SizeOfHandleTableEntry);
00181 
00182    InternalHandle = (PRTL_HANDLE_TABLE_ENTRY)((ULONG_PTR)HandleTable->CommittedHandles +
00183                                               (HandleTable->SizeOfHandleTableEntry * Index));
00184    if (!RtlIsValidHandle(HandleTable, InternalHandle))
00185      return FALSE;
00186 
00187    DPRINT("InternalHandle %p\n", InternalHandle);
00188 
00189    if (Handle != NULL)
00190      *Handle = InternalHandle;
00191 
00192   return TRUE;
00193 }
00194 
00195 /* EOF */

Generated on Sun May 27 2012 04:24:28 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.