[foreign-memaccess+abi] RFR: 8303757: MemorySegment::reinterpret should accept an Arena
Jorn Vernee
jvernee at openjdk.org
Tue Mar 7 16:33:00 UTC 2023
On Tue, 7 Mar 2023 15:42:45 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
> This patch tweaks the signature of `MemorySegment::reinterpret` to accept `Arena` instead of just `MemorySegment.Scope`. After some more thinking, I realized that the current scope-accepting methods imply that there is an action at a distance: passing an arena scope to `MemorySegment::reinterpret` not only results in new temporal bounds (which is ok, since `Scope` is about that), but also sets confinement constraints too (which is surprising, as a `Scope` doesn't have a notion of confinement).
>
> For this reason, I've dialled back the signature to take an `Arena`. This makes the API more consistent (now "of course" the confinement properties of the returned segment will be specified by the provided arena). It also removes a bit of verbosity at the use site, as doing `arena.scope()` is no longer required.
src/java.base/share/classes/java/lang/foreign/MemorySegment.java line 592:
> 590: * Returns a new memory segment with the same address and size as this segment, but with the provided scope.
> 591: * As such, the returned segment cannot be accessed after the provided arena has been closed.
> 592: * Moreover, if the returned segment can be accessed compatibly with the confinement restrictions associated with the
Suggestion:
* Moreover, the returned segment can be accessed compatibly with the confinement restrictions associated with the
src/java.base/share/classes/java/lang/foreign/MemorySegment.java line 596:
> 594: * the returned segment can only be accessed by the arena's owner thread, regardless of the confinement restrictions
> 595: * associated with this segment. In other words, this method returns a segment that behaves as if it had been allocated
> 596: * using the provided arena.
"In other words, this method returns a segment that behaves as if it had been allocated using the provided arena."
I think this is a really nice way to explain this.
src/java.base/share/classes/java/lang/foreign/MemorySegment.java line 604:
> 602: * }
> 603: * That is, the cleanup action receives a segment that is associated with a fresh scope that is always alive,
> 604: * and is accessible from any thread. The size of the segment accepted by the cleanup action is {@code newSize}.
There's no `newSize` here. Looking at the implementation, I think this should be:
Suggestion:
* and is accessible from any thread. The size of the segment accepted by the cleanup action is {@link #byteSize()}.
-------------
PR: https://git.openjdk.org/panama-foreign/pull/812
More information about the panama-dev
mailing list