Exemplo n.º 1
0
        /// <inheritdoc/>
        public override T Rent(bool resetObject = false)
        {
            ThrowForBadAlloc();

            var node = _freeTs;
            _freeTs = node!.Next;

            var obj = _pool[node.Value];

            if (resetObject && obj is IResettable resettable)
            {
                resettable.Reset();
            }

            return obj;
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="elementCount"></param>
        /// <param name="pin"></param>
        public PoolAllocator(int elementCount, bool pin = false)
        {
            if (elementCount < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(nameof(elementCount));
            }

            _pool = DefaultPool.Rent(elementCount);

            _freeTs = new LinkedNode<int>(0);
            var node = _freeTs;

            for (var i = 1; i < _pool.Length; i++)
            {
                node.Next = new LinkedNode<int>(i);
                node = node.Next;
            }

            // https://github.com/dotnet/runtime/issues/36183
            if (pin)
            {
                ThrowHelper.ThrowNotSupportedException();
            }
        }