diff --git a/src/java.base/share/classes/java/lang/foreign/MemoryLayout.java b/src/java.base/share/classes/java/lang/foreign/MemoryLayout.java
index 123c5897f26e5..372b10aab1389 100644
--- a/src/java.base/share/classes/java/lang/foreign/MemoryLayout.java
+++ b/src/java.base/share/classes/java/lang/foreign/MemoryLayout.java
@@ -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.
- *
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.
+ *
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 bigger
+ * than the size of the accessed layout (e.g. when accessing a struct member).
*
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.
@@ -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.
- *
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.
+ *
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 bigger
+ * than the size of the accessed layout (e.g. when accessing a struct member).
*
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.
@@ -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.
- *
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.
+ *
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 bigger
+ * than the size of the accessed layout (e.g. when accessing a struct member).
*
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.
diff --git a/src/java.base/share/classes/java/lang/invoke/VarHandleSegmentViewBase.java b/src/java.base/share/classes/java/lang/invoke/VarHandleSegmentViewBase.java
index 5cb71cf042456..31ec02c3d7c96 100644
--- a/src/java.base/share/classes/java/lang/invoke/VarHandleSegmentViewBase.java
+++ b/src/java.base/share/classes/java/lang/invoke/VarHandleSegmentViewBase.java
@@ -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);
}
diff --git a/src/java.base/share/classes/java/lang/invoke/VarHandles.java b/src/java.base/share/classes/java/lang/invoke/VarHandles.java
index 0a3932004476f..bd608619e58ec 100644
--- a/src/java.base/share/classes/java/lang/invoke/VarHandles.java
+++ b/src/java.base/share/classes/java/lang/invoke/VarHandles.java
@@ -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 @@ else if (viewComponentType == float.class) {
* 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 @@ static VarHandle memorySegmentViewHandle(Class> carrier, long alignmentMask,
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");
}
diff --git a/src/java.base/share/classes/java/lang/invoke/X-VarHandleSegmentView.java.template b/src/java.base/share/classes/java/lang/invoke/X-VarHandleSegmentView.java.template
index aa1c232e5df38..0c088cd5c4b45 100644
--- a/src/java.base/share/classes/java/lang/invoke/X-VarHandleSegmentView.java.template
+++ b/src/java.base/share/classes/java/lang/invoke/X-VarHandleSegmentView.java.template
@@ -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(),
diff --git a/src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java b/src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java
index 05e7f345b266e..f9f6ac2022a2b 100644
--- a/src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java
+++ b/src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java
@@ -357,10 +357,15 @@ private Z toArray(Class arrayClass, ValueLayout elemLayout, IntFunction> 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 MethodHandle sliceHandle() {
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 MethodHandle sliceHandle() {
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));
}
}
diff --git a/src/java.base/share/classes/jdk/internal/foreign/Utils.java b/src/java.base/share/classes/jdk/internal/foreign/Utils.java
index 6a081e23eac0d..f05666b88b691 100644
--- a/src/java.base/share/classes/jdk/internal/foreign/Utils.java
+++ b/src/java.base/share/classes/jdk/internal/foreign/Utils.java
@@ -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.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 static MemorySegment alignUp(MemorySegment ms, long alignment) {
}
public static VarHandle makeSegmentViewVarHandle(ValueLayout layout) {
- final class VarHandleCache {
- private static final Map 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 @@ static VarHandle get(ValueLayout layout) {
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 @@ static VarHandle get(ValueLayout layout) {
pointeeByteSize(addressLayout), pointeeByteAlign(addressLayout)),
MethodType.methodType(MemorySegment.class, baseCarrier)));
}
- return VarHandleCache.put(layout, handle);
+ return handle;
}
public static boolean byteToBoolean(byte b) {
diff --git a/src/java.base/share/classes/jdk/internal/foreign/layout/AbstractLayout.java b/src/java.base/share/classes/jdk/internal/foreign/layout/AbstractLayout.java
index f72009cf690af..796f0027158f1 100644
--- a/src/java.base/share/classes/jdk/internal/foreign/layout/AbstractLayout.java
+++ b/src/java.base/share/classes/jdk/internal/foreign/layout/AbstractLayout.java
@@ -188,6 +188,10 @@ public VarHandle varHandle(PathElement... elements) {
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);
}
diff --git a/src/java.base/share/classes/jdk/internal/foreign/layout/ValueLayouts.java b/src/java.base/share/classes/jdk/internal/foreign/layout/ValueLayouts.java
index 4b41c80f2ebd9..4d19879b01acd 100644
--- a/src/java.base/share/classes/jdk/internal/foreign/layout/ValueLayouts.java
+++ b/src/java.base/share/classes/jdk/internal/foreign/layout/ValueLayouts.java
@@ -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.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 integral types
@@ -157,9 +159,12 @@ static boolean isValidCarrier(Class> carrier) {
@ForceInline
public final VarHandle varHandle() {
+ final class VarHandleCache {
+ private static final Map 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;
}
diff --git a/test/jdk/java/foreign/TestAccessModes.java b/test/jdk/java/foreign/TestAccessModes.java
index 89662c95252ce..0cedcc9058e2b 100644
--- a/test/jdk/java/foreign/TestAccessModes.java
+++ b/test/jdk/java/foreign/TestAccessModes.java
@@ -58,9 +58,8 @@ public void testAccessModes(MemorySegment segment, MemoryLayout layout, AccessMo
} 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());
}
}
diff --git a/test/jdk/java/foreign/TestHeapAlignment.java b/test/jdk/java/foreign/TestHeapAlignment.java
index 99f09611914c1..cc8a95465108c 100644
--- a/test/jdk/java/foreign/TestHeapAlignment.java
+++ b/test/jdk/java/foreign/TestHeapAlignment.java
@@ -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