1 #ifndef __base_object_h
2 #define __base_object_h
6 // #define OBJECT_DEBUG
8 #include <lib/base/smartptr.h>
10 #include <lib/base/eerror.h>
12 #include <lib/base/elock.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(): ref(0) { }
33 operator int&() { return ref; }
36 if (ref) eDebug("OBJECT_DEBUG FATAL: %p has %d references!", this, ref); else eDebug("OBJECT_DEBUG refcount ok! (%p)", this);
43 #define DECLARE_REF(x) \
44 private: oRefCount ref; \
45 public: void AddRef(); \
47 #define DEFINE_REF(c) \
51 __asm__ __volatile__( \
53 "1: ll %0, %1 # load counter \n" \
55 " addu %0, 1 # increment \n" \
57 " sc %0, %1 # try to store, checking for atomicity \n" \
59 " beqz %0, 1b # if not atomic (0), try again \n" \
60 " nop # branch-delay slot \n" \
61 : "=&r" (temp), "=m" ((int)ref) \
68 __asm__ __volatile__( \
72 " subu %0, 1 # decrement \n" \
78 : "=&r" (temp), "=m" ((int)ref) \
84 #elif defined(__ppc__)
85 #define DECLARE_REF(x) \
86 private: oRefCount ref; \
87 public: void AddRef(); \
89 #define DEFINE_REF(c) \
93 __asm__ __volatile__( \
94 "1: lwarx %0, 0, %3 \n" \
95 " add %0, %2, %0 \n" \
97 " stwcx. %0, 0, %3 \n" \
99 : "=&r" (temp), "=m" ((int)ref) \
100 : "r" (1), "r" (&((int)ref)), "m" ((int)ref) \
106 __asm__ __volatile__( \
107 "1: lwarx %0, 0, %3 \n" \
108 " subf %0, %2, %0 \n" \
110 " stwcx. %0, 0, %3 \n" \
112 : "=&r" (temp), "=m" ((int)ref) \
113 : "r" (1), "r" (&((int)ref)), "m" ((int)ref) \
119 #define DECLARE_REF(x) \
120 private:oRefCount ref; \
121 eSingleLock ref_lock; \
122 public: void AddRef(); \
125 extern int object_total_remaining;
126 #define DEFINE_REF(c) \
129 eSingleLocker l(ref_lock); \
130 ++object_total_remaining; \
132 eDebug("OBJECT_DEBUG " #c "+%p now %d", this, (int)ref); \
137 eSingleLocker l(ref_lock); \
138 --object_total_remaining; \
140 eDebug("OBJECT_DEBUG " #c "-%p now %d", this, ref); \
145 #error fix locking for debug
147 #define DEFINE_REF(c) \
150 eSingleLocker l(ref_lock); \
156 eSingleLocker l(ref_lock); \
165 #define DECLARE_REF(x) \