|
BDE 4.14.0 Production release
|
#include <bslma_testallocator.h>
Public Member Functions | |
| TestAllocator (Allocator *basicAllocator=0) | |
| TestAllocator (const char *name, Allocator *basicAllocator=0) | |
| TestAllocator (bool verboseFlag, Allocator *basicAllocator=0) | |
| TestAllocator (const char *name, bool verboseFlag, Allocator *basicAllocator=0) | |
| ~TestAllocator () BSLS_KEYWORD_OVERRIDE | |
| void * | allocate (size_type size) BSLS_KEYWORD_OVERRIDE |
| void | deallocate (void *address) BSLS_KEYWORD_OVERRIDE |
| void | setAllocationLimit (bsls::Types::Int64 limit) |
| void | setNoAbort (bool flagValue) |
| void | setQuiet (bool flagValue) |
| void | setVerbose (bool flagValue) |
| bsls::Types::Int64 | allocationLimit () const |
| bool | isNoAbort () const |
| bool | isQuiet () const |
| bool | isVerbose () const |
| void * | lastAllocatedAddress () const |
| size_type | lastAllocatedNumBytes () const |
| Return the number of bytes of the most recent allocation request. | |
| void * | lastDeallocatedAddress () const |
| size_type | lastDeallocatedNumBytes () const |
| const char * | name () const |
| bsls::Types::Int64 | numAllocations () const |
| bsls::Types::Int64 | numBlocksInUse () const |
| bsls::Types::Int64 | numBlocksMax () const |
| bsls::Types::Int64 | numBlocksTotal () const |
| bsls::Types::Int64 | numBoundsErrors () const |
| bsls::Types::Int64 | numBytesInUse () const |
| bsls::Types::Int64 | numBytesMax () const |
| bsls::Types::Int64 | numBytesTotal () const |
| bsls::Types::Int64 | numDeallocations () const |
| bsls::Types::Int64 | numMismatches () const |
| void | print (std::FILE *f=stdout) const |
| int | status () const |
| void * | lastAllocateAddress () const |
| size_type | lastAllocateNumBytes () const |
| void * | lastDeallocateAddress () const |
| size_type | lastDeallocateNumBytes () const |
| bsls::Types::Int64 | numAllocation () const |
| bsls::Types::Int64 | numDeallocation () 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_resource & | operator= (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 |
Friends | |
| template<class t_OS > | |
| t_OS & | operator<< (t_OS &stream, const TestAllocator &ta) |
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 |
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.
|
explicit |
|
explicit |
|
explicit |
| bslma::TestAllocator::TestAllocator | ( | const char * | name, |
| bool | verboseFlag, | ||
| Allocator * | basicAllocator = 0 |
||
| ) |
Create an instrumented "test" allocator. Optionally specify a name (associated with this object) to be included in diagnostic messages written to stdout, thereby distinguishing this test allocator from others that might be used in the same program. If name is 0 (or not specified), no distinguishing name is incorporated in diagnostics. Optionally specify a verboseFlag indicating whether this test allocator should automatically report all allocation/deallocation events to stdout and print accumulated statistics on destruction. If verboseFlag is false (or not specified), allocation/deallocation and summary messages will not be written automatically. Optionally specify a basicAllocator used to supply memory. If basicAllocator is 0, the MallocFreeAllocator singleton is used.
| bslma::TestAllocator::~TestAllocator | ( | ) |
Destroy this allocator. In verbose mode, print all contained state values of this allocator object to stdout. Except in quiet mode, automatically report any memory leaks to stdout. Abort if either numBlocksInUse or numBytesInUse return non-zero unless in no-abort mode or quiet mode. Note that, in all cases, destroying this object has no effect on outstanding memory blocks allocated from this test allocator (and may result in memory leaks – e.g., if the (default) MallocFreeAllocator singleton was used).
|
virtual |
Return a newly-allocated block of memory of the specified size (in bytes). If size is 0, a null pointer is returned. Otherwise, invoke the allocate method of the allocator supplied at construction, increment the number of currently (and cumulatively) allocated blocks, and increase the number of currently allocated bytes by size. Update all other fields accordingly; if the allocation fails via an exception, numAllocations() is incremented, lastAllocatedNumBytes() is set to size, and lastDeallocatedAddress() is set to 0.
Implements bslma::Allocator.
|
inline |
Return the current number of allocation requests left before an exception is thrown. A negative value indicates that no exception is scheduled.
|
virtual |
Return the memory block at the specified address back to this allocator. If address is 0, this function has no effect (other than to record relevant statistics). Otherwise, if the memory at address is consistent with being allocated from this test allocator, decrement the number of currently allocated blocks, and decrease the number of currently allocated bytes by the size (in bytes) originally requested for the block. Although technically undefined behavior, if the memory can be determined not to have been allocated from this test allocator, increment the number of mismatches, and – unless in quiet mode – immediately report the details of the mismatch to stdout (e.g., as an std::hex memory dump) and abort.
Implements bslma::Allocator.
|
inline |
Return true if this allocator is currently in no-abort mode, and false otherwise. In no-abort mode all diagnostic messages are printed, but all aborts are suppressed. Note that quiet mode implies no-abort mode.
|
inline |
Return true if this allocator is currently in quiet mode, and false otherwise. In quiet mode, messages about mismatched deallocations, overrun/underrun errors, and memory leaks will not be displayed to stdout and will not cause the program to abort.
|
inline |
Return true if this allocator is currently in verbose mode, and false otherwise. In verbose mode, all allocation/deallocation events will be reported on stdout, as will summary statistics upon destruction of this object.
|
inline |
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).
DEPRECATED: use lastAllocatedAddress instead.
|
inline |
Return the address that was returned by the most recent allocation request. Return 0 if the most recent allocation request was for 0 bytes.
|
inline |
|
inline |
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.
DEPRECATED: use lastAllocatedNumBytes instead.
|
inline |
Return the memory address of the last memory deallocation request. Note that the address is always recorded regardless of the validity of the request.
DEPRECATED: use lastDeallocatedAddress instead.
|
inline |
Return the address that was supplied to the most recent deallocation request. Return 0 if a null pointer was most recently deallocated. Note that the address is always recorded regardless of the validity of the request.
|
inline |
Return the number of bytes of the most recent deallocation request. Return 0 if a null pointer was most recently deallocated, or if the request was invalid (e.g., an attempt to deallocate memory not allocated through this allocator).
|
inline |
Return the number of bytes of the most recent memory deallocation request. Return 0 if the request was invalid (e.g., deallocating memory not allocated through this allocator).
DEPRECATED: use lastDeallocatedNumBytes instead.
|
inline |
Return the name of this test allocator, or 0 if no name was specified at construction.
|
inline |
Return the cumulative number of allocation requests. Note that this number is incremented for every allocate invocation, regardless of the validity of the request.
DEPRECATED: use numAllocations instead.
|
inline |
Return the cumulative number of allocation requests. Note that this number is incremented for every allocate invocation.
|
inline |
Return the number of blocks currently allocated from this object. Note that numBlocksInUse() <= numBlocksMax().
|
inline |
Return the maximum number of blocks ever allocated from this object at any one time. Note that numBlocksInUse() <= numBlocksMax() <= numBlocksTotal().
|
inline |
Return the cumulative number of blocks ever allocated from this object. Note that numBlocksMax() <= numBlocksTotal().
|
inline |
Return the number of times memory deallocations have detected that pad areas at the front or back of the user segment had been overwritten.
|
inline |
Return the number of bytes currently allocated from this object. Note that numBytesInUse() <= numBytesMax().
|
inline |
Return the maximum number of bytes ever allocated from this object at any one time. Note that numBytesInUse() <= numBytesMax() <= numBytesTotal().
|
inline |
Return the cumulative number of bytes ever allocated from this object. Note that numBytesMax() <= numBytesTotal().
|
inline |
Return the cumulative number of deallocation requests. Note that this number is incremented for every deallocate invocation, regardless of the validity of the request.
DEPRECATED: use numDeallocations instead.
|
inline |
Return the cumulative number of deallocation requests. Note that this number is incremented for every deallocate invocation, regardless of the validity of the request.
|
inline |
Return the number of mismatched memory deallocations that have occurred since this object was created. A memory deallocation is mismatched if that memory was not allocated directly from this allocator.
| void bslma::TestAllocator::print | ( | std::FILE * | f = stdout | ) | const |
Write the accumulated state information held in this allocator to the optionally specified file f (default stdout) in a reasonable (multi-line) format.
|
inline |
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.
|
inline |
Set the no-abort mode for this test allocator to the specified (boolean) flagValue. If flagValue is true, aborting on fatal errors is suppressed, and the functions simply return. Diagnostics are not affected. Note that the default mode is to abort. Also note that this function is provided primarily to enable visual testing of diagnostic messages produced by this component.
|
inline |
Set the quiet mode for this test allocator to the specified (boolean) flagValue. If flagValue is true, mismatched allocations, overrun/underrun errors, and memory leak messages will not be displayed to stdout and the process will not abort as a result of such conditions. Note that the default mode is not quiet. Also note that this function is provided primarily to enable testing of this component; in quiet mode, situations that would otherwise abort will just quietly increment the numMismatches and/or numBoundsErrors counters.
|
inline |
Set the verbose mode for this test allocator to the specified (boolean) flagValue. If flagValue is true, all allocation/deallocation events will be reported automatically on stdout, as will accumulated statistics upon destruction of this object. Note that the default mode is not verbose.
| int bslma::TestAllocator::status | ( | ) | const |
Return 0 on success, and non-zero otherwise: If there have been any mismatched memory deallocations or over/under runs, return the number of such errors that have occurred as a positive number; if either 0 < numBlocksInUse() or 0 < numBytesInUse(), return an arbitrary negative number; else return 0.
|
friend |
Write the accumulated state information held in the specified allocator ta to the specified stream in a reasonable (multi-line) format identical to the format produced by ta.print() and return a reference offering modifiable access to stream. Output is performed via calls to stream.write(s, count), where write is a required method of class t_OS, s is a const char* holding the formatted output, and count is the length of s excluding any null terminator. Note that std::ostream meets the requirements for t_OS.