*: Regenerate.
[gcc.git] / libstdc++-v3 / doc / html / manual / bk01pt03ch20s04.html
1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Single Thread Example</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><meta name="keywords" content="
2 ISO C++
3 ,
4 allocator
5 "><meta name="keywords" content="
6 ISO C++
7 ,
8 library
9 "><meta name="keywords" content="
10 ISO C++
11 ,
12 runtime
13 ,
14 library
15 "><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="bk01pt03ch20s03.html" title="Implementation"><link rel="next" href="bk01pt03ch20s05.html" title="Multiple Thread Example"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Single Thread Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="bk01pt03ch20s03.html">Prev</a> </td><th width="60%" align="center">Chapter 20. The mt_allocator</th><td width="20%" align="right"> <a accesskey="n" href="bk01pt03ch20s05.html">Next</a></td></tr></table><hr></div><div class="section" title="Single Thread Example"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="allocator.mt.example_single"></a>Single Thread Example</h2></div></div></div><p>
16 Let's start by describing how the data on a freelist is laid out in memory.
17 This is the first two blocks in freelist for thread id 3 in bin 3 (8 bytes):
18 </p><pre class="programlisting">
19 +----------------+
20 | next* ---------|--+ (_S_bin[ 3 ].first[ 3 ] points here)
21 | | |
22 | | |
23 | | |
24 +----------------+ |
25 | thread_id = 3 | |
26 | | |
27 | | |
28 | | |
29 +----------------+ |
30 | DATA | | (A pointer to here is what is returned to the
31 | | | the application when needed)
32 | | |
33 | | |
34 | | |
35 | | |
36 | | |
37 | | |
38 +----------------+ |
39 +----------------+ |
40 | next* |&lt;-+ (If next == NULL it's the last one on the list)
41 | |
42 | |
43 | |
44 +----------------+
45 | thread_id = 3 |
46 | |
47 | |
48 | |
49 +----------------+
50 | DATA |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 +----------------+
59 </pre><p>
60 With this in mind we simplify things a bit for a while and say that there is
61 only one thread (a ST application). In this case all operations are made to
62 what is referred to as the global pool - thread id 0 (No thread may be
63 assigned this id since they span from 1 to _S_max_threads in a MT application).
64 </p><p>
65 When the application requests memory (calling allocate()) we first look at the
66 requested size and if this is &gt; _S_max_bytes we call new() directly and return.
67 </p><p>
68 If the requested size is within limits we start by finding out from which
69 bin we should serve this request by looking in _S_binmap.
70 </p><p>
71 A quick look at _S_bin[ bin ].first[ 0 ] tells us if there are any blocks of
72 this size on the freelist (0). If this is not NULL - fine, just remove the
73 block that _S_bin[ bin ].first[ 0 ] points to from the list,
74 update _S_bin[ bin ].first[ 0 ] and return a pointer to that blocks data.
75 </p><p>
76 If the freelist is empty (the pointer is NULL) we must get memory from the
77 system and build us a freelist within this memory. All requests for new memory
78 is made in chunks of _S_chunk_size. Knowing the size of a block_record and
79 the bytes that this bin stores we then calculate how many blocks we can create
80 within this chunk, build the list, remove the first block, update the pointer
81 (_S_bin[ bin ].first[ 0 ]) and return a pointer to that blocks data.
82 </p><p>
83 Deallocation is equally simple; the pointer is casted back to a block_record
84 pointer, lookup which bin to use based on the size, add the block to the front
85 of the global freelist and update the pointer as needed
86 (_S_bin[ bin ].first[ 0 ]).
87 </p><p>
88 The decision to add deallocated blocks to the front of the freelist was made
89 after a set of performance measurements that showed that this is roughly 10%
90 faster than maintaining a set of "last pointers" as well.
91 </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="bk01pt03ch20s03.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="bk01pt03ch20s05.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Implementation </td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top"> Multiple Thread Example</td></tr></table></div></body></html>