forked from rcedgar/syncmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjmgr.h
120 lines (98 loc) · 2.24 KB
/
objmgr.h
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
#ifndef objmgr_h
#define objmgr_h
#include "objtype.h"
#include "obj.h"
class Obj;
#define T(x) class x;
#include "objtypes.h"
const char *ObjTypeToStr(ObjType Type);
class ObjMgr
{
friend class Obj;
private:
static ObjMgr **m_OMs;
static unsigned m_ThreadCount;
private:
Obj *m_Free[OTCount];
Obj *m_Busy[OTCount];
#if DEBUG
bool m_Validate;
unsigned m_BusyCounts[OTCount];
unsigned m_GetCallCounts[OTCount];
unsigned m_FreeCallCounts[OTCount];
unsigned m_AllocCallCounts[OTCount];
#endif
private:
ObjMgr();
public:
static void Up(Obj *pObj);
static void Down(Obj *pObj);
static void LogGlobalStats();
#define T(x) \
static x *Get##x() { return (x *) StaticGetObj(OT_##x); } \
static x *Get##x(const char *FileName, unsigned LineNr) { return (x *) StaticGetObj(OT_##x, FileName, LineNr); }
#include "objtypes.h"
static ObjMgr *GetObjMgr();
static Obj *StaticGetObj(ObjType Type);
static Obj *StaticGetObj(ObjType Type, const char *FileName, unsigned LineNr);
public:
static void UpdateGlobalStats();
void ThreadUpdateGlobalStats();
Obj *ThreadGetObj(ObjType Type);
Obj *ThreadGetObj(ObjType Type, const char *FileName, unsigned LineNr);
#define T(x) \
x *Get__##x() { return (x *) ThreadGetObj(OT_##x); }
#include "objtypes.h"
void ThreadUp(Obj *pObj)
{
#if DEBUG
if (m_Validate)
Validate();
#endif
assert(pObj->m_RefCount != 0);
++pObj->m_RefCount;
#if DEBUG
if (m_Validate)
Validate();
#endif
}
void ThreadDown(Obj *pObj)
{
#if DEBUG
if (m_Validate)
Validate();
#endif
#if TRACK_OBJ_THREAD
asserta(pObj->m_OMPThreadIndex == omp_get_thread_num());
#endif
assert(pObj->m_RefCount > 0);
--pObj->m_RefCount;
if (pObj->m_RefCount == 0)
{
ObjType Type = pObj->m_Type;
#if DEBUG
assert(m_BusyCounts[Type] > 0);
--(m_BusyCounts[Type]);
#endif
FreeObj(pObj);
pObj->OnZeroRefCount();
}
#if DEBUG
if (m_Validate)
Validate();
#endif
}
#if DEBUG
void Validate() const;
#endif
Obj *AllocNew(ObjType Type);
void FreeObj(Obj *obj);
unsigned GetFreeCount(ObjType Type) const;
unsigned GetBusyCount(ObjType Type) const;
unsigned GetMaxRefCount(ObjType Type) const;
float GetTotalMem(ObjType Type) const;
#if DEBUG
void ValidateType(ObjType Type) const;
#endif
};
#endif // objmgr_h