aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Horizon/Sdk/Sf/Cmif/ServerDomainManager.cs
blob: 7762345af6890a5ece0ec88b8f326ee69464090c (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using Ryujinx.Horizon.Common;
using System;
using System.Collections.Generic;

namespace Ryujinx.Horizon.Sdk.Sf.Cmif
{
    class ServerDomainManager
    {
        private class EntryManager
        {
            public class Entry
            {
                public int Id { get; }
                public Domain Owner { get; set; }
                public ServiceObjectHolder Obj { get; set; }
                public LinkedListNode<Entry> Node { get; set; }

                public Entry(int id)
                {
                    Id = id;
                }
            }

            private readonly LinkedList<Entry> _freeList;
            private readonly Entry[] _entries;

            public EntryManager(int count)
            {
                _freeList = new LinkedList<Entry>();
                _entries = new Entry[count];

                for (int i = 0; i < count; i++)
                {
                    _freeList.AddLast(_entries[i] = new Entry(i + 1));
                }
            }

            public Entry AllocateEntry()
            {
                lock (_freeList)
                {
                    if (_freeList.Count == 0)
                    {
                        return null;
                    }

                    var entry = _freeList.First.Value;
                    _freeList.RemoveFirst();
                    return entry;
                }
            }

            public void FreeEntry(Entry entry)
            {
                lock (_freeList)
                {
                    DebugUtil.Assert(entry.Owner == null);
                    DebugUtil.Assert(entry.Obj == null);
                    _freeList.AddFirst(entry);
                }
            }

            public Entry GetEntry(int id)
            {
                if (id == 0)
                {
                    return null;
                }

                int index = id - 1;

                if ((uint)index >= (uint)_entries.Length)
                {
                    return null;
                }

                return _entries[index];
            }
        }

        private class Domain : DomainServiceObject, IDisposable
        {
            private readonly ServerDomainManager _manager;
            private readonly LinkedList<EntryManager.Entry> _entries;

            public Domain(ServerDomainManager manager)
            {
                _manager = manager;
                _entries = new LinkedList<EntryManager.Entry>();
            }

            public override ServiceObjectHolder GetObject(int id)
            {
                var entry = _manager._entryManager.GetEntry(id);
                if (entry == null)
                {
                    return null;
                }

                lock (_manager._entryOwnerLock)
                {
                    if (entry.Owner != this)
                    {
                        return null;
                    }
                }

                return entry.Obj.Clone();
            }

            public override ServerDomainBase GetServerDomain()
            {
                return this;
            }

            public override void RegisterObject(int id, ServiceObjectHolder obj)
            {
                var entry = _manager._entryManager.GetEntry(id);
                DebugUtil.Assert(entry != null);

                lock (_manager._entryOwnerLock)
                {
                    DebugUtil.Assert(entry.Owner == null);
                    entry.Owner = this;
                    entry.Node = _entries.AddLast(entry);
                }

                entry.Obj = obj;
            }

            public override Result ReserveIds(Span<int> outIds)
            {
                for (int i = 0; i < outIds.Length; i++)
                {
                    var entry = _manager._entryManager.AllocateEntry();
                    if (entry == null)
                    {
                        return SfResult.OutOfDomainEntries;
                    }

                    DebugUtil.Assert(entry.Owner == null);

                    outIds[i] = entry.Id;
                }

                return Result.Success;
            }

            public override ServiceObjectHolder UnregisterObject(int id)
            {
                var entry = _manager._entryManager.GetEntry(id);
                if (entry == null)
                {
                    return null;
                }

                ServiceObjectHolder obj;

                lock (_manager._entryOwnerLock)
                {
                    if (entry.Owner != this)
                    {
                        return null;
                    }

                    entry.Owner = null;
                    obj = entry.Obj;

                    if (obj.ServiceObject is IDisposable disposableObj)
                    {
                        disposableObj.Dispose();
                    }

                    entry.Obj = null;
                    _entries.Remove(entry.Node);
                    entry.Node = null;
                }

                _manager._entryManager.FreeEntry(entry);

                return obj;
            }

            public override void UnreserveIds(ReadOnlySpan<int> ids)
            {
                for (int i = 0; i < ids.Length; i++)
                {
                    var entry = _manager._entryManager.GetEntry(ids[i]);

                    DebugUtil.Assert(entry != null);
                    DebugUtil.Assert(entry.Owner == null);

                    _manager._entryManager.FreeEntry(entry);
                }
            }

            public void Dispose()
            {
                foreach (var entry in _entries)
                {
                    if (entry.Obj.ServiceObject is IDisposable disposableObj)
                    {
                        disposableObj.Dispose();
                    }
                }

                _manager.FreeDomain(this);
            }
        }

        private readonly EntryManager _entryManager;
        private readonly object _entryOwnerLock;
        private readonly HashSet<Domain> _domains;
        private readonly int _maxDomains;

        public ServerDomainManager(int entryCount, int maxDomains)
        {
            _entryManager = new EntryManager(entryCount);
            _entryOwnerLock = new object();
            _domains = new HashSet<Domain>();
            _maxDomains = maxDomains;
        }

        public DomainServiceObject AllocateDomainServiceObject()
        {
            lock (_domains)
            {
                if (_domains.Count == _maxDomains)
                {
                    return null;
                }

                var domain = new Domain(this);
                _domains.Add(domain);
                return domain;
            }
        }

        public static void DestroyDomainServiceObject(DomainServiceObject obj)
        {
            ((Domain)obj).Dispose();
        }

        private void FreeDomain(Domain domain)
        {
            lock (_domains)
            {
                _domains.Remove(domain);
            }
        }
    }
}