Skip to content

Commit

Permalink
8318598: FFM stylistic cleanups
Browse files Browse the repository at this point in the history
Co-authored-by: Per Minborg <pminborg@openjdk.org>
Co-authored-by: Maurizio Cimadamore <mcimadamore@openjdk.org>
Reviewed-by: mcimadamore
  • Loading branch information
3 people committed Oct 20, 2023
1 parent b1228de commit 8065233
Show file tree
Hide file tree
Showing 34 changed files with 230 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import jdk.internal.access.SharedSecrets;
import jdk.internal.access.foreign.UnmapperProxy;
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.misc.Unsafe;
import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.Reflection;
import jdk.internal.util.ArraysSupport;
Expand Down Expand Up @@ -560,23 +559,23 @@ public static AbstractMemorySegmentImpl ofBuffer(Buffer bb) {
bufferScope = MemorySessionImpl.createHeap(bufferRef(bb));
}
if (base != null) {
if (base instanceof byte[]) {
return new HeapMemorySegmentImpl.OfByte(bbAddress + (pos << scaleFactor), base, size << scaleFactor, readOnly, bufferScope);
} else if (base instanceof short[]) {
return new HeapMemorySegmentImpl.OfShort(bbAddress + (pos << scaleFactor), base, size << scaleFactor, readOnly, bufferScope);
} else if (base instanceof char[]) {
return new HeapMemorySegmentImpl.OfChar(bbAddress + (pos << scaleFactor), base, size << scaleFactor, readOnly, bufferScope);
} else if (base instanceof int[]) {
return new HeapMemorySegmentImpl.OfInt(bbAddress + (pos << scaleFactor), base, size << scaleFactor, readOnly, bufferScope);
} else if (base instanceof float[]) {
return new HeapMemorySegmentImpl.OfFloat(bbAddress + (pos << scaleFactor), base, size << scaleFactor, readOnly, bufferScope);
} else if (base instanceof long[]) {
return new HeapMemorySegmentImpl.OfLong(bbAddress + (pos << scaleFactor), base, size << scaleFactor, readOnly, bufferScope);
} else if (base instanceof double[]) {
return new HeapMemorySegmentImpl.OfDouble(bbAddress + (pos << scaleFactor), base, size << scaleFactor, readOnly, bufferScope);
} else {
throw new AssertionError("Cannot get here");
}
return switch (base) {
case byte[] __ ->
new HeapMemorySegmentImpl.OfByte(bbAddress + (pos << scaleFactor), base, size << scaleFactor, readOnly, bufferScope);
case short[] __ ->
new HeapMemorySegmentImpl.OfShort(bbAddress + (pos << scaleFactor), base, size << scaleFactor, readOnly, bufferScope);
case char[] __ ->
new HeapMemorySegmentImpl.OfChar(bbAddress + (pos << scaleFactor), base, size << scaleFactor, readOnly, bufferScope);
case int[] __ ->
new HeapMemorySegmentImpl.OfInt(bbAddress + (pos << scaleFactor), base, size << scaleFactor, readOnly, bufferScope);
case float[] __ ->
new HeapMemorySegmentImpl.OfFloat(bbAddress + (pos << scaleFactor), base, size << scaleFactor, readOnly, bufferScope);
case long[] __ ->
new HeapMemorySegmentImpl.OfLong(bbAddress + (pos << scaleFactor), base, size << scaleFactor, readOnly, bufferScope);
case double[] __ ->
new HeapMemorySegmentImpl.OfDouble(bbAddress + (pos << scaleFactor), base, size << scaleFactor, readOnly, bufferScope);
default -> throw new AssertionError("Cannot get here");
};
} else if (unmapper == null) {
return new NativeMemorySegmentImpl(bbAddress + (pos << scaleFactor), size << scaleFactor, readOnly, bufferScope);
} else {
Expand All @@ -596,26 +595,6 @@ private static Object bufferRef(Buffer buffer) {
}
}

private static int getScaleFactor(Buffer buffer) {
if (buffer instanceof ByteBuffer) {
return 0;
} else if (buffer instanceof CharBuffer) {
return 1;
} else if (buffer instanceof ShortBuffer) {
return 1;
} else if (buffer instanceof IntBuffer) {
return 2;
} else if (buffer instanceof FloatBuffer) {
return 2;
} else if (buffer instanceof LongBuffer) {
return 3;
} else if (buffer instanceof DoubleBuffer) {
return 3;
} else {
throw new AssertionError("Cannot get here");
}
}

@ForceInline
public static void copy(MemorySegment srcSegment, ValueLayout srcElementLayout, long srcOffset,
MemorySegment dstSegment, ValueLayout dstElementLayout, long dstOffset,
Expand Down Expand Up @@ -653,27 +632,25 @@ public static void copy(MemorySegment srcSegment, ValueLayout srcLayout, long sr
Object dstArray, int dstIndex,
int elementCount) {

long baseAndScale = getBaseAndScale(dstArray.getClass());
var dstInfo = Utils.BaseAndScale.of(dstArray);
if (dstArray.getClass().componentType() != srcLayout.carrier()) {
throw new IllegalArgumentException("Incompatible value layout: " + srcLayout);
}
int dstBase = (int)baseAndScale;
long dstWidth = (int)(baseAndScale >> 32); // Use long arithmetics below
AbstractMemorySegmentImpl srcImpl = (AbstractMemorySegmentImpl)srcSegment;
Utils.checkElementAlignment(srcLayout, "Source layout alignment greater than its size");
if (!srcImpl.isAlignedForElement(srcOffset, srcLayout)) {
throw new IllegalArgumentException("Source segment incompatible with alignment constraints");
}
srcImpl.checkAccess(srcOffset, elementCount * dstWidth, true);
srcImpl.checkAccess(srcOffset, elementCount * dstInfo.scale(), true);
Objects.checkFromIndexSize(dstIndex, elementCount, Array.getLength(dstArray));
if (dstWidth == 1 || srcLayout.order() == ByteOrder.nativeOrder()) {
if (dstInfo.scale() == 1 || srcLayout.order() == ByteOrder.nativeOrder()) {
ScopedMemoryAccess.getScopedMemoryAccess().copyMemory(srcImpl.sessionImpl(), null,
srcImpl.unsafeGetBase(), srcImpl.unsafeGetOffset() + srcOffset,
dstArray, dstBase + (dstIndex * dstWidth), elementCount * dstWidth);
dstArray, dstInfo.base() + (dstIndex * dstInfo.scale()), elementCount * dstInfo.scale());
} else {
ScopedMemoryAccess.getScopedMemoryAccess().copySwapMemory(srcImpl.sessionImpl(), null,
srcImpl.unsafeGetBase(), srcImpl.unsafeGetOffset() + srcOffset,
dstArray, dstBase + (dstIndex * dstWidth), elementCount * dstWidth, dstWidth);
dstArray, dstInfo.base() + (dstIndex * dstInfo.scale()), elementCount * dstInfo.scale(), dstInfo.scale());
}
}

Expand All @@ -682,27 +659,25 @@ public static void copy(Object srcArray, int srcIndex,
MemorySegment dstSegment, ValueLayout dstLayout, long dstOffset,
int elementCount) {

long baseAndScale = getBaseAndScale(srcArray.getClass());
var srcInfo = Utils.BaseAndScale.of(srcArray);
if (srcArray.getClass().componentType() != dstLayout.carrier()) {
throw new IllegalArgumentException("Incompatible value layout: " + dstLayout);
}
int srcBase = (int)baseAndScale;
long srcWidth = (int)(baseAndScale >> 32); // Use long arithmetics below
Objects.checkFromIndexSize(srcIndex, elementCount, Array.getLength(srcArray));
AbstractMemorySegmentImpl destImpl = (AbstractMemorySegmentImpl)dstSegment;
Utils.checkElementAlignment(dstLayout, "Destination layout alignment greater than its size");
if (!destImpl.isAlignedForElement(dstOffset, dstLayout)) {
throw new IllegalArgumentException("Destination segment incompatible with alignment constraints");
}
destImpl.checkAccess(dstOffset, elementCount * srcWidth, false);
if (srcWidth == 1 || dstLayout.order() == ByteOrder.nativeOrder()) {
destImpl.checkAccess(dstOffset, elementCount * srcInfo.scale(), false);
if (srcInfo.scale() == 1 || dstLayout.order() == ByteOrder.nativeOrder()) {
ScopedMemoryAccess.getScopedMemoryAccess().copyMemory(null, destImpl.sessionImpl(),
srcArray, srcBase + (srcIndex * srcWidth),
destImpl.unsafeGetBase(), destImpl.unsafeGetOffset() + dstOffset, elementCount * srcWidth);
srcArray, srcInfo.base() + (srcIndex * srcInfo.scale()),
destImpl.unsafeGetBase(), destImpl.unsafeGetOffset() + dstOffset, elementCount * srcInfo.scale());
} else {
ScopedMemoryAccess.getScopedMemoryAccess().copySwapMemory(null, destImpl.sessionImpl(),
srcArray, srcBase + (srcIndex * srcWidth),
destImpl.unsafeGetBase(), destImpl.unsafeGetOffset() + dstOffset, elementCount * srcWidth, srcWidth);
srcArray, srcInfo.base() + (srcIndex * srcInfo.scale()),
destImpl.unsafeGetBase(), destImpl.unsafeGetOffset() + dstOffset, elementCount * srcInfo.scale(), srcInfo.scale());
}
}

Expand Down Expand Up @@ -744,24 +719,16 @@ public static long mismatch(MemorySegment srcSegment, long srcFromOffset, long s
return srcBytes != dstBytes ? bytes : -1;
}

private static long getBaseAndScale(Class<?> arrayType) {
if (arrayType.equals(byte[].class)) {
return (long) Unsafe.ARRAY_BYTE_BASE_OFFSET | ((long)Unsafe.ARRAY_BYTE_INDEX_SCALE << 32);
} else if (arrayType.equals(char[].class)) {
return (long) Unsafe.ARRAY_CHAR_BASE_OFFSET | ((long)Unsafe.ARRAY_CHAR_INDEX_SCALE << 32);
} else if (arrayType.equals(short[].class)) {
return (long)Unsafe.ARRAY_SHORT_BASE_OFFSET | ((long)Unsafe.ARRAY_SHORT_INDEX_SCALE << 32);
} else if (arrayType.equals(int[].class)) {
return (long)Unsafe.ARRAY_INT_BASE_OFFSET | ((long) Unsafe.ARRAY_INT_INDEX_SCALE << 32);
} else if (arrayType.equals(float[].class)) {
return (long)Unsafe.ARRAY_FLOAT_BASE_OFFSET | ((long)Unsafe.ARRAY_FLOAT_INDEX_SCALE << 32);
} else if (arrayType.equals(long[].class)) {
return (long)Unsafe.ARRAY_LONG_BASE_OFFSET | ((long)Unsafe.ARRAY_LONG_INDEX_SCALE << 32);
} else if (arrayType.equals(double[].class)) {
return (long)Unsafe.ARRAY_DOUBLE_BASE_OFFSET | ((long)Unsafe.ARRAY_DOUBLE_INDEX_SCALE << 32);
} else {
throw new IllegalArgumentException("Not a supported array class: " + arrayType.getSimpleName());
}
private static int getScaleFactor(Buffer buffer) {
return switch (buffer) {
case ByteBuffer __ -> 0;
case CharBuffer __ -> 1;
case ShortBuffer __ -> 1;
case IntBuffer __ -> 2;
case FloatBuffer __ -> 2;
case LongBuffer __ -> 3;
case DoubleBuffer __ -> 3;
};
}

// accessors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.lang.ref.Cleaner;

import jdk.internal.vm.annotation.ForceInline;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@

import jdk.internal.access.JavaNioAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.misc.Unsafe;
import jdk.internal.vm.annotation.ForceInline;

/**
* Implementation for heap memory segments. A heap memory segment is composed by an offset and
* a base object (typically an array). To enhance performances, the access to the base object needs to feature
* sharp type information, as well as sharp null-check information. For this reason, many concrete subclasses
* of {@link HeapMemorySegmentImpl} are defined (e.g. {@link OfFloat}, so that each subclass can override the
* of {@link HeapMemorySegmentImpl} are defined (e.g. {@link OfFloat}), so that each subclass can override the
* {@link HeapMemorySegmentImpl#unsafeGetBase()} method so that it returns an array of the correct (sharp) type. Note that
* the field type storing the 'base' coordinate is just Object; similarly, all the constructor in the subclasses
* accept an Object 'base' parameter instead of a sharper type (e.g. {@code byte[]}). This is deliberate, as
Expand All @@ -49,13 +48,15 @@
*/
abstract sealed class HeapMemorySegmentImpl extends AbstractMemorySegmentImpl {

private static final Unsafe UNSAFE = Unsafe.getUnsafe();
private static final int BYTE_ARR_BASE = UNSAFE.arrayBaseOffset(byte[].class);
// Constants defining the maximum alignment supported by various kinds of heap arrays.
// While for most arrays, the maximum alignment is constant (the size, in bytes, of the array elements),
// note that the alignment of a long[]/double[] depends on the platform: it's 4-byte on x86, but 8 bytes on x64
// (as specified by the JAVA_LONG layout constant).

private static final long MAX_ALIGN_1 = ValueLayout.JAVA_BYTE.byteAlignment();
private static final long MAX_ALIGN_2 = ValueLayout.JAVA_SHORT.byteAlignment();
private static final long MAX_ALIGN_4 = ValueLayout.JAVA_INT.byteAlignment();
private static final long MAX_ALIGN_8 = ValueLayout.JAVA_LONG.byteAlignment();
private static final long MAX_ALIGN_BYTE_ARRAY = ValueLayout.JAVA_BYTE.byteAlignment();
private static final long MAX_ALIGN_SHORT_ARRAY = ValueLayout.JAVA_SHORT.byteAlignment();
private static final long MAX_ALIGN_INT_ARRAY = ValueLayout.JAVA_INT.byteAlignment();
private static final long MAX_ALIGN_LONG_ARRAY = ValueLayout.JAVA_LONG.byteAlignment();

final long offset;
final Object base;
Expand Down Expand Up @@ -88,7 +89,7 @@ ByteBuffer makeByteBuffer() {
throw new UnsupportedOperationException("Not an address to an heap-allocated byte array");
}
JavaNioAccess nioAccess = SharedSecrets.getJavaNioAccess();
return nioAccess.newHeapByteBuffer(baseByte, (int)offset - BYTE_ARR_BASE, (int) byteSize(), null);
return nioAccess.newHeapByteBuffer(baseByte, (int)offset - Utils.BaseAndScale.BYTE.base(), (int) byteSize(), null);
}

// factories
Expand All @@ -111,12 +112,12 @@ public byte[] unsafeGetBase() {

@Override
public long maxAlignMask() {
return MAX_ALIGN_1;
return MAX_ALIGN_BYTE_ARRAY;
}

@Override
public long address() {
return offset - Unsafe.ARRAY_BYTE_BASE_OFFSET;
return offset - Utils.BaseAndScale.BYTE.base();
}
}

Expand All @@ -138,12 +139,12 @@ public char[] unsafeGetBase() {

@Override
public long maxAlignMask() {
return MAX_ALIGN_2;
return MAX_ALIGN_SHORT_ARRAY;
}

@Override
public long address() {
return offset - Unsafe.ARRAY_CHAR_BASE_OFFSET;
return offset - Utils.BaseAndScale.CHAR.base();
}
}

Expand All @@ -165,12 +166,12 @@ public short[] unsafeGetBase() {

@Override
public long maxAlignMask() {
return MAX_ALIGN_2;
return MAX_ALIGN_SHORT_ARRAY;
}

@Override
public long address() {
return offset - Unsafe.ARRAY_SHORT_BASE_OFFSET;
return offset - Utils.BaseAndScale.SHORT.base();
}
}

Expand All @@ -192,12 +193,12 @@ public int[] unsafeGetBase() {

@Override
public long maxAlignMask() {
return MAX_ALIGN_4;
return MAX_ALIGN_INT_ARRAY;
}

@Override
public long address() {
return offset - Unsafe.ARRAY_INT_BASE_OFFSET;
return offset - Utils.BaseAndScale.INT.base();
}
}

Expand All @@ -219,12 +220,12 @@ public long[] unsafeGetBase() {

@Override
public long maxAlignMask() {
return MAX_ALIGN_8;
return MAX_ALIGN_LONG_ARRAY;
}

@Override
public long address() {
return offset - Unsafe.ARRAY_LONG_BASE_OFFSET;
return offset - Utils.BaseAndScale.LONG.base();
}
}

Expand All @@ -246,12 +247,12 @@ public float[] unsafeGetBase() {

@Override
public long maxAlignMask() {
return MAX_ALIGN_4;
return MAX_ALIGN_INT_ARRAY;
}

@Override
public long address() {
return offset - Unsafe.ARRAY_FLOAT_BASE_OFFSET;
return offset - Utils.BaseAndScale.FLOAT.base();
}
}

Expand All @@ -273,12 +274,12 @@ public double[] unsafeGetBase() {

@Override
public long maxAlignMask() {
return MAX_ALIGN_8;
return MAX_ALIGN_LONG_ARRAY;
}

@Override
public long address() {
return offset - Unsafe.ARRAY_DOUBLE_BASE_OFFSET;
return offset - Utils.BaseAndScale.DOUBLE.base();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
package jdk.internal.foreign;

import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.Stable;

import java.lang.foreign.AddressLayout;
import java.lang.foreign.GroupLayout;
import java.lang.foreign.MemoryLayout;
Expand All @@ -41,7 +39,6 @@
import java.lang.invoke.VarHandle;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.function.UnaryOperator;
import java.util.stream.IntStream;
Expand All @@ -50,7 +47,7 @@
import static java.util.stream.Collectors.joining;

/**
* This class provide support for constructing layout paths; that is, starting from a root path (see {@link #rootPath(MemoryLayout)},
* This class provide support for constructing layout paths; that is, starting from a root path (see {@link #rootPath(MemoryLayout)}),
* a path can be constructed by selecting layout elements using the selector methods provided by this class
* (see {@link #sequenceElement()}, {@link #sequenceElement(long)}, {@link #sequenceElement(long, long)}, {@link #groupElement(String)}).
* Once a path has been fully constructed, clients can ask for the offset associated with the layout element selected
Expand Down

1 comment on commit 8065233

@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.