Merge from branches/gcc-4_8-branch up to rev 204657.
[official-gcc.git] / gcc-4_8-branch / libstdc++-v3 / doc / html / manual / mt_allocator_impl.html
blob7b3379aa1d4a222e82ebcdb2a0c7b5073a43f972
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!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>Implementation</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.78.1" /><meta name="keywords" content="ISO C++, allocator" /><meta name="keywords" content="ISO C++, library" /><meta name="keywords" content="ISO C++, runtime, library" /><link rel="home" href="../index.html" title="The GNU C++ Library" /><link rel="up" href="mt_allocator.html" title="Chapter 20. The mt_allocator" /><link rel="prev" href="mt_allocator_design.html" title="Design Issues" /><link rel="next" href="mt_allocator_ex_single.html" title="Single Thread Example" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Implementation</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="mt_allocator_design.html">Prev</a> </td><th width="60%" align="center">Chapter 20. The mt_allocator</th><td width="20%" align="right"> <a accesskey="n" href="mt_allocator_ex_single.html">Next</a></td></tr></table><hr /></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="allocator.mt.impl"></a>Implementation</h2></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="allocator.mt.tune"></a>Tunable Parameters</h3></div></div></div><p>Certain allocation parameters can be modified, or tuned. There
3 exists a nested <code class="code">struct __pool_base::_Tune</code> that contains all
4 these parameters, which include settings for
5 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>Alignment</p></li><li class="listitem"><p>Maximum bytes before calling <code class="code">::operator new</code> directly</p></li><li class="listitem"><p>Minimum bytes</p></li><li class="listitem"><p>Size of underlying global allocations</p></li><li class="listitem"><p>Maximum number of supported threads</p></li><li class="listitem"><p>Migration of deallocations to the global free list</p></li><li class="listitem"><p>Shunt for global <code class="code">new</code> and <code class="code">delete</code></p></li></ul></div><p>Adjusting parameters for a given instance of an allocator can only
6 happen before any allocations take place, when the allocator itself is
7 initialized. For instance:
8 </p><pre class="programlisting">
9 #include &lt;ext/mt_allocator.h&gt;
11 struct pod
13 int i;
14 int j;
17 int main()
19 typedef pod value_type;
20 typedef __gnu_cxx::__mt_alloc&lt;value_type&gt; allocator_type;
21 typedef __gnu_cxx::__pool_base::_Tune tune_type;
23 tune_type t_default;
24 tune_type t_opt(16, 5120, 32, 5120, 20, 10, false);
25 tune_type t_single(16, 5120, 32, 5120, 1, 10, false);
27 tune_type t;
28 t = allocator_type::_M_get_options();
29 allocator_type::_M_set_options(t_opt);
30 t = allocator_type::_M_get_options();
32 allocator_type a;
33 allocator_type::pointer p1 = a.allocate(128);
34 allocator_type::pointer p2 = a.allocate(5128);
36 a.deallocate(p1, 128);
37 a.deallocate(p2, 5128);
39 return 0;
41 </pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="allocator.mt.init"></a>Initialization</h3></div></div></div><p>
42 The static variables (pointers to freelists, tuning parameters etc)
43 are initialized as above, or are set to the global defaults.
44 </p><p>
45 The very first allocate() call will always call the
46 _S_initialize_once() function. In order to make sure that this
47 function is called exactly once we make use of a __gthread_once call
48 in MT applications and check a static bool (_S_init) in ST
49 applications.
50 </p><p>
51 The _S_initialize() function:
52 - If the GLIBCXX_FORCE_NEW environment variable is set, it sets the bool
53 _S_force_new to true and then returns. This will cause subsequent calls to
54 allocate() to return memory directly from a new() call, and deallocate will
55 only do a delete() call.
56 </p><p>
57 - If the GLIBCXX_FORCE_NEW environment variable is not set, both ST and MT
58 applications will:
59 - Calculate the number of bins needed. A bin is a specific power of two size
60 of bytes. I.e., by default the allocator will deal with requests of up to
61 128 bytes (or whatever the value of _S_max_bytes is when _S_init() is
62 called). This means that there will be bins of the following sizes
63 (in bytes): 1, 2, 4, 8, 16, 32, 64, 128.
65 - Create the _S_binmap array. All requests are rounded up to the next
66 "large enough" bin. I.e., a request for 29 bytes will cause a block from
67 the "32 byte bin" to be returned to the application. The purpose of
68 _S_binmap is to speed up the process of finding out which bin to use.
69 I.e., the value of _S_binmap[ 29 ] is initialized to 5 (bin 5 = 32 bytes).
70 </p><p>
71 - Create the _S_bin array. This array consists of bin_records. There will be
72 as many bin_records in this array as the number of bins that we calculated
73 earlier. I.e., if _S_max_bytes = 128 there will be 8 entries.
74 Each bin_record is then initialized:
75 - bin_record-&gt;first = An array of pointers to block_records. There will be
76 as many block_records pointers as there are maximum number of threads
77 (in a ST application there is only 1 thread, in a MT application there
78 are _S_max_threads).
79 This holds the pointer to the first free block for each thread in this
80 bin. I.e., if we would like to know where the first free block of size 32
81 for thread number 3 is we would look this up by: _S_bin[ 5 ].first[ 3 ]
83 The above created block_record pointers members are now initialized to
84 their initial values. I.e. _S_bin[ n ].first[ n ] = NULL;
85 </p><p>
86 - Additionally a MT application will:
87 - Create a list of free thread id's. The pointer to the first entry
88 is stored in _S_thread_freelist_first. The reason for this approach is
89 that the __gthread_self() call will not return a value that corresponds to
90 the maximum number of threads allowed but rather a process id number or
91 something else. So what we do is that we create a list of thread_records.
92 This list is _S_max_threads long and each entry holds a size_t thread_id
93 which is initialized to 1, 2, 3, 4, 5 and so on up to _S_max_threads.
94 Each time a thread calls allocate() or deallocate() we call
95 _S_get_thread_id() which looks at the value of _S_thread_key which is a
96 thread local storage pointer. If this is NULL we know that this is a newly
97 created thread and we pop the first entry from this list and saves the
98 pointer to this record in the _S_thread_key variable. The next time
99 we will get the pointer to the thread_record back and we use the
100 thread_record-&gt;thread_id as identification. I.e., the first thread that
101 calls allocate will get the first record in this list and thus be thread
102 number 1 and will then find the pointer to its first free 32 byte block
103 in _S_bin[ 5 ].first[ 1 ]
104 When we create the _S_thread_key we also define a destructor
105 (_S_thread_key_destr) which means that when the thread dies, this
106 thread_record is returned to the front of this list and the thread id
107 can then be reused if a new thread is created.
108 This list is protected by a mutex (_S_thread_freelist_mutex) which is only
109 locked when records are removed or added to the list.
110 </p><p>
111 - Initialize the free and used counters of each bin_record:
112 - bin_record-&gt;free = An array of size_t. This keeps track of the number
113 of blocks on a specific thread's freelist in each bin. I.e., if a thread
114 has 12 32-byte blocks on it's freelists and allocates one of these, this
115 counter would be decreased to 11.
117 - bin_record-&gt;used = An array of size_t. This keeps track of the number
118 of blocks currently in use of this size by this thread. I.e., if a thread
119 has made 678 requests (and no deallocations...) of 32-byte blocks this
120 counter will read 678.
122 The above created arrays are now initialized with their initial values.
123 I.e. _S_bin[ n ].free[ n ] = 0;
124 </p><p>
125 - Initialize the mutex of each bin_record: The bin_record-&gt;mutex
126 is used to protect the global freelist. This concept of a global
127 freelist is explained in more detail in the section "A multi
128 threaded example", but basically this mutex is locked whenever a
129 block of memory is retrieved or returned to the global freelist
130 for this specific bin. This only occurs when a number of blocks
131 are grabbed from the global list to a thread specific list or when
132 a thread decides to return some blocks to the global freelist.
133 </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="allocator.mt.deallocation"></a>Deallocation Notes</h3></div></div></div><p> Notes about deallocation. This allocator does not explicitly
134 release memory. Because of this, memory debugging programs like
135 valgrind or purify may notice leaks: sorry about this
136 inconvenience. Operating systems will reclaim allocated memory at
137 program termination anyway. If sidestepping this kind of noise is
138 desired, there are three options: use an allocator, like
139 <code class="code">new_allocator</code> that releases memory while debugging, use
140 GLIBCXX_FORCE_NEW to bypass the allocator's internal pools, or use a
141 custom pool datum that releases resources on destruction.
142 </p><p>
143 On systems with the function <code class="code">__cxa_atexit</code>, the
144 allocator can be forced to free all memory allocated before program
145 termination with the member function
146 <code class="code">__pool_type::_M_destroy</code>. However, because this member
147 function relies on the precise and exactly-conforming ordering of
148 static destructors, including those of a static local
149 <code class="code">__pool</code> object, it should not be used, ever, on systems
150 that don't have the necessary underlying support. In addition, in
151 practice, forcing deallocation can be tricky, as it requires the
152 <code class="code">__pool</code> object to be fully-constructed before the object
153 that uses it is fully constructed. For most (but not all) STL
154 containers, this works, as an instance of the allocator is constructed
155 as part of a container's constructor. However, this assumption is
156 implementation-specific, and subject to change. For an example of a
157 pool that frees memory, see the following
158 <a class="link" href="http://gcc.gnu.org/viewcvs/trunk/libstdc++-v3/testsuite/ext/mt_allocator/deallocate_local-6.cc?view=markup" target="_top">
159 example.</a>
160 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="mt_allocator_design.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="mt_allocator.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="mt_allocator_ex_single.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Design Issues </td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top"> Single Thread Example</td></tr></table></div></body></html>