X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/c606126c3efaec7e3fdf1283fc37dfb7140bee13..06d7275867f5737235100f50f3ad7d5ec2f526fc:/lib/base/object.h diff --git a/lib/base/object.h b/lib/base/object.h index 93989a67..a3268d4f 100644 --- a/lib/base/object.h +++ b/lib/base/object.h @@ -2,14 +2,12 @@ #define __base_object_h #include +#include +#include -// #define OBJECT_DEBUG +//#define OBJECT_DEBUG -#include -#ifdef OBJECT_DEBUG #include -#endif -#include typedef int RESULT; @@ -19,30 +17,67 @@ 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 -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 - } -}; + 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(__mips__) + #if defined(OBJECT_DEBUG) + extern int object_total_remaining; #define DECLARE_REF(x) \ - private: oRefCount ref; \ public: void AddRef(); \ - void Release(); + void Release(); \ + private:oRefCount ref; \ + eSingleLock ref_lock; + #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) \ + public: void AddRef(); \ + void Release(); \ + private: oRefCount ref; #define DEFINE_REF(c) \ void c::AddRef() \ { \ @@ -78,11 +113,11 @@ struct oRefCount 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() \ { \ @@ -112,60 +147,56 @@ struct oRefCount 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(); - #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() \ + void Release(); \ + private:oRefCount ref; \ + eSingleLock ref_lock; + #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 // SWIG #define DECLARE_REF(x) \ private: \ void AddRef(); \ void Release(); - class Object - { - }; #endif // SWIG -#endif +#endif // __base_object_h