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
|
#ifndef __base_object_h
#define __base_object_h
#include <assert.h>
// #define OBJECT_DEBUG
#include <lib/base/smartptr.h>
#ifdef OBJECT_DEBUG
#include <lib/base/eerror.h>
#endif
#include <lib/base/elock.h>
typedef int RESULT;
class iObject
{
private:
/* we don't allow the default operator here, as it would break the refcount. */
void operator=(const iObject &);
protected:
virtual ~iObject() { }
public:
virtual void AddRef()=0;
virtual void Release()=0;
};
class oRefCount
{
int ref;
public:
oRefCount(): ref(0) { }
operator int&() { return ref; }
~oRefCount() {
#ifdef OBJECT_DEBUG
if (ref) eDebug("OBJECT_DEBUG FATAL: %p has %d references!", this, ref); else eDebug("OBJECT_DEBUG refcount ok! (%p)", this);
#endif
}
};
#ifndef SWIG
#if defined(__mips__)
#define DECLARE_REF(x) \
private: oRefCount ref; \
public: void AddRef(); \
void Release();
#define DEFINE_REF(c) \
void c::AddRef() \
{ \
unsigned long temp; \
__asm__ __volatile__( \
" .set mips3 \n" \
"1: ll %0, %1 # load counter \n" \
" .set mips0 \n" \
" addu %0, 1 # increment \n" \
" .set mips3 \n" \
" sc %0, %1 # try to store, checking for atomicity \n" \
" .set mips0 \n" \
" beqz %0, 1b # if not atomic (0), try again \n" \
" nop # branch-delay slot \n" \
: "=&r" (temp), "=m" ((int)ref) \
: "m" ((int)ref) \
: "memory"); \
} \
void c::Release() \
{ \
unsigned long temp; \
__asm__ __volatile__( \
" .set mips3 \n" \
"1: ll %0, %1 \n" \
" .set mips0 \n" \
" subu %0, 1 # decrement \n" \
" .set mips3 \n" \
" sc %0, %1 \n" \
" .set mips0 \n" \
" beqz %0, 1b \n" \
" nop \n" \
: "=&r" (temp), "=m" ((int)ref) \
: "m" ((int)ref) \
: "memory"); \
if (!ref) \
delete this; \
}
#elif defined(__ppc__)
#define DECLARE_REF(x) \
private: oRefCount ref; \
public: void AddRef(); \
void Release();
#define DEFINE_REF(c) \
void c::AddRef() \
{ \
int temp; \
__asm__ __volatile__( \
"1: lwarx %0, 0, %3 \n" \
" add %0, %2, %0 \n" \
" dcbt 0, %3 \n" \
" stwcx. %0, 0, %3 \n" \
" bne- 1b \n" \
: "=&r" (temp), "=m" ((int)ref) \
: "r" (1), "r" (&((int)ref)), "m" ((int)ref) \
: "cc"); \
} \
void c::Release() \
{ \
int temp; \
__asm__ __volatile__( \
"1: lwarx %0, 0, %3 \n" \
" subf %0, %2, %0 \n" \
" dcbt 0, %3 \n" \
" stwcx. %0, 0, %3 \n" \
" bne- 1b \n" \
: "=&r" (temp), "=m" ((int)ref) \
: "r" (1), "r" (&((int)ref)), "m" ((int)ref) \
: "cc"); \
if (!ref) \
delete this; \
}
#else
#define DECLARE_REF(x) \
private:oRefCount ref; \
eSingleLock ref_lock; \
public: void AddRef(); \
void Release();
#ifdef OBJECT_DEBUG
extern int object_total_remaining;
#define DEFINE_REF(c) \
void c::AddRef() \
{ \
eSingleLocker l(ref_lock); \
++object_total_remaining; \
++ref; \
eDebug("OBJECT_DEBUG " #c "+%p now %d", this, (int)ref); \
} \
void c::Release() \
{ \
{ \
eSingleLocker l(ref_lock); \
--object_total_remaining; \
--ref; \
eDebug("OBJECT_DEBUG " #c "-%p now %d", this, ref); \
} \
if (!ref) \
delete this; \
}
#error fix locking for debug
#else
#define DEFINE_REF(c) \
void c::AddRef() \
{ \
eSingleLocker l(ref_lock); \
++ref; \
} \
void c::Release() \
{ \
{ \
eSingleLocker l(ref_lock); \
--ref; \
} \
if (!ref) \
delete this; \
}
#endif
#endif
#else
#define DECLARE_REF(x) \
private: \
void AddRef(); \
void Release();
#endif
#ifdef SWIG
class Object
{
};
#endif
#endif
|