Move ReferenceCounted and AbstractReferenceCounted to io.netty.util

- Fixes #1441
- Also move and rename IllegalBufferAccessException to ReferenceCountException
- Prettier reference count exception messages
This commit is contained in:
Trustin Lee 2013-06-13 13:14:21 +09:00
parent 283feda119
commit 175526b6bd
20 changed files with 87 additions and 69 deletions

View File

@ -15,6 +15,7 @@
*/
package io.netty.buffer;
import io.netty.util.ReferenceCountException;
import io.netty.util.ResourceLeakDetector;
import java.io.IOException;
@ -1133,7 +1134,7 @@ public abstract class AbstractByteBuf implements ByteBuf {
*/
protected final void ensureAccessible() {
if (refCnt() == 0) {
throw new IllegalBufferAccessException();
throw new ReferenceCountException(0);
}
}
}

View File

@ -16,6 +16,7 @@
package io.netty.buffer;
import io.netty.util.ReferenceCountException;
import io.netty.util.internal.PlatformDependent;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
@ -73,10 +74,10 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
for (;;) {
int refCnt = this.refCnt;
if (refCnt == 0) {
throw new IllegalBufferAccessException();
throw new ReferenceCountException(0, 1);
}
if (refCnt == Integer.MAX_VALUE) {
throw new IllegalBufferAccessException("refCnt overflow");
throw new ReferenceCountException(Integer.MAX_VALUE, 1);
}
if (refCntUpdater.compareAndSet(this, refCnt, refCnt + 1)) {
break;
@ -94,10 +95,10 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
for (;;) {
int refCnt = this.refCnt;
if (refCnt == 0) {
throw new IllegalBufferAccessException();
throw new ReferenceCountException(0, increment);
}
if (refCnt > Integer.MAX_VALUE - increment) {
throw new IllegalBufferAccessException("refCnt overflow");
throw new ReferenceCountException(refCnt, increment);
}
if (refCntUpdater.compareAndSet(this, refCnt, refCnt + increment)) {
break;
@ -111,7 +112,7 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
for (;;) {
int refCnt = this.refCnt;
if (refCnt == 0) {
throw new IllegalBufferAccessException();
throw new ReferenceCountException(0, -1);
}
if (refCntUpdater.compareAndSet(this, refCnt, refCnt - 1)) {
@ -133,7 +134,7 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
for (;;) {
int refCnt = this.refCnt;
if (refCnt < decrement) {
throw new IllegalBufferAccessException();
throw new ReferenceCountException(refCnt, -decrement);
}
if (refCntUpdater.compareAndSet(this, refCnt, refCnt - decrement)) {

View File

@ -15,6 +15,8 @@
*/
package io.netty.buffer;
import io.netty.util.ReferenceCounted;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

View File

@ -15,6 +15,8 @@
*/
package io.netty.buffer;
import io.netty.util.ReferenceCounted;
/**
* A packet which is send or receive.
*/

View File

@ -16,6 +16,7 @@
package io.netty.buffer;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCounted;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

View File

@ -15,6 +15,8 @@
*/
package io.netty.buffer;
import io.netty.util.ReferenceCountException;
/**
* Default implementation of a {@link ByteBufHolder} that holds it's data in a {@link ByteBuf}.
*
@ -33,7 +35,7 @@ public class DefaultByteBufHolder implements ByteBufHolder {
@Override
public ByteBuf content() {
if (data.refCnt() <= 0) {
throw new IllegalBufferAccessException();
throw new ReferenceCountException(data.refCnt());
}
return data;
}

View File

@ -1,40 +0,0 @@
/*
* Copyright 2012 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;
/**
* An {@link IllegalStateException} raised when a user attempts to access a {@link Buf} which was freed by
* {@link Buf#release()} } already.
*/
public class IllegalBufferAccessException extends IllegalStateException {
private static final long serialVersionUID = -6734326916218551327L;
public IllegalBufferAccessException() { }
public IllegalBufferAccessException(String message) {
super(message);
}
public IllegalBufferAccessException(String message, Throwable cause) {
super(message, cause);
}
public IllegalBufferAccessException(Throwable cause) {
super(cause);
}
}

View File

@ -15,10 +15,10 @@
*/
package io.netty.handler.codec.http.multipart;
import io.netty.buffer.AbstractReferenceCounted;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelException;
import io.netty.handler.codec.http.HttpConstants;
import io.netty.util.AbstractReferenceCounted;
import java.io.IOException;
import java.nio.charset.Charset;

View File

@ -16,8 +16,8 @@
package io.netty.handler.codec.spdy;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.IllegalBufferAccessException;
import io.netty.buffer.Unpooled;
import io.netty.util.ReferenceCountException;
import io.netty.util.internal.StringUtil;
/**
@ -73,7 +73,7 @@ public class DefaultSpdyDataFrame extends DefaultSpdyStreamFrame implements Spdy
@Override
public ByteBuf content() {
if (data.refCnt() <= 0) {
throw new IllegalBufferAccessException();
throw new ReferenceCountException(data.refCnt());
}
return data;
}

View File

@ -15,11 +15,11 @@
*/
package io.netty.handler.codec;
import io.netty.buffer.ReferenceCounted;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.channel.MessageList;
import io.netty.util.ReferenceCounted;
import io.netty.util.internal.TypeParameterMatcher;
/**

View File

@ -16,10 +16,10 @@
package io.netty.handler.codec;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.ReferenceCounted;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.MessageList;
import io.netty.util.ReferenceCounted;
import io.netty.util.internal.TypeParameterMatcher;
/**

View File

@ -16,11 +16,11 @@
package io.netty.handler.codec;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.ReferenceCounted;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.channel.MessageList;
import io.netty.util.ReferenceCounted;
import io.netty.util.internal.TypeParameterMatcher;
/**

View File

@ -13,7 +13,7 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.buffer;
package io.netty.util;
import io.netty.util.internal.PlatformDependent;
@ -68,10 +68,10 @@ public abstract class AbstractReferenceCounted implements ReferenceCounted {
for (;;) {
int refCnt = this.refCnt;
if (refCnt == 0) {
throw new IllegalBufferAccessException();
throw new ReferenceCountException(0, 1);
}
if (refCnt == Integer.MAX_VALUE) {
throw new IllegalBufferAccessException("refCnt overflow");
throw new ReferenceCountException(Integer.MAX_VALUE, 1);
}
if (refCntUpdater.compareAndSet(this, refCnt, refCnt + 1)) {
break;
@ -89,10 +89,10 @@ public abstract class AbstractReferenceCounted implements ReferenceCounted {
for (;;) {
int refCnt = this.refCnt;
if (refCnt == 0) {
throw new IllegalBufferAccessException();
throw new ReferenceCountException(0, 1);
}
if (refCnt > Integer.MAX_VALUE - increment) {
throw new IllegalBufferAccessException("refCnt overflow");
throw new ReferenceCountException(refCnt, increment);
}
if (refCntUpdater.compareAndSet(this, refCnt, refCnt + increment)) {
break;
@ -106,7 +106,7 @@ public abstract class AbstractReferenceCounted implements ReferenceCounted {
for (;;) {
int refCnt = this.refCnt;
if (refCnt == 0) {
throw new IllegalBufferAccessException();
throw new ReferenceCountException(0, -1);
}
if (refCntUpdater.compareAndSet(this, refCnt, refCnt - 1)) {
@ -128,7 +128,7 @@ public abstract class AbstractReferenceCounted implements ReferenceCounted {
for (;;) {
int refCnt = this.refCnt;
if (refCnt < decrement) {
throw new IllegalBufferAccessException();
throw new ReferenceCountException(refCnt, -decrement);
}
if (refCntUpdater.compareAndSet(this, refCnt, refCnt - decrement)) {

View File

@ -0,0 +1,48 @@
/*
* Copyright 2012 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.util;
/**
* An {@link IllegalStateException} which is raised when a user attempts to access a {@link ReferenceCounted} whose
* reference count has been decreased to 0 (and consequently freed).
*/
public class ReferenceCountException extends IllegalStateException {
private static final long serialVersionUID = -2507492394288153468L;
public ReferenceCountException() { }
public ReferenceCountException(int refCnt) {
this("refCnt: " + refCnt);
}
public ReferenceCountException(int refCnt, int increment) {
this("refCnt: " + refCnt + ", " + (increment > 0? "increment: " + increment : "decrement: " + -increment));
}
public ReferenceCountException(String message) {
super(message);
}
public ReferenceCountException(String message, Throwable cause) {
super(message, cause);
}
public ReferenceCountException(Throwable cause) {
super(cause);
}
}

View File

@ -13,7 +13,7 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.buffer;
package io.netty.util;
/**
* A reference-counted object that requires explicit deallocation.
@ -24,8 +24,9 @@ package io.netty.buffer;
* the deallocated object will usually result in an access violation.
* </p>
* <p>
* If this object is a container such as {@link MessageBuf} and {@link CompositeByteBuf}, its elements whose type is
* {@link ReferenceCounted} will also be released when this object is deallocated.
* If an object that implements {@link ReferenceCounted} is a container of other objects that implement
* {@link ReferenceCounted}, the contained objects will also be released via {@link #release()} when the container's
* reference count becomes 0.
* </p>
*/
public interface ReferenceCounted {

View File

@ -16,7 +16,7 @@
package io.netty.channel;
import io.netty.buffer.ReferenceCounted;
import io.netty.util.ReferenceCounted;
import java.net.SocketAddress;

View File

@ -17,7 +17,7 @@
package io.netty.channel;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.ReferenceCounted;
import io.netty.util.ReferenceCounted;
import io.netty.util.internal.StringUtil;
import java.net.SocketAddress;

View File

@ -15,7 +15,7 @@
*/
package io.netty.channel;
import io.netty.buffer.AbstractReferenceCounted;
import io.netty.util.AbstractReferenceCounted;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

View File

@ -15,7 +15,7 @@
*/
package io.netty.channel;
import io.netty.buffer.ReferenceCounted;
import io.netty.util.ReferenceCounted;
import java.io.IOException;
import java.nio.channels.FileChannel;

View File

@ -18,12 +18,12 @@ package io.netty.channel;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ReferenceCounted;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.local.LocalAddress;
import io.netty.channel.local.LocalChannel;
import io.netty.channel.local.LocalEventLoopGroup;
import io.netty.channel.local.LocalServerChannel;
import io.netty.util.ReferenceCounted;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Test;