Skip to content

Commit f6f23c8

Browse files
Move AddReference to BridgeProcessingJniHelper, rename Trigger to TriggerJavaGC
- Move AddReference method from BridgeProcessing to BridgeProcessingJniHelper - Rename Trigger method to TriggerJavaGC - Update BridgeProcessing.cs to call helper class methods - Remove unused static fields Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
1 parent b82a214 commit f6f23c8

2 files changed

Lines changed: 35 additions & 35 deletions

File tree

src/Mono.Android/Microsoft.Android.Runtime/BridgeProcessing.cs

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public BridgeProcessing (MarkCrossReferencesArgs* args)
5959
public void Process ()
6060
{
6161
PrepareForJavaCollection ();
62-
BridgeProcessingJniHelper.Trigger ();
62+
BridgeProcessingJniHelper.TriggerJavaGC ();
6363
CleanupAfterJavaCollection ();
6464
s_logger?.LogGcSummary (crossRefs);
6565
}
@@ -133,7 +133,7 @@ void AddCircularReferences (ref StronglyConnectedComponent scc)
133133
var prevRef = new JniObjectReference (prev->ControlBlock->Handle, JniObjectReferenceType.Global);
134134
var nextRef = new JniObjectReference (next->ControlBlock->Handle, JniObjectReferenceType.Global);
135135

136-
bool referenceAdded = AddReference (prevRef, nextRef);
136+
bool referenceAdded = BridgeProcessingJniHelper.AddReference (prevRef, nextRef, s_logger);
137137
if (!referenceAdded) {
138138
throw new InvalidOperationException ("Failed to add reference between objects in a strongly connected component");
139139
}
@@ -148,7 +148,7 @@ void AddCrossReference (nuint sourceIndex, nuint destIndex)
148148
var (fromRef, fromContextPtr) = SelectCrossReferenceTarget (sourceIndex);
149149
var (toRef, _) = SelectCrossReferenceTarget (destIndex);
150150

151-
if (AddReference (fromRef, toRef) && fromContextPtr != IntPtr.Zero) {
151+
if (BridgeProcessingJniHelper.AddReference (fromRef, toRef, s_logger) && fromContextPtr != IntPtr.Zero) {
152152
HandleContext* fromContext = (HandleContext*)fromContextPtr;
153153
fromContext->ControlBlock->RefsAdded = 1;
154154
}
@@ -176,37 +176,6 @@ void AddCrossReference (nuint sourceIndex, nuint destIndex)
176176
return (reference, (IntPtr)context);
177177
}
178178

179-
bool AddReference (JniObjectReference from, JniObjectReference to)
180-
{
181-
if (!from.IsValid || !to.IsValid) {
182-
return false;
183-
}
184-
185-
// Try the optimized path for GCUserPeerable (NativeAOT)
186-
if (!RuntimeFeature.IsCoreClrRuntime) {
187-
if (BridgeProcessingJniHelper.TryAddManagedReference (from, to)) {
188-
return true;
189-
}
190-
}
191-
192-
// Fall back to reflection-based approach
193-
var fromClassRef = JniEnvironment.Types.GetObjectClass (from);
194-
using var fromClass = new JniType (ref fromClassRef, JniObjectReferenceOptions.CopyAndDispose);
195-
196-
JniMethodInfo addMethod;
197-
try {
198-
addMethod = fromClass.GetInstanceMethod ("monodroidAddReference", "(Ljava/lang/Object;)V");
199-
} catch (Java.Lang.NoSuchMethodError) {
200-
s_logger?.LogMissingAddReferencesMethod (fromClass);
201-
return false;
202-
}
203-
204-
JniArgumentValue* args = stackalloc JniArgumentValue[1];
205-
args[0] = new JniArgumentValue (to);
206-
JniEnvironment.InstanceMethods.CallVoidMethod (from, addMethod, args);
207-
return true;
208-
}
209-
210179
void TakeWeakGlobalRef (HandleContext* context)
211180
{
212181
Debug.Assert (context != null && context->ControlBlock != null, "Context or control block is null");

src/Mono.Android/Microsoft.Android.Runtime/BridgeProcessingJniHelper.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static BridgeProcessingJniHelper ()
3333
}
3434
}
3535

36-
public static void Trigger ()
36+
public static void TriggerJavaGC ()
3737
{
3838
try {
3939
JniEnvironment.InstanceMethods.CallVoidMethod (s_RuntimeInstance, s_Runtime_gc!, null);
@@ -42,6 +42,37 @@ public static void Trigger ()
4242
}
4343
}
4444

45+
public static bool AddReference (JniObjectReference from, JniObjectReference to, BridgeProcessingLogger? logger)
46+
{
47+
if (!from.IsValid || !to.IsValid) {
48+
return false;
49+
}
50+
51+
// Try the optimized path for GCUserPeerable (NativeAOT)
52+
if (!RuntimeFeature.IsCoreClrRuntime) {
53+
if (TryAddManagedReference (from, to)) {
54+
return true;
55+
}
56+
}
57+
58+
// Fall back to reflection-based approach
59+
var fromClassRef = JniEnvironment.Types.GetObjectClass (from);
60+
using var fromClass = new JniType (ref fromClassRef, JniObjectReferenceOptions.CopyAndDispose);
61+
62+
JniMethodInfo addMethod;
63+
try {
64+
addMethod = fromClass.GetInstanceMethod ("monodroidAddReference", "(Ljava/lang/Object;)V");
65+
} catch (Java.Lang.NoSuchMethodError) {
66+
logger?.LogMissingAddReferencesMethod (fromClass);
67+
return false;
68+
}
69+
70+
JniArgumentValue* args = stackalloc JniArgumentValue[1];
71+
args[0] = new JniArgumentValue (to);
72+
JniEnvironment.InstanceMethods.CallVoidMethod (from, addMethod, args);
73+
return true;
74+
}
75+
4576
public static bool TryAddManagedReference (JniObjectReference from, JniObjectReference to)
4677
{
4778
if (s_GCUserPeerableClass == null || s_GCUserPeerable_jiAddManagedReference == null) {

0 commit comments

Comments
 (0)