Skip to content

Commit

Permalink
byte array var handles can share a common base class
Browse files Browse the repository at this point in the history
  • Loading branch information
liach committed Sep 21, 2023
1 parent 3a73e9a commit 1240416
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.
7 changes: 1 addition & 6 deletions src/java.base/share/classes/java/lang/invoke/VarHandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,7 @@
public abstract sealed class VarHandle implements Constable
permits IndirectVarHandle, LazyInitializingVarHandle,
VarHandleSegmentViewBase,
VarHandleByteArrayAsChars.ByteArrayViewVarHandle,
VarHandleByteArrayAsDoubles.ByteArrayViewVarHandle,
VarHandleByteArrayAsFloats.ByteArrayViewVarHandle,
VarHandleByteArrayAsInts.ByteArrayViewVarHandle,
VarHandleByteArrayAsLongs.ByteArrayViewVarHandle,
VarHandleByteArrayAsShorts.ByteArrayViewVarHandle,
VarHandleByteArrayBase,
VarHandleBooleans.Array,
VarHandleBooleans.FieldInstanceReadOnly,
VarHandleBooleans.FieldStaticReadOnly,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,54 @@

import jdk.internal.access.JavaNioAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.foreign.AbstractMemorySegmentImpl;
import jdk.internal.foreign.MemorySessionImpl;
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.vm.annotation.ForceInline;

import java.lang.foreign.MemorySegment;
import java.nio.ByteBuffer;

import static java.lang.invoke.MethodHandleStatics.UNSAFE;

/**
* The base class for generated byte array and byte buffer view
* implementations
*/
abstract class VarHandleByteArrayBase {
abstract sealed class VarHandleByteArrayBase extends VarHandle
permits VarHandleByteArrayAsChars.ArrayHandle,
VarHandleByteArrayAsChars.ByteBufferHandle,
VarHandleByteArrayAsDoubles.ArrayHandle,
VarHandleByteArrayAsDoubles.ByteBufferHandle,
VarHandleByteArrayAsFloats.ArrayHandle,
VarHandleByteArrayAsFloats.ByteBufferHandle,
VarHandleByteArrayAsInts.ArrayHandle,
VarHandleByteArrayAsInts.ByteBufferHandle,
VarHandleByteArrayAsLongs.ArrayHandle,
VarHandleByteArrayAsLongs.ByteBufferHandle,
VarHandleByteArrayAsShorts.ArrayHandle,
VarHandleByteArrayAsShorts.ByteBufferHandle {

static final boolean BE = UNSAFE.isBigEndian();

static final JavaNioAccess NIO_ACCESS = SharedSecrets.getJavaNioAccess();

static final ScopedMemoryAccess SCOPED_MEMORY_ACCESS = ScopedMemoryAccess.getScopedMemoryAccess();

final boolean be;

VarHandleByteArrayBase(VarForm form, boolean be, boolean exact) {
super(form, exact);
this.be = be;
}

@ForceInline
static MemorySessionImpl session(ByteBuffer bb) {
MemorySegment segment = NIO_ACCESS.bufferSegment(bb);
return segment != null ?
((AbstractMemorySegmentImpl)segment).sessionImpl() : null;
}

static IllegalStateException newIllegalStateExceptionForMisalignedAccess(int index) {
return new IllegalStateException("Misaligned access at index: " + index);
}
Expand Down
9 changes: 0 additions & 9 deletions src/java.base/share/classes/java/lang/invoke/VarHandles.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Stream;

import static java.lang.invoke.MethodHandleStatics.UNSAFE;
Expand All @@ -46,13 +44,6 @@

final class VarHandles {

static ClassValue<ConcurrentMap<Integer, MethodHandle>> ADDRESS_FACTORIES = new ClassValue<>() {
@Override
protected ConcurrentMap<Integer, MethodHandle> computeValue(Class<?> type) {
return new ConcurrentHashMap<>();
}
};

static VarHandle makeFieldHandle(MemberName f, Class<?> refc, boolean isWriteAllowedOnFinalFields) {
if (!f.isStatic()) {
long foffset = MethodHandleNatives.objectFieldOffset(f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@
*/
package java.lang.invoke;

import jdk.internal.foreign.AbstractMemorySegmentImpl;
import jdk.internal.foreign.MemorySessionImpl;
import jdk.internal.misc.Unsafe;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.ForceInline;

import java.lang.foreign.MemorySegment;
import java.nio.ByteBuffer;
import java.nio.ReadOnlyBufferException;
import java.util.Objects;
Expand All @@ -39,40 +36,30 @@ import static java.lang.invoke.MethodHandleStatics.UNSAFE;

#warn

final class VarHandleByteArrayAs$Type$s extends VarHandleByteArrayBase {
final class VarHandleByteArrayAs$Type$s {

static final int ALIGN = $BoxType$.BYTES - 1;

#if[floatingPoint]
@ForceInline
static $rawType$ convEndian(boolean big, $type$ v) {
$rawType$ rv = $Type$.$type$ToRaw$RawType$Bits(v);
return big == BE ? rv : $RawBoxType$.reverseBytes(rv);
return big == VarHandleByteArrayBase.BE ? rv : $RawBoxType$.reverseBytes(rv);
}

@ForceInline
static $type$ convEndian(boolean big, $rawType$ rv) {
rv = big == BE ? rv : $RawBoxType$.reverseBytes(rv);
rv = big == VarHandleByteArrayBase.BE ? rv : $RawBoxType$.reverseBytes(rv);
return $Type$.$rawType$BitsTo$Type$(rv);
}
#else[floatingPoint]
@ForceInline
static $type$ convEndian(boolean big, $type$ n) {
return big == BE ? n : $BoxType$.reverseBytes(n);
return big == VarHandleByteArrayBase.BE ? n : $BoxType$.reverseBytes(n);
}
#end[floatingPoint]


static abstract sealed class ByteArrayViewVarHandle extends VarHandle {
final boolean be;

ByteArrayViewVarHandle(VarForm form, boolean be, boolean exact) {
super(form, exact);
this.be = be;
}
}

static final class ArrayHandle extends ByteArrayViewVarHandle {
static final class ArrayHandle extends VarHandleByteArrayBase {

ArrayHandle(boolean be) {
this(be, false);
Expand Down Expand Up @@ -568,7 +555,7 @@ final class VarHandleByteArrayAs$Type$s extends VarHandleByteArrayBase {
}


static final class ByteBufferHandle extends ByteArrayViewVarHandle {
static final class ByteBufferHandle extends VarHandleByteArrayBase {

ByteBufferHandle(boolean be) {
this(be, false);
Expand Down Expand Up @@ -602,13 +589,6 @@ final class VarHandleByteArrayAs$Type$s extends VarHandleByteArrayBase {
return Preconditions.checkIndex(index, bb.limit() - ALIGN, null);
}

@ForceInline
static MemorySessionImpl session(ByteBuffer bb) {
MemorySegment segment = NIO_ACCESS.bufferSegment(bb);
return segment != null ?
((AbstractMemorySegmentImpl)segment).sessionImpl() : null;
}

@ForceInline
static int indexRO(ByteBuffer bb, int index) {
if (bb.isReadOnly())
Expand Down

0 comments on commit 1240416

Please sign in to comment.