Exemplo n.º 1
0
        public void AddPool <T>(ArrayPoolType poolType, ArrayPool <T> pool)
        {
            var index = (int)poolType;

            if (index < 0 || index >= _arrayPools.Length)
            {
                throw new ArgumentException("Invalid argument for type of pool provided.", nameof(poolType));
            }

            if (pool == null)
            {
                throw new ArgumentNullException(nameof(pool));
            }

            // If a pool already exists, ignore it
            if (_arrayPools[index] != null)
            {
                return;
            }

            // We ought to avoid locking here
            // since we initialize all the pools synchronously
            //lock (_arrayPools)
            //{
            // A form of double check locking
            if (_arrayPools[index] == null)
            {
                _arrayPools[index] = pool;
            }
            //}
        }
Exemplo n.º 2
0
        public void CreatePool <T>(ArrayPoolType poolType, bool growable)
        {
            var index = (int)poolType;

            if (index < 0 || index >= _arrayPools.Length)
            {
                throw new ArgumentException("Invalid argument for type of pool provided.", nameof(poolType));
            }

            // If the same pool has already been added, ignore it
            if (_arrayPools[index] != null)
            {
                return;
            }

            // We ought to avoid locking here
            // since we initialize all the pools synchronously
            //lock (_arrayPools)
            //{
            // A form of double check locking
            if (_arrayPools[index] == null)
            {
                _arrayPools[index] = CreateArrayPool <T>(growable);
            }
            //}
        }
Exemplo n.º 3
0
        public ArrayPool <T> GetPool <T>(ArrayPoolType poolType)
        {
            var index = (int)poolType;

            if (index < 0 || index >= _arrayPools.Length)
            {
                throw new ArgumentException("Invalid type of pool requested.", nameof(poolType));
            }

            // Lock not taken on purpose since our pools are always initialized before use
            // and we don't want our operations to halt because of locking
            var pool = _arrayPools[index] as ArrayPool <T>;

            if (pool == null)
            {
                throw new InvalidOperationException("Requested pool has not been added to this manager.");
            }

            return(pool);
        }