Skip to content

Commit 7c96b3b

Browse files
committed
keep long returning memory address func in base SwiftInstance
1 parent fc33130 commit 7c96b3b

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

SwiftKitCore/src/main/java/org/swift/swiftkit/core/JNISwiftInstance.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ protected JNISwiftInstance(long pointer, SwiftArena arena) {
3838
this.selfPointer = pointer;
3939
}
4040

41+
@Override
42+
public long $memoryAddress() {
43+
return selfPointer;
44+
}
45+
4146
/**
4247
* Creates a function that will be called when the value should be destroyed.
4348
* This will be code-generated to call a native method to do deinitialization and deallocation.

SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftInstance.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@
1818

1919
public abstract class SwiftInstance {
2020

21+
/**
22+
* The designated constructor of any imported Swift types.
23+
*
24+
* @param arena the arena this object belongs to. When the arena goes out of scope, this value is destroyed.
25+
*/
26+
protected SwiftInstance(SwiftArena arena) {
27+
arena.register(this);
28+
}
29+
30+
/**
31+
* Pointer to the {@code self} of the underlying Swift object or value.
32+
*
33+
* @apiNote When using this pointer one must ensure that the underlying object
34+
* is kept alive using some means (e.g. a class remains retained), as
35+
* this function does not ensure safety of the address in any way.
36+
*/
37+
public abstract long $memoryAddress();
38+
2139
/**
2240
* Called when the arena has decided the value should be destroyed.
2341
* <p/>
@@ -34,22 +52,12 @@ public abstract class SwiftInstance {
3452
* <p/>
3553
* This is exposing the object, rather than performing the action because we don't want to accidentally
3654
* form a strong reference to the {@code SwiftInstance} which could prevent the cleanup from running,
37-
* if using an GC managed instance (e.g. using an {@link AutoSwiftMemorySession}.
55+
* if using an GC managed instance (e.g. using an {@code AutoSwiftMemorySession}.
3856
*/
3957
public final AtomicBoolean $statusDestroyedFlag() {
4058
return this.$state$destroyed;
4159
}
4260

43-
/**
44-
* The designated constructor of any imported Swift types.
45-
*
46-
* @param pointer a pointer to the memory containing the value
47-
* @param arena the arena this object belongs to. When the arena goes out of scope, this value is destroyed.
48-
*/
49-
protected SwiftInstance(SwiftArena arena) {
50-
arena.register(this);
51-
}
52-
5361
/**
5462
* Ensures that this instance has not been destroyed.
5563
* <p/>

SwiftKitFFM/src/main/java/org/swift/swiftkit/ffm/FFMSwiftInstance.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,34 @@
2222
public abstract class FFMSwiftInstance extends SwiftInstance {
2323
private final MemorySegment memorySegment;
2424

25+
/**
26+
* The designated constructor of any imported Swift types.
27+
*
28+
* @param segment the memory segment.
29+
* @param arena the arena this object belongs to. When the arena goes out of scope, this value is destroyed.
30+
*/
31+
protected FFMSwiftInstance(MemorySegment segment, AllocatingSwiftArena arena) {
32+
super(arena);
33+
this.memorySegment = segment;
34+
}
35+
2536
/**
2637
* The pointer to the instance in memory. I.e. the {@code self} of the Swift object or value.
2738
*/
2839
public final MemorySegment $memorySegment() {
2940
return this.memorySegment;
3041
}
3142

43+
@Override
44+
public long $memoryAddress() {
45+
return $memorySegment().address();
46+
}
47+
3248
/**
3349
* The Swift type metadata of this type.
3450
*/
3551
public abstract SwiftAnyType $swiftType();
3652

37-
/**
38-
* The designated constructor of any imported Swift types.
39-
*
40-
* @param segment the memory segment.
41-
* @param arena the arena this object belongs to. When the arena goes out of scope, this value is destroyed.
42-
*/
43-
protected FFMSwiftInstance(MemorySegment segment, AllocatingSwiftArena arena) {
44-
super(arena);
45-
this.memorySegment = segment;
46-
}
4753

4854
@Override
4955
public SwiftInstanceCleanup createCleanupAction() {

0 commit comments

Comments
 (0)