debug invariant checks for DHT observer allocator

This commit is contained in:
Arvid Norberg 2013-05-17 03:19:57 +00:00
parent 360c6a6e16
commit b6eb5d7476
2 changed files with 13 additions and 1 deletions

View File

@ -82,10 +82,12 @@ struct observer : boost::noncopyable
m_in_constructor = true; m_in_constructor = true;
m_was_sent = false; m_was_sent = false;
m_was_abandoned = false; m_was_abandoned = false;
m_in_use = true;
#endif #endif
set_target(ep); set_target(ep);
} }
// defined in rpc_manager.cpp
virtual ~observer(); virtual ~observer();
// this is called when a reply is received // this is called when a reply is received
@ -167,6 +169,7 @@ public:
bool m_in_constructor:1; bool m_in_constructor:1;
bool m_was_sent:1; bool m_was_sent:1;
bool m_was_abandoned:1; bool m_was_abandoned:1;
bool m_in_use:1;
#endif #endif
}; };

View File

@ -216,7 +216,11 @@ void* rpc_manager::allocate_observer()
{ {
m_pool_allocator.set_next_size(10); m_pool_allocator.set_next_size(10);
void* ret = m_pool_allocator.malloc(); void* ret = m_pool_allocator.malloc();
if (ret) ++m_allocated_observers; if (ret)
{
++m_allocated_observers;
TORRENT_ASSERT(reinterpret_cast<observer*>(ret)->m_in_use == false);
}
return ret; return ret;
} }
@ -224,6 +228,7 @@ void rpc_manager::free_observer(void* ptr)
{ {
if (!ptr) return; if (!ptr) return;
--m_allocated_observers; --m_allocated_observers;
TORRENT_ASSERT(reinterpret_cast<observer*>(ptr)->m_in_use == true);
m_pool_allocator.free(ptr); m_pool_allocator.free(ptr);
} }
@ -502,6 +507,10 @@ observer::~observer()
// reported back // reported back
TORRENT_ASSERT(m_was_sent == bool(flags & flag_done) || m_was_abandoned); TORRENT_ASSERT(m_was_sent == bool(flags & flag_done) || m_was_abandoned);
TORRENT_ASSERT(!m_in_constructor); TORRENT_ASSERT(!m_in_constructor);
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
TORRENT_ASSERT(m_in_use);
m_in_use = false;
#endif
} }
} } // namespace libtorrent::dht } } // namespace libtorrent::dht