Skip to content

Commit

Permalink
Thunks: support for fixed size thunk pool (dotnet#8304)
Browse files Browse the repository at this point in the history
* Thunks: support for fixed size thunk pool

Provides the platform independent (OS and CPU architecture) runtime code to support a fixed size thunk pool for systems that doesn't support any way to add more thunks on demand.

Activated with FEATURE_FIXED_POOL_THUNKS..

FEATURE_RX_THUNKS will still override this.

The default is still the template based system.

The code blocks of the pool itself need to be either build and linked as part of the executable or generated by the compiler to be included in the object file.

* fixed size thunk pool (dotnet#23)

* Thunks: support for fixed size thunk pool

Provides the platform independent (OS and CPU architecture) runtime code to support a fixed size thunk pool for systems that doesn't support any way to add more thunks on demand.

Activated with FEATURE_FIXED_POOL_THUNKS..

FEATURE_RX_THUNKS will still override this.

The default is still the template based system.

The code blocks of the pool itself need to be either build and linked as part of the executable or generated by the compiler to be included in the object file.

* Review changes

- Use PalVirtualAlloc instead of GCToOSInterface for memory operations
- Rename global variable to better match the usage
  • Loading branch information
RalfKornmannEnvision authored and jkotas committed Sep 13, 2020
1 parent 86e99b4 commit 03bb19d
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion src/coreclr/src/nativeaot/Runtime/ThunksMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,68 @@ EXTERN_C REDHAWK_API void* __cdecl RhAllocateThunksMapping()
return pThunksSection;
}

#else // FEATURE_RX_THUNKS
// FEATURE_RX_THUNKS
#elif FEATURE_FIXED_POOL_THUNKS

// This thread local variable is used for delegate marshalling
DECLSPEC_THREAD intptr_t tls_thunkData;

// This is used by the thunk code to find the stub data for the called thunk slot
extern "C" uintptr_t g_pThunkStubData;
uintptr_t g_pThunkStubData = NULL;

COOP_PINVOKE_HELPER(int, RhpGetThunkBlockCount, ());
COOP_PINVOKE_HELPER(int, RhpGetNumThunkBlocksPerMapping, ());
COOP_PINVOKE_HELPER(int, RhpGetThunkBlockSize, ());
COOP_PINVOKE_HELPER(void*, RhpGetThunkDataBlockAddress, (void* addr));
COOP_PINVOKE_HELPER(void*, RhpGetThunkStubsBlockAddress, (void* addr));

EXTERN_C REDHAWK_API void* __cdecl RhAllocateThunksMapping()
{
static int nextThunkDataMapping = 0;

int thunkBlocksPerMapping = RhpGetNumThunkBlocksPerMapping();
int thunkBlockSize = RhpGetThunkBlockSize();
int blockCount = RhpGetThunkBlockCount();

ASSERT(blockCount % thunkBlocksPerMapping == 0)

int thunkDataMappingSize = thunkBlocksPerMapping * thunkBlockSize;
int thunkDataMappingCount = blockCount / thunkBlocksPerMapping;

if (nextThunkDataMapping == thunkDataMappingCount)
{
return NULL;
}

if (g_pThunkStubData == NULL)
{
int thunkDataSize = thunkDataMappingSize * thunkDataMappingCount;

g_pThunkStubData = (uintptr_t)PalVirtualAlloc(NULL, thunkDataSize, MEM_RESERVE, PAGE_READWRITE);

if (g_pThunkStubData == NULL)
{
return NULL;
}
}

void* pThunkDataBlock = (int8_t*)g_pThunkStubData + nextThunkDataMapping * thunkDataMappingSize;

if (PalVirtualAlloc(pThunkDataBlock, thunkDataMappingSize, MEM_COMMIT, PAGE_READWRITE) == NULL)
{
return NULL;
}

nextThunkDataMapping++;

void* pThunks = RhpGetThunkStubsBlockAddress(pThunkDataBlock);
ASSERT(RhpGetThunkDataBlockAddress(pThunks) == pThunkDataBlock);

return pThunks;
}

#else // FEATURE_FIXED_POOL_THUNKS

COOP_PINVOKE_HELPER(void*, RhpGetThunksBase, ());
COOP_PINVOKE_HELPER(int, RhpGetNumThunkBlocksPerMapping, ());
Expand Down

0 comments on commit 03bb19d

Please sign in to comment.