For other readers who are following this thread (based on the relatively high read count..)
Lets look at Boost and the implementation of shared_ptr.
Quote:
“Boost is one of the most highly regarded and expertly designed C++ library projects in the world.”
— Herb Sutter and Andrei Alexandrescu, C++ Coding Standards
This is a non-intrusive reference counting pointer (meaning the reference count is managed by the pointer system and not by the object itself).
The key declaration is:
Code:
class sp_counted_base
{
private:
long use_count_; // #shared
long weak_count_; // #weak + (#shared != 0)
public:
virtual ~sp_counted_base() // nothrow
//...
}
This means that the object size is going to be:
Code:
sizeof(sp_counted_base) = 2*sizeof(long) + sizeof(vptr).
Which means a fixed size on most (32 bit) machines of 24 bytes - the exact size is not important, the main points are:
1) ALL Instances are the same size
2) Instances do NOT reference other blocks of memory
3) The instance size IS (what nearly everyone would consider) SMALL
4) The instance size IS NOT the sizeof(T *) nor of sizeof(smart_ptr<T>).
5) There are likely to be a large number of thiese instances created and destroyed during application execution.
When combined these traits directly match the conditions where the standard allocator's performance is at its worst.
Yet the boost library DOES NOT REPLACE THE DEFAULT ALLOCATOR! because of this.
Rather it follows the approach of specifically (with various options based on preprocessor definitions of BOOST_SP_USE_STD_ALLOCATOR and BOOST_SP_USE_QUICK_ALLOCATOR) of overriding operator new for the specific class in question. (sp_counted_impl.hpp in the details subdirectory).