BDE 4.39.x 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/// private:
88/// // NOT IMPLEMENTED
89/// my_SequentialPool(const my_SequentialPool&);
90/// my_SequentialPool& operator=(const my_SequentialPool&);
91///
92/// private:
93/// // PRIVATE MANIPULATORS
94/// int calculateNextSize(int size);
95/// // Return the next buffer size sufficient to satisfy a memory
96/// // allocation request of the specified 'size' (in bytes).
97///
98/// public:
99/// // CREATORS
100/// my_SequentialPool(bsls::BlockGrowth::Strategy strategy);
101/// // Create a pool with the specified memory block growth 'strategy'.
102///
103/// // ...
104///
105/// // MANIPULATORS
106/// void *allocate(int size);
107/// // Return the address of a contiguous block of memory of the
108/// // specified 'size' (in bytes). If the pool cannot return the
109/// // requested number of bytes, 'std::bad_alloc' will be thrown in an
110/// // exception-enabled build, or the program will be aborted. The
111/// // behavior is undefined unless 'size > 0'.
112/// };
113/// @endcode
114/// The implementation for the rest of the class is elided as the function
115/// `calculateNextSize` alone is sufficient to illustrate the use of this
116/// component:
117/// @code
118/// // PRIVATE MANIPULATORS
119/// int my_SequentialPool::calculateNextSize(int size)
120/// {
121/// if (bsls::BlockGrowth::BSLS_CONSTANT == d_growthStrategy) {
122/// return d_currentBufferSize;
123/// }
124/// @endcode
125/// Note that, if the growth strategy in effect is constant growth
126/// (`BSLS_CONSTANT`), the size of the internal buffers will always be the same.
127/// If `size` is greater than the buffer size, the implementation of `allocate`
128/// will return a block having the exact `size` from the internal block list:
129/// @code
130/// int nextSize = d_currentBufferSize;
131///
132/// do {
133/// nextSize *= 2; // growth factor of 2
134/// } while (nextSize < size);
135/// @endcode
136/// Note that, if the growth strategy in effect is geometric growth
137/// (`BSLS_GEOMETRIC`), the size of the internal buffer grows geometrically by a
138/// factor of 2:
139/// @code
140/// return nextSize;
141/// }
142/// @endcode
143/// @}
144/** @} */
145/** @} */
146
147/** @addtogroup bsl
148 * @{
149 */
150/** @addtogroup bsls
151 * @{
152 */
153/** @addtogroup bsls_blockgrowth
154 * @{
155 */
156
157
158
159namespace bsls {
160
161 // ==================
162 // struct BlockGrowth
163 // ==================
164
165/// This struct provides a namespace for memory block growth strategies for
166/// pools, allocators, containers, etc.
167///
168/// See @ref bsls_blockgrowth
170
171 // TYPES
172 enum Strategy {
173 BSLS_GEOMETRIC, // Default. Indicates that memory block sizes grow
174 // geometrically.
175
176 BSLS_CONSTANT // Indicates that memory block size is locked.
177 };
178
179 // CLASS METHODS
180
181 /// Return the string representation of the specified enumerator
182 /// `value`. The string representation of `value` matches its
183 /// corresponding enumerator name with the `BSLS_` prefix elided.
184 static const char *toAscii(BlockGrowth::Strategy value);
185};
186
187} // close package namespace
188
189#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
190// ============================================================================
191// BACKWARD COMPATIBILITY
192// ============================================================================
193
194/// This alias is defined for backward compatibility.
196#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
197
198
199
200#endif
201
202// ----------------------------------------------------------------------------
203// Copyright 2013 Bloomberg Finance L.P.
204//
205// Licensed under the Apache License, Version 2.0 (the "License");
206// you may not use this file except in compliance with the License.
207// You may obtain a copy of the License at
208//
209// http://www.apache.org/licenses/LICENSE-2.0
210//
211// Unless required by applicable law or agreed to in writing, software
212// distributed under the License is distributed on an "AS IS" BASIS,
213// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
214// See the License for the specific language governing permissions and
215// limitations under the License.
216// ----------------------------- END-OF-FILE ----------------------------------
217
218/** @} */
219/** @} */
220/** @} */
bsls::BlockGrowth bsls_BlockGrowth
This alias is defined for backward compatibility.
Definition bsls_blockgrowth.h:195
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlt_iso8601util.h:707
Definition bsls_blockgrowth.h:169
Strategy
Definition bsls_blockgrowth.h:172
@ BSLS_GEOMETRIC
Definition bsls_blockgrowth.h:173
static const char * toAscii(BlockGrowth::Strategy value)