some more work on plugin manager
[enigma2.git] / lib / base / object.h
index d0e2a65e54916e5a336b0b3e820517209ace5206..a3268d4f3851de9357ba3d749891b4829e87b95a 100644 (file)
@@ -7,9 +7,7 @@
 
 //#define OBJECT_DEBUG
 
-#ifdef OBJECT_DEBUG
-       #include <lib/base/eerror.h>
-#endif
+#include <lib/base/eerror.h>
 
 typedef int RESULT;
 
@@ -19,10 +17,17 @@ private:
                /* we don't allow the default operator here, as it would break the refcount. */
        void operator=(const iObject &);
 protected:
+       void operator delete(void *p) { ::operator delete(p); }
        virtual ~iObject() { }
+#ifdef SWIG
+       virtual void AddRef()=0;
+       virtual void Release()=0;
+#endif
 public:
+#ifndef SWIG
        virtual void AddRef()=0;
        virtual void Release()=0;
+#endif
 };
 
 #ifndef SWIG
@@ -31,9 +36,13 @@ public:
                volatile int count;
                oRefCount(): count(0) { }
                operator volatile int&() { return count; }
-               ~oRefCount() { 
+               ~oRefCount()
+               { 
        #ifdef OBJECT_DEBUG
-                       if (count) eDebug("OBJECT_DEBUG FATAL: %p has %d references!", this, count); else eDebug("OBJECT_DEBUG refcount ok! (%p)", this); 
+                       if (count)
+                               eDebug("OBJECT_DEBUG FATAL: %p has %d references!", this, count);
+                       else
+                               eDebug("OBJECT_DEBUG refcount ok! (%p)", this); 
        #endif
                }
        };
@@ -41,10 +50,10 @@ public:
        #if defined(OBJECT_DEBUG)
                extern int object_total_remaining;
                #define DECLARE_REF(x)                  \
-                       private:oRefCount ref;  \
-                                       eSingleLock ref_lock; \
                        public: void AddRef();          \
-                                       void Release();
+                                       void Release();         \
+                       private:oRefCount ref;          \
+                                       eSingleLock ref_lock;
                #define DEFINE_REF(c) \
                        void c::AddRef() \
                        { \
@@ -66,9 +75,9 @@ public:
                        }
        #elif defined(__mips__)
                #define DECLARE_REF(x)                  \
-                       private: oRefCount ref;         \
                        public: void AddRef();          \
-                                       void Release();
+                                       void Release();         \
+                       private: oRefCount ref; 
                #define DEFINE_REF(c) \
                        void c::AddRef() \
                        { \
@@ -104,11 +113,11 @@ public:
                                if (!ref) \
                                        delete this; \
                        }
-       #elif defined(__ppc__)
+       #elif defined(__ppc__) || defined(__powerpc__)
                #define DECLARE_REF(x)                  \
-                       private: oRefCount ref;         \
                        public: void AddRef();          \
-                                       void Release();
+                                       void Release();         \
+                       private: oRefCount ref;
                #define DEFINE_REF(c) \
                        void c::AddRef() \
                        { \
@@ -138,12 +147,35 @@ public:
                                if (!ref) \
                                        delete this; \
                        }
+       #elif defined(__i386__) || defined(__x86_64__)
+               #define DECLARE_REF(x)                  \
+                       public: void AddRef();          \
+                                       void Release();         \
+                       private: oRefCount ref;
+               #define DEFINE_REF(c) \
+                       void c::AddRef() \
+                       { \
+                               __asm__ __volatile__( \
+                               "               lock ; incl     %0      \n" \
+                               : "=m" (ref.count) \
+                               : "m" (ref.count)); \
+                       } \
+                       void c::Release() \
+                       { \
+                               __asm__ __volatile__( \
+                               "               lock ; decl     %0      \n" \
+                               : "=m" (ref.count) \
+                               : "m" (ref.count)); \
+                               if (!ref) \
+                                       delete this; \
+                       }
        #else
+               #warning use non optimized implementation of refcounting.
                #define DECLARE_REF(x)                  \
-                       private:oRefCount ref;  \
-                                       eSingleLock ref_lock; \
                        public: void AddRef();          \
-                                       void Release();
+                                       void Release();         \
+                       private:oRefCount ref;  \
+                                       eSingleLock ref_lock;
                #define DEFINE_REF(c) \
                        void c::AddRef() \
                        { \
@@ -165,9 +197,6 @@ public:
                private: \
                        void AddRef(); \
                        void Release();
-       class Object
-       {
-       };
 #endif  // SWIG
 
 #endif  // __base_object_h