aboutsummaryrefslogtreecommitdiff
path: root/ARMeilleure/Translation/Cache/JumpTableEntryAllocator.cs
blob: ae2c075e146bc75f2e07ff8f588c87fe17b08dc1 (plain)
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
using ARMeilleure.Common;
using System.Collections.Generic;
using System.Diagnostics;

namespace ARMeilleure.Translation.Cache
{
    class JumpTableEntryAllocator
    {
        private readonly BitMap _bitmap;
        private int _freeHint;

        public JumpTableEntryAllocator()
        {
            _bitmap = new BitMap();
        }

        public bool EntryIsValid(int entryIndex)
        {
            lock (_bitmap)
            {
                return _bitmap.IsSet(entryIndex);
            }
        }

        public void SetEntry(int entryIndex)
        {
            lock (_bitmap)
            {
                _bitmap.Set(entryIndex);
            }
        }

        public int AllocateEntry()
        {
            lock (_bitmap)
            {
                int entryIndex;

                if (!_bitmap.IsSet(_freeHint))
                {
                    entryIndex = _freeHint;
                }
                else
                {
                    entryIndex = _bitmap.FindFirstUnset();
                }

                _freeHint = entryIndex + 1;

                bool wasSet = _bitmap.Set(entryIndex);
                Debug.Assert(wasSet);

                return entryIndex;
            }
        }

        public void FreeEntry(int entryIndex)
        {
            lock (_bitmap)
            {
                _bitmap.Clear(entryIndex);

                _freeHint = entryIndex;
            }
        }

        public IEnumerable<int> GetEntries()
        {
            return _bitmap;
        }
    }
}