예제 #1
0
        internal static void DonateArray(T[] donation)
        {
            // Assume anything could have been set to null, start no sync operation, this could be running during DomainUnload
            if (donation == null || Volatile.Read(ref _done) == 0)
            {
                return;
            }
            var pools = _pools;

            if (pools == null)
            {
                return;
            }
            var capacity = donation.Length;

            if (capacity == 0 || capacity < _minCapacity || capacity > _maxCapacity)
            {
                return;
            }
            capacity = NumericHelper.PopulationCount(capacity) == 1 ? capacity : NumericHelper.NextPowerOf2(capacity);
            var index = NumericHelper.Log2(capacity) - _minCapacityLog2;
            var pool  = pools[index];

            if (pool == null)
            {
                return;
            }
            pool.Donate(donation);
        }
예제 #2
0
        private static int Log2SoftwareFallback(uint value)
        {
            if (value == 0)
            {
                return(0);
            }

            return(NumericHelper.Log2(value));
        }
예제 #3
0
        public void Log2Tests()
        {
            var input  = new[] { 22141034, 146009798, 106447544, 66083576, 28048294, 3848650, 119601527, 182384611, 160860217, 52726162 };
            var output = new[] { 24, 27, 26, 25, 24, 21, 26, 27, 27, 25 };

            for (var index = 0; index < input.Length; index++)
            {
                Assert.AreEqual(output[index], NumericHelper.Log2(input[index]));
            }
            Assert.AreEqual(0, NumericHelper.Log2(1));
            Assert.Throws <ArgumentOutOfRangeException>(() => NumericHelper.Log2(0));
            Assert.Throws <ArgumentOutOfRangeException>(() => NumericHelper.Log2(-1));
        }
예제 #4
0
        internal static void DonateArray(T[] donation)
        {
            if (donation == null || Thread.VolatileRead(ref _done) == 0)
            {
                return;
            }
            var capacity = donation.Length;

            if (capacity == 0 || capacity < INT_MinCapacity || capacity > INT_MaxCapacity)
            {
                return;
            }
            capacity = NumericHelper.PopulationCount(capacity) == 1 ? capacity : NumericHelper.NextPowerOf2(capacity);
            var index = NumericHelper.Log2(capacity) - INT_MinCapacityLog2;

            _pools[index].Donate(donation);
        }
예제 #5
0
 internal static T[] GetArray(int capacity)
 {
     if (capacity == 0)
     {
         return(EmptyArray);
     }
     if (capacity < _minCapacity)
     {
         capacity = _minCapacity;
     }
     capacity = NumericHelper.PopulationCount(capacity) == 1 ? capacity : NumericHelper.NextPowerOf2(capacity);
     if (capacity <= _maxCapacity)
     {
         var index       = NumericHelper.Log2(capacity) - _minCapacityLog2;
         var currentPool = _pools[index];
         if (currentPool != null && currentPool.TryGet(out var result))
         {
             return(result);
         }
     }
     return(new T[capacity]);
 }
예제 #6
0
 internal static T[] GetArray(int capacity)
 {
     if (capacity == 0)
     {
         return(_emptyArray);
     }
     if (capacity < INT_MinCapacity)
     {
         capacity = INT_MinCapacity;
     }
     capacity = NumericHelper.PopulationCount(capacity) == 1 ? capacity : NumericHelper.NextPowerOf2(capacity);
     if (capacity <= INT_MaxCapacity && Thread.VolatileRead(ref _done) == 1)
     {
         var index = NumericHelper.Log2(capacity) - INT_MinCapacityLog2;
         T[] result;
         var currentPool = _pools[index];
         if (currentPool.TryGet(out result))
         {
             return(result);
         }
     }
     return(new T[capacity]);
 }
예제 #7
0
 private static int Log2SoftwareFallback(uint value)
 {
     return(value == 0 ? 0 : NumericHelper.Log2(value));
 }