BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslma_infrequentdeleteblocklist.h
Go to the documentation of this file.
1/// @file bslma_infrequentdeleteblocklist.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslma_infrequentdeleteblocklist.h -*-C++-*-
8#ifndef INCLUDED_BSLMA_INFREQUENTDELETEBLOCKLIST
9#define INCLUDED_BSLMA_INFREQUENTDELETEBLOCKLIST
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslma_infrequentdeleteblocklist bslma_infrequentdeleteblocklist
15/// @brief Provide allocation and management of a sequence of memory blocks.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslma
19/// @{
20/// @addtogroup bslma_infrequentdeleteblocklist
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslma_infrequentdeleteblocklist-purpose"> Purpose</a>
25/// * <a href="#bslma_infrequentdeleteblocklist-classes"> Classes </a>
26/// * <a href="#bslma_infrequentdeleteblocklist-description"> Description </a>
27/// * <a href="#bslma_infrequentdeleteblocklist-usage"> Usage </a>
28///
29/// # Purpose {#bslma_infrequentdeleteblocklist-purpose}
30/// Provide allocation and management of a sequence of memory blocks.
31///
32/// @deprecated Use @ref bdlma_infrequentdeleteblocklist instead.
33///
34/// # Classes {#bslma_infrequentdeleteblocklist-classes}
35///
36/// - bslma::InfrequentDeleteBlockList: list of infrequently deleted blocks
37///
38/// @see bdlma_infrequentdeleteblocklist
39///
40/// # Description {#bslma_infrequentdeleteblocklist-description}
41/// This component implements a memory manager that allocates and
42/// manages a sequence of memory blocks, each potentially of a different size as
43/// specified in the `allocate` method's invocation. The
44/// `bslma::InfrequentDeleteBlockList` object's `release` method deallocates the
45/// entire sequence of memory blocks, as does its destructor.
46///
47/// This component does not allow individual items to be deallocated.
48///
49/// ## Usage {#bslma_infrequentdeleteblocklist-usage}
50///
51///
52/// The `bslma::InfrequentDeleteBlockList` object is commonly used to supply
53/// memory to more elaborate memory managers that distribute parts of a memory
54/// block supplied by the `bslma::InfrequentDeleteBlockList` object. The
55/// `my_StrPool` memory pool manager shown below requests relatively large
56/// blocks of memory from its `bslma::InfrequentDeleteBlockList` member object
57/// and distributes memory chunks of varying sizes from each block on demand:
58/// @code
59/// // my_strpool.h
60///
61/// class my_StrPool {
62/// int d_blockSize; // size of current memory block
63/// char *d_block_p; // current free memory block
64/// int d_cursor; // offset to address of next available byte
65/// bslma::InfrequentDeleteBlockList d_blockList;
66/// // supplies managed memory blocks
67///
68/// private:
69///
70/// /// Request a new memory block of at least the specified `numBytes`
71/// /// size and allocate the initial `numBytes` from this block.
72/// /// Return the address of the allocated memory.
73/// void *allocateBlock(int numBytes);
74///
75/// private: // not implemented
76/// my_StrPool(const my_StrPool&);
77/// my_StrPool& operator=(const my_StrPool&);
78///
79/// public:
80/// // CREATORS
81///
82/// /// Create a memory manager using the specified `basicAllocator` to
83/// /// supply memory. If `basicAllocator` is 0, the currently
84/// /// installed default allocator is used.
85/// my_StrPool(bslma::Allocator *basicAllocator = 0);
86///
87/// /// Destroy this object and release all associated memory.
88/// ~my_StrPool();
89///
90/// // MANIPULATORS
91///
92/// /// Allocate the specified `numBytes` of memory and return its
93/// /// address.
94/// void *allocate(int numBytes);
95///
96/// /// Release all memory currently allocated through this instance.
97/// void release();
98/// };
99///
100/// inline
101/// void *my_StrPool::allocate(int numBytes)
102/// {
103/// if (numBytes <= 0) {
104/// return 0;
105/// }
106/// if (d_block_p && numBytes + d_cursor <= d_blockSize) {
107/// char *p = d_block_p + d_cursor;
108/// d_cursor += numBytes;
109/// return p;
110/// }
111/// else {
112/// return allocateBlock(numBytes);
113/// }
114/// }
115///
116/// inline
117/// void my_StrPool::release()
118/// {
119/// d_blockList.release();
120/// d_block_p = 0;
121/// }
122///
123/// // ...
124///
125/// // my_strpool.cpp
126///
127/// enum {
128/// INITIAL_SIZE = 128, // initial block size
129/// GROW_FACTOR = 2, // multiplicative factor to grow block size
130/// THRESHOLD = 128 // Size beyond which an individual block may be
131/// // allocated if it does not fit in the current
132/// // block.
133/// };
134///
135/// void *my_StrPool::allocateBlock(int numBytes)
136/// {
137/// ASSERT(0 < numBytes);
138/// if (THRESHOLD < numBytes) { // Alloc separate block if above threshold.
139/// return (char *) d_blockList.allocate(numBytes);
140/// }
141/// else {
142/// if (d_block_p) { // Don't increase block size if no current block.
143/// d_blockSize *= GROW_FACTOR;
144/// }
145/// d_block_p = (char *) d_blockList.allocate(d_blockSize);
146/// d_cursor = numBytes;
147/// return d_block_p;
148/// }
149/// }
150///
151/// my_StrPool::my_StrPool(bslma::Allocator *basicAllocator)
152/// : d_blockSize(INITIAL_SIZE)
153/// , d_block_p(0)
154/// , d_blockList(basicAllocator)
155/// {
156/// }
157///
158/// my_StrPool::~my_StrPool()
159/// {
160/// assert(INITIAL_SIZE <= d_blockSize);
161/// assert(d_block_p || 0 <= d_cursor && d_cursor <= d_blockSize);
162/// }
163/// @endcode
164/// In the code above, the `my_StrPool` memory manager allocates from its
165/// `bslma::InfrequentDeleteBlockList` member object an initial memory block of
166/// size `INITIAL_SIZE`. This size is multiplied by `GROW_FACTOR` each time a
167/// deplete memory block is replaced by a newly allocated block. The `allocate`
168/// method distributes memory from the current memory block piecemeal, except
169/// when the requested size: 1) is not available in the current block and 2)
170/// exceeds the `THRESHOLD_SIZE`, in which case a separate memory block is
171/// allocated and returned. When the `my_StrPool` memory manager is destroyed,
172/// its `bslma::InfrequentDeleteBlockList` member object is also destroyed,
173/// which in turn automatically deallocates all of its managed memory blocks.
174/// @}
175/** @} */
176/** @} */
177
178/** @addtogroup bsl
179 * @{
180 */
181/** @addtogroup bslma
182 * @{
183 */
184/** @addtogroup bslma_infrequentdeleteblocklist
185 * @{
186 */
187
188#ifdef BDE_OPENSOURCE_PUBLICATION // DEPRECATED
189#error "bslma_infrequentdeleteblocklist is deprecated"
190#endif
191#include <bslscm_version.h>
192
193#include <bslma_default.h>
194
195#include <bsls_alignmentutil.h>
196
197
198
199
200namespace bslma {
201
202class Allocator;
203
204 // ===============================
205 // class InfrequentDeleteBlockList
206 // ===============================
207
208/// This class implements a memory manager that allocates and manages a
209/// sequence of memory blocks, each potentially of a different size as
210/// specified in the `allocate` method's invocation. This object's
211/// `release` method deallocates the entire sequence of memory blocks, as
212/// does its destructor. Note that memory blocks can not be deallocated
213/// individually.
214///
215/// See @ref bslma_infrequentdeleteblocklist
217
218 // TYPES
219 struct Block {
220 Block *d_next_p;
221 bsls::AlignmentUtil::MaxAlignedType d_memory; // force alignment
222 };
223
224 // DATA
225 Block *d_head_p; // refers to the first memory block
226 Allocator *d_allocator_p; // holds (but does not own) the allocator
227
228 // NOT IMPLEMENTED
231 operator=(const InfrequentDeleteBlockList&);
232
233 public:
234 // CREATORS
235
236 /// Create a memory manager. Optionally specify a `basicAllocator` used
237 /// to supply memory. If `basicAllocator` is 0, the currently installed
238 /// default allocator is used.
239 explicit InfrequentDeleteBlockList(Allocator *basicAllocator = 0);
240
241 /// Destroy this object and deallocate all memory blocks managed by this
242 /// object.
244
245 // MANIPULATORS
246
247 /// Allocate a memory block of the specified `numBytes` size and return
248 /// its address. The returned memory is guaranteed to be maximally
249 /// aligned. The behavior is undefined unless `0 <= numBytes`.
250 void *allocate(int numBytes);
251
252 /// This method has no effect.
253 void deallocate(void *address);
254
255 /// Deallocate all memory blocks managed by this object.
256 void release();
257};
258
259// ============================================================================
260// INLINE FUNCTION DEFINITIONS
261// ============================================================================
262
263 // -------------------------------
264 // class InfrequentDeleteBlockList
265 // -------------------------------
266
267// CREATORS
268inline
269InfrequentDeleteBlockList::
270 InfrequentDeleteBlockList(Allocator *basicAllocator)
271: d_head_p(0)
272, d_allocator_p(Default::allocator(basicAllocator))
273{
274}
275
276// MANIPULATORS
277inline
281
282} // close package namespace
283
284#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
285// ============================================================================
286// BACKWARD COMPATIBILITY
287// ============================================================================
288
289/// This alias is defined for backward compatibility.
291#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
292
293
294
295#endif
296
297// ----------------------------------------------------------------------------
298// Copyright 2013 Bloomberg Finance L.P.
299//
300// Licensed under the Apache License, Version 2.0 (the "License");
301// you may not use this file except in compliance with the License.
302// You may obtain a copy of the License at
303//
304// http://www.apache.org/licenses/LICENSE-2.0
305//
306// Unless required by applicable law or agreed to in writing, software
307// distributed under the License is distributed on an "AS IS" BASIS,
308// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
309// See the License for the specific language governing permissions and
310// limitations under the License.
311// ----------------------------- END-OF-FILE ----------------------------------
312
313/** @} */
314/** @} */
315/** @} */
Definition bslma_allocator.h:457
Definition bslma_infrequentdeleteblocklist.h:216
void deallocate(void *address)
This method has no effect.
Definition bslma_infrequentdeleteblocklist.h:278
void * allocate(int numBytes)
void release()
Deallocate all memory blocks managed by this object.
bslma::InfrequentDeleteBlockList bslma_InfrequentDeleteBlockList
This alias is defined for backward compatibility.
Definition bslma_infrequentdeleteblocklist.h:290
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balxml_encoderoptions.h:68
Definition bslma_default.h:740
AlignmentToType< BSLS_MAX_ALIGNMENT >::Type MaxAlignedType
Definition bsls_alignmentutil.h:282