Skip to content

[JExtract/JNI] Add auto arena to SwiftKitCore and add memory management options #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 7, 2025

Conversation

madsodgaard
Copy link
Contributor

@madsodgaard madsodgaard commented Aug 7, 2025

Since java.lang.ref.Cleaner is not available in Android (before API 33), we introduce our own Cleaner which is very similar to the original JDK implementation, but a few simplications for our use case. This allows us to introduce the Auto arena to the JNI mode through SwiftArena.ofAuto().

Note: The SwiftKitFFM still uses the JDK Cleaner, this is only for the JNI/SwiftKitCore library.

In an ideal world we would somehow differentiate between users needing to support Android (use our own) and people just running on regular JVM (then we use the original Cleaner) - perhaps that is something we could look into in the future, by providing an Android "compatibility" library of SwiftKitCore.

On top of that we introduce a new "memory management mode". This mode can be specified using --memory-management-mode and has the following options:

  • explicit (default): Generates only wrappers that require explicit passing of SwifttArena
  • allow-global-automatic: Generates wrappers that use a default global auto arena, but also explicit APIs.

This option is only supported in JNI for now.

Resolves #318

@ktoso
Copy link
Collaborator

ktoso commented Aug 7, 2025

Pretty good, I think that'd be okey -- some comments inline.

I remain unclear about which android versions don't / do have this, so that may be worth double checking before we commit our own cleaner like things but we could do this if we need to.

@madsodgaard madsodgaard changed the title WIP: [JExtract] Add auto arena to SwiftKitCore and add memory management options [JExtract/JNI] Add auto arena to SwiftKitCore and add memory management options Aug 7, 2025
@madsodgaard madsodgaard marked this pull request as ready for review August 7, 2025 16:42
@madsodgaard
Copy link
Contributor Author

@ktoso It was first added in API level 33: https://developer.android.com/reference/java/lang/ref/Cleaner

@ktoso
Copy link
Collaborator

ktoso commented Aug 7, 2025

I see, and we're trying to support 28/29+ I guess... Let's take this then

@ktoso ktoso merged commit f87fe28 into swiftlang:main Aug 7, 2025
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Decide what to do about AutoArena (GC backed) for Android
2 participants