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;
30 oRefCount(): count(0) { }
31 operator volatile int&() { return count; }
36 eDebug("OBJECT_DEBUG FATAL: %p has %d references!", this, count);
38 eDebug("OBJECT_DEBUG refcount ok! (%p)", this);
43 #if defined(OBJECT_DEBUG)
44 extern int object_total_remaining;
45 #define DECLARE_REF(x) \
46 private:oRefCount ref; \
47 eSingleLock ref_lock; \
48 public: void AddRef(); \
50 #define DEFINE_REF(c) \
53 eSingleLocker l(ref_lock); \
54 ++object_total_remaining; \
56 eDebug("OBJECT_DEBUG " #c "+%p now %d", this, (int)ref); \
61 eSingleLocker l(ref_lock); \
62 --object_total_remaining; \
64 eDebug("OBJECT_DEBUG " #c "-%p now %d", this, (int)ref); \
69 #elif defined(__mips__)
70 #define DECLARE_REF(x) \
71 private: oRefCount ref; \
72 public: void AddRef(); \
74 #define DEFINE_REF(c) \
78 __asm__ __volatile__( \
80 "1: ll %0, %1 # load counter \n" \
82 " addu %0, 1 # increment \n" \
84 " sc %0, %1 # try to store, checking for atomicity \n" \
86 " beqz %0, 1b # if not atomic (0), try again \n" \
87 : "=&r" (temp), "=m" (ref.count) \
94 __asm__ __volatile__( \
98 " subu %0, 1 # decrement \n" \
103 : "=&r" (temp), "=m" (ref.count) \
109 #elif defined(__ppc__)
110 #define DECLARE_REF(x) \
111 private: oRefCount ref; \
112 public: void AddRef(); \
114 #define DEFINE_REF(c) \
118 __asm__ __volatile__( \
119 "1: lwarx %0, 0, %3 \n" \
120 " add %0, %2, %0 \n" \
121 " dcbt 0, %3 # workaround for PPC405CR Errata\n" \
122 " stwcx. %0, 0, %3 \n" \
124 : "=&r" (temp), "=m" (ref.count) \
125 : "r" (1), "r" (&ref.count), "m" (ref.count) \
131 __asm__ __volatile__( \
132 "1: lwarx %0, 0, %3 \n" \
133 " subf %0, %2, %0 \n" \
134 " dcbt 0, %3 # workaround for PPC405CR Errata\n" \
135 " stwcx. %0, 0, %3 \n" \
137 : "=&r" (temp), "=m" (ref.count) \
138 : "r" (1), "r" (&ref.count), "m" (ref.count) \
144 #warning use non optimized implementation of refcounting.
145 #define DECLARE_REF(x) \
146 private:oRefCount ref; \
147 eSingleLock ref_lock; \
148 public: void AddRef(); \
150 #define DEFINE_REF(c) \
153 eSingleLocker l(ref_lock); \
159 eSingleLocker l(ref_lock); \
167 #define DECLARE_REF(x) \
176 #endif // __base_object_h