Skip to content

Commit fe0c9e4

Browse files
committed
Update GC implementation
- Update mscorlib declaration. - Update native.runtime declaration. - Move implementation of GC API. - ExecuteGarbageCollection() now returns used bytes (instead of free memory).
1 parent 103337a commit fe0c9e4

File tree

8 files changed

+89
-48
lines changed

8 files changed

+89
-48
lines changed

src/CLR/CorLib/corlib_native.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,18 @@ static const CLR_RT_MethodHandler method_lookup[] =
549549
nullptr,
550550
nullptr,
551551
nullptr,
552-
Library_corlib_native_System_GC::AnyPendingFinalizers___STATIC__BOOLEAN,
553552
nullptr,
554-
Library_corlib_native_System_GC::SuppressFinalize___STATIC__VOID__OBJECT,
553+
nullptr,
554+
nullptr,
555+
nullptr,
556+
Library_corlib_native_System_GC::EnableGCMessages___STATIC__VOID__BOOLEAN,
557+
nullptr,
555558
Library_corlib_native_System_GC::ReRegisterForFinalize___STATIC__VOID__OBJECT,
559+
nullptr,
560+
Library_corlib_native_System_GC::SuppressFinalize___STATIC__VOID__OBJECT,
561+
nullptr,
562+
Library_corlib_native_System_GC::AnyPendingFinalizers___STATIC__BOOLEAN,
563+
Library_corlib_native_System_GC::Run___STATIC__U4__BOOLEAN,
556564
Library_corlib_native_System_Globalization_DateTimeFormat::FormatDigits___STATIC__STRING__I4__I4,
557565
nullptr,
558566
nullptr,
@@ -794,10 +802,6 @@ static const CLR_RT_MethodHandler method_lookup[] =
794802
nullptr,
795803
nullptr,
796804
nullptr,
797-
nullptr,
798-
nullptr,
799-
nullptr,
800-
nullptr,
801805
Library_corlib_native_System_ValueType::Equals___BOOLEAN__OBJECT,
802806
nullptr,
803807
nullptr,
@@ -1282,10 +1286,18 @@ static const CLR_RT_MethodHandler method_lookup[] =
12821286
nullptr,
12831287
nullptr,
12841288
nullptr,
1285-
Library_corlib_native_System_GC::AnyPendingFinalizers___STATIC__BOOLEAN,
12861289
nullptr,
1287-
Library_corlib_native_System_GC::SuppressFinalize___STATIC__VOID__OBJECT,
1290+
nullptr,
1291+
nullptr,
1292+
nullptr,
1293+
Library_corlib_native_System_GC::EnableGCMessages___STATIC__VOID__BOOLEAN,
1294+
nullptr,
12881295
Library_corlib_native_System_GC::ReRegisterForFinalize___STATIC__VOID__OBJECT,
1296+
nullptr,
1297+
Library_corlib_native_System_GC::SuppressFinalize___STATIC__VOID__OBJECT,
1298+
nullptr,
1299+
Library_corlib_native_System_GC::AnyPendingFinalizers___STATIC__BOOLEAN,
1300+
Library_corlib_native_System_GC::Run___STATIC__U4__BOOLEAN,
12891301
Library_corlib_native_System_Globalization_DateTimeFormat::FormatDigits___STATIC__STRING__I4__I4,
12901302
nullptr,
12911303
nullptr,
@@ -1466,10 +1478,6 @@ static const CLR_RT_MethodHandler method_lookup[] =
14661478
nullptr,
14671479
nullptr,
14681480
nullptr,
1469-
nullptr,
1470-
nullptr,
1471-
nullptr,
1472-
nullptr,
14731481
Library_corlib_native_System_ValueType::Equals___BOOLEAN__OBJECT,
14741482
nullptr,
14751483
nullptr,
@@ -1497,11 +1505,12 @@ const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_mscorlib =
14971505

14981506
#if (NANOCLR_REFLECTION == TRUE)
14991507

1500-
0x9D7386C1,
1508+
0xB069B85F,
15011509

15021510
#elif (NANOCLR_REFLECTION == FALSE)
15031511

1504-
0x98F2AA82,
1512+
0x14A216A1,
1513+
,
15051514

15061515
#else
15071516
#error "NANOCLR_REFLECTION has to be define either TRUE or FALSE. Check the build options."

src/CLR/CorLib/corlib_native.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,11 @@ struct Library_corlib_native_System_EventArgs
594594

595595
struct Library_corlib_native_System_GC
596596
{
597-
NANOCLR_NATIVE_DECLARE(AnyPendingFinalizers___STATIC__BOOLEAN);
598-
NANOCLR_NATIVE_DECLARE(SuppressFinalize___STATIC__VOID__OBJECT);
597+
NANOCLR_NATIVE_DECLARE(EnableGCMessages___STATIC__VOID__BOOLEAN);
599598
NANOCLR_NATIVE_DECLARE(ReRegisterForFinalize___STATIC__VOID__OBJECT);
599+
NANOCLR_NATIVE_DECLARE(SuppressFinalize___STATIC__VOID__OBJECT);
600+
NANOCLR_NATIVE_DECLARE(AnyPendingFinalizers___STATIC__BOOLEAN);
601+
NANOCLR_NATIVE_DECLARE(Run___STATIC__U4__BOOLEAN);
600602

601603
//--//
602604
};
Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,98 @@
1-
//
1+
//
22
// Copyright (c) .NET Foundation and Contributors
33
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
44
// See LICENSE file in the project root for full license information.
55
//
66
#include "CorLib.h"
77

8-
HRESULT Library_corlib_native_System_GC::AnyPendingFinalizers___STATIC__BOOLEAN(CLR_RT_StackFrame &stack)
8+
HRESULT Library_corlib_native_System_GC::EnableGCMessages___STATIC__VOID__BOOLEAN(
9+
CLR_RT_StackFrame &stack)
910
{
1011
NATIVE_PROFILE_CLR_CORE();
12+
1113
NANOCLR_HEADER();
1214

13-
stack.SetResult_Boolean(
14-
g_CLR_RT_ExecutionEngine.m_finalizersPending.IsEmpty() == false ||
15-
(g_CLR_RT_ExecutionEngine.m_finalizerThread != nullptr &&
16-
!g_CLR_RT_ExecutionEngine.m_finalizerThread->CanThreadBeReused()));
15+
#if defined(NANOCLR_TRACE_MEMORY_STATS)
16+
s_CLR_RT_fTrace_MemoryStats = stack.Arg0().NumericByRefConst().u1 != 0 ? c_CLR_RT_Trace_Info : c_CLR_RT_Trace_None;
17+
#else
18+
(void)stack;
19+
#endif
1720

1821
NANOCLR_NOCLEANUP_NOLABEL();
1922
}
2023

21-
HRESULT Library_corlib_native_System_GC::SuppressFinalize___STATIC__VOID__OBJECT(CLR_RT_StackFrame &stack)
24+
HRESULT Library_corlib_native_System_GC::ReRegisterForFinalize___STATIC__VOID__OBJECT(CLR_RT_StackFrame &stack)
2225
{
2326
NATIVE_PROFILE_CLR_CORE();
2427
NANOCLR_HEADER();
2528

29+
CLR_RT_TypeDescriptor desc{};
2630
CLR_RT_HeapBlock *pObj = stack.Arg0().Dereference();
2731
FAULT_ON_NULL(pObj);
2832

2933
CLR_RT_HeapBlock_Finalizer::SuppressFinalize(pObj);
3034

35+
NANOCLR_CHECK_HRESULT(desc.InitializeFromObject(stack.Arg0()));
36+
37+
if (desc.m_handlerCls.HasFinalizer())
38+
{
39+
NANOCLR_CHECK_HRESULT(
40+
CLR_RT_HeapBlock_Finalizer::CreateInstance(stack.Arg0().Dereference(), desc.m_handlerCls));
41+
}
42+
3143
NANOCLR_NOCLEANUP();
3244
}
3345

34-
HRESULT Library_corlib_native_System_GC::ReRegisterForFinalize___STATIC__VOID__OBJECT(CLR_RT_StackFrame &stack)
46+
HRESULT Library_corlib_native_System_GC::SuppressFinalize___STATIC__VOID__OBJECT(CLR_RT_StackFrame &stack)
3547
{
3648
NATIVE_PROFILE_CLR_CORE();
3749
NANOCLR_HEADER();
3850

39-
CLR_RT_TypeDescriptor desc{};
4051
CLR_RT_HeapBlock *pObj = stack.Arg0().Dereference();
4152
FAULT_ON_NULL(pObj);
4253

4354
CLR_RT_HeapBlock_Finalizer::SuppressFinalize(pObj);
4455

45-
NANOCLR_CHECK_HRESULT(desc.InitializeFromObject(stack.Arg0()));
56+
NANOCLR_NOCLEANUP();
57+
}
4658

47-
if (desc.m_handlerCls.HasFinalizer())
59+
HRESULT Library_corlib_native_System_GC::AnyPendingFinalizers___STATIC__BOOLEAN(CLR_RT_StackFrame &stack)
60+
{
61+
NATIVE_PROFILE_CLR_CORE();
62+
NANOCLR_HEADER();
63+
64+
stack.SetResult_Boolean(
65+
g_CLR_RT_ExecutionEngine.m_finalizersPending.IsEmpty() == false ||
66+
(g_CLR_RT_ExecutionEngine.m_finalizerThread != nullptr &&
67+
!g_CLR_RT_ExecutionEngine.m_finalizerThread->CanThreadBeReused()));
68+
69+
NANOCLR_NOCLEANUP_NOLABEL();
70+
}
71+
72+
HRESULT Library_corlib_native_System_GC::Run___STATIC__U4__BOOLEAN(CLR_RT_StackFrame &stack)
73+
{
74+
NATIVE_PROFILE_CLR_CORE();
75+
76+
NANOCLR_HEADER();
77+
78+
#if defined(NANOCLR_GC_VERBOSE)
79+
if (s_CLR_RT_fTrace_GC >= c_CLR_RT_Trace_Info)
4880
{
49-
NANOCLR_CHECK_HRESULT(
50-
CLR_RT_HeapBlock_Finalizer::CreateInstance(stack.Arg0().Dereference(), desc.m_handlerCls));
81+
CLR_Debug::Printf(" Memory: Debug.GC.\r\n");
5182
}
83+
#endif
5284

53-
NANOCLR_NOCLEANUP();
85+
stack.SetResult_I4(g_CLR_RT_ExecutionEngine.PerformGarbageCollection());
86+
87+
if (stack.Arg0().NumericByRefConst().u1)
88+
{
89+
//
90+
// Decrement the number of GC, otherwise the outer loop may request another compaction.
91+
//
92+
g_CLR_RT_GarbageCollector.m_numberOfGarbageCollections--;
93+
94+
g_CLR_RT_ExecutionEngine.PerformHeapCompaction();
95+
}
96+
97+
NANOCLR_NOCLEANUP_NOLABEL();
5498
}

src/CLR/Core/GarbageCollector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ CLR_UINT32 CLR_RT_GarbageCollector::ExecuteGarbageCollection()
294294
g_CLR_PRF_Profiler.RecordGarbageCollectionEnd();
295295
#endif
296296

297-
return m_freeBytes;
297+
return m_totalBytes - m_freeBytes;
298298
}
299299

300300
//--//

src/nanoFramework.Runtime.Native/Runtime.Native.vcxproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<ItemGroup>
2222
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Hardware_SystemInfo.cpp" />
2323
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Native_ExecutionConstraint.cpp" />
24-
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Native_GC.cpp" />
2524
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Native_Power.cpp" />
2625
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Native_Rtc_stubs.cpp" />
2726
<ClCompile Include="nf_rt_native_System_Environment.cpp" />

src/nanoFramework.Runtime.Native/Runtime.Native.vcxproj.filters

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Native_ExecutionConstraint.cpp">
2727
<Filter>Source Files</Filter>
2828
</ClCompile>
29-
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Native_GC.cpp">
30-
<Filter>Source Files</Filter>
31-
</ClCompile>
3229
<ClCompile Include="nf_rt_native_nanoFramework_Runtime_Native_Power.cpp">
3330
<Filter>Source Files</Filter>
3431
</ClCompile>

src/nanoFramework.Runtime.Native/nf_rt_native.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ static const CLR_RT_MethodHandler method_lookup[] =
1212
{
1313
nullptr,
1414
Library_nf_rt_native_nanoFramework_Runtime_Native_ExecutionConstraint::Install___STATIC__VOID__I4__I4,
15-
Library_nf_rt_native_nanoFramework_Runtime_Native_GC::Run___STATIC__U4__BOOLEAN,
16-
Library_nf_rt_native_nanoFramework_Runtime_Native_GC::EnableGCMessages___STATIC__VOID__BOOLEAN,
1715
nullptr,
1816
nullptr,
1917
nullptr,
@@ -42,9 +40,9 @@ static const CLR_RT_MethodHandler method_lookup[] =
4240
const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_nanoFramework_Runtime_Native =
4341
{
4442
"nanoFramework.Runtime.Native",
45-
0x0EAE898B,
43+
0xAD1ECE38,
4644
method_lookup,
47-
{ 100, 0, 10, 0 }
45+
{ 100, 0, 11, 0 }
4846
};
4947

5048
// clang-format on

src/nanoFramework.Runtime.Native/nf_rt_native.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) .NET Foundation and Contributors
33
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
44
// See LICENSE file in the project root for full license information.
@@ -34,14 +34,6 @@ struct Library_nf_rt_native_nanoFramework_Runtime_Native_ExecutionConstraint
3434
//--//
3535
};
3636

37-
struct Library_nf_rt_native_nanoFramework_Runtime_Native_GC
38-
{
39-
NANOCLR_NATIVE_DECLARE(Run___STATIC__U4__BOOLEAN);
40-
NANOCLR_NATIVE_DECLARE(EnableGCMessages___STATIC__VOID__BOOLEAN);
41-
42-
//--//
43-
};
44-
4537
struct Library_nf_rt_native_nanoFramework_Runtime_Native_Power
4638
{
4739
static const int FIELD_STATIC__OnRebootEvent = 0;

0 commit comments

Comments
 (0)