Skip to content

Update GC implementation #3136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CMake/Modules/FindNF_CoreCLR.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ set(NF_CoreCLR_SRCS
# Runtime.Native
nf_rt_native.cpp
nf_rt_native_nanoFramework_Runtime_Hardware_SystemInfo.cpp
nf_rt_native_nanoFramework_Runtime_Native_GC.cpp
nf_rt_native_nanoFramework_Runtime_Native_ExecutionConstraint.cpp
nf_rt_native_nanoFramework_Runtime_Native_Power.cpp
nf_rt_native_nanoFramework_Runtime_Native_Rtc_stubs.cpp
Expand Down
37 changes: 23 additions & 14 deletions src/CLR/CorLib/corlib_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,18 @@ static const CLR_RT_MethodHandler method_lookup[] =
nullptr,
nullptr,
nullptr,
Library_corlib_native_System_GC::AnyPendingFinalizers___STATIC__BOOLEAN,
nullptr,
Library_corlib_native_System_GC::SuppressFinalize___STATIC__VOID__OBJECT,
nullptr,
nullptr,
nullptr,
Library_corlib_native_System_GC::EnableGCMessages___STATIC__VOID__BOOLEAN,
nullptr,
Library_corlib_native_System_GC::ReRegisterForFinalize___STATIC__VOID__OBJECT,
nullptr,
Library_corlib_native_System_GC::SuppressFinalize___STATIC__VOID__OBJECT,
nullptr,
Library_corlib_native_System_GC::AnyPendingFinalizers___STATIC__BOOLEAN,
Library_corlib_native_System_GC::Run___STATIC__U4__BOOLEAN,
Library_corlib_native_System_Globalization_DateTimeFormat::FormatDigits___STATIC__STRING__I4__I4,
nullptr,
nullptr,
Expand Down Expand Up @@ -794,10 +802,6 @@ static const CLR_RT_MethodHandler method_lookup[] =
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
Library_corlib_native_System_ValueType::Equals___BOOLEAN__OBJECT,
nullptr,
nullptr,
Expand Down Expand Up @@ -1282,10 +1286,18 @@ static const CLR_RT_MethodHandler method_lookup[] =
nullptr,
nullptr,
nullptr,
Library_corlib_native_System_GC::AnyPendingFinalizers___STATIC__BOOLEAN,
nullptr,
Library_corlib_native_System_GC::SuppressFinalize___STATIC__VOID__OBJECT,
nullptr,
nullptr,
nullptr,
Library_corlib_native_System_GC::EnableGCMessages___STATIC__VOID__BOOLEAN,
nullptr,
Library_corlib_native_System_GC::ReRegisterForFinalize___STATIC__VOID__OBJECT,
nullptr,
Library_corlib_native_System_GC::SuppressFinalize___STATIC__VOID__OBJECT,
nullptr,
Library_corlib_native_System_GC::AnyPendingFinalizers___STATIC__BOOLEAN,
Library_corlib_native_System_GC::Run___STATIC__U4__BOOLEAN,
Library_corlib_native_System_Globalization_DateTimeFormat::FormatDigits___STATIC__STRING__I4__I4,
nullptr,
nullptr,
Expand Down Expand Up @@ -1466,10 +1478,6 @@ static const CLR_RT_MethodHandler method_lookup[] =
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
Library_corlib_native_System_ValueType::Equals___BOOLEAN__OBJECT,
nullptr,
nullptr,
Expand Down Expand Up @@ -1497,11 +1505,12 @@ const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_mscorlib =

#if (NANOCLR_REFLECTION == TRUE)

0x9D7386C1,
0xB069B85F,

#elif (NANOCLR_REFLECTION == FALSE)

0x98F2AA82,
0x14A216A1,
,

#else
#error "NANOCLR_REFLECTION has to be define either TRUE or FALSE. Check the build options."
Expand Down
6 changes: 4 additions & 2 deletions src/CLR/CorLib/corlib_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,11 @@ struct Library_corlib_native_System_EventArgs

struct Library_corlib_native_System_GC
{
NANOCLR_NATIVE_DECLARE(AnyPendingFinalizers___STATIC__BOOLEAN);
NANOCLR_NATIVE_DECLARE(SuppressFinalize___STATIC__VOID__OBJECT);
NANOCLR_NATIVE_DECLARE(EnableGCMessages___STATIC__VOID__BOOLEAN);
NANOCLR_NATIVE_DECLARE(ReRegisterForFinalize___STATIC__VOID__OBJECT);
NANOCLR_NATIVE_DECLARE(SuppressFinalize___STATIC__VOID__OBJECT);
NANOCLR_NATIVE_DECLARE(AnyPendingFinalizers___STATIC__BOOLEAN);
NANOCLR_NATIVE_DECLARE(Run___STATIC__U4__BOOLEAN);

//--//
};
Expand Down
71 changes: 57 additions & 14 deletions src/CLR/CorLib/corlib_native_System_GC.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,97 @@
//
//
// Copyright (c) .NET Foundation and Contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//
#include "CorLib.h"

HRESULT Library_corlib_native_System_GC::AnyPendingFinalizers___STATIC__BOOLEAN(CLR_RT_StackFrame &stack)
HRESULT Library_corlib_native_System_GC::EnableGCMessages___STATIC__VOID__BOOLEAN(CLR_RT_StackFrame &stack)
{
NATIVE_PROFILE_CLR_CORE();

NANOCLR_HEADER();

stack.SetResult_Boolean(
g_CLR_RT_ExecutionEngine.m_finalizersPending.IsEmpty() == false ||
(g_CLR_RT_ExecutionEngine.m_finalizerThread != nullptr &&
!g_CLR_RT_ExecutionEngine.m_finalizerThread->CanThreadBeReused()));
#if defined(NANOCLR_TRACE_MEMORY_STATS)
s_CLR_RT_fTrace_MemoryStats = stack.Arg0().NumericByRefConst().u1 != 0 ? c_CLR_RT_Trace_Info : c_CLR_RT_Trace_None;
#else
(void)stack;
#endif

NANOCLR_NOCLEANUP_NOLABEL();
}

HRESULT Library_corlib_native_System_GC::SuppressFinalize___STATIC__VOID__OBJECT(CLR_RT_StackFrame &stack)
HRESULT Library_corlib_native_System_GC::ReRegisterForFinalize___STATIC__VOID__OBJECT(CLR_RT_StackFrame &stack)
{
NATIVE_PROFILE_CLR_CORE();
NANOCLR_HEADER();

CLR_RT_TypeDescriptor desc{};
CLR_RT_HeapBlock *pObj = stack.Arg0().Dereference();
FAULT_ON_NULL(pObj);

CLR_RT_HeapBlock_Finalizer::SuppressFinalize(pObj);

NANOCLR_CHECK_HRESULT(desc.InitializeFromObject(stack.Arg0()));

if (desc.m_handlerCls.HasFinalizer())
{
NANOCLR_CHECK_HRESULT(
CLR_RT_HeapBlock_Finalizer::CreateInstance(stack.Arg0().Dereference(), desc.m_handlerCls));
}

NANOCLR_NOCLEANUP();
}

HRESULT Library_corlib_native_System_GC::ReRegisterForFinalize___STATIC__VOID__OBJECT(CLR_RT_StackFrame &stack)
HRESULT Library_corlib_native_System_GC::SuppressFinalize___STATIC__VOID__OBJECT(CLR_RT_StackFrame &stack)
{
NATIVE_PROFILE_CLR_CORE();
NANOCLR_HEADER();

CLR_RT_TypeDescriptor desc{};
CLR_RT_HeapBlock *pObj = stack.Arg0().Dereference();
FAULT_ON_NULL(pObj);

CLR_RT_HeapBlock_Finalizer::SuppressFinalize(pObj);

NANOCLR_CHECK_HRESULT(desc.InitializeFromObject(stack.Arg0()));
NANOCLR_NOCLEANUP();
}

if (desc.m_handlerCls.HasFinalizer())
HRESULT Library_corlib_native_System_GC::AnyPendingFinalizers___STATIC__BOOLEAN(CLR_RT_StackFrame &stack)
{
NATIVE_PROFILE_CLR_CORE();
NANOCLR_HEADER();

stack.SetResult_Boolean(
g_CLR_RT_ExecutionEngine.m_finalizersPending.IsEmpty() == false ||
(g_CLR_RT_ExecutionEngine.m_finalizerThread != nullptr &&
!g_CLR_RT_ExecutionEngine.m_finalizerThread->CanThreadBeReused()));

NANOCLR_NOCLEANUP_NOLABEL();
}

HRESULT Library_corlib_native_System_GC::Run___STATIC__U4__BOOLEAN(CLR_RT_StackFrame &stack)
{
NATIVE_PROFILE_CLR_CORE();

NANOCLR_HEADER();

#if defined(NANOCLR_GC_VERBOSE)
if (s_CLR_RT_fTrace_GC >= c_CLR_RT_Trace_Info)
{
NANOCLR_CHECK_HRESULT(
CLR_RT_HeapBlock_Finalizer::CreateInstance(stack.Arg0().Dereference(), desc.m_handlerCls));
CLR_Debug::Printf(" Memory: Debug.GC.\r\n");
}
#endif

NANOCLR_NOCLEANUP();
stack.SetResult_I4(g_CLR_RT_ExecutionEngine.PerformGarbageCollection());

if (stack.Arg0().NumericByRefConst().u1)
{
//
// Decrement the number of GC, otherwise the outer loop may request another compaction.
//
g_CLR_RT_GarbageCollector.m_numberOfGarbageCollections--;

g_CLR_RT_ExecutionEngine.PerformHeapCompaction();
}

NANOCLR_NOCLEANUP_NOLABEL();
}
2 changes: 1 addition & 1 deletion src/CLR/Core/GarbageCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ CLR_UINT32 CLR_RT_GarbageCollector::ExecuteGarbageCollection()
g_CLR_PRF_Profiler.RecordGarbageCollectionEnd();
#endif

return m_freeBytes;
return m_totalBytes - m_freeBytes;
}

//--//
Expand Down
1 change: 0 additions & 1 deletion src/nanoFramework.Runtime.Native/Runtime.Native.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<ItemGroup>
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Hardware_SystemInfo.cpp" />
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Native_ExecutionConstraint.cpp" />
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Native_GC.cpp" />
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Native_Power.cpp" />
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Native_Rtc_stubs.cpp" />
<ClCompile Include="nf_rt_native_System_Environment.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Native_ExecutionConstraint.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Native_GC.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Native_Power.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
6 changes: 2 additions & 4 deletions src/nanoFramework.Runtime.Native/nf_rt_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ static const CLR_RT_MethodHandler method_lookup[] =
{
nullptr,
Library_nf_rt_native_nanoFramework_Runtime_Native_ExecutionConstraint::Install___STATIC__VOID__I4__I4,
Library_nf_rt_native_nanoFramework_Runtime_Native_GC::Run___STATIC__U4__BOOLEAN,
Library_nf_rt_native_nanoFramework_Runtime_Native_GC::EnableGCMessages___STATIC__VOID__BOOLEAN,
nullptr,
nullptr,
nullptr,
Expand Down Expand Up @@ -42,9 +40,9 @@ static const CLR_RT_MethodHandler method_lookup[] =
const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_nanoFramework_Runtime_Native =
{
"nanoFramework.Runtime.Native",
0x0EAE898B,
0xAD1ECE38,
method_lookup,
{ 100, 0, 10, 0 }
{ 100, 0, 11, 0 }
};

// clang-format on
10 changes: 1 addition & 9 deletions src/nanoFramework.Runtime.Native/nf_rt_native.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright (c) .NET Foundation and Contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
Expand Down Expand Up @@ -34,14 +34,6 @@ struct Library_nf_rt_native_nanoFramework_Runtime_Native_ExecutionConstraint
//--//
};

struct Library_nf_rt_native_nanoFramework_Runtime_Native_GC
{
NANOCLR_NATIVE_DECLARE(Run___STATIC__U4__BOOLEAN);
NANOCLR_NATIVE_DECLARE(EnableGCMessages___STATIC__VOID__BOOLEAN);

//--//
};

struct Library_nf_rt_native_nanoFramework_Runtime_Native_Power
{
static const int FIELD_STATIC__OnRebootEvent = 0;
Expand Down

This file was deleted.

Loading