Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b0a10f6

Browse files
Konstantin Baladurinjkotas
authored andcommitted
UMEntryThunk: store freed thunks into FIFO free list
Use free list to delay reusing deleted thunks. It improves collected delegate calls diagnostic.
1 parent 28839fc commit b0a10f6

File tree

2 files changed

+91
-6
lines changed

2 files changed

+91
-6
lines changed

src/vm/dllimportcallback.cpp

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,79 @@ struct UM2MThunk_Args
3333
int argLen;
3434
};
3535

36+
class UMEntryThunkFreeList
37+
{
38+
public:
39+
UMEntryThunkFreeList(size_t threshold) :
40+
m_threshold(threshold),
41+
m_count(0),
42+
m_pHead(NULL),
43+
m_pTail(NULL)
44+
{
45+
WRAPPER_NO_CONTRACT;
46+
47+
m_crst.Init(CrstLeafLock, CRST_UNSAFE_ANYMODE);
48+
}
49+
50+
UMEntryThunk *GetUMEntryThunk()
51+
{
52+
WRAPPER_NO_CONTRACT;
53+
54+
if (m_count < m_threshold)
55+
return NULL;
56+
57+
CrstHolder ch(&m_crst);
58+
59+
UMEntryThunk *pThunk = m_pHead;
60+
61+
if (pThunk == NULL)
62+
return NULL;
63+
64+
m_pHead = m_pHead->m_pNextFreeThunk;
65+
--m_count;
66+
67+
return pThunk;
68+
}
69+
70+
void AddToList(UMEntryThunk *pThunk)
71+
{
72+
CONTRACTL
73+
{
74+
NOTHROW;
75+
}
76+
CONTRACTL_END;
77+
78+
CrstHolder ch(&m_crst);
79+
80+
if (m_pHead == NULL)
81+
{
82+
m_pHead = pThunk;
83+
m_pTail = pThunk;
84+
}
85+
else
86+
{
87+
m_pTail->m_pNextFreeThunk = pThunk;
88+
m_pTail = pThunk;
89+
}
90+
91+
pThunk->m_pNextFreeThunk = NULL;
92+
93+
++m_count;
94+
}
95+
96+
private:
97+
// Used to delay reusing freed thunks
98+
size_t m_threshold;
99+
size_t m_count;
100+
UMEntryThunk *m_pHead;
101+
UMEntryThunk *m_pTail;
102+
CrstStatic m_crst;
103+
};
104+
105+
#define DEFAULT_THUNK_FREE_LIST_THRESHOLD 64
106+
107+
static UMEntryThunkFreeList s_thunkFreeList(DEFAULT_THUNK_FREE_LIST_THRESHOLD);
108+
36109
EXTERN_C void STDCALL UM2MThunk_WrapperHelper(void *pThunkArgs,
37110
int argLen,
38111
void *pAddr,
@@ -1111,20 +1184,26 @@ UMEntryThunk* UMEntryThunk::CreateUMEntryThunk()
11111184

11121185
UMEntryThunk * p;
11131186

1114-
// On the phone, use loader heap to save memory commit of regular executable heap
1115-
p = (UMEntryThunk *)(void *)SystemDomain::GetGlobalLoaderAllocator()->GetExecutableHeap()->AllocMem(S_SIZE_T(sizeof(UMEntryThunk)));
1187+
p = s_thunkFreeList.GetUMEntryThunk();
1188+
1189+
if (p == NULL)
1190+
p = (UMEntryThunk *)(void *)SystemDomain::GetGlobalLoaderAllocator()->GetExecutableHeap()->AllocMem(S_SIZE_T(sizeof(UMEntryThunk)));
11161191

11171192
RETURN p;
11181193
}
11191194

11201195
void UMEntryThunk::Terminate()
11211196
{
1122-
WRAPPER_NO_CONTRACT;
1197+
CONTRACTL
1198+
{
1199+
NOTHROW;
1200+
}
1201+
CONTRACTL_END;
11231202

11241203
_ASSERTE(!SystemDomain::GetGlobalLoaderAllocator()->GetExecutableHeap()->IsZeroInit());
11251204
m_code.Poison();
11261205

1127-
SystemDomain::GetGlobalLoaderAllocator()->GetExecutableHeap()->BackoutMem(this, sizeof(UMEntryThunk));
1206+
s_thunkFreeList.AddToList(this);
11281207
}
11291208

11301209
VOID UMEntryThunk::FreeUMEntryThunk(UMEntryThunk* p)

src/vm/dllimportcallback.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ class UMEntryThunk
250250
{
251251
friend class CheckAsmOffsets;
252252
friend class NDirectStubLinker;
253+
friend class UMEntryThunkFreeList;
253254

254255
private:
255256
#ifdef _DEBUG
@@ -526,8 +527,13 @@ class UMEntryThunk
526527
// Field is NULL for a static method.
527528
OBJECTHANDLE m_pObjectHandle;
528529

529-
// Pointer to the shared structure containing everything else
530-
PTR_UMThunkMarshInfo m_pUMThunkMarshInfo;
530+
union
531+
{
532+
// Pointer to the shared structure containing everything else
533+
PTR_UMThunkMarshInfo m_pUMThunkMarshInfo;
534+
// Pointer to the next UMEntryThunk in the free list. Used when it is freed.
535+
UMEntryThunk *m_pNextFreeThunk;
536+
};
531537

532538
ADID m_dwDomainId; // appdomain of module (cached for fast access)
533539
#ifdef _DEBUG

0 commit comments

Comments
 (0)