This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
/
ArrayMemoryPool.cs
106 lines (87 loc) · 3.51 KB
/
ArrayMemoryPool.cs
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
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
namespace System.Buffers.Internal
{
internal sealed class ArrayMemoryPool<T> : MemoryPool<T>
{
const int DefaultSize = 4096;
readonly static ArrayMemoryPool<T> s_shared = new ArrayMemoryPool<T>();
public static ArrayMemoryPool<T> Shared => s_shared;
public override int MaxBufferSize => 1024 * 1024 * 1024;
public override OwnedMemory<T> Rent(int minimumBufferSize = AnySize)
{
if (minimumBufferSize == AnySize) minimumBufferSize = DefaultSize;
else if (minimumBufferSize > MaxBufferSize || minimumBufferSize < 1) throw new ArgumentOutOfRangeException(nameof(minimumBufferSize));
return new ArrayPoolMemory(minimumBufferSize);
}
protected override void Dispose(bool disposing)
{
}
private sealed class ArrayPoolMemory : OwnedMemory<T>
{
T[] _array;
bool _disposed;
int _referenceCount;
public ArrayPoolMemory(int size)
{
_array = ArrayPool<T>.Shared.Rent(size);
}
public override int Length => _array.Length;
public override bool IsDisposed => _disposed;
protected override bool IsRetained => _referenceCount > 0;
public override Span<T> Span
{
get
{
if (IsDisposed) ThrowObjectDisposedException(nameof(ArrayPoolMemory));
return _array;
}
}
protected override void Dispose(bool disposing)
{
var array = Interlocked.Exchange(ref _array, null);
if (array != null) {
_disposed = true;
ArrayPool<T>.Shared.Return(array);
}
}
protected override bool TryGetArray(out ArraySegment<T> arraySegment)
{
if (IsDisposed) ThrowObjectDisposedException(nameof(ArrayMemoryPool<T>));
arraySegment = new ArraySegment<T>(_array);
return true;
}
public override MemoryHandle Pin()
{
unsafe
{
Retain(); // this checks IsDisposed
var handle = GCHandle.Alloc(_array, GCHandleType.Pinned);
return new MemoryHandle(this, (void*)handle.AddrOfPinnedObject(), handle);
}
}
public override void Retain()
{
if (IsDisposed) ThrowObjectDisposedException(nameof(ArrayPoolMemory));
Interlocked.Increment(ref _referenceCount);
}
public override bool Release()
{
int newRefCount = Interlocked.Decrement(ref _referenceCount);
if (newRefCount < 0) throw new InvalidOperationException();
if (newRefCount == 0)
{
Dispose();
return false;
}
return true;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowObjectDisposedException(string objectName)
=> throw new ObjectDisposedException(objectName);
}
}
}