1
1
/*
2
- * Copyright (c) 2019, 2022 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2019, 2023 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
25
25
*/
26
26
package jdk .internal .foreign .layout ;
27
27
28
- import jdk .internal .foreign .Utils ;
29
- import jdk .internal .vm .annotation .ForceInline ;
30
- import jdk .internal .vm .annotation .Stable ;
31
-
32
28
import java .lang .foreign .GroupLayout ;
33
29
import java .lang .foreign .MemoryLayout ;
34
30
import java .lang .foreign .SequenceLayout ;
41
37
public abstract sealed class AbstractLayout <L extends AbstractLayout <L > & MemoryLayout >
42
38
permits AbstractGroupLayout , PaddingLayoutImpl , SequenceLayoutImpl , ValueLayouts .AbstractValueLayout {
43
39
44
- private final long bitSize ;
45
- private final long bitAlignment ;
40
+ private final long byteSize ;
41
+ private final long byteAlignment ;
46
42
private final Optional <String > name ;
47
- @ Stable
48
- private long byteSize ;
49
43
50
44
AbstractLayout (long bitSize , long bitAlignment , Optional <String > name ) {
51
- this .bitSize = bitSize ;
52
- this .bitAlignment = bitAlignment ;
53
- this .name = name ;
45
+ this .byteSize = MemoryLayoutUtil . requireBitSizeValid ( bitSize ) / 8 ;
46
+ this .byteAlignment = requirePowerOfTwoAndGreaterOrEqualToEight ( bitAlignment ) / 8 ;
47
+ this .name = Objects . requireNonNull ( name ) ;
54
48
}
55
49
56
50
public final L withName (String name ) {
57
51
Objects .requireNonNull (name );
58
- return dup (bitAlignment , Optional .of (name ));
52
+ return dup (bitAlignment () , Optional .of (name ));
59
53
}
60
54
61
55
public final Optional <String > name () {
62
56
return name ;
63
57
}
64
58
65
59
public final L withBitAlignment (long bitAlignment ) {
66
- checkAlignment (bitAlignment );
67
60
return dup (bitAlignment , name );
68
61
}
69
62
70
63
public final long bitAlignment () {
71
- return bitAlignment ;
64
+ return byteAlignment * 8 ;
65
+ }
66
+
67
+ public final long byteAlignment () {
68
+ return byteAlignment ;
72
69
}
73
70
74
- @ ForceInline
75
71
public final long byteSize () {
76
- if (byteSize == 0 ) {
77
- byteSize = Utils .bitsToBytesOrThrow (bitSize (),
78
- () -> new UnsupportedOperationException ("Cannot compute byte size; bit size is not a multiple of 8" ));
79
- }
80
72
return byteSize ;
81
73
}
82
74
83
75
public final long bitSize () {
84
- return bitSize ;
76
+ return byteSize * 8 ;
85
77
}
86
78
87
79
public boolean hasNaturalAlignment () {
88
- return bitSize == bitAlignment ;
80
+ return byteSize == byteAlignment ;
89
81
}
90
82
91
83
// the following methods have to copy the same Javadoc as in MemoryLayout, or subclasses will just show
@@ -96,7 +88,7 @@ public boolean hasNaturalAlignment() {
96
88
*/
97
89
@ Override
98
90
public int hashCode () {
99
- return Objects .hash (name , bitSize , bitAlignment );
91
+ return Objects .hash (name , byteSize , byteAlignment );
100
92
}
101
93
102
94
/**
@@ -124,13 +116,14 @@ public boolean equals(Object other) {
124
116
125
117
return other instanceof AbstractLayout <?> otherLayout &&
126
118
name .equals (otherLayout .name ) &&
127
- bitSize == otherLayout .bitSize &&
128
- bitAlignment == otherLayout .bitAlignment ;
119
+ byteSize == otherLayout .byteSize &&
120
+ byteAlignment == otherLayout .byteAlignment ;
129
121
}
130
122
131
123
/**
132
124
* {@return the string representation of this layout}
133
125
*/
126
+ @ Override
134
127
public abstract String toString ();
135
128
136
129
abstract L dup (long alignment , Optional <String > name );
@@ -140,17 +133,17 @@ String decorateLayoutString(String s) {
140
133
s = String .format ("%s(%s)" , s , name ().get ());
141
134
}
142
135
if (!hasNaturalAlignment ()) {
143
- s = bitAlignment + "%" + s ;
136
+ s = bitAlignment () + "%" + s ;
144
137
}
145
138
return s ;
146
139
}
147
140
148
- private static void checkAlignment (long alignmentBitCount ) {
149
- if (((alignmentBitCount & (alignmentBitCount - 1 )) != 0L ) || //alignment must be a power of two
150
- (alignmentBitCount < 8 )) { //alignment must be greater than 8
151
- throw new IllegalArgumentException ("Invalid alignment: " + alignmentBitCount );
141
+ private static long requirePowerOfTwoAndGreaterOrEqualToEight (long value ) {
142
+ if (((value & (value - 1 )) != 0L ) || // value must be a power of two
143
+ (value < 8 )) { // value must be greater or equal to 8
144
+ throw new IllegalArgumentException ("Invalid alignment: " + value );
152
145
}
146
+ return value ;
153
147
}
154
148
155
-
156
149
}
0 commit comments