public static string DoHexDump(byte[] array, int fromIndex, int length)
            {
                uint uLength = (uint)length;

                if (uLength > SharedConstants.TooBigOrNegative)
                {
                    ThrowHelper.ThrowArgumentException_PositiveOrZero(length, ExceptionArgument.length);
                }

                if (0u >= uLength)
                {
                    return(string.Empty);
                }

                int endIndex = fromIndex + length;
                var buf      = new char[length << 1];

                int srcIdx = fromIndex;
                int dstIdx = 0;

                for (; srcIdx < endIndex; srcIdx++, dstIdx += 2)
                {
                    Array.Copy(HexdumpTable, (array[srcIdx] & 0xFF) << 1, buf, dstIdx, 2);
                }

                return(new string(buf));
            }
Exemple #2
0
        public unsafe PooledByteBufferAllocator(bool preferDirect, int nHeapArena, int nDirectArena, int pageSize, int maxOrder,
                                                int tinyCacheSize, int smallCacheSize, int normalCacheSize)
            : base(preferDirect)
        {
            if (nHeapArena < 0)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(nHeapArena, ExceptionArgument.nHeapArena);
            }
            if (nDirectArena < 0)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(nHeapArena, ExceptionArgument.nDirectArena);
            }

            _threadCache     = new PoolThreadLocalCache(this);
            _tinyCacheSize   = tinyCacheSize;
            _smallCacheSize  = smallCacheSize;
            _normalCacheSize = normalCacheSize;
            _chunkSize       = ValidateAndCalculateChunkSize(pageSize, maxOrder);

            int pageShifts = ValidateAndCalculatePageShifts(pageSize);

            if (nHeapArena > 0)
            {
                _heapArenas = NewArenaArray <byte[]>(nHeapArena);
                var metrics = new List <IPoolArenaMetric>(_heapArenas.Length);
                for (int i = 0; i < _heapArenas.Length; i++)
                {
                    var arena = new HeapArena(this, pageSize, maxOrder, pageShifts, _chunkSize);
                    _heapArenas[i] = arena;
                    metrics.Add(arena);
                }
                _heapArenaMetrics = metrics.AsReadOnly();
            }
            else
            {
                _heapArenas       = null;
                _heapArenaMetrics = new IPoolArenaMetric[0];
            }

            if (nDirectArena > 0)
            {
                _directArenas = NewArenaArray <byte[]>(nDirectArena);
                var metrics = new List <IPoolArenaMetric>(_directArenas.Length);
                for (int i = 0; i < _directArenas.Length; i++)
                {
                    var arena = new DirectArena(this, pageSize, maxOrder, pageShifts, _chunkSize);
                    _directArenas[i] = arena;
                    metrics.Add(arena);
                }
                _directArenaMetrics = metrics.AsReadOnly();
            }
            else
            {
                _directArenas       = null;
                _directArenaMetrics = new IPoolArenaMetric[0];
            }

            _metric = new PooledByteBufferAllocatorMetric(this);
        }
Exemple #3
0
        public int EnsureWritable(int minWritableBytes, bool force)
        {
            if (minWritableBytes < 0)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(minWritableBytes, ExceptionArgument.minWritableBytes);
            }

            return(0u >= (uint)minWritableBytes ? 0 : 1);
        }
Exemple #4
0
        protected void SetMaxCapacity(int newMaxCapacity)
        {
            if ((uint)newMaxCapacity > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(newMaxCapacity, ExceptionArgument.newMaxCapacity);
            }

            _maxCapacity = newMaxCapacity;
        }
Exemple #5
0
        protected AbstractByteBuffer(int maxCapacity)
        {
            if ((uint)maxCapacity > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(maxCapacity, ExceptionArgument.maxCapacity);
            }

            _maxCapacity = maxCapacity;
        }
        protected void SetMaxCapacity(int newMaxCapacity)
        {
            if (newMaxCapacity < 0)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(newMaxCapacity, ExceptionArgument.newMaxCapacity);
            }

            _maxCapacity = newMaxCapacity;
        }
Exemple #7
0
        public int EnsureWritable(int minWritableBytes, bool force)
        {
            if ((uint)minWritableBytes > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(minWritableBytes, ExceptionArgument.minWritableBytes);
            }

            return(0u >= (uint)minWritableBytes ? 0 : 1);
        }
Exemple #8
0
 void CheckLength0(int length)
 {
     if ((uint)length > SharedConstants.TooBigOrNegative)
     {
         ThrowHelper.ThrowArgumentException_PositiveOrZero(length, ExceptionArgument.length);
     }
     if ((uint)length > 0u)
     {
         ThrowHelper.ThrowIndexOutOfRangeException();
     }
 }
 private static void ThrowInvalidNewCapacity(int minNewCapacity, int maxCapacity)
 {
     if ((uint)minNewCapacity > SharedConstants.TooBigOrNegative)
     {
         ThrowHelper.ThrowArgumentException_PositiveOrZero(minNewCapacity, ExceptionArgument.minNewCapacity);
     }
     if (minNewCapacity > maxCapacity)
     {
         ThrowHelper.ThrowArgumentOutOfRangeException_MaxCapacity(minNewCapacity, maxCapacity);
     }
 }
Exemple #10
0
 private static void ThrowInvalidNewCapacity(int minNewCapacity, int maxCapacity)
 {
     if (minNewCapacity < 0)
     {
         ThrowHelper.ThrowArgumentException_PositiveOrZero(minNewCapacity, ExceptionArgument.minNewCapacity);
     }
     if (minNewCapacity > maxCapacity)
     {
         ThrowHelper.ThrowArgumentOutOfRangeException_MaxCapacity(minNewCapacity, maxCapacity);
     }
 }
Exemple #11
0
 void CheckLength0(int length)
 {
     if (length < 0)
     {
         ThrowHelper.ThrowArgumentException_PositiveOrZero(length, ExceptionArgument.length);
     }
     if (length > 0)
     {
         ThrowHelper.ThrowIndexOutOfRangeException();
     }
 }
Exemple #12
0
 void CheckIndex0(int index, int length)
 {
     if (length < 0)
     {
         ThrowHelper.ThrowArgumentException_PositiveOrZero(length, ExceptionArgument.length);
     }
     if ((uint)index > 0u || length > 0)
     {
         ThrowHelper.ThrowIndexOutOfRangeException();
     }
 }
Exemple #13
0
        static void ThrowInvalidInitialCapacity(int initialCapacity, int maxCapacity)
        {
            if (initialCapacity < 0)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(initialCapacity, ExceptionArgument.initialCapacity);
            }

            if (initialCapacity > maxCapacity)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException_InitialCapacity(initialCapacity, maxCapacity);
            }
        }
        static void ThrowInvalidInitialCapacity(int initialCapacity, int maxCapacity)
        {
            if ((uint)initialCapacity > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(initialCapacity, ExceptionArgument.initialCapacity);
            }

            if (initialCapacity > maxCapacity)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException_InitialCapacity(initialCapacity, maxCapacity);
            }
        }
Exemple #15
0
        public IByteBuffer EnsureWritable(int minWritableBytes)
        {
            if (minWritableBytes < 0)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(minWritableBytes, ExceptionArgument.minWritableBytes);
            }

            if (minWritableBytes != 0)
            {
                ThrowHelper.ThrowIndexOutOfRangeException();
            }
            return(this);
        }
Exemple #16
0
        public IByteBuffer EnsureWritable(int minWritableBytes)
        {
            if ((uint)minWritableBytes > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(minWritableBytes, ExceptionArgument.minWritableBytes);
            }

            if (minWritableBytes != 0)
            {
                ThrowHelper.ThrowIndexOutOfRangeException();
            }
            return(this);
        }
Exemple #17
0
        internal int NormalizeCapacity(int reqCapacity)
        {
            uint ureqCapacity = (uint)reqCapacity;

            if (ureqCapacity > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(reqCapacity, ExceptionArgument.reqCapacity);
            }

            if (ureqCapacity >= (uint)ChunkSize)
            {
                return(reqCapacity);
            }

            if (!IsTiny(reqCapacity))
            {
                // >= 512
                // Doubled

                int normalizedCapacity = reqCapacity;
                normalizedCapacity--;
                normalizedCapacity |= normalizedCapacity.RightUShift(1);
                normalizedCapacity |= normalizedCapacity.RightUShift(2);
                normalizedCapacity |= normalizedCapacity.RightUShift(4);
                normalizedCapacity |= normalizedCapacity.RightUShift(8);
                normalizedCapacity |= normalizedCapacity.RightUShift(16);
                normalizedCapacity++;

                if (normalizedCapacity < 0)
                {
                    normalizedCapacity = normalizedCapacity.RightUShift(1);
                }

                return(normalizedCapacity);
            }

            // Quantum-spaced
            if (0u >= (uint)(reqCapacity & 15))
            {
                return(reqCapacity);
            }

            return((reqCapacity & ~15) + 16);
        }
        public UnpooledUnsafeDirectByteBuffer(IByteBufferAllocator alloc, int initialCapacity, int maxCapacity)
            : base(maxCapacity)
        {
            if (alloc is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.alloc);
            }
            if ((uint)initialCapacity > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(initialCapacity, ExceptionArgument.initialCapacity);
            }
            //if ((uint)maxCapacity > SharedConstants.TooBigOrNegative) { ThrowHelper.ThrowArgumentException_PositiveOrZero(maxCapacity, ExceptionArgument.maxCapacity); }

            if ((uint)initialCapacity > (uint)maxCapacity)
            {
                ThrowHelper.ThrowArgumentException_InitialCapacity(initialCapacity, maxCapacity);
            }

            _allocator = alloc;
            SetByteBuffer(NewArray(initialCapacity), false);
        }
Exemple #19
0
        public UnpooledUnsafeDirectByteBuffer(IByteBufferAllocator alloc, int initialCapacity, int maxCapacity)
            : base(maxCapacity)
        {
            if (alloc is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.alloc);
            }
            if (initialCapacity < 0)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(initialCapacity, ExceptionArgument.initialCapacity);
            }
            if (maxCapacity < 0)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(maxCapacity, ExceptionArgument.maxCapacity);
            }

            if (initialCapacity > maxCapacity)
            {
                ThrowHelper.ThrowArgumentException_InitialCapacity(initialCapacity, maxCapacity);
            }

            _allocator = alloc;
            SetByteBuffer(NewArray(initialCapacity), false);
        }
Exemple #20
0
        // TODO: Test if adding padding helps under contention
        //private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;

        internal PoolThreadCache(PoolArena <T> heapArena, PoolArena <T> directArena,
                                 int tinyCacheSize, int smallCacheSize, int normalCacheSize,
                                 int maxCachedBufferCapacity, int freeSweepAllocationThreshold)
        {
            if (maxCachedBufferCapacity < 0)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(maxCachedBufferCapacity, ExceptionArgument.maxCachedBufferCapacity);
            }

            _freeSweepAllocationThreshold = freeSweepAllocationThreshold;
            HeapArena   = heapArena;
            DirectArena = directArena;
            if (directArena is object)
            {
                tinySubPageDirectCaches = CreateSubPageCaches(
                    tinyCacheSize, PoolArena <T> .NumTinySubpagePools, SizeClass.Tiny);
                smallSubPageDirectCaches = CreateSubPageCaches(
                    smallCacheSize, directArena.NumSmallSubpagePools, SizeClass.Small);

                _numShiftsNormalDirect = Log2(directArena.PageSize);
                normalDirectCaches     = CreateNormalCaches(
                    normalCacheSize, maxCachedBufferCapacity, directArena);

                directArena.IncrementNumThreadCaches();
            }
            else
            {
                // No directArea is configured so just null out all caches
                tinySubPageDirectCaches  = null;
                smallSubPageDirectCaches = null;
                normalDirectCaches       = null;
                _numShiftsNormalDirect   = -1;
            }
            if (heapArena is object)
            {
                // Create the caches for the heap allocations
                tinySubPageHeapCaches = CreateSubPageCaches(
                    tinyCacheSize, PoolArena <T> .NumTinySubpagePools, SizeClass.Tiny);
                smallSubPageHeapCaches = CreateSubPageCaches(
                    smallCacheSize, heapArena.NumSmallSubpagePools, SizeClass.Small);

                _numShiftsNormalHeap = Log2(heapArena.PageSize);
                normalHeapCaches     = CreateNormalCaches(
                    normalCacheSize, maxCachedBufferCapacity, heapArena);

                heapArena.IncrementNumThreadCaches();
            }
            else
            {
                // No heapArea is configured so just null out all caches
                tinySubPageHeapCaches  = null;
                smallSubPageHeapCaches = null;
                normalHeapCaches       = null;
                _numShiftsNormalHeap   = -1;
            }

            // We only need to watch the thread when any cache is used.
            if (tinySubPageDirectCaches is object || smallSubPageDirectCaches is object || normalDirectCaches is object ||
                tinySubPageHeapCaches is object || smallSubPageHeapCaches is object || normalHeapCaches is object)
            {
                if (freeSweepAllocationThreshold < 1)
                {
                    ThrowHelper.ThrowArgumentException_Positive(freeSweepAllocationThreshold, ExceptionArgument.freeSweepAllocationThreshold);
                }
                _freeTask         = Free0;
                _deathWatchThread = Thread.CurrentThread;

                // The thread-local cache will keep a list of pooled buffers which must be returned to
                // the pool when the thread is not alive anymore.
                ThreadDeathWatcher.Watch(_deathWatchThread, _freeTask);
            }
            else
            {
                _freeTask         = null;
                _deathWatchThread = null;
            }
        }