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 void operator delete(void *p) { ::operator delete(p); }
21 virtual ~iObject() { }
23 virtual void AddRef()=0;
24 virtual void Release()=0;
28 virtual void AddRef()=0;
29 virtual void Release()=0;
37 oRefCount(): count(0) { }
38 operator volatile int&() { return count; }
43 eDebug("OBJECT_DEBUG FATAL: %p has %d references!", this, count);
45 eDebug("OBJECT_DEBUG refcount ok! (%p)", this);
50 #if defined(OBJECT_DEBUG)
51 extern int object_total_remaining;
52 #define DECLARE_REF(x) \
53 public: void AddRef(); \
55 private:oRefCount ref; \
57 #define DEFINE_REF(c) \
60 eSingleLocker l(ref_lock); \
61 ++object_total_remaining; \
63 eDebug("OBJECT_DEBUG " #c "+%p now %d", this, (int)ref); \
68 eSingleLocker l(ref_lock); \
69 --object_total_remaining; \
71 eDebug("OBJECT_DEBUG " #c "-%p now %d", this, (int)ref); \
76 #elif defined(__mips__)
77 #define DECLARE_REF(x) \
78 public: void AddRef(); \
80 private: oRefCount ref;
81 #define DEFINE_REF(c) \
85 __asm__ __volatile__( \
87 "1: ll %0, %1 # load counter \n" \
89 " addu %0, 1 # increment \n" \
91 " sc %0, %1 # try to store, checking for atomicity \n" \
93 " beqz %0, 1b # if not atomic (0), try again \n" \
94 : "=&r" (temp), "=m" (ref.count) \
100 unsigned long temp; \
101 __asm__ __volatile__( \
105 " subu %0, 1 # decrement \n" \
110 : "=&r" (temp), "=m" (ref.count) \
116 #elif defined(__ppc__) || defined(__powerpc__)
117 #define DECLARE_REF(x) \
118 public: void AddRef(); \
120 private: oRefCount ref;
121 #define DEFINE_REF(c) \
125 __asm__ __volatile__( \
126 "1: lwarx %0, 0, %3 \n" \
127 " add %0, %2, %0 \n" \
128 " dcbt 0, %3 # workaround for PPC405CR Errata\n" \
129 " stwcx. %0, 0, %3 \n" \
131 : "=&r" (temp), "=m" (ref.count) \
132 : "r" (1), "r" (&ref.count), "m" (ref.count) \
138 __asm__ __volatile__( \
139 "1: lwarx %0, 0, %3 \n" \
140 " subf %0, %2, %0 \n" \
141 " dcbt 0, %3 # workaround for PPC405CR Errata\n" \
142 " stwcx. %0, 0, %3 \n" \
144 : "=&r" (temp), "=m" (ref.count) \
145 : "r" (1), "r" (&ref.count), "m" (ref.count) \
150 #elif defined(__i386__) || defined(__x86_64__)
151 #define DECLARE_REF(x) \
152 public: void AddRef(); \
154 private: oRefCount ref;
155 #define DEFINE_REF(c) \
158 __asm__ __volatile__( \
159 " lock ; incl %0 \n" \
161 : "m" (ref.count)); \
165 __asm__ __volatile__( \
166 " lock ; decl %0 \n" \
168 : "m" (ref.count)); \
173 #warning use non optimized implementation of refcounting.
174 #define DECLARE_REF(x) \
175 public: void AddRef(); \
177 private:oRefCount ref; \
178 eSingleLock ref_lock;
179 #define DEFINE_REF(c) \
182 eSingleLocker l(ref_lock); \
188 eSingleLocker l(ref_lock); \
196 #define DECLARE_REF(x) \
202 #endif // __base_object_h