RFR: 8326172: Dubious claim on long[]/double[] alignment in MemorySegment javadoc

Maurizio Cimadamore mcimadamore at openjdk.org
Mon Feb 26 15:24:09 UTC 2024


On Mon, 26 Feb 2024 13:24:11 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:

> This patch changes the alignment for `JAVA_LONG` and `JAVA_DOUBLE` to 8, regardless of the underlying platform. This means that atomic access modes work on memory segments wrapping `long[]` or `double[]`, as they already do when using `MethodHandless::arrayAccessVarHandle`.
> 
> After discussion, we came to the conclusion that it is reasonable for the JDK to require the elements of a `long[]` and `double[]` to be 8 byte aligned. It is ultimately up to the JDK to set these requirements, which are for the VM to implement.
> 
> I was seeing a stack overflow when running test/jdk/java/foreign/stackwalk/TestReentrantUpcalls.java on x86, so I've lowered the recursion to 50 (which is still more than enough I think).
> 
> Testing: `jdk_foreign` on x64 Windows, x64 Windows + fallback linker, and x86 Linux (uses fallback linker)

src/java.base/share/classes/java/lang/foreign/MemorySegment.java line 328:

> 326:  *     physical address 1010.</li>
> 327:  * <li>The starting physical address of a {@code long[]} array will be 8-byte aligned
> 328:  *     (e.g. 1000), so that successive long elements occur at 8-byte aligned addresses

I believe there might be other changes required. I see the following sentences in the javadoc:


 * In other words, heap segments feature a (platform-dependent) <em>maximum</em>
 * alignment which is derived from the size of the elements of the Java array backing the
 * segment, as shown in the following table:
 ```
 

 * In such circumstances, clients have two options. They can use a heap segment backed
 * by a different array type (e.g. {@code long[]}), capable of supporting greater maximum
 * alignment. More specifically, the maximum alignment associated with {@code long[]} is
 * set to {@code ValueLayout.JAVA_LONG.byteAlignment()} which is a platform-dependent
 * value (set to {@code ValueLayout.ADDRESS.byteSize()}). That is, {@code long[]}) is
 * guaranteed to provide at least 8-byte alignment in 64-bit platforms, but only 4-byte
 * alignment in 32-bit platforms:
 ```
 
 ```
 * In practice, the Java runtime lays out arrays in memory so that each n-byte element
 * occurs at an n-byte aligned physical address (except for {@code long[]} and
 * {@code double[]}, where alignment is platform-dependent, as explained below).
 ```

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/18007#discussion_r1502771056


More information about the core-libs-dev mailing list