From d5e897bba2635aff601cb526884164db80a9dd61 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Fri, 14 Feb 2014 13:03:52 -0800 Subject: [PATCH] Determine the default allocator from system property - Add ByteBufAllocator.DEFAULT - The default allocator is now 'pooled' --- .../io/netty/buffer/ByteBufAllocator.java | 2 ++ .../java/io/netty/buffer/ByteBufUtil.java | 23 +++++++++++++++++++ .../netty/channel/DefaultChannelConfig.java | 4 +--- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufAllocator.java b/buffer/src/main/java/io/netty/buffer/ByteBufAllocator.java index 9ced16c185..e1778020b2 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBufAllocator.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBufAllocator.java @@ -21,6 +21,8 @@ package io.netty.buffer; */ public interface ByteBufAllocator { + ByteBufAllocator DEFAULT = ByteBufUtil.DEFAULT_ALLOCATOR; + /** * Allocate a {@link ByteBuf}. If it is a direct or heap buffer * depends on the actual implementation. diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java index f501ed13ca..23f3bd5182 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java @@ -16,6 +16,9 @@ package io.netty.buffer; import io.netty.util.CharsetUtil; +import io.netty.util.internal.SystemPropertyUtil; +import io.netty.util.internal.logging.InternalLogger; +import io.netty.util.internal.logging.InternalLoggerFactory; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -25,20 +28,40 @@ import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetEncoder; import java.nio.charset.CoderResult; +import java.util.Locale; /** * A collection of utility methods that is related with handling {@link ByteBuf}. */ public final class ByteBufUtil { + private static final InternalLogger logger = InternalLoggerFactory.getInstance(ByteBufUtil.class); + private static final char[] HEXDUMP_TABLE = new char[256 * 4]; + static final ByteBufAllocator DEFAULT_ALLOCATOR; + static { final char[] DIGITS = "0123456789abcdef".toCharArray(); for (int i = 0; i < 256; i ++) { HEXDUMP_TABLE[ i << 1 ] = DIGITS[i >>> 4 & 0x0F]; HEXDUMP_TABLE[(i << 1) + 1] = DIGITS[i & 0x0F]; } + + String allocType = SystemPropertyUtil.get("io.netty.allocator.type", "pooled").toLowerCase(Locale.US).trim(); + ByteBufAllocator alloc; + if ("unpooled".equals(allocType)) { + alloc = UnpooledByteBufAllocator.DEFAULT; + logger.debug("-Dio.netty.allocator.type: {}", allocType); + } else if ("pooled".equals(allocType)) { + alloc = PooledByteBufAllocator.DEFAULT; + logger.debug("-Dio.netty.allocator.type: {}", allocType); + } else { + alloc = PooledByteBufAllocator.DEFAULT; + logger.debug("-Dio.netty.allocator.type: pooled (unknown: {})", allocType); + } + + DEFAULT_ALLOCATOR = alloc; } /** diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelConfig.java b/transport/src/main/java/io/netty/channel/DefaultChannelConfig.java index 7404a8f8d9..a69620dd1f 100644 --- a/transport/src/main/java/io/netty/channel/DefaultChannelConfig.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelConfig.java @@ -16,7 +16,6 @@ package io.netty.channel; import io.netty.buffer.ByteBufAllocator; -import io.netty.buffer.UnpooledByteBufAllocator; import io.netty.channel.nio.AbstractNioByteChannel; import io.netty.channel.socket.SocketChannelConfig; @@ -31,7 +30,6 @@ import static io.netty.channel.ChannelOption.*; */ public class DefaultChannelConfig implements ChannelConfig { - private static final ByteBufAllocator DEFAULT_ALLOCATOR = UnpooledByteBufAllocator.DEFAULT; private static final RecvByteBufAllocator DEFAULT_RCVBUF_ALLOCATOR = AdaptiveRecvByteBufAllocator.DEFAULT; private static final MessageSizeEstimator DEFAULT_MSG_SIZE_ESTIMATOR = DefaultMessageSizeEstimator.DEFAULT; @@ -39,7 +37,7 @@ public class DefaultChannelConfig implements ChannelConfig { protected final Channel channel; - private volatile ByteBufAllocator allocator = DEFAULT_ALLOCATOR; + private volatile ByteBufAllocator allocator = ByteBufAllocator.DEFAULT; private volatile RecvByteBufAllocator rcvBufAllocator = DEFAULT_RCVBUF_ALLOCATOR; private volatile MessageSizeEstimator msgSizeEstimator = DEFAULT_MSG_SIZE_ESTIMATOR;