Remove work-around for slice-of-heap-segment-based-buffer JDK bug that got fixed

This commit is contained in:
Chris Vest 2021-01-18 12:11:03 +01:00
parent e22b57ddcd
commit 1ff8b4bf5a

View File

@ -62,9 +62,6 @@ class MemSegBuf extends RcSupport<Buf, MemSegBuf> implements Buf, ReadableCompon
private final AllocatorControl alloc; private final AllocatorControl alloc;
private final boolean isSendable; private final boolean isSendable;
// TODO remove baseOffset when JDK bug is fixed (slices of heap buffers)
// See https://mail.openjdk.java.net/pipermail/panama-dev/2021-January/011810.html
private final int baseOffset;
private MemorySegment seg; private MemorySegment seg;
private MemorySegment wseg; private MemorySegment wseg;
private ByteOrder order; private ByteOrder order;
@ -72,17 +69,15 @@ class MemSegBuf extends RcSupport<Buf, MemSegBuf> implements Buf, ReadableCompon
private int woff; private int woff;
MemSegBuf(MemorySegment segmet, Drop<MemSegBuf> drop, AllocatorControl alloc) { MemSegBuf(MemorySegment segmet, Drop<MemSegBuf> drop, AllocatorControl alloc) {
this(segmet, drop, alloc, true, 0); this(segmet, drop, alloc, true);
} }
private MemSegBuf(MemorySegment segment, Drop<MemSegBuf> drop, AllocatorControl alloc, boolean isSendable, private MemSegBuf(MemorySegment segment, Drop<MemSegBuf> drop, AllocatorControl alloc, boolean isSendable) {
int baseOffset) {
super(drop); super(drop);
this.alloc = alloc; this.alloc = alloc;
seg = segment; seg = segment;
wseg = segment; wseg = segment;
this.isSendable = isSendable; this.isSendable = isSendable;
this.baseOffset = baseOffset;
order = ByteOrder.nativeOrder(); order = ByteOrder.nativeOrder();
} }
@ -162,17 +157,16 @@ class MemSegBuf extends RcSupport<Buf, MemSegBuf> implements Buf, ReadableCompon
@Override @Override
public ByteBuffer readableBuffer() { public ByteBuffer readableBuffer() {
var buffer = seg.asByteBuffer(); var buffer = seg.asByteBuffer();
int base = baseOffset;
if (buffer.isDirect()) { if (buffer.isDirect()) {
// TODO Remove this when the slicing of shared, native segments JDK bug is fixed. // TODO Remove this when the slicing of shared, native segments JDK bug is fixed.
// See https://mail.openjdk.java.net/pipermail/panama-dev/2021-January/011810.html
ByteBuffer tmp = ByteBuffer.allocateDirect(buffer.capacity()); ByteBuffer tmp = ByteBuffer.allocateDirect(buffer.capacity());
tmp.put(buffer); tmp.put(buffer);
buffer = tmp.position(0); buffer = tmp.position(0);
base = 0; // TODO native memory segments do not have the buffer-of-slice bug.
} }
buffer = buffer.asReadOnlyBuffer(); buffer = buffer.asReadOnlyBuffer();
// TODO avoid slicing and just set position+limit when JDK bug is fixed. // TODO avoid slicing and just set position+limit when JDK bug is fixed.
return buffer.slice(base + readerOffset(), readableBytes()).order(order); return buffer.slice(readerOffset(), readableBytes()).order(order);
} }
@Override @Override
@ -203,7 +197,7 @@ class MemSegBuf extends RcSupport<Buf, MemSegBuf> implements Buf, ReadableCompon
buffer = buffer.position(writerOffset()).limit(writerOffset() + writableBytes()); buffer = buffer.position(writerOffset()).limit(writerOffset() + writableBytes());
} else { } else {
// TODO avoid slicing and just set position when JDK bug is fixed. // TODO avoid slicing and just set position when JDK bug is fixed.
buffer = buffer.slice(baseOffset + writerOffset(), writableBytes()); buffer = buffer.slice(writerOffset(), writableBytes());
} }
return buffer.order(order); return buffer.order(order);
} }
@ -241,7 +235,7 @@ class MemSegBuf extends RcSupport<Buf, MemSegBuf> implements Buf, ReadableCompon
b.makeInaccessible(); b.makeInaccessible();
}; };
var sendable = false; // Sending implies ownership change, which we can't do for slices. var sendable = false; // Sending implies ownership change, which we can't do for slices.
return new MemSegBuf(slice, drop, alloc, sendable, baseOffset + offset) return new MemSegBuf(slice, drop, alloc, sendable)
.writerOffset(length) .writerOffset(length)
.order(order()) .order(order())
.readOnly(readOnly()); .readOnly(readOnly());