Skip to content

Commit

Permalink
Linux: fix varhandle for variable length array
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Nov 13, 2024
1 parent a690c72 commit 5f2cd32
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 13 deletions.
13 changes: 1 addition & 12 deletions java-does-usb/src/main/java/net/codecrete/usb/linux/EPoll.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.lang.foreign.Linker;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SequenceLayout;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.VarHandle;

Expand Down Expand Up @@ -51,18 +50,8 @@ private EPoll() {}
JAVA_INT_UNALIGNED.withName("events"),
DATA$LAYOUT.withName("data")).withName("epoll_event");

// Memory layout for an array of epoll_event structs
private static final SequenceLayout EVENT_ARRAY$LAYOUT = MemoryLayout.sequenceLayout(1, EVENT$LAYOUT);

// varhandle to access the "fd" field in an array of epoll_event structs
static final VarHandle EVENT_ARRAY_DATA_FD$VH = EVENT_ARRAY$LAYOUT.varHandle(
MemoryLayout.PathElement.sequenceElement(),
MemoryLayout.PathElement.groupElement("data"),
MemoryLayout.PathElement.groupElement("fd")
);

// varhandle to access the "fd" field in an epoll_event struct
private static final VarHandle EVENT_DATA_FD$VH = EVENT$LAYOUT.varHandle(
static final VarHandle EVENT_DATA_FD$VH = EVENT$LAYOUT.varHandle(
MemoryLayout.PathElement.groupElement("data"),
MemoryLayout.PathElement.groupElement("fd")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ private void asyncCompletionTask() {

// for all ready file descriptors, reap URBs
for (int i = 0; i < res; i++) {
var fd = (int) EPoll.EVENT_ARRAY_DATA_FD$VH.get(events, 0, i);
var event = events.asSlice(i * EPoll.EVENT$LAYOUT.byteSize(), EPoll.EVENT$LAYOUT);
var fd = (int) EPoll.EVENT_DATA_FD$VH.get(event, 0L);
reapURBs(fd, urbPointerHolder, errorState);
}
}
Expand Down

0 comments on commit 5f2cd32

Please sign in to comment.