Esempio n. 1
0
    //object SyncRoot
    //{
    //    get
    //    {
    //        if (_syncRoot == null)
    //        {
    //            System.Threading.Interlocked.CompareExchange<System.Object>(ref _syncRoot, new System.Object(), null);
    //        }
    //        return _syncRoot;
    //    }
    //}

    private BufferPoolNode GetCachedBufferPoolNode(int minLength, int desiredLength, bool bStrictLength)
    {
        CleanOldBuffer();

        int            bufferLen      = _bufferMap.Count();
        BufferPoolNode fondBufferNode = null;

        if (bufferLen > 0)
        {
            cachedSerchNode.bufferLength = minLength;
            int index     = Array.BinarySearch(_bufferMap.buffer, 0, bufferLen, cachedSerchNode, poolCompare);
            int realIndex = index >= 0 ? index : ~index;
            if (realIndex < bufferLen && !bStrictLength)
            {
                int            len      = bufferLen - 1;
                BufferPoolNode tempNode = null;
                do
                {
                    tempNode = _bufferMap.buffer[realIndex];
                }while (tempNode.bufferPool.Count == 0 && realIndex++ < len);
            }

            bool bNodeAvailable = realIndex < bufferLen;
            bNodeAvailable &= bStrictLength ? index >= 0 : bNodeAvailable;
            if (bNodeAvailable)
            {
                fondBufferNode = _bufferMap.buffer[realIndex];
            }
        }

        if (fondBufferNode == null)
        {
            cachedSerchNode.bufferLength = desiredLength;
            int index = 0;
            if (bufferLen > 0)
            {
                index = Array.BinarySearch(_bufferMap.buffer, 0, bufferLen, cachedSerchNode, poolCompare);
            }
            int realIndex = index >= 0 ? index : ~index;

            if (index >= 0 && bufferLen > 0)
            {
                fondBufferNode = _bufferMap.buffer[realIndex];
            }
            else if (realIndex < bufferLen)
            {
                fondBufferNode = CreateNewNode(desiredLength);
                _bufferMap.Insert(realIndex, fondBufferNode);
            }
            else
            {
                fondBufferNode = CreateNewNode(desiredLength);
                _bufferMap.Add(fondBufferNode);
            }
        }

        fondBufferNode.lastUsedFrame = Time.frameCount;

        return(fondBufferNode);
    }
    //~BetterList()
    //{
    //    lock (SyncRoot)
    //    {
    //        RecycleBuffer(buffer);
    //    }
    //}

    /// <summary>
    /// Helper function that expands the size of the array, maintaining the content.
    /// </summary>

    void AllocateMore(int min = 1)
    {
        int newBufferLength = (buffer != null) ? Mathf.Max(buffer.Length << 1, 32) : 32;

        if (newBufferLength < min)
        {
            newBufferLength = (Mathf.Max(32, min) / 32) * 32 << 1;
        }
        BufferPoolNode fondBufferNode = GetCachedBufferPoolNode(newBufferLength, false);

        if (fondBufferNode != null && fondBufferNode.bufferPool.Count > 0)
        {
            LinkedListNode <T[]> firstNode = fondBufferNode.bufferPool.First;
            T[] curBuffer = firstNode.Value;
            if (buffer != null && size > 0)
            {
                Array.ConstrainedCopy(buffer, 0, curBuffer, 0, size);
            }

            buffer = curBuffer;
            fondBufferNode.bufferPool.RemoveFirst();
            firstNode.Value = null;
            _invalidNode.AddLast(firstNode);
            return;
        }

        T[] newList = new T[newBufferLength];
        if (buffer != null && size > 0)
        {
            Array.ConstrainedCopy(buffer, 0, newList, 0, size);
        }

        RecycleBuffer(buffer);
        buffer = newList;
    }
Esempio n. 3
0
    public void RecycleBuffer(T[] dirtyBuffer)
    {
        if (dirtyBuffer == null)
        {
            return;
        }

        BufferPoolNode fondBufferNode = GetCachedBufferPoolNode(dirtyBuffer.Length, true);

        AppendBufferNode(fondBufferNode.bufferPool, dirtyBuffer);
    }
Esempio n. 4
0
    //object SyncRoot
    //{
    //    get
    //    {
    //        if (_syncRoot == null)
    //        {
    //            System.Threading.Interlocked.CompareExchange<System.Object>(ref _syncRoot, new System.Object(), null);
    //        }
    //        return _syncRoot;
    //    }
    //}

    BufferPoolNode GetCachedBufferPoolNode(int requiredLength, bool bStrictLength)
    {
        BufferPoolNode fondBufferNode = null;

        if (_bufferMap.Count > 0)
        {
            var itorNode = _bufferMap.First;

            while (itorNode != null)
            {
                bool bNodeAvailable = bStrictLength ? itorNode.Value.bufferLength == requiredLength : itorNode.Value.bufferLength >= requiredLength;
                if (bNodeAvailable)
                {
                    fondBufferNode = itorNode.Value;
                    break;
                }

                itorNode = itorNode.Next;
            }
        }

        if (fondBufferNode == null)
        {
            fondBufferNode = new BufferPoolNode();
            fondBufferNode.bufferLength = requiredLength;
            fondBufferNode.bufferPool   = new LinkedList <T[]>();

            bool bInserted = false;
            var  itorNode  = _bufferMap.First;
            while (itorNode != null)
            {
                if (itorNode.Value.bufferLength > requiredLength)
                {
                    _bufferMap.AddBefore(itorNode, fondBufferNode);
                    bInserted = true;
                    break;
                }

                itorNode = itorNode.Next;
            }
            if (!bInserted)
            {
                _bufferMap.AddLast(fondBufferNode);
            }
        }

        return(fondBufferNode);
    }
Esempio n. 5
0
    //~BetterList()
    //{
    //    lock (SyncRoot)
    //    {
    //        RecycleBuffer(buffer);
    //    }
    //}

    /// <summary>
    /// Helper function that resizes the size of the array, maintaining the content.
    /// </summary>

    private void ResizeBuffer(bool bExpand, int min = 1)
    {
        int minLength     = min;
        int desiredLength = min;

        if (bExpand)
        {
            minLength     = (buffer != null) ? Mathf.Max(buffer.Length + 1, 32) : 32;
            minLength     = Mathf.Max(minLength, min);
            desiredLength = (buffer != null) ? Mathf.Max(buffer.Length << 1, 32) : 32;
            if (desiredLength < min)
            {
                desiredLength = (Mathf.Max(32, min) / 32) * 32 << 1;
            }
        }

        T[]            newList        = null;
        BufferPoolNode fondBufferNode = GetCachedBufferPoolNode(minLength, desiredLength, !bExpand);
        bool           bFond          = fondBufferNode != null && fondBufferNode.bufferPool.Count > 0;

        if (bFond)
        {
            LinkedListNode <T[]> firstNode = fondBufferNode.bufferPool.First;
            newList = firstNode.Value;
            fondBufferNode.bufferPool.RemoveFirst();
            firstNode.Value = null;
            _invalidNode.AddLast(firstNode);
        }
        else
        {
            newList = new T[desiredLength];
        }

        if (buffer != null && size > 0)
        {
            Array.ConstrainedCopy(buffer, 0, newList, 0, size);
        }

        RecycleBuffer(buffer);
        buffer = newList;
    }