Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OwnedMemory.cs « Buffers « System « shared « System.Private.CoreLib « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eade1feff3f41627833cfeaa616ca56a8fbce3e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime;
using System.Runtime.CompilerServices;

namespace System.Buffers
{
    /// <summary>
    /// Owner of Memory<typeparamref name="T"/> that provides appropriate lifetime management mechanisms for it.
    /// </summary>
    public abstract class OwnedMemory<T> : IDisposable, IRetainable
    {
        /// <summary>
        /// The number of items in the Memory<typeparamref name="T"/>.
        /// </summary>
        public abstract int Length { get; }

        /// <summary>
        /// Returns a span wrapping the underlying memory.
        /// </summary>
        public abstract Span<T> Span { get; }

        /// <summary>
        /// Returns a Memory<typeparamref name="T"/> if the underlying memory has not been freed.
        /// </summary>
        /// <exception cref="System.ObjectDisposedException">
        /// Thrown when the underlying memory has already been disposed.
        /// </exception>
        public Memory<T> Memory
        {
            get
            {
                if (IsDisposed)
                {
                    ThrowHelper.ThrowObjectDisposedException_MemoryDisposed();
                }
                return new Memory<T>(owner: this, 0, Length);
            }
        }

        /// <summary>
        /// Returns a handle for the array that has been pinned and hence its address can be taken
        /// </summary>
        public abstract MemoryHandle Pin(int byteOffset = 0);

        /// <summary>
        /// Returns an array segment.
        /// </summary>
        protected internal abstract bool TryGetArray(out ArraySegment<T> arraySegment);

        /// <summary>
        /// Implements IDisposable.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">
        /// Throw when there are still retained references to the memory
        /// </exception>
        public void Dispose()
        {
            if (IsRetained)
            {
                ThrowHelper.ThrowInvalidOperationException_OutstandingReferences();
            }
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Clean up of any leftover managed and unmanaged resources.
        /// </summary>
        protected abstract void Dispose(bool disposing);

        /// <summary>
        /// Return true if someone is holding a reference to the memory.
        /// </summary>
        protected abstract bool IsRetained { get; }

        /// <summary>
        /// Return true if the underlying memory has been freed.
        /// </summary>
        public abstract bool IsDisposed { get; }

        /// <summary>
        /// Implements IRetainable. Prevent accidental disposal of the memory.
        /// </summary>
        public abstract void Retain();

        /// <summary>
        /// Implements IRetainable. The memory can now be diposed.
        /// </summary>
        public abstract bool Release();

    }
}