@@ -87,43 +87,26 @@ private unsafe ThunksHeap(IntPtr commonStubAddress)
87
87
88
88
_allocatedBlocks = new AllocatedBlock ( ) ;
89
89
90
- IntPtr thunkStubsBlock ;
91
- lock ( this )
92
- {
93
- thunkStubsBlock = ThunkBlocks . GetNewThunksBlock ( ) ;
94
- }
95
-
96
- if ( thunkStubsBlock != IntPtr . Zero )
97
- {
98
- IntPtr thunkDataBlock = RuntimeImports . RhpGetThunkDataBlockAddress ( thunkStubsBlock ) ;
90
+ IntPtr thunkStubsBlock = ThunkBlocks . GetNewThunksBlock ( ) ;
91
+ IntPtr thunkDataBlock = RuntimeImports . RhpGetThunkDataBlockAddress ( thunkStubsBlock ) ;
99
92
100
- // Address of the first thunk data cell should be at the beginning of the thunks data block (page-aligned)
101
- Debug . Assert ( ( ( nuint ) ( nint ) thunkDataBlock % Constants . PageSize ) == 0 ) ;
93
+ // Address of the first thunk data cell should be at the beginning of the thunks data block (page-aligned)
94
+ Debug . Assert ( ( ( nuint ) ( nint ) thunkDataBlock % Constants . PageSize ) == 0 ) ;
102
95
103
- // Update the last pointer value in the thunks data section with the value of the common stub address
104
- * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . PageSize - IntPtr . Size ) ) = commonStubAddress;
105
- Debug. Assert ( * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . PageSize - IntPtr . Size ) ) == commonStubAddress ) ;
96
+ // Update the last pointer value in the thunks data section with the value of the common stub address
97
+ * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . PageSize - IntPtr . Size ) ) = commonStubAddress;
98
+ Debug. Assert ( * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . PageSize - IntPtr . Size ) ) == commonStubAddress ) ;
106
99
107
- // Set the head and end of the linked list
108
- _nextAvailableThunkPtr = thunkDataBlock ;
109
- _lastThunkPtr = _nextAvailableThunkPtr + Constants . ThunkDataSize * ( Constants . NumThunksPerBlock - 1 ) ;
100
+ // Set the head and end of the linked list
101
+ _nextAvailableThunkPtr = thunkDataBlock ;
102
+ _lastThunkPtr = _nextAvailableThunkPtr + Constants . ThunkDataSize * ( Constants . NumThunksPerBlock - 1 ) ;
110
103
111
- _allocatedBlocks. _blockBaseAddress = thunkStubsBlock ;
112
- }
104
+ _allocatedBlocks. _blockBaseAddress = thunkStubsBlock ;
113
105
}
114
106
115
- public static unsafe ThunksHeap ? CreateThunksHeap( IntPtr commonStubAddress )
107
+ public static unsafe ThunksHeap CreateThunksHeap( IntPtr commonStubAddress )
116
108
{
117
- try
118
- {
119
- ThunksHeap newHeap = new ThunksHeap ( commonStubAddress ) ;
120
-
121
- if ( newHeap . _nextAvailableThunkPtr != IntPtr . Zero )
122
- return newHeap;
123
- }
124
- catch ( Exception ) { }
125
-
126
- return null;
109
+ return new ThunksHeap( commonStubAddress ) ;
127
110
}
128
111
129
112
// TODO: Feature
@@ -134,47 +117,30 @@ private unsafe ThunksHeap(IntPtr commonStubAddress)
134
117
//
135
118
// Note: Expected to be called under lock
136
119
//
137
- private unsafe bool ExpandHeap( )
120
+ private unsafe void ExpandHeap( )
138
121
{
139
- AllocatedBlock newBlockInfo;
140
-
141
- try
142
- {
143
- newBlockInfo = new AllocatedBlock ( ) ;
144
- }
145
- catch ( Exception )
146
- {
147
- return false ;
148
- }
122
+ AllocatedBlock newBlockInfo = new AllocatedBlock ( ) ;
149
123
150
124
IntPtr thunkStubsBlock = ThunkBlocks . GetNewThunksBlock ( ) ;
125
+ IntPtr thunkDataBlock = RuntimeImports . RhpGetThunkDataBlockAddress ( thunkStubsBlock ) ;
151
126
152
- if ( thunkStubsBlock ! = IntPtr . Zero )
153
- {
154
- IntPtr thunkDataBlock = RuntimeImports . RhpGetThunkDataBlockAddress ( thunkStubsBlock ) ;
155
-
156
- // Address of the first thunk data cell should be at the beginning of the thunks data block (page-aligned)
157
- Debug . Assert( ( ( nuint ) ( nint ) thunkDataBlock % Constants . PageSize ) == 0 ) ;
158
-
159
- // Update the last pointer value in the thunks data section with the value of the common stub address
160
- * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . PageSize - IntPtr . Size ) ) = _commonStubAddress;
161
- Debug . Assert ( * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . PageSize - IntPtr . Size ) ) == _commonStubAddress) ;
162
-
163
- // Link the last entry in the old list to the first entry in the new list
164
- * ( ( IntPtr * ) _lastThunkPtr) = thunkDataBlock;
127
+ // Address of the first thunk data cell should be at the beginning of the thunks data block (page-aligned)
128
+ Debug . Assert( ( ( nuint ) ( nint ) thunkDataBlock % Constants . PageSize ) == 0 ) ;
165
129
166
- // Update the pointer to the last entry in the list
167
- _lastThunkPtr = * ( ( IntPtr * ) _lastThunkPtr) + Constants . ThunkDataSize * ( Constants . NumThunksPerBlock - 1 ) ;
130
+ // Update the last pointer value in the thunks data section with the value of the common stub address
131
+ * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . PageSize - IntPtr . Size ) ) = _commonStubAddress;
132
+ Debug . Assert ( * ( IntPtr * ) ( thunkDataBlock + ( int ) ( Constants . PageSize - IntPtr . Size ) ) == _commonStubAddress) ;
168
133
169
- newBlockInfo . _blockBaseAddress = thunkStubsBlock ;
170
- newBlockInfo . _nextBlock = _allocatedBlocks ;
134
+ // Link the last entry in the old list to the first entry in the new list
135
+ * ( ( IntPtr * ) _lastThunkPtr ) = thunkDataBlock ;
171
136
172
- _allocatedBlocks = newBlockInfo ;
137
+ // Update the pointer to the last entry in the list
138
+ _lastThunkPtr = * ( ( IntPtr * ) _lastThunkPtr) + Constants . ThunkDataSize * ( Constants . NumThunksPerBlock - 1 ) ;
173
139
174
- return true ;
175
- }
140
+ newBlockInfo . _blockBaseAddress = thunkStubsBlock ;
141
+ newBlockInfo . _nextBlock = _allocatedBlocks ;
176
142
177
- return false ;
143
+ _allocatedBlocks = newBlockInfo ;
178
144
}
179
145
180
146
public unsafe IntPtr AllocateThunk( )
@@ -192,10 +158,7 @@ public unsafe IntPtr AllocateThunk()
192
158
193
159
if ( nextNextAvailableThunkPtr == IntPtr. Zero)
194
160
{
195
- if ( ! ExpandHeap( ) )
196
- {
197
- return IntPtr. Zero;
198
- }
161
+ ExpandHeap( ) ;
199
162
200
163
nextAvailableThunkPtr = _nextAvailableThunkPtr;
201
164
nextNextAvailableThunkPtr = * ( ( IntPtr* ) ( nextAvailableThunkPtr) ) ;
@@ -347,19 +310,12 @@ public static unsafe IntPtr GetNewThunksBlock()
347
310
}
348
311
else
349
312
{
350
- nextThunksBlock = RuntimeImports. RhAllocateThunksMapping( ) ;
351
-
352
- if ( nextThunksBlock == IntPtr. Zero)
353
- {
354
- // We either ran out of memory and can't do anymore mappings of the thunks templates sections,
355
- // or we are using the managed runtime services fallback, which doesn't provide the
356
- // file mapping feature (ex: older version of mrt100.dll, or no mrt100.dll at all).
357
-
358
- // The only option is for the caller to attempt and recycle unused thunks to be able to
359
- // find some free entries.
360
-
361
- return IntPtr. Zero;
362
- }
313
+ nextThunksBlock = IntPtr. Zero;
314
+ int result = RuntimeImports. RhAllocateThunksMapping( & nextThunksBlock) ;
315
+ if ( result == HResults. E_OUTOFMEMORY)
316
+ throw new OutOfMemoryException( ) ;
317
+ else if ( result != HResults. S_OK)
318
+ throw new PlatformNotSupportedException( SR. PlatformNotSupported_DynamicEntrypoint) ;
363
319
364
320
// Each mapping consists of multiple blocks of thunk stubs/data pairs. Keep track of those
365
321
// so that we do not create a new mapping until all blocks in the sections we just mapped are consumed
0 commit comments