8307181: MemoryLayout.structLayout uses undocumented strict alignment constraints

Reviewed-by: jvernee
This commit is contained in:
Maurizio Cimadamore 2023-05-04 09:44:31 +00:00
parent 3f1927a7f3
commit cecf817f5e

View file

@ -739,6 +739,28 @@ public sealed interface MemoryLayout permits SequenceLayout, GroupLayout, Paddin
* @return a struct layout with the given member layouts.
* @throws IllegalArgumentException if the sum of the {@linkplain #bitSize() bit sizes} of the member layouts
* overflows.
* @throws IllegalArgumentException if a member layout in {@code elements} occurs at an offset (relative to the start
* of the struct layout) which is not compatible with its alignment constraint.
*
* @apiNote This factory does not automatically align element layouts, by inserting additional {@linkplain PaddingLayout
* padding layout} elements. As such, the following struct layout creation will fail with an exception:
*
* {@snippet lang = java:
* structLayout(JAVA_SHORT, JAVA_INT)
* }
*
* To avoid the exception, clients can either insert additional padding layout elements:
*
* {@snippet lang = java:
* structLayout(JAVA_SHORT, MemoryLayout.ofPadding(16), JAVA_INT)
* }
*
* Or, alternatively, they can use a member layout which features a smaller alignment constraint. This will result
* in a <em>packed</em> struct layout:
*
* {@snippet lang = java:
* structLayout(JAVA_SHORT, JAVA_INT.withBitAlignment(16))
* }
*/
static StructLayout structLayout(MemoryLayout... elements) {
Objects.requireNonNull(elements);