Skip to content

Commit

Permalink
8316641: VarHandle template classes can share code in the base class
Browse files Browse the repository at this point in the history
  • Loading branch information
liach committed Oct 13, 2023
1 parent 126f2ac commit 90b9763
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 143 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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2023, 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
Expand All @@ -24,7 +24,14 @@
*/
package java.lang.invoke;

import java.nio.Buffer;
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;
Expand All @@ -33,24 +40,39 @@
* The base class for generated byte array and byte buffer view
* implementations
*/
abstract class VarHandleByteArrayBase {
// Buffer.address
static final long BUFFER_ADDRESS
= UNSAFE.objectFieldOffset(Buffer.class, "address");
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();

// Buffer.limit
static final long BUFFER_LIMIT
= UNSAFE.objectFieldOffset(Buffer.class, "limit");
static final JavaNioAccess NIO_ACCESS = SharedSecrets.getJavaNioAccess();

// ByteBuffer.hb
static final long BYTE_BUFFER_HB
= UNSAFE.objectFieldOffset(ByteBuffer.class, "hb");
static final ScopedMemoryAccess SCOPED_MEMORY_ACCESS = ScopedMemoryAccess.getScopedMemoryAccess();

// ByteBuffer.isReadOnly
static final long BYTE_BUFFER_IS_READ_ONLY
= UNSAFE.objectFieldOffset(ByteBuffer.class, "isReadOnly");
final boolean be;

static final boolean BE = UNSAFE.isBigEndian();
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@

package java.lang.invoke;

import jdk.internal.foreign.AbstractMemorySegmentImpl;
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.vm.annotation.ForceInline;

import java.util.Objects;

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

/**
* Base class for memory segment var handle view implementations.
*/
Expand All @@ -37,6 +45,10 @@ abstract sealed class VarHandleSegmentViewBase extends VarHandle permits
VarHandleSegmentAsLongs,
VarHandleSegmentAsShorts {

static final boolean BE = UNSAFE.isBigEndian();

static final ScopedMemoryAccess SCOPED_MEMORY_ACCESS = ScopedMemoryAccess.getScopedMemoryAccess();

/** endianness **/
final boolean be;

Expand All @@ -53,6 +65,24 @@ abstract sealed class VarHandleSegmentViewBase extends VarHandle permits
this.alignmentMask = alignmentMask;
}

@ForceInline
static AbstractMemorySegmentImpl checkAddress(Object obb, long offset, long length, boolean ro) {
AbstractMemorySegmentImpl oo = (AbstractMemorySegmentImpl) Objects.requireNonNull(obb);
oo.checkAccess(offset, length, ro);
return oo;
}

@ForceInline
static long offsetPlain(AbstractMemorySegmentImpl bb, long offset, long alignmentMask) {
long base = bb.unsafeGetOffset();
long address = base + offset;
long maxAlignMask = bb.maxAlignMask();
if (((address | maxAlignMask) & alignmentMask) != 0) {
throw newIllegalArgumentExceptionForMisalignedAccess(address);
}
return address;
}

static IllegalArgumentException newIllegalArgumentExceptionForMisalignedAccess(long address) {
return new IllegalArgumentException("Misaligned access at address: " + address);
}
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 @@ -27,7 +27,6 @@ package java.lang.invoke;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.ForceInline;

import java.lang.invoke.VarHandle.VarHandleDesc;
import java.util.Objects;
import java.util.Optional;

Expand Down
Loading

0 comments on commit 90b9763

Please sign in to comment.