Skip to content

Commit abb2d5a

Browse files
committed
Add TLV allocator FCall
1 parent bf23c6d commit abb2d5a

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/CastHelpers.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,9 @@ internal static object BoxImpl(MethodTable* typeMT, ref byte unboxedData)
557557
Debug.Assert(typeMT != null);
558558
Debug.Assert(typeMT->IsValueType);
559559

560-
object boxed = RuntimeTypeHandle.InternalAllocNoChecks(typeMT);
560+
object boxed = RuntimeTypeHandle.GetThreadLocalAllocInternal(typeMT) ??
561+
RuntimeTypeHandle.InternalAllocNoChecks(typeMT);
562+
561563
if (typeMT->ContainsGCPointers)
562564
{
563565
Buffer.BulkMoveWithWriteBarrier(ref boxed.GetRawData(), ref unboxedData,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,9 @@ public ModuleHandle GetModuleHandle()
449449
[MethodImpl(MethodImplOptions.InternalCall)]
450450
internal static extern bool CompareCanonicalHandles(RuntimeType left, RuntimeType right);
451451

452+
[MethodImpl(MethodImplOptions.InternalCall)]
453+
internal static extern object? GetThreadLocalAllocInternal(MethodTable* pMT);
454+
452455
[MethodImpl(MethodImplOptions.InternalCall)]
453456
internal static extern int GetArrayRank(RuntimeType type);
454457

src/coreclr/vm/ecalllist.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ FCFuncStart(gCOMTypeHandleFuncs)
9797
FCFuncElement("ContainsGenericVariables", RuntimeTypeHandle::ContainsGenericVariables)
9898
FCFuncElement("IsUnmanagedFunctionPointer", RuntimeTypeHandle::IsUnmanagedFunctionPointer)
9999
FCFuncElement("CompareCanonicalHandles", RuntimeTypeHandle::CompareCanonicalHandles)
100+
FCFuncElement("GetThreadLocalAllocInternal", RuntimeTypeHandle::GetThreadLocalAllocInternal)
100101
FCFuncEnd()
101102

102103
FCFuncStart(gMetaDataImport)

src/coreclr/vm/runtimehandles.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,35 @@ FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::CompareCanonicalHandles, ReflectClassBas
11431143
}
11441144
FCIMPLEND
11451145

1146+
FCIMPL1(Object*, RuntimeTypeHandle::GetThreadLocalAllocInternal, MethodTable* pMT)
1147+
{
1148+
FCALL_CONTRACT;
1149+
1150+
_ASSERTE(pMT != nullptr);
1151+
_ASSERTE(pMT->IsValueType());
1152+
_ASSERTE(GCHeapUtilities::UseThreadAllocationContexts());
1153+
1154+
SIZE_T size = pMT->GetBaseSize();
1155+
_ASSERTE(size % DATA_ALIGNMENT == 0);
1156+
1157+
ee_alloc_context* allocContext = &t_runtime_thread_locals.alloc_context;
1158+
BYTE* allocPtr = allocContext->m_GCAllocContext.alloc_ptr;
1159+
BYTE* limit = allocContext->getCombinedLimit();
1160+
1161+
if ((SIZE_T)(limit - allocPtr) < size)
1162+
{
1163+
return NULL; // Fall back to slow path in managed
1164+
}
1165+
1166+
allocContext->m_GCAllocContext.alloc_ptr = allocPtr + size;
1167+
Object* obj = reinterpret_cast<Object*>(allocPtr);
1168+
obj->SetMethodTable(pMT);
1169+
_ASSERTE(obj->HasEmptySyncBlockInfo());
1170+
1171+
return obj;
1172+
}
1173+
FCIMPLEND
1174+
11461175
FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsGenericVariable, PTR_ReflectClassBaseObject pTypeUNSAFE)
11471176
{
11481177
FCALL_CONTRACT;

src/coreclr/vm/runtimehandles.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class RuntimeTypeHandle
125125
FCDECL1(FC_BOOL_RET, ContainsGenericVariables, PTR_ReflectClassBaseObject pType);
126126

127127
static FCDECL2(FC_BOOL_RET, CompareCanonicalHandles, PTR_ReflectClassBaseObject pLeft, PTR_ReflectClassBaseObject pRight);
128+
static FCDECL1(Object*, GetThreadLocalAllocInternal, MethodTable* pMT);
128129

129130
static FCDECL1(EnregisteredTypeHandle, GetElementTypeHandle, EnregisteredTypeHandle th);
130131
static FCDECL1(INT32, GetNumVirtuals, ReflectClassBaseObject *pType);

0 commit comments

Comments
 (0)