OSDN Git Service

EPG解析処理再考中
[iptd/iPTd.git] / src / Raym / Object.cpp
1 //
2 // Object.cpp
3 //
4
5 #define DBG_LEVEL 0
6 #include <Raym/Log.h>
7 #include <Raym/Object.h>
8 #include <Raym/String.h>
9 #include <Raym/AutoreleasePool.h>
10
11 namespace Raym
12 {
13
14 #ifdef RAYM_MEMORY_CHECK
15 int global_raym_count_ = 0;
16 int global_raym_init_count_ = 0;
17 int global_raym_retain_count_ = 0;
18 int global_raym_autorelease_count_ = 0;
19 int global_raym_release_count_ = 0;
20 DEFINE_STATIC_MUTEX(global_lock_);
21 #endif
22
23 Object::Object()
24 {
25     DebugLog2("Object::Object()");
26
27     RAYM_LOCK_CREATE;
28     RAYM_COND_CREATE;
29
30     _retainCount = 1;
31
32 #ifdef RAYM_MEMORY_CHECK
33     global_lock_.lock();
34     ++global_raym_count_;
35 //    DebugLog0("C:0x%08x:%d", this, global_raym_count_);
36     global_lock_.unlock();
37 #endif
38 }
39
40 Object::~Object()
41 {
42     RAYM_LOCK_DESTROY;
43
44 #ifdef RAYM_MEMORY_CHECK
45     global_lock_.lock();
46 //    DebugLog0("D:0x%08x:%d", this, global_raym_count_);
47     --global_raym_count_;
48     global_lock_.unlock();
49 #endif
50
51     DebugLog2("Object::~Object()");
52 }
53
54 Object *Object::alloc()
55 {
56     DebugLog2("Object::alloc()");
57
58     return new Object();
59 }
60
61 Object *Object::init()
62 {
63     DebugLog2("Object::init()");
64
65 #ifdef RAYM_MEMORY_CHECK
66     global_lock_.lock();
67     ++global_raym_init_count_;
68     global_lock_.unlock();
69 #endif
70
71     return this;
72 }
73
74 Object *Object::retain()
75 {
76     DebugLog2("Object::retain()");
77
78     RaymLock(this);
79     ++_retainCount;
80     RaymUnlock(this);
81
82 #ifdef RAYM_MEMORY_CHECK
83     global_lock_.lock();
84     ++global_raym_retain_count_;
85     global_lock_.unlock();
86 #endif
87
88     return this;
89 }
90
91 Object *Object::autorelease()
92 {
93     DebugLog2("Object::autorelease()");
94
95 #ifdef RAYM_MEMORY_CHECK
96     global_lock_.lock();
97     ++global_raym_autorelease_count_;
98     global_lock_.unlock();
99 #endif
100
101     AutoreleasePool::addObject(this);
102     return this;
103 }
104
105 Object *Object::autorelease(bool rootPool)
106 {
107     DebugLog2("Object::autorelease()");
108
109 #ifdef RAYM_MEMORY_CHECK
110     global_lock_.lock();
111     ++global_raym_autorelease_count_;
112     global_lock_.unlock();
113 #endif
114
115     AutoreleasePool::addObject(this, rootPool);
116     return this;
117 }
118
119 void Object::release()
120 {
121     DebugLog2("Object::release()");
122
123 #ifdef RAYM_MEMORY_CHECK
124     global_lock_.lock();
125     ++global_raym_release_count_;
126     global_lock_.unlock();
127 #endif
128
129     RaymLock(this);
130     if (_retainCount > 0)
131     {
132         --_retainCount;
133         if (_retainCount == 0)
134         {
135             RaymUnlock(this);
136             delete this;
137             return;
138         }
139     }
140     else
141     {
142         DebugLog0("object is already released. (0x%016lx)", this);
143         abort();
144     }
145     RaymUnlock(this);
146 }
147
148 String *Object::description()
149 {
150     return String::stringWithFormat("<%s:0x%016lx>", className(), this);
151 }
152
153 const char *Object::className()
154 {
155     return "Object";
156 }
157
158
159 } // Raym