diff options
author | redi <redi@138bc75d-0d04-0410-961f-82ee72b054a4> | 2010-04-21 23:33:44 +0000 |
---|---|---|
committer | redi <redi@138bc75d-0d04-0410-961f-82ee72b054a4> | 2010-04-21 23:33:44 +0000 |
commit | bd8a9cbe3ab8a02f669adf27655c97f0f1211967 (patch) | |
tree | 80f57097da71ed324ad21f9513419c6d2b076a31 /libstdc++-v3/doc/html/manual/memory.html | |
parent | 5a2aecd69f9210e2b54c34d515a6db56ee070527 (diff) | |
download | ppe42-gcc-bd8a9cbe3ab8a02f669adf27655c97f0f1211967.tar.gz ppe42-gcc-bd8a9cbe3ab8a02f669adf27655c97f0f1211967.zip |
2010-04-22 Jonathan Wakely <jwakely.gcc@gmail.com>
* doc/xml/faq.xml: Link to manual.
* doc/xml/manual/using.xml: Expand dynamic libraries section.
* doc/xml/manual/strings.xml: Mention shrink_to_fit() member.
* doc/xml/manual/prerequisites.xml: Link to doxygen requirements.
* doc/xml/manual/appendix_contributing.xml: Update Bash version.
* doc/html/*: Regenerate.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@158624 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libstdc++-v3/doc/html/manual/memory.html')
-rw-r--r-- | libstdc++-v3/doc/html/manual/memory.html | 511 |
1 files changed, 447 insertions, 64 deletions
diff --git a/libstdc++-v3/doc/html/manual/memory.html b/libstdc++-v3/doc/html/manual/memory.html index 18db2d502bf..82ef8c7125f 100644 --- a/libstdc++-v3/doc/html/manual/memory.html +++ b/libstdc++-v3/doc/html/manual/memory.html @@ -1,15 +1,15 @@ <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Chapter 11. Memory</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><meta name="keywords" content=" ISO C++ , library " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="utilities.html" title="Part IV. Utilities" /><link rel="prev" href="pairs.html" title="Chapter 10. Pairs" /><link rel="next" href="auto_ptr.html" title="auto_ptr" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 11. Memory</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="pairs.html">Prev</a> </td><th width="60%" align="center">Part IV. +<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Memory</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><meta name="keywords" content=" ISO C++ , library " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="utilities.html" title="Chapter 6. Utilities" /><link rel="prev" href="pairs.html" title="Pairs" /><link rel="next" href="traits.html" title="Traits" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Memory</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="pairs.html">Prev</a> </td><th width="60%" align="center">Chapter 6. Utilities -</th><td width="20%" align="right"> <a accesskey="n" href="auto_ptr.html">Next</a></td></tr></table><hr /></div><div class="chapter" title="Chapter 11. Memory"><div class="titlepage"><div><div><h2 class="title"><a id="manual.util.memory"></a>Chapter 11. Memory</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="memory.html#manual.util.memory.allocator">Allocators</a></span></dt><dd><dl><dt><span class="sect2"><a href="memory.html#allocator.req">Requirements</a></span></dt><dt><span class="sect2"><a href="memory.html#allocator.design_issues">Design Issues</a></span></dt><dt><span class="sect2"><a href="memory.html#allocator.impl">Implementation</a></span></dt><dt><span class="sect2"><a href="memory.html#allocator.using">Using a Specific Allocator</a></span></dt><dt><span class="sect2"><a href="memory.html#allocator.custom">Custom Allocators</a></span></dt><dt><span class="sect2"><a href="memory.html#allocator.ext">Extension Allocators</a></span></dt></dl></dd><dt><span class="sect1"><a href="auto_ptr.html">auto_ptr</a></span></dt><dd><dl><dt><span class="sect2"><a href="auto_ptr.html#auto_ptr.limitations">Limitations</a></span></dt><dt><span class="sect2"><a href="auto_ptr.html#auto_ptr.using">Use in Containers</a></span></dt></dl></dd><dt><span class="sect1"><a href="shared_ptr.html">shared_ptr</a></span></dt><dd><dl><dt><span class="sect2"><a href="shared_ptr.html#shared_ptr.req">Requirements</a></span></dt><dt><span class="sect2"><a href="shared_ptr.html#shared_ptr.design_issues">Design Issues</a></span></dt><dt><span class="sect2"><a href="shared_ptr.html#shared_ptr.impl">Implementation</a></span></dt><dt><span class="sect2"><a href="shared_ptr.html#shared_ptr.using">Use</a></span></dt><dt><span class="sect2"><a href="shared_ptr.html#shared_ptr.ack">Acknowledgments</a></span></dt></dl></dd></dl></div><p> +</th><td width="20%" align="right"> <a accesskey="n" href="traits.html">Next</a></td></tr></table><hr /></div><div class="section" title="Memory"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="std.util.memory"></a>Memory</h2></div></div></div><p> Memory contains three general areas. First, function and operator calls via <code class="function">new</code> and <code class="function">delete</code> operator or member function calls. Second, allocation via <code class="classname">allocator</code>. And finally, smart pointer and intelligent pointer abstractions. - </p><div class="sect1" title="Allocators"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="manual.util.memory.allocator"></a>Allocators</h2></div></div></div><p> + </p><div class="section" title="Allocators"><div class="titlepage"><div><div><h3 class="title"><a id="std.util.memory.allocator"></a>Allocators</h3></div></div></div><p> Memory management for Standard Library entities is encapsulated in a class template called <code class="classname">allocator</code>. The <code class="classname">allocator</code> abstraction is used throughout the @@ -17,7 +17,7 @@ algorithms, and parts of iostreams. This class, and base classes of it, are the superset of available free store (<span class="quote">“<span class="quote">heap</span>”</span>) management classes. -</p><div class="sect2" title="Requirements"><div class="titlepage"><div><div><h3 class="title"><a id="allocator.req"></a>Requirements</h3></div></div></div><p> +</p><div class="section" title="Requirements"><div class="titlepage"><div><div><h4 class="title"><a id="allocator.req"></a>Requirements</h4></div></div></div><p> The C++ standard only gives a few directives in this area: </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p> When you add elements to a container, and the container must @@ -32,9 +32,9 @@ container-of-T is <code class="classname">allocator<T></code>. </p></li><li class="listitem"><p> The interface of the <code class="classname">allocator<T></code> class is - extremely simple. It has about 20 public declarations (nested - typedefs, member functions, etc), but the two which concern us most - are: + extremely simple. It has about 20 public declarations (nested + typedefs, member functions, etc), but the two which concern us most + are: </p><pre class="programlisting"> T* allocate (size_type n, const void* hint = 0); void deallocate (T* p, size_type n); @@ -43,18 +43,18 @@ functions is a <span class="emphasis"><em>count</em></span> of the number of <span class="type">T</span>'s to allocate space for, <span class="emphasis"><em>not their total size</em></span>. - (This is a simplification; the real signatures use nested typedefs.) + (This is a simplification; the real signatures use nested typedefs.) </p></li><li class="listitem"><p> The storage is obtained by calling <code class="function">::operator new</code>, but it is unspecified when or how - often this function is called. The use of the + often this function is called. The use of the <code class="varname">hint</code> is unspecified, but intended as an aid to locality if an implementation so desires. <code class="constant">[20.4.1.1]/6</code> - </p></li></ul></div><p> + </p></li></ul></div><p> Complete details can be found in the C++ standard, look in <code class="constant">[20.4 Memory]</code>. - </p></div><div class="sect2" title="Design Issues"><div class="titlepage"><div><div><h3 class="title"><a id="allocator.design_issues"></a>Design Issues</h3></div></div></div><p> + </p></div><div class="section" title="Design Issues"><div class="titlepage"><div><div><h4 class="title"><a id="allocator.design_issues"></a>Design Issues</h4></div></div></div><p> The easiest way of fulfilling the requirements is to call <code class="function">operator new</code> each time a container needs memory, and to call <code class="function">operator delete</code> each time @@ -67,7 +67,7 @@ while <code class="classname">__gnu_cxx::malloc_allocator</code> implements much the same thing, only with the C language functions <code class="function">std::malloc</code> and <code class="function">free</code>. - </p><p> + </p><p> Another approach is to use intelligence within the allocator class to cache allocations. This extra machinery can take a variety of forms: a bitmap index, an index into an exponentially increasing @@ -93,33 +93,33 @@ or loading and unloading shared objects in memory. As such, using caching allocators on systems that do not support <code class="function">abi::__cxa_atexit</code> is not recommended. - </p></div><div class="sect2" title="Implementation"><div class="titlepage"><div><div><h3 class="title"><a id="allocator.impl"></a>Implementation</h3></div></div></div><div class="sect3" title="Interface Design"><div class="titlepage"><div><div><h4 class="title"><a id="id630442"></a>Interface Design</h4></div></div></div><p> + </p></div><div class="section" title="Implementation"><div class="titlepage"><div><div><h4 class="title"><a id="allocator.impl"></a>Implementation</h4></div></div></div><div class="section" title="Interface Design"><div class="titlepage"><div><div><h5 class="title"><a id="id444984"></a>Interface Design</h5></div></div></div><p> The only allocator interface that is supported is the standard C++ interface. As such, all STL containers have been adjusted, and all external allocators have - been modified to support this change. - </p><p> + been modified to support this change. + </p><p> The class <code class="classname">allocator</code> just has typedef, constructor, and rebind members. It inherits from one of the high-speed extension allocators, covered below. Thus, all allocation and deallocation depends on the base class. - </p><p> + </p><p> The base class that <code class="classname">allocator</code> is derived from may not be user-configurable. -</p></div><div class="sect3" title="Selecting Default Allocation Policy"><div class="titlepage"><div><div><h4 class="title"><a id="id637894"></a>Selecting Default Allocation Policy</h4></div></div></div><p> +</p></div><div class="section" title="Selecting Default Allocation Policy"><div class="titlepage"><div><div><h5 class="title"><a id="id399970"></a>Selecting Default Allocation Policy</h5></div></div></div><p> It's difficult to pick an allocation strategy that will provide maximum utility, without excessively penalizing some behavior. In fact, it's difficult just deciding which typical actions to measure for speed. - </p><p> + </p><p> Three synthetic benchmarks have been created that provide data that is used to compare different C++ allocators. These tests are: </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p> - Insertion. + Insertion. </p><p> Over multiple iterations, various STL container objects have elements inserted to some maximum amount. A variety - of allocators are tested. + of allocators are tested. Test source for <a class="ulink" href="http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/testsuite/performance/23_containers/insert/sequence.cc?view=markup" target="_top">sequence</a> and <a class="ulink" href="http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/testsuite/performance/23_containers/insert/associative.cc?view=markup" target="_top">associative</a> containers. @@ -128,22 +128,22 @@ </p><p> This test shows the ability of the allocator to reclaim memory on a per-thread basis, as well as measuring thread contention - for memory resources. - Test source + for memory resources. + Test source <a class="ulink" href="http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/testsuite/performance/23_containers/insert_erase/associative.cc?view=markup" target="_top">here</a>. </p></li><li class="listitem"><p> A threaded producer/consumer model. </p><p> Test source for <a class="ulink" href="http://gcc.gnu.org/viewcvs/trunk/libstdc++-v3/testsuite/performance/23_containers/producer_consumer/sequence.cc?view=markup" target="_top">sequence</a> - and + and <a class="ulink" href="http://gcc.gnu.org/viewcvs/trunk/libstdc++-v3/testsuite/performance/23_containers/producer_consumer/associative.cc?view=markup" target="_top">associative</a> containers. </p></li></ol></div><p> The current default choice for <code class="classname">allocator</code> is <code class="classname">__gnu_cxx::new_allocator</code>. - </p></div><div class="sect3" title="Disabling Memory Caching"><div class="titlepage"><div><div><h4 class="title"><a id="id629596"></a>Disabling Memory Caching</h4></div></div></div><p> + </p></div><div class="section" title="Disabling Memory Caching"><div class="titlepage"><div><div><h5 class="title"><a id="id392037"></a>Disabling Memory Caching</h5></div></div></div><p> In use, <code class="classname">allocator</code> may allocate and deallocate using implementation-specified strategies and heuristics. Because of this, every call to an allocator object's @@ -151,13 +151,13 @@ call the global operator new. This situation is also duplicated for calls to the <code class="function">deallocate</code> member function. - </p><p> - This can be confusing. - </p><p> + </p><p> + This can be confusing. + </p><p> In particular, this can make debugging memory errors more difficult, especially when using third party tools like valgrind or debug versions of <code class="function">new</code>. - </p><p> + </p><p> There are various ways to solve this problem. One would be to use a custom allocator that just called operators <code class="function">new</code> and <code class="function">delete</code> @@ -179,7 +179,7 @@ environment, it likely means that you linked against objects built against the older library (objects which might still using the cached allocations...). - </p></div></div><div class="sect2" title="Using a Specific Allocator"><div class="titlepage"><div><div><h3 class="title"><a id="allocator.using"></a>Using a Specific Allocator</h3></div></div></div><p> + </p></div></div><div class="section" title="Using a Specific Allocator"><div class="titlepage"><div><div><h4 class="title"><a id="allocator.using"></a>Using a Specific Allocator</h4></div></div></div><p> You can specify different memory management schemes on a per-container basis, by overriding the default <span class="type">Allocator</span> template parameter. For example, an easy @@ -190,16 +190,16 @@ Likewise, a debugging form of whichever allocator is currently in use: </p><pre class="programlisting"> std::deque <int, __gnu_cxx::debug_allocator<std::allocator<int> > > debug_deque; - </pre></div><div class="sect2" title="Custom Allocators"><div class="titlepage"><div><div><h3 class="title"><a id="allocator.custom"></a>Custom Allocators</h3></div></div></div><p> + </pre></div><div class="section" title="Custom Allocators"><div class="titlepage"><div><div><h4 class="title"><a id="allocator.custom"></a>Custom Allocators</h4></div></div></div><p> Writing a portable C++ allocator would dictate that the interface would look much like the one specified for <code class="classname">allocator</code>. Additional member functions, but not subtractions, would be permissible. - </p><p> + </p><p> Probably the best place to start would be to copy one of the - extension allocators: say a simple one like + extension allocators: say a simple one like <code class="classname">new_allocator</code>. - </p></div><div class="sect2" title="Extension Allocators"><div class="titlepage"><div><div><h3 class="title"><a id="allocator.ext"></a>Extension Allocators</h3></div></div></div><p> + </p></div><div class="section" title="Extension Allocators"><div class="titlepage"><div><div><h4 class="title"><a id="allocator.ext"></a>Extension Allocators</h4></div></div></div><p> Several other allocators are provided as part of this implementation. The location of the extension allocators and their names have changed, but in all cases, functionality is @@ -237,7 +237,7 @@ startup. For usage examples, please consult the testsuite. </p></li><li class="listitem"><p> <code class="classname">debug_allocator</code> - </p><p> + </p><p> A wrapper around an arbitrary allocator A. It passes on slightly increased size requests to A, and uses the extra memory to store size information. When a pointer is passed @@ -246,13 +246,13 @@ guarantee they match. </p></li><li class="listitem"><p> <code class="classname">throw_allocator</code> - </p><p> + </p><p> Includes memory tracking and marking abilities as well as hooks for throwing exceptions at configurable intervals (including random, - all, none). + all, none). </p></li><li class="listitem"><p> <code class="classname">__pool_alloc</code> - </p><p> + </p><p> A high-performance, single pool allocator. The reusable memory is shared among identical instantiations of this type. It calls through <code class="function">::operator new</code> to @@ -261,7 +261,7 @@ size, then the pool is bypassed, and the allocate/deallocate request is passed to <code class="function">::operator new</code> directly. - </p><p> + </p><p> Older versions of this class take a boolean template parameter, called <code class="varname">thr</code>, and an integer template parameter, called <code class="varname">inst</code>. @@ -308,39 +308,422 @@ A high-performance allocator that uses a bit-map to keep track of the used and unused memory locations. It has its own documentation, found <a class="link" href="bitmap_allocator.html" title="bitmap_allocator">here</a>. - </p></li></ol></div></div><div class="bibliography" title="Bibliography"><div class="titlepage"><div><div><h3 class="title"><a id="allocator.biblio"></a>Bibliography</h3></div></div></div><div class="biblioentry" title="ISO/IEC 14882:1998 Programming languages - C++"><a id="id616986"></a><p><span class="title"><i> - ISO/IEC 14882:1998 Programming languages - C++ + </p></li></ol></div></div><div class="bibliography" title="Bibliography"><div class="titlepage"><div><div><h4 class="title"><a id="allocator.biblio"></a>Bibliography</h4></div></div></div><div class="biblioentry" title="ISO/IEC 14882:1998 Programming languages - C++"><a id="id449438"></a><p><span class="title"><i> + ISO/IEC 14882:1998 Programming languages - C++ </i>. </span> isoc++_1998 - <span class="pagenums">20.4 Memory. </span></p></div><div class="biblioentry" title="The Standard Librarian: What Are Allocators Good"><a id="id617001"></a><p><span class="title"><i>The Standard Librarian: What Are Allocators Good - </i>. </span> - austernm - <span class="author"><span class="firstname">Matt</span> <span class="surname">Austern</span>. </span><span class="publisher"><span class="publishername"> - C/C++ Users Journal - . </span></span><span class="biblioid"> - <a class="ulink" href="http://www.cuj.com/documents/s=8000/cujcexp1812austern/" target="_top"> + <span class="pagenums">20.4 Memory. </span></p></div><div class="biblioentry"><a id="id449453"></a><p><span class="biblioid"> + <a class="ulink" href="http://www.drdobbs.com/cpp/184403759" target="_top"> + <em class="citetitle"> + The Standard Librarian: What Are Allocators Good For? + </em> </a> - . </span></p></div><div class="biblioentry" title="The Hoard Memory Allocator"><a id="id658988"></a><p><span class="title"><i>The Hoard Memory Allocator</i>. </span> - emeryb - <span class="author"><span class="firstname">Emery</span> <span class="surname">Berger</span>. </span><span class="biblioid"> + . </span><span class="author"><span class="firstname">Matt</span> <span class="surname">Austern</span>. </span><span class="publisher"><span class="publishername"> + C/C++ Users Journal + . </span></span></p></div><div class="biblioentry"><a id="id396647"></a><p><span class="biblioid"> <a class="ulink" href="http://www.cs.umass.edu/~emery/hoard/" target="_top"> + <em class="citetitle"> + The Hoard Memory Allocator + </em> </a> - . </span></p></div><div class="biblioentry" title="Reconsidering Custom Memory Allocation"><a id="id620190"></a><p><span class="title"><i>Reconsidering Custom Memory Allocation</i>. </span> - bergerzorn - <span class="author"><span class="firstname">Emery</span> <span class="surname">Berger</span>. </span><span class="author"><span class="firstname">Ben</span> <span class="surname">Zorn</span>. </span><span class="author"><span class="firstname">Kathryn</span> <span class="surname">McKinley</span>. </span><span class="copyright">Copyright © 2002 OOPSLA. </span><span class="biblioid"> + . </span><span class="author"><span class="firstname">Emery</span> <span class="surname">Berger</span>. </span></p></div><div class="biblioentry"><a id="id410436"></a><p><span class="biblioid"> <a class="ulink" href="http://www.cs.umass.edu/~emery/pubs/berger-oopsla2002.pdf" target="_top"> + <em class="citetitle"> + Reconsidering Custom Memory Allocation + </em> </a> - . </span></p></div><div class="biblioentry" title="Allocator Types"><a id="id598997"></a><p><span class="title"><i>Allocator Types</i>. </span> - kreftlanger - <span class="author"><span class="firstname">Klaus</span> <span class="surname">Kreft</span>. </span><span class="author"><span class="firstname">Angelika</span> <span class="surname">Langer</span>. </span><span class="publisher"><span class="publishername"> - C/C++ Users Journal - . </span></span><span class="biblioid"> + . </span><span class="author"><span class="firstname">Emery</span> <span class="surname">Berger</span>. </span><span class="author"><span class="firstname">Ben</span> <span class="surname">Zorn</span>. </span><span class="author"><span class="firstname">Kathryn</span> <span class="surname">McKinley</span>. </span><span class="copyright">Copyright © 2002 OOPSLA. </span></p></div><div class="biblioentry"><a id="id507492"></a><p><span class="biblioid"> <a class="ulink" href="http://www.angelikalanger.com/Articles/C++Report/Allocators/Allocators.html" target="_top"> + <em class="citetitle"> + Allocator Types + </em> </a> - . </span></p></div><div class="biblioentry" title="The C++ Programming Language"><a id="id683391"></a><p><span class="title"><i>The C++ Programming Language</i>. </span> - tcpl - <span class="author"><span class="firstname">Bjarne</span> <span class="surname">Stroustrup</span>. </span><span class="copyright">Copyright © 2000 . </span><span class="pagenums">19.4 Allocators. </span><span class="publisher"><span class="publishername"> + . </span><span class="author"><span class="firstname">Klaus</span> <span class="surname">Kreft</span>. </span><span class="author"><span class="firstname">Angelika</span> <span class="surname">Langer</span>. </span><span class="publisher"><span class="publishername"> + C/C++ Users Journal + . </span></span></p></div><div class="biblioentry" title="The C++ Programming Language"><a id="id394630"></a><p><span class="title"><i>The C++ Programming Language</i>. </span><span class="author"><span class="firstname">Bjarne</span> <span class="surname">Stroustrup</span>. </span><span class="copyright">Copyright © 2000 . </span><span class="pagenums">19.4 Allocators. </span><span class="publisher"><span class="publishername"> Addison Wesley - . </span></span></p></div><div class="biblioentry" title="Yalloc: A Recycling C++ Allocator"><a id="id704594"></a><p><span class="title"><i>Yalloc: A Recycling C++ Allocator</i>. </span> - yenf - <span class="author"><span class="firstname">Felix</span> <span class="surname">Yen</span>. </span><span class="copyright">Copyright © . </span></p></div></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pairs.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="utilities.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="auto_ptr.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 10. Pairs </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> auto_ptr</td></tr></table></div></body></html> + . </span></span></p></div><div class="biblioentry" title="Yalloc: A Recycling C++ Allocator"><a id="id444446"></a><p><span class="title"><i>Yalloc: A Recycling C++ Allocator</i>. </span><span class="author"><span class="firstname">Felix</span> <span class="surname">Yen</span>. </span></p></div></div></div><div class="section" title="auto_ptr"><div class="titlepage"><div><div><h3 class="title"><a id="std.util.memory.auto_ptr"></a>auto_ptr</h3></div></div></div><div class="section" title="Limitations"><div class="titlepage"><div><div><h4 class="title"><a id="auto_ptr.limitations"></a>Limitations</h4></div></div></div><p>Explaining all of the fun and delicious things that can + happen with misuse of the <code class="classname">auto_ptr</code> class + template (called <acronym class="acronym">AP</acronym> here) would take some + time. Suffice it to say that the use of <acronym class="acronym">AP</acronym> + safely in the presence of copying has some subtleties. + </p><p> + The AP class is a really + nifty idea for a smart pointer, but it is one of the dumbest of + all the smart pointers -- and that's fine. + </p><p> + AP is not meant to be a supersmart solution to all resource + leaks everywhere. Neither is it meant to be an effective form + of garbage collection (although it can help, a little bit). + And it can <span class="emphasis"><em>not</em></span>be used for arrays! + </p><p> + <acronym class="acronym">AP</acronym> is meant to prevent nasty leaks in the + presence of exceptions. That's <span class="emphasis"><em>all</em></span>. This + code is AP-friendly: + </p><pre class="programlisting"> + // Not a recommend naming scheme, but good for web-based FAQs. + typedef std::auto_ptr<MyClass> APMC; + + extern function_taking_MyClass_pointer (MyClass*); + extern some_throwable_function (); + + void func (int data) + { + APMC ap (new MyClass(data)); + + some_throwable_function(); // this will throw an exception + + function_taking_MyClass_pointer (ap.get()); + } + </pre><p>When an exception gets thrown, the instance of MyClass that's + been created on the heap will be <code class="function">delete</code>'d as the stack is + unwound past <code class="function">func()</code>. + </p><p>Changing that code as follows is not <acronym class="acronym">AP</acronym>-friendly: + </p><pre class="programlisting"> + APMC ap (new MyClass[22]); + </pre><p>You will get the same problems as you would without the use + of <acronym class="acronym">AP</acronym>: + </p><pre class="programlisting"> + char* array = new char[10]; // array new... + ... + delete array; // ...but single-object delete + </pre><p> + AP cannot tell whether the pointer you've passed at creation points + to one or many things. If it points to many things, you are about + to die. AP is trivial to write, however, so you could write your + own <code class="code">auto_array_ptr</code> for that situation (in fact, this has + been done many times; check the mailing lists, Usenet, Boost, etc). + </p></div><div class="section" title="Use in Containers"><div class="titlepage"><div><div><h4 class="title"><a id="auto_ptr.using"></a>Use in Containers</h4></div></div></div><p> + </p><p>All of the <a class="link" href="containers.html" title="Chapter 9. Containers">containers</a> + described in the standard library require their contained types + to have, among other things, a copy constructor like this: + </p><pre class="programlisting"> + struct My_Type + { + My_Type (My_Type const&); + }; + </pre><p> + Note the const keyword; the object being copied shouldn't change. + The template class <code class="code">auto_ptr</code> (called AP here) does not + meet this requirement. Creating a new AP by copying an existing + one transfers ownership of the pointed-to object, which means that + the AP being copied must change, which in turn means that the + copy ctors of AP do not take const objects. + </p><p> + The resulting rule is simple: <span class="emphasis"><em>Never ever use a + container of auto_ptr objects</em></span>. The standard says that + <span class="quote">“<span class="quote">undefined</span>”</span> behavior is the result, but it is + guaranteed to be messy. + </p><p> + To prevent you from doing this to yourself, the + <a class="link" href="ext_compile_checks.html" title="Chapter 16. Compile Time Checks">concept checks</a> built + in to this implementation will issue an error if you try to + compile code like this: + </p><pre class="programlisting"> + #include <vector> + #include <memory> + + void f() + { + std::vector< std::auto_ptr<int> > vec_ap_int; + } + </pre><p> +Should you try this with the checks enabled, you will see an error. + </p></div></div><div class="section" title="shared_ptr"><div class="titlepage"><div><div><h3 class="title"><a id="std.util.memory.shared_ptr"></a>shared_ptr</h3></div></div></div><p> +The shared_ptr class template stores a pointer, usually obtained via new, +and implements shared ownership semantics. +</p><div class="section" title="Requirements"><div class="titlepage"><div><div><h4 class="title"><a id="shared_ptr.req"></a>Requirements</h4></div></div></div><p> + </p><p> + The standard deliberately doesn't require a reference-counted + implementation, allowing other techniques such as a + circular-linked-list. + </p><p> + At the time of writing the C++0x working paper doesn't mention how + threads affect shared_ptr, but it is likely to follow the existing + practice set by <code class="classname">boost::shared_ptr</code>. The + shared_ptr in libstdc++ is derived from Boost's, so the same rules + apply. + </p><p> + </p></div><div class="section" title="Design Issues"><div class="titlepage"><div><div><h4 class="title"><a id="shared_ptr.design_issues"></a>Design Issues</h4></div></div></div><p> +The <code class="classname">shared_ptr</code> code is kindly donated to GCC by the Boost +project and the original authors of the code. The basic design and +algorithms are from Boost, the notes below describe details specific to +the GCC implementation. Names have been uglified in this implementation, +but the design should be recognisable to anyone familiar with the Boost +1.32 shared_ptr. + </p><p> +The basic design is an abstract base class, <code class="code">_Sp_counted_base</code> that +does the reference-counting and calls virtual functions when the count +drops to zero. +Derived classes override those functions to destroy resources in a context +where the correct dynamic type is known. This is an application of the +technique known as type erasure. + </p></div><div class="section" title="Implementation"><div class="titlepage"><div><div><h4 class="title"><a id="shared_ptr.impl"></a>Implementation</h4></div></div></div><div class="section" title="Class Hierarchy"><div class="titlepage"><div><div><h5 class="title"><a id="id412343"></a>Class Hierarchy</h5></div></div></div><p> +A <code class="classname">shared_ptr<T></code> contains a pointer of +type <span class="type">T*</span> and an object of type +<code class="classname">__shared_count</code>. The shared_count contains a +pointer of type <span class="type">_Sp_counted_base*</span> which points to the +object that maintains the reference-counts and destroys the managed +resource. + </p><div class="variablelist"><dl><dt><span class="term"><code class="classname">_Sp_counted_base<Lp></code></span></dt><dd><p> +The base of the hierarchy is parameterized on the lock policy alone. +_Sp_counted_base doesn't depend on the type of pointer being managed, +it only maintains the reference counts and calls virtual functions when +the counts drop to zero. The managed object is destroyed when the last +strong reference is dropped, but the _Sp_counted_base itself must exist +until the last weak reference is dropped. + </p></dd><dt><span class="term"><code class="classname">_Sp_counted_base_impl<Ptr, Deleter, Lp></code></span></dt><dd><p> +Inherits from _Sp_counted_base and stores a pointer of type <span class="type">Ptr</span> +and a deleter of type <code class="code">Deleter</code>. <code class="code">_Sp_deleter</code> is +used when the user doesn't supply a custom deleter. Unlike Boost's, this +default deleter is not "checked" because GCC already issues a warning if +<code class="function">delete</code> is used with an incomplete type. +This is the only derived type used by <code class="classname">shared_ptr<Ptr></code> +and it is never used by <code class="classname">shared_ptr</code>, which uses one of +the following types, depending on how the shared_ptr is constructed. + </p></dd><dt><span class="term"><code class="classname">_Sp_counted_ptr<Ptr, Lp></code></span></dt><dd><p> +Inherits from _Sp_counted_base and stores a pointer of type <span class="type">Ptr</span>, +which is passed to <code class="function">delete</code> when the last reference is dropped. +This is the simplest form and is used when there is no custom deleter or +allocator. + </p></dd><dt><span class="term"><code class="classname">_Sp_counted_deleter<Ptr, Deleter, Alloc></code></span></dt><dd><p> +Inherits from _Sp_counted_ptr and adds support for custom deleter and +allocator. Empty Base Optimization is used for the allocator. This class +is used even when the user only provides a custom deleter, in which case +<code class="classname">allocator</code> is used as the allocator. + </p></dd><dt><span class="term"><code class="classname">_Sp_counted_ptr_inplace<Tp, Alloc, Lp></code></span></dt><dd><p> +Used by <code class="code">allocate_shared</code> and <code class="code">make_shared</code>. +Contains aligned storage to hold an object of type <span class="type">Tp</span>, +which is constructed in-place with placement <code class="function">new</code>. +Has a variadic template constructor allowing any number of arguments to +be forwarded to <span class="type">Tp</span>'s constructor. +Unlike the other <code class="classname">_Sp_counted_*</code> classes, this one is parameterized on the +type of object, not the type of pointer; this is purely a convenience +that simplifies the implementation slightly. + </p></dd></dl></div></div><div class="section" title="Thread Safety"><div class="titlepage"><div><div><h5 class="title"><a id="id401193"></a>Thread Safety</h5></div></div></div><p> +The interface of <code class="classname">tr1::shared_ptr</code> was extended for C++0x +with support for rvalue-references and the other features from +N2351. As with other libstdc++ headers shared by TR1 and C++0x, +boost_shared_ptr.h uses conditional compilation, based on the macros +<code class="constant">_GLIBCXX_INCLUDE_AS_CXX0X</code> and +<code class="constant">_GLIBCXX_INCLUDE_AS_TR1</code>, to enable and disable +features. + </p><p> +C++0x-only features are: rvalue-ref/move support, allocator support, +aliasing constructor, make_shared & allocate_shared. Additionally, +the constructors taking <code class="classname">auto_ptr</code> parameters are +deprecated in C++0x mode. + </p><p> +The +<a class="ulink" href="http://boost.org/libs/smart_ptr/shared_ptr.htm#ThreadSafety" target="_top">Thread +Safety</a> section of the Boost shared_ptr documentation says "shared_ptr +objects offer the same level of thread safety as built-in types." +The implementation must ensure that concurrent updates to separate shared_ptr +instances are correct even when those instances share a reference count e.g. +</p><pre class="programlisting"> +shared_ptr<A> a(new A); +shared_ptr<A> b(a); + +// Thread 1 // Thread 2 + a.reset(); b.reset(); +</pre><p> +The dynamically-allocated object must be destroyed by exactly one of the +threads. Weak references make things even more interesting. +The shared state used to implement shared_ptr must be transparent to the +user and invariants must be preserved at all times. +The key pieces of shared state are the strong and weak reference counts. +Updates to these need to be atomic and visible to all threads to ensure +correct cleanup of the managed resource (which is, after all, shared_ptr's +job!) +On multi-processor systems memory synchronisation may be needed so that +reference-count updates and the destruction of the managed resource are +race-free. +</p><p> +The function <code class="function">_Sp_counted_base::_M_add_ref_lock()</code>, called when +obtaining a shared_ptr from a weak_ptr, has to test if the managed +resource still exists and either increment the reference count or throw +<code class="classname">bad_weak_ptr</code>. +In a multi-threaded program there is a potential race condition if the last +reference is dropped (and the managed resource destroyed) between testing +the reference count and incrementing it, which could result in a shared_ptr +pointing to invalid memory. +</p><p> +The Boost shared_ptr (as used in GCC) features a clever lock-free +algorithm to avoid the race condition, but this relies on the +processor supporting an atomic <span class="emphasis"><em>Compare-And-Swap</em></span> +instruction. For other platforms there are fall-backs using mutex +locks. Boost (as of version 1.35) includes several different +implementations and the preprocessor selects one based on the +compiler, standard library, platform etc. For the version of +shared_ptr in libstdc++ the compiler and library are fixed, which +makes things much simpler: we have an atomic CAS or we don't, see Lock +Policy below for details. +</p></div><div class="section" title="Selecting Lock Policy"><div class="titlepage"><div><div><h5 class="title"><a id="id394558"></a>Selecting Lock Policy</h5></div></div></div><p> + </p><p> +There is a single <code class="classname">_Sp_counted_base</code> class, +which is a template parameterized on the enum +<span class="type">__gnu_cxx::_Lock_policy</span>. The entire family of classes is +parameterized on the lock policy, right up to +<code class="classname">__shared_ptr</code>, <code class="classname">__weak_ptr</code> and +<code class="classname">__enable_shared_from_this</code>. The actual +<code class="classname">std::shared_ptr</code> class inherits from +<code class="classname">__shared_ptr</code> with the lock policy parameter +selected automatically based on the thread model and platform that +libstdc++ is configured for, so that the best available template +specialization will be used. This design is necessary because it would +not be conforming for <code class="classname">shared_ptr</code> to have an +extra template parameter, even if it had a default value. The +available policies are: + </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p> + <span class="type">_S_Atomic</span> + </p><p> +Selected when GCC supports a builtin atomic compare-and-swap operation +on the target processor (see <a class="ulink" href="http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html" target="_top">Atomic +Builtins</a>.) The reference counts are maintained using a lock-free +algorithm and GCC's atomic builtins, which provide the required memory +synchronisation. + </p></li><li class="listitem"><p> + <span class="type">_S_Mutex</span> + </p><p> +The _Sp_counted_base specialization for this policy contains a mutex, +which is locked in add_ref_lock(). This policy is used when GCC's atomic +builtins aren't available so explicit memory barriers are needed in places. + </p></li><li class="listitem"><p> + <span class="type">_S_Single</span> + </p><p> +This policy uses a non-reentrant add_ref_lock() with no locking. It is +used when libstdc++ is built without <code class="literal">--enable-threads</code>. + </p></li></ol></div><p> + For all three policies, reference count increments and + decrements are done via the functions in + <code class="filename">ext/atomicity.h</code>, which detect if the program + is multi-threaded. If only one thread of execution exists in + the program then less expensive non-atomic operations are used. + </p></div><div class="section" title="Dual C++0x and TR1 Implementation"><div class="titlepage"><div><div><h5 class="title"><a id="id382919"></a>Dual C++0x and TR1 Implementation</h5></div></div></div><p> +The classes derived from <code class="classname">_Sp_counted_base</code> (see Class Hierarchy +below) and <code class="classname">__shared_count</code> are implemented separately for C++0x +and TR1, in <code class="filename">bits/boost_sp_shared_count.h</code> and +<code class="filename">tr1/boost_sp_shared_count.h</code> respectively. All other classes +including <code class="classname">_Sp_counted_base</code> are shared by both implementations. +</p><p> +The TR1 implementation is considered relatively stable, so is unlikely to +change unless bug fixes require it. If the code that is common to both +C++0x and TR1 modes needs to diverge further then it might be necessary to +duplicate additional classes and only make changes to the C++0x versions. +</p></div><div class="section" title="Related functions and classes"><div class="titlepage"><div><div><h5 class="title"><a id="id479578"></a>Related functions and classes</h5></div></div></div><div class="variablelist"><dl><dt><span class="term"><code class="code">dynamic_pointer_cast</code>, <code class="code">static_pointer_cast</code>, +<code class="code">const_pointer_cast</code></span></dt><dd><p> +As noted in N2351, these functions can be implemented non-intrusively using +the alias constructor. However the aliasing constructor is only available +in C++0x mode, so in TR1 mode these casts rely on three non-standard +constructors in shared_ptr and __shared_ptr. +In C++0x mode these constructors and the related tag types are not needed. + </p></dd><dt><span class="term"><code class="code">enable_shared_from_this</code></span></dt><dd><p> +The clever overload to detect a base class of type +<code class="code">enable_shared_from_this</code> comes straight from Boost. +There is an extra overload for <code class="code">__enable_shared_from_this</code> to +work smoothly with <code class="code">__shared_ptr<Tp, Lp></code> using any lock +policy. + </p></dd><dt><span class="term"><code class="code">make_shared</code>, <code class="code">allocate_shared</code></span></dt><dd><p> +<code class="code">make_shared</code> simply forwards to <code class="code">allocate_shared</code> +with <code class="code">std::allocator</code> as the allocator. +Although these functions can be implemented non-intrusively using the +alias constructor, if they have access to the implementation then it is +possible to save storage and reduce the number of heap allocations. The +newly constructed object and the _Sp_counted_* can be allocated in a single +block and the standard says implementations are "encouraged, but not required," +to do so. This implementation provides additional non-standard constructors +(selected with the type <code class="code">_Sp_make_shared_tag</code>) which create an +object of type <code class="code">_Sp_counted_ptr_inplace</code> to hold the new object. +The returned <code class="code">shared_ptr<A></code> needs to know the address of the +new <code class="code">A</code> object embedded in the <code class="code">_Sp_counted_ptr_inplace</code>, +but it has no way to access it. +This implementation uses a "covert channel" to return the address of the +embedded object when <code class="code">get_deleter<_Sp_make_shared_tag>()</code> +is called. Users should not try to use this. +As well as the extra constructors, this implementation also needs some +members of _Sp_counted_deleter to be protected where they could otherwise +be private. + </p></dd></dl></div></div></div><div class="section" title="Use"><div class="titlepage"><div><div><h4 class="title"><a id="shared_ptr.using"></a>Use</h4></div></div></div><div class="section" title="Examples"><div class="titlepage"><div><div><h5 class="title"><a id="id386678"></a>Examples</h5></div></div></div><p> + Examples of use can be found in the testsuite, under + <code class="filename">testsuite/tr1/2_general_utilities/shared_ptr</code>. + </p></div><div class="section" title="Unresolved Issues"><div class="titlepage"><div><div><h5 class="title"><a id="id386695"></a>Unresolved Issues</h5></div></div></div><p> + The resolution to C++ Standard Library issue <a class="ulink" href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#674" target="_top">674</a>, + "shared_ptr interface changes for consistency with N1856" will + need to be implemented after it is accepted into the working + paper. Issue <a class="ulink" href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#743" target="_top">743</a> + might also require changes. + </p><p> + The <span class="type">_S_single</span> policy uses atomics when used in MT + code, because it uses the same dispatcher functions that check + <code class="function">__gthread_active_p()</code>. This could be + addressed by providing template specialisations for some members + of <code class="classname">_Sp_counted_base<_S_single></code>. + </p><p> + Unlike Boost, this implementation does not use separate classes + for the pointer+deleter and pointer+deleter+allocator cases in + C++0x mode, combining both into _Sp_counted_deleter and using + <code class="classname">allocator</code> when the user doesn't specify + an allocator. If it was found to be beneficial an additional + class could easily be added. With the current implementation, + the _Sp_counted_deleter and __shared_count constructors taking a + custom deleter but no allocator are technically redundant and + could be removed, changing callers to always specify an + allocator. If a separate pointer+deleter class was added the + __shared_count constructor would be needed, so it has been kept + for now. + </p><p> + The hack used to get the address of the managed object from + <code class="function">_Sp_counted_ptr_inplace::_M_get_deleter()</code> + is accessible to users. This could be prevented if + <code class="function">get_deleter<_Sp_make_shared_tag>()</code> + always returned NULL, since the hack only needs to work at a + lower level, not in the public API. This wouldn't be difficult, + but hasn't been done since there is no danger of accidental + misuse: users already know they are relying on unsupported + features if they refer to implementation details such as + _Sp_make_shared_tag. + </p><p> + tr1::_Sp_deleter could be a private member of tr1::__shared_count but it + would alter the ABI. + </p><p> + Exposing the alias constructor in TR1 mode could simplify the + *_pointer_cast functions. Constructor could be private in TR1 + mode, with the cast functions as friends. + </p></div></div><div class="section" title="Acknowledgments"><div class="titlepage"><div><div><h4 class="title"><a id="shared_ptr.ack"></a>Acknowledgments</h4></div></div></div><p> + The original authors of the Boost shared_ptr, which is really nice + code to work with, Peter Dimov in particular for his help and + invaluable advice on thread safety. Phillip Jordan and Paolo + Carlini for the lock policy implementation. + </p></div><div class="bibliography" title="Bibliography"><div class="titlepage"><div><div><h4 class="title"><a id="shared_ptr.biblio"></a>Bibliography</h4></div></div></div><div class="biblioentry"><a id="id411149"></a><p><span class="biblioid"> + <a class="ulink" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2351.htm" target="_top"> + <em class="citetitle"> + Improving shared_ptr for C++0x, Revision 2 + </em> + </a> + . </span><span class="subtitle"> + N2351 + . </span></p></div><div class="biblioentry"><a id="id413900"></a><p><span class="biblioid"> + <a class="ulink" href="http://open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2456.html" target="_top"> + <em class="citetitle"> + C++ Standard Library Active Issues List + </em> + </a> + . </span><span class="subtitle"> + N2456 + . </span></p></div><div class="biblioentry"><a id="id470749"></a><p><span class="biblioid"> + <a class="ulink" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2461.pdf" target="_top"> + <em class="citetitle"> + Working Draft, Standard for Programming Language C++ + </em> + </a> + . </span><span class="subtitle"> + N2461 + . </span></p></div><div class="biblioentry"><a id="id470771"></a><p><span class="biblioid"> + <a class="ulink" href="http://boost.org/libs/smart_ptr/shared_ptr.htm" target="_top">shared_ptr + <em class="citetitle"> + Boost C++ Libraries documentation, shared_ptr + </em> + </a> + . </span><span class="subtitle"> + N2461 + . </span></p></div></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pairs.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="utilities.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="traits.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Pairs </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> Traits</td></tr></table></div></body></html> |