8282191: Implementation of Foreign Function & Memory API (Preview)

Reviewed-by: erikj, jvernee, psandoz, dholmes, mchung
This commit is contained in:
Maurizio Cimadamore 2022-05-12 16:17:45 +00:00
parent 3be394e160
commit 2c5d136260
303 changed files with 33474 additions and 9186 deletions

View file

@ -28,6 +28,8 @@ package sun.nio.ch;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.MemorySession;
import java.lang.ref.Cleaner.Cleanable;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
@ -46,6 +48,9 @@ import java.util.Objects;
import jdk.internal.access.JavaIOFileDescriptorAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.foreign.AbstractMemorySegmentImpl;
import jdk.internal.foreign.MappedMemorySegmentImpl;
import jdk.internal.foreign.MemorySessionImpl;
import jdk.internal.misc.Blocker;
import jdk.internal.misc.ExtendedMapMode;
import jdk.internal.misc.Unsafe;
@ -1133,10 +1138,41 @@ public class FileChannelImpl
}
}
public Unmapper mapInternal(MapMode mode, long position, long size) throws IOException {
boolean isSync = isSync(Objects.requireNonNull(mode, "Mode is null"));
private static final int MAP_MEM_SEG_DEFAULT_MODES = 0;
private static final int MAP_MEM_SEG_READ_ONLY = 1;
@Override
public MemorySegment map(MapMode mode, long offset, long size,
MemorySession session)
throws IOException
{
Objects.requireNonNull(mode,"Mode is null");
Objects.requireNonNull(session, "Session is null");
MemorySessionImpl sessionImpl = MemorySessionImpl.toSessionImpl(session);
sessionImpl.checkValidStateSlow();
if (offset < 0) throw new IllegalArgumentException("Requested bytes offset must be >= 0.");
if (size < 0) throw new IllegalArgumentException("Requested bytes size must be >= 0.");
boolean isSync = isSync(mode);
int prot = toProt(mode);
return mapInternal(mode, position, size, prot, isSync);
Unmapper unmapper = mapInternal(mode, offset, size, prot, isSync);
int modes = MAP_MEM_SEG_DEFAULT_MODES;
if (mode == MapMode.READ_ONLY) {
modes |= MAP_MEM_SEG_READ_ONLY;
}
if (unmapper != null) {
AbstractMemorySegmentImpl segment = new MappedMemorySegmentImpl(unmapper.address(), unmapper, size,
modes, session);
sessionImpl.addOrCleanupIfFail(new MemorySessionImpl.ResourceList.ResourceCleanup() {
@Override
public void cleanup() {
unmapper.unmap();
}
});
return segment;
} else {
return new MappedMemorySegmentImpl.EmptyMappedMemorySegmentImpl(modes, session);
}
}
private Unmapper mapInternal(MapMode mode, long position, long size, int prot, boolean isSync)