Summary

Class:System.Buffers.ArrayMemoryPool`1
Assembly:System.Memory
File(s):C:\GitHub\corefx\src\System.Memory\src\System\Buffers\ArrayMemoryPool.ArrayMemoryPoolBuffer.cs
C:\GitHub\corefx\src\System.Memory\src\System\Buffers\ArrayMemoryPool.cs
Covered lines:55
Uncovered lines:0
Coverable lines:55
Total lines:131
Line coverage:100%
Branch coverage:100%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
Rent(...)34100100
Dispose(...)10100100
.ctor(...)10100100
Dispose(...)22100100
TryGetArray(...)22100100
Pin(...)32100100
Retain()22100100
Release()34100100

File(s)

C:\GitHub\corefx\src\System.Memory\src\System\Buffers\ArrayMemoryPool.ArrayMemoryPoolBuffer.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System.Runtime.InteropServices;
 6#if !netstandard
 7using Internal.Runtime.CompilerServices;
 8#else
 9using System.Runtime.CompilerServices;
 10#endif
 11
 12namespace System.Buffers
 13{
 14    internal sealed partial class ArrayMemoryPool<T> : MemoryPool<T>
 15    {
 16        private sealed class ArrayMemoryPoolBuffer : OwnedMemory<T>
 17        {
 18            private T[] _array;
 19            private int _refCount;
 20
 121            public ArrayMemoryPoolBuffer(int size)
 122            {
 123                _array = ArrayPool<T>.Shared.Rent(size);
 124            }
 25
 126            public sealed override int Length => _array.Length;
 27
 128            public sealed override bool IsDisposed => _array == null;
 29
 130            protected sealed override bool IsRetained => _refCount > 0;
 31
 32            public sealed override Span<T> Span
 33            {
 34                get
 135                {
 136                    if (IsDisposed)
 137                        ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer();
 38
 139                    return _array;
 140                }
 41            }
 42
 43            protected sealed override void Dispose(bool disposing)
 144            {
 145                if (_array != null)
 146                {
 147                    ArrayPool<T>.Shared.Return(_array);
 148                    _array = null;
 149                }
 150            }
 51
 52            protected
 53#if netstandard // TryGetArray is exposed as "protected internal". Normally, the rules of C# dictate we override it as "
 54                // in a different assembly. Except in the netstandard config where the base class is in the same assembl
 55            internal
 56#endif
 57            sealed override bool TryGetArray(out ArraySegment<T> arraySegment)
 158            {
 159                if (IsDisposed)
 160                    ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer();
 61
 162                arraySegment = new ArraySegment<T>(_array);
 163                return true;
 164            }
 65
 66            public sealed override MemoryHandle Pin(int byteOffset = 0)
 167            {
 68                unsafe
 169                {
 170                    Retain(); // this checks IsDisposed
 71
 172                    if (byteOffset != 0 && (((uint)byteOffset) - 1) / Unsafe.SizeOf<T>() >= _array.Length)
 173                        ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteOffset);
 74
 175                    GCHandle handle = GCHandle.Alloc(_array, GCHandleType.Pinned);
 176                    return new MemoryHandle(this, ((byte*)handle.AddrOfPinnedObject()) + byteOffset, handle);
 77                }
 178            }
 79
 80            public sealed override void Retain()
 181            {
 182                if (IsDisposed)
 183                    ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer();
 84
 185                _refCount++;
 186            }
 87
 88            public sealed override bool Release()
 189            {
 190                if (IsDisposed)
 191                    ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer();
 92
 193                int newRefCount = --_refCount;
 194                if (newRefCount < 0)
 195                    ThrowHelper.ThrowInvalidOperationException();
 96
 197                return newRefCount != 0;
 198            }
 99        }
 100    }
 101}

C:\GitHub\corefx\src\System.Memory\src\System\Buffers\ArrayMemoryPool.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3// See the LICENSE file in the project root for more information.
 4
 5#if !netstandard
 6using Internal.Runtime.CompilerServices;
 7#else
 8using System.Runtime.CompilerServices;
 9#endif
 10
 11namespace System.Buffers
 12{
 13    internal sealed partial class ArrayMemoryPool<T> : MemoryPool<T>
 14    {
 15        private const int s_maxBufferSize = int.MaxValue;
 116        public sealed override int MaxBufferSize => s_maxBufferSize;
 17
 18        public sealed override OwnedMemory<T> Rent(int minimumBufferSize = -1)
 119        {
 120            if (minimumBufferSize == -1)
 121                minimumBufferSize = 1 + (4095 / Unsafe.SizeOf<T>());
 122            else if (((uint)minimumBufferSize) > s_maxBufferSize)
 123                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.minimumBufferSize);
 24
 125            return new ArrayMemoryPoolBuffer(minimumBufferSize);
 126        }
 27
 228        protected sealed override void Dispose(bool disposing) {}  // ArrayMemoryPool is a shared pool so Dispose() woul
 29    }
 30}