Skip to content

Commit 5fc9b57

Browse files
committed
8308276: Change layout API to work with bytes, not bits
Reviewed-by: psandoz, pminborg
1 parent 91aeb5d commit 5fc9b57

File tree

93 files changed

+527
-721
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+527
-721
lines changed

src/java.base/share/classes/java/lang/foreign/AddressLayout.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
/**
3939
* A value layout used to model the address of some region of memory. The carrier associated with an address layout is
4040
* {@code MemorySegment.class}. The size and alignment of an address layout are platform dependent
41-
* (e.g. on a 64-bit platform, the size and alignment of an address layout are set to 64 bits).
41+
* (e.g. on a 64-bit platform, the size and alignment of an address layout are set to 8 bytes).
4242
* <p>
4343
* An address layout may optionally feature a {@linkplain #targetLayout() target layout}. An address layout with
4444
* target layout {@code T} can be used to model the address of a region of memory whose layout is {@code T}.
@@ -74,7 +74,7 @@ public sealed interface AddressLayout extends ValueLayout permits ValueLayouts.O
7474
* {@inheritDoc}
7575
*/
7676
@Override
77-
AddressLayout withBitAlignment(long bitAlignment);
77+
AddressLayout withByteAlignment(long byteAlignment);
7878

7979
/**
8080
* {@inheritDoc}

src/java.base/share/classes/java/lang/foreign/GroupLayout.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public sealed interface GroupLayout extends MemoryLayout permits StructLayout, U
6868
/**
6969
* {@inheritDoc}
7070
* @throws IllegalArgumentException {@inheritDoc}
71-
* @throws IllegalArgumentException if {@code bitAlignment} is less than {@code M}, where {@code M} is the maximum alignment
71+
* @throws IllegalArgumentException if {@code byteAlignment} is less than {@code M}, where {@code M} is the maximum alignment
7272
* constraint in any of the member layouts associated with this group layout.
7373
*/
7474
@Override
75-
GroupLayout withBitAlignment(long bitAlignment);
75+
GroupLayout withByteAlignment(long byteAlignment);
7676
}

src/java.base/share/classes/java/lang/foreign/MemoryLayout.java

Lines changed: 32 additions & 118 deletions
Large diffs are not rendered by default.

src/java.base/share/classes/java/lang/foreign/MemorySegment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@
379379
* to read a pointer from some memory segment. This can be done via the
380380
* {@linkplain MemorySegment#get(AddressLayout, long)} access method. This method accepts an
381381
* {@linkplain AddressLayout address layout} (e.g. {@link ValueLayout#ADDRESS}), the layout of the pointer
382-
* to be read. For instance on a 64-bit platform, the size of an address layout is 64 bits. The access operation
382+
* to be read. For instance on a 64-bit platform, the size of an address layout is 8 bytes. The access operation
383383
* also accepts an offset, expressed in bytes, which indicates the position (relative to the start of the memory segment)
384384
* at which the pointer is stored. The access operation returns a zero-length native memory segment, backed by a region
385385
* of memory whose starting address is the 64-bit value read at the specified offset.
@@ -470,7 +470,7 @@ public sealed interface MemorySegment permits AbstractMemorySegmentImpl {
470470
* @return the element spliterator for this segment
471471
* @throws IllegalArgumentException if {@code elementLayout.byteSize() == 0}.
472472
* @throws IllegalArgumentException if {@code byteSize() % elementLayout.byteSize() != 0}.
473-
* @throws IllegalArgumentException if {@code elementLayout.bitSize() % elementLayout.bitAlignment() != 0}.
473+
* @throws IllegalArgumentException if {@code elementLayout.byteSize() % elementLayout.byteAlignment() != 0}.
474474
* @throws IllegalArgumentException if this segment is <a href="MemorySegment.html#segment-alignment">incompatible
475475
* with the alignment constraint</a> in the provided layout.
476476
*/
@@ -487,7 +487,7 @@ public sealed interface MemorySegment permits AbstractMemorySegmentImpl {
487487
* @return a sequential {@code Stream} over disjoint slices in this segment.
488488
* @throws IllegalArgumentException if {@code elementLayout.byteSize() == 0}.
489489
* @throws IllegalArgumentException if {@code byteSize() % elementLayout.byteSize() != 0}.
490-
* @throws IllegalArgumentException if {@code elementLayout.bitSize() % elementLayout.bitAlignment() != 0}.
490+
* @throws IllegalArgumentException if {@code elementLayout.byteSize() % elementLayout.byteAlignment() != 0}.
491491
* @throws IllegalArgumentException if this segment is <a href="MemorySegment.html#segment-alignment">incompatible
492492
* with the alignment constraint</a> in the provided layout.
493493
*/

src/java.base/share/classes/java/lang/foreign/PaddingLayout.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,5 @@ public sealed interface PaddingLayout extends MemoryLayout permits PaddingLayout
5656
* {@inheritDoc}
5757
* @throws IllegalArgumentException {@inheritDoc}
5858
*/
59-
@Override
60-
PaddingLayout withBitAlignment(long bitAlignment);
59+
PaddingLayout withByteAlignment(long byteAlignment);
6160
}

src/java.base/share/classes/java/lang/foreign/SequenceLayout.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public sealed interface SequenceLayout extends MemoryLayout permits SequenceLayo
143143
/**
144144
* {@inheritDoc}
145145
* @throws IllegalArgumentException {@inheritDoc}
146-
* @throws IllegalArgumentException if {@code bitAlignment < elementLayout().bitAlignment()}.
146+
* @throws IllegalArgumentException if {@code byteAlignment < elementLayout().byteAlignment()}.
147147
*/
148-
SequenceLayout withBitAlignment(long bitAlignment);
148+
SequenceLayout withByteAlignment(long byteAlignment);
149149
}

src/java.base/share/classes/java/lang/foreign/StructLayout.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ public sealed interface StructLayout extends GroupLayout permits StructLayoutImp
5656
* @throws IllegalArgumentException {@inheritDoc}
5757
*/
5858
@Override
59-
StructLayout withBitAlignment(long bitAlignment);
59+
StructLayout withByteAlignment(long byteAlignment);
6060
}

src/java.base/share/classes/java/lang/foreign/UnionLayout.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ public sealed interface UnionLayout extends GroupLayout permits UnionLayoutImpl
5656
* @throws IllegalArgumentException {@inheritDoc}
5757
*/
5858
@Override
59-
UnionLayout withBitAlignment(long bitAlignment);
59+
UnionLayout withByteAlignment(long byteAlignment);
6060
}

src/java.base/share/classes/java/lang/foreign/ValueLayout.java

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* <em>integral</em> values (either signed or unsigned), <em>floating-point</em> values and
3838
* <em>address</em> values.
3939
* <p>
40-
* Each value layout has a size, an alignment (in bits),
40+
* Each value layout has a size, an alignment (both expressed in bytes),
4141
* a {@linkplain ByteOrder byte order}, and a <em>carrier</em>, that is, the Java type that should be used when
4242
* {@linkplain MemorySegment#get(OfInt, long) accessing} a region of memory using the value layout.
4343
* <p>
@@ -129,7 +129,7 @@ public sealed interface ValueLayout extends MemoryLayout permits
129129
* featuring {@code shape.length + 1}
130130
* {@code long} coordinates.
131131
* @throws IllegalArgumentException if {@code shape[i] < 0}, for at least one index {@code i}.
132-
* @throws UnsupportedOperationException if {@code bitAlignment() > bitSize()}.
132+
* @throws UnsupportedOperationException if {@code byteAlignment() > byteSize()}.
133133
* @see MethodHandles#memorySegmentViewVarHandle
134134
* @see MemoryLayout#varHandle(PathElement...)
135135
* @see SequenceLayout
@@ -152,7 +152,7 @@ public sealed interface ValueLayout extends MemoryLayout permits
152152
* @throws IllegalArgumentException {@inheritDoc}
153153
*/
154154
@Override
155-
ValueLayout withBitAlignment(long bitAlignment);
155+
ValueLayout withByteAlignment(long byteAlignment);
156156

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

185185
/**
186186
* {@inheritDoc}
@@ -216,7 +216,7 @@ sealed interface OfByte extends ValueLayout permits ValueLayouts.OfByteImpl {
216216
* @throws IllegalArgumentException {@inheritDoc}
217217
*/
218218
@Override
219-
OfByte withBitAlignment(long bitAlignment);
219+
OfByte withByteAlignment(long byteAlignment);
220220

221221
/**
222222
* {@inheritDoc}
@@ -253,7 +253,7 @@ sealed interface OfChar extends ValueLayout permits ValueLayouts.OfCharImpl {
253253
* @throws IllegalArgumentException {@inheritDoc}
254254
*/
255255
@Override
256-
OfChar withBitAlignment(long bitAlignment);
256+
OfChar withByteAlignment(long byteAlignment);
257257

258258
/**
259259
* {@inheritDoc}
@@ -290,7 +290,7 @@ sealed interface OfShort extends ValueLayout permits ValueLayouts.OfShortImpl {
290290
* @throws IllegalArgumentException {@inheritDoc}
291291
*/
292292
@Override
293-
OfShort withBitAlignment(long bitAlignment);
293+
OfShort withByteAlignment(long byteAlignment);
294294

295295
/**
296296
* {@inheritDoc}
@@ -327,7 +327,7 @@ sealed interface OfInt extends ValueLayout permits ValueLayouts.OfIntImpl {
327327
* @throws IllegalArgumentException {@inheritDoc}
328328
*/
329329
@Override
330-
OfInt withBitAlignment(long bitAlignment);
330+
OfInt withByteAlignment(long byteAlignment);
331331

332332
/**
333333
* {@inheritDoc}
@@ -363,7 +363,7 @@ sealed interface OfFloat extends ValueLayout permits ValueLayouts.OfFloatImpl {
363363
* {@inheritDoc}
364364
*/
365365
@Override
366-
OfFloat withBitAlignment(long bitAlignment);
366+
OfFloat withByteAlignment(long byteAlignment);
367367

368368
/**
369369
* {@inheritDoc}
@@ -400,7 +400,7 @@ sealed interface OfLong extends ValueLayout permits ValueLayouts.OfLongImpl {
400400
* @throws IllegalArgumentException {@inheritDoc}
401401
*/
402402
@Override
403-
OfLong withBitAlignment(long bitAlignment);
403+
OfLong withByteAlignment(long byteAlignment);
404404

405405
/**
406406
* {@inheritDoc}
@@ -437,7 +437,7 @@ sealed interface OfDouble extends ValueLayout permits ValueLayouts.OfDoubleImpl
437437
* @throws IllegalArgumentException {@inheritDoc}
438438
*/
439439
@Override
440-
OfDouble withBitAlignment(long bitAlignment);
440+
OfDouble withByteAlignment(long byteAlignment);
441441

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

590590
}

src/java.base/share/classes/java/lang/invoke/MethodHandles.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7964,7 +7964,7 @@ private static MethodType tableSwitchChecks(MethodHandle defaultCase, MethodHand
79647964
* <p>As an example, consider the memory layout expressed by a {@link GroupLayout} instance constructed as follows:
79657965
* {@snippet lang="java" :
79667966
* GroupLayout seq = java.lang.foreign.MemoryLayout.structLayout(
7967-
* MemoryLayout.paddingLayout(32),
7967+
* MemoryLayout.paddingLayout(4),
79687968
* ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN).withName("value")
79697969
* );
79707970
* }

0 commit comments

Comments
 (0)