Exemplo n.º 1
0
        public void EnsureMinBufferSize(int desiredBufferSize = 0)
        {
            if (desiredBufferSize <= 0)
            {
                desiredBufferSize = CurrentBuffer.Memory.Length * 2;
                if (desiredBufferSize < DefaultMinBufferSize)
                {
                    desiredBufferSize = DefaultMinBufferSize;
                }
            }
            else if (CurrentBuffer.Memory.Length >= desiredBufferSize)
            {
                return;
            }
            IMemoryOwner <byte> newBuffer;

            if (UseMemoryPooling)
            {
                newBuffer = LazinatorUtilities.GetRentedMemory(desiredBufferSize);
            }
            else
            {
                newBuffer = new ReadWriteBytes(new Memory <byte>(new byte[desiredBufferSize]));
            }
            if (TrackMemoryAllocations)
            {
                MemoryAllocations[(int)AllocationID].SetTarget(newBuffer);
            }
            CurrentBuffer.Memory.Span.CopyTo(newBuffer.Memory.Span);
            var oldBuffer = CurrentBuffer;

            CurrentBuffer = newBuffer;
            oldBuffer.Dispose();
        }
Exemplo n.º 2
0
        public ExpandableBytes(int minBufferSize)
        {
            int minimumSize = Math.Max(minBufferSize, DefaultMinBufferSize);

            if (UseMemoryPooling)
            {
                CurrentBuffer = LazinatorUtilities.GetRentedMemory(minimumSize);
            }
            else
            {
                CurrentBuffer = new ReadWriteBytes(new Memory <byte>(new byte[minimumSize]));
            }

            unchecked
            {
                AllocationID = Interlocked.Increment(ref NextAllocationID) - 1;
            }
            if (TrackMemoryAllocations)
            {
                MemoryAllocations.Add(new WeakReference <IMemoryOwner <byte> >(CurrentBuffer));
                MemoryAllocationsManuallyReturned.Add(false);
            }
        }