Author: tkreuzer
Date: Sun Apr 13 16:45:58 2014
New Revision: 62743

URL: http://svn.reactos.org/svn/reactos?rev=62743&view=rev
Log:
[NTOSKRNL]
Simplify code in ExpLookupHandleTableEntry
CORE-6843 #resolve

Modified:
    trunk/reactos/ntoskrnl/ex/handle.c
    trunk/reactos/ntoskrnl/include/internal/ex.h

Modified: trunk/reactos/ntoskrnl/ex/handle.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/ex/handle.c?rev=62743&r1=62742&r2=62743&view=diff
==============================================================================
--- trunk/reactos/ntoskrnl/ex/handle.c  [iso-8859-1] (original)
+++ trunk/reactos/ntoskrnl/ex/handle.c  [iso-8859-1] Sun Apr 13 16:45:58 2014
@@ -34,90 +34,59 @@
 PHANDLE_TABLE_ENTRY
 NTAPI
 ExpLookupHandleTableEntry(IN PHANDLE_TABLE HandleTable,
-                          IN EXHANDLE LookupHandle)
-{
-    ULONG TableLevel, NextHandle;
-    ULONG_PTR i, j, k, TableBase;
-    PHANDLE_TABLE_ENTRY Entry = NULL;
-    EXHANDLE Handle = LookupHandle;
-    PUCHAR Level1, Level2, Level3;
-
-    /* Clear the tag bits and check what the next handle is */
+                          IN EXHANDLE Handle)
+{
+    ULONG TableLevel;
+    ULONG_PTR TableBase;
+    PHANDLE_TABLE_ENTRY HandleArray, Entry;
+    PVOID *PointerArray;
+
+    /* Clear the tag bits */
     Handle.TagBits = 0;
-    NextHandle = *(volatile ULONG*)&HandleTable->NextHandleNeedingPool;
-    if (Handle.Value >= NextHandle) return NULL;
+
+    /* Check if the handle is in the allocated range */
+    if (Handle.Value >= HandleTable->NextHandleNeedingPool)
+    {
+        return NULL;
+    }
 
     /* Get the table code */
-    TableBase = *(volatile ULONG_PTR*)&HandleTable->TableCode;
+    TableBase = HandleTable->TableCode;
 
     /* Extract the table level and actual table base */
     TableLevel = (ULONG)(TableBase & 3);
-    TableBase = TableBase - TableLevel;
+    TableBase &= ~3;
+
+    PointerArray = (PVOID*)TableBase;
+    HandleArray = (PHANDLE_TABLE_ENTRY)TableBase;
 
     /* Check what level we're running at */
     switch (TableLevel)
     {
-        /* Direct index */
+        case 2:
+
+            /* Get the mid level pointer array */
+            PointerArray = PointerArray[Handle.HighIndex];
+
+            /* Fall through */
+        case 1:
+
+            /* Get the handle array */
+            HandleArray = PointerArray[Handle.MidIndex];
+
+            /* Fall through */
         case 0:
 
-            /* Use level 1 and just get the entry directly */
-            Level1 = (PUCHAR)TableBase;
-            Entry = (PVOID)&Level1[Handle.Value *
-                                   (sizeof(HANDLE_TABLE_ENTRY) /
-                                    SizeOfHandle(1))];
-            break;
-
-        /* Nested index into mid level */
-        case 1:
-
-            /* Get the second table and index into it */
-            Level2 = (PUCHAR)TableBase;
-            i = Handle.Value % SizeOfHandle(LOW_LEVEL_ENTRIES);
-
-            /* Substract this index, and get the next one */
-            Handle.Value -= i;
-            j = Handle.Value /
-                (SizeOfHandle(LOW_LEVEL_ENTRIES) / 
sizeof(PHANDLE_TABLE_ENTRY));
-
-            /* Now get the next table and get the entry from it */
-            Level1 = (PUCHAR)*(PHANDLE_TABLE_ENTRY*)&Level2[j];
-            Entry = (PVOID)&Level1[i *
-                                   (sizeof(HANDLE_TABLE_ENTRY) /
-                                    SizeOfHandle(1))];
-            break;
-
-        /* Nested index into high level */
-        case 2:
-
-            /* Start with the 3rd level table */
-            Level3 = (PUCHAR)TableBase;
-            i = Handle.Value % SizeOfHandle(LOW_LEVEL_ENTRIES);
-
-            /* Subtract this index and get the index for the next lower table 
*/
-            Handle.Value -= i;
-            k = Handle.Value /
-                (SizeOfHandle(LOW_LEVEL_ENTRIES) / 
sizeof(PHANDLE_TABLE_ENTRY));
-
-            /* Get the remaining index in the 2nd level table */
-            j = k % (MID_LEVEL_ENTRIES * sizeof(PHANDLE_TABLE_ENTRY));
-
-            /* Get the remaining index, which is in the third table */
-            k -= j;
-            k /= MID_LEVEL_ENTRIES;
-
-            /* Extract the table level for the handle in each table */
-            Level2 = (PUCHAR)*(PHANDLE_TABLE_ENTRY*)&Level3[k];
-            Level1 = (PUCHAR)*(PHANDLE_TABLE_ENTRY*)&Level2[j];
-
-            /* Get the handle table entry */
-            Entry = (PVOID)&Level1[i *
-                                   (sizeof(HANDLE_TABLE_ENTRY) /
-                                    SizeOfHandle(1))];
-
-        default:
+            /* Get the entry using the low index */
+            Entry = &HandleArray[Handle.LowIndex];
 
             /* All done */
             break;
+
+        default:
+
+            NT_ASSERT(FALSE);
+            Entry = NULL;
     }
 
     /* Return the handle entry */
@@ -217,7 +186,7 @@
     PAGED_CODE();
 
     /* Check which level we're at */
-    if (!TableLevel)
+    if (TableLevel == 0)
     {
         /* Select the first level table base and just free it */
         Level1 = (PVOID)TableBase;
@@ -504,7 +473,7 @@
     PAGED_CODE();
 
     /* Check how many levels we already have */
-    if (!TableLevel)
+    if (TableLevel == 0)
     {
         /* Allocate a mid level, since we only have a low level */
         Mid = ExpAllocateMidLevelTable(HandleTable, DoInit, &Low);
@@ -599,6 +568,11 @@
             Value = InterlockedExchangePointer((PVOID*)&ThirdLevel[i][j], Low);
             ASSERT(Value == NULL);
         }
+    }
+    else
+    {
+        /* Something is really broken */
+        ASSERT(FALSE);
     }
 
     /* Update the index of the next handle */

Modified: trunk/reactos/ntoskrnl/include/internal/ex.h
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/include/internal/ex.h?rev=62743&r1=62742&r2=62743&view=diff
==============================================================================
--- trunk/reactos/ntoskrnl/include/internal/ex.h        [iso-8859-1] (original)
+++ trunk/reactos/ntoskrnl/include/internal/ex.h        [iso-8859-1] Sun Apr 13 
16:45:58 2014
@@ -33,18 +33,32 @@
 extern KSPIN_LOCK ExpNonPagedLookasideListLock;
 extern KSPIN_LOCK ExpPagedLookasideListLock;
 
-typedef struct _EXHANDLE
-{
-    union
-    {
-        struct
-        {
-            ULONG TagBits:2;
-            ULONG Index:30;
-        };
-        HANDLE GenericHandleOverlay;
-        ULONG_PTR Value;
-    };
+#ifdef _WIN64
+#define HANDLE_LOW_BITS (PAGE_SHIFT - 4)
+#define HANDLE_HIGH_BITS (PAGE_SHIFT - 3)
+#else
+#define HANDLE_LOW_BITS (PAGE_SHIFT - 3)
+#define HANDLE_HIGH_BITS (PAGE_SHIFT - 2)
+#endif
+#define KERNEL_FLAG_BITS (sizeof(PVOID)*8 - 31)
+
+typedef union _EXHANDLE
+{
+     struct
+     {
+         ULONG_PTR TagBits:2;
+         ULONG_PTR Index:29;
+     };
+     struct
+     {
+         ULONG_PTR TagBits2:2;
+         ULONG_PTR LowIndex:HANDLE_LOW_BITS;
+         ULONG_PTR MidIndex:HANDLE_HIGH_BITS;
+         ULONG_PTR HighIndex:HANDLE_HIGH_BITS;
+         ULONG_PTR KernelFlag:KERNEL_FLAG_BITS;
+     };
+     HANDLE GenericHandleOverlay;
+     ULONG_PTR Value;
 } EXHANDLE, *PEXHANDLE;
 
 typedef struct _ETIMER


Reply via email to