8254162: Implementation of Foreign-Memory Access API (Third Incubator)

Reviewed-by: erikj, psandoz, alanb
This commit is contained in:
Maurizio Cimadamore 2020-11-12 16:37:23 +00:00
parent c6ab0fdb15
commit 3e70aac5cc
82 changed files with 6038 additions and 2837 deletions

View file

@ -31,6 +31,7 @@ import java.io.FileDescriptor;
import java.lang.ref.Reference;
import java.util.Objects;
import jdk.internal.access.foreign.MemorySegmentProxy;
import jdk.internal.misc.ScopedMemoryAccess.Scope;
import jdk.internal.misc.VM;
import jdk.internal.ref.Cleaner;
import sun.nio.ch.DirectBuffer;
@ -264,6 +265,17 @@ class Direct$Type$Buffer$RW$$BO$
#if[rw]
public long address() {
Scope scope = scope();
if (scope != null) {
if (scope.ownerThread() == null) {
throw new UnsupportedOperationException("ByteBuffer derived from shared segments not supported");
}
try {
scope.checkValidState();
} catch (Scope.ScopedAccessError e) {
throw new IllegalStateException("This segment is already closed");
}
}
return address;
}
@ -273,8 +285,7 @@ class Direct$Type$Buffer$RW$$BO$
public $type$ get() {
try {
checkSegment();
return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(nextGetIndex()))));
return $fromBits$($swap$(SCOPED_MEMORY_ACCESS.get$Swaptype$(scope(), null, ix(nextGetIndex()))));
} finally {
Reference.reachabilityFence(this);
}
@ -282,8 +293,7 @@ class Direct$Type$Buffer$RW$$BO$
public $type$ get(int i) {
try {
checkSegment();
return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(checkIndex(i)))));
return $fromBits$($swap$(SCOPED_MEMORY_ACCESS.get$Swaptype$(scope(), null, ix(checkIndex(i)))));
} finally {
Reference.reachabilityFence(this);
}
@ -292,7 +302,7 @@ class Direct$Type$Buffer$RW$$BO$
#if[streamableType]
$type$ getUnchecked(int i) {
try {
return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(i))));
return $fromBits$($swap$(SCOPED_MEMORY_ACCESS.get$Swaptype$(null, null, ix(i))));
} finally {
Reference.reachabilityFence(this);
}
@ -301,7 +311,6 @@ class Direct$Type$Buffer$RW$$BO$
public $Type$Buffer get($type$[] dst, int offset, int length) {
#if[rw]
checkSegment();
if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
Objects.checkFromIndexSize(offset, length, dst.length);
int pos = position();
@ -315,7 +324,7 @@ class Direct$Type$Buffer$RW$$BO$
try {
#if[!byte]
if (order() != ByteOrder.nativeOrder())
UNSAFE.copySwapMemory(null,
SCOPED_MEMORY_ACCESS.copySwapMemory(scope(), null, null,
ix(pos),
dst,
dstOffset,
@ -323,7 +332,7 @@ class Direct$Type$Buffer$RW$$BO$
(long)1 << $LG_BYTES_PER_VALUE$);
else
#end[!byte]
UNSAFE.copyMemory(null,
SCOPED_MEMORY_ACCESS.copyMemory(scope(), null, null,
ix(pos),
dst,
dstOffset,
@ -343,7 +352,6 @@ class Direct$Type$Buffer$RW$$BO$
public $Type$Buffer get(int index, $type$[] dst, int offset, int length) {
#if[rw]
checkSegment();
if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
Objects.checkFromIndexSize(index, length, limit());
Objects.checkFromIndexSize(offset, length, dst.length);
@ -352,7 +360,7 @@ class Direct$Type$Buffer$RW$$BO$
try {
#if[!byte]
if (order() != ByteOrder.nativeOrder())
UNSAFE.copySwapMemory(null,
SCOPED_MEMORY_ACCESS.copySwapMemory(scope(), null, null,
ix(index),
dst,
dstOffset,
@ -360,7 +368,7 @@ class Direct$Type$Buffer$RW$$BO$
(long)1 << $LG_BYTES_PER_VALUE$);
else
#end[!byte]
UNSAFE.copyMemory(null,
SCOPED_MEMORY_ACCESS.copyMemory(scope(), null, null,
ix(index),
dst,
dstOffset,
@ -381,8 +389,7 @@ class Direct$Type$Buffer$RW$$BO$
public $Type$Buffer put($type$ x) {
#if[rw]
try {
checkSegment();
UNSAFE.put$Swaptype$(ix(nextPutIndex()), $swap$($toBits$(x)));
SCOPED_MEMORY_ACCESS.put$Swaptype$(scope(), null, ix(nextPutIndex()), $swap$($toBits$(x)));
} finally {
Reference.reachabilityFence(this);
}
@ -395,8 +402,7 @@ class Direct$Type$Buffer$RW$$BO$
public $Type$Buffer put(int i, $type$ x) {
#if[rw]
try {
checkSegment();
UNSAFE.put$Swaptype$(ix(checkIndex(i)), $swap$($toBits$(x)));
SCOPED_MEMORY_ACCESS.put$Swaptype$(scope(), null, ix(checkIndex(i)), $swap$($toBits$(x)));
} finally {
Reference.reachabilityFence(this);
}
@ -408,7 +414,6 @@ class Direct$Type$Buffer$RW$$BO$
public $Type$Buffer put($Type$Buffer src) {
#if[rw]
checkSegment();
super.put(src);
return this;
#else[rw]
@ -418,7 +423,6 @@ class Direct$Type$Buffer$RW$$BO$
public $Type$Buffer put(int index, $Type$Buffer src, int offset, int length) {
#if[rw]
checkSegment();
super.put(index, src, offset, length);
return this;
#else[rw]
@ -428,7 +432,6 @@ class Direct$Type$Buffer$RW$$BO$
public $Type$Buffer put($type$[] src, int offset, int length) {
#if[rw]
checkSegment();
if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
Objects.checkFromIndexSize(offset, length, src.length);
int pos = position();
@ -442,7 +445,7 @@ class Direct$Type$Buffer$RW$$BO$
try {
#if[!byte]
if (order() != ByteOrder.nativeOrder())
UNSAFE.copySwapMemory(src,
SCOPED_MEMORY_ACCESS.copySwapMemory(scope(), null, src,
srcOffset,
null,
ix(pos),
@ -450,7 +453,7 @@ class Direct$Type$Buffer$RW$$BO$
(long)1 << $LG_BYTES_PER_VALUE$);
else
#end[!byte]
UNSAFE.copyMemory(src,
SCOPED_MEMORY_ACCESS.copyMemory(scope(), null, src,
srcOffset,
null,
ix(pos),
@ -470,7 +473,6 @@ class Direct$Type$Buffer$RW$$BO$
public $Type$Buffer put(int index, $type$[] src, int offset, int length) {
#if[rw]
checkSegment();
if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
Objects.checkFromIndexSize(index, length, limit());
Objects.checkFromIndexSize(offset, length, src.length);
@ -480,7 +482,7 @@ class Direct$Type$Buffer$RW$$BO$
try {
#if[!byte]
if (order() != ByteOrder.nativeOrder())
UNSAFE.copySwapMemory(src,
SCOPED_MEMORY_ACCESS.copySwapMemory(scope(), null, src,
srcOffset,
null,
ix(index),
@ -488,11 +490,9 @@ class Direct$Type$Buffer$RW$$BO$
(long)1 << $LG_BYTES_PER_VALUE$);
else
#end[!byte]
UNSAFE.copyMemory(src,
srcOffset,
null,
ix(index),
(long)length << $LG_BYTES_PER_VALUE$);
SCOPED_MEMORY_ACCESS.copyMemory(
scope(), null, src,
srcOffset, null, ix(index), (long)length << $LG_BYTES_PER_VALUE$);
} finally {
Reference.reachabilityFence(this);
}
@ -512,7 +512,8 @@ class Direct$Type$Buffer$RW$$BO$
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
try {
UNSAFE.copyMemory(ix(pos), ix(0), (long)rem << $LG_BYTES_PER_VALUE$);
SCOPED_MEMORY_ACCESS.copyMemory(scope(), null, null,
ix(pos), null, ix(0), (long)rem << $LG_BYTES_PER_VALUE$);
} finally {
Reference.reachabilityFence(this);
}