File: singleton_pool.html

package info (click to toggle)
boost 1.27.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,908 kB
  • ctags: 26,546
  • sloc: cpp: 122,225; ansic: 10,956; python: 4,412; sh: 855; yacc: 803; makefile: 257; perl: 165; lex: 90; csh: 6
file content (157 lines) | stat: -rw-r--r-- 6,566 bytes parent folder | download
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
154
155
156
157
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<HTML>
<HEAD>
<TITLE>Singleton Pool</TITLE>
<LINK HREF="../pool.css" REL="stylesheet" TYPE="text/css">
</HEAD>
<BODY>

<IMG SRC="../../../../c++boost.gif" WIDTH=276 HEIGHT=86 ALT="C++ Boost">

<H1 ALIGN=CENTER>Singleton Pool</H1>

<P>
<H2>Introduction</H2>

<P>
singleton_pool.hpp provides a template class <SPAN CLASS="code">singleton_pool</SPAN>, which provides access to a <SPAN CLASS="code">pool</SPAN> as a singleton object.  For information on other pool-based interfaces, see <A HREF="../interfaces.html">the other pool interfaces</A>.

<P>
<H2>Synopsis</H2>

<PRE CLASS="code">template &lt;typename Tag, unsigned RequestedSize,
    typename UserAllocator = default_user_allocator_new_delete&gt;
struct singleton_pool
{
  public:
    typedef Tag tag;
    typedef UserAllocator user_allocator;
    typedef typename pool&lt;UserAllocator&gt;::size_type size_type;
    typedef typename pool&lt;UserAllocator&gt;::difference_type difference_type;

    static const unsigned requested_size = RequestedSize;

  private:
    static pool&lt;size_type&gt; p; // exposition only!

    singleton_pool();

  public:
    static bool is_from(void * ptr);

    static void * malloc();
    static void * ordered_malloc();
    static void * ordered_malloc(size_type n);

    static void free(void * ptr);
    static void ordered_free(void * ptr);
    static void free(void * ptr, std::size_t n);
    static void ordered_free(void * ptr, size_type n);

    static bool release_memory();
    static bool purge_memory();
};</PRE>

<P>
<H2>Notes</H2>

<P>
The underlying pool <SPAN CLASS="code">p</SPAN> referenced by the static functions in <SPAN CLASS="code">singleton_pool</SPAN> is actually declared in a way that it is:
<UL>
<LI>Thread-safe if there is only one thread running before main() begins and after main() ends -- all of the static functions of <SPAN CLASS="code">singleton_pool</SPAN> synchronize their access to <SPAN CLASS="code">p</SPAN>.</LI>
<LI>Guaranteed to be constructed before it is used -- thus, the simple static object in the synopsis above would actually be an incorrect implementation.  The actual <A HREF="../implementation/singleton_pool.html">implementation</A> to guarantee this is considerably more complicated.
</UL>

<P>
Note that a different underlying pool <SPAN CLASS="code">p</SPAN> exists for each different set of template parameters, including <A HREF="../implementation/singleton_pool.html">implementation-specific ones</A>.

<P>
<H2>Template Parameters</H2>

<P>
<H3>Tag</H3>

<P>
The <SPAN CLASS="code">Tag</SPAN> template parameter allows different unbounded sets of singleton pools to exist.  For example, the <A HREF="pool_alloc.html">pool allocators</A> use two tag classes to ensure that the two different allocator types never share the same underlying singleton pool.

<P>
<SPAN CLASS="code">Tag</SPAN> is never actually used by <SPAN CLASS="code">singleton_pool</SPAN>.

<P>
<H3>RequestedSize</H3>

<P>
The requested size of memory chunks to allocate.  This is passed as a constructor parameter to the underlying <SPAN CLASS="code">pool</SPAN>.  Must be greater than 0.

<P>
<H3>UserAllocator</H3>

<P>
Defines the method that the underlying <SPAN CLASS="code">pool</SPAN> will use to allocate memory from the system.  See <A HREF="user_allocator.html">User Allocators</A> for details.

<P>
<H2>Semantics</H2>

<P>
<TABLE BORDER ALIGN=CENTER>
<CAPTION><EM>Symbol Table</EM></CAPTION>
<TR><TH>Symbol<TH>Meaning
<TR><TD CLASS="code">SingletonPool<TD CLASS="code">singleton_pool&lt;Tag, RequestedSize, UserAllocator&gt;
<TR><TD CLASS="code">chunk<TD>value of type <SPAN CLASS="code">void *</SPAN>
<TR><TD CLASS="code">n<TD>value of type <SPAN CLASS="code">size_type</SPAN>
</TABLE>

<P>
<TABLE BORDER ALIGN=CENTER>
<CAPTION><EM>Typedefs/Static Const Values</EM></CAPTION>
<TR><TH>Expression<TH>Type/Value
<TR><TD CLASS="code">SingletonPool::tag<TD CLASS="code">Tag
<TR><TD CLASS="code">SingletonPool::user_allocator<TD CLASS="code">UserAllocator
<TR><TD CLASS="code">SingletonPool::size_type<TD CLASS="code">pool&lt;UserAllocator&gt;::size_type
<TR><TD CLASS="code">SingletonPool::difference_type<TD CLASS="code">pool&lt;UserAllocator&gt;::difference_type
<TR><TD CLASS="code">SingletonPool::requested_size<TD CLASS="code">RequestedSize
</TABLE>

<P>
<TABLE BORDER ALIGN=CENTER>
<CAPTION><EM>Functions</EM></CAPTION>
<TR><TH>Expression<TH>Return Type<TH>Semantic Equivalent
<TR><TD CLASS="code">SingletonPool::is_from(chunk)<TD CLASS="code">bool<TD><SPAN CLASS="code">SingletonPool::p.is_from(chunk);</SPAN> synchronized
<TR><TD CLASS="code">SingletonPool::malloc()<TD CLASS="code">void *<TD><SPAN CLASS="code">SingletonPool::p.malloc();</SPAN> synchronized
<TR><TD CLASS="code">SingletonPool::ordered_malloc(n)<TD CLASS="code">void *<TD><SPAN CLASS="code">SingletonPool::p.ordered_malloc(n);</SPAN> synchronized
<TR><TD CLASS="code">SingletonPool::free(chunk)<TD CLASS="code">void<TD><SPAN CLASS="code">SingletonPool::p.free(chunk);</SPAN> synchronized
<TR><TD CLASS="code">SingletonPool::ordered_free(chunk)<TD CLASS="code">void<TD><SPAN CLASS="code">SingletonPool::p.ordered_free(chunk);</SPAN> synchronized
<TR><TD CLASS="code">SingletonPool::free(chunk, n)<TD CLASS="code">void<TD><SPAN CLASS="code">SingletonPool::p.free(chunk, n);</SPAN> synchronized
<TR><TD CLASS="code">SingletonPool::ordered_free(chunk, n)<TD CLASS="code">void<TD><SPAN CLASS="code">SingletonPool::p.ordered_free(chunk, n);</SPAN> synchronized
<TR><TD CLASS="code">SingletonPool::release_memory()<TD CLASS="code">bool<TD><SPAN CLASS="code">SingletonPool::p.release_memory();</SPAN> synchronized
<TR><TD CLASS="code">SingletonPool::purge_memory()<TD CLASS="code">bool<TD><SPAN CLASS="code">SingletonPool::p.purge_memory();</SPAN> synchronized
</TABLE>

<P>
For more information on the semantics of these functions, see the <A HREF="pool.html">pool interface</A>.

<P>
<H2>Symbols</H2>

<P>
<UL>
<LI>boost::singleton_pool</LI>
</UL>

<P>
<H2><A HREF="../implementation/singleton_pool.html">Implementation Details</A></H2>

<P>
<HR>

<P>
Copyright &copy; 2000, 2001 Stephen Cleary (<A HREF="mailto:shammah@voyager.net">shammah@voyager.net</A>)

<P>
This file can be redistributed and/or modified under the terms found in <A HREF="../copyright.html">copyright.html</A>

<P>
This software and its documentation is provided &quot;as is&quot; without express or implied warranty, and with no claim as to its suitability for any purpose.

</BODY>
</HTML>