aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Kernel/Process/KTlsPageManager.cs
blob: 279fa13e8e51987a0df485712f37a7523fb2562b (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
55
56
57
58
59
60
61
using Ryujinx.HLE.HOS.Kernel.Memory;
using System;

namespace Ryujinx.HLE.HOS.Kernel.Process
{
    class KTlsPageManager
    {
        private const int TlsEntrySize = 0x200;

        private readonly long _pagePosition;

        private int _usedSlots;

        private readonly bool[] _slots;

        public bool IsEmpty => _usedSlots == 0;
        public bool IsFull => _usedSlots == _slots.Length;

        public KTlsPageManager(long pagePosition)
        {
            _pagePosition = pagePosition;

            _slots = new bool[KPageTableBase.PageSize / TlsEntrySize];
        }

        public bool TryGetFreeTlsAddr(out long position)
        {
            position = _pagePosition;

            for (int index = 0; index < _slots.Length; index++)
            {
                if (!_slots[index])
                {
                    _slots[index] = true;

                    _usedSlots++;

                    return true;
                }

                position += TlsEntrySize;
            }

            position = 0;

            return false;
        }

        public void FreeTlsSlot(int slot)
        {
            if ((uint)slot > _slots.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(slot));
            }

            _slots[slot] = false;

            _usedSlots--;
        }
    }
}