| | | 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 | | namespace System.Buffers |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents a pool of memory blocks. |
| | | 9 | | /// </summary> |
| | | 10 | | public abstract class MemoryPool<T> : IDisposable |
| | | 11 | | { |
| | 1 | 12 | | private static readonly MemoryPool<T> s_shared = new ArrayMemoryPool<T>(); |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Returns a singleton instance of a MemoryPool based on arrays. |
| | | 16 | | /// </summary> |
| | 1 | 17 | | public static MemoryPool<T> Shared => s_shared; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Returns a memory block capable of holding at least <paramref name="minBufferSize" /> elements of T. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="minBufferSize">If -1 is passed, this is set to a default value for the pool.</param> |
| | | 23 | | public abstract OwnedMemory<T> Rent(int minBufferSize = -1); |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Returns the maximum buffer size supported by this pool. |
| | | 27 | | /// </summary> |
| | | 28 | | public abstract int MaxBufferSize { get; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Constructs a new instance of a memory pool. |
| | | 32 | | /// </summary> |
| | 3 | 33 | | protected MemoryPool() {} |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Frees all resources used by the memory pool. |
| | | 37 | | /// </summary> |
| | | 38 | | public void Dispose() |
| | 1 | 39 | | { |
| | 1 | 40 | | Dispose(true); |
| | 1 | 41 | | GC.SuppressFinalize(this); |
| | 1 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Frees all resources used by the memory pool. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="disposing"></param> |
| | | 48 | | protected abstract void Dispose(bool disposing); |
| | | 49 | | } |
| | | 50 | | } |