-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackAllocHelper.cs
More file actions
45 lines (37 loc) · 1.03 KB
/
StackAllocHelper.cs
File metadata and controls
45 lines (37 loc) · 1.03 KB
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
using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace VT2AssetLib;
[SkipLocalsInit]
internal readonly ref struct StackAllocHelper<T>
{
public Span<T> Span => _span;
private readonly Span<T> _span;
private readonly ArrayPool<T>? _sourcePool;
private readonly T[]? _rented;
public StackAllocHelper(Span<T> buffer)
{
_span = buffer;
_sourcePool = null;
Unsafe.SkipInit(out _rented);
}
public StackAllocHelper(int length) : this(ArrayPool<T>.Shared, length)
{
}
public StackAllocHelper(ArrayPool<T> sourcePool!!, int length)
{
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length));
_sourcePool = sourcePool;
_rented = sourcePool.Rent(length);
_span = _rented.AsSpan(0, length);
}
public void Dispose()
{
if (_sourcePool is not null)
{
Debug.Assert(_rented is not null);
_sourcePool.Return(_rented);
}
}
}