2013-02-10 13:10:09 +09:00
|
|
|
/*
|
|
|
|
* Copyright 2013 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.buffer;
|
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
|
|
|
|
|
|
|
|
public abstract class AbstractReferenceCounted implements ReferenceCounted {
|
|
|
|
|
|
|
|
private static final AtomicIntegerFieldUpdater<AbstractReferenceCounted> refCntUpdater =
|
|
|
|
AtomicIntegerFieldUpdater.newUpdater(AbstractReferenceCounted.class, "refCnt");
|
|
|
|
|
|
|
|
@SuppressWarnings("FieldMayBeFinal")
|
|
|
|
private volatile int refCnt = 1;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int refCnt() {
|
|
|
|
return refCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-02-13 08:09:33 +01:00
|
|
|
public ReferenceCounted retain() {
|
2013-02-10 14:22:14 +09:00
|
|
|
for (;;) {
|
2013-02-10 13:10:09 +09:00
|
|
|
int refCnt = this.refCnt;
|
2013-02-10 14:22:14 +09:00
|
|
|
if (refCnt == 0) {
|
2013-02-10 13:10:09 +09:00
|
|
|
throw new IllegalBufferAccessException();
|
|
|
|
}
|
|
|
|
if (refCnt == Integer.MAX_VALUE) {
|
|
|
|
throw new IllegalBufferAccessException("refCnt overflow");
|
|
|
|
}
|
2013-02-10 14:22:14 +09:00
|
|
|
if (refCntUpdater.compareAndSet(this, refCnt, refCnt + 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-02-13 08:09:33 +01:00
|
|
|
return this;
|
2013-02-10 13:10:09 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-02-13 08:09:33 +01:00
|
|
|
public ReferenceCounted retain(int increment) {
|
2013-02-10 13:10:09 +09:00
|
|
|
if (increment <= 0) {
|
|
|
|
throw new IllegalArgumentException("increment: " + increment + " (expected: > 0)");
|
|
|
|
}
|
|
|
|
|
2013-02-10 14:22:14 +09:00
|
|
|
for (;;) {
|
2013-02-10 13:10:09 +09:00
|
|
|
int refCnt = this.refCnt;
|
2013-02-10 14:22:14 +09:00
|
|
|
if (refCnt == 0) {
|
2013-02-10 13:10:09 +09:00
|
|
|
throw new IllegalBufferAccessException();
|
|
|
|
}
|
|
|
|
if (refCnt > Integer.MAX_VALUE - increment) {
|
|
|
|
throw new IllegalBufferAccessException("refCnt overflow");
|
|
|
|
}
|
2013-02-10 14:22:14 +09:00
|
|
|
if (refCntUpdater.compareAndSet(this, refCnt, refCnt + increment)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-02-13 08:09:33 +01:00
|
|
|
return this;
|
2013-02-10 13:10:09 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final boolean release() {
|
|
|
|
for (;;) {
|
|
|
|
int refCnt = this.refCnt;
|
2013-02-10 14:22:14 +09:00
|
|
|
if (refCnt == 0) {
|
2013-02-10 13:10:09 +09:00
|
|
|
throw new IllegalBufferAccessException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (refCntUpdater.compareAndSet(this, refCnt, refCnt - 1)) {
|
|
|
|
if (refCnt == 1) {
|
|
|
|
deallocate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final boolean release(int decrement) {
|
|
|
|
if (decrement <= 0) {
|
|
|
|
throw new IllegalArgumentException("decrement: " + decrement + " (expected: > 0)");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int refCnt = this.refCnt;
|
|
|
|
if (refCnt < decrement) {
|
|
|
|
throw new IllegalBufferAccessException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (refCntUpdater.compareAndSet(this, refCnt, refCnt - decrement)) {
|
|
|
|
if (refCnt == decrement) {
|
|
|
|
deallocate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract void deallocate();
|
|
|
|
}
|