forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoSwiftMemorySession.java
More file actions
67 lines (58 loc) · 2.69 KB
/
AutoSwiftMemorySession.java
File metadata and controls
67 lines (58 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
package org.swift.swiftkit;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.ref.Cleaner;
import java.util.Objects;
import java.util.concurrent.ThreadFactory;
/**
* A memory session which manages registered objects via the Garbage Collector.
*
* <p> When registered Java wrapper classes around native Swift instances {@link SwiftInstance},
* are eligible for collection, this will trigger the cleanup of the native resources as well.
*
* <p> This memory session is LESS reliable than using a {@link ConfinedSwiftMemorySession} because
* the timing of when the native resources are cleaned up is somewhat undefined, and rely on the
* system GC. Meaning, that if an object nas been promoted to an old generation, there may be a
* long time between the resource no longer being referenced "in Java" and its native memory being released,
* and also the deinit of the Swift type being run.
*
* <p> This can be problematic for Swift applications which rely on quick release of resources, and may expect
* the deinits to run in expected and "quick" succession.
*
* <p> Whenever possible, prefer using an explicitly managed {@link SwiftArena}, such as {@link SwiftArena#ofConfined()}.
*/
final class AutoSwiftMemorySession implements SwiftArena {
private final Arena arena;
private final Cleaner cleaner;
public AutoSwiftMemorySession(ThreadFactory cleanerThreadFactory) {
this.cleaner = Cleaner.create(cleanerThreadFactory);
this.arena = Arena.ofAuto();
}
@Override
public void register(SwiftInstance instance) {
Objects.requireNonNull(instance, "value");
// We're doing this dance to avoid keeping a strong reference to the value itself
var statusDestroyedFlag = instance.$statusDestroyedFlag();
Runnable markAsDestroyed = () -> statusDestroyedFlag.set(true);
MemorySegment resource = instance.$memorySegment();
var cleanupAction = new SwiftInstanceCleanup(resource, instance.$swiftType(), markAsDestroyed);
cleaner.register(instance, cleanupAction);
}
@Override
public MemorySegment allocate(long byteSize, long byteAlignment) {
return arena.allocate(byteSize, byteAlignment);
}
}