BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlma_infrequentdeleteblocklist.h
Go to the documentation of this file.
1/// @file bdlma_infrequentdeleteblocklist.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlma_infrequentdeleteblocklist.h -*-C++-*-
8#ifndef INCLUDED_BDLMA_INFREQUENTDELETEBLOCKLIST
9#define INCLUDED_BDLMA_INFREQUENTDELETEBLOCKLIST
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlma_infrequentdeleteblocklist bdlma_infrequentdeleteblocklist
15/// @brief Provide allocation and management of infrequently deleted blocks.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlma
19/// @{
20/// @addtogroup bdlma_infrequentdeleteblocklist
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlma_infrequentdeleteblocklist-purpose"> Purpose</a>
25/// * <a href="#bdlma_infrequentdeleteblocklist-classes"> Classes </a>
26/// * <a href="#bdlma_infrequentdeleteblocklist-description"> Description </a>
27/// * <a href="#bdlma_infrequentdeleteblocklist-usage"> Usage </a>
28/// * <a href="#bdlma_infrequentdeleteblocklist-example-1-creating-a-memory-pools-for-strings"> Example 1: Creating a Memory Pools for Strings </a>
29///
30/// # Purpose {#bdlma_infrequentdeleteblocklist-purpose}
31/// Provide allocation and management of infrequently deleted blocks.
32///
33/// # Classes {#bdlma_infrequentdeleteblocklist-classes}
34///
35/// - bdlma::InfrequentDeleteBlockList: manager of infrequently deleted blocks
36///
37/// @see bdlma_blocklist
38///
39/// # Description {#bdlma_infrequentdeleteblocklist-description}
40/// This component implements a low-level memory manager,
41/// `bdlma::InfrequentDeleteBlockList`, that allocates and manages a sequence of
42/// memory blocks, each of a potentially different size as specified during the
43/// `allocate` method's invocation. The `release` method of a
44/// `bdlma::InfrequentDeleteBlockList` object deallocates the entire sequence of
45/// outstanding memory blocks, as does its destructor. Note that, in contrast
46/// to `bdlma::BlockList`, the `bdlma::InfrequentDeleteBlockList` class does
47/// *not* support the deallocation of individual items. In particular, although
48/// `bdlma::InfrequentDeleteBlockList` has a `deallocate` method, that method
49/// has no effect.
50///
51/// ## Usage {#bdlma_infrequentdeleteblocklist-usage}
52///
53///
54/// This section illustrates intended use of this component.
55///
56/// ### Example 1: Creating a Memory Pools for Strings {#bdlma_infrequentdeleteblocklist-example-1-creating-a-memory-pools-for-strings}
57///
58///
59/// A `bdlma::InfrequentDeleteBlockList` object is commonly used to supply
60/// memory to more elaborate memory managers that distribute parts of each
61/// (larger) allocated memory block supplied by the
62/// `bdlma::InfrequentDeleteBlockList` object. The `my_StrPool` memory pool
63/// manager shown below requests relatively large blocks of memory from its
64/// `bdlma::InfrequentDeleteBlockList` member object and distributes memory
65/// chunks of varying sizes from each block on demand:
66/// @code
67/// // my_strpool.h
68///
69/// class my_StrPool {
70///
71/// // DATA
72/// char *d_block_p; // current memory block
73///
74/// bsls::Types::size_type d_blockSize; // size of current memory block
75///
76/// bsls::Types::IntPtr d_cursor; // offset to next available byte
77/// // in block
78///
79/// bdlma::InfrequentDeleteBlockList
80/// d_blockList; // supplies managed memory blocks
81///
82/// private:
83/// // PRIVATE MANIPULATORS
84///
85/// // Request a memory block of at least the specified `numBytes` size
86/// // and allocate the initial `numBytes` from this block. Return the
87/// // address of the allocated memory. The behavior is undefined
88/// // unless `0 < numBytes`.
89/// void *allocateBlock(bsls::Types::size_type numBytes);
90///
91/// private:
92/// // NOT IMPLEMENTED
93/// my_StrPool(const my_StrPool&);
94/// my_StrPool& operator=(const my_StrPool&);
95///
96/// public:
97/// // CREATORS
98///
99/// /// Create a string pool. Optionally specify a `basicAllocator`
100/// /// used to supply memory. If `basicAllocator` is 0, the currently
101/// /// installed default allocator is used.
102/// explicit
103/// my_StrPool(bslma::Allocator *basicAllocator = 0);
104///
105/// /// Destroy this object and release all associated memory.
106/// ~my_StrPool();
107///
108/// // MANIPULATORS
109///
110/// /// Return the address of a contiguous block of memory of the
111/// /// specified `size` (in bytes). If `size` is 0, no memory is
112/// /// allocated and 0 is returned.
113/// void *allocate(bsls::Types::size_type size);
114///
115/// /// Release all memory currently allocated through this object.
116/// void release();
117/// };
118///
119/// // MANIPULATORS
120/// inline
121/// void my_StrPool::release()
122/// {
123/// d_blockList.release();
124/// d_block_p = 0;
125/// }
126///
127/// // ...
128///
129/// // my_strpool.cpp
130///
131/// enum {
132/// k_INITIAL_SIZE = 128, // initial block size
133///
134/// k_GROWTH_FACTOR = 2, // multiplicative factor by which to grow block
135///
136/// k_THRESHOLD = 128 // size beyond which an individual block may be
137/// // allocated if it doesn't fit in current block
138/// };
139///
140/// // PRIVATE MANIPULATORS
141/// void *my_StrPool::allocateBlock(bsls::Types::size_type numBytes)
142/// {
143/// assert(0 < numBytes);
144///
145/// if (k_THRESHOLD < numBytes) {
146/// // Allocate separate block if above threshold.
147///
148/// return reinterpret_cast<char *>(
149/// d_blockList.allocate(numBytes)); // RETURN
150/// }
151///
152/// if (d_block_p) {
153/// // Do not increase block size if no current block.
154///
155/// d_blockSize *= k_GROWTH_FACTOR;
156/// }
157/// d_block_p = reinterpret_cast<char*>(d_blockList.allocate(d_blockSize));
158/// d_cursor = numBytes;
159///
160/// return d_block_p;
161/// }
162///
163/// // CREATORS
164/// my_StrPool::my_StrPool(bslma::Allocator *basicAllocator)
165/// : d_block_p(0)
166/// , d_blockSize(k_INITIAL_SIZE)
167/// , d_cursor(0)
168/// , d_blockList(basicAllocator) // the blocklist knows about 'bslma_default'
169/// {
170/// }
171///
172/// my_StrPool::~my_StrPool()
173/// {
174/// assert(k_INITIAL_SIZE <= d_blockSize);
175/// assert(!d_block_p || (0 <= d_cursor && d_cursor <=
176/// static_cast<bsls::Types::IntPtr>(d_blockSize)));
177/// }
178///
179/// // MANIPULATORS
180/// void *my_StrPool::allocate(bsls::Types::size_type size)
181/// {
182/// if (0 == size) {
183/// return 0; // RETURN
184/// }
185///
186/// if (d_block_p && size + d_cursor <= d_blockSize) {
187/// char *p = d_block_p + d_cursor;
188/// d_cursor += size;
189/// return p; // RETURN
190/// }
191/// else {
192/// return allocateBlock(size); // RETURN
193/// }
194/// }
195/// @endcode
196/// In the code shown above, the `my_StrPool` memory manager allocates from its
197/// `bdlma::InfrequentDeleteBlockList` member object an initial memory block of
198/// size `k_INITIAL_SIZE`. This size is multiplied by `k_GROWTH_FACTOR` each
199/// time a depleted memory block is replaced by a newly-allocated block. The
200/// `allocate` method distributes memory from the current memory block
201/// piecemeal, except when the requested size (1) is not available in the
202/// current block, or (2) exceeds the `k_THRESHOLD`, in which case a separate
203/// memory block is allocated and returned. When the `my_StrPool` memory
204/// manager is destroyed, its `bdlma::InfrequentDeleteBlockList` member object
205/// is also destroyed, which in turn automatically deallocates all of its
206/// managed memory blocks.
207/// @}
208/** @} */
209/** @} */
210
211/** @addtogroup bdl
212 * @{
213 */
214/** @addtogroup bdlma
215 * @{
216 */
217/** @addtogroup bdlma_infrequentdeleteblocklist
218 * @{
219 */
220
221#include <bdlscm_version.h>
222
223#include <bslma_allocator.h>
224#include <bslma_default.h>
225
226#include <bsls_alignmentutil.h>
227#include <bsls_types.h>
228
229
230namespace bdlma {
231
232 // ===============================
233 // class InfrequentDeleteBlockList
234 // ===============================
235
236/// This class implements a low-level memory manager that allocates and
237/// manages a sequence of memory blocks -- each potentially of a different
238/// size as specified during the invocation of the `allocate` method. The
239/// `release` method deallocates the entire sequence of memory blocks, as does the destructor.
240///
241/// \note Note that memory blocks cannot be deallocated
242/// individually.
243///
244/// See @ref bdlma_infrequentdeleteblocklist
246
247 // PRIVATE TYPES
248
249 /// This `struct` overlays the beginning of each managed block of
250 /// allocated memory, implementing a singly-linked list of managed
251 /// blocks, and thereby enabling constant-time additions to the list of
252 /// blocks.
253 ///
254 /// See @ref bdlma_infrequentdeleteblocklist
255 struct Block {
256
257 Block *d_next_p; // next pointer
258 bsls::AlignmentUtil::MaxAlignedType d_memory; // force alignment
259 };
260
261 // DATA
262 Block *d_head_p; // address of 1st block of memory (or 0)
263 bslma::Allocator *d_allocator_p; // memory allocator (held, not owned)
264
265 private:
266 // NOT IMPLEMENTED
269
270 public:
271 // CREATORS
272
273 /// Create an empty block list suitable for managing memory blocks of
274 /// varying sizes. Optionally specify a `basicAllocator` used to supply
275 /// memory. If `basicAllocator` is 0, the currently installed default
276 /// allocator is used.
277 explicit
278 InfrequentDeleteBlockList(bslma::Allocator *basicAllocator = 0);
279
280 /// Destroy this object and deallocate all outstanding memory blocks
281 /// managed by this object.
283
284 // MANIPULATORS
285
286 /// Return the address of a contiguous block of memory of the specified
287 /// `size` (in bytes). If `size` is 0, no memory is allocated and 0 is
288 /// returned. The returned memory is guaranteed to be maximally
289 /// aligned.
291
292 /// This method has no effect on the memory block at the specified
293 /// `address` as all memory allocated by this object is managed.
294 ///
295 /// \pre The behavior is undefined unless `address` was allocated by this object,
296 /// and has not already been released.
297 void deallocate(void *address);
298
299 /// Deallocate all memory blocks managed by this object, returning this
300 /// object to its default-constructed state.
301 void release();
302
303 /// Deallocate all except the most-recently obtained block of the memory
304 /// blocks managed by this object. If no blocks are managed, this
305 /// method has no effect.
307
308 // ACCESSORS
309 // Aspects
310
311 /// Return the allocator used by this object to supply memory.
313};
314
315// ============================================================================
316// INLINE DEFINITIONS
317// ============================================================================
318
319 // -------------------------------
320 // class InfrequentDeleteBlockList
321 // -------------------------------
322
323// CREATORS
324inline
325InfrequentDeleteBlockList::InfrequentDeleteBlockList(
326 bslma::Allocator *basicAllocator)
327: d_head_p(0)
328, d_allocator_p(bslma::Default::allocator(basicAllocator))
329{
330}
331
332// MANIPULATORS
333inline
337
338// ACCESSORS
339 // Aspects
340
341inline
343{
344 return d_allocator_p;
345}
346
347} // close package namespace
348
349
350#endif
351
352// ----------------------------------------------------------------------------
353// Copyright 2020 Bloomberg Finance L.P.
354//
355// Licensed under the Apache License, Version 2.0 (the "License");
356// you may not use this file except in compliance with the License.
357// You may obtain a copy of the License at
358//
359// http://www.apache.org/licenses/LICENSE-2.0
360//
361// Unless required by applicable law or agreed to in writing, software
362// distributed under the License is distributed on an "AS IS" BASIS,
363// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
364// See the License for the specific language governing permissions and
365// limitations under the License.
366// ----------------------------- END-OF-FILE ----------------------------------
367
368/** @} */
369/** @} */
370/** @} */
Definition bdlma_infrequentdeleteblocklist.h:245
void * allocate(bsls::Types::size_type size)
void deallocate(void *address)
Definition bdlma_infrequentdeleteblocklist.h:334
bslma::Allocator * allocator() const
Return the allocator used by this object to supply memory.
Definition bdlma_infrequentdeleteblocklist.h:342
Definition bslma_allocator.h:545
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlma_alignedallocator.h:278
Definition baljsn_encoder_testtypes.h:76
AlignmentToType< BSLS_MAX_ALIGNMENT >::Type MaxAlignedType
Definition bsls_alignmentutil.h:307
std::size_t size_type
Definition bsls_types.h:126