BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsls_blockgrowth.h
Go to the documentation of this file.
1/// @file bsls_blockgrowth.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_blockgrowth.h -*-C++-*-
8#ifndef INCLUDED_BSLS_BLOCKGROWTH
9#define INCLUDED_BSLS_BLOCKGROWTH
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsls_blockgrowth bsls_blockgrowth
15/// @brief Provide a namespace for memory block growth strategies.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsls
19/// @{
20/// @addtogroup bsls_blockgrowth
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsls_blockgrowth-purpose"> Purpose</a>
25/// * <a href="#bsls_blockgrowth-classes"> Classes </a>
26/// * <a href="#bsls_blockgrowth-description"> Description </a>
27/// * <a href="#bsls_blockgrowth-block-growth-strategy"> Block Growth Strategy </a>
28/// * <a href="#bsls_blockgrowth-usage"> Usage </a>
29///
30/// # Purpose {#bsls_blockgrowth-purpose}
31/// Provide a namespace for memory block growth strategies.
32///
33/// # Classes {#bsls_blockgrowth-classes}
34///
35/// - bsls::BlockGrowth: namespace for enumerated growth strategy values
36///
37/// @see bsls_alignment
38///
39/// # Description {#bsls_blockgrowth-description}
40/// This component provides a namespace for enumerating memory
41/// block growth strategies, and provides a function that converts each of these
42/// enumerators to its corresponding string representation.
43///
44/// ## Block Growth Strategy {#bsls_blockgrowth-block-growth-strategy}
45///
46///
47/// This component supports two memory block growth strategies:
48///
49/// GEOMETRIC GROWTH: A container, pool or allocator that employs this
50/// strategy, as indicated by the enumerator `BSLS_GEOMETRIC`, grows its
51/// buffer geometrically.
52///
53/// CONSTANT GROWTH: A container, pool or allocator that employs this strategy,
54/// as indicated by the enumerator `BSLS_CONSTANT`, locks the buffer growth.
55/// The new buffer is always the same size as the current buffer.
56///
57/// ## Usage {#bsls_blockgrowth-usage}
58///
59///
60/// Memory block growth strategies are often used in memory managers and
61/// containers to control memory usage. First of all, suppose we have a
62/// `my_BlockList` class that manages a link list of memory blocks:
63/// @code
64/// class my_BlockList {
65/// // ...
66/// };
67/// @endcode
68/// We can then create a memory manager class `my_SequentialPool` that manages a
69/// pool of memory:
70/// @code
71/// class my_SequentialPool {
72/// // This class implements a memory pool that dispenses (heterogeneous)
73/// // blocks of memory (of varying, user-specified-sizes) from a sequence
74/// // of dynamically allocated buffers.
75///
76/// // DATA
77/// char *d_currentBuffer_p; // pointer to current buffer
78///
79/// int d_currentBufferSize; // size of current buffer
80///
81/// bsls::BlockGrowth::Strategy
82/// d_growthStrategy; // growth strategy
83///
84/// my_BlockList d_blockList; // manager for all allocated memory
85/// // blocks
86///
87/// // NOT IMPLEMENTED
88/// my_SequentialPool(const my_SequentialPool&);
89/// my_SequentialPool& operator=(const my_SequentialPool&);
90///
91/// private:
92/// // PRIVATE MANIPULATORS
93/// int calculateNextSize(int size);
94/// // Return the next buffer size sufficient to satisfy a memory
95/// // allocation request of the specified 'size' (in bytes).
96///
97/// public:
98/// // CREATORS
99/// my_SequentialPool(bsls::BlockGrowth::Strategy strategy);
100/// // Create a pool with the specified memory block growth 'strategy'.
101///
102/// // ...
103///
104/// // MANIPULATORS
105/// void *allocate(int size);
106/// // Return the address of a contiguous block of memory of the
107/// // specified 'size' (in bytes). If the pool cannot return the
108/// // requested number of bytes, 'std::bad_alloc' will be thrown in an
109/// // exception-enabled build, or the program will be aborted. The
110/// // behavior is undefined unless 'size > 0'.
111/// };
112/// @endcode
113/// The implementation for the rest of the class is elided as the function
114/// `calculateNextSize` alone is sufficient to illustrate the use of this
115/// component:
116/// @code
117/// // PRIVATE MANIPULATORS
118/// int my_SequentialPool::calculateNextSize(int size)
119/// {
120/// if (bsls::BlockGrowth::BSLS_CONSTANT == d_growthStrategy) {
121/// return d_currentBufferSize;
122/// }
123/// @endcode
124/// Note that, if the growth strategy in effect is constant growth
125/// (`BSLS_CONSTANT`), the size of the internal buffers will always be the same.
126/// If `size` is greater than the buffer size, the implementation of `allocate`
127/// will return a block having the exact `size` from the internal block list:
128/// @code
129/// int nextSize = d_currentBufferSize;
130///
131/// do {
132/// nextSize *= 2; // growth factor of 2
133/// } while (nextSize < size);
134/// @endcode
135/// Note that, if the growth strategy in effect is geometric growth
136/// (`BSLS_GEOMETRIC`), the size of the internal buffer grows geometrically by a
137/// factor of 2:
138/// @code
139/// return nextSize;
140/// }
141/// @endcode
142/// @}
143/** @} */
144/** @} */
145
146/** @addtogroup bsl
147 * @{
148 */
149/** @addtogroup bsls
150 * @{
151 */
152/** @addtogroup bsls_blockgrowth
153 * @{
154 */
155
156
157
158namespace bsls {
159
160 // ==================
161 // struct BlockGrowth
162 // ==================
163
164/// This struct provides a namespace for memory block growth strategies for
165/// pools, allocators, containers, etc.
167
168 // TYPES
169 enum Strategy {
170 BSLS_GEOMETRIC, // Default. Indicates that memory block sizes grow
171 // geometrically.
172
173 BSLS_CONSTANT // Indicates that memory block size is locked.
174 };
175
176 // CLASS METHODS
177
178 /// Return the string representation of the specified enumerator
179 /// `value`. The string representation of `value` matches its
180 /// corresponding enumerator name with the `BSLS_` prefix elided.
181 static const char *toAscii(BlockGrowth::Strategy value);
182};
183
184} // close package namespace
185
186#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
187// ============================================================================
188// BACKWARD COMPATIBILITY
189// ============================================================================
190
191/// This alias is defined for backward compatibility.
193#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
194
195
196
197#endif
198
199// ----------------------------------------------------------------------------
200// Copyright 2013 Bloomberg Finance L.P.
201//
202// Licensed under the Apache License, Version 2.0 (the "License");
203// you may not use this file except in compliance with the License.
204// You may obtain a copy of the License at
205//
206// http://www.apache.org/licenses/LICENSE-2.0
207//
208// Unless required by applicable law or agreed to in writing, software
209// distributed under the License is distributed on an "AS IS" BASIS,
210// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
211// See the License for the specific language governing permissions and
212// limitations under the License.
213// ----------------------------- END-OF-FILE ----------------------------------
214
215/** @} */
216/** @} */
217/** @} */
bsls::BlockGrowth bsls_BlockGrowth
This alias is defined for backward compatibility.
Definition bsls_blockgrowth.h:192
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlt_iso8601util.h:691
Definition bsls_blockgrowth.h:166
Strategy
Definition bsls_blockgrowth.h:169
@ BSLS_GEOMETRIC
Definition bsls_blockgrowth.h:170
static const char * toAscii(BlockGrowth::Strategy value)