Concrete test allocator that tracks memory usage for testing.

Synopsis

Declared in <bslma_testallocator.h>

class TestAllocator
    : public Allocator

Description

This class defines a concrete "test" allocator mechanism that implements the Allocator protocol, and provides instrumentation to track (1) the number of blocks/bytes currently in use, (2) the maximum number of blocks/bytes that have been outstanding at any one time, and (3) the cumulative number of blocks/bytes that have ever been allocated by this test allocator object. The accumulated statistics are based solely on the number of bytes requested. Additional testing facilities include allocation limits, verbosity modes, status, and automated report printing.

Note that, unlike many other allocators, this allocator does NOT rely on the currently installed default allocator (see bslma_default), but instead ‐‐ by default ‐‐ uses the 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 Allocator protocol.

Base Classes

Name

Description

Allocator

Pure abstract protocol for clients and suppliers of raw memory.

Type Aliases

Name

Description

size_type

Alias for an unsigned integral type capable of representing the number of bytes in this platform's virtual address space.

Member Functions

Name

Description

TestAllocator [constructor]

Constructors

~TestAllocator [destructor] [virtual]

Destroy this allocator.

operator=

allocate

Return a newly‐allocated block of memory of the specified size.

allocationLimit

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

deallocate

Return the memory block at address back to this allocator.

deleteObject

deleteObject overloads

deleteObjectRaw

deleteObjectRaw overloads

getFillPattern

Return the current fill pattern value for this allocator. The behavior is undefined unless hasFillPattern() returns true.

hasFillPattern

Return true if a fill pattern is currently set for this allocator.

isNoAbort

Return true if this allocator is currently in no‐abort mode.

isQuiet

Return true if this allocator is currently in quiet mode.

isVerbose

Return true if this allocator is currently in verbose mode.

is_equal

lastAllocateAddress

Return the allocated memory address of the most recent memory request. Return 0 if the request was invalid (e.g., allocate non‐ positive number of bytes).

lastAllocateNumBytes

Return the number of bytes of the most recent memory request. Note that this number is always recorded regardless of the validity of the request.

lastAllocatedAddress

Return the address that was returned by the most recent allocation request. Return 0 if the most recent allocation request was for 0 bytes.

lastAllocatedNumBytes

Return the number of bytes of the most recent allocation request.

lastDeallocateAddress

Return the memory address of the last memory deallocation request. Note that the address is always recorded regardless of the validity of the request.

lastDeallocateNumBytes

Return the number of bytes of the most recent deallocation request.

lastDeallocatedAddress

Return the address supplied to the most recent deallocation request.

lastDeallocatedNumBytes

Return the number of bytes of the most recent deallocation request.

name

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

numAllocation

Return the cumulative number of allocation requests.

numAllocations

Return the cumulative number of allocation requests. Note that this number is incremented for every allocate invocation.

numBlocksInUse

Return the number of blocks currently allocated from this object. Note that numBlocksInUse() <= numBlocksMax().

numBlocksMax

Return the maximum number of blocks ever allocated from this object at any one time. Note that numBlocksInUse() <= numBlocksMax() <= numBlocksTotal().

numBlocksTotal

Return the cumulative number of blocks ever allocated from this object. Note that numBlocksMax() <= numBlocksTotal().

numBoundsErrors

Return the number of times memory deallocations have detected that pad areas at the front or back of the user segment had been overwritten.

numBytesInUse

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

numBytesMax

Return the maximum number of bytes ever allocated from this object at any one time. Note that numBytesInUse() <= numBytesMax() <= numBytesTotal().

numBytesTotal

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

numDeallocation

Return the cumulative number of deallocation requests.

numDeallocations

Return the cumulative number of deallocation requests.

numMismatches

Return the number of mismatched memory deallocations.

print

Write the accumulated state information held in this allocator to the optionally specified file f (default stdout) in a reasonable (multi‐line) format.

restoreStatistics

Restore statistics previously saved by stashStatistics.

setAllocationLimit

Set the allocation limit before an exception is thrown to limit.

setFillPattern

Set the fill pattern for newly allocated memory to pattern.

setNoAbort

Set the no‐abort mode for this test allocator to flagValue.

setQuiet

Set the quiet mode for this test allocator to flagValue.

setVerbose

Set the verbose mode for this test allocator to flagValue.

stashStatistics

Return the current statistics that may later be passed to restoreStatistics, and reset the current statistic as follows:

status

Return a status code summarizing mismatches and outstanding memory.

unsetFillPattern

Unset the fill pattern for this test allocator. After calling this method, newly allocated memory will not be initialized with any fill pattern.

Static Member Functions

Name

Description

throwBadAlloc

Throw std::bad_alloc or abort the program.

Protected Member Functions

Name

Description

do_allocate [virtual]

Return a newly allocated block of at least bytes with alignment.

do_deallocate [virtual]

Return the memory block at p having bytes and alignment.

do_is_equal [virtual]

Return whether this allocator can exchange memory with other.

Friends

Name

Description

BloombergLP::bslma::operator<<

Write the state of ta to stream and return stream.

Non-Member Functions

Name

Description

operator!=

Return true unless the specified rhs is a proxy for lhs.

operator==

Return true if the specified rhs is a proxy for the specified lhs.

Created with MrDocs