diff --git a/common/src/main/java/common/net/buffer/AbstractByteBuf.java b/common/src/main/java/common/net/buffer/AbstractByteBuf.java index 5f4f200..5c0676f 100644 --- a/common/src/main/java/common/net/buffer/AbstractByteBuf.java +++ b/common/src/main/java/common/net/buffer/AbstractByteBuf.java @@ -26,8 +26,8 @@ import java.nio.charset.Charset; import common.net.util.IllegalReferenceCountException; import common.net.util.ResourceLeakDetector; -import common.net.util.internal.PlatformDependent; import common.net.util.internal.StringUtil; +import common.util.Util; /** @@ -1030,7 +1030,7 @@ public abstract class AbstractByteBuf extends ByteBuf { } } while (i < endIndex); } catch (Exception e) { - PlatformDependent.throwException(e); + Util.throwUnchecked(e); } return -1; @@ -1071,7 +1071,7 @@ public abstract class AbstractByteBuf extends ByteBuf { } } while (i >= index); } catch (Exception e) { - PlatformDependent.throwException(e); + Util.throwUnchecked(e); } return -1; diff --git a/common/src/main/java/common/net/buffer/AbstractReferenceCountedByteBuf.java b/common/src/main/java/common/net/buffer/AbstractReferenceCountedByteBuf.java index 53ad03a..d15a7e2 100644 --- a/common/src/main/java/common/net/buffer/AbstractReferenceCountedByteBuf.java +++ b/common/src/main/java/common/net/buffer/AbstractReferenceCountedByteBuf.java @@ -19,7 +19,6 @@ package common.net.buffer; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; import common.net.util.IllegalReferenceCountException; -import common.net.util.internal.PlatformDependent; /** * Abstract base class for {@link ByteBuf} implementations that count references. @@ -30,7 +29,7 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf { static { AtomicIntegerFieldUpdater updater = - PlatformDependent.newAtomicIntegerFieldUpdater(AbstractReferenceCountedByteBuf.class, "refCnt"); + null; if (updater == null) { updater = AtomicIntegerFieldUpdater.newUpdater(AbstractReferenceCountedByteBuf.class, "refCnt"); } diff --git a/common/src/main/java/common/net/buffer/PooledByteBufAllocator.java b/common/src/main/java/common/net/buffer/PooledByteBufAllocator.java index c3284af..a0b0ca4 100644 --- a/common/src/main/java/common/net/buffer/PooledByteBufAllocator.java +++ b/common/src/main/java/common/net/buffer/PooledByteBufAllocator.java @@ -20,7 +20,6 @@ import java.nio.ByteBuffer; import java.util.concurrent.atomic.AtomicInteger; import common.net.util.concurrent.FastThreadLocal; -import common.net.util.internal.PlatformDependent; import common.net.util.internal.SystemPropertyUtil; import common.net.util.internal.logging.InternalLogger; import common.net.util.internal.logging.InternalLoggerFactory; @@ -78,7 +77,7 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator { "game.net.allocator.numDirectArenas", (int) Math.min( runtime.availableProcessors(), - PlatformDependent.maxDirectMemory() / defaultChunkSize / 2 / 3))); + Runtime.getRuntime().maxMemory() / defaultChunkSize / 2 / 3))); // cache sizes DEFAULT_TINY_CACHE_SIZE = SystemPropertyUtil.getInt("game.net.allocator.tinyCacheSize", 512); diff --git a/common/src/main/java/common/net/channel/AbstractChannel.java b/common/src/main/java/common/net/channel/AbstractChannel.java index 2cfa428..2330ef8 100644 --- a/common/src/main/java/common/net/channel/AbstractChannel.java +++ b/common/src/main/java/common/net/channel/AbstractChannel.java @@ -16,7 +16,6 @@ package common.net.channel; import java.io.IOException; -import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.channels.ClosedChannelException; import java.nio.channels.NotYetConnectedException; @@ -27,11 +26,9 @@ import common.net.util.DefaultAttributeMap; import common.net.util.ReferenceCountUtil; import common.net.util.internal.EmptyArrays; import common.net.util.internal.OneTimeTask; -import common.net.util.internal.PlatformDependent; import common.net.util.internal.ThreadLocalRandom; import common.net.util.internal.logging.InternalLogger; import common.net.util.internal.logging.InternalLoggerFactory; -import common.util.Util; /** * A skeletal {@link Channel} implementation. @@ -458,19 +455,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha return; } - // See: https://github.com/netty/netty/issues/576 - if (!Util.WINDOWS && !PlatformDependent.isRoot() && - Boolean.TRUE.equals(config().getOption(ChannelOption.SO_BROADCAST)) && - localAddress instanceof InetSocketAddress && - !((InetSocketAddress) localAddress).getAddress().isAnyLocalAddress()) { - // Warn a user about the fact that a non-root user can't receive a - // broadcast packet on *nix if the socket is bound on non-wildcard address. - logger.warn( - "A non-root user can't receive a broadcast packet if the socket " + - "is not bound to a wildcard address; binding to a non-wildcard " + - "address (" + localAddress + ") anyway as requested."); - } - boolean wasActive = isActive(); try { doBind(localAddress); diff --git a/common/src/main/java/common/net/channel/ChannelOption.java b/common/src/main/java/common/net/channel/ChannelOption.java index 3c72bf0..629d2fe 100644 --- a/common/src/main/java/common/net/channel/ChannelOption.java +++ b/common/src/main/java/common/net/channel/ChannelOption.java @@ -17,11 +17,11 @@ package common.net.channel; import java.net.InetAddress; import java.net.NetworkInterface; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import common.net.buffer.ByteBufAllocator; import common.net.util.UniqueName; -import common.net.util.internal.PlatformDependent; /** * A {@link ChannelOption} allows to configure a {@link ChannelConfig} in a type-safe @@ -34,7 +34,7 @@ import common.net.util.internal.PlatformDependent; public class ChannelOption extends UniqueName { - private static final ConcurrentMap names = PlatformDependent.newConcurrentHashMap(); + private static final ConcurrentMap names = new ConcurrentHashMap(); public static final ChannelOption ALLOCATOR = valueOf("ALLOCATOR"); public static final ChannelOption RCVBUF_ALLOCATOR = valueOf("RCVBUF_ALLOCATOR"); diff --git a/common/src/main/java/common/net/channel/ChannelOutboundBuffer.java b/common/src/main/java/common/net/channel/ChannelOutboundBuffer.java index 4e7e920..e362114 100644 --- a/common/src/main/java/common/net/channel/ChannelOutboundBuffer.java +++ b/common/src/main/java/common/net/channel/ChannelOutboundBuffer.java @@ -27,7 +27,6 @@ import common.net.util.ReferenceCountUtil; import common.net.util.Recycler.Handle; import common.net.util.concurrent.FastThreadLocal; import common.net.util.internal.InternalThreadLocalMap; -import common.net.util.internal.PlatformDependent; import common.net.util.internal.logging.InternalLogger; import common.net.util.internal.logging.InternalLoggerFactory; @@ -78,14 +77,14 @@ public final class ChannelOutboundBuffer { static { AtomicIntegerFieldUpdater writableUpdater = - PlatformDependent.newAtomicIntegerFieldUpdater(ChannelOutboundBuffer.class, "writable"); + null; if (writableUpdater == null) { writableUpdater = AtomicIntegerFieldUpdater.newUpdater(ChannelOutboundBuffer.class, "writable"); } WRITABLE_UPDATER = writableUpdater; AtomicLongFieldUpdater pendingSizeUpdater = - PlatformDependent.newAtomicLongFieldUpdater(ChannelOutboundBuffer.class, "totalPendingSize"); + null; if (pendingSizeUpdater == null) { pendingSizeUpdater = AtomicLongFieldUpdater.newUpdater(ChannelOutboundBuffer.class, "totalPendingSize"); } diff --git a/common/src/main/java/common/net/channel/DefaultChannelPipeline.java b/common/src/main/java/common/net/channel/DefaultChannelPipeline.java index a717a23..86d549c 100644 --- a/common/src/main/java/common/net/channel/DefaultChannelPipeline.java +++ b/common/src/main/java/common/net/channel/DefaultChannelPipeline.java @@ -32,10 +32,10 @@ import common.net.channel.Channel.Unsafe; import common.net.util.ReferenceCountUtil; import common.net.util.concurrent.EventExecutor; import common.net.util.concurrent.EventExecutorGroup; -import common.net.util.internal.PlatformDependent; import common.net.util.internal.StringUtil; import common.net.util.internal.logging.InternalLogger; import common.net.util.internal.logging.InternalLoggerFactory; +import common.util.Util; /** * The default {@link ChannelPipeline} implementation. It is usually created @@ -551,7 +551,7 @@ final class DefaultChannelPipeline implements ChannelPipeline { future.get(); } catch (ExecutionException ex) { // In the arbitrary case, we can throw Error, RuntimeException, and Exception - PlatformDependent.throwException(ex.getCause()); + Util.throwUnchecked(ex.getCause()); } catch (InterruptedException ex) { // Interrupt the calling thread (note that this method is not called from the event loop) Thread.currentThread().interrupt(); diff --git a/common/src/main/java/common/net/channel/FailedChannelFuture.java b/common/src/main/java/common/net/channel/FailedChannelFuture.java index b36b461..f357ae5 100644 --- a/common/src/main/java/common/net/channel/FailedChannelFuture.java +++ b/common/src/main/java/common/net/channel/FailedChannelFuture.java @@ -16,7 +16,7 @@ package common.net.channel; import common.net.util.concurrent.EventExecutor; -import common.net.util.internal.PlatformDependent; +import common.util.Util; /** * The {@link CompleteChannelFuture} which is failed already. It is @@ -53,13 +53,13 @@ final class FailedChannelFuture extends CompleteChannelFuture { @Override public ChannelFuture sync() { - PlatformDependent.throwException(cause); + Util.throwUnchecked(cause); return this; } @Override public ChannelFuture syncUninterruptibly() { - PlatformDependent.throwException(cause); + Util.throwUnchecked(cause); return this; } } diff --git a/common/src/main/java/common/net/channel/local/LocalChannelRegistry.java b/common/src/main/java/common/net/channel/local/LocalChannelRegistry.java index 6af42eb..4fc5b82 100644 --- a/common/src/main/java/common/net/channel/local/LocalChannelRegistry.java +++ b/common/src/main/java/common/net/channel/local/LocalChannelRegistry.java @@ -16,16 +16,16 @@ package common.net.channel.local; import java.net.SocketAddress; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import common.net.channel.Channel; import common.net.channel.ChannelException; -import common.net.util.internal.PlatformDependent; import common.net.util.internal.StringUtil; final class LocalChannelRegistry { - private static final ConcurrentMap boundChannels = PlatformDependent.newConcurrentHashMap(); + private static final ConcurrentMap boundChannels = new ConcurrentHashMap(); static LocalAddress register( Channel channel, LocalAddress oldLocalAddress, SocketAddress localAddress) { diff --git a/common/src/main/java/common/net/channel/nio/NioEventLoop.java b/common/src/main/java/common/net/channel/nio/NioEventLoop.java index 9db7262..dcb46d0 100644 --- a/common/src/main/java/common/net/channel/nio/NioEventLoop.java +++ b/common/src/main/java/common/net/channel/nio/NioEventLoop.java @@ -37,7 +37,7 @@ import common.net.channel.ChannelException; import common.net.channel.EventLoopException; import common.net.channel.SingleThreadEventLoop; import common.net.channel.nio.AbstractNioChannel.NioUnsafe; -import common.net.util.internal.PlatformDependent; +import common.net.util.internal.MpscLinkedQueue; import common.net.util.internal.SystemPropertyUtil; import common.net.util.internal.logging.InternalLogger; import common.net.util.internal.logging.InternalLoggerFactory; @@ -164,7 +164,7 @@ public final class NioEventLoop extends SingleThreadEventLoop { @Override protected Queue newTaskQueue() { // This event loop never calls takeTask() - return PlatformDependent.newMpscQueue(); + return new MpscLinkedQueue(); } /** diff --git a/common/src/main/java/common/net/util/AttributeKey.java b/common/src/main/java/common/net/util/AttributeKey.java index b89a6cd..30f87f0 100644 --- a/common/src/main/java/common/net/util/AttributeKey.java +++ b/common/src/main/java/common/net/util/AttributeKey.java @@ -15,10 +15,9 @@ */ package common.net.util; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -import common.net.util.internal.PlatformDependent; - /** * Key which can be used to access {@link Attribute} out of the {@link AttributeMap}. Be aware that it is not be * possible to have multiple keys with the same name. @@ -29,7 +28,7 @@ import common.net.util.internal.PlatformDependent; // 'T' is used only at compile time public final class AttributeKey extends UniqueName { - private static final ConcurrentMap names = PlatformDependent.newConcurrentHashMap(); + private static final ConcurrentMap names = new ConcurrentHashMap(); /** * Creates a new {@link AttributeKey} with the specified {@code name}. diff --git a/common/src/main/java/common/net/util/DefaultAttributeMap.java b/common/src/main/java/common/net/util/DefaultAttributeMap.java index 605849a..83c28ab 100644 --- a/common/src/main/java/common/net/util/DefaultAttributeMap.java +++ b/common/src/main/java/common/net/util/DefaultAttributeMap.java @@ -19,8 +19,6 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReferenceArray; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; -import common.net.util.internal.PlatformDependent; - /** * Default {@link AttributeMap} implementation which use simple synchronization per bucket to keep the memory overhead * as low as possible. @@ -33,7 +31,7 @@ public class DefaultAttributeMap implements AttributeMap { static { AtomicReferenceFieldUpdater referenceFieldUpdater = - PlatformDependent.newAtomicReferenceFieldUpdater(DefaultAttributeMap.class, "attributes"); + null; if (referenceFieldUpdater == null) { referenceFieldUpdater = AtomicReferenceFieldUpdater .newUpdater(DefaultAttributeMap.class, AtomicReferenceArray.class, "attributes"); diff --git a/common/src/main/java/common/net/util/NetUtil.java b/common/src/main/java/common/net/util/NetUtil.java index 0e9bc4f..99e420a 100644 --- a/common/src/main/java/common/net/util/NetUtil.java +++ b/common/src/main/java/common/net/util/NetUtil.java @@ -28,7 +28,6 @@ import java.util.Enumeration; import java.util.List; import java.util.StringTokenizer; -import common.net.util.internal.PlatformDependent; import common.net.util.internal.logging.InternalLogger; import common.net.util.internal.logging.InternalLoggerFactory; import common.util.Util; @@ -84,7 +83,7 @@ public final class NetUtil { localhost4 = (Inet4Address) InetAddress.getByAddress(LOCALHOST4_BYTES); } catch (Exception e) { // We should not get here as long as the length of the address is correct. - PlatformDependent.throwException(e); + Util.throwUnchecked(e); } LOCALHOST4 = localhost4; @@ -94,7 +93,7 @@ public final class NetUtil { localhost6 = (Inet6Address) InetAddress.getByAddress(LOCALHOST6_BYTES); } catch (Exception e) { // We should not get here as long as the length of the address is correct. - PlatformDependent.throwException(e); + Util.throwUnchecked(e); } LOCALHOST6 = localhost6; diff --git a/common/src/main/java/common/net/util/ResourceLeakDetector.java b/common/src/main/java/common/net/util/ResourceLeakDetector.java index fcc44d6..0159efd 100644 --- a/common/src/main/java/common/net/util/ResourceLeakDetector.java +++ b/common/src/main/java/common/net/util/ResourceLeakDetector.java @@ -24,10 +24,10 @@ import java.lang.ref.ReferenceQueue; import java.util.ArrayDeque; import java.util.Deque; import java.util.EnumSet; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicBoolean; -import common.net.util.internal.PlatformDependent; import common.net.util.internal.SystemPropertyUtil; import common.net.util.internal.logging.InternalLogger; import common.net.util.internal.logging.InternalLoggerFactory; @@ -124,7 +124,7 @@ public final class ResourceLeakDetector { private final DefaultResourceLeak tail = new DefaultResourceLeak(null); private final ReferenceQueue refQueue = new ReferenceQueue(); - private final ConcurrentMap reportedLeaks = PlatformDependent.newConcurrentHashMap(); + private final ConcurrentMap reportedLeaks = new ConcurrentHashMap(); private final String resourceType; private final int samplingInterval; diff --git a/common/src/main/java/common/net/util/Signal.java b/common/src/main/java/common/net/util/Signal.java index ad25833..b0f041d 100644 --- a/common/src/main/java/common/net/util/Signal.java +++ b/common/src/main/java/common/net/util/Signal.java @@ -16,10 +16,9 @@ package common.net.util; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -import common.net.util.internal.PlatformDependent; - /** * A special {@link Error} which is used to signal some state or request by throwing it. * {@link Signal} has an empty stack trace and has no cause to save the instantiation overhead. @@ -28,7 +27,7 @@ public final class Signal extends Error { private static final long serialVersionUID = -221145131122459977L; - private static final ConcurrentMap map = PlatformDependent.newConcurrentHashMap(); + private static final ConcurrentMap map = new ConcurrentHashMap(); private final UniqueName uname; diff --git a/common/src/main/java/common/net/util/ThreadDeathWatcher.java b/common/src/main/java/common/net/util/ThreadDeathWatcher.java index af972be..1020596 100644 --- a/common/src/main/java/common/net/util/ThreadDeathWatcher.java +++ b/common/src/main/java/common/net/util/ThreadDeathWatcher.java @@ -24,8 +24,8 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import common.net.util.concurrent.DefaultThreadFactory; +import common.net.util.internal.MpscLinkedQueue; import common.net.util.internal.MpscLinkedQueueNode; -import common.net.util.internal.PlatformDependent; import common.net.util.internal.logging.InternalLogger; import common.net.util.internal.logging.InternalLoggerFactory; @@ -43,7 +43,7 @@ public final class ThreadDeathWatcher { private static final ThreadFactory threadFactory = new DefaultThreadFactory(ThreadDeathWatcher.class, true, Thread.MIN_PRIORITY); - private static final Queue pendingEntries = PlatformDependent.newMpscQueue(); + private static final Queue pendingEntries = new MpscLinkedQueue(); private static final Watcher watcher = new Watcher(); private static final AtomicBoolean started = new AtomicBoolean(); private static volatile Thread watcherThread; diff --git a/common/src/main/java/common/net/util/concurrent/DefaultPromise.java b/common/src/main/java/common/net/util/concurrent/DefaultPromise.java index eb38442..f7cad80 100644 --- a/common/src/main/java/common/net/util/concurrent/DefaultPromise.java +++ b/common/src/main/java/common/net/util/concurrent/DefaultPromise.java @@ -24,10 +24,10 @@ import java.util.concurrent.TimeUnit; import common.net.util.Signal; import common.net.util.internal.EmptyArrays; import common.net.util.internal.InternalThreadLocalMap; -import common.net.util.internal.PlatformDependent; import common.net.util.internal.StringUtil; import common.net.util.internal.logging.InternalLogger; import common.net.util.internal.logging.InternalLoggerFactory; +import common.util.Util; public class DefaultPromise extends AbstractFuture implements Promise { @@ -233,7 +233,7 @@ public class DefaultPromise extends AbstractFuture implements Promise { return; } - PlatformDependent.throwException(cause); + Util.throwUnchecked(cause); } @Override diff --git a/common/src/main/java/common/net/util/concurrent/FailedFuture.java b/common/src/main/java/common/net/util/concurrent/FailedFuture.java index 72780ba..f0c6ce7 100644 --- a/common/src/main/java/common/net/util/concurrent/FailedFuture.java +++ b/common/src/main/java/common/net/util/concurrent/FailedFuture.java @@ -15,7 +15,7 @@ */ package common.net.util.concurrent; -import common.net.util.internal.PlatformDependent; +import common.util.Util; /** * The {@link CompleteFuture} which is failed already. It is @@ -52,13 +52,13 @@ public final class FailedFuture extends CompleteFuture { @Override public Future sync() { - PlatformDependent.throwException(cause); + Util.throwUnchecked(cause); return this; } @Override public Future syncUninterruptibly() { - PlatformDependent.throwException(cause); + Util.throwUnchecked(cause); return this; } diff --git a/common/src/main/java/common/net/util/concurrent/FastThreadLocal.java b/common/src/main/java/common/net/util/concurrent/FastThreadLocal.java index 05ea6b5..a435abf 100644 --- a/common/src/main/java/common/net/util/concurrent/FastThreadLocal.java +++ b/common/src/main/java/common/net/util/concurrent/FastThreadLocal.java @@ -20,7 +20,7 @@ import java.util.IdentityHashMap; import java.util.Set; import common.net.util.internal.InternalThreadLocalMap; -import common.net.util.internal.PlatformDependent; +import common.util.Util; /** * A special variant of {@link ThreadLocal} that yields higher access performan when accessed from a @@ -154,7 +154,7 @@ public class FastThreadLocal { try { v = initialValue(); } catch (Exception e) { - PlatformDependent.throwException(e); + Util.throwUnchecked(e); } threadLocalMap.setIndexedVariable(index, v); @@ -225,7 +225,7 @@ public class FastThreadLocal { try { onRemoval((V) v); } catch (Exception e) { - PlatformDependent.throwException(e); + Util.throwUnchecked(e); } } } diff --git a/common/src/main/java/common/net/util/concurrent/SingleThreadEventExecutor.java b/common/src/main/java/common/net/util/concurrent/SingleThreadEventExecutor.java index 4a635d1..044f794 100644 --- a/common/src/main/java/common/net/util/concurrent/SingleThreadEventExecutor.java +++ b/common/src/main/java/common/net/util/concurrent/SingleThreadEventExecutor.java @@ -32,7 +32,6 @@ import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; -import common.net.util.internal.PlatformDependent; import common.net.util.internal.logging.InternalLogger; import common.net.util.internal.logging.InternalLoggerFactory; @@ -62,7 +61,7 @@ public abstract class SingleThreadEventExecutor extends AbstractEventExecutor { static { AtomicIntegerFieldUpdater updater = - PlatformDependent.newAtomicIntegerFieldUpdater(SingleThreadEventExecutor.class, "state"); + null; if (updater == null) { updater = AtomicIntegerFieldUpdater.newUpdater(SingleThreadEventExecutor.class, "state"); } diff --git a/common/src/main/java/common/net/util/internal/MpscLinkedQueue.java b/common/src/main/java/common/net/util/internal/MpscLinkedQueue.java index 1b32c49..0e6f378 100644 --- a/common/src/main/java/common/net/util/internal/MpscLinkedQueue.java +++ b/common/src/main/java/common/net/util/internal/MpscLinkedQueue.java @@ -65,7 +65,7 @@ import java.util.Queue; * data structure modified to avoid false sharing between head and tail Ref as per implementation of MpscLinkedQueue * on JCTools project. */ -final class MpscLinkedQueue extends MpscLinkedQueueTailRef implements Queue { +public final class MpscLinkedQueue extends MpscLinkedQueueTailRef implements Queue { private static final long serialVersionUID = -1878402552271506449L; @@ -87,7 +87,7 @@ final class MpscLinkedQueue extends MpscLinkedQueueTailRef implements Queu // // Also note that this class extends AtomicReference for the "tail" slot (which is the one that is appended to) // since Unsafe does not expose XCHG operation intrinsically. - MpscLinkedQueue() { + public MpscLinkedQueue() { MpscLinkedQueueNode tombstone = new DefaultNode(null); setHeadRef(tombstone); setTailRef(tombstone); diff --git a/common/src/main/java/common/net/util/internal/MpscLinkedQueueHeadRef.java b/common/src/main/java/common/net/util/internal/MpscLinkedQueueHeadRef.java index 9b4d089..fc70e65 100644 --- a/common/src/main/java/common/net/util/internal/MpscLinkedQueueHeadRef.java +++ b/common/src/main/java/common/net/util/internal/MpscLinkedQueueHeadRef.java @@ -30,7 +30,7 @@ abstract class MpscLinkedQueueHeadRef extends MpscLinkedQueuePad0 implemen static { AtomicReferenceFieldUpdater updater; - updater = PlatformDependent.newAtomicReferenceFieldUpdater(MpscLinkedQueueHeadRef.class, "headRef"); + updater = null; if (updater == null) { updater = AtomicReferenceFieldUpdater.newUpdater( MpscLinkedQueueHeadRef.class, MpscLinkedQueueNode.class, "headRef"); diff --git a/common/src/main/java/common/net/util/internal/MpscLinkedQueueNode.java b/common/src/main/java/common/net/util/internal/MpscLinkedQueueNode.java index 1c3f007..b44975f 100644 --- a/common/src/main/java/common/net/util/internal/MpscLinkedQueueNode.java +++ b/common/src/main/java/common/net/util/internal/MpscLinkedQueueNode.java @@ -27,7 +27,7 @@ public abstract class MpscLinkedQueueNode { AtomicReferenceFieldUpdater u; - u = PlatformDependent.newAtomicReferenceFieldUpdater(MpscLinkedQueueNode.class, "next"); + u = null; if (u == null) { u = AtomicReferenceFieldUpdater.newUpdater(MpscLinkedQueueNode.class, MpscLinkedQueueNode.class, "next"); } diff --git a/common/src/main/java/common/net/util/internal/MpscLinkedQueueTailRef.java b/common/src/main/java/common/net/util/internal/MpscLinkedQueueTailRef.java index fa2a36c..55f9537 100644 --- a/common/src/main/java/common/net/util/internal/MpscLinkedQueueTailRef.java +++ b/common/src/main/java/common/net/util/internal/MpscLinkedQueueTailRef.java @@ -28,7 +28,7 @@ abstract class MpscLinkedQueueTailRef extends MpscLinkedQueuePad1 { static { AtomicReferenceFieldUpdater updater; - updater = PlatformDependent.newAtomicReferenceFieldUpdater(MpscLinkedQueueTailRef.class, "tailRef"); + updater = null; if (updater == null) { updater = AtomicReferenceFieldUpdater.newUpdater( MpscLinkedQueueTailRef.class, MpscLinkedQueueNode.class, "tailRef"); diff --git a/common/src/main/java/common/net/util/internal/PlatformDependent.java b/common/src/main/java/common/net/util/internal/PlatformDependent.java deleted file mode 100644 index 69b47a9..0000000 --- a/common/src/main/java/common/net/util/internal/PlatformDependent.java +++ /dev/null @@ -1,348 +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 common.net.util.internal; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.lang.reflect.Method; -import java.net.InetSocketAddress; -import java.net.ServerSocket; -import java.nio.ByteBuffer; -import java.util.List; -import java.util.Map; -import java.util.Queue; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; -import java.util.concurrent.atomic.AtomicLongFieldUpdater; -import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import common.net.util.CharsetUtil; -import common.net.util.internal.logging.InternalLogger; -import common.net.util.internal.logging.InternalLoggerFactory; -import common.util.Util; - - -/** - * Utility that detects various properties specific to the current runtime - * environment, such as Java version and the availability of the - * {@code sun.misc.Unsafe} object. - *

- * You can disable the use of {@code sun.misc.Unsafe} if you specify - * the system property game.net.noUnsafe. - */ -public final class PlatformDependent { - - private static final InternalLogger logger = InternalLoggerFactory.getInstance(PlatformDependent.class); - - private static final Pattern MAX_DIRECT_MEMORY_SIZE_ARG_PATTERN = Pattern.compile( - "\\s*-XX:MaxDirectMemorySize\\s*=\\s*([0-9]+)\\s*([kKmMgG]?)\\s*$"); - - private static final boolean IS_ROOT = isRoot0(); - - private static final long MAX_DIRECT_MEMORY = maxDirectMemory0(); - - /** - * Return {@code true} if the current user is root. Note that this method returns - * {@code false} if on Windows. - */ - public static boolean isRoot() { - return IS_ROOT; - } - - /** - * Returns the maximum memory reserved for direct buffer allocation. - */ - public static long maxDirectMemory() { - return MAX_DIRECT_MEMORY; - } - - - /** - * Raises an exception bypassing compiler checks for checked exceptions. - */ - public static void throwException(Throwable t) { - PlatformDependent.throwException0(t); - } - - - private static void throwException0(Throwable t) throws E { - throw (E) t; - } - - /** - * Creates a new fastest {@link ConcurrentMap} implementaion for the current platform. - */ - public static ConcurrentMap newConcurrentHashMap() { -// if (CAN_USE_CHM_V8) { -// return new ConcurrentHashMapV8(); -// } else { - return new ConcurrentHashMap(); -// } - } - - /** - * Creates a new fastest {@link ConcurrentMap} implementaion for the current platform. - */ - public static ConcurrentMap newConcurrentHashMap(int initialCapacity) { -// if (CAN_USE_CHM_V8) { -// return new ConcurrentHashMapV8(initialCapacity); -// } else { - return new ConcurrentHashMap(initialCapacity); -// } - } - - /** - * Creates a new fastest {@link ConcurrentMap} implementaion for the current platform. - */ - public static ConcurrentMap newConcurrentHashMap(int initialCapacity, float loadFactor) { -// if (CAN_USE_CHM_V8) { -// return new ConcurrentHashMapV8(initialCapacity, loadFactor); -// } else { - return new ConcurrentHashMap(initialCapacity, loadFactor); -// } - } - - /** - * Creates a new fastest {@link ConcurrentMap} implementaion for the current platform. - */ - public static ConcurrentMap newConcurrentHashMap( - int initialCapacity, float loadFactor, int concurrencyLevel) { -// if (CAN_USE_CHM_V8) { -// return new ConcurrentHashMapV8(initialCapacity, loadFactor, concurrencyLevel); -// } else { - return new ConcurrentHashMap(initialCapacity, loadFactor, concurrencyLevel); -// } - } - - /** - * Creates a new fastest {@link ConcurrentMap} implementaion for the current platform. - */ - public static ConcurrentMap newConcurrentHashMap(Map map) { -// if (CAN_USE_CHM_V8) { -// return new ConcurrentHashMapV8(map); -// } else { - return new ConcurrentHashMap(map); -// } - } - - /** - * Try to deallocate the specified direct {@link ByteBuffer}. Please note this method does nothing if - * the current platform does not support this operation or the specified buffer is not a direct buffer. - */ - public static void freeDirectBuffer(ByteBuffer buffer) { - } - - /** - * Create a new optimized {@link AtomicReferenceFieldUpdater} or {@code null} if it - * could not be created. Because of this the caller need to check for {@code null} and if {@code null} is returned - * use {@link AtomicReferenceFieldUpdater#newUpdater(Class, Class, String)} as fallback. - */ - public static AtomicReferenceFieldUpdater newAtomicReferenceFieldUpdater( - Class tclass, String fieldName) { - return null; - } - - /** - * Create a new optimized {@link AtomicIntegerFieldUpdater} or {@code null} if it - * could not be created. Because of this the caller need to check for {@code null} and if {@code null} is returned - * use {@link AtomicIntegerFieldUpdater#newUpdater(Class, String)} as fallback. - */ - public static AtomicIntegerFieldUpdater newAtomicIntegerFieldUpdater( - Class tclass, String fieldName) { - return null; - } - - /** - * Create a new optimized {@link AtomicLongFieldUpdater} or {@code null} if it - * could not be created. Because of this the caller need to check for {@code null} and if {@code null} is returned - * use {@link AtomicLongFieldUpdater#newUpdater(Class, String)} as fallback. - */ - public static AtomicLongFieldUpdater newAtomicLongFieldUpdater( - Class tclass, String fieldName) { - return null; - } - - /** - * Create a new {@link Queue} which is safe to use for multiple producers (different threads) and a single - * consumer (one thread!). - */ - public static Queue newMpscQueue() { - return new MpscLinkedQueue(); - } - - private static boolean isRoot0() { - if (Util.WINDOWS) { - return false; - } - - String[] ID_COMMANDS = { "/usr/bin/id", "/bin/id", "id", "/usr/xpg4/bin/id"}; - Pattern UID_PATTERN = Pattern.compile("^(?:0|[1-9][0-9]*)$"); - for (String idCmd: ID_COMMANDS) { - Process p = null; - BufferedReader in = null; - String uid = null; - try { - p = Runtime.getRuntime().exec(new String[] { idCmd, "-u" }); - in = new BufferedReader(new InputStreamReader(p.getInputStream(), CharsetUtil.US_ASCII)); - uid = in.readLine(); - in.close(); - - for (;;) { - try { - int exitCode = p.waitFor(); - if (exitCode != 0) { - uid = null; - } - break; - } catch (InterruptedException e) { - // Ignore - } - } - } catch (Exception e) { - // Failed to run the command. - uid = null; - } finally { - if (in != null) { - try { - in.close(); - } catch (IOException e) { - // Ignore - } - } - if (p != null) { - try { - p.destroy(); - } catch (Exception e) { - // Android sometimes triggers an ErrnoException. - } - } - } - - if (uid != null && UID_PATTERN.matcher(uid).matches()) { - logger.debug("UID: {}", uid); - return "0".equals(uid); - } - } - - logger.debug("Could not determine the current UID using /usr/bin/id; attempting to bind at privileged ports."); - - Pattern PERMISSION_DENIED = Pattern.compile(".*(?:denied|not.*permitted).*"); - for (int i = 1023; i > 0; i --) { - ServerSocket ss = null; - try { - ss = new ServerSocket(); - ss.setReuseAddress(true); - ss.bind(new InetSocketAddress(i)); - if (logger.isDebugEnabled()) { - logger.debug("UID: 0 (succeded to bind at port {})", i); - } - return true; - } catch (Exception e) { - // Failed to bind. - // Check the error message so that we don't always need to bind 1023 times. - String message = e.getMessage(); - if (message == null) { - message = ""; - } - message = message.toLowerCase(); - if (PERMISSION_DENIED.matcher(message).matches()) { - break; - } - } finally { - if (ss != null) { - try { - ss.close(); - } catch (Exception e) { - // Ignore. - } - } - } - } - - logger.debug("UID: non-root (failed to bind at any privileged ports)"); - return false; - } - - - private static long maxDirectMemory0() { - long maxDirectMemory = 0; - try { - // Try to get from sun.misc.VM.maxDirectMemory() which should be most accurate. - Class vmClass = Class.forName("sun.misc.VM", true, ClassLoader.getSystemClassLoader()); - Method m = vmClass.getDeclaredMethod("maxDirectMemory"); - maxDirectMemory = ((Number) m.invoke(null)).longValue(); - } catch (Throwable t) { - // Ignore - } - - if (maxDirectMemory > 0) { - return maxDirectMemory; - } - - try { - // Now try to get the JVM option (-XX:MaxDirectMemorySize) and parse it. - // Note that we are using reflection because Android doesn't have these classes. - Class mgmtFactoryClass = Class.forName( - "java.lang.management.ManagementFactory", true, ClassLoader.getSystemClassLoader()); - Class runtimeClass = Class.forName( - "java.lang.management.RuntimeMXBean", true, ClassLoader.getSystemClassLoader()); - - Object runtime = mgmtFactoryClass.getDeclaredMethod("getRuntimeMXBean").invoke(null); - - - List vmArgs = (List) runtimeClass.getDeclaredMethod("getInputArguments").invoke(runtime); - for (int i = vmArgs.size() - 1; i >= 0; i --) { - Matcher m = MAX_DIRECT_MEMORY_SIZE_ARG_PATTERN.matcher(vmArgs.get(i)); - if (!m.matches()) { - continue; - } - - maxDirectMemory = Long.parseLong(m.group(1)); - switch (m.group(2).charAt(0)) { - case 'k': case 'K': - maxDirectMemory *= 1024; - break; - case 'm': case 'M': - maxDirectMemory *= 1024 * 1024; - break; - case 'g': case 'G': - maxDirectMemory *= 1024 * 1024 * 1024; - break; - } - break; - } - } catch (Throwable t) { - // Ignore - } - - if (maxDirectMemory <= 0) { - maxDirectMemory = Runtime.getRuntime().maxMemory(); - logger.debug("maxDirectMemory: {} bytes (maybe)", maxDirectMemory); - } else { - logger.debug("maxDirectMemory: {} bytes", maxDirectMemory); - } - - return maxDirectMemory; - } - - private PlatformDependent() { - // only static method supported - } -} diff --git a/common/src/main/java/common/net/util/internal/StringUtil.java b/common/src/main/java/common/net/util/internal/StringUtil.java index a7e971f..1716baa 100644 --- a/common/src/main/java/common/net/util/internal/StringUtil.java +++ b/common/src/main/java/common/net/util/internal/StringUtil.java @@ -20,6 +20,8 @@ import java.util.ArrayList; import java.util.Formatter; import java.util.List; +import common.util.Util; + /** * String utility class. */ @@ -136,7 +138,7 @@ public final class StringUtil { try { buf.append(byteToHexStringPadded(value)); } catch (IOException e) { - PlatformDependent.throwException(e); + Util.throwUnchecked(e); } return buf; } @@ -187,7 +189,7 @@ public final class StringUtil { try { buf.append(byteToHexString(value)); } catch (IOException e) { - PlatformDependent.throwException(e); + Util.throwUnchecked(e); } return buf; } diff --git a/common/src/main/java/common/util/Util.java b/common/src/main/java/common/util/Util.java index f44a5ce..1c7920d 100644 --- a/common/src/main/java/common/util/Util.java +++ b/common/src/main/java/common/util/Util.java @@ -488,4 +488,8 @@ int utf_len(const char *str) { } return !hyphen; } + + public static void throwUnchecked(Throwable t) { + throw (RuntimeException)t; + } }