Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct padding of struct layouts #171

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions test/jdk/java/foreign/CallGeneratorHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

/*
* ===========================================================================
* (c) Copyright IBM Corp. 2021, 2022 All Rights Reserved
* (c) Copyright IBM Corp. 2021, 2023 All Rights Reserved
* ===========================================================================
*/

Expand Down Expand Up @@ -148,21 +148,24 @@ MemoryLayout layout(List<StructFieldType> fields) {
long align = 0;
for (StructFieldType field : fields) {
MemoryLayout l = field.layout();
/* Follow the calculation of padding in JDK19 which is based on
* the alignment of layout rather than the bit size.
/* Use the padding calculation from JDK19+ which is based
* on the alignment of the layout rather than its bit size.
*/
long padding = offset % l.bitAlignment();
if (padding != 0) {
long alignment = l.bitAlignment();
long padding = alignment - (offset % alignment);
if (padding != alignment) {
layouts.add(MemoryLayout.paddingLayout(padding));
offset += padding;
}
layouts.add(l.withName("field" + offset));
align = Math.max(align, l.bitAlignment());
offset += l.bitSize();
}
long padding = offset % align;
if (padding != 0) {
layouts.add(MemoryLayout.paddingLayout(padding));
if (align != 0) {
long padding = align - (offset % align);
if (padding != align) {
layouts.add(MemoryLayout.paddingLayout(padding));
}
}
return MemoryLayout.structLayout(layouts.toArray(new MemoryLayout[0]));
} else {
Expand Down