Skip to content

Commit 1823418

Browse files
committed
[NTOSKRNL]
Simplify code in ExpLookupHandleTableEntry CORE-6843 #resolve svn path=/trunk/; revision=62743
1 parent 4d1dfdb commit 1823418

File tree

2 files changed

+63
-75
lines changed

2 files changed

+63
-75
lines changed

reactos/ntoskrnl/ex/handle.c

Lines changed: 38 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -34,90 +34,59 @@ ExpInitializeHandleTables(VOID)
3434
PHANDLE_TABLE_ENTRY
3535
NTAPI
3636
ExpLookupHandleTableEntry(IN PHANDLE_TABLE HandleTable,
37-
IN EXHANDLE LookupHandle)
37+
IN EXHANDLE Handle)
3838
{
39-
ULONG TableLevel, NextHandle;
40-
ULONG_PTR i, j, k, TableBase;
41-
PHANDLE_TABLE_ENTRY Entry = NULL;
42-
EXHANDLE Handle = LookupHandle;
43-
PUCHAR Level1, Level2, Level3;
39+
ULONG TableLevel;
40+
ULONG_PTR TableBase;
41+
PHANDLE_TABLE_ENTRY HandleArray, Entry;
42+
PVOID *PointerArray;
4443

45-
/* Clear the tag bits and check what the next handle is */
44+
/* Clear the tag bits */
4645
Handle.TagBits = 0;
47-
NextHandle = *(volatile ULONG*)&HandleTable->NextHandleNeedingPool;
48-
if (Handle.Value >= NextHandle) return NULL;
46+
47+
/* Check if the handle is in the allocated range */
48+
if (Handle.Value >= HandleTable->NextHandleNeedingPool)
49+
{
50+
return NULL;
51+
}
4952

5053
/* Get the table code */
51-
TableBase = *(volatile ULONG_PTR*)&HandleTable->TableCode;
54+
TableBase = HandleTable->TableCode;
5255

5356
/* Extract the table level and actual table base */
5457
TableLevel = (ULONG)(TableBase & 3);
55-
TableBase = TableBase - TableLevel;
58+
TableBase &= ~3;
59+
60+
PointerArray = (PVOID*)TableBase;
61+
HandleArray = (PHANDLE_TABLE_ENTRY)TableBase;
5662

5763
/* Check what level we're running at */
5864
switch (TableLevel)
5965
{
60-
/* Direct index */
61-
case 0:
62-
63-
/* Use level 1 and just get the entry directly */
64-
Level1 = (PUCHAR)TableBase;
65-
Entry = (PVOID)&Level1[Handle.Value *
66-
(sizeof(HANDLE_TABLE_ENTRY) /
67-
SizeOfHandle(1))];
68-
break;
69-
70-
/* Nested index into mid level */
71-
case 1:
72-
73-
/* Get the second table and index into it */
74-
Level2 = (PUCHAR)TableBase;
75-
i = Handle.Value % SizeOfHandle(LOW_LEVEL_ENTRIES);
76-
77-
/* Substract this index, and get the next one */
78-
Handle.Value -= i;
79-
j = Handle.Value /
80-
(SizeOfHandle(LOW_LEVEL_ENTRIES) / sizeof(PHANDLE_TABLE_ENTRY));
81-
82-
/* Now get the next table and get the entry from it */
83-
Level1 = (PUCHAR)*(PHANDLE_TABLE_ENTRY*)&Level2[j];
84-
Entry = (PVOID)&Level1[i *
85-
(sizeof(HANDLE_TABLE_ENTRY) /
86-
SizeOfHandle(1))];
87-
break;
88-
89-
/* Nested index into high level */
9066
case 2:
9167

92-
/* Start with the 3rd level table */
93-
Level3 = (PUCHAR)TableBase;
94-
i = Handle.Value % SizeOfHandle(LOW_LEVEL_ENTRIES);
68+
/* Get the mid level pointer array */
69+
PointerArray = PointerArray[Handle.HighIndex];
9570

96-
/* Subtract this index and get the index for the next lower table */
97-
Handle.Value -= i;
98-
k = Handle.Value /
99-
(SizeOfHandle(LOW_LEVEL_ENTRIES) / sizeof(PHANDLE_TABLE_ENTRY));
71+
/* Fall through */
72+
case 1:
10073

101-
/* Get the remaining index in the 2nd level table */
102-
j = k % (MID_LEVEL_ENTRIES * sizeof(PHANDLE_TABLE_ENTRY));
74+
/* Get the handle array */
75+
HandleArray = PointerArray[Handle.MidIndex];
10376

104-
/* Get the remaining index, which is in the third table */
105-
k -= j;
106-
k /= MID_LEVEL_ENTRIES;
77+
/* Fall through */
78+
case 0:
10779

108-
/* Extract the table level for the handle in each table */
109-
Level2 = (PUCHAR)*(PHANDLE_TABLE_ENTRY*)&Level3[k];
110-
Level1 = (PUCHAR)*(PHANDLE_TABLE_ENTRY*)&Level2[j];
80+
/* Get the entry using the low index */
81+
Entry = &HandleArray[Handle.LowIndex];
11182

112-
/* Get the handle table entry */
113-
Entry = (PVOID)&Level1[i *
114-
(sizeof(HANDLE_TABLE_ENTRY) /
115-
SizeOfHandle(1))];
83+
/* All done */
84+
break;
11685

11786
default:
11887

119-
/* All done */
120-
break;
88+
NT_ASSERT(FALSE);
89+
Entry = NULL;
12190
}
12291

12392
/* Return the handle entry */
@@ -217,7 +186,7 @@ ExpFreeHandleTable(IN PHANDLE_TABLE HandleTable)
217186
PAGED_CODE();
218187

219188
/* Check which level we're at */
220-
if (!TableLevel)
189+
if (TableLevel == 0)
221190
{
222191
/* Select the first level table base and just free it */
223192
Level1 = (PVOID)TableBase;
@@ -504,7 +473,7 @@ ExpAllocateHandleTableEntrySlow(IN PHANDLE_TABLE HandleTable,
504473
PAGED_CODE();
505474

506475
/* Check how many levels we already have */
507-
if (!TableLevel)
476+
if (TableLevel == 0)
508477
{
509478
/* Allocate a mid level, since we only have a low level */
510479
Mid = ExpAllocateMidLevelTable(HandleTable, DoInit, &Low);
@@ -600,6 +569,11 @@ ExpAllocateHandleTableEntrySlow(IN PHANDLE_TABLE HandleTable,
600569
ASSERT(Value == NULL);
601570
}
602571
}
572+
else
573+
{
574+
/* Something is really broken */
575+
ASSERT(FALSE);
576+
}
603577

604578
/* Update the index of the next handle */
605579
Index = InterlockedExchangeAdd((PLONG) &HandleTable->NextHandleNeedingPool,

reactos/ntoskrnl/include/internal/ex.h

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,32 @@ extern LIST_ENTRY ExpPagedLookasideListHead;
3333
extern KSPIN_LOCK ExpNonPagedLookasideListLock;
3434
extern KSPIN_LOCK ExpPagedLookasideListLock;
3535

36-
typedef struct _EXHANDLE
36+
#ifdef _WIN64
37+
#define HANDLE_LOW_BITS (PAGE_SHIFT - 4)
38+
#define HANDLE_HIGH_BITS (PAGE_SHIFT - 3)
39+
#else
40+
#define HANDLE_LOW_BITS (PAGE_SHIFT - 3)
41+
#define HANDLE_HIGH_BITS (PAGE_SHIFT - 2)
42+
#endif
43+
#define KERNEL_FLAG_BITS (sizeof(PVOID)*8 - 31)
44+
45+
typedef union _EXHANDLE
3746
{
38-
union
39-
{
40-
struct
41-
{
42-
ULONG TagBits:2;
43-
ULONG Index:30;
44-
};
45-
HANDLE GenericHandleOverlay;
46-
ULONG_PTR Value;
47-
};
47+
struct
48+
{
49+
ULONG_PTR TagBits:2;
50+
ULONG_PTR Index:29;
51+
};
52+
struct
53+
{
54+
ULONG_PTR TagBits2:2;
55+
ULONG_PTR LowIndex:HANDLE_LOW_BITS;
56+
ULONG_PTR MidIndex:HANDLE_HIGH_BITS;
57+
ULONG_PTR HighIndex:HANDLE_HIGH_BITS;
58+
ULONG_PTR KernelFlag:KERNEL_FLAG_BITS;
59+
};
60+
HANDLE GenericHandleOverlay;
61+
ULONG_PTR Value;
4862
} EXHANDLE, *PEXHANDLE;
4963

5064
typedef struct _ETIMER

0 commit comments

Comments
 (0)