BDE 4.14.0 Production release
|
Typedefs | |
typedef bslma::BufferAllocator | bslma_BufferAllocator |
This alias is defined for backward compatibility. | |
Support efficient memory allocations from a user-supplied buffer.
This component provides an allocator, bslma::BufferAllocator
, that implements the bslma::Allocator
protocol and sequentially allocates memory blocks from a fixed-size buffer that is supplied by the user at construction. If an allocation request exceeds the remaining space in the buffer, the return value is the result of invoking an optional callback function that was supplied at construction, or zero if no callback was specified. This component also provides static utility functions for allocating memory directly from a user-specified buffer:
The bslma::BufferAllocator
allocates memory using one of the two alignment strategies: 1) MAXIMUM ALIGNMENT or 2) NATURAL ALIGNMENT.
bsls::AlignmentUtil::BSLS_MAX_ALIGNMENT
.int
, etc.) is naturally aligned when it's size evenly divides its address. An object of an aggregate type has natural alignment if the alignment of the most-restrictively aligned sub-object evenly divides the address of the aggregate. Natural alignment is always at least as restrictive as the compiler's required alignment. When only the size of an aggregate is known, and not its composition, we compute the alignment by finding the largest integral power of 2 (up to and including bsls::AlignmentUtil::BSLS_MAX_ALIGNMENT
) that divides the requested (non-zero) number of bytes. This computed alignment is guaranteed to be at least as restrictive as any sub-object within the aggregate.The bslma::BufferAllocator
class defined in this component is commonly used to allocate memory from a static buffer that does not require "deletion". The following snippet of code creates an array that obtains its memory from a fixed-sized buffer, but through the bslma::Allocator
protocol:
The implementation of my_ShortArray
is as follows:
Use buffer allocator to allocate memory for my_ShortArray
as follows:
Note that in the main
function above, memory
does not need to be aligned because bslma::BufferAllocator::allocate
internally performs alignment for each requested memory block based on the allocator's alignment strategy.