|
Boost : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-05-29 06:06:52
Chuck Messenger wrote:
>
> boost/libs/smart_ptr/src/sp_collector.cpp:
>
> There is no sample program to compile/run, so I have to guess somewhat
> at how to use this one.
There is libs/smart_ptr/test/collector_test.cpp. You need to #define
BOOST_SP_ENABLE_DEBUG_HOOKS.
The basic idea of the algorithm is to keep track of all heap objects managed
by a shared_ptr. When the collector is invoked, it can use this information,
coupled with a memory scan in these heap blocks, to compute the reference
counts that would have occured if not for the external shared_ptr instances
that are unknown to the collector.
Using the difference between the computed counts and the actual counts, the
collector can infer which blocks are reachable (they have a larger real
count).
Greg's cyclic_ptr uses the same algorithm except that his code decrements
the counts in place instead of maintaining a parallel count structure. It
also uses x = x; assignments (a clever trick) to discover the pointer
members.
Both pointer discovery methods are flawed, although in different ways. :-)
It is not possible to write a portable collector using this algorithm
without compiler support.
Another class of garbage collected smart pointers keeps track of every smart
pointer _instance_ using a global registry (sp_collector.cpp keeps track of
every _object_). The upside is perfect discovery. The downside is that every
smart pointer copy incurs much larger overhead, esp. in multithreaded code
where the central registry becomes a bottleneck.
HTH
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk