RFR: 8357268: Use JavaNioAccess.getBufferAddress rather than DirectBuffer.address()

Per Minborg pminborg at openjdk.org
Tue May 20 12:06:35 UTC 2025


On Tue, 20 May 2025 10:51:13 GMT, Per Minborg <pminborg at openjdk.org> wrote:

> This PR proposes to use  `JavaNioAccess::getBufferAdress` rather than `DirectBuffer::address` so that `Buffer` instances backed by MemorySegment instances can be used in classes that were not covered by https://github.com/openjdk/jdk/pull/25321
> 
> In some of the cases, this is not strictly needed as the internal cache in `sun.nio.ch.Util#getTemporaryDirectBuffer` is (currently) only returning Buffers that are not backed by a `MemorySegment`. Also, we now always acquire/release buffer sessions before interacting with memory. Again, this is not always needed for temporary direct buffers but provides a consistent handling of buffers at a minimal cost.
> 
> This PR passes tier1, tier2, and tier3 tests on multiple platforms and configurations.

src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java line 254:

> 252:         ByteBuffer dst = Util.getTemporaryDirectBuffer(len);
> 253:         assert dst.position() == 0;
> 254:         NIO_ACCESS.acquireSession(dst);

Not strictly needed, but is proposed for consistency.

src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousFileChannelImpl.java line 459:

> 457: 
> 458:             boolean pending = false;
> 459:             NIO_ACCESS.acquireSession(buf);

Here, we acquire the session *after* we have obtained the address. This is safe as we do not touch the segment before it is acquired. If a segment is deallocated before we try to acquire the session, an exception will be thrown.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/25324#discussion_r2097665536
PR Review Comment: https://git.openjdk.org/jdk/pull/25324#discussion_r2097670174


More information about the net-dev mailing list