From b41b00a41c443f8a6c0c7023aab6b95745c57c23 Mon Sep 17 00:00:00 2001 From: Per Minborg Date: Mon, 13 Nov 2023 10:52:10 +0000 Subject: [PATCH] 8319820: Use unnamed variables in the FFM implementation Reviewed-by: mcimadamore --- .../foreign/AbstractMemorySegmentImpl.java | 41 ++++++++----------- .../classes/jdk/internal/foreign/Utils.java | 14 +++---- .../foreign/abi/fallback/FallbackLinker.java | 2 +- .../foreign/abi/x64/sysv/TypeClass.java | 2 +- 4 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java b/src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java index de0ad6603d54e..2f902e57ee5cd 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java +++ b/src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java @@ -558,29 +558,23 @@ public static AbstractMemorySegmentImpl ofBuffer(Buffer bb) { } else { bufferScope = MemorySessionImpl.createHeap(bufferRef(bb)); } + long off = bbAddress + ((long)pos << scaleFactor); + long len = (long)size << scaleFactor; if (base != null) { 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"); + case byte[] _ -> new HeapMemorySegmentImpl.OfByte(off, base, len, readOnly, bufferScope); + case short[] _ -> new HeapMemorySegmentImpl.OfShort(off, base, len, readOnly, bufferScope); + case char[] _ -> new HeapMemorySegmentImpl.OfChar(off, base, len, readOnly, bufferScope); + case int[] _ -> new HeapMemorySegmentImpl.OfInt(off, base, len, readOnly, bufferScope); + case float[] _ -> new HeapMemorySegmentImpl.OfFloat(off, base, len, readOnly, bufferScope); + case long[] _ -> new HeapMemorySegmentImpl.OfLong(off, base, len, readOnly, bufferScope); + case double[] _ -> new HeapMemorySegmentImpl.OfDouble(off, base, len, readOnly, bufferScope); + default -> throw new AssertionError("Cannot get here"); }; } else if (unmapper == null) { - return new NativeMemorySegmentImpl(bbAddress + (pos << scaleFactor), size << scaleFactor, readOnly, bufferScope); + return new NativeMemorySegmentImpl(off, len, 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, bufferScope); + return new MappedMemorySegmentImpl(off, unmapper, len, readOnly, bufferScope); } } @@ -721,13 +715,10 @@ public static long mismatch(MemorySegment srcSegment, long srcFromOffset, long s 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; + case ByteBuffer _ -> 0; + case CharBuffer _, ShortBuffer _ -> 1; + case IntBuffer _, FloatBuffer _ -> 2; + case LongBuffer _, DoubleBuffer _ -> 3; }; } diff --git a/src/java.base/share/classes/jdk/internal/foreign/Utils.java b/src/java.base/share/classes/jdk/internal/foreign/Utils.java index 00ab90e9612b3..d0d5c866f6b4e 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/Utils.java +++ b/src/java.base/share/classes/jdk/internal/foreign/Utils.java @@ -296,13 +296,13 @@ public record BaseAndScale(int base, long scale) { public static BaseAndScale of(Object array) { return switch (array) { - case byte[] __ -> BaseAndScale.BYTE; - case char[] __ -> BaseAndScale.CHAR; - case short[] __ -> BaseAndScale.SHORT; - case int[] __ -> BaseAndScale.INT; - case float[] __ -> BaseAndScale.FLOAT; - case long[] __ -> BaseAndScale.LONG; - case double[] __ -> BaseAndScale.DOUBLE; + case byte[] _ -> BaseAndScale.BYTE; + case char[] _ -> BaseAndScale.CHAR; + case short[] _ -> BaseAndScale.SHORT; + case int[] _ -> BaseAndScale.INT; + case float[] _ -> BaseAndScale.FLOAT; + case long[] _ -> BaseAndScale.LONG; + case double[] _ -> BaseAndScale.DOUBLE; default -> throw new IllegalArgumentException("Not a supported array class: " + array.getClass().getSimpleName()); }; } diff --git a/src/java.base/share/classes/jdk/internal/foreign/abi/fallback/FallbackLinker.java b/src/java.base/share/classes/jdk/internal/foreign/abi/fallback/FallbackLinker.java index 40ebe3b99cafe..07f75e6919aa6 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/abi/fallback/FallbackLinker.java +++ b/src/java.base/share/classes/jdk/internal/foreign/abi/fallback/FallbackLinker.java @@ -254,7 +254,7 @@ private static void writeValue(Object arg, MemoryLayout layout, MemorySegment ar acquireCallback.accept(addrArg); argSeg.set(al, 0, addrArg); } - case GroupLayout __ -> + case GroupLayout _ -> MemorySegment.copy((MemorySegment) arg, 0, argSeg, 0, argSeg.byteSize()); // by-value struct case null, default -> { assert layout == null; diff --git a/src/java.base/share/classes/jdk/internal/foreign/abi/x64/sysv/TypeClass.java b/src/java.base/share/classes/jdk/internal/foreign/abi/x64/sysv/TypeClass.java index 7a6be52ba2f5f..429b29e7fd1d4 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/abi/x64/sysv/TypeClass.java +++ b/src/java.base/share/classes/jdk/internal/foreign/abi/x64/sysv/TypeClass.java @@ -220,7 +220,7 @@ private static void groupByEightBytes(MemoryLayout layout, } } } - case PaddingLayout __ -> { + case PaddingLayout _ -> { } case SequenceLayout seq -> { MemoryLayout elem = seq.elementLayout();