/
pkg_objectpool.h
283 lines (248 loc) · 8.97 KB
/
pkg_objectpool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// pkg_objectpool.h -*-C++-*-
#ifndef INCLUDED_PKG_OBJECTPOOL
#define INCLUDED_PKG_OBJECTPOOL
//@PURPOSE: Provide a pool for efficient management of objects.
//
//@CLASSES:
// pkg::ObjectPool: container for managed objects
//
//@SEE_ALSO:
//
//@DESCRIPTION: This component provides a mechanism, 'pkg::ObjectPool', for
// managing a generic pool of objects using the acquire-release idiom. An
// object pool provides two main methods: 'getObject', which returns an object
// from the pool, and 'releaseObject', which returns an object to the pool for
// further reuse (thus avoiding the overhead of object construction and
// destruction).
//
///Object Construction, Destruction, and Resetting
///- - - - - - - - - - - - - - - - - - - - - - - -
// This pool requires that the template parameter type used to construct an
// 'ObjectPool' provides a default constructor, public destructor, and a
// 'reset' function that restores an object to its default-constructed state.
//
///Usage
///-----
// This section illustrates intended use of this component.
//
///Example 1: Using an Object Pool of Database Connections
///- - - - - - - - - - - - - - - - - - - - - - - - - - - -
// An object pool can be used to store objects that are expensive to create and
// destroy.
//
// First, we define a 'class', 'DatabaseConnection', that represents a database
// connection:
//..
// class Query;
// class QueryResult;
//
// class DatabaseConnection {
// // This 'class' represents a database connection.
//
// // DATA
//
// // ..
//
// bslma::Allocator *d_allocator_p; // memory allocator (held, not owned)
//
// public:
// // CREATORS
// DatabaseConnection(bslma::Allocator *basicAllocator = 0);
// // Create a 'DatabaseConnection' object. Optionally specify a
// // 'basicAllocator' used to supply memory. If 'basicAllocator' is
// // 0, the currently installed default allocator is used.
//
// ~DatabaseConnection();
// // Destroy this 'DatabaseConnection' object.
//
// // MANIPULATORS
// void reset();
// // Reset this object.
//
// int executeQuery(QueryResult *queryResult, const Query& query);
// // Execute the specified 'query' and load the result in the
// // specified 'queryResult'. Return 0 on success, and a non-zero
// // value otherwise.
// };
//
// // TRAITS
// namespace BloombergLP {
// namespace bslma {
// template <> struct UsesBslmaAllocator<DatabaseConnection>
// : bsl::true_type {};
// }
// }
//..
// Then, we will provide the function definitions for this 'class'. To keep
// this example simple, the constructor, destructor, and 'reset' methods of
// this 'class' are no-ops and are therefore not shown below:
//..
// int DatabaseConnection::executeQuery(QueryResult *queryResult,
// const Query& query)
// {
// // Execute 'query' and load the result into 'queryResult'
//
// return 0;
// }
//..
// Next, we create an object pool representing a list of connections. Since
// we do not supply an allocator, 'pool' will use the default allocator to
// supply memory:
//..
// ObjectPool<DatabaseConnection> pool;
//..
// Now, the 'pool' object can be used to retrieve and return connections as
// required:
//..
// DatabaseConnection *connection = pool.getObject();
//
// // Use the 'connection'.
//
// pool.releaseObject(connection);
//..
#ifndef INCLUDED_BSLMA_ALLOCATOR
#include <bslma_allocator.h>
#endif
#ifndef INCLUDED_BSLMA_DEFAULT
#include <bslma_default.h>
#endif
#ifndef INCLUDED_BSLMA_USESBSLMAALLOCATOR
#include <bslma_usesbslmaallocator.h>
#endif
#ifndef INCLUDED_BSL_LIST
#include <bsl_list.h>
#endif
namespace Enterprise {
namespace pkg {
// ================
// class ObjectPool
// ================
template <typename TYPE>
class ObjectPool {
// This 'class' provides a pool of reusable objects of template parameter
// type 'TYPE' and assumes that 'TYPE' provides a default constructor, a
// public destructor, and a 'reset' method. This 'class' is not
// thread-safe and multiple threads operating on the same object must
// synchronize appropriately.
// DATA
bsl::list<TYPE *> d_objects; // list of managed objects
BloombergLP::bslma::Allocator *d_allocator_p; // memory allocator (held,
// not owned)
// PRIVATE CLASS METHODS
TYPE *createObject(bsl::false_type);
// Create and return an object of the template parameter type 'TYPE'.
// Note that the allocator held by this object is *not* passed to the
// new object's constructor.
TYPE *createObject(bsl::true_type);
// Create and return an object of the template parameter type 'TYPE'
// passing the allocator held by this object to the new object's
// constructor.
public:
// CREATORS
explicit ObjectPool(BloombergLP::bslma::Allocator *basicAllocator = 0);
// Create an object pool that invokes the default constructor of the
// the template parameter type 'TYPE' to construct objects. Optionally
// specify a 'basicAllocator' used to supply memory. If
// 'basicAllocator' is 0, the currently installed default allocator is
// used.
~ObjectPool();
// Destroy this object pool. All objects created by this pool are
// destroyed (even if some of them are still in use) and memory is
// reclaimed.
// MANIPULATORS
TYPE *getObject();
// Return an address providing modifiable access to a
// default-constructed object of the template parameter type 'TYPE'.
// If this pool does not have any free objects, then a
// default-constructed object is allocated and returned.
void releaseObject(TYPE *object);
// Return the specified 'object' back to this object pool for
// subsequent reuse. Invoke the 'reset' method on 'object'. The
// behavior is undefined unless 'object' was constructed by this pool.
// The rest of the interface is elided for brevity.
};
} // close package namespace
} // close enterprise namespace
// TRAITS
namespace BloombergLP {
namespace bslma {
template <typename TYPE>
struct UsesBslmaAllocator<Enterprise::pkg::ObjectPool<TYPE> >
: bsl::true_type {};
} // close package namespace
} // close enterprise namespace
namespace Enterprise {
namespace pkg {
// ============================================================================
// INLINE FUNCTION DEFINITIONS
// ============================================================================
// ----------------
// class ObjectPool
// ----------------
// PRIVATE CLASS METHODS
template <typename TYPE>
inline
TYPE *ObjectPool<TYPE>::createObject(bsl::false_type)
{
return new (*d_allocator_p) TYPE();
}
template <typename TYPE>
inline
TYPE *ObjectPool<TYPE>::createObject(bsl::true_type)
{
return new (*d_allocator_p) TYPE(d_allocator_p);
}
// CREATORS
template <typename TYPE>
inline
ObjectPool<TYPE>::ObjectPool(BloombergLP::bslma::Allocator *basicAllocator)
: d_objects(basicAllocator)
, d_allocator_p(BloombergLP::bslma::Default::allocator(basicAllocator))
{
}
template <typename TYPE>
ObjectPool<TYPE>::~ObjectPool()
{
for (typename bsl::list<TYPE *>::iterator iter = d_objects.begin();
iter != d_objects.end();
++iter) {
d_allocator_p->deleteObject(*iter);
}
d_objects.clear();
}
// MANIPULATORS
template <typename TYPE>
TYPE *ObjectPool<TYPE>::getObject()
{
if (d_objects.size()) {
TYPE *object = d_objects.back();
d_objects.pop_back();
return object; // RETURN
}
return createObject(BloombergLP::bslma::UsesBslmaAllocator<TYPE>());
}
template <typename TYPE>
inline
void ObjectPool<TYPE>::releaseObject(TYPE *object)
{
object->reset();
d_objects.push_back(object);
}
} // close package namespace
} // close enterprise namespace
#endif
// ----------------------------------------------------------------------------
// Copyright 2013 Bloomberg Finance L.P.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------- END-OF-FILE ----------------------------------