Skip to content

Commit

Permalink
8308276: Change layout API to work with bytes, not bits
Browse files Browse the repository at this point in the history
Reviewed-by: psandoz, pminborg
  • Loading branch information
mcimadamore committed May 22, 2023
1 parent 91aeb5d commit 5fc9b57
Show file tree
Hide file tree
Showing 93 changed files with 527 additions and 721 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/**
* A value layout used to model the address of some region of memory. The carrier associated with an address layout is
* {@code MemorySegment.class}. The size and alignment of an address layout are platform dependent
* (e.g. on a 64-bit platform, the size and alignment of an address layout are set to 64 bits).
* (e.g. on a 64-bit platform, the size and alignment of an address layout are set to 8 bytes).
* <p>
* An address layout may optionally feature a {@linkplain #targetLayout() target layout}. An address layout with
* target layout {@code T} can be used to model the address of a region of memory whose layout is {@code T}.
Expand Down Expand Up @@ -74,7 +74,7 @@ public sealed interface AddressLayout extends ValueLayout permits ValueLayouts.O
* {@inheritDoc}
*/
@Override
AddressLayout withBitAlignment(long bitAlignment);
AddressLayout withByteAlignment(long byteAlignment);

/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public sealed interface GroupLayout extends MemoryLayout permits StructLayout, U
/**
* {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @throws IllegalArgumentException if {@code bitAlignment} is less than {@code M}, where {@code M} is the maximum alignment
* @throws IllegalArgumentException if {@code byteAlignment} is less than {@code M}, where {@code M} is the maximum alignment
* constraint in any of the member layouts associated with this group layout.
*/
@Override
GroupLayout withBitAlignment(long bitAlignment);
GroupLayout withByteAlignment(long byteAlignment);
}
150 changes: 32 additions & 118 deletions src/java.base/share/classes/java/lang/foreign/MemoryLayout.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
* to read a pointer from some memory segment. This can be done via the
* {@linkplain MemorySegment#get(AddressLayout, long)} access method. This method accepts an
* {@linkplain AddressLayout address layout} (e.g. {@link ValueLayout#ADDRESS}), the layout of the pointer
* to be read. For instance on a 64-bit platform, the size of an address layout is 64 bits. The access operation
* to be read. For instance on a 64-bit platform, the size of an address layout is 8 bytes. The access operation
* also accepts an offset, expressed in bytes, which indicates the position (relative to the start of the memory segment)
* at which the pointer is stored. The access operation returns a zero-length native memory segment, backed by a region
* of memory whose starting address is the 64-bit value read at the specified offset.
Expand Down Expand Up @@ -470,7 +470,7 @@ public sealed interface MemorySegment permits AbstractMemorySegmentImpl {
* @return the element spliterator for this segment
* @throws IllegalArgumentException if {@code elementLayout.byteSize() == 0}.
* @throws IllegalArgumentException if {@code byteSize() % elementLayout.byteSize() != 0}.
* @throws IllegalArgumentException if {@code elementLayout.bitSize() % elementLayout.bitAlignment() != 0}.
* @throws IllegalArgumentException if {@code elementLayout.byteSize() % elementLayout.byteAlignment() != 0}.
* @throws IllegalArgumentException if this segment is <a href="MemorySegment.html#segment-alignment">incompatible
* with the alignment constraint</a> in the provided layout.
*/
Expand All @@ -487,7 +487,7 @@ public sealed interface MemorySegment permits AbstractMemorySegmentImpl {
* @return a sequential {@code Stream} over disjoint slices in this segment.
* @throws IllegalArgumentException if {@code elementLayout.byteSize() == 0}.
* @throws IllegalArgumentException if {@code byteSize() % elementLayout.byteSize() != 0}.
* @throws IllegalArgumentException if {@code elementLayout.bitSize() % elementLayout.bitAlignment() != 0}.
* @throws IllegalArgumentException if {@code elementLayout.byteSize() % elementLayout.byteAlignment() != 0}.
* @throws IllegalArgumentException if this segment is <a href="MemorySegment.html#segment-alignment">incompatible
* with the alignment constraint</a> in the provided layout.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,5 @@ public sealed interface PaddingLayout extends MemoryLayout permits PaddingLayout
* {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
PaddingLayout withBitAlignment(long bitAlignment);
PaddingLayout withByteAlignment(long byteAlignment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public sealed interface SequenceLayout extends MemoryLayout permits SequenceLayo
/**
* {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @throws IllegalArgumentException if {@code bitAlignment < elementLayout().bitAlignment()}.
* @throws IllegalArgumentException if {@code byteAlignment < elementLayout().byteAlignment()}.
*/
SequenceLayout withBitAlignment(long bitAlignment);
SequenceLayout withByteAlignment(long byteAlignment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ public sealed interface StructLayout extends GroupLayout permits StructLayoutImp
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
StructLayout withBitAlignment(long bitAlignment);
StructLayout withByteAlignment(long byteAlignment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ public sealed interface UnionLayout extends GroupLayout permits UnionLayoutImpl
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
UnionLayout withBitAlignment(long bitAlignment);
UnionLayout withByteAlignment(long byteAlignment);
}
68 changes: 34 additions & 34 deletions src/java.base/share/classes/java/lang/foreign/ValueLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* <em>integral</em> values (either signed or unsigned), <em>floating-point</em> values and
* <em>address</em> values.
* <p>
* Each value layout has a size, an alignment (in bits),
* Each value layout has a size, an alignment (both expressed in bytes),
* a {@linkplain ByteOrder byte order}, and a <em>carrier</em>, that is, the Java type that should be used when
* {@linkplain MemorySegment#get(OfInt, long) accessing} a region of memory using the value layout.
* <p>
Expand Down Expand Up @@ -129,7 +129,7 @@ public sealed interface ValueLayout extends MemoryLayout permits
* featuring {@code shape.length + 1}
* {@code long} coordinates.
* @throws IllegalArgumentException if {@code shape[i] < 0}, for at least one index {@code i}.
* @throws UnsupportedOperationException if {@code bitAlignment() > bitSize()}.
* @throws UnsupportedOperationException if {@code byteAlignment() > byteSize()}.
* @see MethodHandles#memorySegmentViewVarHandle
* @see MemoryLayout#varHandle(PathElement...)
* @see SequenceLayout
Expand All @@ -152,7 +152,7 @@ public sealed interface ValueLayout extends MemoryLayout permits
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
ValueLayout withBitAlignment(long bitAlignment);
ValueLayout withByteAlignment(long byteAlignment);

/**
* A value layout whose carrier is {@code boolean.class}.
Expand Down Expand Up @@ -180,7 +180,7 @@ sealed interface OfBoolean extends ValueLayout permits ValueLayouts.OfBooleanImp
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
OfBoolean withBitAlignment(long bitAlignment);
OfBoolean withByteAlignment(long byteAlignment);

/**
* {@inheritDoc}
Expand Down Expand Up @@ -216,7 +216,7 @@ sealed interface OfByte extends ValueLayout permits ValueLayouts.OfByteImpl {
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
OfByte withBitAlignment(long bitAlignment);
OfByte withByteAlignment(long byteAlignment);

/**
* {@inheritDoc}
Expand Down Expand Up @@ -253,7 +253,7 @@ sealed interface OfChar extends ValueLayout permits ValueLayouts.OfCharImpl {
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
OfChar withBitAlignment(long bitAlignment);
OfChar withByteAlignment(long byteAlignment);

/**
* {@inheritDoc}
Expand Down Expand Up @@ -290,7 +290,7 @@ sealed interface OfShort extends ValueLayout permits ValueLayouts.OfShortImpl {
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
OfShort withBitAlignment(long bitAlignment);
OfShort withByteAlignment(long byteAlignment);

/**
* {@inheritDoc}
Expand Down Expand Up @@ -327,7 +327,7 @@ sealed interface OfInt extends ValueLayout permits ValueLayouts.OfIntImpl {
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
OfInt withBitAlignment(long bitAlignment);
OfInt withByteAlignment(long byteAlignment);

/**
* {@inheritDoc}
Expand Down Expand Up @@ -363,7 +363,7 @@ sealed interface OfFloat extends ValueLayout permits ValueLayouts.OfFloatImpl {
* {@inheritDoc}
*/
@Override
OfFloat withBitAlignment(long bitAlignment);
OfFloat withByteAlignment(long byteAlignment);

/**
* {@inheritDoc}
Expand Down Expand Up @@ -400,7 +400,7 @@ sealed interface OfLong extends ValueLayout permits ValueLayouts.OfLongImpl {
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
OfLong withBitAlignment(long bitAlignment);
OfLong withByteAlignment(long byteAlignment);

/**
* {@inheritDoc}
Expand Down Expand Up @@ -437,7 +437,7 @@ sealed interface OfDouble extends ValueLayout permits ValueLayouts.OfDoubleImpl
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
OfDouble withBitAlignment(long bitAlignment);
OfDouble withByteAlignment(long byteAlignment);

/**
* {@inheritDoc}
Expand All @@ -449,56 +449,56 @@ sealed interface OfDouble extends ValueLayout permits ValueLayouts.OfDoubleImpl

/**
* A value layout constant whose size is the same as that of a machine address ({@code size_t}),
* bit alignment set to {@code sizeof(size_t) * 8}, byte order set to {@link ByteOrder#nativeOrder()}.
* byte alignment set to {@code sizeof(size_t)}, byte order set to {@link ByteOrder#nativeOrder()}.
*/
AddressLayout ADDRESS = ValueLayouts.OfAddressImpl.of(ByteOrder.nativeOrder());

/**
* A value layout constant whose size is the same as that of a Java {@code byte},
* bit alignment set to 8, and byte order set to {@link ByteOrder#nativeOrder()}.
* byte alignment set to 1, and byte order set to {@link ByteOrder#nativeOrder()}.
*/
OfByte JAVA_BYTE = ValueLayouts.OfByteImpl.of(ByteOrder.nativeOrder());

/**
* A value layout constant whose size is the same as that of a Java {@code boolean},
* bit alignment set to 8, and byte order set to {@link ByteOrder#nativeOrder()}.
* byte alignment set to 1, and byte order set to {@link ByteOrder#nativeOrder()}.
*/
OfBoolean JAVA_BOOLEAN = ValueLayouts.OfBooleanImpl.of(ByteOrder.nativeOrder());

/**
* A value layout constant whose size is the same as that of a Java {@code char},
* bit alignment set to 16, and byte order set to {@link ByteOrder#nativeOrder()}.
* byte alignment set to 2, and byte order set to {@link ByteOrder#nativeOrder()}.
*/
OfChar JAVA_CHAR = ValueLayouts.OfCharImpl.of(ByteOrder.nativeOrder());

/**
* A value layout constant whose size is the same as that of a Java {@code short},
* bit alignment set to 16, and byte order set to {@link ByteOrder#nativeOrder()}.
* byte alignment set to 2, and byte order set to {@link ByteOrder#nativeOrder()}.
*/
OfShort JAVA_SHORT = ValueLayouts.OfShortImpl.of(ByteOrder.nativeOrder());

/**
* A value layout constant whose size is the same as that of a Java {@code int},
* bit alignment set to 32, and byte order set to {@link ByteOrder#nativeOrder()}.
* byte alignment set to 4, and byte order set to {@link ByteOrder#nativeOrder()}.
*/
OfInt JAVA_INT = ValueLayouts.OfIntImpl.of(ByteOrder.nativeOrder());

/**
* A value layout constant whose size is the same as that of a Java {@code long},
* (platform-dependent) bit alignment set to {@code ADDRESS.bitSize()},
* (platform-dependent) byte alignment set to {@code ADDRESS.byteSize()},
* and byte order set to {@link ByteOrder#nativeOrder()}.
*/
OfLong JAVA_LONG = ValueLayouts.OfLongImpl.of(ByteOrder.nativeOrder());

/**
* A value layout constant whose size is the same as that of a Java {@code float},
* bit alignment set to 32, and byte order set to {@link ByteOrder#nativeOrder()}.
* byte alignment set to 4, and byte order set to {@link ByteOrder#nativeOrder()}.
*/
OfFloat JAVA_FLOAT = ValueLayouts.OfFloatImpl.of(ByteOrder.nativeOrder());

/**
* A value layout constant whose size is the same as that of a Java {@code double},
* (platform-dependent) bit alignment set to {@code ADDRESS.bitSize()},
* (platform-dependent) byte alignment set to {@code ADDRESS.byteSize()},
* and byte order set to {@link ByteOrder#nativeOrder()}.
*/
OfDouble JAVA_DOUBLE = ValueLayouts.OfDoubleImpl.of(ByteOrder.nativeOrder());
Expand All @@ -508,83 +508,83 @@ sealed interface OfDouble extends ValueLayout permits ValueLayouts.OfDoubleImpl
* and byte order set to {@link ByteOrder#nativeOrder()}.
* Equivalent to the following code:
* {@snippet lang=java :
* ADDRESS.withBitAlignment(8);
* ADDRESS.withByteAlignment(1);
* }
* @apiNote Care should be taken when using unaligned value layouts as they may induce
* performance and portability issues.
*/
AddressLayout ADDRESS_UNALIGNED = ADDRESS.withBitAlignment(8);
AddressLayout ADDRESS_UNALIGNED = ADDRESS.withByteAlignment(1);

/**
* An unaligned value layout constant whose size is the same as that of a Java {@code char}
* and byte order set to {@link ByteOrder#nativeOrder()}.
* Equivalent to the following code:
* {@snippet lang=java :
* JAVA_CHAR.withBitAlignment(8);
* JAVA_CHAR.withByteAlignment(1);
* }
* @apiNote Care should be taken when using unaligned value layouts as they may induce
* performance and portability issues.
*/
OfChar JAVA_CHAR_UNALIGNED = JAVA_CHAR.withBitAlignment(8);
OfChar JAVA_CHAR_UNALIGNED = JAVA_CHAR.withByteAlignment(1);

/**
* An unaligned value layout constant whose size is the same as that of a Java {@code short}
* and byte order set to {@link ByteOrder#nativeOrder()}.
* Equivalent to the following code:
* {@snippet lang=java :
* JAVA_SHORT.withBitAlignment(8);
* JAVA_SHORT.withByteAlignment(1);
* }
* @apiNote Care should be taken when using unaligned value layouts as they may induce
* performance and portability issues.
*/
OfShort JAVA_SHORT_UNALIGNED = JAVA_SHORT.withBitAlignment(8);
OfShort JAVA_SHORT_UNALIGNED = JAVA_SHORT.withByteAlignment(1);

/**
* An unaligned value layout constant whose size is the same as that of a Java {@code int}
* and byte order set to {@link ByteOrder#nativeOrder()}.
* Equivalent to the following code:
* {@snippet lang=java :
* JAVA_INT.withBitAlignment(8);
* JAVA_INT.withByteAlignment(1);
* }
* @apiNote Care should be taken when using unaligned value layouts as they may induce
* performance and portability issues.
*/
OfInt JAVA_INT_UNALIGNED = JAVA_INT.withBitAlignment(8);
OfInt JAVA_INT_UNALIGNED = JAVA_INT.withByteAlignment(1);

/**
* An unaligned value layout constant whose size is the same as that of a Java {@code long}
* and byte order set to {@link ByteOrder#nativeOrder()}.
* Equivalent to the following code:
* {@snippet lang=java :
* JAVA_LONG.withBitAlignment(8);
* JAVA_LONG.withByteAlignment(1);
* }
* @apiNote Care should be taken when using unaligned value layouts as they may induce
* performance and portability issues.
*/
OfLong JAVA_LONG_UNALIGNED = JAVA_LONG.withBitAlignment(8);
OfLong JAVA_LONG_UNALIGNED = JAVA_LONG.withByteAlignment(1);

/**
* An unaligned value layout constant whose size is the same as that of a Java {@code float}
* and byte order set to {@link ByteOrder#nativeOrder()}.
* Equivalent to the following code:
* {@snippet lang=java :
* JAVA_FLOAT.withBitAlignment(8);
* JAVA_FLOAT.withByteAlignment(1);
* }
* @apiNote Care should be taken when using unaligned value layouts as they may induce
* performance and portability issues.
*/
OfFloat JAVA_FLOAT_UNALIGNED = JAVA_FLOAT.withBitAlignment(8);
OfFloat JAVA_FLOAT_UNALIGNED = JAVA_FLOAT.withByteAlignment(1);

/**
* An unaligned value layout constant whose size is the same as that of a Java {@code double}
* and byte order set to {@link ByteOrder#nativeOrder()}.
* Equivalent to the following code:
* {@snippet lang=java :
* JAVA_DOUBLE.withBitAlignment(8);
* JAVA_DOUBLE.withByteAlignment(1);
* }
* @apiNote Care should be taken when using unaligned value layouts as they may induce
* performance and portability issues.
*/
OfDouble JAVA_DOUBLE_UNALIGNED = JAVA_DOUBLE.withBitAlignment(8);
OfDouble JAVA_DOUBLE_UNALIGNED = JAVA_DOUBLE.withByteAlignment(1);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7964,7 +7964,7 @@ private static MethodType tableSwitchChecks(MethodHandle defaultCase, MethodHand
* <p>As an example, consider the memory layout expressed by a {@link GroupLayout} instance constructed as follows:
* {@snippet lang="java" :
* GroupLayout seq = java.lang.foreign.MemoryLayout.structLayout(
* MemoryLayout.paddingLayout(32),
* MemoryLayout.paddingLayout(4),
* ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN).withName("value")
* );
* }
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/jdk/internal/foreign/CABI.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ private static CABI computeCurrent() {
if (ForeignLinkerSupport.isSupported()) {
// figure out the ABI based on the platform
String arch = StaticProperty.osArch();
long addressSize = ADDRESS.bitSize();
long addressSize = ADDRESS.byteSize();
// might be running in a 32-bit VM on a 64-bit platform.
// addressSize will be correctly 32
if ((arch.equals("amd64") || arch.equals("x86_64")) && addressSize == 64) {
if ((arch.equals("amd64") || arch.equals("x86_64")) && addressSize == 8) {
if (OperatingSystem.isWindows()) {
return WIN_64;
} else {
Expand Down
Loading

1 comment on commit 5fc9b57

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