2002-11-21 13:23:15 +00:00
|
|
|
//
|
|
|
|
// sp_debug_hooks.cpp
|
|
|
|
//
|
2003-01-13 18:32:16 +00:00
|
|
|
// Copyright (c) 2002, 2003 Peter Dimov
|
2002-11-21 13:23:15 +00:00
|
|
|
//
|
|
|
|
// Permission to copy, use, modify, sell and distribute this software
|
|
|
|
// is granted provided this copyright notice appears in all copies.
|
|
|
|
// This software is provided "as is" without express or implied
|
|
|
|
// warranty, and with no claim as to its suitability for any purpose.
|
|
|
|
//
|
|
|
|
// http://www.boost.org/libs/smart_ptr/debug_hooks.html
|
|
|
|
//
|
|
|
|
|
|
|
|
#if defined(BOOST_ENABLE_SP_DEBUG_HOOKS)
|
|
|
|
|
|
|
|
#include <boost/assert.hpp>
|
2003-01-13 17:11:28 +00:00
|
|
|
#include <boost/shared_ptr.hpp>
|
2003-01-07 15:34:56 +00:00
|
|
|
#include <boost/detail/lightweight_mutex.hpp>
|
2002-11-21 13:23:15 +00:00
|
|
|
#include <new>
|
|
|
|
#include <cstdlib>
|
2003-01-07 15:34:56 +00:00
|
|
|
#include <map>
|
2003-01-13 17:11:28 +00:00
|
|
|
#include <deque>
|
2003-01-07 15:34:56 +00:00
|
|
|
#include <iostream>
|
2002-11-21 13:23:15 +00:00
|
|
|
|
|
|
|
int const m = 2; // m * sizeof(int) must be aligned appropriately
|
|
|
|
|
2003-01-07 15:34:56 +00:00
|
|
|
// magic values to mark heap blocks with
|
|
|
|
|
2002-11-21 13:23:15 +00:00
|
|
|
int const allocated_scalar = 0x1234560C;
|
|
|
|
int const allocated_array = 0x1234560A;
|
|
|
|
int const adopted_scalar = 0x0567890C;
|
|
|
|
int const adopted_array = 0x0567890A;
|
|
|
|
int const deleted = 0x498769DE;
|
|
|
|
|
|
|
|
using namespace std; // for compilers where things aren't in std
|
|
|
|
|
2003-01-07 15:34:56 +00:00
|
|
|
// operator new
|
|
|
|
|
2002-11-21 13:23:15 +00:00
|
|
|
static new_handler get_new_handler()
|
|
|
|
{
|
|
|
|
new_handler p = set_new_handler(0);
|
|
|
|
set_new_handler(p);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void * allocate(size_t n, int mark)
|
|
|
|
{
|
|
|
|
int * pm;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
pm = static_cast<int*>(malloc(n + m * sizeof(int)));
|
|
|
|
|
|
|
|
if(pm != 0) break;
|
|
|
|
|
|
|
|
if(new_handler pnh = get_new_handler())
|
|
|
|
{
|
|
|
|
pnh();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*pm = mark;
|
|
|
|
|
|
|
|
return pm + m;
|
|
|
|
}
|
|
|
|
|
|
|
|
void * operator new(size_t n) throw(bad_alloc)
|
|
|
|
{
|
|
|
|
void * p = allocate(n, allocated_scalar);
|
|
|
|
|
|
|
|
#if !defined(BOOST_NO_EXCEPTIONS)
|
|
|
|
|
|
|
|
if(p == 0) throw bad_alloc();
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(__BORLANDC__) || (__BORLANDC__ > 0x551)
|
|
|
|
|
|
|
|
void * operator new(size_t n, nothrow_t const &) throw()
|
|
|
|
{
|
|
|
|
return allocate(n, allocated_scalar);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void * operator new[](size_t n) throw(bad_alloc)
|
|
|
|
{
|
|
|
|
void * p = allocate(n, allocated_array);
|
|
|
|
|
|
|
|
#if !defined(BOOST_NO_EXCEPTIONS)
|
|
|
|
|
|
|
|
if(p == 0) throw bad_alloc();
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(__BORLANDC__) || (__BORLANDC__ > 0x551)
|
|
|
|
|
|
|
|
void * operator new[](size_t n, nothrow_t const &) throw()
|
|
|
|
{
|
|
|
|
return allocate(n, allocated_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2003-01-07 15:34:56 +00:00
|
|
|
// cycle detection
|
|
|
|
|
2003-01-13 17:11:28 +00:00
|
|
|
typedef std::map< void const *, std::pair<void *, size_t> > map_type;
|
2003-01-07 15:34:56 +00:00
|
|
|
|
|
|
|
static map_type & get_map()
|
|
|
|
{
|
|
|
|
static map_type m;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef boost::detail::lightweight_mutex mutex_type;
|
|
|
|
|
|
|
|
static mutex_type & get_mutex()
|
|
|
|
{
|
|
|
|
static mutex_type m;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void * init_mutex_before_main = &get_mutex();
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class X;
|
|
|
|
|
|
|
|
struct count_layout
|
|
|
|
{
|
2003-01-14 15:13:53 +00:00
|
|
|
boost::detail::sp_counted_base * pi;
|
2003-01-07 15:34:56 +00:00
|
|
|
int id;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct shared_ptr_layout
|
|
|
|
{
|
|
|
|
X * px;
|
|
|
|
count_layout pn;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// assume 4 byte alignment for pointers when scanning
|
|
|
|
size_t const pointer_align = 4;
|
|
|
|
|
2003-01-13 17:11:28 +00:00
|
|
|
typedef std::map<void const *, long> map2_type;
|
|
|
|
|
|
|
|
static void scan_and_count(void const * area, size_t size, map_type const & m, map2_type & m2)
|
2003-01-07 15:34:56 +00:00
|
|
|
{
|
|
|
|
unsigned char const * p = static_cast<unsigned char const *>(area);
|
|
|
|
|
2003-01-08 15:01:04 +00:00
|
|
|
for(size_t n = 0; n + sizeof(shared_ptr_layout) <= size; p += pointer_align, n += pointer_align)
|
2003-01-07 15:34:56 +00:00
|
|
|
{
|
|
|
|
shared_ptr_layout const * q = reinterpret_cast<shared_ptr_layout const *>(p);
|
|
|
|
|
2003-01-07 23:12:02 +00:00
|
|
|
if(q->pn.id == boost::detail::shared_count_id && q->pn.pi != 0 && m.count(q->pn.pi) != 0)
|
2003-01-07 15:34:56 +00:00
|
|
|
{
|
|
|
|
++m2[q->pn.pi];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-13 17:11:28 +00:00
|
|
|
typedef std::deque<void const *> open_type;
|
|
|
|
|
|
|
|
static void scan_and_mark(void const * area, size_t size, map2_type & m2, open_type & open)
|
2003-01-07 15:34:56 +00:00
|
|
|
{
|
|
|
|
unsigned char const * p = static_cast<unsigned char const *>(area);
|
|
|
|
|
2003-01-08 15:01:04 +00:00
|
|
|
for(size_t n = 0; n + sizeof(shared_ptr_layout) <= size; p += pointer_align, n += pointer_align)
|
2003-01-07 15:34:56 +00:00
|
|
|
{
|
|
|
|
shared_ptr_layout const * q = reinterpret_cast<shared_ptr_layout const *>(p);
|
|
|
|
|
2003-01-13 17:11:28 +00:00
|
|
|
if(q->pn.id == boost::detail::shared_count_id && q->pn.pi != 0 && m2.count(q->pn.pi) != 0)
|
2003-01-07 15:34:56 +00:00
|
|
|
{
|
2003-01-13 17:11:28 +00:00
|
|
|
open.push_back(q->pn.pi);
|
|
|
|
m2.erase(q->pn.pi);
|
2003-01-07 15:34:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-13 17:11:28 +00:00
|
|
|
static void find_unreachable_objects(map_type const & m, map2_type & m2)
|
2003-01-07 15:34:56 +00:00
|
|
|
{
|
|
|
|
// scan objects for shared_ptr members, compute internal counts
|
|
|
|
|
|
|
|
{
|
2003-01-13 17:11:28 +00:00
|
|
|
for(map_type::const_iterator i = m.begin(); i != m.end(); ++i)
|
|
|
|
{
|
2003-01-14 15:13:53 +00:00
|
|
|
boost::detail::sp_counted_base const * p = static_cast<boost::detail::sp_counted_base const *>(i->first);
|
2003-01-07 15:34:56 +00:00
|
|
|
|
2003-01-13 17:11:28 +00:00
|
|
|
BOOST_ASSERT(p->use_count() != 0); // there should be no inactive counts in the map
|
2003-01-07 15:34:56 +00:00
|
|
|
|
2003-01-13 17:11:28 +00:00
|
|
|
scan_and_count(i->second.first, i->second.second, m, m2);
|
|
|
|
}
|
2003-01-07 15:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// mark reachable objects
|
|
|
|
|
|
|
|
{
|
2003-01-13 17:11:28 +00:00
|
|
|
open_type open;
|
2003-01-07 15:34:56 +00:00
|
|
|
|
2003-01-13 17:11:28 +00:00
|
|
|
for(map2_type::iterator i = m2.begin(); i != m2.end(); ++i)
|
2003-01-07 15:34:56 +00:00
|
|
|
{
|
2003-01-14 15:13:53 +00:00
|
|
|
boost::detail::sp_counted_base const * p = static_cast<boost::detail::sp_counted_base const *>(i->first);
|
2003-01-13 17:11:28 +00:00
|
|
|
if(p->use_count() != i->second) open.push_back(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(open_type::iterator j = open.begin(); j != open.end(); ++j)
|
|
|
|
{
|
|
|
|
m2.erase(*j);
|
|
|
|
}
|
|
|
|
|
|
|
|
while(!open.empty())
|
|
|
|
{
|
|
|
|
void const * p = open.front();
|
|
|
|
open.pop_front();
|
|
|
|
|
|
|
|
map_type::const_iterator i = m.find(p);
|
|
|
|
BOOST_ASSERT(i != m.end());
|
|
|
|
|
|
|
|
scan_and_mark(i->second.first, i->second.second, m2, open);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// m2 now contains the unreachable objects
|
|
|
|
}
|
|
|
|
|
|
|
|
void report_unreachable_objects(bool verbose)
|
|
|
|
{
|
|
|
|
map2_type m2;
|
|
|
|
|
|
|
|
mutex_type::scoped_lock lock(get_mutex());
|
|
|
|
|
|
|
|
map_type const & m = get_map();
|
2003-01-07 15:34:56 +00:00
|
|
|
|
2003-01-13 17:11:28 +00:00
|
|
|
find_unreachable_objects(m, m2);
|
|
|
|
|
|
|
|
if(verbose)
|
|
|
|
{
|
|
|
|
for(map2_type::iterator j = m2.begin(); j != m2.end(); ++j)
|
|
|
|
{
|
|
|
|
map_type::const_iterator i = m.find(j->first);
|
|
|
|
BOOST_ASSERT(i != m.end());
|
|
|
|
// std::cerr << "Unreachable object at " << i->second.first << ", " << i->second.second << " bytes long.\n";
|
2003-01-07 15:34:56 +00:00
|
|
|
}
|
2003-01-13 17:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(verbose || !m2.empty())
|
|
|
|
{
|
|
|
|
std::cerr << m2.size() << " unreachable objects.\n";
|
|
|
|
}
|
|
|
|
}
|
2003-01-07 15:34:56 +00:00
|
|
|
|
2003-01-13 17:11:28 +00:00
|
|
|
typedef std::deque< boost::shared_ptr<X> > free_list_type;
|
2003-01-07 15:34:56 +00:00
|
|
|
|
2003-01-13 17:11:28 +00:00
|
|
|
static void scan_and_free(void * area, size_t size, map2_type const & m2, free_list_type & free)
|
|
|
|
{
|
|
|
|
unsigned char * p = static_cast<unsigned char *>(area);
|
2003-01-07 15:34:56 +00:00
|
|
|
|
2003-01-13 17:11:28 +00:00
|
|
|
for(size_t n = 0; n + sizeof(shared_ptr_layout) <= size; p += pointer_align, n += pointer_align)
|
2003-01-07 15:34:56 +00:00
|
|
|
{
|
2003-01-13 17:11:28 +00:00
|
|
|
shared_ptr_layout * q = reinterpret_cast<shared_ptr_layout *>(p);
|
2003-01-07 15:34:56 +00:00
|
|
|
|
2003-01-13 17:11:28 +00:00
|
|
|
if(q->pn.id == boost::detail::shared_count_id && q->pn.pi != 0 && m2.count(q->pn.pi) != 0 && q->px != 0)
|
2003-01-07 15:34:56 +00:00
|
|
|
{
|
2003-01-13 17:11:28 +00:00
|
|
|
boost::shared_ptr<X> * ppx = reinterpret_cast< boost::shared_ptr<X> * >(p);
|
|
|
|
free.push_back(*ppx);
|
|
|
|
ppx->reset();
|
2003-01-07 15:34:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-13 17:11:28 +00:00
|
|
|
void free_unreachable_objects()
|
|
|
|
{
|
|
|
|
map2_type m2;
|
|
|
|
|
|
|
|
mutex_type::scoped_lock lock(get_mutex());
|
|
|
|
|
|
|
|
map_type const & m = get_map();
|
|
|
|
|
|
|
|
find_unreachable_objects(m, m2);
|
|
|
|
|
|
|
|
free_list_type free;
|
|
|
|
|
|
|
|
for(map2_type::iterator j = m2.begin(); j != m2.end(); ++j)
|
|
|
|
{
|
|
|
|
map_type::const_iterator i = m.find(j->first);
|
|
|
|
BOOST_ASSERT(i != m.end());
|
|
|
|
scan_and_free(i->second.first, i->second.second, m2, free);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-07 15:34:56 +00:00
|
|
|
// debug hooks
|
|
|
|
|
2002-11-21 13:23:15 +00:00
|
|
|
namespace boost
|
|
|
|
{
|
|
|
|
|
|
|
|
void sp_scalar_constructor_hook(void * p)
|
|
|
|
{
|
|
|
|
if(p == 0) return;
|
|
|
|
|
|
|
|
int * pm = static_cast<int*>(p);
|
|
|
|
pm -= m;
|
|
|
|
|
|
|
|
BOOST_ASSERT(*pm != adopted_scalar); // second smart pointer to the same address
|
|
|
|
BOOST_ASSERT(*pm != allocated_array); // allocated with new[]
|
|
|
|
BOOST_ASSERT(*pm == allocated_scalar); // not allocated with new
|
|
|
|
|
|
|
|
*pm = adopted_scalar;
|
|
|
|
}
|
|
|
|
|
2003-01-07 15:34:56 +00:00
|
|
|
void sp_scalar_constructor_hook(void * px, std::size_t size, void * pn)
|
|
|
|
{
|
|
|
|
sp_scalar_constructor_hook(px);
|
|
|
|
mutex_type::scoped_lock lock(get_mutex());
|
|
|
|
get_map()[pn] = std::make_pair(px, size);
|
|
|
|
}
|
|
|
|
|
2002-11-21 13:23:15 +00:00
|
|
|
void sp_scalar_destructor_hook(void * p)
|
|
|
|
{
|
|
|
|
if(p == 0) return;
|
|
|
|
|
|
|
|
int * pm = static_cast<int*>(p);
|
|
|
|
pm -= m;
|
|
|
|
|
|
|
|
BOOST_ASSERT(*pm == adopted_scalar); // attempt to destroy nonmanaged block
|
|
|
|
|
|
|
|
*pm = allocated_scalar;
|
|
|
|
}
|
|
|
|
|
2003-01-07 15:34:56 +00:00
|
|
|
void sp_scalar_destructor_hook(void * px, std::size_t /*size*/, void * pn)
|
|
|
|
{
|
|
|
|
sp_scalar_destructor_hook(px);
|
|
|
|
mutex_type::scoped_lock lock(get_mutex());
|
|
|
|
get_map().erase(pn);
|
|
|
|
}
|
|
|
|
|
2002-11-21 13:23:15 +00:00
|
|
|
// It is not possible to handle the array hooks in a portable manner.
|
|
|
|
// The implementation typically reserves a bit of storage for the number
|
|
|
|
// of objects in the array, and the argument of the array hook isn't
|
|
|
|
// equal to the return value of operator new[].
|
|
|
|
|
|
|
|
void sp_array_constructor_hook(void * /* p */)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
if(p == 0) return;
|
|
|
|
|
|
|
|
// adjust p depending on the implementation
|
|
|
|
|
|
|
|
int * pm = static_cast<int*>(p);
|
|
|
|
pm -= m;
|
|
|
|
|
|
|
|
BOOST_ASSERT(*pm != adopted_array); // second smart array pointer to the same address
|
|
|
|
BOOST_ASSERT(*pm != allocated_scalar); // allocated with new
|
|
|
|
BOOST_ASSERT(*pm == allocated_array); // not allocated with new[]
|
|
|
|
|
|
|
|
*pm = adopted_array;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void sp_array_destructor_hook(void * /* p */)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
if(p == 0) return;
|
|
|
|
|
|
|
|
// adjust p depending on the implementation
|
|
|
|
|
|
|
|
int * pm = static_cast<int*>(p);
|
|
|
|
pm -= m;
|
|
|
|
|
|
|
|
BOOST_ASSERT(*pm == adopted_array); // attempt to destroy nonmanaged block
|
|
|
|
|
|
|
|
*pm = allocated_array;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace boost
|
|
|
|
|
2003-01-07 15:34:56 +00:00
|
|
|
// operator delete
|
|
|
|
|
2002-11-21 13:23:15 +00:00
|
|
|
void operator delete(void * p) throw()
|
|
|
|
{
|
|
|
|
if(p == 0) return;
|
|
|
|
|
|
|
|
int * pm = static_cast<int*>(p);
|
|
|
|
pm -= m;
|
|
|
|
|
|
|
|
BOOST_ASSERT(*pm != deleted); // double delete
|
|
|
|
BOOST_ASSERT(*pm != adopted_scalar); // delete p.get();
|
|
|
|
BOOST_ASSERT(*pm != allocated_array); // allocated with new[]
|
|
|
|
BOOST_ASSERT(*pm == allocated_scalar); // not allocated with new
|
|
|
|
|
|
|
|
*pm = deleted;
|
|
|
|
|
|
|
|
free(pm);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(__BORLANDC__) || (__BORLANDC__ > 0x551)
|
|
|
|
|
|
|
|
void operator delete(void * p, nothrow_t const &) throw()
|
|
|
|
{
|
|
|
|
::operator delete(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void operator delete[](void * p) throw()
|
|
|
|
{
|
|
|
|
if(p == 0) return;
|
|
|
|
|
|
|
|
int * pm = static_cast<int*>(p);
|
|
|
|
pm -= m;
|
|
|
|
|
|
|
|
BOOST_ASSERT(*pm != deleted); // double delete
|
|
|
|
BOOST_ASSERT(*pm != adopted_scalar); // delete p.get();
|
|
|
|
BOOST_ASSERT(*pm != allocated_scalar); // allocated with new
|
|
|
|
BOOST_ASSERT(*pm == allocated_array); // not allocated with new[]
|
|
|
|
|
|
|
|
*pm = deleted;
|
|
|
|
|
|
|
|
free(pm);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(__BORLANDC__) || (__BORLANDC__ > 0x551)
|
|
|
|
|
|
|
|
void operator delete[](void * p, nothrow_t const &) throw()
|
|
|
|
{
|
|
|
|
::operator delete[](p);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // defined(BOOST_ENABLE_SP_DEBUG_HOOKS)
|