@@ -34,90 +34,59 @@ ExpInitializeHandleTables(VOID)
34
34
PHANDLE_TABLE_ENTRY
35
35
NTAPI
36
36
ExpLookupHandleTableEntry (IN PHANDLE_TABLE HandleTable ,
37
- IN EXHANDLE LookupHandle )
37
+ IN EXHANDLE Handle )
38
38
{
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 ;
44
43
45
- /* Clear the tag bits and check what the next handle is */
44
+ /* Clear the tag bits */
46
45
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
+ }
49
52
50
53
/* Get the table code */
51
- TableBase = * ( volatile ULONG_PTR * ) & HandleTable -> TableCode ;
54
+ TableBase = HandleTable -> TableCode ;
52
55
53
56
/* Extract the table level and actual table base */
54
57
TableLevel = (ULONG )(TableBase & 3 );
55
- TableBase = TableBase - TableLevel ;
58
+ TableBase &= ~3 ;
59
+
60
+ PointerArray = (PVOID * )TableBase ;
61
+ HandleArray = (PHANDLE_TABLE_ENTRY )TableBase ;
56
62
57
63
/* Check what level we're running at */
58
64
switch (TableLevel )
59
65
{
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 */
90
66
case 2 :
91
67
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 ];
95
70
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 :
100
73
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 ] ;
103
76
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 :
107
79
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 ];
111
82
112
- /* Get the handle table entry */
113
- Entry = (PVOID )& Level1 [i *
114
- (sizeof (HANDLE_TABLE_ENTRY ) /
115
- SizeOfHandle (1 ))];
83
+ /* All done */
84
+ break ;
116
85
117
86
default :
118
87
119
- /* All done */
120
- break ;
88
+ NT_ASSERT (FALSE);
89
+ Entry = NULL ;
121
90
}
122
91
123
92
/* Return the handle entry */
@@ -217,7 +186,7 @@ ExpFreeHandleTable(IN PHANDLE_TABLE HandleTable)
217
186
PAGED_CODE ();
218
187
219
188
/* Check which level we're at */
220
- if (! TableLevel )
189
+ if (TableLevel == 0 )
221
190
{
222
191
/* Select the first level table base and just free it */
223
192
Level1 = (PVOID )TableBase ;
@@ -504,7 +473,7 @@ ExpAllocateHandleTableEntrySlow(IN PHANDLE_TABLE HandleTable,
504
473
PAGED_CODE ();
505
474
506
475
/* Check how many levels we already have */
507
- if (! TableLevel )
476
+ if (TableLevel == 0 )
508
477
{
509
478
/* Allocate a mid level, since we only have a low level */
510
479
Mid = ExpAllocateMidLevelTable (HandleTable , DoInit , & Low );
@@ -600,6 +569,11 @@ ExpAllocateHandleTableEntrySlow(IN PHANDLE_TABLE HandleTable,
600
569
ASSERT (Value == NULL );
601
570
}
602
571
}
572
+ else
573
+ {
574
+ /* Something is really broken */
575
+ ASSERT (FALSE);
576
+ }
603
577
604
578
/* Update the index of the next handle */
605
579
Index = InterlockedExchangeAdd ((PLONG ) & HandleTable -> NextHandleNeedingPool ,
0 commit comments