Skip to content

Commit

Permalink
8307181: MemoryLayout.structLayout uses undocumented strict alignment…
Browse files Browse the repository at this point in the history
… constraints

Reviewed-by: jvernee
  • Loading branch information
mcimadamore committed May 4, 2023
1 parent 3f1927a commit cecf817
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/java.base/share/classes/java/lang/foreign/MemoryLayout.java
Expand Up @@ -739,6 +739,28 @@ static SequenceLayout sequenceLayout(MemoryLayout elementLayout) {
* @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);
Expand Down

1 comment on commit cecf817

@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.