Fix more review comments

This commit is contained in:
Chris Vest 2021-05-28 17:10:46 +02:00
parent 53a8d71c64
commit db7ac99699
9 changed files with 31 additions and 6 deletions

View File

@ -65,7 +65,7 @@ public interface MemoryManagers {
} }
/** /**
* Find a {@link MemoryManagers} implementation by its {@linkplain #implementationName() implementation name}. * Find a {@link MemoryManagers} implementation by its {@linkplain #getImplementationName() implementation name}.
* <p> * <p>
* Note: All available {@link MemoryManagers} instances are service loaded and instantiated every time this * Note: All available {@link MemoryManagers} instances are service loaded and instantiated every time this
* method is called. * method is called.
@ -82,7 +82,7 @@ public interface MemoryManagers {
return Stream.empty(); return Stream.empty();
} }
}) })
.filter(impl -> implementationName.equals(impl.implementationName())) .filter(impl -> implementationName.equals(impl.getImplementationName()))
.findFirst(); .findFirst();
} }
@ -106,5 +106,5 @@ public interface MemoryManagers {
* *
* @return The name of this memory managers implementation. * @return The name of this memory managers implementation.
*/ */
String implementationName(); String getImplementationName();
} }

View File

@ -1678,6 +1678,9 @@ public final class ByteBufAdaptor extends ByteBuf {
@Override @Override
public boolean release(int decrement) { public boolean release(int decrement) {
if (!buffer.isAccessible() || decrement > 1 + Statics.countBorrows((ResourceSupport<?, ?>) buffer)) {
throw new IllegalReferenceCountException();
}
for (int i = 0; i < decrement; i++) { for (int i = 0; i < decrement; i++) {
try { try {
buffer.close(); buffer.close();

View File

@ -30,7 +30,7 @@ public class ByteBufferMemoryManagers implements MemoryManagers {
} }
@Override @Override
public String implementationName() { public String getImplementationName() {
return "ByteBuffer"; return "ByteBuffer";
} }

View File

@ -32,6 +32,7 @@ import io.netty.buffer.api.WritableComponent;
import io.netty.buffer.api.WritableComponentProcessor; import io.netty.buffer.api.WritableComponentProcessor;
import io.netty.buffer.api.internal.ArcDrop; import io.netty.buffer.api.internal.ArcDrop;
import io.netty.buffer.api.internal.Statics; import io.netty.buffer.api.internal.Statics;
import io.netty.util.IllegalReferenceCountException;
import io.netty.util.ReferenceCounted; import io.netty.util.ReferenceCounted;
import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.PlatformDependent;
@ -1245,6 +1246,9 @@ class NioBuffer extends ResourceSupport<Buffer, NioBuffer> implements Buffer, Re
@Override @Override
public boolean release(int decrement) { public boolean release(int decrement) {
if (!isAccessible() || decrement > 1 + countBorrows()) {
throw new IllegalReferenceCountException();
}
for (int i = 0; i < decrement; i++) { for (int i = 0; i < decrement; i++) {
close(); close();
} }

View File

@ -32,6 +32,7 @@ import io.netty.buffer.api.WritableComponent;
import io.netty.buffer.api.WritableComponentProcessor; import io.netty.buffer.api.WritableComponentProcessor;
import io.netty.buffer.api.internal.ArcDrop; import io.netty.buffer.api.internal.ArcDrop;
import io.netty.buffer.api.internal.Statics; import io.netty.buffer.api.internal.Statics;
import io.netty.util.IllegalReferenceCountException;
import io.netty.util.ReferenceCounted; import io.netty.util.ReferenceCounted;
import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.PlatformDependent;
@ -1660,6 +1661,9 @@ class UnsafeBuffer extends ResourceSupport<Buffer, UnsafeBuffer> implements Buff
@Override @Override
public boolean release(int decrement) { public boolean release(int decrement) {
if (!isAccessible() || decrement > 1 + countBorrows()) {
throw new IllegalReferenceCountException();
}
for (int i = 0; i < decrement; i++) { for (int i = 0; i < decrement; i++) {
close(); close();
} }

View File

@ -40,7 +40,7 @@ public class UnsafeMemoryManagers implements MemoryManagers {
} }
@Override @Override
public String implementationName() { public String getImplementationName() {
return "Unsafe"; return "Unsafe";
} }

View File

@ -32,6 +32,7 @@ import io.netty.buffer.api.WritableComponentProcessor;
import io.netty.buffer.api.Drop; import io.netty.buffer.api.Drop;
import io.netty.buffer.api.Owned; import io.netty.buffer.api.Owned;
import io.netty.buffer.api.internal.ResourceSupport; import io.netty.buffer.api.internal.ResourceSupport;
import io.netty.util.IllegalReferenceCountException;
import jdk.incubator.foreign.MemorySegment; import jdk.incubator.foreign.MemorySegment;
import jdk.incubator.foreign.ResourceScope; import jdk.incubator.foreign.ResourceScope;
@ -1266,6 +1267,9 @@ class MemSegBuffer extends ResourceSupport<Buffer, MemSegBuffer> implements Buff
@Override @Override
public boolean release(int decrement) { public boolean release(int decrement) {
if (!isAccessible() || decrement > 1 + countBorrows()) {
throw new IllegalReferenceCountException();
}
for (int i = 0; i < decrement; i++) { for (int i = 0; i < decrement; i++) {
close(); close();
} }

View File

@ -30,7 +30,7 @@ public class SegmentMemoryManagers implements MemoryManagers {
} }
@Override @Override
public String implementationName() { public String getImplementationName() {
return "MemorySegment"; return "MemorySegment";
} }

View File

@ -61,6 +61,7 @@ import static io.netty.buffer.Unpooled.directBuffer;
import static io.netty.buffer.Unpooled.unreleasableBuffer; import static io.netty.buffer.Unpooled.unreleasableBuffer;
import static io.netty.buffer.Unpooled.wrappedBuffer; import static io.netty.buffer.Unpooled.wrappedBuffer;
import static io.netty.util.internal.EmptyArrays.EMPTY_BYTES; import static io.netty.util.internal.EmptyArrays.EMPTY_BYTES;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
@ -3463,6 +3464,15 @@ public abstract class AbstractByteBufTest {
assertThrows(IllegalReferenceCountException.class, () -> releasedBuffer().release()); assertThrows(IllegalReferenceCountException.class, () -> releasedBuffer().release());
} }
@Test
public void overReleasingMustNotCloseBuffer() {
ByteBuf buf = newBuffer(1);
assertThrows(IllegalReferenceCountException.class, () -> buf.release(10));
assertThrows(IllegalReferenceCountException.class, () -> buf.release(2));
assertThat(buf.refCnt()).isNotZero();
assertTrue(buf.release());
}
private static void assertDuplicateFailAfterRelease(ByteBuf... bufs) { private static void assertDuplicateFailAfterRelease(ByteBuf... bufs) {
for (ByteBuf buf : bufs) { for (ByteBuf buf : bufs) {
if (buf.refCnt() > 0) { if (buf.refCnt() > 0) {