The FFM API allows the creation of `MemorySegment` instances viewed as buffers. These buffers need to be guarded during use so they are not closed underneath. Also, it is better to use `dst.isDirect()` rather than instanceof testing.
While an asynchronous event is taking place, the buffer needs to remain under guard. It is only when the event completes, the guard can be lifted.
Guard: IOUtil.acquireScope(buf, true);
We could also add a buffer variant in the test:
// returns ByteBuffer with random bytes
static ByteBuffer genBuffer() {
int size = 1024 + rand.nextInt(16000);
byte[] buf = new byte[size];
return switch (rand.nextInt(4)) {
case 0 -> ByteBuffer.allocateDirect(buf.length)
.put(buf)
.flip();
case 1 -> ByteBuffer.wrap(buf);
case 2 -> Arena.ofAuto().allocate(buf.length).asByteBuffer()
.put(buf)
.flip();
case 3 -> Arena.ofShared().allocate(buf.length).asByteBuffer()
.put(buf)
.flip();
default -> throw new InternalError("Should not reach here");
};
}
While an asynchronous event is taking place, the buffer needs to remain under guard. It is only when the event completes, the guard can be lifted.
Guard: IOUtil.acquireScope(buf, true);
We could also add a buffer variant in the test:
// returns ByteBuffer with random bytes
static ByteBuffer genBuffer() {
int size = 1024 + rand.nextInt(16000);
byte[] buf = new byte[size];
return switch (rand.nextInt(4)) {
case 0 -> ByteBuffer.allocateDirect(buf.length)
.put(buf)
.flip();
case 1 -> ByteBuffer.wrap(buf);
case 2 -> Arena.ofAuto().allocate(buf.length).asByteBuffer()
.put(buf)
.flip();
case 3 -> Arena.ofShared().allocate(buf.length).asByteBuffer()
.put(buf)
.flip();
default -> throw new InternalError("Should not reach here");
};
}
- relates to
-
JDK-8357268 Use JavaNioAccess.getBufferAddress rather than DirectBuffer.address()
-
- Resolved
-
- links to
-
Review(master) openjdk/jdk/25531