Class PlatformDependent


  • public final class PlatformDependent
    extends java.lang.Object
    Provide utilities that are dependent on the current runtime environment. This class is forked from the netty project and modified to suit our needs.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long allocateMemory​(long size)
      Allocates direct memory.
      static void freeMemory​(long address)
      Deallocates direct memory.
      static boolean hasUnsafe()
      Checks if sun.misc.Unsafe is available and has not been disabled.
      static java.nio.ByteBuffer newDirectBuffer​(long address, long size, int capacity)
      Creates a new ByteBuffer for the specified pre-allocated direct memory.
      static <T> java.util.Queue<T> newMpscQueue​(int maxCapacity)
      Create a new Queue which is safe to use for multiple producers (different threads) and a single consumer (one thread!).
      static <T> java.util.Queue<T> newMpscQueue​(int initialCapacity, int maxCapacity)
      Create a new Queue which is safe to use for multiple producers (different threads) and a single consumer (one thread!).
      static <T> java.util.Queue<T> newSpscQueue​(int maxCapacity)
      Create a new Queue which is safe to use for single producer (one thread!) and a single consumer (one thread!).
      static <T> java.util.Queue<T> newSpscQueue​(int initialCapacity, int maxCapacity)
      Create a new Queue which is safe to use for single producer (one thread!) and a single consumer (one thread!).
      static <T> java.util.Queue<T> newUnboundedLinkedMpscQueue()
      Create a new MPSC Queue that will use a linked data structure and supports Collection.remove(Object).
      static <T> java.util.Queue<T> newUnboundedMpscQueue()
      Create a new Queue which is safe to use for multiple producers (different threads) and a single consumer (one thread!).
      static <T> java.util.Queue<T> newUnboundedMpscQueue​(int initialCapacity)
      Create a new Queue which is safe to use for multiple producers (different threads) and a single consumer (one thread!).
      static <T> java.util.Queue<T> newUnboundedSpscQueue​(int initialCapacity)
      Create a new unbounded Queue which is safe to use for single producer (one thread!) and a single consumer (one thread!).
      static void reserveMemory​(long size, int capacity)
      Reserves direct memory for the specified size and capacity.
      static <T> T throwException​(java.lang.Throwable t)
      Raises an exception bypassing compiler checks for checked exceptions.
      static void unreserveMemory​(long size, int capacity)
      Unreserves direct memory for the specified size and capacity.
      static boolean useDirectBufferWithoutZeroing()
      Checks if it is possible to create a new direct ByteBuffer without zeroing the direct memory.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • hasUnsafe

        public static boolean hasUnsafe()
        Checks if sun.misc.Unsafe is available and has not been disabled.
        Returns:
        true if sun.misc.Unsafe is available.
      • useDirectBufferWithoutZeroing

        public static boolean useDirectBufferWithoutZeroing()
        Checks if it is possible to create a new direct ByteBuffer without zeroing the direct memory.
        Returns:
        true if it is possible to create a new direct ByteBuffer without zeroing the direct memory; false otherwise.
      • reserveMemory

        public static void reserveMemory​(long size,
                                         int capacity)
        Reserves direct memory for the specified size and capacity.
        Parameters:
        size - The size of direct memory to reserve.
        capacity - The capacity of direct memory to reserve.
      • unreserveMemory

        public static void unreserveMemory​(long size,
                                           int capacity)
        Unreserves direct memory for the specified size and capacity.
        Parameters:
        size - The size of direct memory to unreserve.
        capacity - The capacity of direct memory to unreserve.
      • allocateMemory

        public static long allocateMemory​(long size)
        Allocates direct memory.
        Parameters:
        size - The size of direct memory to allocate.
        Returns:
        The address of allocated memory.
      • freeMemory

        public static void freeMemory​(long address)
        Deallocates direct memory.
        Parameters:
        address - The address of direct memory to free.
      • newDirectBuffer

        public static java.nio.ByteBuffer newDirectBuffer​(long address,
                                                          long size,
                                                          int capacity)
        Creates a new ByteBuffer for the specified pre-allocated direct memory.
        Parameters:
        address - The address of pre-allocated direct memory.
        size - The size of pre-allocated direct memory.
        capacity - The capacity of pre-allocated direct memory.
        Returns:
        A new ByteBuffer.
      • throwException

        public static <T> T throwException​(java.lang.Throwable t)
        Raises an exception bypassing compiler checks for checked exceptions.
        Type Parameters:
        T - The expected type
        Parameters:
        t - The Throwable to throw.
        Returns:
        nothing actually will be returned from this method because it rethrows the specified exception. Making this method return an arbitrary type makes the caller method easier as they do not have to add a return statement after calling this method.
      • newUnboundedMpscQueue

        public static <T> java.util.Queue<T> newUnboundedMpscQueue()
        Create a new Queue which is safe to use for multiple producers (different threads) and a single consumer (one thread!).
        Type Parameters:
        T - Type of items stored in the queue.
        Returns:
        A new unbounded MPSC Queue.
      • newUnboundedMpscQueue

        public static <T> java.util.Queue<T> newUnboundedMpscQueue​(int initialCapacity)
        Create a new Queue which is safe to use for multiple producers (different threads) and a single consumer (one thread!).
        Type Parameters:
        T - Type of items stored in the queue.
        Parameters:
        initialCapacity - of the returned queue.
        Returns:
        A new unbounded MPSC Queue.
      • newMpscQueue

        public static <T> java.util.Queue<T> newMpscQueue​(int maxCapacity)
        Create a new Queue which is safe to use for multiple producers (different threads) and a single consumer (one thread!).
        Type Parameters:
        T - Type of items stored in the queue.
        Parameters:
        maxCapacity - of the queue.
        Returns:
        A new MPSC Queue with max capacity of maxCapacity.
      • newMpscQueue

        public static <T> java.util.Queue<T> newMpscQueue​(int initialCapacity,
                                                          int maxCapacity)
        Create a new Queue which is safe to use for multiple producers (different threads) and a single consumer (one thread!).
        Type Parameters:
        T - Type of items stored in the queue.
        Parameters:
        initialCapacity - Initial capacity for the queue.
        maxCapacity - of the queue.
        Returns:
        A new MPSC Queue with max capacity of maxCapacity.
      • newUnboundedLinkedMpscQueue

        public static <T> java.util.Queue<T> newUnboundedLinkedMpscQueue()
        Create a new MPSC Queue that will use a linked data structure and supports Collection.remove(Object).

        Some reasons to use this queue as opposed to newUnboundedMpscQueue() include the following:

        • Lower Initial Memory Overhead - Currently the linked queues consume 392 bytes vs 568 for the array based queues. Also consider the JDK based ConcurrentLinkedQueue only has 24 bytes of initial overhead so this maybe a viable alternative if memory pressure exists and the size is expected to be small.
        • Collection.remove(Object) support - Current only the linked variants support this operation.
        Type Parameters:
        T - The data type of the queue.
        Returns:
        a new MPSC Queue that will use a linked data structure and supports Collection.remove(Object).
      • newSpscQueue

        public static <T> java.util.Queue<T> newSpscQueue​(int maxCapacity)
        Create a new Queue which is safe to use for single producer (one thread!) and a single consumer (one thread!).
        Type Parameters:
        T - Type of items stored in the queue.
        Parameters:
        maxCapacity - of the queue.
        Returns:
        A new SPSC Queue with max capacity of maxCapacity.
      • newSpscQueue

        public static <T> java.util.Queue<T> newSpscQueue​(int initialCapacity,
                                                          int maxCapacity)
        Create a new Queue which is safe to use for single producer (one thread!) and a single consumer (one thread!).
        Type Parameters:
        T - Type of items stored in the queue.
        Parameters:
        initialCapacity - Initial capacity for the queue.
        maxCapacity - of the queue.
        Returns:
        A new SPSC Queue with max capacity of maxCapacity.
      • newUnboundedSpscQueue

        public static <T> java.util.Queue<T> newUnboundedSpscQueue​(int initialCapacity)
        Create a new unbounded Queue which is safe to use for single producer (one thread!) and a single consumer (one thread!).
        Type Parameters:
        T - Type of items stored in the queue.
        Parameters:
        initialCapacity - of the returned queue.
        Returns:
        A new unbounded SPSC Queue.