aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Kernel/Memory/KPageList.cs
blob: 7f2f1ba67bb8487702e2332b599a33643550cd7f (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using Ryujinx.HLE.HOS.Kernel.Common;
using System.Collections;
using System.Collections.Generic;

namespace Ryujinx.HLE.HOS.Kernel.Memory
{
    class KPageList : IEnumerable<KPageNode>
    {
        public LinkedList<KPageNode> Nodes { get; }

        public KPageList()
        {
            Nodes = new LinkedList<KPageNode>();
        }

        public KernelResult AddRange(ulong address, ulong pagesCount)
        {
            if (pagesCount != 0)
            {
                if (Nodes.Last != null)
                {
                    KPageNode lastNode = Nodes.Last.Value;

                    if (lastNode.Address + lastNode.PagesCount * KPageTableBase.PageSize == address)
                    {
                        address     = lastNode.Address;
                        pagesCount += lastNode.PagesCount;

                        Nodes.RemoveLast();
                    }
                }

                Nodes.AddLast(new KPageNode(address, pagesCount));
            }

            return KernelResult.Success;
        }

        public ulong GetPagesCount()
        {
            ulong sum = 0;

            foreach (KPageNode node in Nodes)
            {
                sum += node.PagesCount;
            }

            return sum;
        }

        public bool IsEqual(KPageList other)
        {
            LinkedListNode<KPageNode> thisNode  = Nodes.First;
            LinkedListNode<KPageNode> otherNode = other.Nodes.First;

            while (thisNode != null && otherNode != null)
            {
                if (thisNode.Value.Address    != otherNode.Value.Address ||
                    thisNode.Value.PagesCount != otherNode.Value.PagesCount)
                {
                    return false;
                }

                thisNode  = thisNode.Next;
                otherNode = otherNode.Next;
            }

            return thisNode == null && otherNode == null;
        }

        public void IncrementPagesReferenceCount(KMemoryManager manager)
        {
            foreach (var node in this)
            {
                manager.IncrementPagesReferenceCount(node.Address, node.PagesCount);
            }
        }

        public void DecrementPagesReferenceCount(KMemoryManager manager)
        {
            foreach (var node in this)
            {
                manager.DecrementPagesReferenceCount(node.Address, node.PagesCount);
            }
        }

        public IEnumerator<KPageNode> GetEnumerator()
        {
            return Nodes.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}