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; }
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); }
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)); }
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); }
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); }
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); }