Skip to content

Commit

Permalink
8318966: Some methods make promises about Java array element alignmen…
Browse files Browse the repository at this point in the history
…t that are too strong

Reviewed-by: psandoz, mcimadamore
  • Loading branch information
JornVernee committed Feb 14, 2024
1 parent 737b4c5 commit 9c852df
Show file tree
Hide file tree
Showing 19 changed files with 1,693 additions and 4,521 deletions.
62 changes: 19 additions & 43 deletions src/java.base/share/classes/java/lang/invoke/MethodHandles.java
Original file line number Diff line number Diff line change
Expand Up @@ -4513,48 +4513,18 @@ public static VarHandle arrayElementVarHandle(Class<?> arrayClass) throws Illega
* or greater than the {@code byte[]} array length minus the size (in bytes)
* of {@code T}.
* <p>
* Access of bytes at an index may be aligned or misaligned for {@code T},
* with respect to the underlying memory address, {@code A} say, associated
* with the array and index.
* If access is misaligned then access for anything other than the
* {@code get} and {@code set} access modes will result in an
* {@code IllegalStateException}. In such cases atomic access is only
* guaranteed with respect to the largest power of two that divides the GCD
* of {@code A} and the size (in bytes) of {@code T}.
* If access is aligned then following access modes are supported and are
* guaranteed to support atomic access:
* <ul>
* <li>read write access modes for all {@code T}, with the exception of
* access modes {@code get} and {@code set} for {@code long} and
* {@code double} on 32-bit platforms.
* <li>atomic update access modes for {@code int}, {@code long},
* {@code float} or {@code double}.
* (Future major platform releases of the JDK may support additional
* types for certain currently unsupported access modes.)
* <li>numeric atomic update access modes for {@code int} and {@code long}.
* (Future major platform releases of the JDK may support additional
* numeric types for certain currently unsupported access modes.)
* <li>bitwise atomic update access modes for {@code int} and {@code long}.
* (Future major platform releases of the JDK may support additional
* numeric types for certain currently unsupported access modes.)
* </ul>
* <p>
* Misaligned access, and therefore atomicity guarantees, may be determined
* for {@code byte[]} arrays without operating on a specific array. Given
* an {@code index}, {@code T} and its corresponding boxed type,
* {@code T_BOX}, misalignment may be determined as follows:
* <pre>{@code
* int sizeOfT = T_BOX.BYTES; // size in bytes of T
* int misalignedAtZeroIndex = ByteBuffer.wrap(new byte[0]).
* alignmentOffset(0, sizeOfT);
* int misalignedAtIndex = (misalignedAtZeroIndex + index) % sizeOfT;
* boolean isMisaligned = misalignedAtIndex != 0;
* }</pre>
* <p>
* If the variable type is {@code float} or {@code double} then atomic
* update access modes compare values using their bitwise representation
* (see {@link Float#floatToRawIntBits} and
* {@link Double#doubleToRawLongBits}, respectively).
* Only plain {@linkplain VarHandle.AccessMode#GET get} and {@linkplain VarHandle.AccessMode#SET set}
* access modes are supported by the returned var handle. For all other access modes, an
* {@link UnsupportedOperationException} will be thrown.
*
* @apiNote if access modes other than plain access are required, clients should
* consider using off-heap memory through
* {@linkplain java.nio.ByteBuffer#allocateDirect(int) direct byte buffers} or
* off-heap {@linkplain java.lang.foreign.MemorySegment memory segments},
* or memory segments backed by a
* {@linkplain java.lang.foreign.MemorySegment#ofArray(long[]) {@code long[]}},
* for which stronger alignment guarantees can be made.
*
* @param viewArrayClass the view array class, with a component type of
* type {@code T}
* @param byteOrder the endianness of the view array elements, as
Expand Down Expand Up @@ -4600,7 +4570,13 @@ public static VarHandle byteArrayViewVarHandle(Class<?> viewArrayClass,
* or greater than the {@code ByteBuffer} limit minus the size (in bytes) of
* {@code T}.
* <p>
* Access of bytes at an index may be aligned or misaligned for {@code T},
* For heap byte buffers, access is always unaligned. As a result, only the plain
* {@linkplain VarHandle.AccessMode#GET get}
* and {@linkplain VarHandle.AccessMode#SET set} access modes are supported by the
* returned var handle. For all other access modes, an {@link IllegalStateException}
* will be thrown.
* <p>
* For direct buffers only, access of bytes at an index may be aligned or misaligned for {@code T},
* with respect to the underlying memory address, {@code A} say, associated
* with the {@code ByteBuffer} and index.
* If access is misaligned then access for anything other than the
Expand Down
Loading

1 comment on commit 9c852df

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.