File: interfaces.html

package info (click to toggle)
boost 1.34.1-14
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 116,412 kB
  • ctags: 259,566
  • sloc: cpp: 642,395; xml: 56,450; python: 17,612; ansic: 14,520; sh: 2,265; yacc: 858; perl: 481; makefile: 478; lex: 94; sql: 74; csh: 6
file content (153 lines) | stat: -rw-r--r-- 5,250 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
  <meta http-equiv="Content-Language" content="en-us">
  <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
  <link href="pool.css" rel="stylesheet" type="text/css">

  <title>Boost Pool Interfaces</title>
</head>

<body>
  <img src="../../../boost.png" width="276" height="86" alt="C++ Boost">

  <h1 align="center">Boost Pool Interfaces</h1>

  <h2>Introduction</h2>

  <p>There are several interfaces provided which allow users great flexibility 
  in how they want to use Pools. Review the <a href=
  "concepts.html">concepts document</a> to get the basic understanding of how 
  Pools work.</p>

  <h2>Terminology and Tradeoffs</h2>

  <h3>Object Usage vs. Singleton Usage</h3>

  <p><em>Object Usage</em> is the method where each Pool is an object that may 
  be created and destroyed. Destroying a Pool implicitly frees all chunks that 
  have been allocated from it.</p>

  <p><em>Singleton Usage</em> is the method where each Pool is an object with 
  static duration; that is, it will not be destroyed until program exit. Pool 
  objects with Singleton Usage may be shared; thus, Singleton Usage implies 
  thread-safety as well. System memory allocated by Pool objects with 
  Singleton Usage may be freed through <span class=
  "code">release_memory</span> or <span class="code">purge_memory</span>.</p>

  <h3>Out-of-Memory Conditions: Exceptions vs. Null Return</h3>

  <p>Some Pool interfaces throw exceptions when out-of-memory; others will 
  return 0. In general, unless mandated by the Standard, Pool interfaces will 
  always prefer to return 0 instead of throw an exception.</p>

  <h2>The Interfaces</h2>

  <h3>pool</h3>

  <p>The <a href="interfaces/pool.html">pool interface</a> is a simple Object 
  Usage interface with Null Return.</p>

  <p>Example:</p>
  <pre class="code">
void func()
{
  boost::pool&lt;&gt; p(sizeof(int));
  for (int i = 0; i &lt; 10000; ++i)
  {
    int * const t = p.malloc();
    ... // Do something with t; don't take the time to free() it
  }
} // on function exit, p is destroyed, and all malloc()'ed ints are implicitly freed
</pre>

  <h3>object_pool</h3>

  <p>The <a href="interfaces/object_pool.html">object_pool interface</a> is an 
  Object Usage interface with Null Return, but is aware of the type of the 
  object for which it is allocating chunks. On destruction, any chunks that 
  have been allocated from that object_pool will have their destructors 
  called.</p>

  <p>Example:</p>
  <pre class="code">
struct X { ... }; // has destructor with side-effects

void func()
{
  boost::object_pool&lt;X&gt; p;
  for (int i = 0; i &lt; 10000; ++i)
  {
    X * const t = p.malloc();
    ... // Do something with t; don't take the time to free() it
  }
} // on function exit, p is destroyed, and all destructors for the X objects are called
</pre>

  <h3>singleton_pool</h3>

  <p>The <a href="interfaces/singleton_pool.html">singleton_pool interface</a> 
  is a Singleton Usage interface with Null Return. It's just the same as the 
  pool interface but with Singleton Usage instead.</p>

  <p>Example:</p>
  <pre class="code">
struct MyPoolTag { };

typedef boost::singleton_pool&lt;MyPoolTag, sizeof(int)&gt; my_pool;
void func()
{
  for (int i = 0; i &lt; 10000; ++i)
  {
    int * const t = my_pool::malloc();
    ... // Do something with t; don't take the time to free() it
  }
  // Explicitly free all malloc()'ed int's
  my_pool::purge_memory();
}
</pre>

  <h3>pool_alloc</h3>

  <p>The <a href="interfaces/pool_alloc.html">pool_alloc interface</a> is a 
  Singleton Usage interface with Exceptions. It is built on the singleton_pool 
  interface, and provides a Standard Allocator-compliant class (for use in 
  containers, etc.).</p>

  <p>Example:</p>
  <pre class="code">
void func()
{
  std::vector&lt;int, boost::pool_allocator&lt;int&gt; &gt; v;
  for (int i = 0; i &lt; 10000; ++i)
    v.push_back(13);
} // Exiting the function does NOT free the system memory allocated by the pool allocator
  // You must call
  //  boost::singleton_pool&lt;boost::pool_allocator_tag, sizeof(int)&gt;::release_memory()
  // in order to force that
</pre>

  <h2>Future Directions</h2>

  <p>Another pool interface will be written: a base class for per-class pool 
  allocation. This &quot;pool_base&quot; interface will be Singleton Usage with 
  Exceptions, and built on the singleton_pool interface.</p>
  <hr>

  <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
  "http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01 Transitional"
  height="31" width="88"></a></p>

  <p>Revised 
  <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->05 December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38516" --></p>

  <p><i>Copyright &copy; 2000, 2001 Stephen Cleary (scleary AT jerviswebb DOT com)</i></p>

  <p><i>Distributed under the Boost Software License, Version 1.0. (See 
  accompanying file <a href="../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or 
  copy at <a href=
  "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
</body>
</html>