aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Vulkan/CacheByRange.cs
blob: f3f503da40634d2d2bebb7ec55d9f0ff3c1f7287 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;

namespace Ryujinx.Graphics.Vulkan
{
    struct CacheByRange<T> where T : IDisposable
    {
        private Dictionary<ulong, T> _ranges;

        public void Add(int offset, int size, T value)
        {
            EnsureInitialized();
            _ranges.Add(PackRange(offset, size),  value);
        }

        public bool TryGetValue(int offset, int size, out T value)
        {
            EnsureInitialized();
            return _ranges.TryGetValue(PackRange(offset, size), out value);
        }

        public void Clear()
        {
            if (_ranges != null)
            {
                foreach (T value in _ranges.Values)
                {
                    value.Dispose();
                }

                _ranges.Clear();
                _ranges = null;
            }
        }

        private void EnsureInitialized()
        {
            if (_ranges == null)
            {
                _ranges = new Dictionary<ulong, T>();
            }
        }

        private static ulong PackRange(int offset, int size)
        {
            return (uint)offset | ((ulong)size << 32);
        }

        public void Dispose()
        {
            Clear();
        }
    }
}