BDE 4.14.0 Production release
|
Functions | |
void * | operator new (bsl::size_t size, BloombergLP::bdlma::BufferedSequentialPool &pool) |
void | operator delete (void *address, BloombergLP::bdlma::BufferedSequentialPool &pool) |
Provide sequential memory using an external buffer and a fallback.
This component provides a maximally efficient sequential memory pool, bdlma::BufferedSequentialPool
, that dispenses heterogeneous memory blocks (of varying, user-specified sizes) from an external buffer. If an allocation request exceeds the remaining free memory space in the external buffer, the pool will fall back to a sequence of dynamically allocated buffers. Users can optionally specify a growth strategy at construction that governs the growth rate of the dynamically-allocated buffers. If no growth strategy is specified at construction, geometric growth is used. Users can also optionally specify an alignment strategy at construction that governs the alignment of allocated memory blocks. If no alignment strategy is specified, natural alignment is used. The release
method releases all memory allocated through the pool, as does the destructor. The rewind
method releases all memory allocated through the pool and returns to the underlying allocator only memory that was allocated outside of the typical internal buffer growth of the pool (i.e., large blocks). Note that individually allocated memory blocks cannot be separately deallocated.
A bdlma::BufferedSequentialPool
is typically used when users have a reasonable estimation of the amount of memory needed. This amount of memory would typically be created directly on the program stack, and used as the initial external buffer of the pool for fast memory allocation. While the buffer has sufficient capacity, memory allocations using the pool will not trigger any dynamic memory allocation, will have optimal locality of reference, and will not require deallocation upon destruction.
Once the external buffer is exhausted, subsequent allocation requests require dynamic memory allocation, and the performance of the pool degrades.
An optional maxBufferSize
parameter can be supplied at construction to specify the maximum size (in bytes) of the dynamically-allocated buffers for geometric growth. Once the internal buffer grows up to the maxBufferSize
, further requests that exceed this size will be served by a separate memory block instead of the internal buffer. The behavior is undefined unless size <= maxBufferSize
, where size
is the extent (in bytes) of the external buffer supplied at construction.
Note that, even when a buffer having n
bytes of memory is supplied at construction, it does not mean that n
bytes of memory are available before dynamic memory allocation is triggered. This is due to memory alignment requirements. If the buffer supplied is not aligned, the first call to the allocate
method may automatically skip one or more bytes such that the memory allocated is properly aligned. The number of bytes that are wasted depends on whether natural alignment, maximum alignment, or 1-byte alignment is used (see bsls_alignment for more details).
This section illustrates intended use of this component.
Suppose we define a container class, my_BufferedIntDoubleArray
, that holds both int
and double
values. The class can be implemented using two parallel arrays: one storing the type information, and the other storing pointers to the int
and double
values. Furthermore, if we can approximate the amount of memory needed, we can use a bdlma::BufferedSequentialPool
for memory allocation for maximum efficiency:
The use of a buffered sequential pool and the release
method allows the removeAll
method to quickly deallocate memory of all elements:
The buffered sequential pool optimizes the allocation of memory by using a buffer supplied at construction. As described in the "DESCRIPTION" section, the need for all dynamic memory allocations are eliminated provided that the buffer is not exhausted. The pool provides maximal memory allocation efficiency:
Note that in the destructor, all outstanding memory blocks are deallocated automatically when d_pool
is destroyed:
bslma::Allocator
is used throughout the interfaces of BDE components. Suppose we would like to create a fast allocator, my_FastAllocator
, that allocates memory from a buffer in a similar fashion to bdlma::BufferedSequentialPool
. bdlma::BufferedSequentialPool
can be used directly to implement such an allocator.
Note that the documentation for this class is simplified for this usage example. Please see bdlma_bufferedsequentialallocator for full documentation of a similar class.
|
inline |
Use the specified pool
to deallocate the memory at the specified address
. The behavior is undefined unless address
was allocated using pool
and has not already been deallocated. This operator is supplied solely to allow the compiler to arrange for it to be called in case of an exception.
|
inline |
Return a block of memory of the specified size
(in bytes) allocated from the specified pool
. Note that an object may allocate additional memory internally, requiring the allocator to be passed in as a constructor argument:
Also note that the analogous version of operator delete
should not be called directly. Instead, this component provides a static template member function, deleteObject
, parameterized by TYPE
that performs the following: