1 #ifndef __base_object_h
2 #define __base_object_h
5 #include <lib/base/smartptr.h>
6 #include <lib/base/elock.h>
10 #include <lib/base/eerror.h>
17 /* we don't allow the default operator here, as it would break the refcount. */
18 void operator=(const iObject &);
20 virtual ~iObject() { }
22 virtual void AddRef()=0;
23 virtual void Release()=0;
27 virtual void AddRef()=0;
28 virtual void Release()=0;
36 oRefCount(): count(0) { }
37 operator volatile int&() { return count; }
42 eDebug("OBJECT_DEBUG FATAL: %p has %d references!", this, count);
44 eDebug("OBJECT_DEBUG refcount ok! (%p)", this);
49 #if defined(OBJECT_DEBUG)
50 extern int object_total_remaining;
51 #define DECLARE_REF(x) \
52 public: void AddRef(); \
54 private:oRefCount ref; \
56 #define DEFINE_REF(c) \
59 eSingleLocker l(ref_lock); \
60 ++object_total_remaining; \
62 eDebug("OBJECT_DEBUG " #c "+%p now %d", this, (int)ref); \
67 eSingleLocker l(ref_lock); \
68 --object_total_remaining; \
70 eDebug("OBJECT_DEBUG " #c "-%p now %d", this, (int)ref); \
75 #elif defined(__mips__)
76 #define DECLARE_REF(x) \
77 public: void AddRef(); \
79 private: oRefCount ref;
80 #define DEFINE_REF(c) \
84 __asm__ __volatile__( \
86 "1: ll %0, %1 # load counter \n" \
88 " addu %0, 1 # increment \n" \
90 " sc %0, %1 # try to store, checking for atomicity \n" \
92 " beqz %0, 1b # if not atomic (0), try again \n" \
93 : "=&r" (temp), "=m" (ref.count) \
100 __asm__ __volatile__( \
104 " subu %0, 1 # decrement \n" \
109 : "=&r" (temp), "=m" (ref.count) \
115 #elif defined(__ppc__) || defined(__powerpc__)
116 #define DECLARE_REF(x) \
117 public: void AddRef(); \
119 private: oRefCount ref;
120 #define DEFINE_REF(c) \
124 __asm__ __volatile__( \
125 "1: lwarx %0, 0, %3 \n" \
126 " add %0, %2, %0 \n" \
127 " dcbt 0, %3 # workaround for PPC405CR Errata\n" \
128 " stwcx. %0, 0, %3 \n" \
130 : "=&r" (temp), "=m" (ref.count) \
131 : "r" (1), "r" (&ref.count), "m" (ref.count) \
137 __asm__ __volatile__( \
138 "1: lwarx %0, 0, %3 \n" \
139 " subf %0, %2, %0 \n" \
140 " dcbt 0, %3 # workaround for PPC405CR Errata\n" \
141 " stwcx. %0, 0, %3 \n" \
143 : "=&r" (temp), "=m" (ref.count) \
144 : "r" (1), "r" (&ref.count), "m" (ref.count) \
149 #elif defined(__i386__) || defined(__x86_64__)
150 #define DECLARE_REF(x) \
151 public: void AddRef(); \
153 private: oRefCount ref;
154 #define DEFINE_REF(c) \
157 __asm__ __volatile__( \
158 " lock ; incl %0 \n" \
160 : "m" (ref.count)); \
164 __asm__ __volatile__( \
165 " lock ; decl %0 \n" \
167 : "m" (ref.count)); \
172 #warning use non optimized implementation of refcounting.
173 #define DECLARE_REF(x) \
174 public: void AddRef(); \
176 private:oRefCount ref; \
177 eSingleLock ref_lock;
178 #define DEFINE_REF(c) \
181 eSingleLocker l(ref_lock); \
187 eSingleLocker l(ref_lock); \
195 #define DECLARE_REF(x) \
201 #endif // __base_object_h