Skip to content

Commit d21576c

Browse files
committed
Managed impl
1 parent 0d97ceb commit d21576c

File tree

12 files changed

+43
-31
lines changed

12 files changed

+43
-31
lines changed

src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ internal unsafe struct MethodTable
781781
private const uint enum_flag_HasTypeEquivalence = 0x02000000;
782782
#endif // FEATURE_TYPEEQUIVALENCE
783783
private const uint enum_flag_HasFinalizer = 0x00100000;
784+
private const uint enum_flag_Collectible = 0x00200000;
784785
private const uint enum_flag_Category_Mask = 0x000F0000;
785786
private const uint enum_flag_Category_ValueType = 0x00040000;
786787
private const uint enum_flag_Category_Nullable = 0x00050000;
@@ -841,6 +842,8 @@ internal unsafe struct MethodTable
841842

842843
public bool HasFinalizer => (Flags & enum_flag_HasFinalizer) != 0;
843844

845+
public bool IsCollectible => (Flags & enum_flag_Collectible) != 0;
846+
844847
internal static bool AreSameType(MethodTable* mt1, MethodTable* mt2) => mt1 == mt2;
845848

846849
public bool HasDefaultConstructor => (Flags & (enum_flag_HasComponentSize | enum_flag_HasDefaultCtor)) == enum_flag_HasDefaultCtor;
@@ -978,13 +981,23 @@ internal unsafe struct TypeDesc
978981
private uint _typeAndFlags;
979982
private nint _exposedClassObject;
980983

984+
private const uint enum_flag_IsCollectible = 0x00000100;
985+
981986
public RuntimeType? ExposedClassObject
982987
{
983988
get
984989
{
985990
return *(RuntimeType*)Unsafe.AsPointer(ref _exposedClassObject);
986991
}
987992
}
993+
994+
public bool IsCollectible
995+
{
996+
get
997+
{
998+
return (_typeAndFlags & enum_flag_IsCollectible) != 0;
999+
}
1000+
}
9881001
}
9891002

9901003
[StructLayout(LayoutKind.Sequential)]

src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -824,9 +824,6 @@ internal RuntimeType MakePointer()
824824
return type!;
825825
}
826826

827-
[MethodImpl(MethodImplOptions.InternalCall)]
828-
internal static extern bool IsCollectible(RuntimeType type);
829-
830827
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "RuntimeTypeHandle_GetGenericTypeDefinition")]
831828
internal static partial void GetGenericTypeDefinition(QCallTypeHandle type, ObjectHandleOnStack retType);
832829

src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3227,7 +3227,17 @@ public override MemberInfo GetMemberWithSameMetadataDefinitionAs(MemberInfo memb
32273227

32283228
#region Identity
32293229

3230-
public sealed override bool IsCollectible => RuntimeTypeHandle.IsCollectible(this);
3230+
public sealed override unsafe bool IsCollectible
3231+
{
3232+
get
3233+
{
3234+
TypeHandle th = GetNativeTypeHandle();
3235+
3236+
bool isCollectible = th.IsTypeDesc ? th.AsTypeDesc()->IsCollectible : th.AsMethodTable()->IsCollectible;
3237+
GC.KeepAlive(this);
3238+
return isCollectible;
3239+
}
3240+
}
32313241

32323242
public override MethodBase? DeclaringMethod
32333243
{

src/coreclr/vm/dynamicmethod.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void DynamicMethodTable::Destroy()
129129
// DynamicMethodTable yet (from the DynamicMethodTable::CreateDynamicMethodTable in case
130130
// there were two threads racing to construct the instance for the thread that lost
131131
// the race)
132-
if (m_pMethodTable != NULL && !m_pMethodTable->GetLoaderAllocator()->IsCollectible())
132+
if (m_pMethodTable != NULL && !m_pMethodTable->Collectible())
133133
{
134134
MethodTable::IntroducedMethodIterator it(m_pMethodTable);
135135
_ASSERTE(!it.IsValid());

src/coreclr/vm/ecalllist.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ FCFuncStart(gCOMTypeHandleFuncs)
9494
FCFuncElement("IsGenericVariable", RuntimeTypeHandle::IsGenericVariable)
9595
FCFuncElement("ContainsGenericVariables", RuntimeTypeHandle::ContainsGenericVariables)
9696
FCFuncElement("IsUnmanagedFunctionPointer", RuntimeTypeHandle::IsUnmanagedFunctionPointer)
97-
FCFuncElement("IsCollectible", RuntimeTypeHandle::IsCollectible)
9897
FCFuncElement("CompareCanonicalHandles", RuntimeTypeHandle::CompareCanonicalHandles)
9998
FCFuncElement("InternalAllocNoChecks_FastPath", RuntimeTypeHandle::InternalAllocNoChecks_FastPath)
10099
FCFuncEnd()

src/coreclr/vm/method.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3859,7 +3859,7 @@ MethodDesc *MethodDesc::GetInterfaceMD()
38593859
bool MethodDesc::IsCollectible()
38603860
{
38613861
LIMITED_METHOD_DAC_CONTRACT;
3862-
return HasMethodInstantiation() ? GetLoaderAllocator()->IsCollectible() : GetMethodTable()->Collectible();
3862+
return GetLoaderAllocator()->IsCollectible();
38633863
}
38643864

38653865
PTR_LoaderAllocator MethodDesc::GetLoaderAllocator()

src/coreclr/vm/methodtable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3731,7 +3731,7 @@ void MethodTable::DoRunClassInitThrowing()
37313731
// subsequent attempts to run the .cctor.
37323732
pEntry->AddRef();
37333733
// For collectible types, register the entry for cleanup.
3734-
if (GetLoaderAllocator()->IsCollectible())
3734+
if (Collectible())
37353735
{
37363736
GetLoaderAllocator()->RegisterFailedTypeInitForCleanup(pEntry);
37373737
}

src/coreclr/vm/runtimehandles.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,15 +1092,6 @@ extern "C" void QCALLTYPE RuntimeTypeHandle_MakeByRef(QCall::TypeHandle pTypeHan
10921092
return;
10931093
}
10941094

1095-
FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsCollectible, PTR_ReflectClassBaseObject pTypeUNSAFE)
1096-
{
1097-
FCALL_CONTRACT;
1098-
1099-
REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1100-
FC_RETURN_BOOL(refType->GetType().IsCollectible());
1101-
}
1102-
FCIMPLEND
1103-
11041095
extern "C" void QCALLTYPE RuntimeTypeHandle_Instantiate(QCall::TypeHandle pTypeHandle, TypeHandle * pInstArray, INT32 cInstArray, QCall::ObjectHandleOnStack retType)
11051096
{
11061097
QCALL_CONTRACT;
@@ -1253,7 +1244,7 @@ extern "C" void QCALLTYPE RuntimeTypeHandle_RegisterCollectibleTypeDependency(QC
12531244

12541245
if (pLoaderAllocator->IsCollectible())
12551246
{
1256-
if ((pAssembly == NULL) || !pAssembly->GetLoaderAllocator()->IsCollectible())
1247+
if ((pAssembly == NULL) || !pAssembly->IsCollectible())
12571248
{
12581249
COMPlusThrow(kNotSupportedException, W("NotSupported_CollectibleBoundNonCollectible"));
12591250
}

src/coreclr/vm/runtimehandles.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ class RuntimeTypeHandle
113113

114114
static FCDECL1(FC_BOOL_RET, IsUnmanagedFunctionPointer, ReflectClassBaseObject *pTypeUNSAFE);
115115

116-
static
117-
FCDECL1(FC_BOOL_RET, IsCollectible, PTR_ReflectClassBaseObject pType);
118-
119116
static
120117
FCDECL1(FC_BOOL_RET, IsGenericVariable, PTR_ReflectClassBaseObject pType);
121118

src/coreclr/vm/typedesc.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class TypeDesc
3232
{
3333
public:
3434
#ifndef DACCESS_COMPILE
35-
TypeDesc(CorElementType type) {
35+
TypeDesc(CorElementType type, bool isCollectible) {
3636
LIMITED_METHOD_CONTRACT;
3737

38-
_typeAndFlags = type;
38+
_typeAndFlags = type | (isCollectible ? enum_flag_IsCollectible : 0);
3939
}
4040
#endif
4141

@@ -112,14 +112,19 @@ class TypeDesc
112112
// Is actually ParamTypeDesc (BYREF, PTR)
113113
BOOL HasTypeParam();
114114

115+
bool IsCollectible() const
116+
{
117+
LIMITED_METHOD_CONTRACT;
118+
return (_typeAndFlags & TypeDesc::enum_flag_IsCollectible) != 0;
119+
}
115120

116-
BOOL HasTypeEquivalence() const
121+
bool HasTypeEquivalence() const
117122
{
118123
LIMITED_METHOD_CONTRACT;
119124
return (_typeAndFlags & TypeDesc::enum_flag_HasTypeEquivalence) != 0;
120125
}
121126

122-
BOOL IsFullyLoaded() const
127+
bool IsFullyLoaded() const
123128
{
124129
LIMITED_METHOD_CONTRACT;
125130

@@ -186,7 +191,7 @@ class TypeDesc
186191
// See methodtable.h for details of the flags with the same name there
187192
enum
188193
{
189-
// unused = 0x00000100,
194+
enum_flag_IsCollectible = 0x00000100,
190195
// unused = 0x00000200,
191196
// unused = 0x00000400,
192197
// unused = 0x00000800,
@@ -228,7 +233,7 @@ class ParamTypeDesc : public TypeDesc {
228233
public:
229234
#ifndef DACCESS_COMPILE
230235
ParamTypeDesc(CorElementType type, TypeHandle arg)
231-
: TypeDesc(type), m_Arg(arg) {
236+
: TypeDesc(type, arg.IsCollectible()), m_Arg(arg) {
232237

233238
LIMITED_METHOD_CONTRACT;
234239

@@ -291,7 +296,7 @@ class TypeVarTypeDesc : public TypeDesc
291296
#ifndef DACCESS_COMPILE
292297

293298
TypeVarTypeDesc(PTR_Module pModule, mdToken typeOrMethodDef, unsigned int index, mdGenericParam token) :
294-
TypeDesc(TypeFromToken(typeOrMethodDef) == mdtTypeDef ? ELEMENT_TYPE_VAR : ELEMENT_TYPE_MVAR)
299+
TypeDesc(TypeFromToken(typeOrMethodDef) == mdtTypeDef ? ELEMENT_TYPE_VAR : ELEMENT_TYPE_MVAR, pModule->IsCollectible())
295300
{
296301
CONTRACTL
297302
{
@@ -416,7 +421,7 @@ class FnPtrTypeDesc : public TypeDesc
416421
public:
417422
#ifndef DACCESS_COMPILE
418423
FnPtrTypeDesc(BYTE callConv, DWORD numArgs, TypeHandle * retAndArgTypes, PTR_Module pLoaderModule)
419-
: TypeDesc(ELEMENT_TYPE_FNPTR), m_pLoaderModule(pLoaderModule), m_NumArgs(numArgs), m_CallConv(callConv)
424+
: TypeDesc(ELEMENT_TYPE_FNPTR, pLoaderModule->IsCollectible()), m_pLoaderModule(pLoaderModule), m_NumArgs(numArgs), m_CallConv(callConv)
420425
{
421426
LIMITED_METHOD_CONTRACT;
422427
for (DWORD i = 0; i <= numArgs; i++)

0 commit comments

Comments
 (0)