Skip to content

Commit 509a2f1

Browse files
noahfalkCopilot
andauthored
Refactor loader heap backout adapter to remove multiple inheritance (#125867)
A more complete example of what I was talking about in #125129 (comment). We can either merge this PR or close it and incorporate similar changes in 125129 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 5daf9dc commit 509a2f1

2 files changed

Lines changed: 70 additions & 11 deletions

File tree

src/coreclr/inc/loaderheap.h

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct TaggedMemAllocPtr
5454
void *m_pMem; //Pointer to AllocMem'd block (needed to pass back to BackoutMem)
5555
size_t m_dwRequestedSize; //Requested allocation size (needed to pass back to BackoutMem)
5656

57-
ILoaderHeapBackout *m_pHeap; //The heap that alloc'd the block (needed to know who to call BackoutMem on)
57+
ILoaderHeapBackout *m_pHeap; //The backout target for the allocation (needed to know who to call BackoutMem on)
5858

5959
//For AllocMem'd blocks, this is always 0.
6060
//For AllocAlignedMem blocks, you have to add m_dwExtra to m_pMem to arrive
@@ -207,7 +207,7 @@ typedef bool EnumPageRegionsCallback (PTR_VOID pvArgs, PTR_VOID pvAllocationBase
207207
// pointer allocation (although not the actual allocation routines).
208208
//===============================================================================
209209
typedef DPTR(class UnlockedLoaderHeapBase) PTR_UnlockedLoaderHeapBase;
210-
class UnlockedLoaderHeapBase : public UnlockedLoaderHeapBaseTraversable, public ILoaderHeapBackout
210+
class UnlockedLoaderHeapBase : public UnlockedLoaderHeapBaseTraversable
211211
{
212212
#ifdef _DEBUG
213213
friend class LoaderHeapSniffer;
@@ -216,9 +216,64 @@ class UnlockedLoaderHeapBase : public UnlockedLoaderHeapBaseTraversable, public
216216
friend class ClrDataAccess;
217217
#endif
218218

219+
private:
220+
class LoaderHeapBackout final : public ILoaderHeapBackout
221+
{
222+
public:
223+
explicit LoaderHeapBackout(UnlockedLoaderHeapBase *pOwner)
224+
: m_pOwner(pOwner)
225+
{
226+
LIMITED_METHOD_CONTRACT;
227+
}
228+
229+
void RealBackoutMem(void *pMem
230+
, size_t dwSize
231+
#ifdef _DEBUG
232+
, _In_ _In_z_ const char *szFile
233+
, int lineNum
234+
, _In_ _In_z_ const char *szAllocFile
235+
, int allocLineNum
236+
#endif
237+
) override
238+
{
239+
WRAPPER_NO_CONTRACT;
240+
m_pOwner->RealBackoutMem(pMem
241+
, dwSize
242+
#ifdef _DEBUG
243+
, szFile
244+
, lineNum
245+
, szAllocFile
246+
, allocLineNum
247+
#endif
248+
);
249+
}
250+
251+
private:
252+
UnlockedLoaderHeapBase *m_pOwner;
253+
};
254+
219255
protected:
220256
size_t GetBytesAvailCommittedRegion();
257+
ILoaderHeapBackout *GetBackoutInterface()
258+
{
259+
LIMITED_METHOD_CONTRACT;
260+
return &m_backout;
261+
}
221262

263+
virtual void RealBackoutMem(void *pMem
264+
, size_t dwSize
265+
#ifdef _DEBUG
266+
, _In_ _In_z_ const char *szFile
267+
, int lineNum
268+
, _In_ _In_z_ const char *szAllocFile
269+
, int allocLineNum
270+
#endif
271+
) = 0;
272+
273+
private:
274+
LoaderHeapBackout m_backout;
275+
276+
protected:
222277
#ifndef DACCESS_COMPILE
223278
const
224279
#endif
@@ -234,7 +289,10 @@ class UnlockedLoaderHeapBase : public UnlockedLoaderHeapBaseTraversable, public
234289

235290
public:
236291
#ifdef DACCESS_COMPILE
237-
UnlockedLoaderHeapBase() {}
292+
UnlockedLoaderHeapBase()
293+
: m_backout(this)
294+
{
295+
}
238296
#else
239297
UnlockedLoaderHeapBase(LoaderHeapImplementationKind kind);
240298
virtual ~UnlockedLoaderHeapBase();
@@ -801,7 +859,7 @@ class LoaderHeap : public UnlockedLoaderHeap
801859
TaggedMemAllocPtr tmap;
802860
tmap.m_pMem = NULL;
803861
tmap.m_dwRequestedSize = dwSize.Value();
804-
tmap.m_pHeap = this;
862+
tmap.m_pHeap = GetBackoutInterface();
805863
tmap.m_dwExtra = 0;
806864
#ifdef _DEBUG
807865
tmap.m_szFile = szFile;
@@ -836,7 +894,7 @@ class LoaderHeap : public UnlockedLoaderHeap
836894
);
837895
tmap.m_pMem = pResult;
838896
tmap.m_dwRequestedSize = dwSize;
839-
tmap.m_pHeap = this;
897+
tmap.m_pHeap = GetBackoutInterface();
840898
tmap.m_dwExtra = 0;
841899
#ifdef _DEBUG
842900
tmap.m_szFile = szFile;
@@ -869,7 +927,7 @@ class LoaderHeap : public UnlockedLoaderHeap
869927

870928
tmap.m_pMem = pResult;
871929
tmap.m_dwRequestedSize = dwSize;
872-
tmap.m_pHeap = this;
930+
tmap.m_pHeap = GetBackoutInterface();
873931
tmap.m_dwExtra = 0;
874932
#ifdef _DEBUG
875933
tmap.m_szFile = szFile;
@@ -918,7 +976,7 @@ class LoaderHeap : public UnlockedLoaderHeap
918976

919977
tmap.m_pMem = (void*)(((BYTE*)pResult) - dwExtra);
920978
tmap.m_dwRequestedSize = dwRequestedSize + dwExtra;
921-
tmap.m_pHeap = this;
979+
tmap.m_pHeap = GetBackoutInterface();
922980
tmap.m_dwExtra = dwExtra;
923981
#ifdef _DEBUG
924982
tmap.m_szFile = szFile;
@@ -959,7 +1017,7 @@ class LoaderHeap : public UnlockedLoaderHeap
9591017

9601018
tmap.m_pMem = (void*)(((BYTE*)pResult) - dwExtra);
9611019
tmap.m_dwRequestedSize = dwRequestedSize + dwExtra;
962-
tmap.m_pHeap = this;
1020+
tmap.m_pHeap = GetBackoutInterface();
9631021
tmap.m_dwExtra = dwExtra;
9641022
#ifdef _DEBUG
9651023
tmap.m_szFile = szFile;
@@ -982,7 +1040,7 @@ class LoaderHeap : public UnlockedLoaderHeap
9821040
, _In_ _In_z_ const char *szAllocFile
9831041
, int allocLineNum
9841042
#endif
985-
)
1043+
) override
9861044
{
9871045
WRAPPER_NO_CONTRACT;
9881046
CRITSEC_Holder csh(m_CriticalSection);
@@ -1093,7 +1151,7 @@ class InterleavedLoaderHeap : public UnlockedInterleavedLoaderHeap
10931151

10941152
tmap.m_pMem = pResult;
10951153
tmap.m_dwRequestedSize = 1;
1096-
tmap.m_pHeap = this;
1154+
tmap.m_pHeap = GetBackoutInterface();
10971155
tmap.m_dwExtra = 0;
10981156
#ifdef _DEBUG
10991157
tmap.m_szFile = szFile;
@@ -1116,7 +1174,7 @@ class InterleavedLoaderHeap : public UnlockedInterleavedLoaderHeap
11161174
, _In_ _In_z_ const char *szAllocFile
11171175
, int allocLineNum
11181176
#endif
1119-
)
1177+
) override
11201178
{
11211179
WRAPPER_NO_CONTRACT;
11221180
CRITSEC_Holder csh(m_CriticalSection);

src/coreclr/utilcode/loaderheap_shared.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ RandomForLoaderHeap s_randomForLoaderHeap;
1515
INDEBUG(DWORD UnlockedLoaderHeapBase::s_dwNumInstancesOfLoaderHeaps = 0;)
1616

1717
UnlockedLoaderHeapBase::UnlockedLoaderHeapBase(LoaderHeapImplementationKind kind) :
18+
m_backout(this),
1819
m_kind(kind),
1920
m_dwTotalAlloc(0),
2021
m_pAllocPtr(NULL),

0 commit comments

Comments
 (0)