directly translate handles to indexes in ex/handle.c
Modified: trunk/reactos/ntoskrnl/ex/handle.c
Modified: trunk/reactos/ntoskrnl/include/internal/ex.h
Modified: trunk/reactos/ntoskrnl/include/internal/ob.h
Modified: trunk/reactos/ntoskrnl/ob/handle.c
Modified: trunk/reactos/ntoskrnl/ob/wait.c
Modified: trunk/reactos/ntoskrnl/ps/process.c
Modified: trunk/reactos/ntoskrnl/ps/thread.c
Modified: trunk/reactos/ntoskrnl/rtl/libsupp.c

Modified: trunk/reactos/ntoskrnl/ex/handle.c
--- trunk/reactos/ntoskrnl/ex/handle.c	2005-08-07 23:37:59 UTC (rev 17190)
+++ trunk/reactos/ntoskrnl/ex/handle.c	2005-08-07 23:43:58 UTC (rev 17191)
@@ -77,6 +77,11 @@
 #define IS_VALID_EX_HANDLE(index)                                              \
   (((index) & ~VALID_HANDLE_MASK) == 0)
 
+#define HANDLE_TO_EX_HANDLE(handle)                                            \
+  (LONG)(((LONG)(handle) >> 2) - 1)
+#define EX_HANDLE_TO_HANDLE(exhandle)                                          \
+  (HANDLE)(((exhandle) + 1) << 2)
+
 static BOOLEAN ExpInitialized = FALSE;
 
 /******************************************************************************/
@@ -491,7 +496,7 @@
 
 static PHANDLE_TABLE_ENTRY
 ExpAllocateHandleTableEntry(IN PHANDLE_TABLE HandleTable,
-                            OUT PLONG Handle)
+                            OUT PHANDLE Handle)
 {
   PHANDLE_TABLE_ENTRY Entry = NULL;
 
@@ -520,7 +525,7 @@
 
       Entry = &HandleTable->Table[tli][mli][eli];
 
-      *Handle = HandleTable->FirstFreeTableEntry;
+      *Handle = EX_HANDLE_TO_HANDLE(HandleTable->FirstFreeTableEntry);
 
       /* save the index to the next free handle (if available) */
       HandleTable->FirstFreeTableEntry = Entry->u2.NextFreeTableEntry;
@@ -629,7 +634,7 @@
       Entry->u1.ObAttributes = EX_HANDLE_ENTRY_LOCKED;
       Entry->u2.NextFreeTableEntry = 0;
 
-      *Handle = HandleTable->NextIndexNeedingPool;
+      *Handle = EX_HANDLE_TO_HANDLE(HandleTable->NextIndexNeedingPool);
 
       HandleTable->HandleCount++;
 
@@ -803,12 +808,12 @@
                FALSE);
 }
 
-LONG
+HANDLE
 ExCreateHandle(IN PHANDLE_TABLE HandleTable,
                IN PHANDLE_TABLE_ENTRY Entry)
 {
   PHANDLE_TABLE_ENTRY NewHandleTableEntry;
-  LONG Handle = EX_INVALID_HANDLE;
+  HANDLE Handle = NULL;
 
   PAGED_CODE();
 
@@ -841,20 +846,23 @@
 
 BOOLEAN
 ExDestroyHandle(IN PHANDLE_TABLE HandleTable,
-                IN LONG Handle)
+                IN HANDLE Handle)
 {
   PHANDLE_TABLE_ENTRY HandleTableEntry;
+  LONG ExHandle;
   BOOLEAN Ret = FALSE;
 
   PAGED_CODE();
 
   ASSERT(HandleTable);
+  
+  ExHandle = HANDLE_TO_EX_HANDLE(Handle);
 
   KeEnterCriticalRegion();
   ExAcquireHandleTableLockExclusive(HandleTable);
 
   HandleTableEntry = ExpLookupHandleTableEntry(HandleTable,
-                                               Handle);
+                                               ExHandle);
 
   if(HandleTableEntry != NULL && ExLockHandleTableEntry(HandleTable, HandleTableEntry))
   {
@@ -862,7 +870,7 @@
        the contention event since other locks on this entry will fail */
     ExpFreeHandleTableEntry(HandleTable,
                             HandleTableEntry,
-                            Handle);
+                            ExHandle);
     Ret = TRUE;
   }
 
@@ -875,7 +883,7 @@
 VOID
 ExDestroyHandleByEntry(IN PHANDLE_TABLE HandleTable,
                        IN PHANDLE_TABLE_ENTRY Entry,
-                       IN LONG Handle)
+                       IN HANDLE Handle)
 {
   PAGED_CODE();
 
@@ -894,7 +902,7 @@
      the contention event since other locks on this entry will fail */
   ExpFreeHandleTableEntry(HandleTable,
                           Entry,
-                          Handle);
+                          HANDLE_TO_EX_HANDLE(Handle));
 
   ExReleaseHandleTableLock(HandleTable);
   KeLeaveCriticalRegion();
@@ -902,7 +910,7 @@
 
 PHANDLE_TABLE_ENTRY
 ExMapHandleToPointer(IN PHANDLE_TABLE HandleTable,
-                     IN LONG Handle)
+                     IN HANDLE Handle)
 {
   PHANDLE_TABLE_ENTRY HandleTableEntry;
 
@@ -911,7 +919,7 @@
   ASSERT(HandleTable);
 
   HandleTableEntry = ExpLookupHandleTableEntry(HandleTable,
-                                               Handle);
+                                               HANDLE_TO_EX_HANDLE(Handle));
   if (HandleTableEntry != NULL && ExLockHandleTableEntry(HandleTable, HandleTableEntry))
   {
     DPRINT("ExMapHandleToPointer HT:0x%p Entry:0x%p locked\n", HandleTable, HandleTableEntry);
@@ -923,7 +931,7 @@
 
 BOOLEAN
 ExChangeHandle(IN PHANDLE_TABLE HandleTable,
-               IN LONG Handle,
+               IN HANDLE Handle,
                IN PEX_CHANGE_HANDLE_CALLBACK ChangeHandleCallback,
                IN PVOID Context)
 {
@@ -938,7 +946,7 @@
   KeEnterCriticalRegion();
 
   HandleTableEntry = ExpLookupHandleTableEntry(HandleTable,
-                                               Handle);
+                                               HANDLE_TO_EX_HANDLE(Handle));
 
   if(HandleTableEntry != NULL && ExLockHandleTableEntry(HandleTable, HandleTableEntry))
   {

Modified: trunk/reactos/ntoskrnl/include/internal/ex.h
--- trunk/reactos/ntoskrnl/include/internal/ex.h	2005-08-07 23:37:59 UTC (rev 17190)
+++ trunk/reactos/ntoskrnl/include/internal/ex.h	2005-08-07 23:43:58 UTC (rev 17191)
@@ -86,8 +86,6 @@
 
 #define EX_HANDLE_TABLE_CLOSING 0x1
 
-#define EX_INVALID_HANDLE (~0)
-
 #define EX_HANDLE_ENTRY_FLAGSMASK (EX_HANDLE_ENTRY_LOCKED |                    \
                                    EX_HANDLE_ENTRY_PROTECTFROMCLOSE |          \
                                    EX_HANDLE_ENTRY_INHERITABLE |               \
@@ -145,7 +143,7 @@
     IN PHANDLE_TABLE_ENTRY Entry
 );
 
-LONG
+HANDLE
 ExCreateHandle(
     IN PHANDLE_TABLE HandleTable,
     IN PHANDLE_TABLE_ENTRY Entry
@@ -154,26 +152,26 @@
 BOOLEAN
 ExDestroyHandle(
     IN PHANDLE_TABLE HandleTable,
-    IN LONG Handle
+    IN HANDLE Handle
 );
 
 VOID
 ExDestroyHandleByEntry(
     IN PHANDLE_TABLE HandleTable,
     IN PHANDLE_TABLE_ENTRY Entry,
-    IN LONG Handle
+    IN HANDLE Handle
 );
 
 PHANDLE_TABLE_ENTRY
 ExMapHandleToPointer(
     IN PHANDLE_TABLE HandleTable,
-    IN LONG Handle
+    IN HANDLE Handle
 );
 
 BOOLEAN
 ExChangeHandle(
     IN PHANDLE_TABLE HandleTable,
-    IN LONG Handle,
+    IN HANDLE Handle,
     IN PEX_CHANGE_HANDLE_CALLBACK ChangeHandleCallback,
     IN PVOID Context
 );

Modified: trunk/reactos/ntoskrnl/include/internal/ob.h
--- trunk/reactos/ntoskrnl/include/internal/ob.h	2005-08-07 23:37:59 UTC (rev 17190)
+++ trunk/reactos/ntoskrnl/include/internal/ob.h	2005-08-07 23:43:58 UTC (rev 17191)
@@ -62,11 +62,6 @@
 
 #define OBJECT_ALLOC_SIZE(ObjectSize) ((ObjectSize)+sizeof(OBJECT_HEADER))
 
-#define HANDLE_TO_EX_HANDLE(handle)                                            \
-  (LONG)(((LONG)(handle) >> 2) - 1)
-#define EX_HANDLE_TO_HANDLE(exhandle)                                          \
-  (HANDLE)(((exhandle) + 1) << 2)
-
 extern PDIRECTORY_OBJECT NameSpaceRoot;
 extern POBJECT_TYPE ObSymbolicLinkType;
 extern PHANDLE_TABLE ObpKernelHandleTable;

Modified: trunk/reactos/ntoskrnl/ob/handle.c
--- trunk/reactos/ntoskrnl/ob/handle.c	2005-08-07 23:37:59 UTC (rev 17190)
+++ trunk/reactos/ntoskrnl/ob/handle.c	2005-08-07 23:43:58 UTC (rev 17191)
@@ -90,7 +90,6 @@
 {
   PHANDLE_TABLE HandleTable;
   PHANDLE_TABLE_ENTRY HandleTableEntry;
-  LONG ExHandle;
 
   PAGED_CODE();
 
@@ -99,18 +98,17 @@
   if(ObIsKernelHandle(Handle, ExGetPreviousMode()))
   {
     HandleTable = ObpKernelHandleTable;
-    ExHandle = HANDLE_TO_EX_HANDLE(ObKernelHandleToHandle(Handle));
+    Handle = ObKernelHandleToHandle(Handle);
   }
   else
   {
     HandleTable = PsGetCurrentProcess()->ObjectTable;
-    ExHandle = HANDLE_TO_EX_HANDLE(Handle);
   }
 
   KeEnterCriticalRegion();
 
   HandleTableEntry = ExMapHandleToPointer(HandleTable,
-                                          ExHandle);
+                                          Handle);
   if (HandleTableEntry == NULL)
     {
       KeLeaveCriticalRegion();
@@ -135,7 +133,6 @@
 {
   PHANDLE_TABLE HandleTable;
   PHANDLE_TABLE_ENTRY HandleTableEntry;
-  LONG ExHandle;
 
   PAGED_CODE();
 
@@ -144,18 +141,17 @@
   if(ObIsKernelHandle(Handle, ExGetPreviousMode()))
   {
     HandleTable = ObpKernelHandleTable;
-    ExHandle = HANDLE_TO_EX_HANDLE(ObKernelHandleToHandle(Handle));
+    Handle = ObKernelHandleToHandle(Handle);
   }
   else
   {
     HandleTable = PsGetCurrentProcess()->ObjectTable;
-    ExHandle = HANDLE_TO_EX_HANDLE(Handle);
   }
 
   KeEnterCriticalRegion();
 
   HandleTableEntry = ExMapHandleToPointer(HandleTable,
-                                          ExHandle);
+                                          Handle);
   if (HandleTableEntry == NULL)
     {
       KeLeaveCriticalRegion();
@@ -190,7 +186,6 @@
    PHANDLE_TABLE_ENTRY HandleEntry;
    PVOID Body;
    POBJECT_HEADER ObjectHeader;
-   LONG ExHandle = HANDLE_TO_EX_HANDLE(Handle);
 
    PAGED_CODE();
 
@@ -199,7 +194,7 @@
    KeEnterCriticalRegion();
 
    HandleEntry = ExMapHandleToPointer(HandleTable,
-                                      ExHandle);
+                                      Handle);
    if(HandleEntry != NULL)
    {
      if(HandleEntry->u1.ObAttributes & EX_HANDLE_ENTRY_PROTECTFROMCLOSE)
@@ -220,7 +215,7 @@
      /* destroy and unlock the handle entry */
      ExDestroyHandleByEntry(HandleTable,
                             HandleEntry,
-                            ExHandle);
+                            Handle);
 
      KeLeaveCriticalRegion();
 
@@ -245,9 +240,8 @@
   HANDLE_TABLE_ENTRY NewHandleEntry;
   PVOID ObjectBody;
   POBJECT_HEADER ObjectHeader;
-  LONG ExTargetHandle;
-  LONG ExSourceHandle;
   ULONG NewHandleCount;
+  HANDLE NewTargetHandle;
 
   PAGED_CODE();
 
@@ -261,12 +255,10 @@
     SourceHandleTable = SourceProcess->ObjectTable;
   }
 
-  ExSourceHandle = HANDLE_TO_EX_HANDLE(SourceHandle);
-
   KeEnterCriticalRegion();
 
   SourceHandleEntry = ExMapHandleToPointer(SourceHandleTable,
-                                           ExSourceHandle);
+                                           SourceHandle);
   if (SourceHandleEntry == NULL)
     {
       KeLeaveCriticalRegion();
@@ -316,9 +308,9 @@
   KeLeaveCriticalRegion();
 
   /* attempt to create the new handle */
-  ExTargetHandle = ExCreateHandle(TargetProcess->ObjectTable,
-                                  &NewHandleEntry);
-  if (ExTargetHandle != EX_INVALID_HANDLE)
+  NewTargetHandle = ExCreateHandle(TargetProcess->ObjectTable,
+                                   &NewHandleEntry);
+  if (NewTargetHandle != NULL)
   {
     if (Options & DUPLICATE_CLOSE_SOURCE)
     {
@@ -328,7 +320,7 @@
 
     ObDereferenceObject(ObjectBody);
 
-    *TargetHandle = EX_HANDLE_TO_HANDLE(ExTargetHandle);
+    *TargetHandle = NewTargetHandle;
 
     return STATUS_SUCCESS;
   }
@@ -607,7 +599,7 @@
 {
    HANDLE_TABLE_ENTRY NewEntry;
    POBJECT_HEADER ObjectHeader;
-   LONG ExHandle;
+   HANDLE Handle;
 
    PAGED_CODE();
 
@@ -639,10 +631,10 @@
      NewEntry.u1.ObAttributes &= ~EX_HANDLE_ENTRY_INHERITABLE;
    NewEntry.u2.GrantedAccess = GrantedAccess;
 
-   ExHandle = ExCreateHandle(Process->ObjectTable,
-                             &NewEntry);
-   DPRINT("ObCreateHandle(0x%x)==0x%x [HT:0x%x]\n", ObjectHeader, *HandleReturn, Process->ObjectTable);
-   if(ExHandle != EX_INVALID_HANDLE)
+   Handle = ExCreateHandle(Process->ObjectTable,
+                           &NewEntry);
+   DPRINT("ObCreateHandle(0x%x)==0x%x [HT:0x%x]\n", ObjectHeader, Handle, Process->ObjectTable);
+   if(Handle != NULL)
    {
      if(InterlockedIncrement(&ObjectHeader->HandleCount) == 1)
      {
@@ -652,7 +644,7 @@
 			         UserMode);
      }
 
-     *HandleReturn = EX_HANDLE_TO_HANDLE(ExHandle);
+     *HandleReturn = Handle;
 
      return STATUS_SUCCESS;
    }
@@ -670,7 +662,6 @@
 {
   PHANDLE_TABLE_ENTRY HandleEntry;
   PEPROCESS Process;
-  LONG ExHandle = HANDLE_TO_EX_HANDLE(Handle);
 
   PAGED_CODE();
 
@@ -681,7 +672,7 @@
   KeEnterCriticalRegion();
 
   HandleEntry = ExMapHandleToPointer(Process->ObjectTable,
-                                     ExHandle);
+                                     Handle);
   if(HandleEntry != NULL)
   {
     *GenerateOnClose = (HandleEntry->u1.ObAttributes & EX_HANDLE_ENTRY_AUDITONCLOSE) != 0;
@@ -729,7 +720,6 @@
    PVOID ObjectBody;
    ACCESS_MASK GrantedAccess;
    ULONG Attributes;
-   LONG ExHandle;
 
    PAGED_CODE();
 
@@ -800,18 +790,17 @@
    if(ObIsKernelHandle(Handle, AccessMode))
    {
       HandleTable = ObpKernelHandleTable;
-      ExHandle = HANDLE_TO_EX_HANDLE(ObKernelHandleToHandle(Handle));
+      Handle = ObKernelHandleToHandle(Handle);
    }
    else
    {
       HandleTable = PsGetCurrentProcess()->ObjectTable;
-      ExHandle = HANDLE_TO_EX_HANDLE(Handle);
    }
 
    KeEnterCriticalRegion();
 
    HandleEntry = ExMapHandleToPointer(HandleTable,
-				      ExHandle);
+				      Handle);
    if (HandleEntry == NULL)
      {
         KeLeaveCriticalRegion();

Modified: trunk/reactos/ntoskrnl/ob/wait.c
--- trunk/reactos/ntoskrnl/ob/wait.c	2005-08-07 23:37:59 UTC (rev 17190)
+++ trunk/reactos/ntoskrnl/ob/wait.c	2005-08-07 23:43:58 UTC (rev 17191)
@@ -38,7 +38,6 @@
     POBJECT_HEADER ObjectHeader;
     PHANDLE_TABLE HandleTable;
     ACCESS_MASK GrantedAccess;
-    LONG ExHandle;
     PVOID DefaultObject;
     NTSTATUS Status = STATUS_SUCCESS;
 
@@ -120,17 +119,16 @@
         {
             /* Use the System Handle Table and decode */
             HandleTable = ObpKernelHandleTable;
-            ExHandle = HANDLE_TO_EX_HANDLE(ObKernelHandleToHandle(Handles[i]));
+            Handles[i] = ObKernelHandleToHandle(Handles[i]);
         }
         else
         {
             /* Use the Process' Handle table and get the Ex Handle */
             HandleTable = PsGetCurrentProcess()->ObjectTable;
-            ExHandle = HANDLE_TO_EX_HANDLE(Handles[i]);
         }
 
         /* Get a pointer to it */
-        if (!(HandleEntry = ExMapHandleToPointer(HandleTable, ExHandle)))
+        if (!(HandleEntry = ExMapHandleToPointer(HandleTable, Handles[i])))
         {
             DPRINT1("Invalid handle\n");
             Status = STATUS_INVALID_HANDLE;

Modified: trunk/reactos/ntoskrnl/ps/process.c
--- trunk/reactos/ntoskrnl/ps/process.c	2005-08-07 23:37:59 UTC (rev 17190)
+++ trunk/reactos/ntoskrnl/ps/process.c	2005-08-07 23:43:58 UTC (rev 17191)
@@ -480,7 +480,7 @@
 
     /* Get the CID Handle Entry */
     if (!(CidEntry = ExMapHandleToPointer(PspCidTable,
-                                          HANDLE_TO_EX_HANDLE(ProcessId))))
+                                          ProcessId)))
     {
         /* Get the Process */
         FoundProcess = CidEntry->u1.Object;
@@ -518,7 +518,7 @@
 
     /* Get the CID Handle Entry */
     if (!(CidEntry = ExMapHandleToPointer(PspCidTable,
-                                          HANDLE_TO_EX_HANDLE(Cid->UniqueThread))))
+                                          Cid->UniqueThread)))
     {
         /* Get the Process */
         FoundThread = CidEntry->u1.Object;

Modified: trunk/reactos/ntoskrnl/ps/thread.c
--- trunk/reactos/ntoskrnl/ps/thread.c	2005-08-07 23:37:59 UTC (rev 17190)
+++ trunk/reactos/ntoskrnl/ps/thread.c	2005-08-07 23:43:58 UTC (rev 17191)
@@ -377,7 +377,7 @@
 
     /* Get the CID Handle Entry */
     if (!(CidEntry = ExMapHandleToPointer(PspCidTable, 
-                                          HANDLE_TO_EX_HANDLE(ThreadId))))
+                                          ThreadId)))
     {
         /* Get the Process */
         FoundThread = CidEntry->u1.Object;
@@ -717,7 +717,6 @@
         if (ClientId->UniqueProcess)
         {
             /* Get the Process */
-            if (ClientId->UniqueProcess == (HANDLE)-1) KEBUGCHECK(0);
             DPRINT("Opening by Process ID: %x\n", ClientId->UniqueProcess);
             Status = PsLookupProcessThreadByCid(ClientId,
                                                 NULL,

Modified: trunk/reactos/ntoskrnl/rtl/libsupp.c
--- trunk/reactos/ntoskrnl/rtl/libsupp.c	2005-08-07 23:37:59 UTC (rev 17190)
+++ trunk/reactos/ntoskrnl/rtl/libsupp.c	2005-08-07 23:43:58 UTC (rev 17191)
@@ -246,33 +246,35 @@
 RtlpFreeAtomHandle(PRTL_ATOM_TABLE AtomTable, PRTL_ATOM_TABLE_ENTRY Entry)
 {
    ExDestroyHandle(AtomTable->ExHandleTable,
-                   (LONG)Entry->HandleIndex);
+                   (HANDLE)((ULONG_PTR)Entry->HandleIndex << 2));
 }
 
 BOOLEAN
 RtlpCreateAtomHandle(PRTL_ATOM_TABLE AtomTable, PRTL_ATOM_TABLE_ENTRY Entry)
 {
    HANDLE_TABLE_ENTRY ExEntry;
-   LONG HandleIndex;
+   HANDLE Handle;
+   USHORT HandleIndex;
    
    ExEntry.u1.Object = Entry;
    ExEntry.u2.GrantedAccess = 0x1; /* FIXME - valid handle */
    
-   HandleIndex = ExCreateHandle(AtomTable->ExHandleTable,
+   Handle = ExCreateHandle(AtomTable->ExHandleTable,
                                 &ExEntry);
-   if (HandleIndex != EX_INVALID_HANDLE)
+   HandleIndex = (USHORT)((ULONG_PTR)Handle >> 2);
+   if (Handle != NULL)
    {
       /* FIXME - Handle Indexes >= 0xC000 ?! */
       if (HandleIndex < 0xC000)
       {
-         Entry->HandleIndex = (USHORT)HandleIndex;
-         Entry->Atom = 0xC000 + (USHORT)HandleIndex;
+         Entry->HandleIndex = HandleIndex;
+         Entry->Atom = 0xC000 + HandleIndex;
          
          return TRUE;
       }
       else
          ExDestroyHandle(AtomTable->ExHandleTable,
-                         HandleIndex);
+                         Handle);
    }
    
    return FALSE;
@@ -284,7 +286,7 @@
    PHANDLE_TABLE_ENTRY ExEntry;
    
    ExEntry = ExMapHandleToPointer(AtomTable->ExHandleTable,
-                                  (LONG)Index);
+                                  (HANDLE)((ULONG_PTR)Index << 2));
    if (ExEntry != NULL)
    {
       PRTL_ATOM_TABLE_ENTRY Entry;