|
BDE 4.14.0 Production release
|
Functions | |
| void * | operator new (bsl::size_t size, BloombergLP::bdlma::SequentialPool &pool) |
| void | operator delete (void *address, BloombergLP::bdlma::SequentialPool &pool) |
Provide sequential memory using dynamically-allocated buffers.
This component provides a fast sequential memory pool, bdlma::SequentialPool, that dispenses heterogeneous memory blocks (of varying, user-specified sizes) from a dynamically-allocated internal buffer. If an allocation request exceeds the remaining free memory space in the internal buffer, the pool either replenishes its buffer with new memory to satisfy the request, or returns a separate memory block, depending on whether the request size exceeds an optionally-specified maximum buffer size. 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::SequentialPool is typically used when fast allocation and deallocation is needed, but the user does not know in advance the maximum amount of memory needed.
An optional initialSize parameter can be supplied at construction to specify the initial size of the internal buffer. If initialSize is not supplied, an implementation-defined value is used for the initial internal size of the buffer.
If initialSize is specified, an optional maxBufferSize parameter can be supplied at construction to specify the maximum buffer size 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 maxBufferSize >= initialSize. Note that reserveCapacity always ensures that the requested number of bytes is available (allocating a new internal buffer if necessary) regardless of whether the size of the request exceeds maxBufferSize.
An optional growthStrategy parameter can be supplied at construction to specify the growth rate of the dynamically-allocated buffers. The buffers can grow either geometrically or remain constant in size. If growthStrategy is not specified, geometric growth is used. See bsls_blockgrowth for more details.
An optional alignmentStrategy parameter can be supplied at construction to specify the memory alignment strategy. Allocated memory blocks can either follow maximum alignment, natural alignment, or 1-byte alignment. If alignmentStrategy is not specified, natural alignment is used. See bsls_alignment for more details.
This section illustrates intended use of this component.
Suppose we define a container class, my_IntDoubleArray, 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. For efficient memory allocation, we can use a bdlma::SequentialPool for memory allocation:
The use of a sequential pool and the release method allows the removeAll method to quickly deallocate memory of all elements:
The sequential pool optimizes the allocation of memory by using dynamically-allocated buffers to supply memory. This greatly reduces the amount of dynamic allocation needed:
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::SequentialPool. bdlma::SequentialPool 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_sequentialallocator 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: