/
MemoryPool.cs
51 lines (44 loc) · 1.82 KB
/
MemoryPool.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace System.Buffers
{
/// <summary>
/// Represents a pool of memory blocks.
/// </summary>
public abstract class MemoryPool<T> : IDisposable
{
// Store the shared ArrayMemoryPool in a field of its derived sealed type so the Jit can "see" the exact type
// when the Shared property is inlined which will allow it to devirtualize calls made on it.
private static readonly ArrayMemoryPool<T> s_shared = new ArrayMemoryPool<T>();
/// <summary>
/// Returns a singleton instance of a MemoryPool based on arrays.
/// </summary>
public static MemoryPool<T> Shared => s_shared;
/// <summary>
/// Returns a memory block capable of holding at least <paramref name="minBufferSize" /> elements of T.
/// </summary>
/// <param name="minBufferSize">If -1 is passed, this is set to a default value for the pool.</param>
public abstract IMemoryOwner<T> Rent(int minBufferSize = -1);
/// <summary>
/// Returns the maximum buffer size supported by this pool.
/// </summary>
public abstract int MaxBufferSize { get; }
/// <summary>
/// Constructs a new instance of a memory pool.
/// </summary>
protected MemoryPool() { }
/// <summary>
/// Frees all resources used by the memory pool.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Frees all resources used by the memory pool.
/// </summary>
/// <param name="disposing"></param>
protected abstract void Dispose(bool disposing);
}
}