BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslma_testallocatorstatisticsguard.h
Go to the documentation of this file.
1/// @file bslma_testallocatorstatisticsguard.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslma_testallocatorstatisticsguard.h -*-C++-*-
8#ifndef INCLUDED_BSLMA_TESTALLOCATORSTATISTICSGUARD
9#define INCLUDED_BSLMA_TESTALLOCATORSTATISTICSGUARD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslma_testallocatorstatisticsguard bslma_testallocatorstatisticsguard
15/// @brief Provide mechanism for `bslma::TestAllocator` scoped statistics.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslma
19/// @{
20/// @addtogroup bslma_testallocatorstatisticsguard
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslma_testallocatorstatisticsguard-purpose"> Purpose</a>
25/// * <a href="#bslma_testallocatorstatisticsguard-classes"> Classes </a>
26/// * <a href="#bslma_testallocatorstatisticsguard-description"> Description </a>
27/// * <a href="#bslma_testallocatorstatisticsguard-statistics"> Statistics </a>
28/// * <a href="#bslma_testallocatorstatisticsguard-usage"> Usage </a>
29/// * <a href="#bslma_testallocatorstatisticsguard-example-1-determine-maximums-for-a-scope"> Example 1: Determine Maximums for a Scope </a>
30///
31/// # Purpose {#bslma_testallocatorstatisticsguard-purpose}
32/// Provide mechanism for `bslma::TestAllocator` scoped statistics.
33///
34/// # Classes {#bslma_testallocatorstatisticsguard-classes}
35///
36/// - bslma::TestAllocatorStatiscticsGuard: `bslma::TestAllocator` scoped stats
37///
38/// @see bslma_testallocator
39///
40/// # Description {#bslma_testallocatorstatisticsguard-description}
41/// This component provides a single mechanism class,
42/// `bslma::TestAllocatorStatiscticsGuard`, which is used, in concert with
43/// `bslma::TestAllocator`, in the implementation of test drivers. Upon its
44/// creation the `bslma::TestAllocatorStatiscticsGuard` stashes and resets the
45/// current statistics state of the specified `bslma::TestAllocator` so that
46/// local values (maximum etc) may be measured in thw scope of the guard. Upon
47/// its destruction the guard restores the statistics values of the guarded test
48/// allocator to the state what it would have if the guard hasn't been there (as
49/// if no stashing and resetting had taken place), but combining the stashed
50/// values with the current statistics values of the test allocator.
51///
52/// ## Statistics {#bslma_testallocatorstatisticsguard-statistics}
53///
54///
55/// On creation the current statistics are saved to be used later in restoring,
56/// and then reset as follows:
57///
58/// Statistic | Reset value to
59/// ---------------- | --------------
60/// numAllocations | numBlocksInUse
61/// numDeallocations | ZERO
62/// numMismatches | ZERO
63/// numBoundsErrors | ZERO
64/// numBlocksMax | numBlocksInUse
65/// numBytesMax | numBytesInUse
66/// numBlocksTotal | numBlocksInUse
67/// numBytesTotal | numBytesInUse
68///
69/// During destruction the guard restores the state of the statistics, as if the
70/// reset (on construction) has never happened, in the following manner:
71///
72/// Statistic | Restore value as
73/// ---------------- | ---------------------------------------------------
74/// numAllocations | saved + current - saved.numBlocksInUse
75/// numDeallocations | saved.numDeallocations + current.numDeallocations
76/// numMismatches | saved.numMismatches + current.numMismatches
77/// numBoundsErrors | saved.numBoundsErrors + current.numBoundsErrors
78/// numBlocksMax | max(saved.numBlocksMax, current.numBlocksMax)
79/// numBytesMax | max(saved.numBytesMax, current.numBytesMax)
80/// numBlocksTotal | saved + current - saved.numBlocksInUse
81/// numBytesTotal | saved + current - saved.numBytesInUse
82///
83/// See also `bslma::TestAllocator::stashStatistics` and
84/// `bslma::TestAllocator::restoreStatistics`.
85///
86/// ## Usage {#bslma_testallocatorstatisticsguard-usage}
87///
88///
89/// This section illustrates intended use of this component.
90///
91/// ### Example 1: Determine Maximums for a Scope {#bslma_testallocatorstatisticsguard-example-1-determine-maximums-for-a-scope}
92///
93///
94/// Suppose that, in a test driver, we would like to ensure that a certain
95/// operation does not use too much memory. However, to do that operation we
96/// need to first do something that may or may not use more memory than what we
97/// allow. In order for us to be able to measure local maximums (or any
98/// statistics) we need to stash and reset the statistics of the used test
99/// allocator, and later (at the end of our measured local scope) restore them
100/// as if we had never reset them.
101///
102/// First, we define our `TestAllocator` that we will use throughout:
103/// @code
104/// bslma::TestAllocator testAllocator, *ta = &testAllocator;
105/// @endcode
106/// Then, we perform the test-preparation operation that may allocate and then
107/// release a lot of memory, therefore skewing later statistics:
108/// @code
109/// someOperation(ta);
110///
111/// assert(ta->numBlocksMax() - ta->numBlocksInUse() > 4);
112/// @endcode
113/// Next, we prepare the local statistics by creating a scope and declaring a
114/// guard variable:
115/// @code
116/// {
117/// bslma::TestAllocatorStatisticsGuard tasg(ta);
118/// assert(ta->numBlocksInUse() == tasg.originalNumBlocksInUse());
119/// @endcode
120/// Now, we run the measured operation and verify that it has not allocated more
121/// than 4 blocks (in addition to what was already allocated before):
122/// @code
123/// measuredOperation(ta);
124/// assert(ta->numBlocksMax() - tasg.originalNumBlocksInUse() <= 4);
125/// }
126/// @endcode
127/// Finally, we demonstrate that the guard restores the statistics:
128/// @code
129/// assert(ta->numBlocksMax() - ta->numBlocksInUse() > 4);
130/// @endcode
131/// @}
132/** @} */
133/** @} */
134
135/** @addtogroup bsl
136 * @{
137 */
138/** @addtogroup bslma
139 * @{
140 */
141/** @addtogroup bslma_testallocatorstatisticsguard
142 * @{
143 */
144
145#include <bslscm_version.h>
146
147#include <bslma_testallocator.h>
148
149#include <bsls_assert.h>
150
151
152namespace bslma {
153
154 // ==================================
155 // class TestAllocatorStatisticsGuard
156 // ==================================
157
158/// This mechanism provides the means to get localized `TestAllocator`
159/// statistics in a scope since the construction of the guard, and also
160/// reinstates the statistics upon its destruction as if the local reset has
161/// not happened. See the @ref bslma_testallocatorstatisticsguard-statistics section for the statistics managed.
162///
163/// See @ref bslma_testallocatorstatisticsguard
165
166 // DATA
167 TestAllocatorStashedStatistics d_stashed; // the stashed statistics
168
169 private:
170 // NOT IMPLEMENTED
172 // = delete
174 const TestAllocatorStatisticsGuard&); // = delete
175
176 public:
177 // CREATORS
178
179 /// Create a `TestAllocatorStatisticsGuard` object to stash, reset, and
180 /// restore statistics of the specified `testAllocator`.
181 explicit TestAllocatorStatisticsGuard(TestAllocator *testAllocator);
182
183 /// Destroy this object and restore the statistics of the `testAllocator`
184 /// supplied at construction to a state as if this guard had never existed.
186
187 // ACCESSORS
188
189 /// Return the stashed value of `numBlocksInUse` at construction.
191
192 /// Return the stashed value of `numBytesInUse` at construction.
194};
195
196// ============================================================================
197// INLINE DEFINITIONS
198// ============================================================================
199
200 // ----------------------------------
201 // class TestAllocatorStatisticsGuard
202 // ----------------------------------
203
204// CREATORS
205inline
206TestAllocatorStatisticsGuard::TestAllocatorStatisticsGuard(
207 TestAllocator *testAllocator)
208: d_stashed(testAllocator->stashStatistics())
209{
210}
211
212inline
217
218// ACCESSORS
219inline
224
225inline
230
231} // close package namespace
232
233
234#endif
235
236// ----------------------------------------------------------------------------
237// Copyright 2024 Bloomberg Finance L.P.
238//
239// Licensed under the Apache License, Version 2.0 (the "License");
240// you may not use this file except in compliance with the License.
241// You may obtain a copy of the License at
242//
243// http://www.apache.org/licenses/LICENSE-2.0
244//
245// Unless required by applicable law or agreed to in writing, software
246// distributed under the License is distributed on an "AS IS" BASIS,
247// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
248// See the License for the specific language governing permissions and
249// limitations under the License.
250// ----------------------------- END-OF-FILE ----------------------------------
251
252/** @} */
253/** @} */
254/** @} */
Definition bslma_testallocator.h:926
bsls::Types::Int64 numBlocksInUse() const
Return the numBlocksInUse attribute value.
Definition bslma_testallocator.h:1505
void restore()
Call origin->restoreStatistics(*this).
Definition bslma_testallocator.h:1496
bsls::Types::Int64 numBytesInUse() const
Return the numBytesInUse attribute value.
Definition bslma_testallocator.h:1511
Definition bslma_testallocatorstatisticsguard.h:164
~TestAllocatorStatisticsGuard()
Definition bslma_testallocatorstatisticsguard.h:213
bsls::Types::Int64 originalNumBytesInUse() const
Return the stashed value of numBytesInUse at construction.
Definition bslma_testallocatorstatisticsguard.h:226
bsls::Types::Int64 originalNumBlocksInUse() const
Return the stashed value of numBlocksInUse at construction.
Definition bslma_testallocatorstatisticsguard.h:220
Definition bslma_testallocator.h:402
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition baljsn_encoder_testtypes.h:76
long long Int64
Definition bsls_types.h:134