Update implementation with latest Netty and Panama Foreign API changes

This commit is contained in:
Chris Vest 2021-09-30 14:13:36 +02:00
parent 6c9f1cbb4e
commit 4576961971
5 changed files with 198 additions and 135 deletions

View File

@ -31,26 +31,12 @@ import io.netty.buffer.api.internal.ArcDrop;
import io.netty.buffer.api.internal.Statics; import io.netty.buffer.api.internal.Statics;
import jdk.incubator.foreign.MemorySegment; import jdk.incubator.foreign.MemorySegment;
import jdk.incubator.foreign.ResourceScope; import jdk.incubator.foreign.ResourceScope;
import jdk.incubator.foreign.ValueLayout;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import static io.netty.buffer.api.internal.Statics.bufferIsClosed; import static io.netty.buffer.api.internal.Statics.bufferIsClosed;
import static io.netty.buffer.api.internal.Statics.bufferIsReadOnly; import static io.netty.buffer.api.internal.Statics.bufferIsReadOnly;
import static jdk.incubator.foreign.MemoryAccess.getByteAtOffset;
import static jdk.incubator.foreign.MemoryAccess.getCharAtOffset;
import static jdk.incubator.foreign.MemoryAccess.getDoubleAtOffset;
import static jdk.incubator.foreign.MemoryAccess.getFloatAtOffset;
import static jdk.incubator.foreign.MemoryAccess.getIntAtOffset;
import static jdk.incubator.foreign.MemoryAccess.getLongAtOffset;
import static jdk.incubator.foreign.MemoryAccess.getShortAtOffset;
import static jdk.incubator.foreign.MemoryAccess.setByteAtOffset;
import static jdk.incubator.foreign.MemoryAccess.setCharAtOffset;
import static jdk.incubator.foreign.MemoryAccess.setDoubleAtOffset;
import static jdk.incubator.foreign.MemoryAccess.setFloatAtOffset;
import static jdk.incubator.foreign.MemoryAccess.setIntAtOffset;
import static jdk.incubator.foreign.MemoryAccess.setLongAtOffset;
import static jdk.incubator.foreign.MemoryAccess.setShortAtOffset;
class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComponent, WritableComponent { class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComponent, WritableComponent {
private static final MemorySegment CLOSED_SEGMENT; private static final MemorySegment CLOSED_SEGMENT;
@ -66,17 +52,26 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
MemorySegment segment = MemorySegment.allocateNative(1, scope); MemorySegment segment = MemorySegment.allocateNative(1, scope);
CLOSED_SEGMENT = segment.asSlice(0, 0); CLOSED_SEGMENT = segment.asSlice(0, 0);
} }
ZERO_OFFHEAP_SEGMENT = MemorySegment.allocateNative(1, ResourceScope.newImplicitScope()).asSlice(0, 0); ZERO_OFFHEAP_SEGMENT = MemorySegment.allocateNative(1, ResourceScope.globalScope()).asSlice(0, 0);
ZERO_ONHEAP_SEGMENT = MemorySegment.ofArray(new byte[0]); ZERO_ONHEAP_SEGMENT = MemorySegment.ofArray(new byte[0]);
SEGMENT_CLOSE = new Drop<MemSegBuffer>() { SEGMENT_CLOSE = new Drop<MemSegBuffer>() {
@Override @Override
public void drop(MemSegBuffer buf) { public void drop(MemSegBuffer buf) {
ResourceScope scope = buf.base.scope(); ResourceScope scope = buf.base.scope();
if (!scope.isImplicit()) { if (scope != ResourceScope.globalScope()) {
scope.close(); scope.close();
} }
} }
@Override
public Drop<MemSegBuffer> fork() {
return this;
}
@Override
public void attach(MemSegBuffer obj) {
}
@Override @Override
public String toString() { public String toString() {
return "SEGMENT_CLOSE"; return "SEGMENT_CLOSE";
@ -88,30 +83,27 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
private MemorySegment base; private MemorySegment base;
private MemorySegment seg; private MemorySegment seg;
private MemorySegment wseg; private MemorySegment wseg;
private ByteOrder order;
private int roff; private int roff;
private int woff; private int woff;
private boolean constBuffer; private boolean constBuffer;
MemSegBuffer(MemorySegment base, MemorySegment view, Drop<MemSegBuffer> drop, AllocatorControl control) { MemSegBuffer(MemorySegment base, MemorySegment view, Drop<MemSegBuffer> drop, AllocatorControl control) {
super(ArcDrop.wrap(drop)); super(drop, control);
this.control = control; this.control = control;
this.base = base; this.base = base;
seg = view; seg = view;
wseg = view; wseg = view;
order = ByteOrder.BIG_ENDIAN;
} }
/** /**
* Constructor for {@linkplain BufferAllocator#constBufferSupplier(byte[]) const buffers}. * Constructor for {@linkplain BufferAllocator#constBufferSupplier(byte[]) const buffers}.
*/ */
MemSegBuffer(MemSegBuffer parent) { MemSegBuffer(MemSegBuffer parent) {
super(new ArcDrop<>(ArcDrop.acquire(parent.unsafeGetDrop()))); super(parent.unsafeGetDrop(), parent.control);
control = parent.control; control = parent.control;
base = parent.base; base = parent.base;
seg = parent.seg; seg = parent.seg;
wseg = parent.wseg; wseg = parent.wseg;
order = parent.order;
roff = parent.roff; roff = parent.roff;
woff = parent.woff; woff = parent.woff;
constBuffer = true; constBuffer = true;
@ -197,7 +189,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
var buffer = seg.asByteBuffer(); var buffer = seg.asByteBuffer();
buffer = buffer.asReadOnlyBuffer(); buffer = buffer.asReadOnlyBuffer();
buffer = buffer.position(readerOffset()).limit(readerOffset() + readableBytes()); buffer = buffer.position(readerOffset()).limit(readerOffset() + readableBytes());
return buffer.order(order); return buffer;
} }
@Override @Override
@ -229,7 +221,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
public ByteBuffer writableBuffer() { public ByteBuffer writableBuffer() {
var buffer = wseg.asByteBuffer(); var buffer = wseg.asByteBuffer();
buffer = buffer.position(writerOffset()).limit(writerOffset() + writableBytes()); buffer = buffer.position(writerOffset()).limit(writerOffset() + writableBytes());
return buffer.order(order); return buffer;
} }
// </editor-fold> // </editor-fold>
@ -269,7 +261,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
} else { } else {
zero = ZERO_OFFHEAP_SEGMENT; zero = ZERO_OFFHEAP_SEGMENT;
} }
return new MemSegBuffer(zero, zero, Statics.noOpDrop(), control); return new MemSegBuffer(zero, zero, SEGMENT_CLOSE, control);
} }
AllocatorControl.UntetheredMemory memory = control.allocateUntethered(this, length); AllocatorControl.UntetheredMemory memory = control.allocateUntethered(this, length);
@ -350,7 +342,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public boolean readByte() { public boolean readByte() {
if (index < end) { if (index < end) {
byteValue = getByteAtOffset(segment, index); byteValue = segment.get(ValueLayout.JAVA_BYTE, index);
index++; index++;
return true; return true;
} }
@ -407,7 +399,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public boolean readByte() { public boolean readByte() {
if (index > end) { if (index > end) {
byteValue = getByteAtOffset(segment, index); byteValue = segment.get(ValueLayout.JAVA_BYTE, index);
index--; index--;
return true; return true;
} }
@ -578,6 +570,62 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
} }
// <editor-fold defaultstate="collapsed" desc="Primitive accessors implementation."> // <editor-fold defaultstate="collapsed" desc="Primitive accessors implementation.">
private static byte getByteAtOffset(MemorySegment seg, int roff) {
return seg.get(ValueLayout.JAVA_BYTE, roff);
}
private static void setByteAtOffset(MemorySegment seg, int woff, byte value) {
seg.set(ValueLayout.JAVA_BYTE, woff, value);
}
private static short getShortAtOffset(MemorySegment seg, int roff) {
return seg.get(ValueLayout.JAVA_SHORT, roff);
}
private static void setShortAtOffset(MemorySegment seg, int woff, short value) {
seg.set(ValueLayout.JAVA_SHORT, woff, value);
}
private static char getCharAtOffset(MemorySegment seg, int roff) {
return seg.get(ValueLayout.JAVA_CHAR, roff);
}
private static void setCharAtOffset(MemorySegment seg, int woff, char value) {
seg.set(ValueLayout.JAVA_CHAR, woff, value);
}
private static int getIntAtOffset(MemorySegment seg, int roff) {
return seg.get(ValueLayout.JAVA_INT, roff);
}
private static void setIntAtOffset(MemorySegment seg, int woff, int value) {
seg.set(ValueLayout.JAVA_INT, woff, value);
}
private static float getFloatAtOffset(MemorySegment seg, int roff) {
return seg.get(ValueLayout.JAVA_FLOAT, roff);
}
private static void setFloatAtOffset(MemorySegment seg, int woff, float value) {
seg.set(ValueLayout.JAVA_FLOAT, woff, value);
}
private static long getLongAtOffset(MemorySegment seg, int roff) {
return seg.get(ValueLayout.JAVA_LONG, roff);
}
private static void setLongAtOffset(MemorySegment seg, int woff, long value) {
seg.set(ValueLayout.JAVA_LONG, woff, value);
}
private static double getDoubleAtOffset(MemorySegment seg, int roff) {
return seg.get(ValueLayout.JAVA_DOUBLE, roff);
}
private static void setDoubleAtOffset(MemorySegment seg, int woff, double value) {
seg.set(ValueLayout.JAVA_DOUBLE, woff, value);
}
@Override @Override
public byte readByte() { public byte readByte() {
checkRead(roff, Byte.BYTES); checkRead(roff, Byte.BYTES);
@ -651,7 +699,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public char readChar() { public char readChar() {
checkRead(roff, 2); checkRead(roff, 2);
char value = getCharAtOffset(seg, roff, order); char value = getCharAtOffset(seg, roff);
roff += 2; roff += 2;
return value; return value;
} }
@ -659,13 +707,13 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public char getChar(int roff) { public char getChar(int roff) {
checkGet(roff, 2); checkGet(roff, 2);
return getCharAtOffset(seg, roff, order); return getCharAtOffset(seg, roff);
} }
@Override @Override
public Buffer writeChar(char value) { public Buffer writeChar(char value) {
try { try {
setCharAtOffset(wseg, woff, order, value); setCharAtOffset(wseg, woff, value);
woff += 2; woff += 2;
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
@ -676,7 +724,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public Buffer setChar(int woff, char value) { public Buffer setChar(int woff, char value) {
try { try {
setCharAtOffset(wseg, woff, order, value); setCharAtOffset(wseg, woff, value);
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
throw checkWriteState(e); throw checkWriteState(e);
@ -686,7 +734,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public short readShort() { public short readShort() {
checkRead(roff, Short.BYTES); checkRead(roff, Short.BYTES);
short value = getShortAtOffset(seg, roff, order); short value = getShortAtOffset(seg, roff);
roff += Short.BYTES; roff += Short.BYTES;
return value; return value;
} }
@ -694,13 +742,13 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public short getShort(int roff) { public short getShort(int roff) {
checkGet(roff, Short.BYTES); checkGet(roff, Short.BYTES);
return getShortAtOffset(seg, roff, order); return getShortAtOffset(seg, roff);
} }
@Override @Override
public int readUnsignedShort() { public int readUnsignedShort() {
checkRead(roff, Short.BYTES); checkRead(roff, Short.BYTES);
int value = getShortAtOffset(seg, roff, order) & 0xFFFF; int value = getShortAtOffset(seg, roff) & 0xFFFF;
roff += Short.BYTES; roff += Short.BYTES;
return value; return value;
} }
@ -708,13 +756,13 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public int getUnsignedShort(int roff) { public int getUnsignedShort(int roff) {
checkGet(roff, Short.BYTES); checkGet(roff, Short.BYTES);
return getShortAtOffset(seg, roff, order) & 0xFFFF; return getShortAtOffset(seg, roff) & 0xFFFF;
} }
@Override @Override
public Buffer writeShort(short value) { public Buffer writeShort(short value) {
try { try {
setShortAtOffset(wseg, woff, order, value); setShortAtOffset(wseg, woff, value);
woff += Short.BYTES; woff += Short.BYTES;
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
@ -725,7 +773,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public Buffer setShort(int woff, short value) { public Buffer setShort(int woff, short value) {
try { try {
setShortAtOffset(wseg, woff, order, value); setShortAtOffset(wseg, woff, value);
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
throw checkWriteState(e); throw checkWriteState(e);
@ -735,7 +783,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public Buffer writeUnsignedShort(int value) { public Buffer writeUnsignedShort(int value) {
try { try {
setShortAtOffset(wseg, woff, order, (short) (value & 0xFFFF)); setShortAtOffset(wseg, woff, (short) (value & 0xFFFF));
woff += Short.BYTES; woff += Short.BYTES;
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
@ -746,7 +794,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public Buffer setUnsignedShort(int woff, int value) { public Buffer setUnsignedShort(int woff, int value) {
try { try {
setShortAtOffset(wseg, woff, order, (short) (value & 0xFFFF)); setShortAtOffset(wseg, woff, (short) (value & 0xFFFF));
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
throw checkWriteState(e); throw checkWriteState(e);
@ -756,13 +804,9 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public int readMedium() { public int readMedium() {
checkRead(roff, 3); checkRead(roff, 3);
int value = order == ByteOrder.BIG_ENDIAN? int value = getByteAtOffset(seg, roff) << 16 |
getByteAtOffset(seg, roff) << 16 |
(getByteAtOffset(seg, roff + 1) & 0xFF) << 8 | (getByteAtOffset(seg, roff + 1) & 0xFF) << 8 |
getByteAtOffset(seg, roff + 2) & 0xFF : getByteAtOffset(seg, roff + 2) & 0xFF;
getByteAtOffset(seg, roff) & 0xFF |
(getByteAtOffset(seg, roff + 1) & 0xFF) << 8 |
getByteAtOffset(seg, roff + 2) << 16;
roff += 3; roff += 3;
return value; return value;
} }
@ -770,25 +814,17 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public int getMedium(int roff) { public int getMedium(int roff) {
checkGet(roff, 3); checkGet(roff, 3);
return order == ByteOrder.BIG_ENDIAN? return getByteAtOffset(seg, roff) << 16 |
getByteAtOffset(seg, roff) << 16 |
(getByteAtOffset(seg, roff + 1) & 0xFF) << 8 | (getByteAtOffset(seg, roff + 1) & 0xFF) << 8 |
getByteAtOffset(seg, roff + 2) & 0xFF : getByteAtOffset(seg, roff + 2) & 0xFF;
getByteAtOffset(seg, roff) & 0xFF |
(getByteAtOffset(seg, roff + 1) & 0xFF) << 8 |
getByteAtOffset(seg, roff + 2) << 16;
} }
@Override @Override
public int readUnsignedMedium() { public int readUnsignedMedium() {
checkRead(roff, 3); checkRead(roff, 3);
int value = order == ByteOrder.BIG_ENDIAN? int value = (getByteAtOffset(seg, roff) << 16 |
(getByteAtOffset(seg, roff) << 16 |
(getByteAtOffset(seg, roff + 1) & 0xFF) << 8 | (getByteAtOffset(seg, roff + 1) & 0xFF) << 8 |
getByteAtOffset(seg, roff + 2) & 0xFF) & 0xFFFFFF : getByteAtOffset(seg, roff + 2) & 0xFF) & 0xFFFFFF;
(getByteAtOffset(seg, roff) & 0xFF |
(getByteAtOffset(seg, roff + 1) & 0xFF) << 8 |
getByteAtOffset(seg, roff + 2) << 16) & 0xFFFFFF;
roff += 3; roff += 3;
return value; return value;
} }
@ -796,27 +832,17 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public int getUnsignedMedium(int roff) { public int getUnsignedMedium(int roff) {
checkGet(roff, 3); checkGet(roff, 3);
return order == ByteOrder.BIG_ENDIAN? return (getByteAtOffset(seg, roff) << 16 |
(getByteAtOffset(seg, roff) << 16 |
(getByteAtOffset(seg, roff + 1) & 0xFF) << 8 | (getByteAtOffset(seg, roff + 1) & 0xFF) << 8 |
getByteAtOffset(seg, roff + 2) & 0xFF) & 0xFFFFFF : getByteAtOffset(seg, roff + 2) & 0xFF) & 0xFFFFFF;
(getByteAtOffset(seg, roff) & 0xFF |
(getByteAtOffset(seg, roff + 1) & 0xFF) << 8 |
getByteAtOffset(seg, roff + 2) << 16) & 0xFFFFFF;
} }
@Override @Override
public Buffer writeMedium(int value) { public Buffer writeMedium(int value) {
checkWrite(woff, 3); checkWrite(woff, 3);
if (order == ByteOrder.BIG_ENDIAN) { setByteAtOffset(wseg, woff, (byte) (value >> 16));
setByteAtOffset(wseg, woff, (byte) (value >> 16)); setByteAtOffset(wseg, woff + 1, (byte) (value >> 8 & 0xFF));
setByteAtOffset(wseg, woff + 1, (byte) (value >> 8 & 0xFF)); setByteAtOffset(wseg, woff + 2, (byte) (value & 0xFF));
setByteAtOffset(wseg, woff + 2, (byte) (value & 0xFF));
} else {
setByteAtOffset(wseg, woff, (byte) (value & 0xFF));
setByteAtOffset(wseg, woff + 1, (byte) (value >> 8 & 0xFF));
setByteAtOffset(wseg, woff + 2, (byte) (value >> 16 & 0xFF));
}
woff += 3; woff += 3;
return this; return this;
} }
@ -824,30 +850,18 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public Buffer setMedium(int woff, int value) { public Buffer setMedium(int woff, int value) {
checkSet(woff, 3); checkSet(woff, 3);
if (order == ByteOrder.BIG_ENDIAN) { setByteAtOffset(wseg, woff, (byte) (value >> 16));
setByteAtOffset(wseg, woff, (byte) (value >> 16)); setByteAtOffset(wseg, woff + 1, (byte) (value >> 8 & 0xFF));
setByteAtOffset(wseg, woff + 1, (byte) (value >> 8 & 0xFF)); setByteAtOffset(wseg, woff + 2, (byte) (value & 0xFF));
setByteAtOffset(wseg, woff + 2, (byte) (value & 0xFF));
} else {
setByteAtOffset(wseg, woff, (byte) (value & 0xFF));
setByteAtOffset(wseg, woff + 1, (byte) (value >> 8 & 0xFF));
setByteAtOffset(wseg, woff + 2, (byte) (value >> 16 & 0xFF));
}
return this; return this;
} }
@Override @Override
public Buffer writeUnsignedMedium(int value) { public Buffer writeUnsignedMedium(int value) {
checkWrite(woff, 3); checkWrite(woff, 3);
if (order == ByteOrder.BIG_ENDIAN) { setByteAtOffset(wseg, woff, (byte) (value >> 16));
setByteAtOffset(wseg, woff, (byte) (value >> 16)); setByteAtOffset(wseg, woff + 1, (byte) (value >> 8 & 0xFF));
setByteAtOffset(wseg, woff + 1, (byte) (value >> 8 & 0xFF)); setByteAtOffset(wseg, woff + 2, (byte) (value & 0xFF));
setByteAtOffset(wseg, woff + 2, (byte) (value & 0xFF));
} else {
setByteAtOffset(wseg, woff, (byte) (value & 0xFF));
setByteAtOffset(wseg, woff + 1, (byte) (value >> 8 & 0xFF));
setByteAtOffset(wseg, woff + 2, (byte) (value >> 16 & 0xFF));
}
woff += 3; woff += 3;
return this; return this;
} }
@ -855,22 +869,16 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public Buffer setUnsignedMedium(int woff, int value) { public Buffer setUnsignedMedium(int woff, int value) {
checkSet(woff, 3); checkSet(woff, 3);
if (order == ByteOrder.BIG_ENDIAN) { setByteAtOffset(wseg, woff, (byte) (value >> 16));
setByteAtOffset(wseg, woff, (byte) (value >> 16)); setByteAtOffset(wseg, woff + 1, (byte) (value >> 8 & 0xFF));
setByteAtOffset(wseg, woff + 1, (byte) (value >> 8 & 0xFF)); setByteAtOffset(wseg, woff + 2, (byte) (value & 0xFF));
setByteAtOffset(wseg, woff + 2, (byte) (value & 0xFF));
} else {
setByteAtOffset(wseg, woff, (byte) (value & 0xFF));
setByteAtOffset(wseg, woff + 1, (byte) (value >> 8 & 0xFF));
setByteAtOffset(wseg, woff + 2, (byte) (value >> 16 & 0xFF));
}
return this; return this;
} }
@Override @Override
public int readInt() { public int readInt() {
checkRead(roff, Integer.BYTES); checkRead(roff, Integer.BYTES);
int value = getIntAtOffset(seg, roff, order); int value = getIntAtOffset(seg, roff);
roff += Integer.BYTES; roff += Integer.BYTES;
return value; return value;
} }
@ -878,13 +886,13 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public int getInt(int roff) { public int getInt(int roff) {
checkGet(roff, Integer.BYTES); checkGet(roff, Integer.BYTES);
return getIntAtOffset(seg, roff, order); return getIntAtOffset(seg, roff);
} }
@Override @Override
public long readUnsignedInt() { public long readUnsignedInt() {
checkRead(roff, Integer.BYTES); checkRead(roff, Integer.BYTES);
long value = getIntAtOffset(seg, roff, order) & 0xFFFFFFFFL; long value = getIntAtOffset(seg, roff) & 0xFFFFFFFFL;
roff += Integer.BYTES; roff += Integer.BYTES;
return value; return value;
} }
@ -892,13 +900,13 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public long getUnsignedInt(int roff) { public long getUnsignedInt(int roff) {
checkGet(roff, Integer.BYTES); checkGet(roff, Integer.BYTES);
return getIntAtOffset(seg, roff, order) & 0xFFFFFFFFL; return getIntAtOffset(seg, roff) & 0xFFFFFFFFL;
} }
@Override @Override
public Buffer writeInt(int value) { public Buffer writeInt(int value) {
try { try {
setIntAtOffset(wseg, woff, order, value); setIntAtOffset(wseg, woff, value);
woff += Integer.BYTES; woff += Integer.BYTES;
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
@ -909,7 +917,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public Buffer setInt(int woff, int value) { public Buffer setInt(int woff, int value) {
try { try {
setIntAtOffset(wseg, woff, order, value); setIntAtOffset(wseg, woff, value);
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
throw checkWriteState(e); throw checkWriteState(e);
@ -919,7 +927,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public Buffer writeUnsignedInt(long value) { public Buffer writeUnsignedInt(long value) {
try { try {
setIntAtOffset(wseg, woff, order, (int) (value & 0xFFFFFFFFL)); setIntAtOffset(wseg, woff, (int) (value & 0xFFFFFFFFL));
woff += Integer.BYTES; woff += Integer.BYTES;
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
@ -930,7 +938,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public Buffer setUnsignedInt(int woff, long value) { public Buffer setUnsignedInt(int woff, long value) {
try { try {
setIntAtOffset(wseg, woff, order, (int) (value & 0xFFFFFFFFL)); setIntAtOffset(wseg, woff, (int) (value & 0xFFFFFFFFL));
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
throw checkWriteState(e); throw checkWriteState(e);
@ -940,7 +948,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public float readFloat() { public float readFloat() {
checkRead(roff, Float.BYTES); checkRead(roff, Float.BYTES);
float value = getFloatAtOffset(seg, roff, order); float value = getFloatAtOffset(seg, roff);
roff += Float.BYTES; roff += Float.BYTES;
return value; return value;
} }
@ -948,13 +956,13 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public float getFloat(int roff) { public float getFloat(int roff) {
checkGet(roff, Float.BYTES); checkGet(roff, Float.BYTES);
return getFloatAtOffset(seg, roff, order); return getFloatAtOffset(seg, roff);
} }
@Override @Override
public Buffer writeFloat(float value) { public Buffer writeFloat(float value) {
try { try {
setFloatAtOffset(wseg, woff, order, value); setFloatAtOffset(wseg, woff, value);
woff += Float.BYTES; woff += Float.BYTES;
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
@ -965,7 +973,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public Buffer setFloat(int woff, float value) { public Buffer setFloat(int woff, float value) {
try { try {
setFloatAtOffset(wseg, woff, order, value); setFloatAtOffset(wseg, woff, value);
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
throw checkWriteState(e); throw checkWriteState(e);
@ -975,7 +983,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public long readLong() { public long readLong() {
checkRead(roff, Long.BYTES); checkRead(roff, Long.BYTES);
long value = getLongAtOffset(seg, roff, order); long value = getLongAtOffset(seg, roff);
roff += Long.BYTES; roff += Long.BYTES;
return value; return value;
} }
@ -983,13 +991,13 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public long getLong(int roff) { public long getLong(int roff) {
checkGet(roff, Long.BYTES); checkGet(roff, Long.BYTES);
return getLongAtOffset(seg, roff, order); return getLongAtOffset(seg, roff);
} }
@Override @Override
public Buffer writeLong(long value) { public Buffer writeLong(long value) {
try { try {
setLongAtOffset(wseg, woff, order, value); setLongAtOffset(wseg, woff, value);
woff += Long.BYTES; woff += Long.BYTES;
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
@ -1000,7 +1008,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public Buffer setLong(int woff, long value) { public Buffer setLong(int woff, long value) {
try { try {
setLongAtOffset(wseg, woff, order, value); setLongAtOffset(wseg, woff, value);
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
throw checkWriteState(e); throw checkWriteState(e);
@ -1010,7 +1018,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public double readDouble() { public double readDouble() {
checkRead(roff, Double.BYTES); checkRead(roff, Double.BYTES);
double value = getDoubleAtOffset(seg, roff, order); double value = getDoubleAtOffset(seg, roff);
roff += Double.BYTES; roff += Double.BYTES;
return value; return value;
} }
@ -1018,13 +1026,13 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public double getDouble(int roff) { public double getDouble(int roff) {
checkGet(roff, Double.BYTES); checkGet(roff, Double.BYTES);
return getDoubleAtOffset(seg, roff, order); return getDoubleAtOffset(seg, roff);
} }
@Override @Override
public Buffer writeDouble(double value) { public Buffer writeDouble(double value) {
try { try {
setDoubleAtOffset(wseg, woff, order, value); setDoubleAtOffset(wseg, woff, value);
woff += Double.BYTES; woff += Double.BYTES;
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
@ -1035,7 +1043,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public Buffer setDouble(int woff, double value) { public Buffer setDouble(int woff, double value) {
try { try {
setDoubleAtOffset(wseg, woff, order, value); setDoubleAtOffset(wseg, woff, value);
return this; return this;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
throw checkWriteState(e); throw checkWriteState(e);
@ -1045,7 +1053,6 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
protected Owned<MemSegBuffer> prepareSend() { protected Owned<MemSegBuffer> prepareSend() {
var order = this.order;
var roff = this.roff; var roff = this.roff;
var woff = this.woff; var woff = this.woff;
var readOnly = readOnly(); var readOnly = readOnly();
@ -1057,7 +1064,6 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
@Override @Override
public MemSegBuffer transferOwnership(Drop<MemSegBuffer> drop) { public MemSegBuffer transferOwnership(Drop<MemSegBuffer> drop) {
MemSegBuffer copy = new MemSegBuffer(base, transferSegment, drop, control); MemSegBuffer copy = new MemSegBuffer(base, transferSegment, drop, control);
copy.order = order;
copy.roff = roff; copy.roff = roff;
copy.woff = woff; copy.woff = woff;
if (readOnly) { if (readOnly) {
@ -1078,16 +1084,6 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComp
woff = 0; woff = 0;
} }
@Override
public boolean isOwned() {
return super.isOwned() && ((ArcDrop<MemSegBuffer>) unsafeGetDrop()).isOwned();
}
@Override
public int countBorrows() {
return super.countBorrows() + ((ArcDrop<MemSegBuffer>) unsafeGetDrop()).countBorrows();
}
private void checkRead(int index, int size) { private void checkRead(int index, int size) {
if (index < 0 || woff < index + size) { if (index < 0 || woff < index + size) {
throw readAccessCheckException(index); throw readAccessCheckException(index);

View File

@ -53,12 +53,12 @@ public class SegmentMemoryManagers implements MemoryManager {
} }
@Override @Override
public Buffer allocateShared(AllocatorControl allocatorControl, long size, Drop<Buffer> drop, Cleaner cleaner, public Buffer allocateShared(AllocatorControl allocatorControl, long size, Drop<Buffer> drop,
AllocationType type) { AllocationType type) {
if (type instanceof StandardAllocationTypes stype) { if (type instanceof StandardAllocationTypes stype) {
var segment = switch (stype) { var segment = switch (stype) {
case ON_HEAP -> createHeapSegment(size); case ON_HEAP -> createHeapSegment(size);
case OFF_HEAP -> createNativeSegment(size, cleaner); case OFF_HEAP -> createNativeSegment(size, null);
}; };
return new MemSegBuffer(segment, segment, Statics.convert(drop), allocatorControl); return new MemSegBuffer(segment, segment, Statics.convert(drop), allocatorControl);
} }

View File

@ -560,8 +560,8 @@ public class BufferLifeCycleTest extends BufferTestSupport {
copy.resetOffsets().ensureWritable(Long.BYTES); copy.resetOffsets().ensureWritable(Long.BYTES);
assertThat(split.capacity()).isEqualTo(Long.BYTES); assertThat(split.capacity()).isEqualTo(Long.BYTES);
assertThat(copy.capacity()).isEqualTo(Long.BYTES); assertThat(copy.capacity()).isEqualTo(Long.BYTES);
assertThat(split.getLong(0)).isEqualTo(0x01020304_00000000L); assertEquals(0x01020304, split.getInt(0));
assertThat(copy.getLong(0)).isEqualTo(0x05060708_00000000L); assertEquals(0x05060708, copy.getInt(0));
} }
} }
} }

View File

@ -15,6 +15,7 @@
*/ */
package io.netty.buffer.api.tests; package io.netty.buffer.api.tests;
import io.netty.buffer.api.AllocationType;
import io.netty.buffer.api.Buffer; import io.netty.buffer.api.Buffer;
import io.netty.buffer.api.BufferAllocator; import io.netty.buffer.api.BufferAllocator;
import io.netty.buffer.api.BufferClosedException; import io.netty.buffer.api.BufferClosedException;
@ -184,6 +185,17 @@ public abstract class BufferTestSupport {
return new TestAllocator() { return new TestAllocator() {
final BufferAllocator a = first.get(); final BufferAllocator a = first.get();
final BufferAllocator b = second.get(); final BufferAllocator b = second.get();
@Override
public boolean isPooling() {
return a.isPooling();
}
@Override
public AllocationType getAllocationType() {
return a.getAllocationType();
}
@Override @Override
public Buffer allocate(int size) { public Buffer allocate(int size) {
int half = size / 2; int half = size / 2;
@ -207,6 +219,17 @@ public abstract class BufferTestSupport {
builder.add(new Fixture("compose(heap,heap,heap)", () -> { builder.add(new Fixture("compose(heap,heap,heap)", () -> {
return new TestAllocator() { return new TestAllocator() {
final BufferAllocator alloc = BufferAllocator.onHeapUnpooled(); final BufferAllocator alloc = BufferAllocator.onHeapUnpooled();
@Override
public boolean isPooling() {
return alloc.isPooling();
}
@Override
public AllocationType getAllocationType() {
return alloc.getAllocationType();
}
@Override @Override
public Buffer allocate(int size) { public Buffer allocate(int size) {
int part = size / 3; int part = size / 3;
@ -228,6 +251,17 @@ public abstract class BufferTestSupport {
builder.add(new Fixture(fixture + ".ensureWritable", () -> { builder.add(new Fixture(fixture + ".ensureWritable", () -> {
return new TestAllocator() { return new TestAllocator() {
final BufferAllocator allocator = fixture.createAllocator(); final BufferAllocator allocator = fixture.createAllocator();
@Override
public boolean isPooling() {
return allocator.isPooling();
}
@Override
public AllocationType getAllocationType() {
return allocator.getAllocationType();
}
@Override @Override
public Buffer allocate(int size) { public Buffer allocate(int size) {
if (size < 2) { if (size < 2) {
@ -247,6 +281,17 @@ public abstract class BufferTestSupport {
builder.add(new Fixture(fixture + ".compose.ensureWritable", () -> { builder.add(new Fixture(fixture + ".compose.ensureWritable", () -> {
return new TestAllocator() { return new TestAllocator() {
final BufferAllocator allocator = fixture.createAllocator(); final BufferAllocator allocator = fixture.createAllocator();
@Override
public boolean isPooling() {
return allocator.isPooling();
}
@Override
public AllocationType getAllocationType() {
return allocator.getAllocationType();
}
@Override @Override
public Buffer allocate(int size) { public Buffer allocate(int size) {
if (size < 2) { if (size < 2) {
@ -275,6 +320,17 @@ public abstract class BufferTestSupport {
builder.add(new Fixture(f + ".split", () -> { builder.add(new Fixture(f + ".split", () -> {
return new TestAllocator() { return new TestAllocator() {
final BufferAllocator allocatorBase = f.get(); final BufferAllocator allocatorBase = f.get();
@Override
public boolean isPooling() {
return allocatorBase.isPooling();
}
@Override
public AllocationType getAllocationType() {
return allocatorBase.getAllocationType();
}
@Override @Override
public Buffer allocate(int size) { public Buffer allocate(int size) {
try (Buffer buf = allocatorBase.allocate(size + 1)) { try (Buffer buf = allocatorBase.allocate(size + 1)) {

View File

@ -15,6 +15,7 @@
*/ */
package io.netty.buffer.api.tests.examples.bytetomessagedecoder; package io.netty.buffer.api.tests.examples.bytetomessagedecoder;
import io.netty.buffer.api.AllocationType;
import io.netty.buffer.api.Buffer; import io.netty.buffer.api.Buffer;
import io.netty.buffer.api.BufferAllocator; import io.netty.buffer.api.BufferAllocator;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
@ -407,6 +408,16 @@ public class ByteToMessageDecoderTest {
Buffer newCumulation = writeFailingCumulation(untilFailure, 16); Buffer newCumulation = writeFailingCumulation(untilFailure, 16);
BufferAllocator allocator = new BufferAllocator() { BufferAllocator allocator = new BufferAllocator() {
@Override
public boolean isPooling() {
return false;
}
@Override
public AllocationType getAllocationType() {
return null;
}
@Override @Override
public Buffer allocate(int capacity) { public Buffer allocate(int capacity) {
return newCumulation; return newCumulation;