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
|
#if ENABLE_PROFILER
#include "UnityPrefix.h"
#include "MemoryProfilerStats.h"
#include "Runtime/BaseClasses/BaseObject.h"
#include "Runtime/Threads/AtomicOps.h"
#include "Runtime/Threads/Thread.h"
#include "Runtime/Serialize/PersistentManager.h"
void profiler_register_object(Object* obj)
{
GetMemoryProfilerStats().RegisterObject(obj);
}
void profiler_unregister_object(Object* obj)
{
GetMemoryProfilerStats().UnregisterObject(obj);
}
void profiler_change_persistancy(int instanceID, bool oldvalue, bool newvalue)
{
GetMemoryProfilerStats().ChangePersistancyflag(instanceID, oldvalue, newvalue);
}
void TestAndInsertObject(Object* obj, int objClassID, int classID, dynamic_array<Object*>& objs)
{
if (objClassID == classID)
objs.push_back(obj);
}
void TestAndRemoveObject(Object* obj, int objClassID, int classID, dynamic_array<Object*>& objs)
{
if (objClassID == classID)
{
// run from the end - last created objects are most likely to be destoyed
dynamic_array<Object*>::iterator it = objs.end();
while(it != objs.begin())
{
--it;
if(*it == obj){
objs.erase(it, it+1);
return;
}
}
ErrorString(Format("An object that was removed, was not found in the object list for that type (%s)", Object::ClassIDToString(classID).c_str()));
}
}
void MemoryProfilerStats::ChangePersistancyflag(int instanceID, bool oldvalue, bool newvalue)
{
if(oldvalue == newvalue)
return;
#if SUPPORT_THREADS
if(!Thread::EqualsCurrentThreadID(GetPersistentManager().GetMainThreadID()))
return;
#endif
Object* obj = Object::IDToPointer(instanceID);
if(obj == NULL)
return;
if(oldvalue == true)
{
AtomicDecrement(&assetCount);
AddDynamicObjectCount(obj, obj->GetClassID());
}
else
{
AtomicIncrement(&assetCount);
RemoveDynamicObjectCount(obj, obj->GetClassID());
}
}
void MemoryProfilerStats::AddDynamicObjectCount(Object* obj, int classID)
{
AtomicIncrement(&sceneObjectCount);
if( classID == ClassID(GameObject) )
AtomicIncrement(&gameObjectCount);
}
void MemoryProfilerStats::RemoveDynamicObjectCount(Object* obj, int classID)
{
AtomicDecrement(&sceneObjectCount);
if( classID == ClassID(GameObject) )
AtomicDecrement(&gameObjectCount);
}
void MemoryProfilerStats::RegisterObject ( Object* obj )
{
int classID = obj->GetClassID();
TestAndInsertObject(obj, classID, ClassID(Texture2D), textures);
TestAndInsertObject(obj, classID, ClassID(Mesh), meshes);
TestAndInsertObject(obj, classID, ClassID(Material), materials);
TestAndInsertObject(obj, classID, ClassID(AnimationClip), animations);
TestAndInsertObject(obj, classID, ClassID(AudioClip), audioclips);
if(classCount.size() <= classID)
classCount.resize_initialized(classID+1,0);
++classCount[classID];
if(obj->IsPersistent())
AtomicIncrement(&assetCount);
else
AddDynamicObjectCount(obj, classID);
}
void MemoryProfilerStats::UnregisterObject ( Object* obj )
{
int classID = obj->GetClassID();
TestAndRemoveObject(obj, classID, ClassID(Texture2D), textures);
TestAndRemoveObject(obj, classID, ClassID(Mesh), meshes);
TestAndRemoveObject(obj, classID, ClassID(Material), materials);
TestAndRemoveObject(obj, classID, ClassID(AnimationClip), animations);
TestAndRemoveObject(obj, classID, ClassID(AudioClip), audioclips);
Assert (classCount.size() > classID);
--classCount[classID];
if(obj->IsPersistent())
AtomicDecrement(&assetCount);
else
RemoveDynamicObjectCount(obj, classID);
}
MemoryProfilerStats::MemoryProfilerStats()
: assetCount(0)
, sceneObjectCount(0)
, gameObjectCount(0)
{
}
MemoryProfilerStats::~MemoryProfilerStats()
{
}
MemoryProfilerStats* gMemoryProfilerStats = NULL;
MemoryProfilerStats& GetMemoryProfilerStats()
{
Assert(gMemoryProfilerStats != NULL);
return *gMemoryProfilerStats;
}
void InitializeMemoryProfilerStats()
{
Assert(gMemoryProfilerStats == NULL);
gMemoryProfilerStats = new MemoryProfilerStats();
}
void CleanupMemoryProfilerStats()
{
Assert(gMemoryProfilerStats != NULL);
delete gMemoryProfilerStats;
gMemoryProfilerStats = NULL;
}
#endif
|