8331865: Consolidate size and alignment checks in LayoutPath
Reviewed-by: psandoz, jvernee
This commit is contained in:
parent
6d718ae51a
commit
c003c1207f
src/java.base/share/classes
java/lang
foreign
invoke
jdk/internal/foreign
test/jdk/java/foreign
@ -625,12 +625,12 @@ public sealed interface MemoryLayout
|
||||
* (this layout), or an {@link IllegalArgumentException} is thrown. Note
|
||||
* that the alignment constraint of the root layout can be more strict
|
||||
* (but not less) than the alignment constraint of the selected value layout.</li>
|
||||
* <li>The offset of the access operation (computed as above) must fall inside
|
||||
* the spatial bounds of the accessed memory segment, or an
|
||||
* {@link IndexOutOfBoundsException} is thrown. This is the case when
|
||||
* {@code O + A <= S}, where {@code O} is the accessed offset (computed as above),
|
||||
* {@code A} is the size of the selected layout and {@code S} is the size of the
|
||||
* accessed memory segment.</li>
|
||||
* <li>The access operation must fall inside the spatial bounds of the accessed
|
||||
* memory segment, or an {@link IndexOutOfBoundsException} is thrown. This is the case
|
||||
* when {@code B + A <= S}, where {@code B} is the base offset (defined above),
|
||||
* {@code A} is the size of this layout and {@code S} is the size of the
|
||||
* accessed memory segment. Note that the size of this layout might be <em>bigger</em>
|
||||
* than the size of the accessed layout (e.g. when accessing a struct member).</li>
|
||||
* <li>If the provided layout path has an open path element whose size is {@code S},
|
||||
* its corresponding trailing {@code long} coordinate value {@code I} must be
|
||||
* {@code 0 <= I < S}, or an {@link IndexOutOfBoundsException} is thrown.</li>
|
||||
@ -753,12 +753,12 @@ public sealed interface MemoryLayout
|
||||
* (this layout), or an {@link IllegalArgumentException} is thrown. Note
|
||||
* that the alignment constraint of the root layout can be more strict
|
||||
* (but not less) than the alignment constraint of the selected value layout.</li>
|
||||
* <li>The offset of the access operation (computed as above) must fall inside
|
||||
* the spatial bounds of the accessed memory segment, or an
|
||||
* {@link IndexOutOfBoundsException} is thrown. This is the case when
|
||||
* {@code O + A <= S}, where {@code O} is the accessed offset (computed as above),
|
||||
* {@code A} is the size of the selected layout and {@code S} is the size of the
|
||||
* accessed memory segment.</li>
|
||||
* <li>The access operation must fall inside the spatial bounds of the accessed
|
||||
* memory segment, or an {@link IndexOutOfBoundsException} is thrown. This is the case
|
||||
* when {@code B + A <= S}, where {@code B} is the base offset (defined above),
|
||||
* {@code A} is the size of this layout and {@code S} is the size of the
|
||||
* accessed memory segment. Note that the size of this layout might be <em>bigger</em>
|
||||
* than the size of the accessed layout (e.g. when accessing a struct member).</li>
|
||||
* <li>If the provided layout path has an open path element whose size is {@code S},
|
||||
* its corresponding trailing {@code long} coordinate value {@code I} must be
|
||||
* {@code 0 <= I < S}, or an {@link IndexOutOfBoundsException} is thrown.</li>
|
||||
@ -822,12 +822,12 @@ public sealed interface MemoryLayout
|
||||
* (this layout), or an {@link IllegalArgumentException} will be issued. Note
|
||||
* that the alignment constraint of the root layout can be more strict
|
||||
* (but not less) than the alignment constraint of the selected layout.</li>
|
||||
* <li>The start offset of the slicing operation (computed as above) must fall
|
||||
* inside the spatial bounds of the accessed memory segment, or an
|
||||
* {@link IndexOutOfBoundsException} is thrown. This is the case when
|
||||
* {@code O + A <= S}, where {@code O} is the start offset of
|
||||
* the slicing operation (computed as above), {@code A} is the size of the
|
||||
* selected layout and {@code S} is the size of the accessed memory segment.</li>
|
||||
* <li>The slicing operation must fall inside the spatial bounds of the accessed
|
||||
* memory segment, or an {@link IndexOutOfBoundsException} is thrown. This is the case
|
||||
* when {@code B + A <= S}, where {@code B} is the base offset (defined above),
|
||||
* {@code A} is the size of this layout and {@code S} is the size of the
|
||||
* accessed memory segment. Note that the size of this layout might be <em>bigger</em>
|
||||
* than the size of the accessed layout (e.g. when accessing a struct member).</li>
|
||||
* <li>If the provided layout path has an open path element whose size is {@code S},
|
||||
* its corresponding trailing {@code long} coordinate value {@code I} must be
|
||||
* {@code 0 <= I < S}, or an {@link IndexOutOfBoundsException} is thrown.</li>
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2019, 2024, 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
|
||||
@ -25,8 +25,6 @@
|
||||
|
||||
package java.lang.invoke;
|
||||
|
||||
import jdk.internal.foreign.Utils;
|
||||
|
||||
/**
|
||||
* Base class for memory segment var handle view implementations.
|
||||
*/
|
||||
@ -42,23 +40,15 @@ abstract sealed class VarHandleSegmentViewBase extends VarHandle permits
|
||||
/** endianness **/
|
||||
final boolean be;
|
||||
|
||||
/** access size (in bytes, computed from var handle carrier type) **/
|
||||
final long length;
|
||||
|
||||
/** alignment constraint (in bytes, expressed as a bit mask) **/
|
||||
final long alignmentMask;
|
||||
|
||||
VarHandleSegmentViewBase(VarForm form, boolean be, long length, long alignmentMask, boolean exact) {
|
||||
VarHandleSegmentViewBase(VarForm form, boolean be, long alignmentMask, boolean exact) {
|
||||
super(form, exact);
|
||||
this.be = be;
|
||||
this.length = length;
|
||||
this.alignmentMask = alignmentMask;
|
||||
}
|
||||
|
||||
static IllegalArgumentException newIllegalArgumentExceptionForMisalignedAccess(long address) {
|
||||
return new IllegalArgumentException("Misaligned access at address: " + Utils.toHexString(address));
|
||||
}
|
||||
|
||||
static UnsupportedOperationException newUnsupportedAccessModeForAlignment(long alignment) {
|
||||
return new UnsupportedOperationException("Unsupported access mode for alignment: " + alignment);
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2014, 2024, 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
|
||||
@ -306,6 +306,9 @@ final class VarHandles {
|
||||
* The resulting var handle will take a memory segment as first argument (the segment to be dereferenced),
|
||||
* and a {@code long} as second argument (the offset into the segment).
|
||||
*
|
||||
* Note: the returned var handle does not perform any size or alignment check. It is up to clients
|
||||
* to adapt the returned var handle and insert the appropriate checks.
|
||||
*
|
||||
* @param carrier the Java carrier type.
|
||||
* @param alignmentMask alignment requirement to be checked upon access. In bytes. Expressed as a mask.
|
||||
* @param byteOrder the byte order.
|
||||
@ -316,24 +319,23 @@ final class VarHandles {
|
||||
if (!carrier.isPrimitive() || carrier == void.class || carrier == boolean.class) {
|
||||
throw new IllegalArgumentException("Invalid carrier: " + carrier.getName());
|
||||
}
|
||||
long size = Utils.byteWidthOfPrimitive(carrier);
|
||||
boolean be = byteOrder == ByteOrder.BIG_ENDIAN;
|
||||
boolean exact = VAR_HANDLE_SEGMENT_FORCE_EXACT;
|
||||
|
||||
if (carrier == byte.class) {
|
||||
return maybeAdapt(new VarHandleSegmentAsBytes(be, size, alignmentMask, exact));
|
||||
return maybeAdapt(new VarHandleSegmentAsBytes(be, alignmentMask, exact));
|
||||
} else if (carrier == char.class) {
|
||||
return maybeAdapt(new VarHandleSegmentAsChars(be, size, alignmentMask, exact));
|
||||
return maybeAdapt(new VarHandleSegmentAsChars(be, alignmentMask, exact));
|
||||
} else if (carrier == short.class) {
|
||||
return maybeAdapt(new VarHandleSegmentAsShorts(be, size, alignmentMask, exact));
|
||||
return maybeAdapt(new VarHandleSegmentAsShorts(be, alignmentMask, exact));
|
||||
} else if (carrier == int.class) {
|
||||
return maybeAdapt(new VarHandleSegmentAsInts(be, size, alignmentMask, exact));
|
||||
return maybeAdapt(new VarHandleSegmentAsInts(be, alignmentMask, exact));
|
||||
} else if (carrier == float.class) {
|
||||
return maybeAdapt(new VarHandleSegmentAsFloats(be, size, alignmentMask, exact));
|
||||
return maybeAdapt(new VarHandleSegmentAsFloats(be, alignmentMask, exact));
|
||||
} else if (carrier == long.class) {
|
||||
return maybeAdapt(new VarHandleSegmentAsLongs(be, size, alignmentMask, exact));
|
||||
return maybeAdapt(new VarHandleSegmentAsLongs(be, alignmentMask, exact));
|
||||
} else if (carrier == double.class) {
|
||||
return maybeAdapt(new VarHandleSegmentAsDoubles(be, size, alignmentMask, exact));
|
||||
return maybeAdapt(new VarHandleSegmentAsDoubles(be, alignmentMask, exact));
|
||||
} else {
|
||||
throw new IllegalStateException("Cannot get here");
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2019, 2024, 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
|
||||
@ -47,8 +47,8 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
|
||||
static final VarForm FORM = new VarForm(VarHandleSegmentAs$Type$s.class, MemorySegment.class, $type$.class, long.class);
|
||||
|
||||
VarHandleSegmentAs$Type$s(boolean be, long length, long alignmentMask, boolean exact) {
|
||||
super(FORM, be, length, alignmentMask, exact);
|
||||
VarHandleSegmentAs$Type$s(boolean be, long alignmentMask, boolean exact) {
|
||||
super(FORM, be, alignmentMask, exact);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -60,14 +60,14 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
public VarHandleSegmentAs$Type$s withInvokeExactBehavior() {
|
||||
return hasInvokeExactBehavior() ?
|
||||
this :
|
||||
new VarHandleSegmentAs$Type$s(be, length, alignmentMask, true);
|
||||
new VarHandleSegmentAs$Type$s(be, alignmentMask, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VarHandleSegmentAs$Type$s withInvokeBehavior() {
|
||||
return !hasInvokeExactBehavior() ?
|
||||
this :
|
||||
new VarHandleSegmentAs$Type$s(be, length, alignmentMask, false);
|
||||
new VarHandleSegmentAs$Type$s(be, alignmentMask, false);
|
||||
}
|
||||
|
||||
#if[floatingPoint]
|
||||
@ -97,9 +97,9 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
#end[floatingPoint]
|
||||
|
||||
@ForceInline
|
||||
static AbstractMemorySegmentImpl checkAddress(Object obb, long offset, long length, boolean ro) {
|
||||
static AbstractMemorySegmentImpl checkReadOnly(Object obb, boolean ro) {
|
||||
AbstractMemorySegmentImpl oo = (AbstractMemorySegmentImpl)Objects.requireNonNull(obb);
|
||||
oo.checkAccess(offset, length, ro);
|
||||
oo.checkReadOnly(ro);
|
||||
return oo;
|
||||
}
|
||||
|
||||
@ -108,39 +108,34 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
if ((alignmentMask & NON_PLAIN_ACCESS_MIN_ALIGN_MASK) != NON_PLAIN_ACCESS_MIN_ALIGN_MASK) {
|
||||
throw VarHandleSegmentViewBase.newUnsupportedAccessModeForAlignment(alignmentMask + 1);
|
||||
}
|
||||
return offsetPlain(bb, offset, alignmentMask);
|
||||
return offsetPlain(bb, offset);
|
||||
}
|
||||
|
||||
@ForceInline
|
||||
static long offsetPlain(AbstractMemorySegmentImpl bb, long offset, long alignmentMask) {
|
||||
static long offsetPlain(AbstractMemorySegmentImpl bb, long offset) {
|
||||
long base = bb.unsafeGetOffset();
|
||||
long address = base + offset;
|
||||
long maxAlignMask = bb.maxAlignMask();
|
||||
if (((address | maxAlignMask) & alignmentMask) != 0) {
|
||||
throw VarHandleSegmentViewBase.newIllegalArgumentExceptionForMisalignedAccess(address);
|
||||
}
|
||||
return address;
|
||||
return base + offset;
|
||||
}
|
||||
|
||||
@ForceInline
|
||||
static $type$ get(VarHandle ob, Object obb, long base) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, true);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, true);
|
||||
#if[floatingPoint]
|
||||
$rawType$ rawValue = SCOPED_MEMORY_ACCESS.get$RawType$Unaligned(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
offsetPlain(bb, base, handle.alignmentMask),
|
||||
offsetPlain(bb, base),
|
||||
handle.be);
|
||||
return $Type$.$rawType$BitsTo$Type$(rawValue);
|
||||
#else[floatingPoint]
|
||||
#if[byte]
|
||||
return SCOPED_MEMORY_ACCESS.get$Type$(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
offsetPlain(bb, base, handle.alignmentMask));
|
||||
offsetPlain(bb, base));
|
||||
#else[byte]
|
||||
return SCOPED_MEMORY_ACCESS.get$Type$Unaligned(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
offsetPlain(bb, base, handle.alignmentMask),
|
||||
offsetPlain(bb, base),
|
||||
handle.be);
|
||||
#end[byte]
|
||||
#end[floatingPoint]
|
||||
@ -149,23 +144,23 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static void set(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
#if[floatingPoint]
|
||||
SCOPED_MEMORY_ACCESS.put$RawType$Unaligned(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
offsetPlain(bb, base, handle.alignmentMask),
|
||||
offsetPlain(bb, base),
|
||||
$Type$.$type$ToRaw$RawType$Bits(value),
|
||||
handle.be);
|
||||
#else[floatingPoint]
|
||||
#if[byte]
|
||||
SCOPED_MEMORY_ACCESS.put$Type$(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
offsetPlain(bb, base, handle.alignmentMask),
|
||||
offsetPlain(bb, base),
|
||||
value);
|
||||
#else[byte]
|
||||
SCOPED_MEMORY_ACCESS.put$Type$Unaligned(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
offsetPlain(bb, base, handle.alignmentMask),
|
||||
offsetPlain(bb, base),
|
||||
value,
|
||||
handle.be);
|
||||
#end[byte]
|
||||
@ -175,7 +170,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getVolatile(VarHandle ob, Object obb, long base) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, true);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, true);
|
||||
return convEndian(handle.be,
|
||||
SCOPED_MEMORY_ACCESS.get$RawType$Volatile(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -185,7 +180,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static void setVolatile(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
SCOPED_MEMORY_ACCESS.put$RawType$Volatile(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
offsetNonPlain(bb, base, handle.alignmentMask),
|
||||
@ -195,7 +190,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAcquire(VarHandle ob, Object obb, long base) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, true);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, true);
|
||||
return convEndian(handle.be,
|
||||
SCOPED_MEMORY_ACCESS.get$RawType$Acquire(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -205,7 +200,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static void setRelease(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
SCOPED_MEMORY_ACCESS.put$RawType$Release(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
offsetNonPlain(bb, base, handle.alignmentMask),
|
||||
@ -215,7 +210,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getOpaque(VarHandle ob, Object obb, long base) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, true);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, true);
|
||||
return convEndian(handle.be,
|
||||
SCOPED_MEMORY_ACCESS.get$RawType$Opaque(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -225,7 +220,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static void setOpaque(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
SCOPED_MEMORY_ACCESS.put$RawType$Opaque(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
offsetNonPlain(bb, base, handle.alignmentMask),
|
||||
@ -236,7 +231,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static boolean compareAndSet(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
return SCOPED_MEMORY_ACCESS.compareAndSet$RawType$(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
offsetNonPlain(bb, base, handle.alignmentMask),
|
||||
@ -246,7 +241,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ compareAndExchange(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
return convEndian(handle.be,
|
||||
SCOPED_MEMORY_ACCESS.compareAndExchange$RawType$(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -257,7 +252,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ compareAndExchangeAcquire(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
return convEndian(handle.be,
|
||||
SCOPED_MEMORY_ACCESS.compareAndExchange$RawType$Acquire(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -268,7 +263,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ compareAndExchangeRelease(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
return convEndian(handle.be,
|
||||
SCOPED_MEMORY_ACCESS.compareAndExchange$RawType$Release(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -279,7 +274,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static boolean weakCompareAndSetPlain(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
return SCOPED_MEMORY_ACCESS.weakCompareAndSet$RawType$Plain(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
offsetNonPlain(bb, base, handle.alignmentMask),
|
||||
@ -289,7 +284,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static boolean weakCompareAndSet(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
return SCOPED_MEMORY_ACCESS.weakCompareAndSet$RawType$(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
offsetNonPlain(bb, base, handle.alignmentMask),
|
||||
@ -299,7 +294,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static boolean weakCompareAndSetAcquire(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
return SCOPED_MEMORY_ACCESS.weakCompareAndSet$RawType$Acquire(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
offsetNonPlain(bb, base, handle.alignmentMask),
|
||||
@ -309,7 +304,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static boolean weakCompareAndSetRelease(VarHandle ob, Object obb, long base, $type$ expected, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
return SCOPED_MEMORY_ACCESS.weakCompareAndSet$RawType$Release(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
offsetNonPlain(bb, base, handle.alignmentMask),
|
||||
@ -319,7 +314,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndSet(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
return convEndian(handle.be,
|
||||
SCOPED_MEMORY_ACCESS.getAndSet$RawType$(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -330,7 +325,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndSetAcquire(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
return convEndian(handle.be,
|
||||
SCOPED_MEMORY_ACCESS.getAndSet$RawType$Acquire(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -341,7 +336,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndSetRelease(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
return convEndian(handle.be,
|
||||
SCOPED_MEMORY_ACCESS.getAndSet$RawType$Release(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -354,7 +349,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndAdd(VarHandle ob, Object obb, long base, $type$ delta) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
if (handle.be == BE) {
|
||||
return SCOPED_MEMORY_ACCESS.getAndAdd$RawType$(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -368,7 +363,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndAddAcquire(VarHandle ob, Object obb, long base, $type$ delta) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
if (handle.be == BE) {
|
||||
return SCOPED_MEMORY_ACCESS.getAndAdd$RawType$Acquire(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -382,7 +377,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndAddRelease(VarHandle ob, Object obb, long base, $type$ delta) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
if (handle.be == BE) {
|
||||
return SCOPED_MEMORY_ACCESS.getAndAdd$RawType$Release(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -410,7 +405,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndBitwiseOr(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
if (handle.be == BE) {
|
||||
return SCOPED_MEMORY_ACCESS.getAndBitwiseOr$RawType$(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -424,7 +419,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndBitwiseOrRelease(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
if (handle.be == BE) {
|
||||
return SCOPED_MEMORY_ACCESS.getAndBitwiseOr$RawType$Release(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -438,7 +433,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndBitwiseOrAcquire(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
if (handle.be == BE) {
|
||||
return SCOPED_MEMORY_ACCESS.getAndBitwiseOr$RawType$Acquire(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -464,7 +459,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndBitwiseAnd(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
if (handle.be == BE) {
|
||||
return SCOPED_MEMORY_ACCESS.getAndBitwiseAnd$RawType$(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -478,7 +473,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndBitwiseAndRelease(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
if (handle.be == BE) {
|
||||
return SCOPED_MEMORY_ACCESS.getAndBitwiseAnd$RawType$Release(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -492,7 +487,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndBitwiseAndAcquire(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
if (handle.be == BE) {
|
||||
return SCOPED_MEMORY_ACCESS.getAndBitwiseAnd$RawType$Acquire(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -519,7 +514,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndBitwiseXor(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
if (handle.be == BE) {
|
||||
return SCOPED_MEMORY_ACCESS.getAndBitwiseXor$RawType$(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -533,7 +528,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndBitwiseXorRelease(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
if (handle.be == BE) {
|
||||
return SCOPED_MEMORY_ACCESS.getAndBitwiseXor$RawType$Release(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
@ -547,7 +542,7 @@ final class VarHandleSegmentAs$Type$s extends VarHandleSegmentViewBase {
|
||||
@ForceInline
|
||||
static $type$ getAndBitwiseXorAcquire(VarHandle ob, Object obb, long base, $type$ value) {
|
||||
VarHandleSegmentViewBase handle = (VarHandleSegmentViewBase)ob;
|
||||
AbstractMemorySegmentImpl bb = checkAddress(obb, base, handle.length, false);
|
||||
AbstractMemorySegmentImpl bb = checkReadOnly(obb, false);
|
||||
if (handle.be == BE) {
|
||||
return SCOPED_MEMORY_ACCESS.getAndBitwiseXor$RawType$Acquire(bb.sessionImpl(),
|
||||
bb.unsafeGetBase(),
|
||||
|
@ -357,10 +357,15 @@ public abstract sealed class AbstractMemorySegmentImpl
|
||||
}
|
||||
|
||||
@ForceInline
|
||||
public void checkAccess(long offset, long length, boolean readOnly) {
|
||||
public void checkReadOnly(boolean readOnly) {
|
||||
if (!readOnly && this.readOnly) {
|
||||
throw new IllegalArgumentException("Attempt to write a read-only segment");
|
||||
}
|
||||
}
|
||||
|
||||
@ForceInline
|
||||
public void checkAccess(long offset, long length, boolean readOnly) {
|
||||
checkReadOnly(readOnly);
|
||||
checkBounds(offset, length);
|
||||
}
|
||||
|
||||
@ -823,6 +828,7 @@ public abstract sealed class AbstractMemorySegmentImpl
|
||||
@ForceInline
|
||||
@Override
|
||||
public void set(AddressLayout layout, long offset, MemorySegment value) {
|
||||
Objects.requireNonNull(value);
|
||||
layout.varHandle().set((MemorySegment)this, offset, value);
|
||||
}
|
||||
|
||||
@ -948,6 +954,7 @@ public abstract sealed class AbstractMemorySegmentImpl
|
||||
@ForceInline
|
||||
@Override
|
||||
public void setAtIndex(AddressLayout layout, long index, MemorySegment value) {
|
||||
Objects.requireNonNull(value);
|
||||
Utils.checkElementAlignment(layout, "Layout alignment greater than its size");
|
||||
layout.varHandle().set((MemorySegment)this, index * layout.byteSize(), value);
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ public class LayoutPath {
|
||||
private static final MethodHandle MH_ADD_SCALED_OFFSET;
|
||||
private static final MethodHandle MH_SLICE;
|
||||
private static final MethodHandle MH_SLICE_LAYOUT;
|
||||
private static final MethodHandle MH_CHECK_ALIGN;
|
||||
private static final MethodHandle MH_CHECK_ENCL_LAYOUT;
|
||||
private static final MethodHandle MH_SEGMENT_RESIZE;
|
||||
private static final MethodHandle MH_ADD;
|
||||
|
||||
@ -77,7 +77,7 @@ public class LayoutPath {
|
||||
MethodType.methodType(MemorySegment.class, long.class, long.class));
|
||||
MH_SLICE_LAYOUT = lookup.findVirtual(MemorySegment.class, "asSlice",
|
||||
MethodType.methodType(MemorySegment.class, long.class, MemoryLayout.class));
|
||||
MH_CHECK_ALIGN = lookup.findStatic(LayoutPath.class, "checkAlign",
|
||||
MH_CHECK_ENCL_LAYOUT = lookup.findStatic(LayoutPath.class, "checkEnclosingLayout",
|
||||
MethodType.methodType(void.class, MemorySegment.class, long.class, MemoryLayout.class));
|
||||
MH_SEGMENT_RESIZE = lookup.findStatic(LayoutPath.class, "resizeSegment",
|
||||
MethodType.methodType(MemorySegment.class, MemorySegment.class, MemoryLayout.class));
|
||||
@ -205,16 +205,16 @@ public class LayoutPath {
|
||||
String.format("Path does not select a value layout: %s", breadcrumbs()));
|
||||
}
|
||||
|
||||
VarHandle handle = valueLayout.varHandle();
|
||||
VarHandle handle = Utils.makeSegmentViewVarHandle(valueLayout);
|
||||
handle = MethodHandles.collectCoordinates(handle, 1, offsetHandle());
|
||||
|
||||
// we only have to check the alignment of the root layout for the first dereference we do,
|
||||
// as each dereference checks the alignment of the target address when constructing its segment
|
||||
// (see Utils::longToAddress)
|
||||
if (derefAdapters.length == 0 && enclosing != null) {
|
||||
if (derefAdapters.length == 0) {
|
||||
// insert align check for the root layout on the initial MS + offset
|
||||
List<Class<?>> coordinateTypes = handle.coordinateTypes();
|
||||
MethodHandle alignCheck = MethodHandles.insertArguments(MH_CHECK_ALIGN, 2, rootLayout());
|
||||
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);
|
||||
@ -275,7 +275,7 @@ public class LayoutPath {
|
||||
if (enclosing != null) {
|
||||
// insert align check for the root layout on the initial MS + offset
|
||||
MethodType oldType = sliceHandle.type();
|
||||
MethodHandle alignCheck = MethodHandles.insertArguments(MH_CHECK_ALIGN, 2, rootLayout());
|
||||
MethodHandle alignCheck = MethodHandles.insertArguments(MH_CHECK_ENCL_LAYOUT, 2, rootLayout());
|
||||
sliceHandle = MethodHandles.collectArguments(sliceHandle, 0, alignCheck); // (MS, long, MS, long) -> MS
|
||||
int[] reorder = IntStream.concat(IntStream.of(0, 1), IntStream.range(0, oldType.parameterCount())).toArray();
|
||||
sliceHandle = MethodHandles.permuteArguments(sliceHandle, oldType, reorder); // (MS, long, ...) -> MS
|
||||
@ -284,11 +284,12 @@ public class LayoutPath {
|
||||
return sliceHandle;
|
||||
}
|
||||
|
||||
private static void checkAlign(MemorySegment segment, long offset, MemoryLayout constraint) {
|
||||
if (!((AbstractMemorySegmentImpl) segment).isAlignedForElement(offset, constraint)) {
|
||||
private static void checkEnclosingLayout(MemorySegment segment, long offset, MemoryLayout enclosing) {
|
||||
((AbstractMemorySegmentImpl)segment).checkAccess(offset, enclosing.byteSize(), 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, constraint.byteAlignment(), constraint, segment));
|
||||
, offset, enclosing.byteAlignment(), enclosing, segment));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2019, 2024, 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
|
||||
@ -37,8 +37,6 @@ import java.lang.invoke.MethodType;
|
||||
import java.lang.invoke.VarHandle;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import jdk.internal.access.SharedSecrets;
|
||||
@ -90,26 +88,6 @@ public final class Utils {
|
||||
}
|
||||
|
||||
public static VarHandle makeSegmentViewVarHandle(ValueLayout layout) {
|
||||
final class VarHandleCache {
|
||||
private static final Map<ValueLayout, VarHandle> HANDLE_MAP = new ConcurrentHashMap<>();
|
||||
|
||||
static VarHandle put(ValueLayout layout, VarHandle handle) {
|
||||
VarHandle prev = HANDLE_MAP.putIfAbsent(layout, handle);
|
||||
return prev != null ? prev : handle;
|
||||
}
|
||||
|
||||
static VarHandle get(ValueLayout layout) {
|
||||
return HANDLE_MAP.get(layout);
|
||||
}
|
||||
}
|
||||
layout = layout.withoutName(); // name doesn't matter
|
||||
// keep the addressee layout as it's used below
|
||||
|
||||
VarHandle handle = VarHandleCache.get(layout);
|
||||
if (handle != null) {
|
||||
return handle;
|
||||
}
|
||||
|
||||
Class<?> baseCarrier = layout.carrier();
|
||||
if (layout.carrier() == MemorySegment.class) {
|
||||
baseCarrier = switch ((int) ValueLayout.ADDRESS.byteSize()) {
|
||||
@ -121,7 +99,7 @@ public final class Utils {
|
||||
baseCarrier = byte.class;
|
||||
}
|
||||
|
||||
handle = SharedSecrets.getJavaLangInvokeAccess().memorySegmentViewHandle(baseCarrier,
|
||||
VarHandle handle = SharedSecrets.getJavaLangInvokeAccess().memorySegmentViewHandle(baseCarrier,
|
||||
layout.byteAlignment() - 1, layout.order());
|
||||
|
||||
if (layout.carrier() == boolean.class) {
|
||||
@ -133,7 +111,7 @@ public final class Utils {
|
||||
pointeeByteSize(addressLayout), pointeeByteAlign(addressLayout)),
|
||||
MethodType.methodType(MemorySegment.class, baseCarrier)));
|
||||
}
|
||||
return VarHandleCache.put(layout, handle);
|
||||
return handle;
|
||||
}
|
||||
|
||||
public static boolean byteToBoolean(byte b) {
|
||||
|
@ -188,6 +188,10 @@ public abstract sealed class AbstractLayout<L extends AbstractLayout<L> & Memory
|
||||
if (this instanceof ValueLayout vl && elements.length == 0) {
|
||||
return vl.varHandle(); // fast path
|
||||
}
|
||||
return varHandleInternal(elements);
|
||||
}
|
||||
|
||||
public VarHandle varHandleInternal(PathElement... elements) {
|
||||
return computePathOp(LayoutPath.rootPath((MemoryLayout) this), LayoutPath::dereferenceHandle,
|
||||
Set.of(), elements);
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2019, 2024, 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
|
||||
@ -38,8 +38,10 @@ import java.lang.foreign.MemorySegment;
|
||||
import java.lang.foreign.ValueLayout;
|
||||
import java.lang.invoke.VarHandle;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* A value layout. A value layout is used to model the memory layout associated with values of basic data types, such as <em>integral</em> types
|
||||
@ -157,9 +159,12 @@ public final class ValueLayouts {
|
||||
|
||||
@ForceInline
|
||||
public final VarHandle varHandle() {
|
||||
final class VarHandleCache {
|
||||
private static final Map<ValueLayout, VarHandle> HANDLE_MAP = new ConcurrentHashMap<>();
|
||||
}
|
||||
if (handle == null) {
|
||||
// this store to stable field is safe, because return value of 'makeMemoryAccessVarHandle' has stable identity
|
||||
handle = Utils.makeSegmentViewVarHandle(self());
|
||||
handle = VarHandleCache.HANDLE_MAP.computeIfAbsent(self().withoutName(), _ -> varHandleInternal());
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
@ -58,9 +58,8 @@ public class TestAccessModes {
|
||||
} catch (UnsupportedOperationException ex) {
|
||||
assertFalse(compatible);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// access is unaligned, but access mode is supported
|
||||
assertTrue(compatible ||
|
||||
(layout instanceof GroupLayout && segment.maxByteAlignment() < layout.byteAlignment()));
|
||||
// access is unaligned
|
||||
assertTrue(segment.maxByteAlignment() < layout.byteAlignment());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2021, 2024, 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
|
||||
@ -45,7 +45,7 @@ public class TestHeapAlignment {
|
||||
public void testHeapAlignment(MemorySegment segment, int align, Object val, Object arr, ValueLayout layout, Function<Object, MemorySegment> segmentFactory) {
|
||||
assertAligned(align, layout, () -> layout.varHandle().get(segment, 0L));
|
||||
assertAligned(align, layout, () -> layout.varHandle().set(segment, 0L, val));
|
||||
MemoryLayout seq = MemoryLayout.sequenceLayout(10, layout);
|
||||
MemoryLayout seq = MemoryLayout.sequenceLayout(1, layout);
|
||||
assertAligned(align, layout, () -> seq.varHandle(MemoryLayout.PathElement.sequenceElement()).get(segment, 0L, 0L));
|
||||
assertAligned(align, layout, () -> seq.varHandle(MemoryLayout.PathElement.sequenceElement()).set(segment, 0L, 0L, val));
|
||||
assertAligned(align, layout, () -> segment.spliterator(layout));
|
||||
|
@ -34,6 +34,7 @@ import org.testng.annotations.*;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.VarHandle;
|
||||
import java.lang.invoke.VarHandle.AccessMode;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -331,6 +332,57 @@ public class TestLayoutPaths {
|
||||
}
|
||||
}
|
||||
|
||||
@Test(dataProvider = "testLayouts")
|
||||
public void testVarHandleBadSegment(MemoryLayout layout, PathElement[] pathElements, long[] indexes,
|
||||
long expectedByteOffset) throws Throwable {
|
||||
MemoryLayout seqLayout = MemoryLayout.sequenceLayout(10, layout);
|
||||
PathElement[] seqPathElements = new PathElement[pathElements.length + 1];
|
||||
long[] seqIndexes = new long[indexes.length + 1];
|
||||
System.arraycopy(pathElements, 0, seqPathElements, 1, pathElements.length);
|
||||
System.arraycopy(indexes, 0, seqIndexes, 1, indexes.length);
|
||||
seqPathElements[0] = PathElement.sequenceElement();
|
||||
seqIndexes[0] = 0;
|
||||
MethodHandle getter_handle = seqLayout.varHandle(seqPathElements)
|
||||
.toMethodHandle(AccessMode.GET)
|
||||
.asSpreader(long[].class, seqIndexes.length);
|
||||
MemorySegment segment = Arena.ofAuto().allocate(layout);
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> getter_handle.invoke(segment, 0L, seqIndexes));
|
||||
}
|
||||
|
||||
@Test(dataProvider = "testLayouts")
|
||||
public void testSliceHandleBadSegment(MemoryLayout layout, PathElement[] pathElements, long[] indexes,
|
||||
long expectedByteOffset) throws Throwable {
|
||||
MemoryLayout seqLayout = MemoryLayout.sequenceLayout(10, layout);
|
||||
PathElement[] seqPathElements = new PathElement[pathElements.length + 1];
|
||||
long[] seqIndexes = new long[indexes.length + 1];
|
||||
System.arraycopy(pathElements, 0, seqPathElements, 1, pathElements.length);
|
||||
System.arraycopy(indexes, 0, seqIndexes, 1, indexes.length);
|
||||
seqPathElements[0] = PathElement.sequenceElement();
|
||||
seqIndexes[0] = 0;
|
||||
MethodHandle getter_handle = seqLayout.sliceHandle(seqPathElements)
|
||||
.asSpreader(long[].class, seqIndexes.length);
|
||||
MemorySegment segment = Arena.ofAuto().allocate(layout);
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> getter_handle.invoke(segment, 0L, seqIndexes));
|
||||
}
|
||||
|
||||
@Test(dataProvider = "testLayouts")
|
||||
public void testArrayElementVarHandleBadSegment(MemoryLayout layout, PathElement[] pathElements, long[] indexes,
|
||||
long expectedByteOffset) throws Throwable {
|
||||
MemoryLayout seqLayout = MemoryLayout.sequenceLayout(10, layout);
|
||||
PathElement[] seqPathElements = new PathElement[pathElements.length + 1];
|
||||
long[] seqIndexes = new long[indexes.length + 2];
|
||||
System.arraycopy(pathElements, 0, seqPathElements, 1, pathElements.length);
|
||||
System.arraycopy(indexes, 0, seqIndexes, 2, indexes.length);
|
||||
seqPathElements[0] = PathElement.sequenceElement();
|
||||
seqIndexes[0] = 0;
|
||||
seqIndexes[1] = 0;
|
||||
MethodHandle getter_handle = seqLayout.arrayElementVarHandle(seqPathElements)
|
||||
.toMethodHandle(AccessMode.GET)
|
||||
.asSpreader(long[].class, seqIndexes.length);
|
||||
MemorySegment segment = Arena.ofAuto().allocate(layout);
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> getter_handle.invoke(segment, 0L, seqIndexes));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashCodeCollision() {
|
||||
PathElement sequenceElement = PathElement.sequenceElement();
|
||||
|
Loading…
x
Reference in New Issue
Block a user