[#BloombergLP-bdlma-CountingAllocator]
= xref:BloombergLP.adoc[BloombergLP]::xref:BloombergLP/bdlma.adoc[bdlma]::CountingAllocator
:relfileprefix: ../../
:mrdocs:

Concrete allocator that counts bytes currently in use and ever allocated&period;

== Synopsis

Declared in `&lt;bdlma&lowbar;countingallocator&period;h&gt;`

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

== Description

This class defines a concrete &quot;counting&quot; allocator mechanism that implements the `bslma&colon;&colon;Allocator` protocol, and provides instrumentation to track&colon; (1) the number of bytes currently in use, and (2) the cumulative number of bytes that have ever been allocated&period; The accumulated statistics are based solely on the number of bytes requested (see `allocate`)&period;

Note that, like many other allocators, this allocator relies on the currently installed default allocator (see `bslma&lowbar;default`)&period; Clients may, however, override this allocator by supplying (at construction) any other allocator implementing the `bslma&colon;&colon;Allocator` protocol provided that it is fully thread&hyphen;safe&period;

== Base Classes

[cols="1,4"]
|===
| Name| Description
| `xref:BloombergLP/bslma/Allocator.adoc[bslma::Allocator]`
| Pure abstract protocol for clients and suppliers of raw memory&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/bdlma/CountingAllocator/2constructor-09.adoc[`CountingAllocator`]         [.small]#[constructor]#
| Constructors
| xref:BloombergLP/bdlma/CountingAllocator/2destructor.adoc[`&#126;CountingAllocator`] [.small]#[destructor]# [.small]#[virtual]#
| Destroy this allocator object&period; Note that destroying this allocator has no effect on any outstanding allocated memory&period;
| xref:BloombergLP/bslma/Allocator/operator_assign.adoc[`operator&equals;`] 
| 
| xref:BloombergLP/bdlma/CountingAllocator/allocate-04.adoc[`allocate`] 
| `allocate` overloads
| xref:BloombergLP/bdlma/CountingAllocator/deallocate-0d.adoc[`deallocate`] 
| Return the memory block at the specified `address` back to this allocator&period;
| xref:BloombergLP/bslma/Allocator/deleteObject-09.adoc[`deleteObject`] 
| `deleteObject` overloads
| xref:BloombergLP/bslma/Allocator/deleteObjectRaw-06f.adoc[`deleteObjectRaw`] 
| `deleteObjectRaw` overloads
| xref:BloombergLP/bslma/Allocator/is_equal.adoc[`is&lowbar;equal`] 
| 
| xref:BloombergLP/bdlma/CountingAllocator/name.adoc[`name`] 
| Return the name of this counting allocator, or 0 if no name was specified at construction&period;
| xref:BloombergLP/bdlma/CountingAllocator/numBytesInUse.adoc[`numBytesInUse`] 
| Return the number of bytes currently allocated from this object&period; Note that `numBytesInUse() &lt;&equals; numBytesTotal()`&period;
| xref:BloombergLP/bdlma/CountingAllocator/numBytesTotal.adoc[`numBytesTotal`] 
| Return the cumulative number of bytes ever allocated from this object&period; Note that `numBytesInUse() &lt;&equals; numBytesTotal()`&period;
| xref:BloombergLP/bdlma/CountingAllocator/print.adoc[`print`] 
| Write the accumulated state information held in this allocator to the specified `stream` in some reasonable (multi&hyphen;line) format, and return a reference to `stream`&period;
|===


== Static Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:BloombergLP/bslma/Allocator/throwBadAlloc.adoc[`throwBadAlloc`] 
| Throw `std&colon;&colon;bad&lowbar;alloc` or abort the program&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 at least `bytes` with `alignment`&period;
| xref:BloombergLP/bslma/Allocator/do_deallocate.adoc[`do&lowbar;deallocate`]  [.small]#[virtual]#
| Return the memory block at `p` having `bytes` and `alignment`&period;
| xref:BloombergLP/bslma/Allocator/do_is_equal.adoc[`do&lowbar;is&lowbar;equal`]  [.small]#[virtual]#
| Return whether this allocator can exchange memory with `other`&period;
|===



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