aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.GAL/Multithreading/Model/SpanRef.cs
blob: faa496f1bb0d2becee69911b949ad107aea16b55 (plain) (blame)
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
using System;

namespace Ryujinx.Graphics.GAL.Multithreading.Model
{
    readonly struct SpanRef<T> where T : unmanaged
    {
        private readonly int _packedLengthId;

        public SpanRef(ThreadedRenderer renderer, T[] data)
        {
            _packedLengthId = -(renderer.AddTableRef(data) + 1);
        }

        public SpanRef(int length)
        {
            _packedLengthId = length;
        }

        public Span<T> Get(ThreadedRenderer renderer)
        {
            if (_packedLengthId >= 0)
            {
                return renderer.SpanPool.Get<T>(_packedLengthId);
            }
            else
            {
                return new Span<T>((T[])renderer.RemoveTableRef(-(_packedLengthId + 1)));
            }
        }

        public void Dispose(ThreadedRenderer renderer)
        {
            if (_packedLengthId > 0)
            {
                renderer.SpanPool.Dispose<T>(_packedLengthId);
            }
        }
    }
}