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