Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
/ jdk20 Public archive

Commit

Permalink
8298277: Replace "session" with "scope" for FFM access
Browse files Browse the repository at this point in the history
Reviewed-by: mcimadamore
  • Loading branch information
minborg authored and mcimadamore committed Dec 14, 2022
1 parent 581f9f2 commit ebc4710
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,22 @@ public abstract sealed class AbstractMemorySegmentImpl

final long length;
final boolean readOnly;
final SegmentScope session;
final SegmentScope scope;

@ForceInline
AbstractMemorySegmentImpl(long length, boolean readOnly, SegmentScope session) {
AbstractMemorySegmentImpl(long length, boolean readOnly, SegmentScope scope) {
this.length = length;
this.readOnly = readOnly;
this.session = session;
this.scope = scope;
}

abstract AbstractMemorySegmentImpl dup(long offset, long size, boolean readOnly, SegmentScope session);
abstract AbstractMemorySegmentImpl dup(long offset, long size, boolean readOnly, SegmentScope scope);

abstract ByteBuffer makeByteBuffer();

@Override
public AbstractMemorySegmentImpl asReadOnly() {
return dup(0, length, true, session);
return dup(0, length, true, scope);
}

@Override
Expand All @@ -113,7 +113,7 @@ public AbstractMemorySegmentImpl asSlice(long offset) {
}

private AbstractMemorySegmentImpl asSliceNoCheck(long offset, long newSize) {
return dup(offset, newSize, readOnly, session);
return dup(offset, newSize, readOnly, scope);
}

@Override
Expand Down Expand Up @@ -359,12 +359,12 @@ public RuntimeException apply(String s, List<Number> numbers) {

@Override
public SegmentScope scope() {
return session;
return scope;
}

@ForceInline
public final MemorySessionImpl sessionImpl() {
return (MemorySessionImpl)session;
return (MemorySessionImpl)scope;
}

private IndexOutOfBoundsException outOfBoundException(long offset, long length) {
Expand Down Expand Up @@ -481,11 +481,11 @@ public static AbstractMemorySegmentImpl ofBuffer(Buffer bb) {
int size = limit - pos;

AbstractMemorySegmentImpl bufferSegment = (AbstractMemorySegmentImpl) NIO_ACCESS.bufferSegment(bb);
final SegmentScope bufferSession;
final SegmentScope bufferScope;
if (bufferSegment != null) {
bufferSession = bufferSegment.session;
bufferScope = bufferSegment.scope;
} else {
bufferSession = MemorySessionImpl.heapSession(bb);
bufferScope = MemorySessionImpl.heapSession(bb);
}
boolean readOnly = bb.isReadOnly();
int scaleFactor = getScaleFactor(bb);
Expand All @@ -508,10 +508,10 @@ public static AbstractMemorySegmentImpl ofBuffer(Buffer bb) {
throw new AssertionError("Cannot get here");
}
} else if (unmapper == null) {
return new NativeMemorySegmentImpl(bbAddress + (pos << scaleFactor), size << scaleFactor, readOnly, bufferSession);
return new NativeMemorySegmentImpl(bbAddress + (pos << scaleFactor), size << scaleFactor, readOnly, bufferScope);
} else {
// we can ignore scale factor here, a mapped buffer is always a byte buffer, so scaleFactor == 0.
return new MappedMemorySegmentImpl(bbAddress + pos, unmapper, size, readOnly, bufferSession);
return new MappedMemorySegmentImpl(bbAddress + pos, unmapper, size, readOnly, bufferScope);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public long unsafeGetOffset() {
}

@Override
abstract HeapMemorySegmentImpl dup(long offset, long size, boolean readOnly, SegmentScope session);
abstract HeapMemorySegmentImpl dup(long offset, long size, boolean readOnly, SegmentScope scope);

@Override
ByteBuffer makeByteBuffer() {
Expand All @@ -99,7 +99,7 @@ public static final class OfByte extends HeapMemorySegmentImpl {
}

@Override
OfByte dup(long offset, long size, boolean readOnly, SegmentScope session) {
OfByte dup(long offset, long size, boolean readOnly, SegmentScope scope) {
return new OfByte(this.offset + offset, base, size, readOnly);
}

Expand Down Expand Up @@ -132,7 +132,7 @@ public static final class OfChar extends HeapMemorySegmentImpl {
}

@Override
OfChar dup(long offset, long size, boolean readOnly, SegmentScope session) {
OfChar dup(long offset, long size, boolean readOnly, SegmentScope scope) {
return new OfChar(this.offset + offset, base, size, readOnly);
}

Expand Down Expand Up @@ -165,7 +165,7 @@ public static final class OfShort extends HeapMemorySegmentImpl {
}

@Override
OfShort dup(long offset, long size, boolean readOnly, SegmentScope session) {
OfShort dup(long offset, long size, boolean readOnly, SegmentScope scope) {
return new OfShort(this.offset + offset, base, size, readOnly);
}

Expand Down Expand Up @@ -198,7 +198,7 @@ public static final class OfInt extends HeapMemorySegmentImpl {
}

@Override
OfInt dup(long offset, long size, boolean readOnly, SegmentScope session) {
OfInt dup(long offset, long size, boolean readOnly, SegmentScope scope) {
return new OfInt(this.offset + offset, base, size, readOnly);
}

Expand Down Expand Up @@ -231,7 +231,7 @@ public static final class OfLong extends HeapMemorySegmentImpl {
}

@Override
OfLong dup(long offset, long size, boolean readOnly, SegmentScope session) {
OfLong dup(long offset, long size, boolean readOnly, SegmentScope scope) {
return new OfLong(this.offset + offset, base, size, readOnly);
}

Expand Down Expand Up @@ -264,7 +264,7 @@ public static final class OfFloat extends HeapMemorySegmentImpl {
}

@Override
OfFloat dup(long offset, long size, boolean readOnly, SegmentScope session) {
OfFloat dup(long offset, long size, boolean readOnly, SegmentScope scope) {
return new OfFloat(this.offset + offset, base, size, readOnly);
}

Expand Down Expand Up @@ -297,7 +297,7 @@ public static final class OfDouble extends HeapMemorySegmentImpl {
}

@Override
OfDouble dup(long offset, long size, boolean readOnly, SegmentScope session) {
OfDouble dup(long offset, long size, boolean readOnly, SegmentScope scope) {
return new OfDouble(this.offset + offset, base, size, readOnly);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ public sealed class MappedMemorySegmentImpl extends NativeMemorySegmentImpl {

static final ScopedMemoryAccess SCOPED_MEMORY_ACCESS = ScopedMemoryAccess.getScopedMemoryAccess();

public MappedMemorySegmentImpl(long min, UnmapperProxy unmapper, long length, boolean readOnly, SegmentScope session) {
super(min, length, readOnly, session);
public MappedMemorySegmentImpl(long min, UnmapperProxy unmapper, long length, boolean readOnly, SegmentScope scope) {
super(min, length, readOnly, scope);
this.unmapper = unmapper;
}

@Override
ByteBuffer makeByteBuffer() {
return NIO_ACCESS.newMappedByteBuffer(unmapper, min, (int)length, null,
session == MemorySessionImpl.GLOBAL ? null : this);
scope == MemorySessionImpl.GLOBAL ? null : this);
}

@Override
MappedMemorySegmentImpl dup(long offset, long size, boolean readOnly, SegmentScope session) {
return new MappedMemorySegmentImpl(min + offset, unmapper, size, readOnly, session);
MappedMemorySegmentImpl dup(long offset, long size, boolean readOnly, SegmentScope scope) {
return new MappedMemorySegmentImpl(min + offset, unmapper, size, readOnly, scope);
}

// mapped segment methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ public final Thread ownerThread() {
return owner;
}

public static boolean sameOwnerThread(SegmentScope session1, SegmentScope session2) {
return ((MemorySessionImpl) session1).ownerThread() ==
((MemorySessionImpl) session2).ownerThread();
public static boolean sameOwnerThread(SegmentScope scope1, SegmentScope scope2) {
return ((MemorySessionImpl) scope1).ownerThread() ==
((MemorySessionImpl) scope2).ownerThread();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public sealed class NativeMemorySegmentImpl extends AbstractMemorySegmentImpl pe
final long min;

@ForceInline
NativeMemorySegmentImpl(long min, long length, boolean readOnly, SegmentScope session) {
super(length, readOnly, session);
NativeMemorySegmentImpl(long min, long length, boolean readOnly, SegmentScope scope) {
super(length, readOnly, scope);
this.min = min;
}

Expand All @@ -69,14 +69,14 @@ public Optional<Object> array() {

@ForceInline
@Override
NativeMemorySegmentImpl dup(long offset, long size, boolean readOnly, SegmentScope session) {
return new NativeMemorySegmentImpl(min + offset, size, readOnly, session);
NativeMemorySegmentImpl dup(long offset, long size, boolean readOnly, SegmentScope scope) {
return new NativeMemorySegmentImpl(min + offset, size, readOnly, scope);
}

@Override
ByteBuffer makeByteBuffer() {
return NIO_ACCESS.newDirectByteBuffer(min, (int) this.length, null,
session == MemorySessionImpl.GLOBAL ? null : this);
scope == MemorySessionImpl.GLOBAL ? null : this);
}

@Override
Expand All @@ -101,8 +101,8 @@ public long maxAlignMask() {

// factories

public static MemorySegment makeNativeSegment(long byteSize, long byteAlignment, SegmentScope session) {
MemorySessionImpl sessionImpl = (MemorySessionImpl) session;
public static MemorySegment makeNativeSegment(long byteSize, long byteAlignment, SegmentScope scope) {
MemorySessionImpl sessionImpl = (MemorySessionImpl) scope;
sessionImpl.checkValidState();
if (VM.isDirectMemoryPageAligned()) {
byteAlignment = Math.max(byteAlignment, NIO_ACCESS.pageSize());
Expand All @@ -119,7 +119,7 @@ public static MemorySegment makeNativeSegment(long byteSize, long byteAlignment,
}
long alignedBuf = Utils.alignUp(buf, byteAlignment);
AbstractMemorySegmentImpl segment = new NativeMemorySegmentImpl(buf, alignedSize,
false, session);
false, scope);
sessionImpl.addOrCleanupIfFail(new MemorySessionImpl.ResourceList.ResourceCleanup() {
@Override
public void cleanup() {
Expand All @@ -138,21 +138,21 @@ public void cleanup() {
// associated with MemorySegment::ofAddress.

@ForceInline
public static MemorySegment makeNativeSegmentUnchecked(long min, long byteSize, SegmentScope session, Runnable action) {
MemorySessionImpl sessionImpl = (MemorySessionImpl) session;
public static MemorySegment makeNativeSegmentUnchecked(long min, long byteSize, SegmentScope scope, Runnable action) {
MemorySessionImpl sessionImpl = (MemorySessionImpl) scope;
if (action == null) {
sessionImpl.checkValidState();
} else {
sessionImpl.addCloseAction(action);
}
return new NativeMemorySegmentImpl(min, byteSize, false, session);
return new NativeMemorySegmentImpl(min, byteSize, false, scope);
}

@ForceInline
public static MemorySegment makeNativeSegmentUnchecked(long min, long byteSize, SegmentScope session) {
MemorySessionImpl sessionImpl = (MemorySessionImpl) session;
public static MemorySegment makeNativeSegmentUnchecked(long min, long byteSize, SegmentScope scope) {
MemorySessionImpl sessionImpl = (MemorySessionImpl) scope;
sessionImpl.checkValidState();
return new NativeMemorySegmentImpl(min, byteSize, false, session);
return new NativeMemorySegmentImpl(min, byteSize, false, scope);
}

@ForceInline
Expand Down
28 changes: 14 additions & 14 deletions src/java.base/share/classes/jdk/internal/foreign/abi/Binding.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,19 @@ public interface Binding {
*/
class Context implements AutoCloseable {
private final SegmentAllocator allocator;
private final SegmentScope session;
private final SegmentScope scope;

private Context(SegmentAllocator allocator, SegmentScope session) {
private Context(SegmentAllocator allocator, SegmentScope scope) {
this.allocator = allocator;
this.session = session;
this.scope = scope;
}

public SegmentAllocator allocator() {
return allocator;
}

public SegmentScope session() {
return session;
public SegmentScope scope() {
return scope;
}

@Override
Expand Down Expand Up @@ -242,7 +242,7 @@ public void close() {
public static Context ofAllocator(SegmentAllocator allocator) {
return new Context(allocator, null) {
@Override
public SegmentScope session() {
public SegmentScope scope() {
throw new UnsupportedOperationException();
}
};
Expand All @@ -252,7 +252,7 @@ public SegmentScope session() {
* Create a binding context from given scope. The resulting context will throw when
* the context's allocator is accessed.
*/
public static Context ofSession() {
public static Context ofScope() {
Arena arena = Arena.openConfined();
return new Context(null, arena.scope()) {
@Override
Expand All @@ -276,7 +276,7 @@ public SegmentAllocator allocator() {
}

@Override
public SegmentScope session() {
public SegmentScope scope() {
throw new UnsupportedOperationException();
}

Expand Down Expand Up @@ -678,10 +678,10 @@ public void interpret(Deque<Object> stack, BindingInterpreter.StoreFunc storeFun

/**
* BOX_ADDRESS()
* Pops a 'long' from the operand stack, converts it to a 'MemorySegment', with the given size and memory session
* (either the context session, or the global session), and pushes that onto the operand stack.
* Pops a 'long' from the operand stack, converts it to a 'MemorySegment', with the given size and memory scope
* (either the context scope, or the global scope), and pushes that onto the operand stack.
*/
record BoxAddress(long size, boolean needsSession) implements Binding {
record BoxAddress(long size, boolean needsScope) implements Binding {

@Override
public Tag tag() {
Expand All @@ -698,9 +698,9 @@ public void verify(Deque<Class<?>> stack) {
@Override
public void interpret(Deque<Object> stack, BindingInterpreter.StoreFunc storeFunc,
BindingInterpreter.LoadFunc loadFunc, Context context) {
SegmentScope session = needsSession ?
context.session() : SegmentScope.global();
stack.push(NativeMemorySegmentImpl.makeNativeSegmentUnchecked((long) stack.pop(), size, session));
SegmentScope scope = needsScope ?
context.scope() : SegmentScope.global();
stack.push(NativeMemorySegmentImpl.makeNativeSegmentUnchecked((long) stack.pop(), size, scope));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public class BindingSpecializer {

private static final String BINDING_CONTEXT_DESC = Binding.Context.class.descriptorString();
private static final String OF_BOUNDED_ALLOCATOR_DESC = methodType(Binding.Context.class, long.class).descriptorString();
private static final String OF_SESSION_DESC = methodType(Binding.Context.class).descriptorString();
private static final String OF_SCOPE_DESC = methodType(Binding.Context.class).descriptorString();
private static final String ALLOCATOR_DESC = methodType(SegmentAllocator.class).descriptorString();
private static final String SESSION_DESC = methodType(SegmentScope.class).descriptorString();
private static final String SCOPE_DESC = methodType(SegmentScope.class).descriptorString();
private static final String SESSION_IMPL_DESC = methodType(MemorySessionImpl.class).descriptorString();
private static final String CLOSE_DESC = VOID_DESC;
private static final String UNBOX_SEGMENT_DESC = methodType(long.class, MemorySegment.class).descriptorString();
Expand Down Expand Up @@ -294,7 +294,7 @@ private void specialize() {
emitConst(callingSequence.allocationSize());
emitInvokeStatic(Binding.Context.class, "ofBoundedAllocator", OF_BOUNDED_ALLOCATOR_DESC);
} else if (callingSequence.forUpcall() && needsSession()) {
emitInvokeStatic(Binding.Context.class, "ofSession", OF_SESSION_DESC);
emitInvokeStatic(Binding.Context.class, "ofScope", OF_SCOPE_DESC);
} else {
emitGetStatic(Binding.Context.class, "DUMMY", BINDING_CONTEXT_DESC);
}
Expand Down Expand Up @@ -436,7 +436,7 @@ private boolean needsSession() {
return callingSequence.argumentBindings()
.filter(Binding.BoxAddress.class::isInstance)
.map(Binding.BoxAddress.class::cast)
.anyMatch(Binding.BoxAddress::needsSession);
.anyMatch(Binding.BoxAddress::needsScope);
}

private boolean shouldAcquire(int paramIndex) {
Expand Down Expand Up @@ -561,7 +561,7 @@ private int newLocal(Class<?> type) {
private void emitLoadInternalSession() {
assert contextIdx != -1;
emitLoad(Object.class, contextIdx);
emitInvokeVirtual(Binding.Context.class, "session", SESSION_DESC);
emitInvokeVirtual(Binding.Context.class, "scope", SCOPE_DESC);
}

private void emitLoadInternalAllocator() {
Expand Down

1 comment on commit ebc4710

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.