-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
AbstractMemorySegmentImpl.java
565 lines (488 loc) · 19.8 KB
/
AbstractMemorySegmentImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.foreign;
import jdk.incubator.foreign.*;
import jdk.internal.access.JavaNioAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.access.foreign.MemorySegmentProxy;
import jdk.internal.access.foreign.UnmapperProxy;
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.util.ArraysSupport;
import jdk.internal.vm.annotation.ForceInline;
import sun.security.action.GetPropertyAction;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static jdk.incubator.foreign.ValueLayout.JAVA_BYTE;
/**
* This abstract class provides an immutable implementation for the {@code MemorySegment} interface. This class contains information
* about the segment's spatial and temporal bounds; each memory segment implementation is associated with an owner thread which is set at creation time.
* Access to certain sensitive operations on the memory segment will fail with {@code IllegalStateException} if the
* segment is either in an invalid state (e.g. it has already been closed) or if access occurs from a thread other
* than the owner thread. See {@link ResourceScopeImpl} for more details on management of temporal bounds. Subclasses
* are defined for each memory segment kind, see {@link NativeMemorySegmentImpl}, {@link HeapMemorySegmentImpl} and
* {@link MappedMemorySegmentImpl}.
*/
public abstract non-sealed class AbstractMemorySegmentImpl extends MemorySegmentProxy implements MemorySegment, SegmentAllocator, Scoped {
private static final ScopedMemoryAccess SCOPED_MEMORY_ACCESS = ScopedMemoryAccess.getScopedMemoryAccess();
private static final boolean enableSmallSegments =
Boolean.parseBoolean(GetPropertyAction.privilegedGetProperty("jdk.incubator.foreign.SmallSegments", "true"));
static final int READ_ONLY = 1;
static final int SMALL = READ_ONLY << 1;
static final long NONCE = new Random().nextLong();
static final JavaNioAccess nioAccess = SharedSecrets.getJavaNioAccess();
final long length;
final int mask;
final ResourceScopeImpl scope;
@ForceInline
AbstractMemorySegmentImpl(long length, int mask, ResourceScopeImpl scope) {
this.length = length;
this.mask = mask;
this.scope = scope;
}
abstract long min();
abstract Object base();
abstract AbstractMemorySegmentImpl dup(long offset, long size, int mask, ResourceScopeImpl scope);
abstract ByteBuffer makeByteBuffer();
static int defaultAccessModes(long size) {
return (enableSmallSegments && size < Integer.MAX_VALUE) ?
SMALL : 0;
}
@Override
public AbstractMemorySegmentImpl asReadOnly() {
return dup(0, length, mask | READ_ONLY, scope);
}
@Override
public boolean isReadOnly() {
return isSet(READ_ONLY);
}
@Override
public AbstractMemorySegmentImpl asSlice(long offset, long newSize) {
checkBounds(offset, newSize);
return asSliceNoCheck(offset, newSize);
}
@Override
public AbstractMemorySegmentImpl asSlice(long offset) {
checkBounds(offset, 0);
return asSliceNoCheck(offset, length - offset);
}
private AbstractMemorySegmentImpl asSliceNoCheck(long offset, long newSize) {
return dup(offset, newSize, mask, scope);
}
@Override
public Spliterator<MemorySegment> spliterator(MemoryLayout elementLayout) {
Objects.requireNonNull(elementLayout);
if (elementLayout.byteSize() == 0) {
throw new IllegalArgumentException("Element layout size cannot be zero");
}
Utils.checkElementAlignment(elementLayout, "Element layout alignment greater than its size");
if (!isAlignedForElement(0, elementLayout)) {
throw new IllegalArgumentException("Incompatible alignment constraints");
}
if (!Utils.isAligned(byteSize(), elementLayout.byteSize())) {
throw new IllegalArgumentException("Segment size is not a multiple of layout size");
}
return new SegmentSplitter(elementLayout.byteSize(), byteSize() / elementLayout.byteSize(),
this);
}
@Override
public Stream<MemorySegment> elements(MemoryLayout elementLayout) {
return StreamSupport.stream(spliterator(elementLayout), false);
}
@Override
public final MemorySegment fill(byte value){
checkAccess(0, length, false);
SCOPED_MEMORY_ACCESS.setMemory(scope, base(), min(), length, value);
return this;
}
@Override
public MemorySegment allocate(long bytesSize, long bytesAlignment) {
return asSlice(0, bytesSize);
}
@Override
public long mismatch(MemorySegment other) {
AbstractMemorySegmentImpl that = (AbstractMemorySegmentImpl)Objects.requireNonNull(other);
final long thisSize = this.byteSize();
final long thatSize = that.byteSize();
final long length = Math.min(thisSize, thatSize);
this.checkAccess(0, length, true);
that.checkAccess(0, length, true);
if (this == other) {
checkValidState();
return -1;
}
long i = 0;
if (length > 7) {
if (get(JAVA_BYTE, 0) != that.get(JAVA_BYTE, 0)) {
return 0;
}
i = vectorizedMismatchLargeForBytes(scope, that.scope,
this.base(), this.min(),
that.base(), that.min(),
length);
if (i >= 0) {
return i;
}
long remaining = ~i;
assert remaining < 8 : "remaining greater than 7: " + remaining;
i = length - remaining;
}
for (; i < length; i++) {
if (get(JAVA_BYTE, i) != that.get(JAVA_BYTE, i)) {
return i;
}
}
return thisSize != thatSize ? length : -1;
}
/**
* Mismatch over long lengths.
*/
private static long vectorizedMismatchLargeForBytes(ResourceScopeImpl aScope, ResourceScopeImpl bScope,
Object a, long aOffset,
Object b, long bOffset,
long length) {
long off = 0;
long remaining = length;
int i, size;
boolean lastSubRange = false;
while (remaining > 7 && !lastSubRange) {
if (remaining > Integer.MAX_VALUE) {
size = Integer.MAX_VALUE;
} else {
size = (int) remaining;
lastSubRange = true;
}
i = SCOPED_MEMORY_ACCESS.vectorizedMismatch(aScope, bScope,
a, aOffset + off,
b, bOffset + off,
size, ArraysSupport.LOG2_ARRAY_BYTE_INDEX_SCALE);
if (i >= 0)
return off + i;
i = size - ~i;
off += i;
remaining -= i;
}
return ~remaining;
}
@Override
public MemoryAddress address() {
throw new UnsupportedOperationException("Cannot obtain address of on-heap segment");
}
@Override
public final ByteBuffer asByteBuffer() {
checkArraySize("ByteBuffer", 1);
ByteBuffer _bb = makeByteBuffer();
if (isSet(READ_ONLY)) {
//scope is IMMUTABLE - obtain a RO byte buffer
_bb = _bb.asReadOnlyBuffer();
}
return _bb;
}
@Override
public final long byteSize() {
return length;
}
public final boolean isAlive() {
return scope.isAlive();
}
public Thread ownerThread() {
return scope.ownerThread();
}
@Override
public boolean isMapped() {
return false;
}
@Override
public boolean isNative() {
return false;
}
@Override
public final MemorySegment asOverlappingSlice(MemorySegment other) {
AbstractMemorySegmentImpl that = (AbstractMemorySegmentImpl)Objects.requireNonNull(other);
if (base() == that.base()) { // both either native or heap
final long thisStart = this.min();
final long thatStart = that.min();
final long thisEnd = thisStart + this.byteSize();
final long thatEnd = thatStart + that.byteSize();
if (thisStart < thatEnd && thisEnd > thatStart) { //overlap occurs
long offsetToThat = this.segmentOffset(that);
long newOffset = offsetToThat >= 0 ? offsetToThat : 0;
return asSlice(newOffset, Math.min(this.byteSize() - newOffset, that.byteSize() + offsetToThat));
}
}
return null;
}
@Override
public final long segmentOffset(MemorySegment other) {
AbstractMemorySegmentImpl that = (AbstractMemorySegmentImpl) Objects.requireNonNull(other);
if (base() == that.base()) {
return that.min() - this.min();
}
throw new UnsupportedOperationException("Cannot compute offset from native to heap (or vice versa).");
}
@Override
public void load() {
throw new UnsupportedOperationException("Not a mapped segment");
}
@Override
public void unload() {
throw new UnsupportedOperationException("Not a mapped segment");
}
@Override
public boolean isLoaded() {
throw new UnsupportedOperationException("Not a mapped segment");
}
@Override
public void force() {
throw new UnsupportedOperationException("Not a mapped segment");
}
@Override
public final byte[] toArray(ValueLayout.OfByte elementLayout) {
return toArray(byte[].class, elementLayout, byte[]::new, MemorySegment::ofArray);
}
@Override
public final short[] toArray(ValueLayout.OfShort elementLayout) {
return toArray(short[].class, elementLayout, short[]::new, MemorySegment::ofArray);
}
@Override
public final char[] toArray(ValueLayout.OfChar elementLayout) {
return toArray(char[].class, elementLayout, char[]::new, MemorySegment::ofArray);
}
@Override
public final int[] toArray(ValueLayout.OfInt elementLayout) {
return toArray(int[].class, elementLayout, int[]::new, MemorySegment::ofArray);
}
@Override
public final float[] toArray(ValueLayout.OfFloat elementLayout) {
return toArray(float[].class, elementLayout, float[]::new, MemorySegment::ofArray);
}
@Override
public final long[] toArray(ValueLayout.OfLong elementLayout) {
return toArray(long[].class, elementLayout, long[]::new, MemorySegment::ofArray);
}
@Override
public final double[] toArray(ValueLayout.OfDouble elementLayout) {
return toArray(double[].class, elementLayout, double[]::new, MemorySegment::ofArray);
}
private <Z> Z toArray(Class<Z> arrayClass, ValueLayout elemLayout, IntFunction<Z> arrayFactory, Function<Z, MemorySegment> segmentFactory) {
int size = checkArraySize(arrayClass.getSimpleName(), (int)elemLayout.byteSize());
Z arr = arrayFactory.apply(size);
MemorySegment arrSegment = segmentFactory.apply(arr);
MemorySegment.copy(this, elemLayout, 0, arrSegment, elemLayout.withOrder(ByteOrder.nativeOrder()), 0, size);
return arr;
}
@Override
public boolean isSmall() {
return isSet(SMALL);
}
@Override
public void checkAccess(long offset, long length, boolean readOnly) {
if (!readOnly && isSet(READ_ONLY)) {
throw new UnsupportedOperationException("Attempt to write a read-only segment");
}
checkBounds(offset, length);
}
void checkValidState() {
try {
scope.checkValidState();
} catch (ScopedMemoryAccess.Scope.ScopedAccessError ex) {
throw new IllegalStateException("This segment is already closed");
}
}
@Override
public long unsafeGetOffset() {
return min();
}
@Override
public Object unsafeGetBase() {
return base();
}
// Helper methods
private boolean isSet(int mask) {
return (this.mask & mask) != 0;
}
@ForceInline
public final boolean isAlignedForElement(long offset, MemoryLayout layout) {
return (((unsafeGetOffset() + offset) | maxAlignMask()) & (layout.byteAlignment() - 1)) == 0;
}
private int checkArraySize(String typeName, int elemSize) {
if (!Utils.isAligned(length, elemSize)) {
throw new IllegalStateException(String.format("Segment size is not a multiple of %d. Size: %d", elemSize, length));
}
long arraySize = length / elemSize;
if (arraySize > (Integer.MAX_VALUE - 8)) { //conservative check
throw new IllegalStateException(String.format("Segment is too large to wrap as %s. Size: %d", typeName, length));
}
return (int)arraySize;
}
private void checkBounds(long offset, long length) {
if (isSmall() &&
offset <= Integer.MAX_VALUE && length <= Integer.MAX_VALUE &&
offset >= Integer.MIN_VALUE && length >= Integer.MIN_VALUE) {
checkBoundsSmall((int)offset, (int)length);
} else if (this != NativeMemorySegmentImpl.EVERYTHING) { // oob not possible for everything segment
if (
length < 0 ||
offset < 0 ||
offset > this.length - length) { // careful of overflow
throw outOfBoundException(offset, length);
}
}
}
@Override
public ResourceScopeImpl scope() {
return scope;
}
private void checkBoundsSmall(int offset, int length) {
if (length < 0 ||
offset < 0 ||
offset > (int)this.length - length) { // careful of overflow
throw outOfBoundException(offset, length);
}
}
private IndexOutOfBoundsException outOfBoundException(long offset, long length) {
return new IndexOutOfBoundsException(String.format("Out of bound access on segment %s; new offset = %d; new length = %d",
this, offset, length));
}
protected int id() {
//compute a stable and random id for this memory segment
return Math.abs(Objects.hash(base(), min(), NONCE));
}
static class SegmentSplitter implements Spliterator<MemorySegment> {
AbstractMemorySegmentImpl segment;
long elemCount;
final long elementSize;
long currentIndex;
SegmentSplitter(long elementSize, long elemCount, AbstractMemorySegmentImpl segment) {
this.segment = segment;
this.elementSize = elementSize;
this.elemCount = elemCount;
}
@Override
public SegmentSplitter trySplit() {
if (currentIndex == 0 && elemCount > 1) {
AbstractMemorySegmentImpl parent = segment;
long rem = elemCount % 2;
long split = elemCount / 2;
long lobound = split * elementSize;
long hibound = lobound + (rem * elementSize);
elemCount = split + rem;
segment = parent.asSliceNoCheck(lobound, hibound);
return new SegmentSplitter(elementSize, split, parent.asSliceNoCheck(0, lobound));
} else {
return null;
}
}
@Override
public boolean tryAdvance(Consumer<? super MemorySegment> action) {
Objects.requireNonNull(action);
if (currentIndex < elemCount) {
AbstractMemorySegmentImpl acquired = segment;
try {
action.accept(acquired.asSliceNoCheck(currentIndex * elementSize, elementSize));
} finally {
currentIndex++;
if (currentIndex == elemCount) {
segment = null;
}
}
return true;
} else {
return false;
}
}
@Override
public void forEachRemaining(Consumer<? super MemorySegment> action) {
Objects.requireNonNull(action);
if (currentIndex < elemCount) {
AbstractMemorySegmentImpl acquired = segment;
try {
if (acquired.isSmall()) {
int index = (int) currentIndex;
int limit = (int) elemCount;
int elemSize = (int) elementSize;
for (; index < limit; index++) {
action.accept(acquired.asSliceNoCheck(index * elemSize, elemSize));
}
} else {
for (long i = currentIndex ; i < elemCount ; i++) {
action.accept(acquired.asSliceNoCheck(i * elementSize, elementSize));
}
}
} finally {
currentIndex = elemCount;
segment = null;
}
}
}
@Override
public long estimateSize() {
return elemCount;
}
@Override
public int characteristics() {
return NONNULL | SUBSIZED | SIZED | IMMUTABLE | ORDERED;
}
}
// Object methods
@Override
public String toString() {
return "MemorySegment{ id=0x" + Long.toHexString(id()) + " limit: " + length + " }";
}
public static AbstractMemorySegmentImpl ofBuffer(ByteBuffer bb) {
Objects.requireNonNull(bb);
long bbAddress = nioAccess.getBufferAddress(bb);
Object base = nioAccess.getBufferBase(bb);
UnmapperProxy unmapper = nioAccess.unmapper(bb);
int pos = bb.position();
int limit = bb.limit();
int size = limit - pos;
AbstractMemorySegmentImpl bufferSegment = (AbstractMemorySegmentImpl)nioAccess.bufferSegment(bb);
final ResourceScopeImpl bufferScope;
int modes;
if (bufferSegment != null) {
bufferScope = bufferSegment.scope;
modes = bufferSegment.mask;
} else {
bufferScope = ResourceScopeImpl.GLOBAL;
modes = defaultAccessModes(size);
}
if (bb.isReadOnly()) {
modes |= READ_ONLY;
}
if (base != null) {
return new HeapMemorySegmentImpl.OfByte(bbAddress + pos, (byte[])base, size, modes);
} else if (unmapper == null) {
return new NativeMemorySegmentImpl(bbAddress + pos, size, modes, bufferScope);
} else {
return new MappedMemorySegmentImpl(bbAddress + pos, unmapper, size, modes, bufferScope);
}
}
}