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)

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -31,7 +31,6 @@ import java.nio.ByteBuffer;
import java.util.Objects;
import jdk.internal.access.JavaNioAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.misc.ScopedMemoryAccess.Scope;
/**
* File-descriptor based I/O utilities that are shared by NIO classes.
@ -476,7 +475,7 @@ public class IOUtil {
private static final JavaNioAccess NIO_ACCESS = SharedSecrets.getJavaNioAccess();
static Runnable acquireScope(ByteBuffer bb, boolean async) {
return NIO_ACCESS.acquireScope(bb, async);
return NIO_ACCESS.acquireSession(bb, async);
}
private static void releaseScope(Runnable handle) {

View file

@ -27,6 +27,7 @@ package sun.nio.ch;
import java.io.FileDescriptor;
import java.io.IOException;
import java.lang.foreign.MemorySegment;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
@ -39,7 +40,6 @@ import java.util.Set;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.access.foreign.MemorySegmentProxy;
import jdk.internal.misc.TerminatingThreadLocal;
import jdk.internal.misc.Unsafe;
import sun.security.action.GetPropertyAction;
@ -420,7 +420,7 @@ public class Util {
long.class,
FileDescriptor.class,
Runnable.class,
boolean.class, MemorySegmentProxy.class});
boolean.class, MemorySegment.class});
ctor.setAccessible(true);
directByteBufferConstructor = ctor;
} catch (ClassNotFoundException |
@ -469,7 +469,7 @@ public class Util {
long.class,
FileDescriptor.class,
Runnable.class,
boolean.class, MemorySegmentProxy.class });
boolean.class, MemorySegment.class });
ctor.setAccessible(true);
directByteBufferRConstructor = ctor;
} catch (ClassNotFoundException |