Skip to content

Commit 98e840f

Browse files
committed
Improved compatibility with GraalVM
1 parent 99a80e6 commit 98e840f

File tree

9 files changed

+22
-1203
lines changed

9 files changed

+22
-1203
lines changed

java-does-usb/jextract/macos/gen_macos.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,7 @@ $JEXTRACT --output ../../src/main/java \
7979
--target-package net.codecrete.usb.macos.gen.mach \
8080
--include-function mach_error_string \
8181
$SDK_DIR/usr/include/mach/mach.h
82+
83+
sed -i '' -E -f remove_fp_upcall.sed ../../src/main/java/net/codecrete/usb/macos/gen/iokit/IOUSBDeviceStruct187.java
84+
sed -i '' -E -f remove_fp_upcall.sed ../../src/main/java/net/codecrete/usb/macos/gen/iokit/IOUSBInterfaceStruct190.java
85+
sed -i '' -E -f remove_fp_upcall.sed ../../src/main/java/net/codecrete/usb/macos/gen/iokit/IOCFPlugInInterfaceStruct.java
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/The function pointer signature/,/ \}/ {
2+
s/^.*function pointer signature.*$/ *\//p
3+
d
4+
5+
}
6+
/MethodHandle UP\$MH = /,/ \}/d

java-does-usb/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>net.codecrete.usb</groupId>
88
<artifactId>java-does-usb</artifactId>
9-
<version>1.2.0</version>
9+
<version>1.3.0-SNAPSHOT</version>
1010

1111
<properties>
1212
<maven.compiler.source>23</maven.compiler.source>

java-does-usb/src/main/java/net/codecrete/usb/common/EndpointInputStream.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public abstract class EndpointInputStream extends InputStream {
6262
protected EndpointInputStream(UsbDeviceImpl device, int endpointNumber, int bufferSize) {
6363
this.device = device;
6464
this.endpointNumber = endpointNumber;
65-
arena = Arena.ofShared();
65+
//arena = Arena.ofShared(); // not supported by GraalVM
66+
arena = Arena.ofAuto();
6667

6768
var packetSize = device.getEndpoint(UsbDirection.IN, endpointNumber).getPacketSize();
6869

@@ -218,7 +219,7 @@ private void collectOutstandingTransfers() {
218219

219220
completedTransferQueue.clear();
220221
currentTransfer = null;
221-
arena.close();
222+
//arena.close();
222223
}
223224

224225
protected abstract void submitTransferIn(Transfer transfer);

java-does-usb/src/main/java/net/codecrete/usb/common/EndpointOutputStream.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public abstract class EndpointOutputStream extends OutputStream {
6464
protected EndpointOutputStream(UsbDeviceImpl device, int endpointNumber, int bufferSize) {
6565
this.device = device;
6666
this.endpointNumber = endpointNumber;
67-
arena = Arena.ofShared();
67+
//arena = Arena.ofShared(); // not supported by GraalVM
68+
arena = Arena.ofAuto();
6869

6970
packetSize = device.getEndpoint(UsbDirection.OUT, endpointNumber).getPacketSize();
7071

@@ -111,7 +112,7 @@ public void close() throws IOException {
111112
device = null;
112113
availableTransferQueue.clear();
113114
currentTransfer = null;
114-
arena.close();
115+
//arena.close();
115116
}
116117

117118
@Override

java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUsbDeviceRegistry.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,16 @@
1212
import net.codecrete.usb.common.UsbDeviceRegistry;
1313
import net.codecrete.usb.macos.gen.corefoundation.CoreFoundation;
1414
import net.codecrete.usb.macos.gen.iokit.IOKit;
15+
import net.codecrete.usb.macos.gen.iokit.IOServiceAddMatchingNotification$callback;
1516

1617
import java.lang.foreign.Arena;
17-
import java.lang.foreign.FunctionDescriptor;
18-
import java.lang.foreign.Linker;
1918
import java.lang.foreign.MemorySegment;
2019
import java.lang.foreign.SegmentAllocator;
21-
import java.lang.invoke.MethodHandle;
22-
import java.lang.invoke.MethodHandles;
23-
import java.lang.invoke.MethodType;
2420
import java.util.ArrayList;
2521
import java.util.function.Consumer;
2622

2723
import static java.lang.System.Logger.Level.INFO;
2824
import static java.lang.foreign.MemorySegment.NULL;
29-
import static java.lang.foreign.ValueLayout.ADDRESS;
3025
import static java.lang.foreign.ValueLayout.JAVA_INT;
3126
import static java.lang.foreign.ValueLayout.JAVA_LONG;
3227
import static net.codecrete.usb.macos.CoreFoundationHelper.createCFStringRef;
@@ -86,21 +81,17 @@ protected void monitorDevices() {
8681
CoreFoundation.CFRunLoopAddSource(runLoop, runLoopSource, IOKit.kCFRunLoopDefaultMode());
8782

8883
// setup notification for connected devices
89-
var onDeviceConnectedMH = MethodHandles.lookup().findVirtual(MacosUsbDeviceRegistry.class,
90-
"onDevicesConnected", MethodType.methodType(void.class, MemorySegment.class, int.class));
9184
var deviceConnectedIter = setupNotification(arena, notifyPort, IOKit.kIOFirstMatchNotification(),
92-
onDeviceConnectedMH);
85+
this::onDevicesConnected);
9386

9487
// iterate current devices in order to arm the notifications (and build initial device list)
9588
var deviceList = new ArrayList<UsbDevice>();
9689
iterateDevices(deviceConnectedIter, device -> deviceList.add(device)); // NOSONAR
9790
setInitialDeviceList(deviceList);
9891

9992
// setup notification for disconnected devices
100-
var onDeviceDisconnectedMH = MethodHandles.lookup().findVirtual(MacosUsbDeviceRegistry.class,
101-
"onDevicesDisconnected", MethodType.methodType(void.class, MemorySegment.class, int.class));
10293
var deviceDisconnectedIter = setupNotification(arena, notifyPort, IOKit.kIOTerminatedNotification(),
103-
onDeviceDisconnectedMH);
94+
this::onDevicesDisconnected);
10495

10596
// iterate current devices in order to arm the notifications
10697
onDevicesDisconnected(NULL, deviceDisconnectedIter);
@@ -218,14 +209,13 @@ private UsbDevice createDevice(Long entryID, int service, MemorySegment deviceIn
218209
}
219210

220211
private int setupNotification(Arena arena, MemorySegment notifyPort, MemorySegment notificationType,
221-
MethodHandle callback) {
212+
IOServiceAddMatchingNotification$callback.Function callback) {
222213

223214
// new matching dictionary for (dis)connected device notifications (NOSONAR)
224215
var matchingDict = IOKit.IOServiceMatching(IOKit.kIOUSBDeviceClassName());
225216

226217
// create callback stub
227-
var onDeviceCallbackStub = Linker.nativeLinker().upcallStub(callback.bindTo(this),
228-
FunctionDescriptor.ofVoid(ADDRESS, JAVA_INT), Arena.global());
218+
var onDeviceCallbackStub = IOServiceAddMatchingNotification$callback.allocate(callback, Arena.global());
229219

230220
// Set up a notification to be called when a device is first matched / terminated by I/O Kit.
231221
// This method consumes the matchingDict reference.

java-does-usb/src/main/java/net/codecrete/usb/macos/gen/iokit/IOCFPlugInInterfaceStruct.java

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,7 @@ private QueryInterface() {
109109
}
110110

111111
/**
112-
* The function pointer signature, expressed as a functional interface
113112
*/
114-
public interface Function {
115-
int apply(MemorySegment _x0, MemorySegment _x1, MemorySegment _x2);
116-
}
117113

118114
private static final FunctionDescriptor $DESC = FunctionDescriptor.of(
119115
IOKit.C_INT,
@@ -129,15 +125,6 @@ public static FunctionDescriptor descriptor() {
129125
return $DESC;
130126
}
131127

132-
private static final MethodHandle UP$MH = IOKit.upcallHandle(QueryInterface.Function.class, "apply", $DESC);
133-
134-
/**
135-
* Allocates a new upcall stub, whose implementation is defined by {@code fi}.
136-
* The lifetime of the returned segment is managed by {@code arena}
137-
*/
138-
public static MemorySegment allocate(QueryInterface.Function fi, Arena arena) {
139-
return Linker.nativeLinker().upcallStub(UP$MH.bindTo(fi), $DESC, arena);
140-
}
141128

142129
private static final MethodHandle DOWN$MH = Linker.nativeLinker().downcallHandle($DESC);
143130

@@ -211,11 +198,7 @@ private AddRef() {
211198
}
212199

213200
/**
214-
* The function pointer signature, expressed as a functional interface
215201
*/
216-
public interface Function {
217-
int apply(MemorySegment _x0);
218-
}
219202

220203
private static final FunctionDescriptor $DESC = FunctionDescriptor.of(
221204
IOKit.C_INT,
@@ -229,15 +212,6 @@ public static FunctionDescriptor descriptor() {
229212
return $DESC;
230213
}
231214

232-
private static final MethodHandle UP$MH = IOKit.upcallHandle(AddRef.Function.class, "apply", $DESC);
233-
234-
/**
235-
* Allocates a new upcall stub, whose implementation is defined by {@code fi}.
236-
* The lifetime of the returned segment is managed by {@code arena}
237-
*/
238-
public static MemorySegment allocate(AddRef.Function fi, Arena arena) {
239-
return Linker.nativeLinker().upcallStub(UP$MH.bindTo(fi), $DESC, arena);
240-
}
241215

242216
private static final MethodHandle DOWN$MH = Linker.nativeLinker().downcallHandle($DESC);
243217

@@ -311,11 +285,7 @@ private Release() {
311285
}
312286

313287
/**
314-
* The function pointer signature, expressed as a functional interface
315288
*/
316-
public interface Function {
317-
int apply(MemorySegment _x0);
318-
}
319289

320290
private static final FunctionDescriptor $DESC = FunctionDescriptor.of(
321291
IOKit.C_INT,
@@ -329,15 +299,6 @@ public static FunctionDescriptor descriptor() {
329299
return $DESC;
330300
}
331301

332-
private static final MethodHandle UP$MH = IOKit.upcallHandle(Release.Function.class, "apply", $DESC);
333-
334-
/**
335-
* Allocates a new upcall stub, whose implementation is defined by {@code fi}.
336-
* The lifetime of the returned segment is managed by {@code arena}
337-
*/
338-
public static MemorySegment allocate(Release.Function fi, Arena arena) {
339-
return Linker.nativeLinker().upcallStub(UP$MH.bindTo(fi), $DESC, arena);
340-
}
341302

342303
private static final MethodHandle DOWN$MH = Linker.nativeLinker().downcallHandle($DESC);
343304

@@ -499,11 +460,7 @@ private Probe() {
499460
}
500461

501462
/**
502-
* The function pointer signature, expressed as a functional interface
503463
*/
504-
public interface Function {
505-
int apply(MemorySegment _x0, MemorySegment _x1, int _x2, MemorySegment _x3);
506-
}
507464

508465
private static final FunctionDescriptor $DESC = FunctionDescriptor.of(
509466
IOKit.C_INT,
@@ -520,15 +477,6 @@ public static FunctionDescriptor descriptor() {
520477
return $DESC;
521478
}
522479

523-
private static final MethodHandle UP$MH = IOKit.upcallHandle(Probe.Function.class, "apply", $DESC);
524-
525-
/**
526-
* Allocates a new upcall stub, whose implementation is defined by {@code fi}.
527-
* The lifetime of the returned segment is managed by {@code arena}
528-
*/
529-
public static MemorySegment allocate(Probe.Function fi, Arena arena) {
530-
return Linker.nativeLinker().upcallStub(UP$MH.bindTo(fi), $DESC, arena);
531-
}
532480

533481
private static final MethodHandle DOWN$MH = Linker.nativeLinker().downcallHandle($DESC);
534482

@@ -602,11 +550,7 @@ private Start() {
602550
}
603551

604552
/**
605-
* The function pointer signature, expressed as a functional interface
606553
*/
607-
public interface Function {
608-
int apply(MemorySegment _x0, MemorySegment _x1, int _x2);
609-
}
610554

611555
private static final FunctionDescriptor $DESC = FunctionDescriptor.of(
612556
IOKit.C_INT,
@@ -622,15 +566,6 @@ public static FunctionDescriptor descriptor() {
622566
return $DESC;
623567
}
624568

625-
private static final MethodHandle UP$MH = IOKit.upcallHandle(Start.Function.class, "apply", $DESC);
626-
627-
/**
628-
* Allocates a new upcall stub, whose implementation is defined by {@code fi}.
629-
* The lifetime of the returned segment is managed by {@code arena}
630-
*/
631-
public static MemorySegment allocate(Start.Function fi, Arena arena) {
632-
return Linker.nativeLinker().upcallStub(UP$MH.bindTo(fi), $DESC, arena);
633-
}
634569

635570
private static final MethodHandle DOWN$MH = Linker.nativeLinker().downcallHandle($DESC);
636571

@@ -704,11 +639,7 @@ private Stop() {
704639
}
705640

706641
/**
707-
* The function pointer signature, expressed as a functional interface
708642
*/
709-
public interface Function {
710-
int apply(MemorySegment _x0);
711-
}
712643

713644
private static final FunctionDescriptor $DESC = FunctionDescriptor.of(
714645
IOKit.C_INT,
@@ -722,15 +653,6 @@ public static FunctionDescriptor descriptor() {
722653
return $DESC;
723654
}
724655

725-
private static final MethodHandle UP$MH = IOKit.upcallHandle(Stop.Function.class, "apply", $DESC);
726-
727-
/**
728-
* Allocates a new upcall stub, whose implementation is defined by {@code fi}.
729-
* The lifetime of the returned segment is managed by {@code arena}
730-
*/
731-
public static MemorySegment allocate(Stop.Function fi, Arena arena) {
732-
return Linker.nativeLinker().upcallStub(UP$MH.bindTo(fi), $DESC, arena);
733-
}
734656

735657
private static final MethodHandle DOWN$MH = Linker.nativeLinker().downcallHandle($DESC);
736658

0 commit comments

Comments
 (0)