8338677: Improve startup of memory access var handles by simplifying combinator chains

Reviewed-by: redestad
This commit is contained in:
Maurizio Cimadamore 2024-08-21 15:11:32 +00:00
parent 3aeb6733f9
commit 0e8fe3550b
4 changed files with 168 additions and 150 deletions

View File

@ -25,9 +25,11 @@
package java.lang.invoke; package java.lang.invoke;
import jdk.internal.foreign.AbstractMemorySegmentImpl; import jdk.internal.foreign.AbstractMemorySegmentImpl;
import jdk.internal.foreign.Utils;
import jdk.internal.misc.ScopedMemoryAccess; import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.vm.annotation.ForceInline; import jdk.internal.vm.annotation.ForceInline;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment; import java.lang.foreign.MemorySegment;
import java.lang.ref.Reference; import java.lang.ref.Reference;
@ -45,7 +47,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
static final int NON_PLAIN_ACCESS_MIN_ALIGN_MASK = $BoxType$.BYTES - 1; static final int NON_PLAIN_ACCESS_MIN_ALIGN_MASK = $BoxType$.BYTES - 1;
static final VarForm FORM = new VarForm(VarHandleSegmentAs$Type$s.class, MemorySegment.class, $type$.class, long.class); static final VarForm FORM = new VarForm(VarHandleSegmentAs$Type$s.class, MemorySegment.class, $type$.class, MemoryLayout.class, long.class, long.class);
VarHandleSegmentAs$Type$s(boolean be, long alignmentMask, boolean exact) { VarHandleSegmentAs$Type$s(boolean be, long alignmentMask, boolean exact) {
super(FORM, be, alignmentMask, exact); super(FORM, be, alignmentMask, exact);
@ -53,7 +55,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
@Override @Override
final MethodType accessModeTypeUncached(VarHandle.AccessType accessType) { final MethodType accessModeTypeUncached(VarHandle.AccessType accessType) {
return accessType.accessModeType(MemorySegment.class, $type$.class, long.class); return accessType.accessModeType(MemorySegment.class, $type$.class, MemoryLayout.class, long.class, long.class);
} }
@Override @Override
@ -97,70 +99,70 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
#end[floatingPoint] #end[floatingPoint]
@ForceInline @ForceInline
static AbstractMemorySegmentImpl checkReadOnly(Object obb, boolean ro) { static AbstractMemorySegmentImpl checkSegment(Object obb, Object encl, long base, boolean ro) {
AbstractMemorySegmentImpl oo = (AbstractMemorySegmentImpl)Objects.requireNonNull(obb); AbstractMemorySegmentImpl oo = (AbstractMemorySegmentImpl)Objects.requireNonNull(obb);
oo.checkReadOnly(ro); oo.checkEnclosingLayout(base, (MemoryLayout)encl, ro);
return oo; return oo;
} }
@ForceInline @ForceInline
static long offsetNonPlain(AbstractMemorySegmentImpl bb, long offset, long alignmentMask) { static long offsetNonPlain(AbstractMemorySegmentImpl bb, long base, long offset, long alignmentMask) {
if ((alignmentMask & NON_PLAIN_ACCESS_MIN_ALIGN_MASK) != NON_PLAIN_ACCESS_MIN_ALIGN_MASK) { if ((alignmentMask & NON_PLAIN_ACCESS_MIN_ALIGN_MASK) != NON_PLAIN_ACCESS_MIN_ALIGN_MASK) {
throw VarHandleSegmentViewBase.newUnsupportedAccessModeForAlignment(alignmentMask + 1); throw VarHandleSegmentViewBase.newUnsupportedAccessModeForAlignment(alignmentMask + 1);
} }
return offsetPlain(bb, offset); return offsetPlain(bb, base, offset);
} }
@ForceInline @ForceInline
static long offsetPlain(AbstractMemorySegmentImpl bb, long offset) { static long offsetPlain(AbstractMemorySegmentImpl bb, long base, long offset) {
long base = bb.unsafeGetOffset(); long segment_base = bb.unsafeGetOffset();
return base + offset; return segment_base + base + offset;
} }
@ForceInline @ForceInline
static $type$ get(VarHandle ob, Object obb, long base) { static $type$ get(VarHandle ob, Object obb, Object encl, long base, long offset) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, true); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, true);
#if[floatingPoint] #if[floatingPoint]
$rawType$ rawValue = SCOPED_MEMORY_ACCESS.get$RawType$Unaligned(bb.sessionImpl(), $rawType$ rawValue = SCOPED_MEMORY_ACCESS.get$RawType$Unaligned(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetPlain(bb, base), offsetPlain(bb, base, offset),
handle.be); handle.be);
return $Type$.$rawType$BitsTo$Type$(rawValue); return $Type$.$rawType$BitsTo$Type$(rawValue);
#else[floatingPoint] #else[floatingPoint]
#if[byte] #if[byte]
return SCOPED_MEMORY_ACCESS.get$Type$(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.get$Type$(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetPlain(bb, base)); offsetPlain(bb, base, offset));
#else[byte] #else[byte]
return SCOPED_MEMORY_ACCESS.get$Type$Unaligned(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.get$Type$Unaligned(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetPlain(bb, base), offsetPlain(bb, base, offset),
handle.be); handle.be);
#end[byte] #end[byte]
#end[floatingPoint] #end[floatingPoint]
} }
@ForceInline @ForceInline
static void set(VarHandle ob, Object obb, long base, $type$ value) { static void set(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
#if[floatingPoint] #if[floatingPoint]
SCOPED_MEMORY_ACCESS.put$RawType$Unaligned(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.put$RawType$Unaligned(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetPlain(bb, base), offsetPlain(bb, base, offset),
$Type$.$type$ToRaw$RawType$Bits(value), $Type$.$type$ToRaw$RawType$Bits(value),
handle.be); handle.be);
#else[floatingPoint] #else[floatingPoint]
#if[byte] #if[byte]
SCOPED_MEMORY_ACCESS.put$Type$(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.put$Type$(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetPlain(bb, base), offsetPlain(bb, base, offset),
value); value);
#else[byte] #else[byte]
SCOPED_MEMORY_ACCESS.put$Type$Unaligned(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.put$Type$Unaligned(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetPlain(bb, base), offsetPlain(bb, base, offset),
value, value,
handle.be); handle.be);
#end[byte] #end[byte]
@ -168,223 +170,223 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
} }
@ForceInline @ForceInline
static $type$ getVolatile(VarHandle ob, Object obb, long base) { static $type$ getVolatile(VarHandle ob, Object obb, Object encl, long base, long offset) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, true); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, true);
return convEndian(handle.be, return convEndian(handle.be,
SCOPED_MEMORY_ACCESS.get$RawType$Volatile(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.get$RawType$Volatile(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask))); offsetNonPlain(bb, base, offset, handle.alignmentMask)));
} }
@ForceInline @ForceInline
static void setVolatile(VarHandle ob, Object obb, long base, $type$ value) { static void setVolatile(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
SCOPED_MEMORY_ACCESS.put$RawType$Volatile(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.put$RawType$Volatile(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
convEndian(handle.be, value)); convEndian(handle.be, value));
} }
@ForceInline @ForceInline
static $type$ getAcquire(VarHandle ob, Object obb, long base) { static $type$ getAcquire(VarHandle ob, Object obb, Object encl, long base, long offset) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, true); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, true);
return convEndian(handle.be, return convEndian(handle.be,
SCOPED_MEMORY_ACCESS.get$RawType$Acquire(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.get$RawType$Acquire(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask))); offsetNonPlain(bb, base, offset, handle.alignmentMask)));
} }
@ForceInline @ForceInline
static void setRelease(VarHandle ob, Object obb, long base, $type$ value) { static void setRelease(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
SCOPED_MEMORY_ACCESS.put$RawType$Release(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.put$RawType$Release(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
convEndian(handle.be, value)); convEndian(handle.be, value));
} }
@ForceInline @ForceInline
static $type$ getOpaque(VarHandle ob, Object obb, long base) { static $type$ getOpaque(VarHandle ob, Object obb, Object encl, long base, long offset) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, true); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, true);
return convEndian(handle.be, return convEndian(handle.be,
SCOPED_MEMORY_ACCESS.get$RawType$Opaque(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.get$RawType$Opaque(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask))); offsetNonPlain(bb, base, offset, handle.alignmentMask)));
} }
@ForceInline @ForceInline
static void setOpaque(VarHandle ob, Object obb, long base, $type$ value) { static void setOpaque(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
SCOPED_MEMORY_ACCESS.put$RawType$Opaque(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.put$RawType$Opaque(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
convEndian(handle.be, value)); convEndian(handle.be, value));
} }
#if[CAS] #if[CAS]
@ForceInline @ForceInline
static boolean compareAndSet(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) { static boolean compareAndSet(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ expected, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
return SCOPED_MEMORY_ACCESS.compareAndSet$RawType$(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.compareAndSet$RawType$(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
convEndian(handle.be, expected), convEndian(handle.be, value)); convEndian(handle.be, expected), convEndian(handle.be, value));
} }
@ForceInline @ForceInline
static $type$ compareAndExchange(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) { static $type$ compareAndExchange(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ expected, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
return convEndian(handle.be, return convEndian(handle.be,
SCOPED_MEMORY_ACCESS.compareAndExchange$RawType$(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.compareAndExchange$RawType$(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
convEndian(handle.be, expected), convEndian(handle.be, value))); convEndian(handle.be, expected), convEndian(handle.be, value)));
} }
@ForceInline @ForceInline
static $type$ compareAndExchangeAcquire(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) { static $type$ compareAndExchangeAcquire(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ expected, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
return convEndian(handle.be, return convEndian(handle.be,
SCOPED_MEMORY_ACCESS.compareAndExchange$RawType$Acquire(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.compareAndExchange$RawType$Acquire(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
convEndian(handle.be, expected), convEndian(handle.be, value))); convEndian(handle.be, expected), convEndian(handle.be, value)));
} }
@ForceInline @ForceInline
static $type$ compareAndExchangeRelease(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) { static $type$ compareAndExchangeRelease(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ expected, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
return convEndian(handle.be, return convEndian(handle.be,
SCOPED_MEMORY_ACCESS.compareAndExchange$RawType$Release(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.compareAndExchange$RawType$Release(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
convEndian(handle.be, expected), convEndian(handle.be, value))); convEndian(handle.be, expected), convEndian(handle.be, value)));
} }
@ForceInline @ForceInline
static boolean weakCompareAndSetPlain(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) { static boolean weakCompareAndSetPlain(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ expected, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
return SCOPED_MEMORY_ACCESS.weakCompareAndSet$RawType$Plain(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.weakCompareAndSet$RawType$Plain(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
convEndian(handle.be, expected), convEndian(handle.be, value)); convEndian(handle.be, expected), convEndian(handle.be, value));
} }
@ForceInline @ForceInline
static boolean weakCompareAndSet(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) { static boolean weakCompareAndSet(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ expected, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
return SCOPED_MEMORY_ACCESS.weakCompareAndSet$RawType$(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.weakCompareAndSet$RawType$(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
convEndian(handle.be, expected), convEndian(handle.be, value)); convEndian(handle.be, expected), convEndian(handle.be, value));
} }
@ForceInline @ForceInline
static boolean weakCompareAndSetAcquire(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) { static boolean weakCompareAndSetAcquire(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ expected, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
return SCOPED_MEMORY_ACCESS.weakCompareAndSet$RawType$Acquire(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.weakCompareAndSet$RawType$Acquire(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
convEndian(handle.be, expected), convEndian(handle.be, value)); convEndian(handle.be, expected), convEndian(handle.be, value));
} }
@ForceInline @ForceInline
static boolean weakCompareAndSetRelease(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) { static boolean weakCompareAndSetRelease(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ expected, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
return SCOPED_MEMORY_ACCESS.weakCompareAndSet$RawType$Release(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.weakCompareAndSet$RawType$Release(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
convEndian(handle.be, expected), convEndian(handle.be, value)); convEndian(handle.be, expected), convEndian(handle.be, value));
} }
@ForceInline @ForceInline
static $type$ getAndSet(VarHandle ob, Object obb, long base, $type$ value) { static $type$ getAndSet(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
return convEndian(handle.be, return convEndian(handle.be,
SCOPED_MEMORY_ACCESS.getAndSet$RawType$(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.getAndSet$RawType$(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
convEndian(handle.be, value))); convEndian(handle.be, value)));
} }
@ForceInline @ForceInline
static $type$ getAndSetAcquire(VarHandle ob, Object obb, long base, $type$ value) { static $type$ getAndSetAcquire(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
return convEndian(handle.be, return convEndian(handle.be,
SCOPED_MEMORY_ACCESS.getAndSet$RawType$Acquire(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.getAndSet$RawType$Acquire(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
convEndian(handle.be, value))); convEndian(handle.be, value)));
} }
@ForceInline @ForceInline
static $type$ getAndSetRelease(VarHandle ob, Object obb, long base, $type$ value) { static $type$ getAndSetRelease(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
return convEndian(handle.be, return convEndian(handle.be,
SCOPED_MEMORY_ACCESS.getAndSet$RawType$Release(bb.sessionImpl(), SCOPED_MEMORY_ACCESS.getAndSet$RawType$Release(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
convEndian(handle.be, value))); convEndian(handle.be, value)));
} }
#end[CAS] #end[CAS]
#if[AtomicAdd] #if[AtomicAdd]
@ForceInline @ForceInline
static $type$ getAndAdd(VarHandle ob, Object obb, long base, $type$ delta) { static $type$ getAndAdd(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ delta) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
if (handle.be == BE) { if (handle.be == BE) {
return SCOPED_MEMORY_ACCESS.getAndAdd$RawType$(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.getAndAdd$RawType$(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
delta); delta);
} else { } else {
return getAndAddConvEndianWithCAS(bb, offsetNonPlain(bb, base, handle.alignmentMask), delta); return getAndAddConvEndianWithCAS(bb, offsetNonPlain(bb, base, offset, handle.alignmentMask), delta);
} }
} }
@ForceInline @ForceInline
static $type$ getAndAddAcquire(VarHandle ob, Object obb, long base, $type$ delta) { static $type$ getAndAddAcquire(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ delta) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
if (handle.be == BE) { if (handle.be == BE) {
return SCOPED_MEMORY_ACCESS.getAndAdd$RawType$Acquire(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.getAndAdd$RawType$Acquire(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
delta); delta);
} else { } else {
return getAndAddConvEndianWithCAS(bb, offsetNonPlain(bb, base, handle.alignmentMask), delta); return getAndAddConvEndianWithCAS(bb, offsetNonPlain(bb, base, offset, handle.alignmentMask), delta);
} }
} }
@ForceInline @ForceInline
static $type$ getAndAddRelease(VarHandle ob, Object obb, long base, $type$ delta) { static $type$ getAndAddRelease(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ delta) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
if (handle.be == BE) { if (handle.be == BE) {
return SCOPED_MEMORY_ACCESS.getAndAdd$RawType$Release(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.getAndAdd$RawType$Release(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
delta); delta);
} else { } else {
return getAndAddConvEndianWithCAS(bb, offsetNonPlain(bb, base, handle.alignmentMask), delta); return getAndAddConvEndianWithCAS(bb, offsetNonPlain(bb, base, offset, handle.alignmentMask), delta);
} }
} }
@ -403,44 +405,44 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
#if[Bitwise] #if[Bitwise]
@ForceInline @ForceInline
static $type$ getAndBitwiseOr(VarHandle ob, Object obb, long base, $type$ value) { static $type$ getAndBitwiseOr(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
if (handle.be == BE) { if (handle.be == BE) {
return SCOPED_MEMORY_ACCESS.getAndBitwiseOr$RawType$(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.getAndBitwiseOr$RawType$(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
value); value);
} else { } else {
return getAndBitwiseOrConvEndianWithCAS(bb, offsetNonPlain(bb, base, handle.alignmentMask), value); return getAndBitwiseOrConvEndianWithCAS(bb, offsetNonPlain(bb, base, offset, handle.alignmentMask), value);
} }
} }
@ForceInline @ForceInline
static $type$ getAndBitwiseOrRelease(VarHandle ob, Object obb, long base, $type$ value) { static $type$ getAndBitwiseOrRelease(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
if (handle.be == BE) { if (handle.be == BE) {
return SCOPED_MEMORY_ACCESS.getAndBitwiseOr$RawType$Release(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.getAndBitwiseOr$RawType$Release(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
value); value);
} else { } else {
return getAndBitwiseOrConvEndianWithCAS(bb, offsetNonPlain(bb, base, handle.alignmentMask), value); return getAndBitwiseOrConvEndianWithCAS(bb, offsetNonPlain(bb, base, offset, handle.alignmentMask), value);
} }
} }
@ForceInline @ForceInline
static $type$ getAndBitwiseOrAcquire(VarHandle ob, Object obb, long base, $type$ value) { static $type$ getAndBitwiseOrAcquire(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
if (handle.be == BE) { if (handle.be == BE) {
return SCOPED_MEMORY_ACCESS.getAndBitwiseOr$RawType$Acquire(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.getAndBitwiseOr$RawType$Acquire(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
value); value);
} else { } else {
return getAndBitwiseOrConvEndianWithCAS(bb, offsetNonPlain(bb, base, handle.alignmentMask), value); return getAndBitwiseOrConvEndianWithCAS(bb, offsetNonPlain(bb, base, offset, handle.alignmentMask), value);
} }
} }
@ -457,44 +459,44 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
} }
@ForceInline @ForceInline
static $type$ getAndBitwiseAnd(VarHandle ob, Object obb, long base, $type$ value) { static $type$ getAndBitwiseAnd(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
if (handle.be == BE) { if (handle.be == BE) {
return SCOPED_MEMORY_ACCESS.getAndBitwiseAnd$RawType$(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.getAndBitwiseAnd$RawType$(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
value); value);
} else { } else {
return getAndBitwiseAndConvEndianWithCAS(bb, offsetNonPlain(bb, base, handle.alignmentMask), value); return getAndBitwiseAndConvEndianWithCAS(bb, offsetNonPlain(bb, base, offset, handle.alignmentMask), value);
} }
} }
@ForceInline @ForceInline
static $type$ getAndBitwiseAndRelease(VarHandle ob, Object obb, long base, $type$ value) { static $type$ getAndBitwiseAndRelease(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
if (handle.be == BE) { if (handle.be == BE) {
return SCOPED_MEMORY_ACCESS.getAndBitwiseAnd$RawType$Release(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.getAndBitwiseAnd$RawType$Release(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
value); value);
} else { } else {
return getAndBitwiseAndConvEndianWithCAS(bb, offsetNonPlain(bb, base, handle.alignmentMask), value); return getAndBitwiseAndConvEndianWithCAS(bb, offsetNonPlain(bb, base, offset, handle.alignmentMask), value);
} }
} }
@ForceInline @ForceInline
static $type$ getAndBitwiseAndAcquire(VarHandle ob, Object obb, long base, $type$ value) { static $type$ getAndBitwiseAndAcquire(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
if (handle.be == BE) { if (handle.be == BE) {
return SCOPED_MEMORY_ACCESS.getAndBitwiseAnd$RawType$Acquire(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.getAndBitwiseAnd$RawType$Acquire(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
value); value);
} else { } else {
return getAndBitwiseAndConvEndianWithCAS(bb, offsetNonPlain(bb, base, handle.alignmentMask), value); return getAndBitwiseAndConvEndianWithCAS(bb, offsetNonPlain(bb, base, offset, handle.alignmentMask), value);
} }
} }
@ -512,44 +514,44 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
@ForceInline @ForceInline
static $type$ getAndBitwiseXor(VarHandle ob, Object obb, long base, $type$ value) { static $type$ getAndBitwiseXor(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
if (handle.be == BE) { if (handle.be == BE) {
return SCOPED_MEMORY_ACCESS.getAndBitwiseXor$RawType$(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.getAndBitwiseXor$RawType$(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
value); value);
} else { } else {
return getAndBitwiseXorConvEndianWithCAS(bb, offsetNonPlain(bb, base, handle.alignmentMask), value); return getAndBitwiseXorConvEndianWithCAS(bb, offsetNonPlain(bb, base, offset, handle.alignmentMask), value);
} }
} }
@ForceInline @ForceInline
static $type$ getAndBitwiseXorRelease(VarHandle ob, Object obb, long base, $type$ value) { static $type$ getAndBitwiseXorRelease(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
if (handle.be == BE) { if (handle.be == BE) {
return SCOPED_MEMORY_ACCESS.getAndBitwiseXor$RawType$Release(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.getAndBitwiseXor$RawType$Release(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
value); value);
} else { } else {
return getAndBitwiseXorConvEndianWithCAS(bb, offsetNonPlain(bb, base, handle.alignmentMask), value); return getAndBitwiseXorConvEndianWithCAS(bb, offsetNonPlain(bb, base, offset, handle.alignmentMask), value);
} }
} }
@ForceInline @ForceInline
static $type$ getAndBitwiseXorAcquire(VarHandle ob, Object obb, long base, $type$ value) { static $type$ getAndBitwiseXorAcquire(VarHandle ob, Object obb, Object encl, long base, long offset, $type$ value) {
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob; VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false); AbstractMemorySegmentImpl bb = checkSegment(obb, encl, base, false);
if (handle.be == BE) { if (handle.be == BE) {
return SCOPED_MEMORY_ACCESS.getAndBitwiseXor$RawType$Acquire(bb.sessionImpl(), return SCOPED_MEMORY_ACCESS.getAndBitwiseXor$RawType$Acquire(bb.sessionImpl(),
bb.unsafeGetBase(), bb.unsafeGetBase(),
offsetNonPlain(bb, base, handle.alignmentMask), offsetNonPlain(bb, base, offset, handle.alignmentMask),
value); value);
} else { } else {
return getAndBitwiseXorConvEndianWithCAS(bb, offsetNonPlain(bb, base, handle.alignmentMask), value); return getAndBitwiseXorConvEndianWithCAS(bb, offsetNonPlain(bb, base, offset, handle.alignmentMask), value);
} }
} }

View File

@ -373,6 +373,16 @@ public abstract sealed class AbstractMemorySegmentImpl
sessionImpl().checkValidState(); sessionImpl().checkValidState();
} }
@ForceInline
public final void checkEnclosingLayout(long offset, MemoryLayout enclosing, boolean readOnly) {
checkAccess(offset, enclosing.byteSize(), readOnly);
if (!isAlignedForElement(offset, enclosing)) {
throw new IllegalArgumentException(String.format(
"Target offset %d is incompatible with alignment constraint %d (of %s) for segment %s"
, offset, enclosing.byteAlignment(), enclosing, this));
}
}
public abstract long unsafeGetOffset(); public abstract long unsafeGetOffset();
public abstract Object unsafeGetBase(); public abstract Object unsafeGetBase();

View File

@ -80,7 +80,7 @@ public class LayoutPath {
MH_CHECK_ENCL_LAYOUT = lookup.findStatic(LayoutPath.class, "checkEnclosingLayout", MH_CHECK_ENCL_LAYOUT = lookup.findStatic(LayoutPath.class, "checkEnclosingLayout",
MethodType.methodType(void.class, MemorySegment.class, long.class, MemoryLayout.class)); MethodType.methodType(void.class, MemorySegment.class, long.class, MemoryLayout.class));
MH_SEGMENT_RESIZE = lookup.findStatic(LayoutPath.class, "resizeSegment", MH_SEGMENT_RESIZE = lookup.findStatic(LayoutPath.class, "resizeSegment",
MethodType.methodType(MemorySegment.class, MemorySegment.class, MemoryLayout.class)); MethodType.methodType(MemorySegment.class, MemorySegment.class));
MH_ADD = lookup.findStatic(Long.class, "sum", MH_ADD = lookup.findStatic(Long.class, "sum",
MethodType.methodType(long.class, long.class, long.class)); MethodType.methodType(long.class, long.class, long.class));
} catch (Throwable ex) { } catch (Throwable ex) {
@ -180,13 +180,14 @@ public class LayoutPath {
} }
MemoryLayout derefLayout = addressLayout.targetLayout().get(); MemoryLayout derefLayout = addressLayout.targetLayout().get();
MethodHandle handle = dereferenceHandle(false).toMethodHandle(VarHandle.AccessMode.GET); MethodHandle handle = dereferenceHandle(false).toMethodHandle(VarHandle.AccessMode.GET);
handle = MethodHandles.filterReturnValue(handle, handle = MethodHandles.filterReturnValue(handle, MH_SEGMENT_RESIZE);
MethodHandles.insertArguments(MH_SEGMENT_RESIZE, 1, derefLayout));
return derefPath(derefLayout, handle, this); return derefPath(derefLayout, handle, this);
} }
private static MemorySegment resizeSegment(MemorySegment segment, MemoryLayout layout) { private static MemorySegment resizeSegment(MemorySegment segment) {
return Utils.longToAddress(segment.address(), layout.byteSize(), layout.byteAlignment()); // Avoid adapting for specific target layout. The check for the root layout
// size and alignment will be inserted by LayoutPath::dereferenceHandle anyway.
return Utils.longToAddress(segment.address(), Long.MAX_VALUE, 1);
} }
// Layout path projections // Layout path projections
@ -205,19 +206,15 @@ public class LayoutPath {
String.format("Path does not select a value layout: %s", breadcrumbs())); String.format("Path does not select a value layout: %s", breadcrumbs()));
} }
VarHandle handle = Utils.makeRawSegmentViewVarHandle(valueLayout); VarHandle handle = Utils.makeRawSegmentViewVarHandle(valueLayout); // (MS, ML, long, long)
handle = MethodHandles.collectCoordinates(handle, 1, offsetHandle()); handle = MethodHandles.insertCoordinates(handle, 1, rootLayout()); // (MS, long, long)
if (strides.length > 0) {
// we only have to check the alignment of the root layout for the first dereference we do, MethodHandle offsetAdapter = offsetHandle();
// as each dereference checks the alignment of the target address when constructing its segment offsetAdapter = MethodHandles.insertArguments(offsetAdapter, 0, 0L);
// (see Utils::longToAddress) handle = MethodHandles.collectCoordinates(handle, 2, offsetAdapter); // (MS, long)
if (derefAdapters.length == 0) { } else {
// insert align check for the root layout on the initial MS + offset // simpler adaptation
List<Class<?>> coordinateTypes = handle.coordinateTypes(); handle = MethodHandles.insertCoordinates(handle, 2, offset); // (MS, long)
MethodHandle alignCheck = MethodHandles.insertArguments(MH_CHECK_ENCL_LAYOUT, 2, rootLayout());
handle = MethodHandles.collectCoordinates(handle, 0, alignCheck);
int[] reorder = IntStream.concat(IntStream.of(0, 1), IntStream.range(0, coordinateTypes.size())).toArray();
handle = MethodHandles.permuteCoordinates(handle, coordinateTypes, reorder);
} }
if (adapt) { if (adapt) {
@ -241,6 +238,8 @@ public class LayoutPath {
@ForceInline @ForceInline
private static long addScaledOffset(long base, long index, long stride, long bound) { private static long addScaledOffset(long base, long index, long stride, long bound) {
Objects.checkIndex(index, bound); Objects.checkIndex(index, bound);
// note: the below can overflow, depending on 'base'. When constructing var handles
// through the layout API, this is never the case, as the injected 'base' is always 0.
return base + (stride * index); return base + (stride * index);
} }
@ -285,12 +284,7 @@ public class LayoutPath {
} }
private static void checkEnclosingLayout(MemorySegment segment, long offset, MemoryLayout enclosing) { private static void checkEnclosingLayout(MemorySegment segment, long offset, MemoryLayout enclosing) {
((AbstractMemorySegmentImpl)segment).checkAccess(offset, enclosing.byteSize(), true); ((AbstractMemorySegmentImpl)segment).checkEnclosingLayout(offset, enclosing, true);
if (!((AbstractMemorySegmentImpl) segment).isAlignedForElement(offset, enclosing)) {
throw new IllegalArgumentException(String.format(
"Target offset %d is incompatible with alignment constraint %d (of %s) for segment %s"
, offset, enclosing.byteAlignment(), enclosing, segment));
}
} }
public MemoryLayout layout() { public MemoryLayout layout() {

View File

@ -63,7 +63,8 @@ public final class Utils {
private static final MethodHandle BYTE_TO_BOOL; private static final MethodHandle BYTE_TO_BOOL;
private static final MethodHandle BOOL_TO_BYTE; private static final MethodHandle BOOL_TO_BYTE;
private static final MethodHandle ADDRESS_TO_LONG; private static final MethodHandle ADDRESS_TO_LONG;
private static final MethodHandle LONG_TO_ADDRESS; private static final MethodHandle LONG_TO_ADDRESS_TARGET;
private static final MethodHandle LONG_TO_ADDRESS_NO_TARGET;
static { static {
try { try {
@ -74,8 +75,10 @@ public final class Utils {
MethodType.methodType(byte.class, boolean.class)); MethodType.methodType(byte.class, boolean.class));
ADDRESS_TO_LONG = lookup.findStatic(SharedUtils.class, "unboxSegment", ADDRESS_TO_LONG = lookup.findStatic(SharedUtils.class, "unboxSegment",
MethodType.methodType(long.class, MemorySegment.class)); MethodType.methodType(long.class, MemorySegment.class));
LONG_TO_ADDRESS = lookup.findStatic(Utils.class, "longToAddress", LONG_TO_ADDRESS_TARGET = lookup.findStatic(Utils.class, "longToAddress",
MethodType.methodType(MemorySegment.class, long.class, long.class, long.class)); MethodType.methodType(MemorySegment.class, long.class, AddressLayout.class));
LONG_TO_ADDRESS_NO_TARGET = lookup.findStatic(Utils.class, "longToAddress",
MethodType.methodType(MemorySegment.class, long.class));
} catch (Throwable ex) { } catch (Throwable ex) {
throw new ExceptionInInitializerError(ex); throw new ExceptionInInitializerError(ex);
} }
@ -129,11 +132,10 @@ public final class Utils {
if (layout.carrier() == boolean.class) { if (layout.carrier() == boolean.class) {
handle = MethodHandles.filterValue(handle, BOOL_TO_BYTE, BYTE_TO_BOOL); handle = MethodHandles.filterValue(handle, BOOL_TO_BYTE, BYTE_TO_BOOL);
} else if (layout instanceof AddressLayout addressLayout) { } else if (layout instanceof AddressLayout addressLayout) {
handle = MethodHandles.filterValue(handle, MethodHandle longToAddressAdapter = addressLayout.targetLayout().isPresent() ?
MethodHandles.explicitCastArguments(ADDRESS_TO_LONG, MethodType.methodType(baseCarrier, MemorySegment.class)), MethodHandles.insertArguments(LONG_TO_ADDRESS_TARGET, 1, addressLayout) :
MethodHandles.explicitCastArguments(MethodHandles.insertArguments(LONG_TO_ADDRESS, 1, LONG_TO_ADDRESS_NO_TARGET;
pointeeByteSize(addressLayout), pointeeByteAlign(addressLayout)), handle = MethodHandles.filterValue(handle, ADDRESS_TO_LONG, longToAddressAdapter);
MethodType.methodType(MemorySegment.class, baseCarrier)));
} }
return handle; return handle;
} }
@ -146,6 +148,16 @@ public final class Utils {
return b ? (byte)1 : (byte)0; return b ? (byte)1 : (byte)0;
} }
@ForceInline
public static MemorySegment longToAddress(long addr) {
return longToAddress(addr, 0, 1);
}
@ForceInline
public static MemorySegment longToAddress(long addr, AddressLayout layout) {
return longToAddress(addr, pointeeByteSize(layout), pointeeByteAlign(layout));
}
@ForceInline @ForceInline
public static MemorySegment longToAddress(long addr, long size, long align) { public static MemorySegment longToAddress(long addr, long size, long align) {
if (!isAligned(addr, align)) { if (!isAligned(addr, align)) {