BDE 4.39.x 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 does its destructor.
212///
213/// \note Note that memory blocks can not be deallocated
214/// individually.
215///
216/// See @ref bslma_infrequentdeleteblocklist
218
219 // TYPES
220 struct Block {
221 Block *d_next_p;
222 bsls::AlignmentUtil::MaxAlignedType d_memory; // force alignment
223 };
224
225 // DATA
226 Block *d_head_p; // refers to the first memory block
227 Allocator *d_allocator_p; // holds (but does not own) the allocator
228
229 private:
230 // NOT IMPLEMENTED
233 operator=(const InfrequentDeleteBlockList&);
234
235 public:
236 // CREATORS
237
238 /// Create a memory manager. Optionally specify a `basicAllocator` used
239 /// to supply memory. If `basicAllocator` is 0, the currently installed
240 /// default allocator is used.
241 explicit InfrequentDeleteBlockList(Allocator *basicAllocator = 0);
242
243 /// Destroy this object and deallocate all memory blocks managed by this
244 /// object.
246
247 // MANIPULATORS
248
249 /// Allocate a memory block of the specified `numBytes` size and return
250 /// its address. The returned memory is guaranteed to be maximally aligned.
251 ///
252 /// \pre The behavior is undefined unless `0 <= numBytes`.
253 void *allocate(int numBytes);
254
255 /// This method has no effect.
256 void deallocate(void *address);
257
258 /// Deallocate all memory blocks managed by this object.
259 void release();
260};
261
262// ============================================================================
263// INLINE FUNCTION DEFINITIONS
264// ============================================================================
265
266 // -------------------------------
267 // class InfrequentDeleteBlockList
268 // -------------------------------
269
270// CREATORS
271inline
272InfrequentDeleteBlockList::
273 InfrequentDeleteBlockList(Allocator *basicAllocator)
274: d_head_p(0)
275, d_allocator_p(Default::allocator(basicAllocator))
276{
277}
278
279// MANIPULATORS
280inline
284
285} // close package namespace
286
287#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
288// ============================================================================
289// BACKWARD COMPATIBILITY
290// ============================================================================
291
292/// This alias is defined for backward compatibility.
294#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
295
296
297
298#endif
299
300// ----------------------------------------------------------------------------
301// Copyright 2013 Bloomberg Finance L.P.
302//
303// Licensed under the Apache License, Version 2.0 (the "License");
304// you may not use this file except in compliance with the License.
305// You may obtain a copy of the License at
306//
307// http://www.apache.org/licenses/LICENSE-2.0
308//
309// Unless required by applicable law or agreed to in writing, software
310// distributed under the License is distributed on an "AS IS" BASIS,
311// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
312// See the License for the specific language governing permissions and
313// limitations under the License.
314// ----------------------------- END-OF-FILE ----------------------------------
315
316/** @} */
317/** @} */
318/** @} */
Definition bslma_allocator.h:545
Definition bslma_infrequentdeleteblocklist.h:217
void deallocate(void *address)
This method has no effect.
Definition bslma_infrequentdeleteblocklist.h:281
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:293
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition baljsn_encoder_testtypes.h:76
Definition bslma_default.h:745
AlignmentToType< BSLS_MAX_ALIGNMENT >::Type MaxAlignedType
Definition bsls_alignmentutil.h:307