39
39
/**
40
40
* Implementation for heap memory segments. An heap memory segment is composed by an offset and
41
41
* a base object (typically an array). To enhance performances, the access to the base object needs to feature
42
- * sharp type information, as well as sharp null-check information. For this reason, the factories for heap segments
43
- * use a lambda to implement the base object accessor , so that the type information will remain sharp (e.g.
44
- * the static compiler will generate specialized base accessor for us) .
42
+ * sharp type information, as well as sharp null-check information. For this reason, many concrete subclasses
43
+ * of {@link HeapMemorySegmentImpl} are defined (e.g. {@link OfFloat} , so that each subclass can override the
44
+ * {@link HeapMemorySegmentImpl#base()} method so that it returns an array of the correct (sharp) type .
45
45
*/
46
- public class HeapMemorySegmentImpl <H > extends AbstractMemorySegmentImpl {
46
+ public abstract class HeapMemorySegmentImpl <H > extends AbstractMemorySegmentImpl {
47
47
48
48
private static final Unsafe UNSAFE = Unsafe .getUnsafe ();
49
49
private static final int BYTE_ARR_BASE = UNSAFE .arrayBaseOffset (byte [].class );
50
50
51
51
final long offset ;
52
- final Supplier < H > baseProvider ;
52
+ final H base ;
53
53
54
54
@ ForceInline
55
- HeapMemorySegmentImpl (long offset , Supplier < H > baseProvider , long length , int mask , MemoryScope scope ) {
55
+ HeapMemorySegmentImpl (long offset , H base , long length , int mask , MemoryScope scope ) {
56
56
super (length , mask , scope );
57
57
this .offset = offset ;
58
- this .baseProvider = baseProvider ;
58
+ this .base = base ;
59
59
}
60
60
61
61
@ Override
62
- H base () {
63
- return Objects .requireNonNull (baseProvider .get ());
64
- }
62
+ abstract H base ();
65
63
66
64
@ Override
67
65
long min () {
68
66
return offset ;
69
67
}
70
68
71
69
@ Override
72
- HeapMemorySegmentImpl <H > dup (long offset , long size , int mask , MemoryScope scope ) {
73
- return new HeapMemorySegmentImpl <>(this .offset + offset , baseProvider , size , mask , scope );
74
- }
70
+ abstract HeapMemorySegmentImpl <H > dup (long offset , long size , int mask , MemoryScope scope );
75
71
76
72
@ Override
77
73
ByteBuffer makeByteBuffer () {
@@ -84,44 +80,164 @@ ByteBuffer makeByteBuffer() {
84
80
85
81
// factories
86
82
87
- public static MemorySegment makeArraySegment (byte [] arr ) {
88
- return makeHeapSegment (() -> arr , arr .length ,
89
- Unsafe .ARRAY_BYTE_BASE_OFFSET , Unsafe .ARRAY_BYTE_INDEX_SCALE );
90
- }
83
+ public static class OfByte extends HeapMemorySegmentImpl <byte []> {
84
+
85
+ OfByte (long offset , byte [] base , long length , int mask , MemoryScope scope ) {
86
+ super (offset , base , length , mask , scope );
87
+ }
88
+
89
+ @ Override
90
+ OfByte dup (long offset , long size , int mask , MemoryScope scope ) {
91
+ return new OfByte (this .offset + offset , base , size , mask , scope );
92
+ }
91
93
92
- public static MemorySegment makeArraySegment (char [] arr ) {
93
- return makeHeapSegment (() -> arr , arr .length ,
94
- Unsafe .ARRAY_CHAR_BASE_OFFSET , Unsafe .ARRAY_CHAR_INDEX_SCALE );
94
+ @ Override
95
+ byte [] base () {
96
+ return Objects .requireNonNull (base );
97
+ }
98
+
99
+ public static MemorySegment fromArray (byte [] arr ) {
100
+ int byteSize = arr .length * Unsafe .ARRAY_BYTE_INDEX_SCALE ;
101
+ MemoryScope scope = MemoryScope .createConfined (null , MemoryScope .DUMMY_CLEANUP_ACTION , null );
102
+ return new OfByte (Unsafe .ARRAY_BYTE_BASE_OFFSET , arr , byteSize , defaultAccessModes (byteSize ), scope );
103
+ }
95
104
}
96
105
97
- public static MemorySegment makeArraySegment (short [] arr ) {
98
- return makeHeapSegment (() -> arr , arr .length ,
99
- Unsafe .ARRAY_SHORT_BASE_OFFSET , Unsafe .ARRAY_SHORT_INDEX_SCALE );
106
+ public static class OfChar extends HeapMemorySegmentImpl <char []> {
107
+
108
+ OfChar (long offset , char [] base , long length , int mask , MemoryScope scope ) {
109
+ super (offset , base , length , mask , scope );
110
+ }
111
+
112
+ @ Override
113
+ OfChar dup (long offset , long size , int mask , MemoryScope scope ) {
114
+ return new OfChar (this .offset + offset , base , size , mask , scope );
115
+ }
116
+
117
+ @ Override
118
+ char [] base () {
119
+ return Objects .requireNonNull (base );
120
+ }
121
+
122
+ public static MemorySegment fromArray (char [] arr ) {
123
+ int byteSize = arr .length * Unsafe .ARRAY_CHAR_INDEX_SCALE ;
124
+ MemoryScope scope = MemoryScope .createConfined (null , MemoryScope .DUMMY_CLEANUP_ACTION , null );
125
+ return new OfChar (Unsafe .ARRAY_CHAR_BASE_OFFSET , arr , byteSize , defaultAccessModes (byteSize ), scope );
126
+ }
100
127
}
101
128
102
- public static MemorySegment makeArraySegment (int [] arr ) {
103
- return makeHeapSegment (() -> arr , arr .length ,
104
- Unsafe .ARRAY_INT_BASE_OFFSET , Unsafe .ARRAY_INT_INDEX_SCALE );
129
+ public static class OfShort extends HeapMemorySegmentImpl <short []> {
130
+
131
+ OfShort (long offset , short [] base , long length , int mask , MemoryScope scope ) {
132
+ super (offset , base , length , mask , scope );
133
+ }
134
+
135
+ @ Override
136
+ OfShort dup (long offset , long size , int mask , MemoryScope scope ) {
137
+ return new OfShort (this .offset + offset , base , size , mask , scope );
138
+ }
139
+
140
+ @ Override
141
+ short [] base () {
142
+ return Objects .requireNonNull (base );
143
+ }
144
+
145
+ public static MemorySegment fromArray (short [] arr ) {
146
+ int byteSize = arr .length * Unsafe .ARRAY_SHORT_INDEX_SCALE ;
147
+ MemoryScope scope = MemoryScope .createConfined (null , MemoryScope .DUMMY_CLEANUP_ACTION , null );
148
+ return new OfShort (Unsafe .ARRAY_SHORT_BASE_OFFSET , arr , byteSize , defaultAccessModes (byteSize ), scope );
149
+ }
105
150
}
106
151
107
- public static MemorySegment makeArraySegment (long [] arr ) {
108
- return makeHeapSegment (() -> arr , arr .length ,
109
- Unsafe .ARRAY_LONG_BASE_OFFSET , Unsafe .ARRAY_LONG_INDEX_SCALE );
152
+ public static class OfInt extends HeapMemorySegmentImpl <int []> {
153
+
154
+ OfInt (long offset , int [] base , long length , int mask , MemoryScope scope ) {
155
+ super (offset , base , length , mask , scope );
156
+ }
157
+
158
+ @ Override
159
+ OfInt dup (long offset , long size , int mask , MemoryScope scope ) {
160
+ return new OfInt (this .offset + offset , base , size , mask , scope );
161
+ }
162
+
163
+ @ Override
164
+ int [] base () {
165
+ return Objects .requireNonNull (base );
166
+ }
167
+
168
+ public static MemorySegment fromArray (int [] arr ) {
169
+ int byteSize = arr .length * Unsafe .ARRAY_INT_INDEX_SCALE ;
170
+ MemoryScope scope = MemoryScope .createConfined (null , MemoryScope .DUMMY_CLEANUP_ACTION , null );
171
+ return new OfInt (Unsafe .ARRAY_INT_BASE_OFFSET , arr , byteSize , defaultAccessModes (byteSize ), scope );
172
+ }
110
173
}
111
174
112
- public static MemorySegment makeArraySegment (float [] arr ) {
113
- return makeHeapSegment (() -> arr , arr .length ,
114
- Unsafe .ARRAY_FLOAT_BASE_OFFSET , Unsafe .ARRAY_FLOAT_INDEX_SCALE );
175
+ public static class OfLong extends HeapMemorySegmentImpl <long []> {
176
+
177
+ OfLong (long offset , long [] base , long length , int mask , MemoryScope scope ) {
178
+ super (offset , base , length , mask , scope );
179
+ }
180
+
181
+ @ Override
182
+ OfLong dup (long offset , long size , int mask , MemoryScope scope ) {
183
+ return new OfLong (this .offset + offset , base , size , mask , scope );
184
+ }
185
+
186
+ @ Override
187
+ long [] base () {
188
+ return Objects .requireNonNull (base );
189
+ }
190
+
191
+ public static MemorySegment fromArray (long [] arr ) {
192
+ int byteSize = arr .length * Unsafe .ARRAY_LONG_INDEX_SCALE ;
193
+ MemoryScope scope = MemoryScope .createConfined (null , MemoryScope .DUMMY_CLEANUP_ACTION , null );
194
+ return new OfLong (Unsafe .ARRAY_LONG_BASE_OFFSET , arr , byteSize , defaultAccessModes (byteSize ), scope );
195
+ }
115
196
}
116
197
117
- public static MemorySegment makeArraySegment (double [] arr ) {
118
- return makeHeapSegment (() -> arr , arr .length ,
119
- Unsafe .ARRAY_DOUBLE_BASE_OFFSET , Unsafe .ARRAY_DOUBLE_INDEX_SCALE );
198
+ public static class OfFloat extends HeapMemorySegmentImpl <float []> {
199
+
200
+ OfFloat (long offset , float [] base , long length , int mask , MemoryScope scope ) {
201
+ super (offset , base , length , mask , scope );
202
+ }
203
+
204
+ @ Override
205
+ OfFloat dup (long offset , long size , int mask , MemoryScope scope ) {
206
+ return new OfFloat (this .offset + offset , base , size , mask , scope );
207
+ }
208
+
209
+ @ Override
210
+ float [] base () {
211
+ return Objects .requireNonNull (base );
212
+ }
213
+
214
+ public static MemorySegment fromArray (float [] arr ) {
215
+ int byteSize = arr .length * Unsafe .ARRAY_FLOAT_INDEX_SCALE ;
216
+ MemoryScope scope = MemoryScope .createConfined (null , MemoryScope .DUMMY_CLEANUP_ACTION , null );
217
+ return new OfFloat (Unsafe .ARRAY_FLOAT_BASE_OFFSET , arr , byteSize , defaultAccessModes (byteSize ), scope );
218
+ }
120
219
}
121
220
122
- static <Z > HeapMemorySegmentImpl <Z > makeHeapSegment (Supplier <Z > obj , int length , int base , int scale ) {
123
- int byteSize = length * scale ;
124
- MemoryScope scope = MemoryScope .createConfined (null , MemoryScope .DUMMY_CLEANUP_ACTION , null );
125
- return new HeapMemorySegmentImpl <>(base , obj , byteSize , defaultAccessModes (byteSize ), scope );
221
+ public static class OfDouble extends HeapMemorySegmentImpl <double []> {
222
+
223
+ OfDouble (long offset , double [] base , long length , int mask , MemoryScope scope ) {
224
+ super (offset , base , length , mask , scope );
225
+ }
226
+
227
+ @ Override
228
+ OfDouble dup (long offset , long size , int mask , MemoryScope scope ) {
229
+ return new OfDouble (this .offset + offset , base , size , mask , scope );
230
+ }
231
+
232
+ @ Override
233
+ double [] base () {
234
+ return Objects .requireNonNull (base );
235
+ }
236
+
237
+ public static MemorySegment fromArray (double [] arr ) {
238
+ int byteSize = arr .length * Unsafe .ARRAY_DOUBLE_INDEX_SCALE ;
239
+ MemoryScope scope = MemoryScope .createConfined (null , MemoryScope .DUMMY_CLEANUP_ACTION , null );
240
+ return new OfDouble (Unsafe .ARRAY_DOUBLE_BASE_OFFSET , arr , byteSize , defaultAccessModes (byteSize ), scope );
241
+ }
126
242
}
127
243
}
0 commit comments