]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Use custom allocater to decide if refcountbase was allocated on the heap and should...
authordanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>
Tue, 20 Oct 2009 00:55:22 +0000 (00:55 +0000)
committerdanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>
Tue, 20 Oct 2009 00:55:22 +0000 (00:55 +0000)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11928 e03df62e-2008-0410-955e-edbf42e46eb7

include/base.h
src/base.cpp
src/modules.cpp

index 0de37e781d6653796d589078e3dcf27a3f01d3bc..daff74e5663468be7a092b931978dccf5591f9e1 100644 (file)
@@ -82,6 +82,8 @@ class CoreExport refcountbase
        virtual ~refcountbase();
        inline unsigned int GetReferenceCount() const { return refcount; }
        friend class reference_base;
+       void* operator new(size_t);
+       void operator delete(void*);
  private:
        // uncopyable
        refcountbase(const refcountbase&);
@@ -93,6 +95,7 @@ class CoreExport reference_base
  protected:
        template<typename T> static inline unsigned int inc(T* v) { return ++(v->refcount); }
        template<typename T> static inline unsigned int dec(T* v) { return --(v->refcount); }
+
 };
 
 template <typename T>
@@ -189,6 +192,8 @@ class CoreExport ModuleException : public CoreException
        ModuleException(const std::string &message, Module* me = NULL);
 };
 
+/** Module reference, similar to reference<Module>
+ */
 class CoreExport ModuleRef : public reference_base
 {
        Module* const value;
index c0bc40811a6ee0fd32a505471ee8e8f6cefe97a3..bf42327544dd925b6b951ed03fe226ec2d998478 100644 (file)
@@ -41,14 +41,35 @@ CullResult::CullResult()
 {
 }
 
-refcountbase::refcountbase() : refcount(0)
+// This trick detects heap allocations of refcountbase objects
+static void* last_heap = NULL;
+static const unsigned int top_bit = 1 << (8*sizeof(unsigned int) - 1);
+
+void* refcountbase::operator new(size_t size)
+{
+       last_heap = ::operator new(size);
+       return last_heap;
+}
+
+void refcountbase::operator delete(void* obj)
 {
+       if (last_heap == obj)
+               last_heap = NULL;
+       ::operator delete(obj);
+}
+
+refcountbase::refcountbase()
+{
+       if (this == last_heap)
+               refcount = 0;
+       else
+               refcount = top_bit;
 }
 
 refcountbase::~refcountbase()
 {
-       if (refcount && ServerInstance && ServerInstance->Logs)
-               ServerInstance->Logs->Log("CULLLIST", DEBUG, "refcountbase::~ @%p with refcount %d",
+       if ((refcount & ~top_bit) && ServerInstance && ServerInstance->Logs)
+               ServerInstance->Logs->Log("CULLLIST", DEBUG, "refcountbase::~ @%p with refcount %x",
                        (void*)this, refcount);
 }
 
index 856addbf53371568256a0d826bde8694875a5555..7e4e0ec6869d67b561df3763eb5162430cd885a0 100644 (file)
@@ -51,14 +51,14 @@ void Event::Send()
 
 // These declarations define the behavours of the base class Module (which does nothing at all)
 
-Module::Module() : refcount(0) { }
+Module::Module() : refcount(1) { }
 CullResult Module::cull()
 {
        return classbase::cull();
 }
 Module::~Module()
 {
-       if (refcount)
+       if (refcount != 1)
                ServerInstance->Logs->Log("MODULE", DEFAULT, "References remain to destructed module " + ModuleSourceFile);
 }