| | | 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 | | using System.Runtime.InteropServices; |
| | | 6 | | #if !netstandard |
| | | 7 | | using Internal.Runtime.CompilerServices; |
| | | 8 | | #else |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | #endif |
| | | 11 | | |
| | | 12 | | namespace 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 | | |
| | 1 | 21 | | public ArrayMemoryPoolBuffer(int size) |
| | 1 | 22 | | { |
| | 1 | 23 | | _array = ArrayPool<T>.Shared.Rent(size); |
| | 1 | 24 | | } |
| | | 25 | | |
| | 1 | 26 | | public sealed override int Length => _array.Length; |
| | | 27 | | |
| | 1 | 28 | | public sealed override bool IsDisposed => _array == null; |
| | | 29 | | |
| | 1 | 30 | | protected sealed override bool IsRetained => _refCount > 0; |
| | | 31 | | |
| | | 32 | | public sealed override Span<T> Span |
| | | 33 | | { |
| | | 34 | | get |
| | 1 | 35 | | { |
| | 1 | 36 | | if (IsDisposed) |
| | 1 | 37 | | ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer(); |
| | | 38 | | |
| | 1 | 39 | | return _array; |
| | 1 | 40 | | } |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | protected sealed override void Dispose(bool disposing) |
| | 1 | 44 | | { |
| | 1 | 45 | | if (_array != null) |
| | 1 | 46 | | { |
| | 1 | 47 | | ArrayPool<T>.Shared.Return(_array); |
| | 1 | 48 | | _array = null; |
| | 1 | 49 | | } |
| | 1 | 50 | | } |
| | | 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) |
| | 1 | 58 | | { |
| | 1 | 59 | | if (IsDisposed) |
| | 1 | 60 | | ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer(); |
| | | 61 | | |
| | 1 | 62 | | arraySegment = new ArraySegment<T>(_array); |
| | 1 | 63 | | return true; |
| | 1 | 64 | | } |
| | | 65 | | |
| | | 66 | | public sealed override MemoryHandle Pin(int byteOffset = 0) |
| | 1 | 67 | | { |
| | | 68 | | unsafe |
| | 1 | 69 | | { |
| | 1 | 70 | | Retain(); // this checks IsDisposed |
| | | 71 | | |
| | 1 | 72 | | if (byteOffset != 0 && (((uint)byteOffset) - 1) / Unsafe.SizeOf<T>() >= _array.Length) |
| | 1 | 73 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteOffset); |
| | | 74 | | |
| | 1 | 75 | | GCHandle handle = GCHandle.Alloc(_array, GCHandleType.Pinned); |
| | 1 | 76 | | return new MemoryHandle(this, ((byte*)handle.AddrOfPinnedObject()) + byteOffset, handle); |
| | | 77 | | } |
| | 1 | 78 | | } |
| | | 79 | | |
| | | 80 | | public sealed override void Retain() |
| | 1 | 81 | | { |
| | 1 | 82 | | if (IsDisposed) |
| | 1 | 83 | | ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer(); |
| | | 84 | | |
| | 1 | 85 | | _refCount++; |
| | 1 | 86 | | } |
| | | 87 | | |
| | | 88 | | public sealed override bool Release() |
| | 1 | 89 | | { |
| | 1 | 90 | | if (IsDisposed) |
| | 1 | 91 | | ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer(); |
| | | 92 | | |
| | 1 | 93 | | int newRefCount = --_refCount; |
| | 1 | 94 | | if (newRefCount < 0) |
| | 1 | 95 | | ThrowHelper.ThrowInvalidOperationException(); |
| | | 96 | | |
| | 1 | 97 | | return newRefCount != 0; |
| | 1 | 98 | | } |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | } |