add optimized addref / release for x86
[enigma2.git] / lib / base / object.h
index adb5d67cbb0304a8bb85b3d0efc81fe9c14eec6d..8ac92b83b0a0ab91e295fe2c7cb07113eb97f41c 100644 (file)
@@ -2,14 +2,12 @@
 #define __base_object_h
 
 #include <assert.h>
+#include <lib/base/smartptr.h>
+#include <lib/base/elock.h>
 
-// #define OBJECT_DEBUG
+//#define OBJECT_DEBUG
 
-#include <lib/base/smartptr.h>
-#ifdef OBJECT_DEBUG
 #include <lib/base/eerror.h>
-#endif
-#include <lib/base/elock.h>
 
 typedef int RESULT;
 
@@ -25,20 +23,50 @@ public:
        virtual void Release()=0;
 };
 
-struct oRefCount
-{
-       volatile int count;
-       oRefCount(): count(0) { }
-       operator volatile int&() { return count; }
-       ~oRefCount() { 
-#ifdef OBJECT_DEBUG
-               if (count) eDebug("OBJECT_DEBUG FATAL: %p has %d references!", this, ref); else eDebug("OBJECT_DEBUG refcount ok! (%p)", this); 
-#endif
-       }
-};
-
 #ifndef SWIG
-       #if defined(__mips__)
+       struct oRefCount
+       {
+               volatile int count;
+               oRefCount(): count(0) { }
+               operator volatile int&() { return count; }
+               ~oRefCount()
+               { 
+       #ifdef OBJECT_DEBUG
+                       if (count)
+                               eDebug("OBJECT_DEBUG FATAL: %p has %d references!", this, count);
+                       else
+                               eDebug("OBJECT_DEBUG refcount ok! (%p)", this); 
+       #endif
+               }
+       };
+
+       #if defined(OBJECT_DEBUG)
+               extern int object_total_remaining;
+               #define DECLARE_REF(x)                  \
+                       private:oRefCount ref;  \
+                                       eSingleLock ref_lock; \
+                       public: void AddRef();          \
+                                       void Release();
+               #define DEFINE_REF(c) \
+                       void c::AddRef() \
+                       { \
+                               eSingleLocker l(ref_lock); \
+                               ++object_total_remaining; \
+                               ++ref; \
+                               eDebug("OBJECT_DEBUG " #c "+%p now %d", this, (int)ref); \
+                       } \
+                       void c::Release() \
+                       { \
+                               { \
+                                       eSingleLocker l(ref_lock); \
+                                       --object_total_remaining; \
+                                       --ref; \
+                                       eDebug("OBJECT_DEBUG " #c "-%p now %d", this, (int)ref); \
+                               } \
+                               if (!ref) \
+                                       delete this; \
+                       }
+       #elif defined(__mips__)
                #define DECLARE_REF(x)                  \
                        private: oRefCount ref;         \
                        public: void AddRef();          \
@@ -78,7 +106,7 @@ struct oRefCount
                                if (!ref) \
                                        delete this; \
                        }
-       #elif defined(__ppc__)
+       #elif defined(__ppc__) || defined(__powerpc__)
                #define DECLARE_REF(x)                  \
                        private: oRefCount ref;         \
                        public: void AddRef();          \
@@ -112,63 +140,59 @@ struct oRefCount
                                if (!ref) \
                                        delete this; \
                        }
+       #elif defined(__i386__)
+               #define DECLARE_REF(x)                  \
+                       private: oRefCount ref;         \
+                       public: void AddRef();          \
+                                       void Release();
+               #define DEFINE_REF(c) \
+                       void c::AddRef() \
+                       { \
+                               __asm__ __volatile__( \
+                               "               incl    %0      \n" \
+                               : "=m" (ref.count) \
+                               : "m" (ref.count); \
+                       } \
+                       void c::Release() \
+                       { \
+                               __asm__ __volatile__( \
+                               "               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();
-               #ifdef OBJECT_DEBUG
-                       extern int object_total_remaining;
-                       #define DEFINE_REF(c) \
-                               void c::AddRef() \
-                               { \
-                                       eSingleLocker l(ref_lock); \
-                                       ++object_total_remaining; \
-                                       ++ref; \
-                                       eDebug("OBJECT_DEBUG " #c "+%p now %d", this, ref.count); \
-                               } \
-                               void c::Release() \
-                               { \
-                                       { \
-                                               eSingleLocker l(ref_lock); \
-                                               --object_total_remaining; \
-                                               --ref; \
-                                               eDebug("OBJECT_DEBUG " #c "-%p now %d", this, ref); \
-                                       } \
-                                       if (!ref) \
-                                               delete this; \
-                               }
-                               #error fix locking for debug
-               #else
-                       #define DEFINE_REF(c) \
-                               void c::AddRef() \
+               #define DEFINE_REF(c) \
+                       void c::AddRef() \
+                       { \
+                               eSingleLocker l(ref_lock); \
+                               ++ref; \
+                       } \
+                       void c::Release() \
+                       { \
                                { \
                                        eSingleLocker l(ref_lock); \
-                                       ++ref; \
+                                       --ref; \
                                } \
-                               void c::Release() \
-                               { \
-                                       { \
-                                               eSingleLocker l(ref_lock); \
-                                               --ref; \
-                                       } \
-                                       if (!ref) \
-                                               delete this; \
-                               }
-               #endif
+                               if (!ref) \
+                                       delete this; \
+                       }
        #endif
-#else
+#else  // SWIG
        #define DECLARE_REF(x) \
                private: \
                        void AddRef(); \
                        void Release();
-#endif
-
-#ifdef SWIG
-class Object
-{
-};
-#endif
+       class Object
+       {
+       };
+#endif  // SWIG
 
-#endif
+#endif  // __base_object_h