BDE 4.14.0 Production release
Loading...
Searching...
No Matches
balst::StackTraceTestAllocator Class Reference

#include <balst_stacktracetestallocator.h>

Inheritance diagram for balst::StackTraceTestAllocator:
bdlma::ManagedAllocator bslma::Allocator bsl::memory_resource

Public Types

typedef bsl::function< void()> FailureHandler
 
- Public Types inherited from bslma::Allocator
typedef std::size_t size_type
 

Public Member Functions

 StackTraceTestAllocator (bslma::Allocator *basicAllocator=0)
 
 StackTraceTestAllocator (int numRecordedFrames, bslma::Allocator *basicAllocator=0)
 
 ~StackTraceTestAllocator () BSLS_KEYWORD_OVERRIDE
 
void * allocate (size_type size) BSLS_KEYWORD_OVERRIDE
 
void deallocate (void *address) BSLS_KEYWORD_OVERRIDE
 
void release () BSLS_KEYWORD_OVERRIDE
 Deallocate all memory held by this allocator.
 
void setAllocationLimit (bsls::Types::Int64 limit)
 
void setDemanglingPreferredFlag (bool value)
 
void setFailureHandler (const FailureHandler &func)
 
void setName (const char *name)
 
void setOstream (bsl::ostream *ostream)
 
bsls::Types::Int64 allocationLimit () const
 
const FailureHandlerfailureHandler () const
 
bsls::Types::Int64 numAllocations () const
 
bsl::size_t numBlocksInUse () const
 Return the number of blocks currently allocated from this object.
 
void reportBlocksInUse (bsl::ostream *ostream=0) 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
 

Static Public Member Functions

static void failAbort ()
 
static void failNoop ()
 
- Static Public Member Functions inherited from bslma::Allocator
static void throwBadAlloc ()
 

Additional Inherited Members

- 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 "test" allocator mechanism that implements the bdlma::ManagedAllocator protocol, and provides instrumentation to track the set of all blocks allocated by this allocator that have yet to be freed. At any time it can produce a report about such blocks, listing for each place that any unfreed blocks were allocated

  • the number of unfreed blocks allocated at that place
  • the stack trace at that place The allocator will also detect redundant frees of the same block, and frees by the wrong allocator. The client can choose whether such violations are handled by a core dump, or merely a report being written.

Note that, unlike many other allocators, this allocator does DOES NOT rely on the currently installed default allocator (see bslma_default ) at all, but instead – by default – uses MallocFreeAllocator singleton, which in turn calls the C Standard Library functions malloc and free as needed. Clients may, however, override this allocator by supplying (at construction) any other allocator implementing the bslma::Allocator protocol.

See balst_stacktracetestallocator

Member Typedef Documentation

◆ FailureHandler

Constructor & Destructor Documentation

◆ StackTraceTestAllocator() [1/2]

balst::StackTraceTestAllocator::StackTraceTestAllocator ( bslma::Allocator basicAllocator = 0)
explicit

◆ StackTraceTestAllocator() [2/2]

balst::StackTraceTestAllocator::StackTraceTestAllocator ( int  numRecordedFrames,
bslma::Allocator basicAllocator = 0 
)
explicit

Create a test allocator. Optionally specify numRecordedFrames, the number of stack trace frame pointers to be saved for every allocation. Specifying a larger value of numRecordedFrames means that stack traces, when given, will be more complete, but will also mean that both more CPU time and more memory per allocation will be consumed. If numRecordedFrames is not specified, a value of 12 will be assumed. Optionally specify basicAllocator, the allocator from which memory will be provided. If basicAllocator is 0, the MallocFreeAllocator singleton is used. Associate bsl::cerr with this object for error diagnostic output, which may be changed by calling the setOstream manipulator. Set the demanglingPreferringFlag attribute to true, which may be changed using the setDemanglingPreferredFlag manipulator. The behavior is undefined if numRecordedFrames < 2.

◆ ~StackTraceTestAllocator()

balst::StackTraceTestAllocator::~StackTraceTestAllocator ( )

Destroy this allocator. Report any memory leaks to the ostream that was supplied at construction. If no memory leaks are observed, nothing is written to the output ostream. Call the failure handler if numBlocksInUse() > 0. Note that a report of outstanding memory blocks is written to ostream before the failure handler is called, and if the failure handler returns, all outstanding memory blocks will be released.

Member Function Documentation

◆ allocate()

void * balst::StackTraceTestAllocator::allocate ( size_type  size)
virtual

Return a newly allocated block of memory of the specified positive size (in bytes). If size is 0, a null pointer is returned with no other other effect. Otherwise, invoke the allocate method of the allocator supplied at construction and record the returned block in order to be able to report leaked blocks upon destruction.

Implements bslma::Allocator.

◆ allocationLimit()

bsls::Types::Int64 balst::StackTraceTestAllocator::allocationLimit ( ) const

Return the current number of allocation requests left before an exception is thrown. A negative value indicates that no exception is scheduled.

◆ deallocate()

void balst::StackTraceTestAllocator::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. Otherwise, if the memory at address is consistent with being allocated from this test allocator, deallocate it using the underlying allocator and delete it from the data structures keeping track of blocks in use'. If address is not zero and is not the address of a block allocated with this allocator (or if it is being deallocated a second time), write an error message and call the failure handler.

Implements bslma::Allocator.

◆ failAbort()

static void balst::StackTraceTestAllocator::failAbort ( )
static

Calls bsl::abort(), d_failureHandler is initialized to this value by all constructors. Note that in ALL failure situations, errors or warnings will be written to the ostream associated with this object prior to the failure handler call.

◆ failNoop()

static void balst::StackTraceTestAllocator::failNoop ( )
static

Does nothing. setFailureHandler may be called with this function, in which case this allocator object, when a failure occurs, will recover rather than abort. Note that in ALL failure situations, errors or warnings will be written to the ostream associated with this object prior to the failure handler call.

◆ failureHandler()

const StackTraceTestAllocator::FailureHandler & balst::StackTraceTestAllocator::failureHandler ( ) const
inline

Return a reference to the function that will be called when a failure is observed.

◆ numAllocations()

bsls::Types::Int64 balst::StackTraceTestAllocator::numAllocations ( ) const
inline

Return the number of allocations from this object that have occurred since creation. Note that this does not count allocations of 0 length, which return null pointers.

◆ numBlocksInUse()

bsl::size_t balst::StackTraceTestAllocator::numBlocksInUse ( ) const
inline

◆ release()

void balst::StackTraceTestAllocator::release ( )
virtual

◆ reportBlocksInUse()

void balst::StackTraceTestAllocator::reportBlocksInUse ( bsl::ostream *  ostream = 0) const

Write a report to the specified ostream, reporting the unique call-stacks for each block that has been allocated and has not yet been freed. If ostream is not specified, the value of ostream passed to the last call to setOstream will be used. If setOstream was never called, bsl::cerr will be used.

◆ setAllocationLimit()

void balst::StackTraceTestAllocator::setAllocationLimit ( bsls::Types::Int64  limit)

Set the number of valid allocation requests before an exception is to be thrown for this allocator to the specified limit. If limit is less than 0, no exception is to be thrown. By default, no exception is scheduled.

◆ setDemanglingPreferredFlag()

void balst::StackTraceTestAllocator::setDemanglingPreferredFlag ( bool  value)

Set the demanglingPreferredFlag attribute, which is used to determine whether demangling of symbols is to be attempted when generating diagnostics, to the specified value. The default value of the flag is true. However the flag is ignored on some platforms; demangling never happens on some platforms and always happens on others.

◆ setFailureHandler()

void balst::StackTraceTestAllocator::setFailureHandler ( const FailureHandler func)

Set the failure handler associated with this allocator object to the specified func. Upon construction, the function failAbort is associated with this object by default. Note that func will be called by this object's destructor if memory is leaked, so it is important that it not throw. Note that in ALL failure situations, errors or warnings will be written to the ostream associated with this object prior to the call to the failure handler.

◆ setName()

void balst::StackTraceTestAllocator::setName ( const char *  name)

Set the name of this allocator to the specified name. If setName is never called, the name of the allocator is "<unnamed>". Note that the lifetime of name must exceed the lifetime of this object.

◆ setOstream()

void balst::StackTraceTestAllocator::setOstream ( bsl::ostream *  ostream)

Set the stream to which diagnostics will be written to the specified ostream. If setOstream is never called, diagnostics will be written to bsl::cerr.


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