RFR: 8333884: MemorySegment::reinterpret removes read-only property

Maurizio Cimadamore mcimadamore at openjdk.org
Mon Jun 10 13:48:14 UTC 2024


On Mon, 10 Jun 2024 13:18:43 GMT, Per Minborg <pminborg at openjdk.org> wrote:

> This PR proposes to retain the read-only state when any of the `MemorySegment::reinterpret` methods is called.
> 
> Previously, the read-only state was lost and the returned `MemorySegment` was always writable regardless of the original segment's read-only state.

Note that `asSlice` methods also lacks a similar guarantee on read-only (but the impl works fine there)

src/java.base/share/classes/java/lang/foreign/MemorySegment.java line 708:

> 706:     /**
> 707:      * Returns a new memory segment that has the same address, scope, and
> 708:      * read-only state as this segment, but with the provided size.

In other parts of the javadoc we say something like this:


* If the buffer is {@linkplain Buffer#isReadOnly() read-only}, the resulting segment
     * is also {@linkplain ByteBuffer#isReadOnly() read-only}


I think this is better, as read-only is a bit of info that is not IMHO worth spelling out in the method javadoc first para,

Also, the term "read-only state" does not appear anywhere else in the FFM javadoc, so I'd recommend against using it. We can just say "if this segment is (link) read-only, the returned segment is read-only", or something like that.

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

PR Review: https://git.openjdk.org/jdk/pull/19629#pullrequestreview-2107854767
PR Review Comment: https://git.openjdk.org/jdk/pull/19629#discussion_r1633280123


More information about the core-libs-dev mailing list