Better method names and javadocs in RcSupport

This commit is contained in:
Chris Vest 2020-12-11 12:09:32 +01:00
parent 3cddd2b0b2
commit 02eb4286fa
2 changed files with 16 additions and 5 deletions

View File

@ -16,7 +16,6 @@
package io.netty.buffer.api; package io.netty.buffer.api;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function;
public abstract class RcSupport<I extends Rc<I>, T extends RcSupport<I, T>> implements Rc<I> { public abstract class RcSupport<I extends Rc<I>, T extends RcSupport<I, T>> implements Rc<I> {
private int acquires; // Closed if negative. private int acquires; // Closed if negative.
@ -83,6 +82,11 @@ public abstract class RcSupport<I extends Rc<I>, T extends RcSupport<I, T>> impl
return new TransferSend<I, T>(owned, drop); return new TransferSend<I, T>(owned, drop);
} }
/**
* Create an {@link IllegalStateException} with a custom message, tailored to this particular {@link Rc} instance,
* for when the object cannot be sent for some reason.
* @return An {@link IllegalStateException} to be thrown when this object cannot be sent.
*/
protected IllegalStateException notSendableException() { protected IllegalStateException notSendableException() {
return new IllegalStateException( return new IllegalStateException(
"Cannot send() a reference counted object with " + acquires + " outstanding acquires: " + this + '.'); "Cannot send() a reference counted object with " + acquires + " outstanding acquires: " + this + '.');
@ -111,15 +115,21 @@ public abstract class RcSupport<I extends Rc<I>, T extends RcSupport<I, T>> impl
/** /**
* Get access to the underlying {@link Drop} object. * Get access to the underlying {@link Drop} object.
* This method is unsafe because it open the possibility of bypassing and overriding resource lifetimes. * This method is unsafe because it open the possibility of bypassing and overriding resource lifetimes.
*
* @return The {@link Drop} object used by this reference counted object. * @return The {@link Drop} object used by this reference counted object.
*/ */
protected Drop<T> unsafeGetDrop() { protected Drop<T> unsafeGetDrop() {
return drop; return drop;
} }
protected Drop<T> unsafeExchangeDrop(Drop<T> replacement) { /**
* Replace the current underlying {@link Drop} object with the given one.
* This method is unsafe because it open the possibility of bypassing and overring resource lifetimes.
*
* @param replacement The new {@link Drop} object to use instead of the current one.
*/
protected void unsafeSetDrop(Drop<T> replacement) {
drop = Objects.requireNonNull(replacement, "Replacement drop cannot be null."); drop = Objects.requireNonNull(replacement, "Replacement drop cannot be null.");
return replacement;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -322,7 +322,7 @@ class MemSegBuf extends RcSupport<Buf, MemSegBuf> implements Buf {
// Disconnect from the bifurcated drop, since we'll get our own fresh memory segment. // Disconnect from the bifurcated drop, since we'll get our own fresh memory segment.
drop.drop(this); drop.drop(this);
drop = ((BifurcatedDrop<MemSegBuf>) drop).unwrap(); drop = ((BifurcatedDrop<MemSegBuf>) drop).unwrap();
unsafeExchangeDrop(drop); unsafeSetDrop(drop);
} else { } else {
alloc.recoverMemory(recoverableMemory()); alloc.recoverMemory(recoverableMemory());
} }
@ -345,7 +345,8 @@ class MemSegBuf extends RcSupport<Buf, MemSegBuf> implements Buf {
if (drop instanceof BifurcatedDrop) { if (drop instanceof BifurcatedDrop) {
((BifurcatedDrop<?>) drop).increment(); ((BifurcatedDrop<?>) drop).increment();
} else { } else {
drop = unsafeExchangeDrop(new BifurcatedDrop<MemSegBuf>(new MemSegBuf(seg, drop, alloc), drop)); drop = new BifurcatedDrop<MemSegBuf>(new MemSegBuf(seg, drop, alloc), drop);
unsafeSetDrop(drop);
} }
var bifurcatedSeg = seg.asSlice(0, woff); var bifurcatedSeg = seg.asSlice(0, woff);
var bifurcatedBuf = new MemSegBuf(bifurcatedSeg, drop, alloc); var bifurcatedBuf = new MemSegBuf(bifurcatedSeg, drop, alloc);