From 9facfd70b5a8ca4dca16fc93f6fcfe1c2bf6ce0f Mon Sep 17 00:00:00 2001 From: danieldg Date: Tue, 20 Oct 2009 00:55:22 +0000 Subject: Use custom allocater to decide if refcountbase was allocated on the heap and should be deleted when refcount reaches zero git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11928 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/base.cpp | 27 ++++++++++++++++++++++++--- src/modules.cpp | 4 ++-- 2 files changed, 26 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/base.cpp b/src/base.cpp index c0bc40811..bf4232754 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -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); } diff --git a/src/modules.cpp b/src/modules.cpp index 856addbf5..7e4e0ec68 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -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); } -- cgit v1.2.3