[#BloombergLP-bslma-TestAllocator]
= xref:BloombergLP.adoc[BloombergLP]::xref:BloombergLP/bslma.adoc[bslma]::TestAllocator
:relfileprefix: ../../
:mrdocs:

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

== Synopsis

Declared in `&lt;bslma&lowbar;testallocator&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
class TestAllocator
    : public xref:BloombergLP/bslma/Allocator.adoc[Allocator]
----

== Description

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

== Base Classes

[cols="1,4"]
|===
| Name| Description
| `xref:BloombergLP/bslma/Allocator.adoc[Allocator]`
| This protocol class provides a pure abstract interface and contract for clients and suppliers of raw memory&period; If the requested memory cannot be returned, the contract requires that an `std&colon;&colon;bad&lowbar;alloc` exception be thrown&period; Note that memory is guaranteed to be sufficiently aligned for any object of the requested size on the current platform, which may be less than the maximal alignment guarantee afforded by global `operator new`&period;
|===

== Type Aliases

[cols="1,4"]
|===
| Name| Description
| xref:BloombergLP/bslma/Allocator/size_type.adoc[`size&lowbar;type`] 
| Alias for an unsigned integral type capable of representing the number of bytes in this platform&apos;s virtual address space&period;
|===


== Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:BloombergLP/bslma/TestAllocator/2constructor-0b1.adoc[`TestAllocator`]         [.small]#[constructor]#
| Constructors
| xref:BloombergLP/bslma/TestAllocator/2destructor.adoc[`&#126;TestAllocator`] [.small]#[destructor]# [.small]#[virtual]#
| Destroy this allocator&period; In verbose mode, print all contained state values of this allocator object to `stdout`&period; Except in quiet mode, automatically report any memory leaks to `stdout`&period; Abort if either `numBlocksInUse` or `numBytesInUse` return non&hyphen;zero unless in no&hyphen;abort mode or quiet mode&period; 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 &hyphen;&hyphen; e&period;g&period;, if the (default) `MallocFreeAllocator` singleton was used)&period;
| xref:BloombergLP/bslma/Allocator/operator_assign.adoc[`operator&equals;`] 
| 
| xref:BloombergLP/bslma/TestAllocator/allocate-0e.adoc[`allocate`] 
| Return a newly&hyphen;allocated block of memory of the specified `size` (in bytes)&period; If `size` is 0, a null pointer is returned&period; 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`&period; Update all other fields accordingly&semi; if the allocation fails via an exception, `numAllocations()` is incremented, `lastAllocatedNumBytes()` is set to `size`, and `lastDeallocatedAddress()` is set to 0&period;
| xref:BloombergLP/bslma/TestAllocator/allocationLimit.adoc[`allocationLimit`] 
| Return the current number of allocation requests left before an exception is thrown&period; A negative value indicates that no exception is scheduled&period;
| xref:BloombergLP/bslma/TestAllocator/deallocate-0c.adoc[`deallocate`] 
| Return the memory block at the specified `address` back to this allocator&period; If `address` is 0, this function has no effect (other than to record relevant statistics)&period; 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&period; 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 &hyphen;&hyphen; unless in quiet mode &hyphen;&hyphen; immediately report the details of the mismatch to `stdout` (e&period;g&period;, as an `std&colon;&colon;hex` memory dump) and abort&period;
| xref:BloombergLP/bslma/Allocator/deleteObject-09.adoc[`deleteObject`] 
| `deleteObject` overloads
| xref:BloombergLP/bslma/Allocator/deleteObjectRaw-06f.adoc[`deleteObjectRaw`] 
| `deleteObjectRaw` overloads
| xref:BloombergLP/bslma/TestAllocator/getFillPattern.adoc[`getFillPattern`] 
| Return the current fill pattern value for this allocator&period; The behavior is undefined unless `hasFillPattern()` returns `true`&period;
| xref:BloombergLP/bslma/TestAllocator/hasFillPattern.adoc[`hasFillPattern`] 
| Return `true` if a fill pattern is currently set for this allocator, and `false` otherwise&period; When a fill pattern is set, newly allocated memory is filled with the pattern value&period;
| xref:BloombergLP/bslma/TestAllocator/isNoAbort.adoc[`isNoAbort`] 
| Return `true` if this allocator is currently in no&hyphen;abort mode, and `false` otherwise&period; In no&hyphen;abort mode all diagnostic messages are printed, but all aborts are suppressed&period; Note that quiet mode implies no&hyphen;abort mode&period;
| xref:BloombergLP/bslma/TestAllocator/isQuiet.adoc[`isQuiet`] 
| Return `true` if this allocator is currently in quiet mode, and `false` otherwise&period; In quiet mode, messages about mismatched deallocations, overrun&sol;underrun errors, and memory leaks will not be displayed to `stdout` and will not cause the program to abort&period;
| xref:BloombergLP/bslma/TestAllocator/isVerbose.adoc[`isVerbose`] 
| Return `true` if this allocator is currently in verbose mode, and `false` otherwise&period; In verbose mode, all allocation&sol;deallocation events will be reported on `stdout`, as will summary statistics upon destruction of this object&period;
| xref:BloombergLP/bslma/Allocator/is_equal.adoc[`is&lowbar;equal`] 
| 
| xref:BloombergLP/bslma/TestAllocator/lastAllocateAddress.adoc[`lastAllocateAddress`] 
| Return the allocated memory address of the most recent memory request&period; Return 0 if the request was invalid (e&period;g&period;, allocate non&hyphen; positive number of bytes)&period;
| xref:BloombergLP/bslma/TestAllocator/lastAllocateNumBytes.adoc[`lastAllocateNumBytes`] 
| Return the number of bytes of the most recent memory request&period; Note that this number is always recorded regardless of the validity of the request&period;
| xref:BloombergLP/bslma/TestAllocator/lastAllocatedAddress.adoc[`lastAllocatedAddress`] 
| Return the address that was returned by the most recent allocation request&period; Return 0 if the most recent allocation request was for 0 bytes&period;
| xref:BloombergLP/bslma/TestAllocator/lastAllocatedNumBytes.adoc[`lastAllocatedNumBytes`] 
| Return the number of bytes of the most recent allocation request&period;
| xref:BloombergLP/bslma/TestAllocator/lastDeallocateAddress.adoc[`lastDeallocateAddress`] 
| Return the memory address of the last memory deallocation request&period; Note that the address is always recorded regardless of the validity of the request&period;
| xref:BloombergLP/bslma/TestAllocator/lastDeallocateNumBytes.adoc[`lastDeallocateNumBytes`] 
| Return the number of bytes of the most recent memory deallocation request&period; Return 0 if the request was invalid (e&period;g&period;, deallocating memory not allocated through this allocator)&period;
| xref:BloombergLP/bslma/TestAllocator/lastDeallocatedAddress.adoc[`lastDeallocatedAddress`] 
| Return the address that was supplied to the most recent deallocation request&period; Return 0 if a null pointer was most recently deallocated&period; Note that the address is always recorded regardless of the validity of the request&period;
| xref:BloombergLP/bslma/TestAllocator/lastDeallocatedNumBytes.adoc[`lastDeallocatedNumBytes`] 
| Return the number of bytes of the most recent deallocation request&period; Return 0 if a null pointer was most recently deallocated, or if the request was invalid (e&period;g&period;, an attempt to deallocate memory not allocated through this allocator)&period;
| xref:BloombergLP/bslma/TestAllocator/name.adoc[`name`] 
| Return the name of this test allocator, or 0 if no name was specified at construction&period;
| xref:BloombergLP/bslma/TestAllocator/numAllocation.adoc[`numAllocation`] 
| Return the cumulative number of allocation requests&period; Note that this number is incremented for every `allocate` invocation, regardless of the validity of the request&period;
| xref:BloombergLP/bslma/TestAllocator/numAllocations.adoc[`numAllocations`] 
| Return the cumulative number of allocation requests&period; Note that this number is incremented for every `allocate` invocation&period;
| xref:BloombergLP/bslma/TestAllocator/numBlocksInUse.adoc[`numBlocksInUse`] 
| Return the number of blocks currently allocated from this object&period; Note that `numBlocksInUse() &lt;&equals; numBlocksMax()`&period;
| xref:BloombergLP/bslma/TestAllocator/numBlocksMax.adoc[`numBlocksMax`] 
| Return the maximum number of blocks ever allocated from this object at any one time&period; Note that `numBlocksInUse() &lt;&equals; numBlocksMax() &lt;&equals; numBlocksTotal()`&period;
| xref:BloombergLP/bslma/TestAllocator/numBlocksTotal.adoc[`numBlocksTotal`] 
| Return the cumulative number of blocks ever allocated from this object&period; Note that `numBlocksMax() &lt;&equals; numBlocksTotal()`&period;
| xref:BloombergLP/bslma/TestAllocator/numBoundsErrors.adoc[`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&period;
| xref:BloombergLP/bslma/TestAllocator/numBytesInUse.adoc[`numBytesInUse`] 
| Return the number of bytes currently allocated from this object&period; Note that `numBytesInUse() &lt;&equals; numBytesMax()`&period;
| xref:BloombergLP/bslma/TestAllocator/numBytesMax.adoc[`numBytesMax`] 
| Return the maximum number of bytes ever allocated from this object at any one time&period; Note that `numBytesInUse() &lt;&equals; numBytesMax() &lt;&equals; numBytesTotal()`&period;
| xref:BloombergLP/bslma/TestAllocator/numBytesTotal.adoc[`numBytesTotal`] 
| Return the cumulative number of bytes ever allocated from this object&period; Note that `numBytesMax() &lt;&equals; numBytesTotal()`&period;
| xref:BloombergLP/bslma/TestAllocator/numDeallocation.adoc[`numDeallocation`] 
| Return the cumulative number of deallocation requests&period; Note that this number is incremented for every `deallocate` invocation, regardless of the validity of the request&period;
| xref:BloombergLP/bslma/TestAllocator/numDeallocations.adoc[`numDeallocations`] 
| Return the cumulative number of deallocation requests&period; Note that this number is incremented for every `deallocate` invocation, regardless of the validity of the request&period;
| xref:BloombergLP/bslma/TestAllocator/numMismatches.adoc[`numMismatches`] 
| Return the number of mismatched memory deallocations that have occurred since this object was created&period; A memory deallocation is _mismatched_ if that memory was not allocated directly from this allocator&period;
| xref:BloombergLP/bslma/TestAllocator/print.adoc[`print`] 
| Write the accumulated state information held in this allocator to the optionally specified file `f` (default `stdout`) in a reasonable (multi&hyphen;line) format&period;
| xref:BloombergLP/bslma/TestAllocator/restoreStatistics.adoc[`restoreStatistics`] 
| Restore the statistics from the specified `savedStatistics` by appropriately combining the current values and the saved values&period; The behavior is undefined unless `savedStatistics` is a value returned by a previous call to `stashStatictics` on this same object, which has not been passed to `restoreStatistics` yet&period; This method restores the state of the statistics as if the corresponding `stashStatistics` call has never happened, in the following manner&colon;
| xref:BloombergLP/bslma/TestAllocator/setAllocationLimit.adoc[`setAllocationLimit`] 
| Set the number of valid allocation requests before an exception is to be thrown for this allocator to the specified `limit`&period; If `limit` is less than 0, no exception is to be thrown&period; By default, no exception is scheduled&period;
| xref:BloombergLP/bslma/TestAllocator/setFillPattern.adoc[`setFillPattern`] 
| Set the fill pattern for this test allocator to the specified 64&hyphen;bit `pattern`&period; Newly allocated memory will be filled with the specified pattern value&period; Note that the fill pattern, if set, is applied to the entire user segment of allocated memory blocks&period;
| xref:BloombergLP/bslma/TestAllocator/setNoAbort.adoc[`setNoAbort`] 
| Set the no&hyphen;abort mode for this test allocator to the specified (boolean) `flagValue`&period; `If flagValue` is `true`, aborting on fatal errors is suppressed, and the functions simply return&period; Diagnostics are not affected&period; Note that the default mode is to abort&period; Also note that this function is provided primarily to enable visual testing of diagnostic messages produced by this component&period;
| xref:BloombergLP/bslma/TestAllocator/setQuiet.adoc[`setQuiet`] 
| Set the quiet mode for this test allocator to the specified (boolean) `flagValue`&period; If `flagValue` is `true`, mismatched allocations, overrun&sol;underrun errors, and memory leak messages will not be displayed to `stdout` and the process will not abort as a result of such conditions&period; Note that the default mode is _not_ quiet&period; Also note that this function is provided primarily to enable testing of this component&semi; in quiet mode, situations that would otherwise abort will just quietly increment the `numMismatches` and&sol;or `numBoundsErrors` counters&period;
| xref:BloombergLP/bslma/TestAllocator/setVerbose.adoc[`setVerbose`] 
| Set the verbose mode for this test allocator to the specified (boolean) `flagValue`&period; If `flagValue` is `true`, all allocation&sol;deallocation events will be reported automatically on `stdout`, as will accumulated statistics upon destruction of this object&period; Note that the default mode is _not_ verbose&period;
| xref:BloombergLP/bslma/TestAllocator/stashStatistics.adoc[`stashStatistics`] 
| Return the current statistics that may later be passed to `restoreStatistics`, and reset the current statistic as follows&colon;
| xref:BloombergLP/bslma/TestAllocator/status.adoc[`status`] 
| Return 0 on success, and non&hyphen;zero otherwise&colon; If there have been any mismatched memory deallocations or over&sol;under runs, return the number of such errors that have occurred as a positive number&semi; if either `0 &lt; numBlocksInUse()` or `0 &lt; numBytesInUse()`, return an arbitrary negative number&semi; else return 0&period;
| xref:BloombergLP/bslma/TestAllocator/unsetFillPattern.adoc[`unsetFillPattern`] 
| Unset the fill pattern for this test allocator&period; After calling this method, newly allocated memory will not be initialized with any fill pattern&period;
|===


== Static Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:BloombergLP/bslma/Allocator/throwBadAlloc.adoc[`throwBadAlloc`] 
| Throw `std&colon;&colon;bad&lowbar;alloc` if exceptions are enabled or abort the program otherwise&period; Derived classes and helper functions will typically call this function when they are unable to satisfy an allocation request&period; This function never returns&period;
|===


== Protected Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:BloombergLP/bslma/Allocator/do_allocate.adoc[`do&lowbar;allocate`]  [.small]#[virtual]#
| Return a newly allocated block of memory of (at least) the specified positive `bytes` and having at least the specified `alignment`&period; Unless overriden in a derived class, the return value is `this&hyphen;&gt;allocate(bytes)`&period; If this allocator cannot return the requested number of bytes or cannot satisfy the alignment request, then it will throw a `std&colon;&colon;bad&lowbar;alloc` exception in an exception&hyphen;enabled build, or else will abort the program in a non&hyphen;exception build&period; Unless overriden in a derived class, this function will forward the allocation request to the `allocate` virtual function, padding `bytes` and adjusting the return value as necessary to ensure sufficient alignment&period; Note that if `bytes` is `0`, the same non&hyphen;null value will be returned every time&period;
| xref:BloombergLP/bslma/Allocator/do_deallocate.adoc[`do&lowbar;deallocate`]  [.small]#[virtual]#
| Return the memory block at the specified `p` address, having the specified `bytes` and specified `alignment`, back to this allocator&period; Unless overriden in a derived class, this function will forward the deallocation request to the `deallocate` virtual function, padding `bytes` and adjusting `p` as necessary to account for `alignment` values other than the natural alignment for an object of size `bytes`&period; The behavior is undefined unless `address` is a block allocated from this allocator object using the same `bytes` and `alignment` and not already deallocated&period;
| xref:BloombergLP/bslma/Allocator/do_is_equal.adoc[`do&lowbar;is&lowbar;equal`]  [.small]#[virtual]#
| Return `true` if this allocator is equal to the specified `other` allocator, meaning (at least) that a memory block allocated by one can be deallocated by the other&semi; otherwise return `false`&period; Unless overriden, this method returns `this &equals;&equals; &amp;other`&period;
|===


== Friends

[cols="1,4"]
|===
| Name| Description
| `xref:BloombergLP/bslma/operator_lshift.adoc[BloombergLP::bslma::operator&lt;&lt;]`
| Write the accumulated state information held in the specified allocator `ta` to the specified `stream` in a reasonable (multi&hyphen;line) format identical to the format produced by `ta&period;print()` and return a reference offering modifiable access to `stream`&period; Output is performed via calls to `stream&period;write(s, count)`, where `write` is a required method of class `t&lowbar;OS`, `s` is a `const char&ast;` holding the formatted output, and `count` is the length of `s` excluding any null terminator&period; Note that `std&colon;&colon;ostream` meets the requirements for `t&lowbar;OS`&period;
|===

== Non-Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:BloombergLP/bslma/operator_not_eq-0a.adoc[`operator!&equals;`]
| Return `true` unless the specified `rhs` is a proxy for the specified `lhs`, in which case return `false`&period;
| xref:BloombergLP/bslma/operator_eq-0b.adoc[`operator&equals;&equals;`]
| Return `true` if the specified `rhs` is a proxy for the specified `lhs`, and `false` otherwise&period;
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
