BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslma_defaultallocatorguard.h
Go to the documentation of this file.
1/// @file bslma_defaultallocatorguard.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslma_defaultallocatorguard.h -*-C++-*-
8#ifndef INCLUDED_BSLMA_DEFAULTALLOCATORGUARD
9#define INCLUDED_BSLMA_DEFAULTALLOCATORGUARD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslma_defaultallocatorguard bslma_defaultallocatorguard
15/// @brief Provide scoped guard to temporarily change the default allocator.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslma
19/// @{
20/// @addtogroup bslma_defaultallocatorguard
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslma_defaultallocatorguard-purpose"> Purpose</a>
25/// * <a href="#bslma_defaultallocatorguard-classes"> Classes </a>
26/// * <a href="#bslma_defaultallocatorguard-description"> Description </a>
27/// * <a href="#bslma_defaultallocatorguard-usage"> Usage </a>
28///
29/// # Purpose {#bslma_defaultallocatorguard-purpose}
30/// Provide scoped guard to temporarily change the default allocator.
31///
32/// # Classes {#bslma_defaultallocatorguard-classes}
33///
34/// - bslma::DefaultAllocatorGuard: default-allocator scoped guard
35///
36/// @see bslma_allocator, bslma_default
37///
38/// # Description {#bslma_defaultallocatorguard-description}
39/// This component provides an object,
40/// `bslma::DefaultAllocatorGuard`, that serves as a "scoped guard" to enable
41/// the temporary replacement of the process-wide default allocator. This
42/// functionality is intended for *testing* only, and in no event should this
43/// component be used except in `main`.
44///
45/// The guard object takes as its constructor argument the address of an object
46/// of a class derived from `bslma::Allocator`. The default allocator at the
47/// time of guard construction is held by the guard, and the
48/// constructor-argument allocator is installed as the new process-wide default
49/// allocator (via a call to `bslma::Default::setDefaultAllocatorRaw`). Upon
50/// destruction of the guard object, its held allocator is restored as the
51/// process-wide default allocator (via another call to
52/// `bslma::Default::setDefaultAllocatorRaw`).
53///
54/// ## Usage {#bslma_defaultallocatorguard-usage}
55///
56///
57/// The @ref bslma_default component ensures that, unless the owner of `main` or
58/// some intervening code explicitly installs a default allocator, the
59/// `bslma::NewDeleteAllocator::singleton()` will be the default allocator for
60/// that process (i.e., calls to `bslma::Default::defaultAllocator()` will
61/// return the new-delete allocator unless someone has set the default allocator
62/// to a different allocator intentionally). Consider for purposes of this
63/// illustrative example the case where we, as owners of a test driver `main`,
64/// can count on the `bslma::NewDeleteAllocator` singleton being the default.
65///
66/// Consider now that, for testing purposes, we want a simple counting allocator
67/// that uses `new` and `delete`, and that also keeps count of the number of
68/// memory blocks that have been allocated but never deallocated. (Note that,
69/// in testing real production code, `bslma::TestAllocator` serves this
70/// purpose.)
71/// @code
72/// class my_CountingAllocator : public bslma::Allocator
73/// {
74/// int d_blocksOutstanding;
75/// public:
76/// my_CountingAllocator();
77/// ~my_CountingAllocator();
78///
79/// virtual void *allocate(int size);
80/// virtual void deallocate(void *address);
81///
82/// int blocksOutstanding() const { return d_blocksOutstanding; }
83/// };
84///
85/// inline
86/// my_CountingAllocator::my_CountingAllocator()
87/// : d_blocksOutstanding(0)
88/// {
89/// }
90///
91/// inline
92/// my_CountingAllocator::~my_CountingAllocator()
93/// {
94/// if (0 < d_blocksOutstanding) {
95/// std::cout << "***ERROR: Memory Leak***" << std::endl
96/// << d_blocksOutstanding << " block(s) leaked. "
97/// << "Program aborting." << std::endl;
98/// assert(0);
99/// }
100/// }
101///
102/// inline
103/// void *my_CountingAllocator::allocate(int size)
104/// {
105/// ++d_blocksOutstanding;
106/// return operator new(size);
107/// }
108///
109/// inline
110/// void my_CountingAllocator::deallocate(void *address)
111/// {
112/// --d_blocksOutstanding;
113/// operator delete(address);
114/// }
115/// @endcode
116/// We may now write a test driver for some component that uses a
117/// `bslma::Allocator` and the `bslma::Default` mechanism. First, we confirm
118/// that the `bslma::NewDeleteAllocator` singleton is indeed installed.
119/// @code
120/// //my_component.t.cpp
121///
122/// // ...
123///
124/// int main()
125/// {
126/// // ...
127/// bslma::NewDeleteAllocator *na =
128/// &bslma::NewDeleteAllocator::singleton();
129/// assert(na == bslma::Default::defaultAllocator());
130/// @endcode
131/// Now, we can go into some inner scope and use a guard object to install a
132/// test allocator as the default for any newly created objects. When the inner
133/// scope ends, the guard will be destroyed, automatically restoring the
134/// original default allocator.
135/// @code
136/// {
137/// my_CountingAllocator testAllocator;
138/// bslma::DefaultAllocatorGuard guard(&testAllocator);
139/// assert(&testAllocator == bslma::Default::defaultAllocator());
140///
141/// // Create and test the object under test, which will use the
142/// // test allocator *by* *default*.
143///
144/// // ...
145/// }
146/// assert(na == bslma::Default::defaultAllocator());
147/// @endcode
148/// If the test block above has a memory leak, the program will print an error
149/// to `stdout` and abort. Otherwise, the new-delete allocator will be
150/// re-installed as the default allocator, and the program will proceed to the
151/// next block of code.
152/// @}
153/** @} */
154/** @} */
155
156/** @addtogroup bsl
157 * @{
158 */
159/** @addtogroup bslma
160 * @{
161 */
162/** @addtogroup bslma_defaultallocatorguard
163 * @{
164 */
165
166#include <bslscm_version.h>
167
168
169
170
171namespace bslma {
172
173class Allocator;
174
175 // ===========================
176 // class DefaultAllocatorGuard
177 // ===========================
178
179/// Upon construction, an object of this class saves the current default
180/// allocator and installs the user-specified allocator as the default
181/// allocator. On destruction, the original default allocator is restored.
182/// Objects of this class are intended for testing purposes *only* and are
183/// *not* guaranteed to be safe in production environments.
184///
185/// See @ref bslma_defaultallocatorguard
187
188 Allocator *d_original_p; // original (to be restored at destruction)
189
190 // NOT IMPLEMENTED
193
194 public:
195 // CREATORS
196
197 /// Create a scoped guard that installs the specified `temporary`
198 /// allocator as the default allocator. Note that the default allocator
199 /// is automatically restored to the original allocator on destruction.
200 explicit
202
203 /// Restore the default allocator that was in place when this scoped
204 /// guard was created and destroy this guard.
206};
207
208} // close package namespace
209
210#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
211// ============================================================================
212// BACKWARD COMPATIBILITY
213// ============================================================================
214
215/// This alias is defined for backward compatibility.
217#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
218
219
220
221#endif
222
223// ----------------------------------------------------------------------------
224// Copyright 2013 Bloomberg Finance L.P.
225//
226// Licensed under the Apache License, Version 2.0 (the "License");
227// you may not use this file except in compliance with the License.
228// You may obtain a copy of the License at
229//
230// http://www.apache.org/licenses/LICENSE-2.0
231//
232// Unless required by applicable law or agreed to in writing, software
233// distributed under the License is distributed on an "AS IS" BASIS,
234// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
235// See the License for the specific language governing permissions and
236// limitations under the License.
237// ----------------------------- END-OF-FILE ----------------------------------
238
239/** @} */
240/** @} */
241/** @} */
Definition bslma_allocator.h:457
Definition bslma_defaultallocatorguard.h:186
DefaultAllocatorGuard(Allocator *temporary)
bslma::DefaultAllocatorGuard bslma_DefaultAllocatorGuard
This alias is defined for backward compatibility.
Definition bslma_defaultallocatorguard.h:216
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balxml_encoderoptions.h:68