68507a2b74f485986342366e3912052fbfb49c9f
[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  # load counter                                                  \n" \
54                                 "               .set    mips0                                                                                   \n" \
55                                 "               addu    %0, 1   # increment                                                             \n" \
56                                 "               .set    mips3                                                                                   \n" \
57                                 "               sc              %0, %1  # try to store, checking for atomicity  \n" \
58                                 "               .set    mips0                                                                                   \n" \
59                                 "               beqz    %0, 1b  # if not atomic (0), try again                  \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   # decrement     \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         #elif defined(__ppc__)
83                 #define DECLARE_REF(x)                  \
84                         private: oRefCount ref;         \
85                         public: void AddRef();          \
86                                         void Release();
87                 #define DEFINE_REF(c) \
88                         void c::AddRef() \
89                         { \
90                                 int temp; \
91                                 __asm__ __volatile__( \
92                                 "1:             lwarx   %0, 0, %3       \n" \
93                                 "               add             %0, %2, %0      \n" \
94                                 "               dcbt    0, %3           \n" \
95                                 "               stwcx.  %0, 0, %3       \n" \
96                                 "               bne-    1b                      \n" \
97                                 : "=&r" (temp), "=m" ((int)ref) \
98                                 : "r" (1), "r" (&((int)ref)), "m" ((int)ref) \
99                                 : "cc"); \
100                         } \
101                         void c::Release() \
102                         { \
103                                 int temp; \
104                                 __asm__ __volatile__( \
105                                 "1:             lwarx   %0, 0, %3       \n" \
106                                 "               subf    %0, %2, %0      \n" \
107                                 "               dcbt    0, %3           \n" \
108                                 "               stwcx.  %0, 0, %3       \n" \
109                                 "               bne-    1b                      \n" \
110                                 : "=&r" (temp), "=m" ((int)ref) \
111                                 : "r" (1), "r" (&((int)ref)), "m" ((int)ref) \
112                                 : "cc"); \
113                                 if (!ref) \
114                                         delete this; \
115                         }
116         #else
117                 #define DECLARE_REF(x)                  \
118                         private:oRefCount ref;  \
119                                         eSingleLock ref_lock; \
120                         public: void AddRef();          \
121                                         void Release();
122                 #ifdef OBJECT_DEBUG
123                         extern int object_total_remaining;
124                         #define DEFINE_REF(c) \
125                                 void c::AddRef() \
126                                 { \
127                                         eSingleLocker l(ref_lock); \
128                                         ++object_total_remaining; \
129                                         ++ref; \
130                                         eDebug("OBJECT_DEBUG " #c "+%p now %d", this, (int)ref); \
131                                 } \
132                                 void c::Release() \
133                                 { \
134                                         { \
135                                                 eSingleLocker l(ref_lock); \
136                                                 --object_total_remaining; \
137                                                 --ref; \
138                                                 eDebug("OBJECT_DEBUG " #c "-%p now %d", this, ref); \
139                                         } \
140                                         if (!ref) \
141                                                 delete this; \
142                                 }
143                                 #error fix locking for debug
144                 #else
145                         #define DEFINE_REF(c) \
146                                 void c::AddRef() \
147                                 { \
148                                         eSingleLocker l(ref_lock); \
149                                         ++ref; \
150                                 } \
151                                 void c::Release() \
152                                 { \
153                                         { \
154                                                 eSingleLocker l(ref_lock); \
155                                                 --ref; \
156                                         } \
157                                         if (!ref) \
158                                                 delete this; \
159                                 }
160                 #endif
161         #endif
162 #else
163         #define DECLARE_REF(x) \
164                 private: \
165                         void AddRef(); \
166                         void Release();
167 #endif
168
169 #ifdef SWIG
170 class Object
171 {
172 };
173 #endif
174
175 #endif