BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlbb_pooledblobbufferfactory.h
Go to the documentation of this file.
1/// @file bdlbb_pooledblobbufferfactory.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlbb_pooledblobbufferfactory.h -*-C++-*-
8#ifndef INCLUDED_BDLBB_POOLEDBLOBBUFFERFACTORY
9#define INCLUDED_BDLBB_POOLEDBLOBBUFFERFACTORY
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlbb_pooledblobbufferfactory bdlbb_pooledblobbufferfactory
15/// @brief Provide a concrete implementation of `bdlbb::BlobBufferFactory`.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlbb
19/// @{
20/// @addtogroup bdlbb_pooledblobbufferfactory
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlbb_pooledblobbufferfactory-purpose"> Purpose</a>
25/// * <a href="#bdlbb_pooledblobbufferfactory-classes"> Classes </a>
26/// * <a href="#bdlbb_pooledblobbufferfactory-description"> Description </a>
27/// * <a href="#bdlbb_pooledblobbufferfactory-thread-safety"> Thread Safety </a>
28/// * <a href="#bdlbb_pooledblobbufferfactory-potential-lifetime-issues"> Potential Lifetime Issues </a>
29///
30/// # Purpose {#bdlbb_pooledblobbufferfactory-purpose}
31/// Provide a concrete implementation of `bdlbb::BlobBufferFactory`.
32///
33/// # Classes {#bdlbb_pooledblobbufferfactory-classes}
34///
35/// - bdlbb::PooledBlobBufferFactory: mechanism pooling `bdlbb::BlobBuffer`s
36///
37/// @see bdlbb_blob, bdlma_concurrentpool
38///
39/// # Description {#bdlbb_pooledblobbufferfactory-description}
40/// This component provides a mechanism for allocating
41/// `bdlbb::BlobBuffer` objects of a fixed specified size. The size is passed
42/// at construction of the `bdlbb::PooledBlobBufferFactory` instance. A
43/// `bdlbb::BlobBuffer` is basically a shared pointer to a buffer of `char` of
44/// the prescribed size. Thus it is generally more efficient to create them
45/// with a uniform size, for the same reason as a pool is more efficient than a
46/// general-purpose memory allocator. In order to gain further efficiency, this
47/// factory allocates the shared pointer representation together with the buffer
48/// (contiguously).
49///
50/// ## Thread Safety {#bdlbb_pooledblobbufferfactory-thread-safety}
51///
52///
53/// The `bdlbb::PooledBlobBufferFactory` class is *fully thread-safe* (i.e., all
54/// non-creator methods can correctly execute concurrently). See
55/// @ref bsldoc_glossary for complete definitions of *fully thread-safe*.
56///
57/// It is safe to access and modify factory from multiple threads. However, it
58/// is *not* safe to allocate and load a new buffer into the same `BlobBuffer`
59/// object simultaneously from multiple threads.
60///
61/// ## Potential Lifetime Issues {#bdlbb_pooledblobbufferfactory-potential-lifetime-issues}
62///
63///
64/// Be aware that the destruction of a `bdlbb::PooledBlobBufferFactory` object
65/// releases all the `BlobBuffer` objects allocated by that factory. A common
66/// misconception is that, because of the use of `shared_ptr`, the data referred
67/// to in `BlobBuffer` objects created will remain valid until the last shared
68/// reference is destroyed, even if a reference outlives the
69/// `PooledBlobBufferFactory` that created it. This is *not* the case.
70/// Destroying a `PooledBlobBufferFactory` releases any memory created by that
71/// pool, even if shared references remain to the data, and it is therefore
72/// undefined behavior to use any `BlobBuffer` created by a pool after that pool
73/// is destroyed. A user must clearly understand the lifetime of memory
74/// allocated by a subsystem, and scope the lifetime of the
75/// `PooledBlobBufferFactory` to be greater than the active lifetime of that
76/// subsystem.
77/// @}
78/** @} */
79/** @} */
80
81/** @addtogroup bdl
82 * @{
83 */
84/** @addtogroup bdlbb
85 * @{
86 */
87/** @addtogroup bdlbb_pooledblobbufferfactory
88 * @{
89 */
90
91#include <bdlscm_version.h>
92
93#include <bdlbb_blob.h>
94
96
97#include <bsls_keyword.h>
98
99
100namespace bdlbb {
101
102 // =============================
103 // class PooledBlobBufferFactory
104 // =============================
105
106/// This class implements the `BlobBufferFactory` protocol and provides a
107/// mechanism for allocating `BlobBuffer` objects of a fixed size passed at
108/// construction.
109///
110/// See @ref bdlbb_pooledblobbufferfactory
112
113 // DATA
114 int d_bufferSize; // size of allocated blob buffers
115
116 bdlma::ConcurrentPoolAllocator d_spPool; // pool used to allocate shared
117 // pointers and buffers
118 // contiguously
119 public:
120 // CREATORS
121
123 bslma::Allocator *basicAllocator = 0);
125 bsls::BlockGrowth::Strategy growthStrategy,
126 bslma::Allocator *basicAllocator = 0);
127 /// Create a pooled factory for allocating `BlobBuffer` objects of the
128 /// specified `bufferSize`. Optionally specify a `growthStrategy` used
129 /// to control the growth of internal memory chunks (from which memory
130 /// blocks are dispensed). If `growthStrategy` is not specified,
131 /// geometric growth is used. If `growthStrategy` is specified,
132 /// optionally specify a `maxBlocksPerChunk`, indicating the maximum
133 /// number of blocks to be allocated at once when the underlying pool
134 /// must be replenished. If `maxBlocksPerChunk` is not specified, an
135 /// implementation-defined value is used. If geometric growth is used,
136 /// the chunk size grows starting at the value returned by `blockSize`,
137 /// doubling in size until the size is exactly
138 /// `blockSize() * maxBlocksPerChunk`. If constant growth is used, the
139 /// chunk size is always `maxBlocksPerChunk`. Optionally specify a
140 /// `basicAllocator` used to supply memory. If `basicAllocator` is 0,
141 /// the currently installed default allocator is used. The behavior is
142 /// undefined unless `0 < bufferSize`, and `1 <= maxBlocksPerChunk`.
144 bsls::BlockGrowth::Strategy growthStrategy,
145 int maxBlocksPerChunk,
146 bslma::Allocator *basicAllocator = 0);
147
148 /// Destroy this factory. This operation releases all `BlobBuffer`
149 /// objects allocated via this factory.
151
152 // MANIPULATORS
153
154 /// Allocate a new buffer with the buffer size specified at construction
155 /// and load it into the specified `buffer`. Note that destruction of
156 /// the `bdlbb::PooledBlobBufferFactory` object releases all
157 /// `BlobBuffer` objects allocated via this factory.
159
160 // ACCESSORS
161
162 /// Return the buffer size specified at construction of this factory.
163 int bufferSize() const;
164};
165
166// ============================================================================
167// INLINE DEFINITIONS
168// ============================================================================
169
170 // -----------------------------
171 // class PooledBlobBufferFactory
172 // -----------------------------
173
174// ACCESSORS
175inline
177{
178 return d_bufferSize;
179}
180
181} // close package namespace
182
183
184#endif
185
186// ----------------------------------------------------------------------------
187// Copyright 2018 Bloomberg Finance L.P.
188//
189// Licensed under the Apache License, Version 2.0 (the "License");
190// you may not use this file except in compliance with the License.
191// You may obtain a copy of the License at
192//
193// http://www.apache.org/licenses/LICENSE-2.0
194//
195// Unless required by applicable law or agreed to in writing, software
196// distributed under the License is distributed on an "AS IS" BASIS,
197// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
198// See the License for the specific language governing permissions and
199// limitations under the License.
200// ----------------------------- END-OF-FILE ----------------------------------
201
202/** @} */
203/** @} */
204/** @} */
Definition bdlbb_blob.h:616
Definition bdlbb_blob.h:455
Definition bdlbb_pooledblobbufferfactory.h:111
PooledBlobBufferFactory(int bufferSize, bslma::Allocator *basicAllocator=0)
PooledBlobBufferFactory(int bufferSize, bsls::BlockGrowth::Strategy growthStrategy, bslma::Allocator *basicAllocator=0)
int bufferSize() const
Return the buffer size specified at construction of this factory.
Definition bdlbb_pooledblobbufferfactory.h:176
void allocate(BlobBuffer *buffer) BSLS_KEYWORD_OVERRIDE
PooledBlobBufferFactory(int bufferSize, bsls::BlockGrowth::Strategy growthStrategy, int maxBlocksPerChunk, bslma::Allocator *basicAllocator=0)
~PooledBlobBufferFactory() BSLS_KEYWORD_OVERRIDE
Definition bdlma_concurrentpoolallocator.h:635
Definition bslma_allocator.h:457
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:653
Definition bdlbb_blob.h:442
Strategy
Definition bsls_blockgrowth.h:169