optimized refcounting for MIPS (use asm optimized atomic inc/dec)
[enigma2.git] / lib / base / object.h
1 #ifndef __base_object_h
2 #define __base_object_h
3
4 #include <assert.h>
5
6 // #define OBJECT_DEBUG
7
8 #include <lib/base/smartptr.h>
9 #ifdef OBJECT_DEBUG
10 #include <lib/base/eerror.h>
11 #endif
12 #include <lib/base/elock.h>
13
14 typedef int RESULT;
15
16 class iObject
17 {
18 private:
19                 /* we don't allow the default operator here, as it would break the refcount. */
20         void operator=(const iObject &);
21 protected:
22         virtual ~iObject() { }
23 public:
24         virtual void AddRef()=0;
25         virtual void Release()=0;
26 };
27
28 class oRefCount
29 {
30         int ref;
31 public:
32         oRefCount(): ref(0) { }
33         operator int&() { return ref; }
34         ~oRefCount() { 
35 #ifdef OBJECT_DEBUG
36                 if (ref) eDebug("OBJECT_DEBUG FATAL: %p has %d references!", this, ref); else eDebug("OBJECT_DEBUG refcount ok! (%p)", this); 
37 #endif
38         }
39 };
40
41 #ifndef SWIG
42         #if defined(__mips__)
43                 #define DECLARE_REF(x)                  \
44                         private: oRefCount ref;         \
45                         public: void AddRef();          \
46                                         void Release();
47                 #define DEFINE_REF(c) \
48                         void c::AddRef() \
49                         { \
50                                 unsigned long temp; \
51                                 __asm__ __volatile__( \
52                                 "               .set    mips3           \n" \
53                                 "1:             ll              %0, %1          \n" \
54                                 "               .set    mips0           \n" \
55                                 "               addu    %0, 1           \n" \
56                                 "               .set    mips3           \n" \
57                                 "               sc              %0, %1          \n" \
58                                 "               .set    mips0           \n" \
59                                 "               beqz    %0, 1b          \n" \
60                                 : "=&r" (temp), "=m" ((int)ref) \
61                                 : "m" ((int)ref) \
62                                 : "memory"); \
63                         } \
64                         void c::Release() \
65                         { \
66                                 unsigned long temp; \
67                                 __asm__ __volatile__( \
68                                 "               .set    mips3           \n" \
69                                 "1:             ll              %0, %1          \n" \
70                                 "               .set    mips0           \n" \
71                                 "               subu    %0, 1           \n" \
72                                 "               .set    mips3           \n" \
73                                 "               sc              %0, %1          \n" \
74                                 "               .set    mips0           \n" \
75                                 "               beqz    %0, 1b          \n" \
76                                 : "=&r" (temp), "=m" ((int)ref) \
77                                 : "m" ((int)ref) \
78                                 : "memory"); \
79                                 if (!ref) \
80                                         delete this; \
81                         }
82         #else
83                 #define DECLARE_REF(x)                  \
84                         private:oRefCount ref;  \
85                                         eSingleLock ref_lock; \
86                         public: void AddRef();          \
87                                         void Release();
88                 #ifdef OBJECT_DEBUG
89                         extern int object_total_remaining;
90                         #define DEFINE_REF(c) \
91                                 void c::AddRef() \
92                                 { \
93                                         eSingleLocker l(ref_lock); \
94                                         ++object_total_remaining; \
95                                         ++ref; \
96                                         eDebug("OBJECT_DEBUG " #c "+%p now %d", this, (int)ref); \
97                                 } \
98                                 void c::Release() \
99                                 { \
100                                         { \
101                                                 eSingleLocker l(ref_lock); \
102                                                 --object_total_remaining; \
103                                                 --ref; \
104                                                 eDebug("OBJECT_DEBUG " #c "-%p now %d", this, ref); \
105                                         } \
106                                         if (!ref) \
107                                                 delete this; \
108                                 }
109                                 #error fix locking for debug
110                 #else
111                         #define DEFINE_REF(c) \
112                                 void c::AddRef() \
113                                 { \
114                                         eSingleLocker l(ref_lock); \
115                                         ++ref; \
116                                 } \
117                                 void c::Release() \
118                                 { \
119                                         { \
120                                                 eSingleLocker l(ref_lock); \
121                                                 --ref; \
122                                         } \
123                                         if (!ref) \
124                                                 delete this; \
125                                 }
126                 #endif
127         #endif
128 #else
129         #define DECLARE_REF(x) \
130                 private: \
131                         void AddRef(); \
132                         void Release();
133 #endif
134
135 #ifdef SWIG
136 class Object
137 {
138 };
139 #endif
140
141 #endif