1 #ifndef __base_object_h
2 #define __base_object_h
5 #include <lib/base/smartptr.h>
6 #include <lib/base/elock.h>
11 #include <lib/base/eerror.h>
19 /* we don't allow the default operator here, as it would break the refcount. */
20 void operator=(const iObject &);
22 virtual ~iObject() { }
24 virtual void AddRef()=0;
25 virtual void Release()=0;
32 oRefCount(): count(0) { }
33 operator volatile int&() { return count; }
38 eDebug("OBJECT_DEBUG FATAL: %p has %d references!", this, count);
40 eDebug("OBJECT_DEBUG refcount ok! (%p)", this);
45 #if defined(OBJECT_DEBUG)
46 extern int object_total_remaining;
47 #define DECLARE_REF(x) \
48 private:oRefCount ref; \
49 eSingleLock ref_lock; \
50 public: void AddRef(); \
52 #define DEFINE_REF(c) \
55 eSingleLocker l(ref_lock); \
56 ++object_total_remaining; \
58 eDebug("OBJECT_DEBUG " #c "+%p now %d", this, (int)ref); \
63 eSingleLocker l(ref_lock); \
64 --object_total_remaining; \
66 eDebug("OBJECT_DEBUG " #c "-%p now %d", this, (int)ref); \
71 #elif defined(__mips__)
72 #define DECLARE_REF(x) \
73 private: oRefCount ref; \
74 public: void AddRef(); \
76 #define DEFINE_REF(c) \
80 __asm__ __volatile__( \
82 "1: ll %0, %1 # load counter \n" \
84 " addu %0, 1 # increment \n" \
86 " sc %0, %1 # try to store, checking for atomicity \n" \
88 " beqz %0, 1b # if not atomic (0), try again \n" \
89 : "=&r" (temp), "=m" (ref.count) \
96 __asm__ __volatile__( \
100 " subu %0, 1 # decrement \n" \
105 : "=&r" (temp), "=m" (ref.count) \
111 #elif defined(__ppc__)
112 #define DECLARE_REF(x) \
113 private: oRefCount ref; \
114 public: void AddRef(); \
116 #define DEFINE_REF(c) \
120 __asm__ __volatile__( \
121 "1: lwarx %0, 0, %3 \n" \
122 " add %0, %2, %0 \n" \
123 " dcbt 0, %3 # workaround for PPC405CR Errata\n" \
124 " stwcx. %0, 0, %3 \n" \
126 : "=&r" (temp), "=m" (ref.count) \
127 : "r" (1), "r" (&ref.count), "m" (ref.count) \
133 __asm__ __volatile__( \
134 "1: lwarx %0, 0, %3 \n" \
135 " subf %0, %2, %0 \n" \
136 " dcbt 0, %3 # workaround for PPC405CR Errata\n" \
137 " stwcx. %0, 0, %3 \n" \
139 : "=&r" (temp), "=m" (ref.count) \
140 : "r" (1), "r" (&ref.count), "m" (ref.count) \
146 #warning use non optimized implementation of refcounting.
147 #define DECLARE_REF(x) \
148 private:oRefCount ref; \
149 eSingleLock ref_lock; \
150 public: void AddRef(); \
152 #define DEFINE_REF(c) \
155 eSingleLocker l(ref_lock); \
161 eSingleLocker l(ref_lock); \
169 #define DECLARE_REF(x) \
178 #endif // __base_object_h