Quick Links: |
#include <bdlma_countingallocator.h>
Public Types | |
typedef bsls::Types::size_type | size_type |
Public Member Functions | |
CountingAllocator (bslma::Allocator *basicAllocator=0) | |
CountingAllocator (const char *name, bslma::Allocator *basicAllocator=0) | |
virtual | ~CountingAllocator () |
virtual void * | allocate (bsls::Types::size_type size) |
virtual void | deallocate (void *address) |
const char * | name () const |
bsls::Types::Int64 | numBytesInUse () const |
bsls::Types::Int64 | numBytesTotal () const |
bsl::ostream & | print (bsl::ostream &stream) const |
virtual void * | allocate (size_type size)=0 |
template<class TYPE > | |
void | deleteObject (const TYPE *object) |
void | deleteObject (bsl::nullptr_t) |
template<class TYPE > | |
void | deleteObjectRaw (const TYPE *object) |
void | deleteObjectRaw (bsl::nullptr_t) |
Static Public Member Functions | |
static void | throwBadAlloc () |
This class defines a concrete "counting" allocator mechanism that implements the bslma::Allocator
protocol, and provides instrumentation to track: (1) the number of bytes currently in use, and (2) the cumulative number of bytes that have ever been allocated. The accumulated statistics are based solely on the number of bytes requested (see allocate
).
Note that, like many other allocators, this allocator relies on the currently installed default allocator (see bslma_default
). Clients may, however, override this allocator by supplying (at construction) any other allocator implementing the bslma::Allocator
protocol provided that it is fully thread-safe.
See Component bdlma_countingallocator
typedef bsls::Types::size_type bslma::Allocator::size_type [inherited] |
Reimplemented in bdlma::ConcurrentPoolAllocator.
bdlma::CountingAllocator::CountingAllocator | ( | bslma::Allocator * | basicAllocator = 0 |
) | [explicit] |
bdlma::CountingAllocator::CountingAllocator | ( | const char * | name, | |
bslma::Allocator * | basicAllocator = 0 | |||
) | [explicit] |
Create a counting allocator. Optionally specify a name
(associated with this object) to be included in messages output by the print
method, thereby distinguishing this counting allocator from others that might be used in the same program. If name
is 0 (or not specified), no distinguishing name is incorporated in print
output. Optionally specify a basicAllocator
used to supply memory. If basicAllocator
is 0, the currently installed default allocator is used.
virtual bdlma::CountingAllocator::~CountingAllocator | ( | ) | [virtual] |
Destroy this allocator object. Note that destroying this allocator has no effect on any outstanding allocated memory.
virtual void* bdlma::CountingAllocator::allocate | ( | bsls::Types::size_type | size | ) | [virtual] |
Return a newly-allocated block of memory of the specified size
(in bytes). If size
is 0, a null pointer is returned with no other effect (e.g., on allocation statistics). Otherwise, invoke the allocate
method of the allocator supplied at construction, and increment the number of currently (and cumulatively) allocated bytes by size
.
virtual void bdlma::CountingAllocator::deallocate | ( | void * | address | ) | [virtual] |
Return the memory block at the specified address
back to this allocator. If address
is 0, this function has no effect (e.g., on allocation statistics). Otherwise, decrease the number of currently allocated bytes by the size originally requested for the block. The behavior is undefined unless address
was allocated using this allocator object and has not already been deallocated.
Implements bslma::Allocator.
const char* bdlma::CountingAllocator::name | ( | ) | const |
Return the name of this counting allocator, or 0 if no name was specified at construction.
bsls::Types::Int64 bdlma::CountingAllocator::numBytesInUse | ( | ) | const |
Return the number of bytes currently allocated from this object. Note that numBytesInUse() <= numBytesTotal()
.
bsls::Types::Int64 bdlma::CountingAllocator::numBytesTotal | ( | ) | const |
Return the cumulative number of bytes ever allocated from this object. Note that numBytesInUse() <= numBytesTotal()
.
bsl::ostream& bdlma::CountingAllocator::print | ( | bsl::ostream & | stream | ) | const |
Write the accumulated state information held in this allocator to the specified stream
in some reasonable (multi-line) format, and return a reference to stream
.
static void bslma::Allocator::throwBadAlloc | ( | ) | [static, inherited] |
DEPRECATED: Use bsls::BslExceptionUtil::throwBadAlloc
instead.
Throw std::bad_alloc
if exceptions are enabled or abort the program otherwise. Derived classes and helper functions will typically call this function when they are unable to satisfy an allocation request. This function never returns.
virtual void* bslma::Allocator::allocate | ( | size_type | size | ) | [pure virtual, inherited] |
Return a newly allocated block of memory of (at least) the specified positive size
(in bytes). If size
is 0, a null pointer is returned with no other effect. If this allocator cannot return the requested number of bytes, then it will throw a std::bad_alloc
exception in an exception-enabled build, or else will abort the program in a non-exception build. The behavior is undefined unless 0 <= size
. Note that the alignment of the address returned conforms to the platform requirement for any object of the specified size
.
Implemented in ball::CountingAllocator, balst::StackTraceTestAllocator, bdlma::BufferedSequentialAllocator, bslma::AllocatorAdaptor_Imp< STL_ALLOC >, bslma::BufferAllocator, bslma::MallocFreeAllocator, bslma::NewDeleteAllocator, bslma::SequentialAllocator, bslma::TestAllocator, and bslma::AllocatorAdaptor_Imp< STL_ALLOC::template rebind< char >::other >.
void bslma::Allocator::deleteObject | ( | const TYPE * | object | ) | [inherited] |
Destroy the specified object
based on its dynamic type and then use this allocator to deallocate its memory footprint. Do nothing if object
is a null pointer. The behavior is undefined unless object
, when cast appropriately to void *
, was allocated using this allocator and has not already been deallocated. Note that dynamic_cast<void *>(object)
is applied if TYPE
is polymorphic, and static_cast<void *>(object)
is applied otherwise.
void bslma::Allocator::deleteObject | ( | bsl::nullptr_t | ) | [inherited] |
This function has no effect. Note that it exists to support calling deleteObject
will a null pointer literal, that would otherwise not deduce to a pointer type for the method above. As calls to deleteObject
with (typed) null pointer values have well-defined behavior, it should also support calls with a null pointer literal.
void bslma::Allocator::deleteObjectRaw | ( | const TYPE * | object | ) | [inherited] |
Destroy the specified object
and then use this allocator to deallocate its memory footprint. Do nothing if object
is a null pointer. The behavior is undefined unless object
was allocated using this allocator, is not a secondary base class pointer -- i.e., the address is (numerically) the same as when it was originally dispensed by this allocator, and has not already been deallocated.
void bslma::Allocator::deleteObjectRaw | ( | bsl::nullptr_t | ) | [inherited] |
This function has no effect. Note that it exists to support calling deleteObjectRaw
will a null pointer literal, that would otherwise not deduce to a pointer type for the method above. As calls to deleteObjectRaw
with (typed) null pointer values have well-defined behavior, it should also support calls with a null pointer literal.