BDE 4.14.0 Production release
|
Provide efficient allocation of memory blocks for a specific type.
This component implements a memory pool, bslstl::SimplePool
, that allocates and manages memory blocks of for a parameterized type. A bslstl::SimplePool
object maintains an internal linked list of free memory blocks, and dispenses one block for each allocate
method invocation. When a memory block is deallocated, it is returned to the free list for potential reuse.
Whenever the linked list of free memory blocks is depleted, bslstl::SimplePool
replenishes the list by first allocating a large, contiguous "chunk" of memory, then splitting the chunk into multiple memory blocks each having the sizeof
the simple pool's parameterized type. A chunk and its constituent memory blocks can be depicted visually:
This pool implementation is simple because its allocation strategy is not configurable. The size of a chunk starts from 1 memory block, and doubles each time a chunk is allocated up to an implementation defined maximum number of blocks.
There are a few differences between bslstl::SimplePool
and bdema_Pool
:
bslstl::SimplePool
is parameterized on both allocator and type, which improve performance and memory usage in exchange for increase in code size.bslstl::SimplePool
uses the allocator through the use of bsl::allocator_traits
(which is generally not relevant to non-container type.bslstl::SimplePool
is less configurable in order to achieve abstraction of allocation and improvement in performance.Clients are encouraged to use bdema_Pool
as bslstl::SimplePool
is designed for node-based STL containers, and its pooling behavior may change according to the needs of those containers.
This section illustrates intended use for this component.
Suppose that we want to implement a stack with a linked list. It is expensive to allocate memory every time a node is inserted. Therefore, we can use SimplePool
to efficiently manage the memory for the list.
First, we define the class that implements the stack:
Now, we define the implementation of the stack. Notice how bslstl::SimplePool
is used to allocate memory in push
and deallocate memory in pop
:
Finally, we test our stack by pushing and popping some elements: