22 questions
2
votes
1
answer
57
views
VarHandle for arrays of dynamic size
How can a VarHandle instance be created to access elements of a dynamically sized array (in the context of the Foreign Function and Memory API)?
package org.example;
import java.lang.foreign.*;
...
1
vote
0
answers
67
views
How to use Java's FFM API to call NSEvent.addGlobalMonitorForEventsMatchingMask on macOS?
I try to use Java's FFM API to call NSEvent.addGlobalMonitorForEventsMatchingMask on macOS.
import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
...
-1
votes
1
answer
58
views
Pass argument of type void** (pointers of pointers) using Java FFM API
How to pass argument void** using java FFM API?
void initHandle(void* handle);
void useHandles(void** handles);
The following did not work for me:
var LINKER = Linker.nativeLinker();
var SYM_LOOKUP = ...
0
votes
1
answer
102
views
JNA's IntByReference in Java Foreign Function & Memory API
I have a native .so library that has the following function:
void foo(void* handle);
It works well in C#:
[DllImport("library.so", CallingConvention = (CallingConvention) 3)]
private static ...
2
votes
1
answer
59
views
How do you create a StructLayout for a native C structure that is recursive using the Java FFM API
I have a MemoryLayout like this:
public class TestStruct {
private static final MemoryLayout layout = MemoryLayout.structLayout(
JAVA_INT.withName("i_one_thing"),
JAVA_INT.withName(...
3
votes
1
answer
58
views
Using Foreign Function and Memory API with bit offsets instead of BitSet
I would like to define a MemoryLayout from the Panama API in JDK 22 with bit offsets. ValueLayouts like ValueLayout.JAVA_BYTE seem to only be usable with a byte granularity, which doesn't fit my use ...
0
votes
1
answer
197
views
Java Foreign Function & Memory API, problems with Locale
Long short story, I've been calling the native Lammps using the latest Foreign Function & Memory API (Project Panama) in JDK 22.
I have problems with the locale.
atof will parse 2.2598258677677969 ...
0
votes
0
answers
79
views
Accessing Java objects across container boundaries
Is it possible to invoke Java instance methods living in a host from a local Docker container through shared memory? If so, how?
Thus, from code running in a JVM J1, I’d like to run a Docker container ...
3
votes
1
answer
82
views
Is there a way to get the logical length of a MemorySegment without doing byte arithmetic?
One of the great benefits of the new FFM API in Java 22 is the MemoryLayout class, which obviates the need for nearly all byte arithmetic. However, there is one remaining place where I can't seem to ...
1
vote
1
answer
117
views
Does VarHandle.compareAndSet throw an UnsupportedOperationException when used with longs on 32-bit architectures?
Per the MemoryLayout Access mode restrictions docs:
On 32-bit platforms, access modes get and set for long, double and MemorySegment are supported but might lead to word tearing, as described in ...
4
votes
1
answer
85
views
What is the difference between `JAVA_LONG` and `JAVA_LONG_UNALIGNED` in `java.lang.foreign.ValueLayout`?
The java.lang.foreign.ValueLayout API that was introduced in Java 22 provides a convenient method for declaring the layout of manually-managed memory so you don't need to do byte arithmetic on reads &...
2
votes
2
answers
326
views
How can I use VarHandle to perform a compare-and-swap with memory allocated by SegmentAllocator or sun.misc.Unsafe?
In our codebase we have an array-like datastructure class that allocates memory with sun.misc.Unsafe so it can accept a long as a size & index parameter. This class also uses the sun.misc.Unsafe ...
5
votes
1
answer
423
views
What is the correct way to obtain a String from a Foreign Function that returns a char pointer
Is there an efficient way to obtain a Java string from a Foreign Function that returns a C-style char pointer?
For example the SQLite library contains a function to return the library version number:
...
4
votes
1
answer
386
views
How can I write an array-like datastructure in Java that takes longs as indices using modern unsafe APIs?
Arrays in Java are limited to Integer.MAX_VALUE for initial capacity & indexable elements (around 2 billion). I would like to write a data structure class that uses a long for this instead.
I know ...
1
vote
2
answers
179
views
Gradle Java include native library (Project Panama)
I've a rust library that I want to call from Java.
I've managed to create the corresponding .h-file and extract from that the Java bindings.
As long as my project structure looks like the following, I ...
2
votes
1
answer
435
views
Java FFM, how to link a java object with a C struct?
I'd like to know how I can "link" a C struct to an object in Java with the FFM library.
I have a game in Java, and I'd ultimately like to allow users to create Luau scripts to interface with ...
2
votes
1
answer
320
views
How to represent C++ class in java to be used via FFI?
I have following sample code in C++
class SomeClass: public ISomeClass
{
private:
int myMember;
public:
SomeClass(int value) : myMember(value) {}
__declspec(dllexport)
int GetCount() ...
2
votes
1
answer
417
views
VarHandle to Java Foreign Memory Segment without bounds checking in Java 21
I'm trying to update from Java 17 to 21, but I have problems with VarHandles from java.lang.foreign. I obtain VarHandle to the memory segment using
VarHandle varhandle = layout.varHandle(path)
But ...
2
votes
1
answer
261
views
OpenJDK 17 - JEP 412: Foreign Function & Memory API (Incubator)
What is wrong with this Java 17 CLinker.getInstance().downcallHandle(CLinker.systemLookup().lookup("radixsort"), ...); with reference to reverse-engineering JEP 412: Foreign Function & ...
1
vote
1
answer
326
views
OpenJDK 20: JEP 434: Foreign Function & Memory API (Second Preview)
How to reinterpret MemorySegment in Java 20 Preview like Java 21 Preview MemorySegment:
MemorySegment reinterpret(long newSize) - Returns a new memory segment that has the same address and scope as ...
4
votes
1
answer
488
views
'IndexOutOfBoundsException: Out of bound access on segment ...' when accessing pointer read from MemorySegment
I am attempting to test the Foreign Function and Memory features of Java 21. Here is my code :
public static void main(String[] args) {
// 1. Find foreign function on the C library path
...
1
vote
3
answers
383
views
Java Foreign API: How to get string from C_POINTER?
I want to use libimobiledevice by Java Foreign API,
This is my code, written by Kotlin:
class Device {
private val arena: Arena = Arena.openConfined()
private val udidAddress: MemorySegment = ...