BDE 4.14.0 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/// void *allocateBlock(bsls::Types::size_type numBytes);
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///
90/// private:
91/// // NOT IMPLEMENTED
92/// my_StrPool(const my_StrPool&);
93/// my_StrPool& operator=(const my_StrPool&);
94///
95/// public:
96/// // CREATORS
97/// explicit
98/// my_StrPool(bslma::Allocator *basicAllocator = 0);
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///
103/// ~my_StrPool();
104/// // Destroy this object and release all associated memory.
105///
106/// // MANIPULATORS
107/// void *allocate(bsls::Types::size_type size);
108/// // Return the address of a contiguous block of memory of the
109/// // specified 'size' (in bytes). If 'size' is 0, no memory is
110/// // allocated and 0 is returned.
111///
112/// void release();
113/// // Release all memory currently allocated through this object.
114/// };
115///
116/// // MANIPULATORS
117/// inline
118/// void my_StrPool::release()
119/// {
120/// d_blockList.release();
121/// d_block_p = 0;
122/// }
123///
124/// // ...
125///
126/// // my_strpool.cpp
127///
128/// enum {
129/// k_INITIAL_SIZE = 128, // initial block size
130///
131/// k_GROWTH_FACTOR = 2, // multiplicative factor by which to grow block
132///
133/// k_THRESHOLD = 128 // size beyond which an individual block may be
134/// // allocated if it doesn't fit in current block
135/// };
136///
137/// // PRIVATE MANIPULATORS
138/// void *my_StrPool::allocateBlock(bsls::Types::size_type numBytes)
139/// {
140/// assert(0 < numBytes);
141///
142/// if (k_THRESHOLD < numBytes) {
143/// // Allocate separate block if above threshold.
144///
145/// return reinterpret_cast<char *>(
146/// d_blockList.allocate(numBytes)); // RETURN
147/// }
148///
149/// if (d_block_p) {
150/// // Do not increase block size if no current block.
151///
152/// d_blockSize *= k_GROWTH_FACTOR;
153/// }
154/// d_block_p = reinterpret_cast<char*>(d_blockList.allocate(d_blockSize));
155/// d_cursor = numBytes;
156///
157/// return d_block_p;
158/// }
159///
160/// // CREATORS
161/// my_StrPool::my_StrPool(bslma::Allocator *basicAllocator)
162/// : d_block_p(0)
163/// , d_blockSize(k_INITIAL_SIZE)
164/// , d_cursor(0)
165/// , d_blockList(basicAllocator) // the blocklist knows about 'bslma_default'
166/// {
167/// }
168///
169/// my_StrPool::~my_StrPool()
170/// {
171/// assert(k_INITIAL_SIZE <= d_blockSize);
172/// assert(!d_block_p || (0 <= d_cursor && d_cursor <=
173/// static_cast<bsls::Types::IntPtr>(d_blockSize)));
174/// }
175///
176/// // MANIPULATORS
177/// void *my_StrPool::allocate(bsls::Types::size_type size)
178/// {
179/// if (0 == size) {
180/// return 0; // RETURN
181/// }
182///
183/// if (d_block_p && size + d_cursor <= d_blockSize) {
184/// char *p = d_block_p + d_cursor;
185/// d_cursor += size;
186/// return p; // RETURN
187/// }
188/// else {
189/// return allocateBlock(size); // RETURN
190/// }
191/// }
192/// @endcode
193/// In the code shown above, the `my_StrPool` memory manager allocates from its
194/// `bdlma::InfrequentDeleteBlockList` member object an initial memory block of
195/// size `k_INITIAL_SIZE`. This size is multiplied by `k_GROWTH_FACTOR` each
196/// time a depleted memory block is replaced by a newly-allocated block. The
197/// `allocate` method distributes memory from the current memory block
198/// piecemeal, except when the requested size (1) is not available in the
199/// current block, or (2) exceeds the `k_THRESHOLD`, in which case a separate
200/// memory block is allocated and returned. When the `my_StrPool` memory
201/// manager is destroyed, its `bdlma::InfrequentDeleteBlockList` member object
202/// is also destroyed, which in turn automatically deallocates all of its
203/// managed memory blocks.
204/// @}
205/** @} */
206/** @} */
207
208/** @addtogroup bdl
209 * @{
210 */
211/** @addtogroup bdlma
212 * @{
213 */
214/** @addtogroup bdlma_infrequentdeleteblocklist
215 * @{
216 */
217
218#include <bdlscm_version.h>
219
220#include <bslma_allocator.h>
221#include <bslma_default.h>
222
223#include <bsls_alignmentutil.h>
224#include <bsls_types.h>
225
226
227namespace bdlma {
228
229 // ===============================
230 // class InfrequentDeleteBlockList
231 // ===============================
232
233/// This class implements a low-level memory manager that allocates and
234/// manages a sequence of memory blocks -- each potentially of a different
235/// size as specified during the invocation of the `allocate` method. The
236/// `release` method deallocates the entire sequence of memory blocks, as
237/// does the destructor. Note that memory blocks cannot be deallocated
238/// individually.
239///
240/// See @ref bdlma_infrequentdeleteblocklist
242
243 // PRIVATE TYPES
244
245 /// This `struct` overlays the beginning of each managed block of
246 /// allocated memory, implementing a singly-linked list of managed
247 /// blocks, and thereby enabling constant-time additions to the list of
248 /// blocks.
249 struct Block {
250
251 Block *d_next_p; // next pointer
252 bsls::AlignmentUtil::MaxAlignedType d_memory; // force alignment
253 };
254
255 // DATA
256 Block *d_head_p; // address of 1st block of memory (or 0)
257 bslma::Allocator *d_allocator_p; // memory allocator (held, not owned)
258
259 private:
260 // NOT IMPLEMENTED
263
264 public:
265 // CREATORS
266
267 /// Create an empty block list suitable for managing memory blocks of
268 /// varying sizes. Optionally specify a `basicAllocator` used to supply
269 /// memory. If `basicAllocator` is 0, the currently installed default
270 /// allocator is used.
271 explicit
272 InfrequentDeleteBlockList(bslma::Allocator *basicAllocator = 0);
273
274 /// Destroy this object and deallocate all outstanding memory blocks
275 /// managed by this object.
277
278 // MANIPULATORS
279
280 /// Return the address of a contiguous block of memory of the specified
281 /// `size` (in bytes). If `size` is 0, no memory is allocated and 0 is
282 /// returned. The returned memory is guaranteed to be maximally
283 /// aligned.
285
286 /// This method has no effect on the memory block at the specified
287 /// `address` as all memory allocated by this object is managed. The
288 /// behavior is undefined unless `address` was allocated by this object,
289 /// and has not already been released.
290 void deallocate(void *address);
291
292 /// Deallocate all memory blocks managed by this object, returning this
293 /// object to its default-constructed state.
294 void release();
295
296 /// Deallocate all except the most-recently obtained block of the memory
297 /// blocks managed by this object. If no blocks are managed, this
298 /// method has no effect.
300
301 // ACCESSORS
302 // Aspects
303
304 /// Return the allocator used by this object to supply memory.
306};
307
308// ============================================================================
309// INLINE DEFINITIONS
310// ============================================================================
311
312 // -------------------------------
313 // class InfrequentDeleteBlockList
314 // -------------------------------
315
316// CREATORS
317inline
318InfrequentDeleteBlockList::InfrequentDeleteBlockList(
319 bslma::Allocator *basicAllocator)
320: d_head_p(0)
321, d_allocator_p(bslma::Default::allocator(basicAllocator))
322{
323}
324
325// MANIPULATORS
326inline
330
331// ACCESSORS
332 // Aspects
333
334inline
336{
337 return d_allocator_p;
338}
339
340} // close package namespace
341
342
343#endif
344
345// ----------------------------------------------------------------------------
346// Copyright 2020 Bloomberg Finance L.P.
347//
348// Licensed under the Apache License, Version 2.0 (the "License");
349// you may not use this file except in compliance with the License.
350// You may obtain a copy of the License at
351//
352// http://www.apache.org/licenses/LICENSE-2.0
353//
354// Unless required by applicable law or agreed to in writing, software
355// distributed under the License is distributed on an "AS IS" BASIS,
356// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
357// See the License for the specific language governing permissions and
358// limitations under the License.
359// ----------------------------- END-OF-FILE ----------------------------------
360
361/** @} */
362/** @} */
363/** @} */
Definition bdlma_infrequentdeleteblocklist.h:241
void * allocate(bsls::Types::size_type size)
void deallocate(void *address)
Definition bdlma_infrequentdeleteblocklist.h:327
bslma::Allocator * allocator() const
Return the allocator used by this object to supply memory.
Definition bdlma_infrequentdeleteblocklist.h:335
Definition bslma_allocator.h:457
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlma_alignedallocator.h:276
Definition balxml_encoderoptions.h:68
AlignmentToType< BSLS_MAX_ALIGNMENT >::Type MaxAlignedType
Definition bsls_alignmentutil.h:282
std::size_t size_type
Definition bsls_types.h:124