Quick Links:

bal | bbl | bdl | bsl

Public Types | Public Member Functions | Static Public Member Functions

balst::StackTraceTestAllocator Class Reference

#include <balst_stacktracetestallocator.h>

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

List of all members.

Public Types

typedef bsl::function< void()> FailureHandler
typedef bsls::Types::size_type size_type

Public Member Functions

 StackTraceTestAllocator (bslma::Allocator *basicAllocator=0)
 StackTraceTestAllocator (int numRecordedFrames, bslma::Allocator *basicAllocator=0)
virtual ~StackTraceTestAllocator ()
virtual void * allocate (size_type size)
virtual void deallocate (void *address)
virtual void release ()
void setDemanglingPreferredFlag (bool value)
void setFailureHandler (const FailureHandler &func)
void setName (const char *name)
void setOstream (bsl::ostream *ostream)
const FailureHandlerfailureHandler () const
bsl::size_t numBlocksInUse () const
void reportBlocksInUse (bsl::ostream *ostream=0) const
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 failAbort ()
static void failNoop ()
static void throwBadAlloc ()

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 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 Component balst_stacktracetestallocator


Member Typedef Documentation

Type of functor called by this object to handle failures. Note that this can be set and accessed using th setFailureHandler and failureHandler methods respectively.


Constructor & Destructor Documentation

balst::StackTraceTestAllocator::StackTraceTestAllocator ( bslma::Allocator basicAllocator = 0  )  [explicit]
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.

virtual balst::StackTraceTestAllocator::~StackTraceTestAllocator (  )  [virtual]

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

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.

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.

virtual 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.

virtual 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.

virtual void balst::StackTraceTestAllocator::release (  )  [virtual]

Deallocate all memory held by this allocator.

Implements bdlma::ManagedAllocator.

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.

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.

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.

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.

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

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

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

Return the number of blocks currently allocated from this object.

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.

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.

template<class TYPE >
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.

template<class TYPE >
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.


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