OSDN Git Service

recording
[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
29     _retainCount = 1;
30
31 #ifdef RAYM_MEMORY_CHECK
32     global_lock_.lock();
33     ++global_raym_count_;
34 //    DebugLog0("C:0x%08x:%d", this, global_raym_count_);
35     global_lock_.unlock();
36 #endif
37 }
38
39 Object::~Object()
40 {
41     RAYM_LOCK_DESTROY;
42
43 #ifdef RAYM_MEMORY_CHECK
44     global_lock_.lock();
45 //    DebugLog0("D:0x%08x:%d", this, global_raym_count_);
46     --global_raym_count_;
47     global_lock_.unlock();
48 #endif
49
50     DebugLog2("Object::~Object()");
51 }
52
53 Object *Object::alloc()
54 {
55     DebugLog2("Object::alloc()");
56
57     return new Object();
58 }
59
60 Object *Object::init()
61 {
62     DebugLog2("Object::init()");
63
64 #ifdef RAYM_MEMORY_CHECK
65     global_lock_.lock();
66     ++global_raym_init_count_;
67     global_lock_.unlock();
68 #endif
69
70     return this;
71 }
72
73 Object *Object::retain()
74 {
75     DebugLog2("Object::retain()");
76
77     RaymLock(this);
78     ++_retainCount;
79     RaymUnlock(this);
80
81 #ifdef RAYM_MEMORY_CHECK
82     global_lock_.lock();
83     ++global_raym_retain_count_;
84     global_lock_.unlock();
85 #endif
86
87     return this;
88 }
89
90 Object *Object::autorelease()
91 {
92     DebugLog2("Object::autorelease()");
93
94 #ifdef RAYM_MEMORY_CHECK
95     global_lock_.lock();
96     ++global_raym_autorelease_count_;
97     global_lock_.unlock();
98 #endif
99
100     AutoreleasePool::addObject(this);
101     return this;
102 }
103
104 Object *Object::autorelease(bool rootPool)
105 {
106     DebugLog2("Object::autorelease()");
107
108 #ifdef RAYM_MEMORY_CHECK
109     global_lock_.lock();
110     ++global_raym_autorelease_count_;
111     global_lock_.unlock();
112 #endif
113
114     AutoreleasePool::addObject(this, rootPool);
115     return this;
116 }
117
118 void Object::release()
119 {
120     DebugLog2("Object::release()");
121
122 #ifdef RAYM_MEMORY_CHECK
123     global_lock_.lock();
124     ++global_raym_release_count_;
125     global_lock_.unlock();
126 #endif
127
128     RaymLock(this);
129     if (_retainCount > 0)
130     {
131         --_retainCount;
132         if (_retainCount == 0)
133         {
134             RaymUnlock(this);
135             delete this;
136             return;
137         }
138     }
139     else
140     {
141         DebugLog0("object is already released. (0x%016lx)", this);
142         abort();
143     }
144     RaymUnlock(this);
145 }
146
147 String *Object::description()
148 {
149     return String::stringWithFormat("<%s:0x%016lx>", className(), this);
150 }
151
152 const char *Object::className()
153 {
154     return "Object";
155 }
156
157
158 } // Raym