Update compatibility and fix build

The compatibility baseline is now Java 17, which means we can use new language features and new bytecode version.

The MemSegBuffer implementation has been updated with the latest API changes from netty/main.
This commit is contained in:
Chris Vest 2021-09-23 10:57:22 +02:00
parent a4beebf355
commit 6c9f1cbb4e
5 changed files with 36 additions and 118 deletions

View File

@ -26,7 +26,6 @@ import io.netty.buffer.api.ReadableComponent;
import io.netty.buffer.api.ReadableComponentProcessor;
import io.netty.buffer.api.WritableComponent;
import io.netty.buffer.api.WritableComponentProcessor;
import io.netty.buffer.api.adaptor.BufferIntegratable;
import io.netty.buffer.api.internal.AdaptableBuffer;
import io.netty.buffer.api.internal.ArcDrop;
import io.netty.buffer.api.internal.Statics;
@ -53,8 +52,7 @@ 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 Buffer, ReadableComponent,
WritableComponent, BufferIntegratable {
class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements ReadableComponent, WritableComponent {
private static final MemorySegment CLOSED_SEGMENT;
private static final MemorySegment ZERO_OFFHEAP_SEGMENT;
private static final MemorySegment ZERO_ONHEAP_SEGMENT;
@ -96,7 +94,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, Read
private boolean constBuffer;
MemSegBuffer(MemorySegment base, MemorySegment view, Drop<MemSegBuffer> drop, AllocatorControl control) {
super(new MakeInaccisbleOnDrop(ArcDrop.wrap(drop)));
super(ArcDrop.wrap(drop));
this.control = control;
this.base = base;
seg = view;
@ -108,7 +106,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, Read
* Constructor for {@linkplain BufferAllocator#constBufferSupplier(byte[]) const buffers}.
*/
MemSegBuffer(MemSegBuffer parent) {
super(new MakeInaccisbleOnDrop(new ArcDrop<>(ArcDrop.acquire(parent.unsafeGetDrop()))));
super(new ArcDrop<>(ArcDrop.acquire(parent.unsafeGetDrop())));
control = parent.control;
base = parent.base;
seg = parent.seg;
@ -119,44 +117,6 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, Read
constBuffer = true;
}
private static final class MakeInaccisbleOnDrop implements Drop<MemSegBuffer> {
final Drop<MemSegBuffer> delegate;
private MakeInaccisbleOnDrop(Drop<MemSegBuffer> delegate) {
this.delegate = delegate;
}
@Override
public void drop(MemSegBuffer buf) {
try {
delegate.drop(buf);
} finally {
buf.makeInaccessible();
}
}
@Override
public void attach(MemSegBuffer buf) {
delegate.attach(buf);
}
@Override
public String toString() {
return "MemSegDrop(" + delegate + ')';
}
}
@Override
protected Drop<MemSegBuffer> unsafeGetDrop() {
MakeInaccisbleOnDrop drop = (MakeInaccisbleOnDrop) super.unsafeGetDrop();
return drop.delegate;
}
@Override
protected void unsafeSetDrop(Drop<MemSegBuffer> replacement) {
super.unsafeSetDrop(new MakeInaccisbleOnDrop(replacement));
}
@Override
public String toString() {
return "Buffer[roff:" + roff + ", woff:" + woff + ", cap:" + seg.byteSize() + ']';
@ -273,8 +233,7 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, Read
}
// </editor-fold>
@Override
public long nativeAddress() {
private long nativeAddress() {
if (!isAccessible()) {
throw bufferIsClosed(this);
}
@ -386,22 +345,8 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, Read
final MemorySegment segment = seg;
int index = fromOffset;
final int end = index + length;
long longValue = -1;
byte byteValue = -1;
public boolean readLong() {
if (index + Long.BYTES <= end) {
longValue = getLongAtOffset(segment, index, ByteOrder.BIG_ENDIAN);
index += Long.BYTES;
return true;
}
return false;
}
public long getLong() {
return longValue;
}
@Override
public boolean readByte() {
if (index < end) {
@ -457,23 +402,8 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, Read
final MemorySegment segment = seg;
int index = fromOffset;
final int end = index - length;
long longValue = -1;
byte byteValue = -1;
public boolean readLong() {
if (index - Long.BYTES >= end) {
index -= 7;
longValue = getLongAtOffset(segment, index, ByteOrder.LITTLE_ENDIAN);
index--;
return true;
}
return false;
}
public long getLong() {
return longValue;
}
@Override
public boolean readByte() {
if (index > end) {
@ -1139,7 +1069,8 @@ class MemSegBuffer extends AdaptableBuffer<MemSegBuffer> implements Buffer, Read
};
}
void makeInaccessible() {
@Override
protected void makeInaccessible() {
base = CLOSED_SEGMENT;
seg = CLOSED_SEGMENT;
wseg = CLOSED_SEGMENT;

View File

@ -228,24 +228,6 @@ public class BufferBulkAccessTest extends BufferTestSupport {
}
}
@ParameterizedTest
@MethodSource("heapAllocators")
public void heapBufferMustHaveZeroAddress(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThat(buf.nativeAddress()).isZero();
}
}
@ParameterizedTest
@MethodSource("directAllocators")
public void directBufferMustHaveNonZeroAddress(Fixture fixture) {
try (BufferAllocator allocator = fixture.createAllocator();
Buffer buf = allocator.allocate(8)) {
assertThat(buf.nativeAddress()).isNotZero();
}
}
@ParameterizedTest
@MethodSource("allocators")
public void writeBytesMustWriteAllBytesFromByteArray(Fixture fixture) {

View File

@ -113,7 +113,7 @@ public class BufferCompositionTest extends BufferTestSupport {
Buffer a = allocator.allocate(8);
Buffer b = allocator.allocate(8);
CompositeBuffer composed = CompositeBuffer.compose(allocator, a.send())) {
try (Buffer ignore = acquire(composed)) {
try (Buffer ignore = acquire((ResourceSupport<?, ?>) composed)) {
var exc = assertThrows(IllegalStateException.class, () -> composed.extendWith(b.send()));
assertThat(exc).hasMessageContaining("owned");
}
@ -144,13 +144,13 @@ public class BufferCompositionTest extends BufferTestSupport {
try (BufferAllocator allocator = BufferAllocator.onHeapUnpooled()) {
Buffer a = allocator.allocate(1);
CompositeBuffer composite = CompositeBuffer.compose(allocator, a.send());
assertTrue(isOwned(composite));
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertThat(composite.capacity()).isOne();
assertThat(composite.countComponents()).isOne();
try (Buffer b = CompositeBuffer.compose(allocator)) {
composite.extendWith(b.send());
}
assertTrue(isOwned(composite));
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertThat(composite.capacity()).isOne();
assertThat(composite.countComponents()).isOne();
}
@ -393,11 +393,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(),
allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsFloor(4)) {
assertTrue(isOwned(split));
assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible());
assertThat(split.capacity()).isZero();
assertTrue(isOwned(composite));
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(16);
}
@ -411,11 +411,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(),
allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsFloor(7)) {
assertTrue(isOwned(split));
assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible());
assertThat(split.capacity()).isZero();
assertTrue(isOwned(composite));
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(16);
}
@ -429,11 +429,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(),
allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsFloor(12)) {
assertTrue(isOwned(split));
assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible());
assertThat(split.capacity()).isEqualTo(8);
assertTrue(isOwned(composite));
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(8);
}
@ -447,11 +447,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(),
allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsFloor(8)) {
assertTrue(isOwned(split));
assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible());
assertThat(split.capacity()).isEqualTo(8);
assertTrue(isOwned(composite));
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(8);
}
@ -465,11 +465,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(),
allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsCeil(4)) {
assertTrue(isOwned(split));
assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible());
assertThat(split.capacity()).isEqualTo(8);
assertTrue(isOwned(composite));
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(8);
}
@ -483,11 +483,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(),
allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsCeil(7)) {
assertTrue(isOwned(split));
assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible());
assertThat(split.capacity()).isEqualTo(8);
assertTrue(isOwned(composite));
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(8);
}
@ -501,11 +501,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(),
allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsCeil(12)) {
assertTrue(isOwned(split));
assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible());
assertThat(split.capacity()).isEqualTo(16);
assertTrue(isOwned(composite));
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(0);
}
@ -517,11 +517,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(),
allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsCeil(12)) {
assertTrue(isOwned(split));
assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible());
assertThat(split.capacity()).isEqualTo(16);
assertTrue(isOwned(composite));
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(8);
}
@ -535,11 +535,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(),
allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsCeil(7)) {
assertTrue(isOwned(split));
assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible());
assertThat(split.capacity()).isEqualTo(8);
assertTrue(isOwned(composite));
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(8);
}
@ -553,11 +553,11 @@ public class BufferCompositionTest extends BufferTestSupport {
allocator.allocate(8).send(),
allocator.allocate(8).send())) {
try (CompositeBuffer split = composite.splitComponentsCeil(0)) {
assertTrue(isOwned(split));
assertTrue(isOwned((ResourceSupport<?, ?>) split));
assertTrue(split.isAccessible());
assertThat(split.capacity()).isZero();
assertTrue(isOwned(composite));
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
assertTrue(composite.isAccessible());
assertThat(composite.capacity()).isEqualTo(16);
}

View File

@ -617,6 +617,11 @@ public abstract class ByteToMessageDecoder extends ChannelHandlerAdapter {
return ctx.alloc();
}
@Override
public BufferAllocator bufferAllocator() {
return ctx.bufferAllocator();
}
@Override
@Deprecated
public <T> Attribute<T> attr(AttributeKey<T> key) {

View File

@ -70,8 +70,8 @@
<javaModuleName>io.netty.incubator.buffer</javaModuleName>
<netty.version>5.0.0.Final-SNAPSHOT</netty.version>
<netty.build.version>29</netty.build.version>
<!-- Java version for source and bytecode compatibility. -->
<java.compatibility>11</java.compatibility>
<!-- Java version for bytecode compatibility. -->
<java.compatibility>17</java.compatibility>
<!-- Java version actually used to perform the build; compiling and testing. -->
<java.version>17</java.version>
<junit.version>5.7.0</junit.version>