BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlma::CountingAllocator Class Reference

#include <bdlma_countingallocator.h>

Inheritance diagram for bdlma::CountingAllocator:
bslma::Allocator bsl::memory_resource

Public Member Functions

 CountingAllocator (bslma::Allocator *basicAllocator=0)
 
 CountingAllocator (const char *name, bslma::Allocator *basicAllocator=0)
 
 ~CountingAllocator () BSLS_KEYWORD_OVERRIDE
 
void * allocate (bsls::Types::size_type size) BSLS_KEYWORD_OVERRIDE
 
void deallocate (void *address) BSLS_KEYWORD_OVERRIDE
 
const char * name () const
 
bsls::Types::Int64 numBytesInUse () const
 
bsls::Types::Int64 numBytesTotal () const
 
bsl::ostream & print (bsl::ostream &stream) const
 
- Public Member Functions inherited from bslma::Allocator
 ~Allocator () BSLS_KEYWORD_OVERRIDE
 
template<class TYPE >
void deleteObject (const TYPE *object)
 
template<class TYPE >
void deleteObjectRaw (const TYPE *object)
 
void deleteObject (bsl::nullptr_t)
 
void deleteObjectRaw (bsl::nullptr_t)
 
- Public Member Functions inherited from bsl::memory_resource
 memory_resource () BSLS_KEYWORD_DEFAULT
 Create this object. Has no effect other than to begin its lifetime.
 
 memory_resource (const memory_resource &) BSLS_KEYWORD_DEFAULT
 
virtual ~memory_resource ()
 Destroy this object. Has no effect other than to end its lifetime.
 
memory_resourceoperator= (const memory_resource &) BSLS_KEYWORD_DEFAULT
 Return a modifiable reference to this object.
 
BSLS_ANNOTATION_NODISCARD void * allocate (size_t bytes, size_t alignment=k_MAX_ALIGN)
 
void deallocate (void *p, size_t bytes, size_t alignment=k_MAX_ALIGN)
 
bool is_equal (const memory_resource &other) const BSLS_KEYWORD_NOEXCEPT
 

Additional Inherited Members

- Public Types inherited from bslma::Allocator
typedef std::size_t size_type
 
- Static Public Member Functions inherited from bslma::Allocator
static void throwBadAlloc ()
 
- Protected Member Functions inherited from bslma::Allocator
void * do_allocate (std::size_t bytes, std::size_t alignment) BSLS_KEYWORD_OVERRIDE
 
void do_deallocate (void *p, std::size_t bytes, std::size_t alignment) BSLS_KEYWORD_OVERRIDE
 
bool do_is_equal (const memory_resource &other) const BSLS_KEYWORD_NOEXCEPT BSLS_KEYWORD_OVERRIDE
 

Detailed Description

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 bdlma_countingallocator

Constructor & Destructor Documentation

◆ CountingAllocator() [1/2]

bdlma::CountingAllocator::CountingAllocator ( bslma::Allocator basicAllocator = 0)
explicit

◆ CountingAllocator() [2/2]

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.

◆ ~CountingAllocator()

bdlma::CountingAllocator::~CountingAllocator ( )

Destroy this allocator object. Note that destroying this allocator has no effect on any outstanding allocated memory.

Member Function Documentation

◆ allocate()

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.

Implements bslma::Allocator.

◆ deallocate()

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.

◆ name()

const char * bdlma::CountingAllocator::name ( ) const
inline

Return the name of this counting allocator, or 0 if no name was specified at construction.

◆ numBytesInUse()

bsls::Types::Int64 bdlma::CountingAllocator::numBytesInUse ( ) const
inline

Return the number of bytes currently allocated from this object. Note that numBytesInUse() <= numBytesTotal().

◆ numBytesTotal()

bsls::Types::Int64 bdlma::CountingAllocator::numBytesTotal ( ) const
inline

Return the cumulative number of bytes ever allocated from this object. Note that numBytesInUse() <= numBytesTotal().

◆ print()

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.


The documentation for this class was generated from the following file: