| | | 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 | | |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | namespace System.Buffers |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Manager of <see cref="Memory{T}"/> that provides the implementation. |
| | | 10 | | /// </summary> |
| | | 11 | | public abstract class MemoryManager<T> : IMemoryOwner<T>, IPinnable |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Returns a <see cref="Memory{T}"/>. |
| | | 15 | | /// </summary> |
| | 0 | 16 | | public virtual Memory<T> Memory => new Memory<T>(this, GetSpan().Length); |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Returns a span wrapping the underlying memory. |
| | | 20 | | /// </summary> |
| | | 21 | | public abstract Span<T> GetSpan(); |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Returns a handle to the memory that has been pinned and hence its address can be taken. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="elementIndex">The offset to the element within the memory at which the returned <see cref="Memo |
| | | 27 | | public abstract MemoryHandle Pin(int elementIndex = 0); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Lets the garbage collector know that the object is free to be moved now. |
| | | 31 | | /// </summary> |
| | | 32 | | public abstract void Unpin(); |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Returns a <see cref="Memory{T}"/> for the current <see cref="MemoryManager{T}"/>. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="length">The element count in the memory, starting at offset 0.</param> |
| | | 38 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 39 | | protected Memory<T> CreateMemory(int length) => new Memory<T>(this, length); |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Returns a <see cref="Memory{T}"/> for the current <see cref="MemoryManager{T}"/>. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="start">The offset to the element which the returned memory starts at.</param> |
| | | 45 | | /// <param name="length">The element count in the memory, starting at element offset <paramref name="start"/>.</ |
| | | 46 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 47 | | protected Memory<T> CreateMemory(int start, int length) => new Memory<T>(this, start, length); |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Returns an array segment. |
| | | 51 | | /// <remarks>Returns the default array segment if not overridden.</remarks> |
| | | 52 | | /// </summary> |
| | | 53 | | protected internal virtual bool TryGetArray(out ArraySegment<T> segment) |
| | | 54 | | { |
| | 0 | 55 | | segment = default; |
| | 0 | 56 | | return false; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Implements IDisposable. |
| | | 61 | | /// </summary> |
| | | 62 | | void IDisposable.Dispose() |
| | | 63 | | { |
| | 0 | 64 | | Dispose(disposing: true); |
| | 0 | 65 | | GC.SuppressFinalize(this); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Clean up of any leftover managed and unmanaged resources. |
| | | 70 | | /// </summary> |
| | | 71 | | protected abstract void Dispose(bool disposing); |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | |