Remove ChannelBuf/ByteBuf.Unsafe

- Fixes #826
Unsafe.isFreed(), free(), suspend/resumeIntermediaryAllocations() are not that dangerous. internalNioBuffer() and internalNioBuffers() are dangerous but it seems like nobody is using it even inside Netty. Removing those two methods also removes the necessity to keep Unsafe interface at all.
This commit is contained in:
Trustin Lee 2012-12-17 17:41:21 +09:00
parent 33134b1343
commit 03e68482bb
32 changed files with 228 additions and 335 deletions

View File

@ -171,6 +171,27 @@ public abstract class AbstractByteBuf implements ByteBuf {
return this;
}
@Override
public ByteBuf discardSomeReadBytes() {
if (readerIndex == 0) {
return this;
}
if (readerIndex == writerIndex) {
adjustMarkers(readerIndex);
writerIndex = readerIndex = 0;
return this;
}
if (readerIndex >= capacity() >>> 1) {
setBytes(0, this, readerIndex, writerIndex - readerIndex);
writerIndex -= readerIndex;
adjustMarkers(readerIndex);
readerIndex = 0;
}
return this;
}
protected void adjustMarkers(int decrement) {
markedReaderIndex = Math.max(markedReaderIndex - decrement, 0);
markedWriterIndex = Math.max(markedWriterIndex - decrement, 0);

View File

@ -457,6 +457,14 @@ public interface ByteBuf extends ChannelBuf, Comparable<ByteBuf> {
*/
ByteBuf discardReadBytes();
/**
* Similar to {@link ByteBuf#discardReadBytes()} except that this method might discard
* some, all, or none of read bytes depending on its internal implementation to reduce
* overall memory bandwidth consumption at the cost of potentially additional memory
* consumption.
*/
ByteBuf discardSomeReadBytes();
/**
* Makes sure the number of {@linkplain #writableBytes() the writable bytes}
* is equal to or greater than the specified value. If there is enough
@ -1827,6 +1835,23 @@ public interface ByteBuf extends ChannelBuf, Comparable<ByteBuf> {
*/
String toString(int index, int length, Charset charset);
/**
* Suspends the intermediary deallocation of the internal memory block of this buffer until asked via
* {@link #resumeIntermediaryDeallocations()}. An intermediary deallocation is usually made when the capacity of
* a buffer changes.
*
* @throws UnsupportedOperationException if this buffer is derived
*/
ByteBuf suspendIntermediaryDeallocations();
/**
* Resumes the intermediary deallocation of the internal memory block of this buffer, suspended by
* {@link #suspendIntermediaryDeallocations()}.
*
* @throws UnsupportedOperationException if this buffer is derived
*/
ByteBuf resumeIntermediaryDeallocations();
/**
* Returns a hash code which was calculated from the content of this
* buffer. If there's a byte array which is
@ -1868,50 +1893,4 @@ public interface ByteBuf extends ChannelBuf, Comparable<ByteBuf> {
*/
@Override
String toString();
/**
* Provides access to potentially unsafe operations of this buffer.
*/
@Override
Unsafe unsafe();
/**
* Provides the potentially unsafe operations of {@link ByteBuf}.
*/
interface Unsafe extends ChannelBuf.Unsafe {
/**
* Returns the internal NIO buffer that is reused for I/O.
*
* @throws UnsupportedOperationException if the buffer has no internal NIO buffer
*/
ByteBuffer internalNioBuffer();
/**
* Returns the internal NIO buffer array that is reused for I/O.
*
* @throws UnsupportedOperationException if the buffer has no internal NIO buffer array
*/
ByteBuffer[] internalNioBuffers();
/**
* Similar to {@link ByteBuf#discardReadBytes()} except that this method might discard
* some, all, or none of read bytes depending on its internal implementation to reduce
* overall memory bandwidth consumption at the cost of potentially additional memory
* consumption.
*/
void discardSomeReadBytes();
/**
* Suspends the intermediary deallocation of the internal memory block of this buffer until asked via
* {@link #resumeIntermediaryDeallocations()}. An intermediary deallocation is usually made when the capacity of
* a buffer changes.
*/
void suspendIntermediaryDeallocations();
/**
* Resumes the intermediary deallocation of the internal memory block of this buffer, suspended by
* {@link #suspendIntermediaryDeallocations()}.
*/
void resumeIntermediaryDeallocations();
}
}

View File

@ -22,23 +22,15 @@ public interface ChannelBuf {
ChannelBufType type();
/**
* Provides access to potentially unsafe operations of this buffer.
* Returns {@code true} if and only if this buffer has been deallocated by {@link #free()}.
*/
Unsafe unsafe();
boolean isFreed();
/**
* Provides the potentially unsafe operations of {@link ByteBuf}.
* Deallocates the internal memory block of this buffer or returns it to the allocator or pool it came from.
* The result of accessing a released buffer is unspecified and can even cause JVM crash.
*
* @throws UnsupportedOperationException if this buffer is derived
*/
interface Unsafe {
/**
* Returns {@code true} if and only if this buffer has been deallocated by {@link #free()}.
*/
boolean isFreed();
/**
* Deallocates the internal memory block of this buffer or returns it to the {@link ByteBufAllocator} it came
* from. The result of accessing a released buffer is unspecified and can even cause JVM crash.
*/
void free();
}
void free();
}

View File

@ -81,6 +81,9 @@ public interface CompositeByteBuf extends ByteBuf, Iterable<ByteBuf> {
@Override
CompositeByteBuf discardReadBytes();
@Override
CompositeByteBuf discardSomeReadBytes();
@Override
CompositeByteBuf ensureWritableBytes(int minWritableBytes);
@ -225,4 +228,9 @@ public interface CompositeByteBuf extends ByteBuf, Iterable<ByteBuf> {
@Override
CompositeByteBuf writeZero(int length);
@Override
CompositeByteBuf suspendIntermediaryDeallocations();
@Override
CompositeByteBuf resumeIntermediaryDeallocations();
}

View File

@ -15,7 +15,6 @@
*/
package io.netty.buffer;
import io.netty.buffer.ByteBuf.Unsafe;
import io.netty.util.internal.DetectionUtil;
import java.io.IOException;
@ -40,7 +39,7 @@ import java.util.Queue;
* is recommended to use {@link Unpooled#wrappedBuffer(ByteBuf...)}
* instead of calling the constructor explicitly.
*/
public class DefaultCompositeByteBuf extends AbstractByteBuf implements CompositeByteBuf, Unsafe {
public class DefaultCompositeByteBuf extends AbstractByteBuf implements CompositeByteBuf {
private static final ByteBuffer[] EMPTY_NIOBUFFERS = new ByteBuffer[0];
@ -1292,7 +1291,7 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit
}
if (suspendedDeallocations == null) {
buf.unsafe().free(); // We should not get a NPE here. If so, it must be a bug.
buf.free(); // We should not get a NPE here. If so, it must be a bug.
} else {
suspendedDeallocations.add(buf);
}
@ -1525,26 +1524,8 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit
}
@Override
public ByteBuffer internalNioBuffer() {
if (components.size() == 1) {
return components.get(0).buf.unsafe().internalNioBuffer();
}
throw new UnsupportedOperationException();
}
@Override
public ByteBuffer[] internalNioBuffers() {
ByteBuffer[] nioBuffers = new ByteBuffer[components.size()];
int index = 0;
for (Component component : components) {
nioBuffers[index++] = component.buf.unsafe().internalNioBuffer();
}
return nioBuffers;
}
@Override
public void discardSomeReadBytes() {
discardReadComponents();
public CompositeByteBuf discardSomeReadBytes() {
return discardReadComponents();
}
@Override
@ -1566,33 +1547,30 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit
}
@Override
public void suspendIntermediaryDeallocations() {
public CompositeByteBuf suspendIntermediaryDeallocations() {
if (suspendedDeallocations == null) {
suspendedDeallocations = new ArrayDeque<ByteBuf>(2);
}
return this;
}
@Override
public void resumeIntermediaryDeallocations() {
public CompositeByteBuf resumeIntermediaryDeallocations() {
if (suspendedDeallocations == null) {
return;
return this;
}
Queue<ByteBuf> suspendedDeallocations = this.suspendedDeallocations;
this.suspendedDeallocations = null;
for (ByteBuf buf: suspendedDeallocations) {
buf.unsafe().free();
buf.free();
}
return this;
}
@Override
public ByteBuf unwrap() {
return null;
}
@Override
public Unsafe unsafe() {
return this;
}
}

View File

@ -15,12 +15,10 @@
*/
package io.netty.buffer;
import io.netty.buffer.ChannelBuf.Unsafe;
import java.util.ArrayDeque;
import java.util.Collection;
final class DefaultMessageBuf<T> extends ArrayDeque<T> implements MessageBuf<T>, Unsafe {
final class DefaultMessageBuf<T> extends ArrayDeque<T> implements MessageBuf<T> {
private static final long serialVersionUID = 1229808623624907552L;
@ -65,11 +63,6 @@ final class DefaultMessageBuf<T> extends ArrayDeque<T> implements MessageBuf<T>,
return cnt;
}
@Override
public Unsafe unsafe() {
return this;
}
@Override
public boolean isFreed() {
return freed;

View File

@ -15,8 +15,6 @@
*/
package io.netty.buffer;
import io.netty.buffer.ByteBuf.Unsafe;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -31,7 +29,7 @@ import java.nio.channels.ScatteringByteChannel;
* parent. It is recommended to use {@link ByteBuf#duplicate()} instead
* of calling the constructor explicitly.
*/
public class DuplicatedByteBuf extends AbstractByteBuf implements Unsafe {
public class DuplicatedByteBuf extends AbstractByteBuf {
private final ByteBuf buffer;
@ -234,38 +232,24 @@ public class DuplicatedByteBuf extends AbstractByteBuf implements Unsafe {
return buffer.nioBuffers(index, length);
}
@Override
public ByteBuffer internalNioBuffer() {
return buffer.unsafe().internalNioBuffer();
}
@Override
public ByteBuffer[] internalNioBuffers() {
return buffer.unsafe().internalNioBuffers();
}
@Override
public void discardSomeReadBytes() {
throw new UnsupportedOperationException();
}
@Override
public boolean isFreed() {
return buffer.unsafe().isFreed();
return buffer.isFreed();
}
@Override
public void free() { }
public void free() {
throw new UnsupportedOperationException("derived");
}
@Override
public void suspendIntermediaryDeallocations() { }
public ByteBuf suspendIntermediaryDeallocations() {
throw new UnsupportedOperationException("derived");
}
@Override
public void resumeIntermediaryDeallocations() { }
@Override
public Unsafe unsafe() {
return this;
public ByteBuf resumeIntermediaryDeallocations() {
throw new UnsupportedOperationException("derived");
}
}

View File

@ -0,0 +1,40 @@
/*
* 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;
/**
* A {@link RuntimeException} raised by a {@link ByteBuf} where a user requested an operation on a freed
* {@link ByteBuf}.
*/
public class IllegalMemoryAccessException extends RuntimeException {
private static final long serialVersionUID = -6734326916218551327L;
public IllegalMemoryAccessException() { }
public IllegalMemoryAccessException(String message) {
super(message);
}
public IllegalMemoryAccessException(String message, Throwable cause) {
super(message, cause);
}
public IllegalMemoryAccessException(Throwable cause) {
super(cause);
}
}

View File

@ -16,14 +16,12 @@
package io.netty.buffer;
import io.netty.buffer.ByteBuf.Unsafe;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayDeque;
import java.util.Queue;
abstract class PooledByteBuf<T> extends AbstractByteBuf implements Unsafe {
abstract class PooledByteBuf<T> extends AbstractByteBuf {
protected PoolChunk<T> chunk;
protected long handle;
@ -85,13 +83,7 @@ abstract class PooledByteBuf<T> extends AbstractByteBuf implements Unsafe {
return null;
}
@Override
public Unsafe unsafe() {
return this;
}
@Override
public ByteBuffer internalNioBuffer() {
protected ByteBuffer internalNioBuffer() {
ByteBuffer tmpNioBuf = this.tmpNioBuf;
if (tmpNioBuf == null) {
this.tmpNioBuf = tmpNioBuf = newInternalNioBuffer(memory);
@ -99,50 +91,34 @@ abstract class PooledByteBuf<T> extends AbstractByteBuf implements Unsafe {
return tmpNioBuf;
}
@Override
public ByteBuffer[] internalNioBuffers() {
return new ByteBuffer[] { internalNioBuffer() };
}
protected abstract ByteBuffer newInternalNioBuffer(T memory);
@Override
public void discardSomeReadBytes() {
final int readerIndex = readerIndex();
if (readerIndex == writerIndex()) {
discardReadBytes();
return;
}
if (readerIndex > 0 && readerIndex >= capacity() >>> 1) {
discardReadBytes();
}
}
@Override
public void suspendIntermediaryDeallocations() {
public ByteBuf suspendIntermediaryDeallocations() {
assert !isFreed();
if (suspendedDeallocations == null) {
suspendedDeallocations = new ArrayDeque<Allocation<T>>(2);
}
return this;
}
@Override
public void resumeIntermediaryDeallocations() {
public ByteBuf resumeIntermediaryDeallocations() {
if (suspendedDeallocations == null) {
return;
return this;
}
Queue<Allocation<T>> suspendedDeallocations = this.suspendedDeallocations;
this.suspendedDeallocations = null;
if (suspendedDeallocations.isEmpty()) {
return;
return this;
}
for (Allocation<T> a: suspendedDeallocations) {
chunk.arena.free(a.chunk, a.handle);
}
return this;
}
@Override

View File

@ -15,13 +15,11 @@
*/
package io.netty.buffer;
import io.netty.buffer.ChannelBuf.Unsafe;
import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;
final class QueueBackedMessageBuf<T> implements MessageBuf<T>, Unsafe {
final class QueueBackedMessageBuf<T> implements MessageBuf<T> {
private final Queue<T> queue;
private boolean freed;
@ -156,11 +154,6 @@ final class QueueBackedMessageBuf<T> implements MessageBuf<T>, Unsafe {
return cnt;
}
@Override
public Unsafe unsafe() {
return this;
}
@Override
public boolean isFreed() {
return freed;

View File

@ -15,8 +15,6 @@
*/
package io.netty.buffer;
import io.netty.buffer.ByteBuf.Unsafe;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -31,7 +29,7 @@ import java.nio.channels.ScatteringByteChannel;
* recommended to use {@link Unpooled#unmodifiableBuffer(ByteBuf)}
* instead of calling the constructor explicitly.
*/
public class ReadOnlyByteBuf extends AbstractByteBuf implements Unsafe {
public class ReadOnlyByteBuf extends AbstractByteBuf {
private final ByteBuf buffer;
@ -232,37 +230,23 @@ public class ReadOnlyByteBuf extends AbstractByteBuf implements Unsafe {
throw new ReadOnlyBufferException();
}
@Override
public ByteBuffer internalNioBuffer() {
return buffer.unsafe().internalNioBuffer();
}
@Override
public ByteBuffer[] internalNioBuffers() {
return buffer.unsafe().internalNioBuffers();
}
@Override
public void discardSomeReadBytes() {
throw new UnsupportedOperationException();
}
@Override
public boolean isFreed() {
return buffer.unsafe().isFreed();
return buffer.isFreed();
}
@Override
public void free() { }
public void free() {
throw new UnsupportedOperationException("derived");
}
@Override
public void suspendIntermediaryDeallocations() { }
public ByteBuf suspendIntermediaryDeallocations() {
throw new UnsupportedOperationException("derived");
}
@Override
public void resumeIntermediaryDeallocations() { }
@Override
public Unsafe unsafe() {
return this;
public ByteBuf resumeIntermediaryDeallocations() {
throw new UnsupportedOperationException("derived");
}
}

View File

@ -15,8 +15,6 @@
*/
package io.netty.buffer;
import io.netty.buffer.ByteBuf.Unsafe;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -32,7 +30,7 @@ import java.nio.channels.ScatteringByteChannel;
* {@link ByteBuf#slice(int, int)} instead of calling the constructor
* explicitly.
*/
public class SlicedByteBuf extends AbstractByteBuf implements Unsafe {
public class SlicedByteBuf extends AbstractByteBuf {
private final ByteBuf buffer;
private final int adjustment;
@ -306,37 +304,23 @@ public class SlicedByteBuf extends AbstractByteBuf implements Unsafe {
}
}
@Override
public ByteBuffer internalNioBuffer() {
return buffer.nioBuffer(adjustment, length);
}
@Override
public ByteBuffer[] internalNioBuffers() {
return buffer.nioBuffers(adjustment, length);
}
@Override
public void discardSomeReadBytes() {
throw new UnsupportedOperationException();
}
@Override
public boolean isFreed() {
return buffer.unsafe().isFreed();
return buffer.isFreed();
}
@Override
public void free() { }
public void free() {
throw new UnsupportedOperationException("derived");
}
@Override
public void suspendIntermediaryDeallocations() { }
public ByteBuf suspendIntermediaryDeallocations() {
throw new UnsupportedOperationException("derived");
}
@Override
public void resumeIntermediaryDeallocations() { }
@Override
public Unsafe unsafe() {
return this;
public ByteBuf resumeIntermediaryDeallocations() {
throw new UnsupportedOperationException("derived");
}
}

View File

@ -182,6 +182,12 @@ public final class SwappedByteBuf implements ByteBuf {
return this;
}
@Override
public ByteBuf discardSomeReadBytes() {
buf.discardSomeReadBytes();
return this;
}
@Override
public ByteBuf ensureWritableBytes(int writableBytes) {
buf.ensureWritableBytes(writableBytes);
@ -768,6 +774,28 @@ public final class SwappedByteBuf implements ByteBuf {
return buf.toString(index, length, charset);
}
@Override
public ByteBuf suspendIntermediaryDeallocations() {
buf.suspendIntermediaryDeallocations();
return this;
}
@Override
public ByteBuf resumeIntermediaryDeallocations() {
buf.resumeIntermediaryDeallocations();
return this;
}
@Override
public boolean isFreed() {
return buf.isFreed();
}
@Override
public void free() {
buf.free();
}
@Override
public int hashCode() {
return buf.hashCode();
@ -793,9 +821,4 @@ public final class SwappedByteBuf implements ByteBuf {
public String toString() {
return "Swapped(" + buf.toString() + ')';
}
@Override
public Unsafe unsafe() {
return buf.unsafe();
}
}

View File

@ -15,7 +15,6 @@
*/
package io.netty.buffer;
import io.netty.buffer.ByteBuf.Unsafe;
import sun.misc.Cleaner;
import java.io.IOException;
@ -36,7 +35,7 @@ import java.util.Queue;
* constructor explicitly.
*/
@SuppressWarnings("restriction")
final class UnpooledDirectByteBuf extends AbstractByteBuf implements Unsafe {
final class UnpooledDirectByteBuf extends AbstractByteBuf {
private static final Field CLEANER_FIELD;
@ -477,8 +476,7 @@ final class UnpooledDirectByteBuf extends AbstractByteBuf implements Unsafe {
return new UnpooledDirectByteBuf(alloc(), dst, maxCapacity());
}
@Override
public ByteBuffer internalNioBuffer() {
private ByteBuffer internalNioBuffer() {
ByteBuffer tmpNioBuf = this.tmpNioBuf;
if (tmpNioBuf == null) {
this.tmpNioBuf = tmpNioBuf = buffer.duplicate();
@ -486,24 +484,6 @@ final class UnpooledDirectByteBuf extends AbstractByteBuf implements Unsafe {
return tmpNioBuf;
}
@Override
public ByteBuffer[] internalNioBuffers() {
throw new UnsupportedOperationException();
}
@Override
public void discardSomeReadBytes() {
final int readerIndex = readerIndex();
if (readerIndex == writerIndex()) {
discardReadBytes();
return;
}
if (readerIndex > 0 && readerIndex >= capacity >>> 1) {
discardReadBytes();
}
}
@Override
public boolean isFreed() {
return freed;
@ -525,16 +505,17 @@ final class UnpooledDirectByteBuf extends AbstractByteBuf implements Unsafe {
}
@Override
public void suspendIntermediaryDeallocations() {
public ByteBuf suspendIntermediaryDeallocations() {
if (suspendedDeallocations == null) {
suspendedDeallocations = new ArrayDeque<ByteBuffer>(2);
}
return this;
}
@Override
public void resumeIntermediaryDeallocations() {
public ByteBuf resumeIntermediaryDeallocations() {
if (suspendedDeallocations == null) {
return;
return this;
}
Queue<ByteBuffer> suspendedDeallocations = this.suspendedDeallocations;
@ -543,15 +524,11 @@ final class UnpooledDirectByteBuf extends AbstractByteBuf implements Unsafe {
for (ByteBuffer buf: suspendedDeallocations) {
freeDirect(buf);
}
return this;
}
@Override
public ByteBuf unwrap() {
return null;
}
@Override
public Unsafe unsafe() {
return this;
}
}

View File

@ -15,8 +15,6 @@
*/
package io.netty.buffer;
import io.netty.buffer.ByteBuf.Unsafe;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -29,7 +27,7 @@ import java.nio.channels.ScatteringByteChannel;
/**
* Big endian Java heap buffer implementation.
*/
final class UnpooledHeapByteBuf extends AbstractByteBuf implements Unsafe {
final class UnpooledHeapByteBuf extends AbstractByteBuf {
private final ByteBufAllocator alloc;
private byte[] array;
@ -345,8 +343,7 @@ final class UnpooledHeapByteBuf extends AbstractByteBuf implements Unsafe {
return new UnpooledHeapByteBuf(alloc(), copiedArray, maxCapacity());
}
@Override
public ByteBuffer internalNioBuffer() {
private ByteBuffer internalNioBuffer() {
ByteBuffer tmpNioBuf = this.tmpNioBuf;
if (tmpNioBuf == null) {
this.tmpNioBuf = tmpNioBuf = ByteBuffer.wrap(array);
@ -354,24 +351,6 @@ final class UnpooledHeapByteBuf extends AbstractByteBuf implements Unsafe {
return tmpNioBuf;
}
@Override
public ByteBuffer[] internalNioBuffers() {
throw new UnsupportedOperationException();
}
@Override
public void discardSomeReadBytes() {
final int readerIndex = readerIndex();
if (readerIndex == writerIndex()) {
discardReadBytes();
return;
}
if (readerIndex > 0 && readerIndex >= capacity() >>> 1) {
discardReadBytes();
}
}
@Override
public boolean isFreed() {
return freed;
@ -383,18 +362,17 @@ final class UnpooledHeapByteBuf extends AbstractByteBuf implements Unsafe {
}
@Override
public void suspendIntermediaryDeallocations() { }
public ByteBuf suspendIntermediaryDeallocations() {
return this;
}
@Override
public void resumeIntermediaryDeallocations() { }
public ByteBuf resumeIntermediaryDeallocations() {
return this;
}
@Override
public ByteBuf unwrap() {
return null;
}
@Override
public Unsafe unsafe() {
return this;
}
}

View File

@ -70,7 +70,7 @@ public abstract class ByteToByteDecoder extends ChannelInboundByteHandlerAdapter
}
}
in.unsafe().discardSomeReadBytes();
in.discardSomeReadBytes();
if (out.readableBytes() > oldOutSize) {
ctx.fireInboundBufferUpdated();
}

View File

@ -43,7 +43,7 @@ public abstract class ByteToByteEncoder extends ChannelOutboundByteHandlerAdapte
}
}
in.unsafe().discardSomeReadBytes();
in.discardSomeReadBytes();
ctx.flush(future);
}

View File

@ -91,7 +91,7 @@ public abstract class ByteToMessageDecoder<O>
break;
}
} catch (Throwable t) {
in.unsafe().discardSomeReadBytes();
in.discardSomeReadBytes();
if (decoded) {
decoded = false;
@ -106,7 +106,7 @@ public abstract class ByteToMessageDecoder<O>
}
}
in.unsafe().discardSomeReadBytes();
in.discardSomeReadBytes();
if (decoded) {
ctx.fireInboundBufferUpdated();

View File

@ -75,7 +75,7 @@ public abstract class MessageToMessageCodec<INBOUND_IN, INBOUND_OUT, OUTBOUND_IN
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
buf.unsafe().free();
buf.free();
}
@Override
@ -91,7 +91,7 @@ public abstract class MessageToMessageCodec<INBOUND_IN, INBOUND_OUT, OUTBOUND_IN
@Override
public void freeOutboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
buf.unsafe().free();
buf.free();
}
@Override

View File

@ -458,7 +458,7 @@ public abstract class ReplayingDecoder<O, S> extends ByteToMessageDecoder<O> {
private void fireInboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) {
final int oldReaderIndex = in.readerIndex();
in.unsafe().discardSomeReadBytes();
in.discardSomeReadBytes();
final int newReaderIndex = in.readerIndex();
checkpoint -= oldReaderIndex - newReaderIndex;
ctx.fireInboundBufferUpdated();

View File

@ -16,7 +16,6 @@
package io.netty.handler.codec;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBuf.Unsafe;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufIndexFinder;
import io.netty.buffer.ChannelBufType;
@ -32,7 +31,7 @@ import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
final class ReplayingDecoderBuffer implements ByteBuf, Unsafe {
final class ReplayingDecoderBuffer implements ByteBuf {
private static final Signal REPLAY = ReplayingDecoder.REPLAY;
@ -844,23 +843,13 @@ final class ReplayingDecoderBuffer implements ByteBuf, Unsafe {
}
@Override
public ByteBuffer internalNioBuffer() {
throw new UnreplayableOperationException();
}
@Override
public ByteBuffer[] internalNioBuffers() {
throw new UnreplayableOperationException();
}
@Override
public void discardSomeReadBytes() {
public ByteBuf discardSomeReadBytes() {
throw new UnreplayableOperationException();
}
@Override
public boolean isFreed() {
return buffer.unsafe().isFreed();
return buffer.isFreed();
}
@Override
@ -869,12 +858,12 @@ final class ReplayingDecoderBuffer implements ByteBuf, Unsafe {
}
@Override
public void suspendIntermediaryDeallocations() {
public ByteBuf suspendIntermediaryDeallocations() {
throw new UnreplayableOperationException();
}
@Override
public void resumeIntermediaryDeallocations() {
public ByteBuf resumeIntermediaryDeallocations() {
throw new UnreplayableOperationException();
}
@ -882,9 +871,4 @@ final class ReplayingDecoderBuffer implements ByteBuf, Unsafe {
public ByteBuf unwrap() {
throw new UnreplayableOperationException();
}
@Override
public Unsafe unsafe() {
return this;
}
}

View File

@ -97,7 +97,7 @@ public class CompatibleObjectEncoder extends MessageToByteEncoder<Object> {
oos.reset();
// Also discard the byproduct to avoid OOM on the sending side.
out.unsafe().discardSomeReadBytes();
out.discardSomeReadBytes();
}
}

View File

@ -118,12 +118,12 @@ public class ByteLoggingHandler
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
buf.unsafe().free();
buf.free();
}
@Override
public void freeOutboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
buf.unsafe().free();
buf.free();
}
@Override

View File

@ -384,12 +384,12 @@ public class SslHandler
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
buf.unsafe().free();
buf.free();
}
@Override
public void freeOutboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
buf.unsafe().free();
buf.free();
}
@Override
@ -409,7 +409,7 @@ public class SslHandler
final ByteBuf in = ctx.outboundByteBuffer();
final ByteBuf out = ctx.nextOutboundByteBuffer();
out.unsafe().discardSomeReadBytes();
out.discardSomeReadBytes();
// Do not encrypt the first write request if this handler is
// created with startTLS flag turned on.
@ -481,7 +481,7 @@ public class SslHandler
setHandshakeFailure(e);
throw e;
} finally {
in.unsafe().discardSomeReadBytes();
in.discardSomeReadBytes();
flush0(ctx, bytesConsumed);
}
}

View File

@ -51,7 +51,7 @@ public class ByteBufAllocatorBenchmark extends DefaultBenchmark {
@Override
protected void tearDown() throws Exception {
for (ByteBuf b: queue) {
b.unsafe().free();
b.free();
}
queue.clear();
}
@ -63,7 +63,7 @@ public class ByteBufAllocatorBenchmark extends DefaultBenchmark {
for (int i = 0; i < reps; i ++) {
queue.add(alloc.buffer(size));
queue.removeFirst().unsafe().free();
queue.removeFirst().free();
}
}

View File

@ -43,7 +43,7 @@ public abstract class ChannelInboundByteHandlerAdapter
inboundBufferUpdated(ctx, in);
} finally {
if (!in.readable()) {
in.unsafe().discardSomeReadBytes();
in.discardReadBytes();
}
}
}

View File

@ -23,7 +23,7 @@ public abstract class ChannelInboundHandlerAdapter
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
buf.unsafe().free();
buf.free();
}
/**

View File

@ -21,7 +21,7 @@ public abstract class ChannelOutboundHandlerAdapter
extends ChannelOperationHandlerAdapter implements ChannelOutboundHandler {
@Override
public void freeOutboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
buf.unsafe().free();
buf.free();
}
@Override

View File

@ -1237,8 +1237,7 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
data = ctx.alloc().buffer(dataLen, dataLen);
}
byteBuf.readBytes(data, dataLen);
byteBuf.unsafe().discardSomeReadBytes();
byteBuf.readBytes(data, dataLen).discardSomeReadBytes();
exchangeBuf.add(data);
}
@ -1259,7 +1258,7 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
try {
out.writeBytes(data);
} finally {
data.unsafe().free();
data.free();
}
}
}

View File

@ -1472,7 +1472,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
@Override
public void freeOutboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) {
buf.unsafe().free();
buf.free();
}
@Override

View File

@ -254,7 +254,7 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
try {
if (buf.readable()) {
for (;;) {
if (buf.unsafe().isFreed()) {
if (buf.isFreed()) {
break;
}
// Ensure the readerIndex of the buffer is 0 before beginning an async write.
@ -280,7 +280,7 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
if (asyncWriteInProgress) {
// JDK decided to write data (or notify handler) later.
buf.unsafe().suspendIntermediaryDeallocations();
buf.suspendIntermediaryDeallocations();
break;
}
@ -316,7 +316,7 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
try {
for (;;) {
ByteBuf byteBuf = pipeline().inboundByteBuffer();
if (byteBuf.unsafe().isFreed()) {
if (byteBuf.isFreed()) {
break;
}
@ -363,7 +363,7 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
channel.asyncWriteInProgress = false;
ByteBuf buf = channel.unsafe().directOutboundContext().outboundByteBuffer();
buf.unsafe().resumeIntermediaryDeallocations();
buf.resumeIntermediaryDeallocations();
int writtenBytes = result.intValue();
if (writtenBytes > 0) {

View File

@ -412,7 +412,7 @@ public class LocalTransportThreadModelTest {
@Override
public void freeOutboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) {
buf.unsafe().free();
buf.free();
}
@Override
@ -464,7 +464,7 @@ public class LocalTransportThreadModelTest {
out.add(msg);
}
}
in.unsafe().discardSomeReadBytes();
in.discardSomeReadBytes();
if (swallow) {
future.setSuccess();
} else {
@ -507,7 +507,7 @@ public class LocalTransportThreadModelTest {
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
buf.unsafe().free();
buf.free();
}
@Override