RFR: 8286715: Generalize MemorySegment::ofBuffer
Jorn Vernee
jvernee at openjdk.java.net
Mon May 16 16:36:35 UTC 2022
On Fri, 13 May 2022 12:33:10 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
> This patch makes MemorySegment::ofBuffer more general, by allowing clients to pass *any* `Buffer` instance, not just `ByteBuffer`.
> This allows us to match expressiveness of JNI API, where JNI clients can obtain the address of any direct buffer instance, using the `GetDirectBufferAddress` function.
>
> We thought about also providing a more general way to view a segment as a buffer (e.g. asIntBuffer) but doing that doesn't seem worth it: direct buffers can only created form `ByteBuffer`.
> So, to create a direct `IntBuffer`, clients have to first create a direct `ByteBuffer` then to view that buffer as an `IntBuffer`.
>
> In other words, `IntBuffer` and friends are not first-class citizens in the `Buffer` API. As such it would not be possible to map many memory segments into an `IntBuffer`; in fact, the only segment we could safely map into an `IntBuffer` would be an _heap_ segment backed by an `int[]`. As such it doesn't seem worth adding a lot of API surface (in terms of additional overloads) for such a corner case.
Marked as reviewed by jvernee (Reviewer).
src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java line 572:
> 570: return new NativeMemorySegmentImpl(bbAddress + (pos << scaleFactor), size << scaleFactor, modes, bufferSession);
> 571: } else {
> 572: // we can ignore scale factor here, a mapped buffer is always a byte buffer, so scaleFactor == 1.
Suggestion:
// we can ignore scale factor here, a mapped buffer is always a byte buffer, so scaleFactor == 0.
-------------
PR: https://git.openjdk.java.net/jdk/pull/8701
More information about the security-dev
mailing list