/
ContentManager.ReferenceCounting.cs
174 lines (157 loc) · 6.4 KB
/
ContentManager.ReferenceCounting.cs
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
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
namespace Stride.Core.Serialization.Contents
{
partial class ContentManager
{
// Used internally for Garbage Collection
// Allocate once and reuse collection for every GC
private Stack<Reference> stack = new Stack<Reference>();
private uint nextCollectIndex;
/// <summary>
/// Increments reference count of an <see cref="Reference"/>.
/// </summary>
/// <param name="reference">The reference.</param>
/// <param name="publicReference">True if public reference.</param>
internal void IncrementReference(Reference reference, bool publicReference)
{
if (publicReference)
{
reference.PublicReferenceCount++;
}
else
{
reference.PrivateReferenceCount++;
}
}
/// <summary>
/// Decrements reference count of an <see cref="Reference"/>.
/// </summary>
/// <param name="reference">The reference.</param>
/// <param name="publicReference">True if public erefere</param>
internal void DecrementReference(Reference reference, bool publicReference)
{
int referenceCount;
if (publicReference)
{
if (reference.PublicReferenceCount <= 0)
throw new InvalidOperationException("Cannot release an object that doesn't have active public references. Load/Unload pairs must match.");
referenceCount = --reference.PublicReferenceCount + reference.PrivateReferenceCount;
}
else
{
if (reference.PrivateReferenceCount <= 0)
throw new InvalidOperationException("Cannot release an object that doesn't have active private references. This is either due to non-matching Load/Unload pairs or an engine internal error.");
referenceCount = --reference.PrivateReferenceCount + reference.PublicReferenceCount;
}
if (referenceCount == 0)
{
// Free the object itself
ReleaseAsset(reference);
// Free all its referenced objects
foreach (var childReference in reference.References)
{
DecrementReference(childReference, false);
}
}
else if (publicReference && reference.PublicReferenceCount == 0)
{
// If there is no more public reference but object is still alive, let's kick a cycle GC
CollectUnreferencedCycles();
}
}
/// <summary>
/// Releases an asset.
/// </summary>
/// <param name="reference">The reference.</param>
private void ReleaseAsset(Reference reference)
{
var referencable = reference.Object as IReferencable;
if (referencable != null)
{
referencable.Release();
}
else
{
var disposable = reference.Object as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
// Remove Reference from loaded assets.
var oldPrev = reference.Prev;
var oldNext = reference.Next;
if (oldPrev != null)
oldPrev.Next = oldNext;
if (oldNext != null)
oldNext.Prev = oldPrev;
if (oldPrev == null)
{
if (oldNext == null)
LoadedAssetUrls.Remove(reference.Url);
else
LoadedAssetUrls[reference.Url] = oldNext;
}
LoadedAssetReferences.Remove(reference.Object);
reference.Object = null;
}
internal void CollectUnreferencedCycles()
{
// Push everything on the stack
var currentCollectIndex = nextCollectIndex++;
foreach (var asset in LoadedAssetUrls)
{
var currentAsset = asset.Value;
do
{
if (asset.Value.PublicReferenceCount > 0)
stack.Push(asset.Value);
currentAsset = currentAsset.Next;
}
while (currentAsset != null);
}
// Until stack is empty, collect references and push them on the stack
while (stack.Count > 0)
{
var v = stack.Pop();
// We use CollectIndex to know if object has already been processed during current collection
var collectIndex = v.CollectIndex;
if (collectIndex != currentCollectIndex)
{
v.CollectIndex = currentCollectIndex;
foreach (var reference in v.References)
{
if (reference.CollectIndex != currentCollectIndex)
stack.Push(reference);
}
}
}
// Collect objects that are not referenceable.
// Reuse stack
// TODO: Use collections where you can iterate and remove at the same time?
foreach (var asset in LoadedAssetUrls)
{
var currentAsset = asset.Value;
do
{
if (asset.Value.CollectIndex != currentCollectIndex)
{
stack.Push(asset.Value);
}
currentAsset = currentAsset.Next;
}
while (currentAsset != null);
}
// Release those objects
// Note: order of release might be unexpected (i.e. if A ref B, B might be released before A)
// We don't really have a choice if there is cycle anyway, but still user could have reference himself to prevent or enforce this order if it's really important.
foreach (var assetReference in stack)
{
ReleaseAsset(assetReference);
}
}
}
}