-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoolBuffer.cs
111 lines (95 loc) · 3.59 KB
/
PoolBuffer.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
107
108
109
110
111
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.LinuxTracepoints.Decode
{
using System;
using System.Buffers;
using Debug = System.Diagnostics.Debug;
/// <summary>
/// Manages a buffer rented from ArrayPool.
/// </summary>
internal sealed class PoolBuffer : IDisposable
{
private byte[] array = Array.Empty<byte>();
/// <summary>
/// Value is unchecked and does NOT affect capacity.
/// ValidLength is used by the ValidSpan and ValidMemory properties for the returned size.
/// </summary>
public int ValidLength;
/// <summary>
/// Gets the current capacity of the buffer.
/// </summary>
public int Capacity => this.array.Length;
/// <summary>
/// Returns Span(0..ValidLength). Throws unless 0 <= ValidLength <= Capacity.
/// </summary>
public Span<byte> ValidSpan => new Span<byte>(this.array, 0, this.ValidLength);
/// <summary>
/// Returns Memory(0..ValidLength). Throws unless 0 <= ValidLength <= Capacity.
/// </summary>
public Memory<byte> ValidMemory => new Memory<byte>(this.array, 0, this.ValidLength);
/// <summary>
/// Returns Span(0..Capacity).
/// </summary>
public Span<byte> CapacitySpan => new Span<byte>(this.array);
/// <summary>
/// Returns Memory(0..Capacity).
/// </summary>
public Memory<byte> CapacityMemory => new Memory<byte>(this.array);
/// <summary>
/// Sets ValidLength = 0, Capacity = 0.
/// </summary>
public void Dispose()
{
this.InvalidateAndTrim(0);
}
/// <summary>
/// Sets ValidLength = 0.
/// If Capacity > freeIfCapacityGreaterThan, sets Capacity to 0 (frees buffer).
/// </summary>
public void InvalidateAndTrim(int freeIfCapacityGreaterThan)
{
Debug.Assert(freeIfCapacityGreaterThan >= 0);
this.ValidLength = 0;
var oldArray = this.array;
if (oldArray.Length > freeIfCapacityGreaterThan)
{
this.array = Array.Empty<byte>();
ArrayPool<byte>.Shared.Return(oldArray);
}
}
/// <summary>
/// Keeps buffer content (0..keepData), ensures Capacity >= minimumCapacity.
/// Requires 0 <= keepData <= Capacity and keepData <= minimumCapacity.
/// Throws if minimumCapacity > 1 GB.
/// </summary>
public void EnsureCapacity(int minimumCapacity, int keepSize)
{
Debug.Assert(0 <= keepSize);
Debug.Assert(keepSize <= this.Capacity);
Debug.Assert(keepSize <= minimumCapacity);
if (this.array.Length < minimumCapacity)
{
this.Grow(minimumCapacity, keepSize);
}
#if DEBUG
Array.Fill(this.array, (byte)0xCD, keepSize, minimumCapacity - keepSize);
#endif
}
private void Grow(int minimumCapacity, int keepSize)
{
if (minimumCapacity > 0x40000000)
{
throw new ArgumentOutOfRangeException(nameof(minimumCapacity));
}
var newArray = ArrayPool<byte>.Shared.Rent(minimumCapacity);
var oldArray = this.array;
Array.Copy(oldArray, newArray, keepSize);
this.array = newArray;
if (oldArray.Length > 0)
{
ArrayPool<byte>.Shared.Return(oldArray);
}
}
}
}