예제 #1
0
        private void RentMemory(BufferSegment segment, int sizeHint)
        {
            // Segment should be new or reset, otherwise a memory leak could occur
            Debug.Assert(segment.MemoryOwner is null);
            Debug.Assert(sizeHint >= 0);

            MemoryPool <byte>?pool = null;
            int maxSize            = -1;

            if (!_options.IsDefaultSharedMemoryPool)
            {
                pool    = _options.Pool;
                maxSize = pool.MaxBufferSize;
            }

            if (sizeHint <= maxSize)
            {
                // Use the specified pool as it fits. Specified pool is not null as maxSize == -1 if _pool is null.
                segment.SetOwnedMemory(pool !.Rent(GetSegmentSize(sizeHint, maxSize)));
            }
            else
            {
                // Use the array pool
                int sizeToRequest = GetSegmentSize(sizeHint);
                segment.SetOwnedMemory(ArrayPool <byte> .Shared.Rent(sizeToRequest));
            }

            _writingHeadMemory = segment.AvailableMemory;
        }
예제 #2
0
파일: Pipe.cs 프로젝트: pedrobsaila/runtime
        private BufferSegment AllocateSegment(int sizeHint)
        {
            Debug.Assert(sizeHint >= 0);
            BufferSegment newSegment = CreateSegmentUnsynchronized();

            MemoryPool <byte>?pool = null;
            int maxSize            = -1;

            if (!_options.IsDefaultSharedMemoryPool)
            {
                pool    = _options.Pool;
                maxSize = pool.MaxBufferSize;
            }

            if (sizeHint <= maxSize)
            {
                // Use the specified pool as it fits. Specified pool is not null as maxSize == -1 if _pool is null.
                newSegment.SetOwnedMemory(pool !.Rent(GetSegmentSize(sizeHint, maxSize)));
            }
            else
            {
                // Use the array pool
                int sizeToRequest = GetSegmentSize(sizeHint);
                newSegment.SetOwnedMemory(ArrayPool <byte> .Shared.Rent(sizeToRequest));
            }

            _writingHeadMemory = newSegment.AvailableMemory;

            return(newSegment);
        }
예제 #3
0
        private BufferSegment AllocateSegment(int sizeHint)
        {
            BufferSegment newSegment = CreateSegmentUnsynchronized();

            if (_pool is null || sizeHint > _pool.MaxBufferSize)
            {
                // Use the array pool
                int sizeToRequest = GetSegmentSize(sizeHint);
                newSegment.SetOwnedMemory(ArrayPool <byte> .Shared.Rent(sizeToRequest));
            }
예제 #4
0
파일: Pipe.cs 프로젝트: zouql/runtime
        private BufferSegment AllocateSegment(int sizeHint)
        {
            BufferSegment newSegment = CreateSegmentUnsynchronized();

            int maxSize = _maxPooledBufferSize;

            if (_pool != null && sizeHint <= maxSize)
            {
                // Use the specified pool as it fits
                newSegment.SetOwnedMemory(_pool.Rent(GetSegmentSize(sizeHint, maxSize)));
            }
            else
            {
                // Use the array pool
                int sizeToRequest = GetSegmentSize(sizeHint);
                newSegment.SetOwnedMemory(ArrayPool <byte> .Shared.Rent(sizeToRequest));
            }

            _writingHeadMemory = newSegment.AvailableMemory;

            return(newSegment);
        }
예제 #5
0
        private BufferSegment AllocateSegment(int sizeHint)
        {
            BufferSegment newSegment = CreateSegmentUnsynchronized();

            if (_pool is null)
            {
                // Use the array pool
                newSegment.SetOwnedMemory(ArrayPool <byte> .Shared.Rent(GetSegmentSize(sizeHint)));
            }
            else if (sizeHint <= _pool.MaxBufferSize)
            {
                // Use the specified pool if it fits
                newSegment.SetOwnedMemory(_pool.Rent(GetSegmentSize(sizeHint, _pool.MaxBufferSize)));
            }
            else
            {
                // We can't use the pool so allocate an array
                newSegment.SetUnownedMemory(new byte[sizeHint]);
            }

            _tailMemory = newSegment.AvailableMemory;

            return(newSegment);
        }
예제 #6
0
        private BufferSegment AllocateSegment(int sizeHint)
        {
            Debug.Assert(sizeHint >= 0);
            BufferSegment newSegment = CreateSegmentUnsynchronized();

            int maxSize = _maxPooledBufferSize;

            if (sizeHint <= maxSize)
            {
                // Use the specified pool as it fits. Specified pool is not null as maxSize == -1 if _pool is null.
                newSegment.SetOwnedMemory(_pool !.Rent(GetSegmentSize(sizeHint, maxSize)));
            }
            else
            {
                // Use the array pool
                int sizeToRequest = GetSegmentSize(sizeHint);
                newSegment.SetOwnedMemory(ArrayPool <byte> .Shared.Rent(sizeToRequest));
            }

            _tailMemory = newSegment.AvailableMemory;

            return(newSegment);
        }