forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryElement.cs
More file actions
260 lines (229 loc) · 6.9 KB
/
Copy pathMemoryElement.cs
File metadata and controls
260 lines (229 loc) · 6.9 KB
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
253
254
255
256
257
258
259
260
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System.Collections.Generic;
using UnityEditorInternal;
using UnityEngine;
namespace UnityEditor
{
internal class ObjectInfo
{
public int instanceId;
public long memorySize;
public int reason;
public List<ObjectInfo> referencedBy;
public string name;
public string className;
}
[System.Serializable]
internal class MemoryElement
{
public List<MemoryElement> children = null;
public MemoryElement parent = null;
public ObjectInfo memoryInfo;
public long totalMemory;
public int totalChildCount;
public string name;
public bool expanded;
public string description;
public MemoryElement()
{
children = new List<MemoryElement>();
}
public MemoryElement(string n)
{
expanded = false;
name = n;
children = new List<MemoryElement>();
description = "";
}
public MemoryElement(ObjectInfo memInfo, bool finalize)
{
expanded = false;
memoryInfo = memInfo;
name = memoryInfo.name;
totalMemory = memInfo != null ? memInfo.memorySize : 0;
totalChildCount = 1;
if (finalize)
children = new List<MemoryElement>();
}
public MemoryElement(string n, List<MemoryElement> groups)
{
name = n;
expanded = false;
description = "";
totalMemory = 0;
totalChildCount = 0;
children = new List<MemoryElement>();
foreach (MemoryElement group in groups)
{
AddChild(group);
}
}
public void ExpandChildren()
{
if (children != null)
return;
children = new List<MemoryElement>();
for (int i = 0; i < ReferenceCount(); i++)
AddChild(new MemoryElement(memoryInfo.referencedBy[i], false));
}
public int AccumulatedChildCount()
{
return totalChildCount;
}
public int ChildCount()
{
if (children != null)
return children.Count;
return ReferenceCount();
}
public int ReferenceCount()
{
return memoryInfo != null && memoryInfo.referencedBy != null ? memoryInfo.referencedBy.Count : 0;
}
public void AddChild(MemoryElement node)
{
if (node == this)
{
throw new System.Exception("Should not AddChild to itself");
}
children.Add(node);
node.parent = this;
totalMemory += node.totalMemory;
totalChildCount += node.totalChildCount;
}
public int GetChildIndexInList()
{
for (int i = 0; i < parent.children.Count; i++)
{
if (parent.children[i] == this)
return i;
}
return parent.children.Count;
}
public MemoryElement GetPrevNode()
{
int siblingindex = GetChildIndexInList() - 1;
if (siblingindex >= 0)
{
MemoryElement prev = parent.children[siblingindex];
while (prev.expanded)
{
prev = prev.children[prev.children.Count - 1];
}
return prev;
}
else
return parent;
}
public MemoryElement GetNextNode()
{
if (expanded && children.Count > 0)
return children[0];
int nextsiblingindex = GetChildIndexInList() + 1;
if (nextsiblingindex < parent.children.Count)
return parent.children[nextsiblingindex];
MemoryElement p = parent;
while (p.parent != null)
{
int parentsiblingindex = p.GetChildIndexInList() + 1;
if (parentsiblingindex < p.parent.children.Count)
return p.parent.children[parentsiblingindex];
p = p.parent;
}
return null;
}
public MemoryElement GetRoot()
{
if (parent != null)
return parent.GetRoot();
return this;
}
public MemoryElement FirstChild()
{
return children[0];
}
public MemoryElement LastChild()
{
if (!expanded)
return this;
return children[children.Count - 1].LastChild();
}
}
[System.Serializable]
class MemoryElementSelection
{
private MemoryElement m_Selected = null;
public void SetSelection(MemoryElement node)
{
m_Selected = node;
MemoryElement parent = node.parent;
while (parent != null)
{
parent.expanded = true;
parent = parent.parent;
}
}
public void ClearSelection()
{
m_Selected = null;
}
public bool isSelected(MemoryElement node)
{
return (m_Selected == node);
}
public MemoryElement Selected
{
get { return m_Selected; }
}
public void MoveUp()
{
if (m_Selected == null)
return;
if (m_Selected.parent == null)
return;
MemoryElement prev = m_Selected.GetPrevNode();
if (prev.parent != null)
SetSelection(prev);
else
SetSelection(prev.FirstChild());
}
public void MoveDown()
{
if (m_Selected == null)
return;
if (m_Selected.parent == null)
return;
MemoryElement next = m_Selected.GetNextNode();
if (next != null)
SetSelection(next);
}
public void MoveFirst()
{
if (m_Selected == null)
return;
if (m_Selected.parent == null)
return;
SetSelection(m_Selected.GetRoot().FirstChild());
}
public void MoveLast()
{
if (m_Selected == null)
return;
if (m_Selected.parent == null)
return;
SetSelection(m_Selected.GetRoot().LastChild());
}
public void MoveParent()
{
if (m_Selected == null)
return;
if (m_Selected.parent == null)
return;
if (m_Selected.parent.parent == null)
return;
SetSelection(m_Selected.parent);
}
}
}