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