BDE 4.14.0 Production release
|
Provide a memory-pooling allocator of heterogeneous block sizes.
This component provides a general-purpose, managed allocator, bdlma::MultipoolAllocator
, that implements the bdlma::ManagedAllocator
protocol and provides an allocator that maintains a configurable number of bdlma::Pool
objects, each dispensing maximally-aligned memory blocks of a unique size. The bdlma::Pool
objects are placed in an array, starting at index 0, with each successive pool managing memory blocks of a size twice that of the previous pool. Each multipool allocation (deallocation) request allocates memory from (returns memory to) the internal pool managing memory blocks of the smallest size not less than the requested size, or else from a separately managed list of memory blocks, if no internal pool managing memory blocks of sufficient size exists. Both the release
method and the destructor of a bdlma::MultipoolAllocator
release all memory currently allocated via the object.
The main difference between a bdlma::MultipoolAllocator
and a bdlma::Multipool
is that, very often, a bdlma::MultipoolAllocator
is managed through a bslma::Allocator
pointer. Hence, every call to the allocate
method invokes a virtual function call, which is slower than invoking the non-virtual allocate
method on a bdlma::Multipool
. However, since bslma::Allocator *
is widely used across BDE interfaces, bdlma::MultipoolAllocator
is more general purpose than bdlma::Multipool
.
When creating a bdlma::MultipoolAllocator
, clients can optionally configure:
A default-constructed multipool allocator has a relatively small, implementation-defined number of pools, N
, with respective block sizes ranging from 2^3 = 8
to 2^(N+2)
. By default, the initial chunk size, (i.e., the number of blocks of a given size allocated at once to replenish a pool's memory) is 1, and each pool's chunk size grows geometrically until it reaches an implementation-defined maximum, at which it is capped. Finally, unless otherwise specified, all memory comes from the allocator that was the currently installed default allocator at the time the bdlma::MultipoolAllocator
was created.
Using the various pooling options described above, we can configure the number of pools maintained, whether replenishment should be adaptive (i.e., geometric starting with 1) or fixed at a maximum chunk size, what that maximum chunk size should be (which need not be an integral power of 2), and the underlying allocator used to supply memory. Note that both GROWTH STRATEGY and MAX BLOCKS PER CHUNK can be specified separately either as a single value applying to all of the maintained pools, or as an array of values, with the elements applying to each individually maintained pool.
This section illustrates intended use of this component.
A bdlma::MultipoolAllocator
can be used to supply memory to node-based data structures such as bsl::set
, bsl::list
, and bsl::map
. Suppose we are implementing a container of named graphs, where a graph is defined by a set of edges and a set of nodes. The various fixed-sized nodes and edges can be efficiently allocated by a bdlma::MultipoolAllocator
.
First, the edge class, my_Edge
, is defined as follows:
Then, the node class, my_Node
, is defined as follows:
Then, we define the graph class, my_Graph
, as follows:
Next, the container for the collection of named graphs, my_NamedGraphContainer
, is defined as follows:
Finally, in main
, we can create a bdlma::MultipoolAllocator
and pass it to our my_NamedGraphContainer
. Since we know that the maximum block size needed is 32 (sizeof(my_Graph)
), we can calculate the number of pools needed by using the formula given in the "Configuration at Construction" section:
When solved for the above equation, the smallest N
that satisfies this relationship is 3:
A bdlma::MultipoolAllocator
can greatly improve efficiency when it is used to supply memory to node-based data structures that frequently both insert and remove nodes, while growing to significant size before being destroyed. The following experiment will illustrate the benefits of using a bdlma::MultipoolAllocator
under this scenario by comparing the following 3 different allocator uses:
bslma::NewDeleteAllocator
.bdlma::MultipoolAllocator
as a substitute for the bslma::NewDeleteAllocator
.bdlma::MultipoolAllocator
by avoiding invocation of the destructor of the data structure, since the destruction of the allocator will automatically reclaim all memory.First, we create a test data structure that contains three bsl::list
s. Each list holds a different type of object, where each type has a different size. For simplicity, we create these different object types as different instantiations of a template class, parameterized on the object size:
Again, for simplicity, the sizes of these objects are chosen to be 20, 40, and 80, instead of being parameterized as part of the test data structure:
The test will consist of the following steps:
d_list1
, then d_list2
, then d_list3
.d_list1
, then d_list2
, then d_list3
.The above 3 steps will be repeated n
times, where n
is a parameter specified by the user. This process will both grow the list and incorporate a large number of node removals. Note that nodes are removed from the front of the list to simulate a particular real-world usage, where nodes removed are rarely those recently added (this also removes the possibility of noise from potential optimizations with relinquishing memory blocks that are most recently allocated).
The push
method will push into the 3 bsl::list
objects managed by my_TestDataStructure
sequentially. Similarly, the pop
method will pop from the lists sequentially:
The above push
and pop
methods will allow us to evaluate the cost to add and remove nodes using different allocators. To evaluate the cost of destruction (and hence deallocation of all allocated memory in the list objects), we supply a static
test
method within a my_TestUtil
class to create the test mechanism, run the test, and destroy the test mechanism.
The test
method accepts a testLengthFactor
argument specified by the user to control the length of the test. The effect of testLengthFactor
is shown below:
For each row of the specified testLengthFactor
, a my_TestDataStructure
will be created "iterations" times, and each time the lists within the data structure will grow by invoking push
twice and pop
once. Note that "n" measures the effect of insertion and removal of nodes, while "iterations" measures the effect of construction and destruction of entire lists of nodes.
The test
method also accepts a bslma::Allocator *
to be used as the allocator used to construct the test mechanism and its internal lists:
Next, to fully test the benefit of being able to relinquish all allocated memory at once, we use the testManaged
method, which accepts only managed allocators. Instead of creating the test mechanism on the stack, the test mechanism will be created on the heap. After running the test, the release
method of the allocator will reclaim all outstanding allocations at once, eliminating the need to run the destructor of the test mechanism:
Finally, in main, we run the test with the different allocators and different allocator configurations based on command line arguments:
An excerpt of the results of the test running on IBM under optimized mode, using default constructed bdlma::MultipoolAllocator
parameters, is shown below:
It is clear that using a bdlma::MultipoolAllocator
results in an improvement in memory allocation by a factor of about 4. Furthermore, if the managed aspect of the multipool allocator is exploited, the cost of destruction rapidly decreases in relative terms as the list grows larger (increasing n
).