aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Kernel/KTlsPageInfo.cs
blob: 18dc2decf4a54d1e9e6685459924d3ea01394d4b (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
73
namespace Ryujinx.HLE.HOS.Kernel
{
    class KTlsPageInfo
    {
        public const int TlsEntrySize = 0x200;

        public ulong PageAddr { get; private set; }

        private bool[] IsSlotFree;

        public KTlsPageInfo(ulong PageAddress)
        {
            this.PageAddr = PageAddress;

            IsSlotFree = new bool[KMemoryManager.PageSize / TlsEntrySize];

            for (int Index = 0; Index < IsSlotFree.Length; Index++)
            {
                IsSlotFree[Index] = true;
            }
        }

        public bool TryGetFreePage(out ulong Address)
        {
            Address = PageAddr;

            for (int Index = 0; Index < IsSlotFree.Length; Index++)
            {
                if (IsSlotFree[Index])
                {
                    IsSlotFree[Index] = false;

                    return true;
                }

                Address += TlsEntrySize;
            }

            Address = 0;

            return false;
        }

        public bool IsFull()
        {
            bool HasFree = false;

            for (int Index = 0; Index < IsSlotFree.Length; Index++)
            {
                HasFree |= IsSlotFree[Index];
            }

            return !HasFree;
        }

        public bool IsEmpty()
        {
            bool AllFree = true;

            for (int Index = 0; Index < IsSlotFree.Length; Index++)
            {
                AllFree &= IsSlotFree[Index];
            }

            return AllFree;
        }

        public void FreeTlsSlot(ulong Address)
        {
            IsSlotFree[(Address - PageAddr) / TlsEntrySize] = true;
        }
    }
}