aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Memory/Range/MultiRangeList.cs
blob: 5131889fcd8bad139618a349a7c24785b7105725 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using Ryujinx.Common.Collections;
using System.Collections;
using System.Collections.Generic;

namespace Ryujinx.Memory.Range
{
    public class MultiRangeList<T> : IEnumerable<T> where T : IMultiRangeItem
    {
        private readonly IntervalTree<ulong, T> _items;

        public int Count { get; private set; }

        /// <summary>
        /// Creates a new range list.
        /// </summary>
        public MultiRangeList()
        {
            _items = new IntervalTree<ulong, T>();
        }

        /// <summary>
        /// Adds a new item to the list.
        /// </summary>
        /// <param name="item">The item to be added</param>
        public void Add(T item)
        {
            MultiRange range = item.Range;

            for (int i = 0; i < range.Count; i++)
            {
                var subrange = range.GetSubRange(i);

                if (IsInvalid(ref subrange))
                {
                    continue;
                }

                _items.Add(subrange.Address, subrange.EndAddress, item);
            }

            Count++;
        }

        /// <summary>
        /// Removes an item from the list.
        /// </summary>
        /// <param name="item">The item to be removed</param>
        /// <returns>True if the item was removed, or false if it was not found</returns>
        public bool Remove(T item)
        {
            MultiRange range = item.Range;

            int removed = 0;

            for (int i = 0; i < range.Count; i++)
            {
                var subrange = range.GetSubRange(i);

                if (IsInvalid(ref subrange))
                {
                    continue;
                }

                removed += _items.Remove(subrange.Address, item);
            }

            if (removed > 0)
            {
                // All deleted intervals are for the same item - the one we removed.
                Count--;
            }

            return removed > 0;
        }

        /// <summary>
        /// Gets all items on the list overlapping the specified memory range.
        /// </summary>
        /// <param name="address">Start address of the range</param>
        /// <param name="size">Size in bytes of the range</param>
        /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
        /// <returns>The number of overlapping items found</returns>
        public int FindOverlaps(ulong address, ulong size, ref T[] output)
        {
            return FindOverlaps(new MultiRange(address, size), ref output);
        }

        /// <summary>
        /// Gets all items on the list overlapping the specified memory ranges.
        /// </summary>
        /// <param name="range">Ranges of memory being searched</param>
        /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
        /// <returns>The number of overlapping items found</returns>
        public int FindOverlaps(MultiRange range, ref T[] output)
        {
            int overlapCount = 0;

            for (int i = 0; i < range.Count; i++)
            {
                var subrange = range.GetSubRange(i);

                if (IsInvalid(ref subrange))
                {
                    continue;
                }

                overlapCount = _items.Get(subrange.Address, subrange.EndAddress, ref output, overlapCount);
            }

            // Remove any duplicates, caused by items having multiple sub range nodes in the tree.
            if (overlapCount > 1)
            {
                int insertPtr = 0;
                for (int i = 0; i < overlapCount; i++)
                {
                    T item = output[i];
                    bool duplicate = false;

                    for (int j = insertPtr - 1; j >= 0; j--)
                    {
                        if (item.Equals(output[j]))
                        {
                            duplicate = true;
                            break;
                        }
                    }

                    if (!duplicate)
                    {
                        if (insertPtr != i)
                        {
                            output[insertPtr] = item;
                        }

                        insertPtr++;
                    }
                }

                overlapCount = insertPtr;
            }

            return overlapCount;
        }

        /// <summary>
        /// Checks if a given sub-range of memory is invalid.
        /// Those are used to represent unmapped memory regions (holes in the region mapping).
        /// </summary>
        /// <param name="subRange">Memory range to checl</param>
        /// <returns>True if the memory range is considered invalid, false otherwise</returns>
        private static bool IsInvalid(ref MemoryRange subRange)
        {
            return subRange.Address == ulong.MaxValue;
        }

        /// <summary>
        /// Gets all items on the list starting at the specified memory address.
        /// </summary>
        /// <param name="baseAddress">Base address to find</param>
        /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
        /// <returns>The number of matches found</returns>
        public int FindOverlaps(ulong baseAddress, ref T[] output)
        {
            int count = _items.Get(baseAddress, ref output);

            // Only output items with matching base address
            int insertPtr = 0;
            for (int i = 0; i < count; i++)
            {
                if (output[i].BaseAddress == baseAddress)
                {
                    if (i != insertPtr)
                    {
                        output[insertPtr] = output[i];
                    }

                    insertPtr++;
                }
            }

            return insertPtr;
        }

        private List<T> GetList()
        {
            var items = _items.AsList();
            var result = new List<T>();

            foreach (RangeNode<ulong, T> item in items)
            {
                if (item.Start == item.Value.BaseAddress)
                {
                    result.Add(item.Value);
                }
            }

            return result;
        }

        public IEnumerator<T> GetEnumerator()
        {
            return GetList().GetEnumerator();
        }

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