diff --git a/src/main/java/io/netty/buffer/api/BufferHolder.java b/src/main/java/io/netty/buffer/api/BufferHolder.java index 6a1f6db..e17600d 100644 --- a/src/main/java/io/netty/buffer/api/BufferHolder.java +++ b/src/main/java/io/netty/buffer/api/BufferHolder.java @@ -78,11 +78,6 @@ public abstract class BufferHolder> implements Rc { return buf.isOwned(); } - @Override - public int countBorrows() { - return buf.countBorrows(); - } - @SuppressWarnings("unchecked") @Override public Send send() { diff --git a/src/main/java/io/netty/buffer/api/Rc.java b/src/main/java/io/netty/buffer/api/Rc.java index 6ec164e..48d776a 100644 --- a/src/main/java/io/netty/buffer/api/Rc.java +++ b/src/main/java/io/netty/buffer/api/Rc.java @@ -69,15 +69,6 @@ public interface Rc> extends AutoCloseable { */ boolean isOwned(); - /** - * Count the number of borrows of this object. - * Note that even if the number of borrows is {@code 0}, this object might not be {@linkplain #isOwned() owned} - * because there could be other restrictions involved in ownership. - * - * @return The number of borrows, if any, of this object. - */ - int countBorrows(); - /** * Check if this object is accessible. * diff --git a/src/main/java/io/netty/buffer/api/RcSupport.java b/src/main/java/io/netty/buffer/api/RcSupport.java index 99a4310..6448adc 100644 --- a/src/main/java/io/netty/buffer/api/RcSupport.java +++ b/src/main/java/io/netty/buffer/api/RcSupport.java @@ -106,7 +106,13 @@ public abstract class RcSupport, T extends RcSupport> impl return acquires == 0; } - @Override + /** + * Count the number of borrows of this object. + * Note that even if the number of borrows is {@code 0}, this object might not be {@linkplain #isOwned() owned} + * because there could be other restrictions involved in ownership. + * + * @return The number of borrows, if any, of this object. + */ public int countBorrows() { return Math.max(acquires, 0); } diff --git a/src/main/java/io/netty/buffer/api/adaptor/ByteBufAdaptor.java b/src/main/java/io/netty/buffer/api/adaptor/ByteBufAdaptor.java index fa0ec00..619e0ff 100644 --- a/src/main/java/io/netty/buffer/api/adaptor/ByteBufAdaptor.java +++ b/src/main/java/io/netty/buffer/api/adaptor/ByteBufAdaptor.java @@ -24,6 +24,7 @@ import io.netty.buffer.SlicedByteBuf; import io.netty.buffer.Unpooled; import io.netty.buffer.api.Buffer; import io.netty.buffer.api.BufferAllocator; +import io.netty.buffer.api.RcSupport; import io.netty.util.ByteProcessor; import io.netty.util.IllegalReferenceCountException; @@ -212,13 +213,13 @@ public final class ByteBufAdaptor extends ByteBuf { public ByteBuf ensureWritable(int minWritableBytes) { checkAccess(); if (writableBytes() < minWritableBytes) { - int borrows = buffer.countBorrows(); try { - if (borrows == 0) { + if (buffer.isOwned()) { // Good place. buffer.ensureWritable(minWritableBytes); } else { // Highly questionable place, but ByteBuf technically allows this, so we have to emulate. + int borrows = countBorrows(); release(borrows); try { buffer.ensureWritable(minWritableBytes); @@ -1650,7 +1651,18 @@ public final class ByteBufAdaptor extends ByteBuf { @Override public int refCnt() { - return buffer.isAccessible()? 1 + buffer.countBorrows() : 0; + return 1 + countBorrows(); + } + + private int countBorrows() { + if (!buffer.isAccessible()) { + return -1; + } + if (buffer instanceof RcSupport) { + var rc = (RcSupport) buffer; + return rc.countBorrows(); + } + return buffer.isOwned()? 0 : 1; } @Override