Reduce conditionals in AbstractReferenceCountedByteBuf

Motivation:
AbstractReferenceCountedByteBuf as independent conditional statements to check the bounds of the retain IllegalReferenceCountException condition. One of the exceptions also uses the incorrect increment. The same fix was done for AbstractReferenceCounted as 01523e7835.

Modifications:
- Combined independent conditional checks into 1 where possible
- Correct IllegalReferenceCountException with incorrect increment
- Remove the subtract to check for overflow and re-use the addition and check for overflow to remove 1 arithmetic operation (see http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.2)

Result:
AbstractReferenceCountedByteBuf has less independent branch statements and more correct IllegalReferenceCountException. Compilation size of AbstractReferenceCountedByteBuf.retain() is reduced.
This commit is contained in:
Norman Maurer 2016-08-02 16:40:31 +02:00
parent aa6e6ae307
commit 3fa8f31055

View File

@ -59,11 +59,8 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
public ByteBuf retain() { public ByteBuf retain() {
for (;;) { for (;;) {
int refCnt = this.refCnt; int refCnt = this.refCnt;
if (refCnt == 0) { if (refCnt == 0 || refCnt == Integer.MAX_VALUE) {
throw new IllegalReferenceCountException(0, 1); throw new IllegalReferenceCountException(refCnt, 1);
}
if (refCnt == Integer.MAX_VALUE) {
throw new IllegalReferenceCountException(Integer.MAX_VALUE, 1);
} }
if (refCntUpdater.compareAndSet(this, refCnt, refCnt + 1)) { if (refCntUpdater.compareAndSet(this, refCnt, refCnt + 1)) {
break; break;
@ -79,14 +76,12 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
} }
for (;;) { for (;;) {
final int nextCnt;
int refCnt = this.refCnt; int refCnt = this.refCnt;
if (refCnt == 0) { if (refCnt == 0 || (nextCnt = refCnt + increment) < 0) {
throw new IllegalReferenceCountException(0, increment);
}
if (refCnt > Integer.MAX_VALUE - increment) {
throw new IllegalReferenceCountException(refCnt, increment); throw new IllegalReferenceCountException(refCnt, increment);
} }
if (refCntUpdater.compareAndSet(this, refCnt, refCnt + increment)) { if (refCntUpdater.compareAndSet(this, refCnt, nextCnt)) {
break; break;
} }
} }