aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Kernel/KTlsPageManager.cs
blob: 1fb2ce6ad89794897692787413b33e9a9d22ad37 (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
using System;

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

        private long PagePosition;

        private int UsedSlots;

        private bool[] Slots;

        public bool IsEmpty => UsedSlots == 0;
        public bool IsFull  => UsedSlots == Slots.Length;

        public KTlsPageManager(long PagePosition)
        {
            this.PagePosition = PagePosition;

            Slots = new bool[KMemoryManager.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--;
        }
    }
}