BDE 4.14.0 Production release
|
Provide a concrete allocator that keeps count of allocated bytes.
This component provides a special-purpose instrumented allocator, ball::CountingAllocator
, that implements the bslma::Allocator
protocol and guarantees maximal alignment of allocated blocks, even when the allocator supplied at construction guarantees only natural alignment (or no alignment at all). ball::CountingAllocator
maintains a user-resettable running sum of the total number of bytes allocated (called byte count, see below).
The byte count maintained by ball::CountingAllocator
is set to 0 upon construction and after each call to resetNumBytesTotal
. Each call of allocate(size)
increases the byte count by the sum of the least multiple of bsls::AlignmentUtil::BSLS_MAX_ALIGNMENT
that is greater than or equal to size
and bsls::AlignmentUtil::BSLS_MAX_ALIGNMENT
. Each call of deallocate
decrements the byte count by the same amount by which the byte count was incremented on matching allocate
call. The current value of the byte count is returned by the numBytesTotal
accessor.
This section illustrates intended use of this component.
In the following example we demonstrate how the counting allocator can be used to know the amount of dynamic memory allocated by a vector<int>
after pushing one integer. Let us assume that memory for the vector comes from a bslma::Allocator
named allocator
.