|
16 | 16 |
|
17 | 17 | import java.lang.foreign.Arena;
|
18 | 18 | import java.lang.foreign.MemorySegment;
|
| 19 | +import java.util.LinkedList; |
| 20 | +import java.util.List; |
19 | 21 | import java.util.concurrent.ConcurrentSkipListSet;
|
20 |
| - |
| 22 | +import java.util.concurrent.atomic.AtomicInteger; |
| 23 | + |
| 24 | +/** |
| 25 | + * A Swift arena manages Swift allocated memory for classes, structs, enums etc. |
| 26 | + * When an arena is closed, it will destroy all managed swift objects in a way appropriate to their type. |
| 27 | + * <p> |
| 28 | + * A confined arena has an associated owner thread that confines some operations to |
| 29 | + * associated owner thread such as {@link #close()}. |
| 30 | + */ |
21 | 31 | public interface SwiftArena extends Arena {
|
22 | 32 |
|
23 |
| - void release(MemorySegment segment); |
| 33 | + static SwiftArena ofConfined() { |
| 34 | + return new ConfinedSwiftMemorySession(Thread.currentThread()); |
| 35 | + } |
24 | 36 |
|
25 |
| - void retain(MemorySegment segment); |
| 37 | + /** |
| 38 | + * Register a struct, enum or other non-reference counted Swift object. |
| 39 | + * Its memory should be considered managed by this arena, and be destroyed when the arena is closed. |
| 40 | + * |
| 41 | + * @param object |
| 42 | + */ |
| 43 | + void register(SwiftHeapObject object); |
26 | 44 |
|
27 |
| - long retainCount(MemorySegment segment); |
| 45 | + void register(SwiftValue value); |
28 | 46 |
|
29 | 47 | }
|
30 | 48 |
|
31 |
| -final class AutoSwiftArena implements SwiftArena { |
32 |
| - Arena underlying; |
| 49 | +final class ConfinedSwiftMemorySession implements SwiftArena { |
| 50 | + |
| 51 | + final Arena underlying; |
| 52 | + final Thread owner; |
| 53 | + final SwiftResourceList resources; |
33 | 54 |
|
34 |
| - ConcurrentSkipListSet<MemorySegment> managedMemorySegments; |
| 55 | + // TODO: just int and volatile updates |
| 56 | + final int CLOSED = 0; |
| 57 | + final int ACTIVE = 1; |
| 58 | + final AtomicInteger state; |
| 59 | + |
| 60 | + public ConfinedSwiftMemorySession(Thread owner) { |
| 61 | + this.owner = owner; |
| 62 | + underlying = Arena.ofConfined(); |
| 63 | + resources = new ConfinedResourceList(); |
| 64 | + state = new AtomicInteger(ACTIVE); |
| 65 | + } |
35 | 66 |
|
36 | 67 | @Override
|
37 | 68 | public MemorySegment allocate(long byteSize, long byteAlignment) {
|
38 |
| - return null; |
| 69 | + return underlying.allocate(byteSize, byteAlignment); |
39 | 70 | }
|
40 | 71 |
|
41 | 72 | @Override
|
42 | 73 | public MemorySegment.Scope scope() {
|
43 |
| - return null; |
| 74 | + return underlying.scope(); |
44 | 75 | }
|
45 | 76 |
|
46 |
| - @Override |
47 |
| - public void close() { |
48 |
| - |
| 77 | + public void checkValid() throws RuntimeException { |
| 78 | + if (this.owner != null && this.owner != Thread.currentThread()) { |
| 79 | + throw new WrongThreadException("ConfinedSwift arena is confined to %s but was closed from %s!".formatted(this.owner, Thread.currentThread())); |
| 80 | + } else if (this.state.get() < ACTIVE) { |
| 81 | + throw new RuntimeException("Arena is already closed!"); |
| 82 | + } |
49 | 83 | }
|
50 | 84 |
|
51 | 85 | @Override
|
52 |
| - public void release(MemorySegment segment) { |
53 |
| - SwiftKit.release(segment); |
| 86 | + public void register(SwiftHeapObject object) { |
| 87 | + this.resources.add(new SwiftHeapObjectCleanup(object.$memorySegment())); |
54 | 88 | }
|
55 | 89 |
|
56 | 90 | @Override
|
57 |
| - public void retain(MemorySegment segment) { |
58 |
| - SwiftKit.retain(segment); |
| 91 | + public void register(SwiftValue value) { |
| 92 | + this.resources.add(new SwiftHeapObjectCleanup(value.$memorySegment())); |
59 | 93 | }
|
60 | 94 |
|
61 | 95 | @Override
|
62 |
| - public long retainCount(MemorySegment segment) { |
63 |
| - return SwiftKit.retainCount(segment); |
| 96 | + public void close() { |
| 97 | + checkValid(); |
| 98 | + |
| 99 | + // Cleanup all resources |
| 100 | + if (this.state.compareAndExchange(ACTIVE, CLOSED) == ACTIVE) { |
| 101 | + this.resources.cleanup(); |
| 102 | + } // else, was already closed; do nothing |
| 103 | + |
| 104 | + |
| 105 | + // Those the underlying arena |
| 106 | + this.underlying.close(); |
| 107 | + |
| 108 | + // After this method returns normally, the scope must be not alive anymore |
| 109 | + assert (!this.scope().isAlive()); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Represents a list of resources that need a cleanup, e.g. allocated classes/structs. |
| 114 | + */ |
| 115 | + static abstract class SwiftResourceList implements Runnable { |
| 116 | + // TODO: Could use intrusive linked list to avoid one indirection here |
| 117 | + final List<SwiftMemoryResourceCleanup> resourceCleanups = new LinkedList<>(); |
| 118 | + |
| 119 | + abstract void add(SwiftMemoryResourceCleanup cleanup); |
| 120 | + |
| 121 | + public abstract void cleanup(); |
| 122 | + |
| 123 | + public final void run() { |
| 124 | + cleanup(); // cleaner interop |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + static final class ConfinedResourceList extends SwiftResourceList { |
| 129 | + @Override |
| 130 | + void add(SwiftMemoryResourceCleanup cleanup) { |
| 131 | + resourceCleanups.add(cleanup); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public void cleanup() { |
| 136 | + for (SwiftMemoryResourceCleanup cleanup : resourceCleanups) { |
| 137 | + cleanup.run(); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +final class UnexpectedRetainCountException extends RuntimeException { |
| 144 | + public UnexpectedRetainCountException(MemorySegment resource, long retainCount, int expectedRetainCount) { |
| 145 | + super(("Attempting to cleanup managed memory segment %s, but it's retain count was different than [%d] (was %d)! " + |
| 146 | + "This would result in destroying a swift object that is still retained by other code somewhere." |
| 147 | + ).formatted(resource, expectedRetainCount, retainCount)); |
64 | 148 | }
|
65 | 149 | }
|
0 commit comments