Skip to content

Commit 4c27964

Browse files
committed
WIP
1 parent 8c00836 commit 4c27964

File tree

3 files changed

+21
-33
lines changed

3 files changed

+21
-33
lines changed

SwiftKit/src/main/java/org/swift/swiftkit/AutoSwiftMemorySession.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ public void register(SwiftInstance instance) {
6060
cleaner.register(instance, cleanupAction);
6161
}
6262

63+
// visible for testing
64+
void register(SwiftInstance object, SwiftInstanceCleanup cleanupAction) {
65+
Objects.requireNonNull(object, "obj");
66+
Objects.requireNonNull(cleanupAction, "cleanupAction");
67+
68+
cleaner.register(object, cleanupAction);
69+
}
70+
6371
@Override
6472
public MemorySegment allocate(long byteSize, long byteAlignment) {
6573
return arena.allocate(byteSize, byteAlignment);

SwiftKit/src/main/java/org/swift/swiftkit/SwiftInstanceCleanup.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ record SwiftInstanceCleanup(
2727

2828
@Override
2929
public void run() {
30-
System.out.println("[debug] Destroy swift value [" + selfType.getSwiftName() + "]: " + selfPointer);
31-
3230
markAsDestroyed.run();
33-
SwiftValueWitnessTable.destroy(selfType, selfPointer);
31+
32+
// Allow null pointers just for AutoArena tests.
33+
if (selfType != null && selfPointer != null) {
34+
System.out.println("[debug] Destroy swift value [" + selfType.getSwiftName() + "]: " + selfPointer);
35+
SwiftValueWitnessTable.destroy(selfType, selfPointer);
36+
}
3437
}
3538
}

SwiftKit/src/test/java/org/swift/swiftkit/AutoArenaTest.java

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,49 +26,31 @@ public class AutoArenaTest {
2626
@Test
2727
@SuppressWarnings("removal") // System.runFinalization() will be removed
2828
public void cleaner_releases_native_resource() {
29-
SwiftHeapObject object = new FakeSwiftHeapObject();
30-
31-
// Latch waiting for the cleanup of the object
32-
var cleanupLatch = new CountDownLatch(1);
33-
34-
// we're retaining the `object`, register it with the arena:
35-
AutoSwiftMemorySession arena = (AutoSwiftMemorySession) SwiftArena.ofAuto();
29+
SwiftArena arena = SwiftArena.ofAuto();
3630

31+
// This object is registered to the arena.
32+
var object = new FakeSwiftInstance(arena);
3733
var statusDestroyedFlag = object.$statusDestroyedFlag();
38-
Runnable markAsDestroyed = () -> statusDestroyedFlag.set(true);
39-
40-
arena.register(object,
41-
new SwiftHeapObjectCleanup(object.$memorySegment(), object.$swiftType(), markAsDestroyed) {
42-
@Override
43-
public void run() throws UnexpectedRetainCountException {
44-
cleanupLatch.countDown();
45-
}
46-
});
4734

4835
// Release the object and hope it gets GC-ed soon
4936

5037
// noinspection UnusedAssignment
5138
object = null;
5239

5340
var i = 1_000;
54-
while (cleanupLatch.getCount() != 0) {
41+
while (!statusDestroyedFlag.get()) {
5542
System.runFinalization();
5643
System.gc();
5744

5845
if (i-- < 1) {
5946
throw new RuntimeException("Reference was not cleaned up! Did Cleaner not pick up the release?");
6047
}
6148
}
62-
6349
}
6450

65-
private static class FakeSwiftHeapObject implements SwiftHeapObject {
66-
public FakeSwiftHeapObject() {
67-
}
68-
69-
@Override
70-
public MemorySegment $memorySegment() {
71-
return MemorySegment.NULL;
51+
private static class FakeSwiftInstance extends SwiftInstance implements SwiftHeapObject {
52+
public FakeSwiftInstance(SwiftArena arena) {
53+
super(MemorySegment.NULL, arena);
7254
}
7355

7456
@Override
@@ -80,10 +62,5 @@ public FakeSwiftHeapObject() {
8062
public SwiftAnyType $swiftType() {
8163
return null;
8264
}
83-
84-
@Override
85-
public AtomicBoolean $statusDestroyedFlag() {
86-
return new AtomicBoolean();
87-
}
8865
}
8966
}

0 commit comments

Comments
 (0)