C++ Boost::Pool::allocator














































C++ Boost::Pool::allocator



Description :
         Boost.Pool also provides the class boost::pool_allocator, which you can pass as an allocator to containers and also provides an allocator called boost::fast_pool_allocator.
    The allocator provides memory required by the container.     
        The class is an allocator that is usually passed as a second template parameter to containers from the standard library.
         If the macro BOOST_POOL_NO_MT is defined then multithreading support for Boost.Pool is disabled.

Header File Used :
         #include <boost/pool/pool_alloc.hpp>.

Similarities :
        Both boost::pool_allocator and boost::fast_pool_allocator is based on boost::singleton_pool.
        Both accepts the same parameters.
        The tag is defined by Boost.Pool and is used by both boost::pool_allocator and boost::fast_pool_allocator for the internal . Like if you need to release memory, you have to use a tag to access boost::singleton_pool and call purge_memory() or release_memory().

Differences :
       boost::pool_allocator should be preferred if you are requesting contiguous segments while boost::fast_pool_allocator can be used if segments are requested one by one.
      boost::pool_allocator used for std::vector  and boost::fast_pool_allocator for std::list .

Functions :
      Functions are already mentioned in these articles. (singleton_pool, object_pool, simple_segregated_storage).

Program1 :  (boost::pool_allocator)
      
#include <boost/pool/pool_alloc.hpp>
#include<bits/stdc++.h>
using namespace std;
int main()
{
std::vector<int, boost::pool_allocator<int>> v;
for (int i = 0; i < 100; ++i)
v.push_back(i);

for (auto it =v.begin(); it <v.end(); ++it)
cout<<*it<<" ";
v.clear();
boost::singleton_pool<boost::pool_allocator_tag, sizeof(int)>::
purge_memory();
}

Illustration :
When Program1 calls push_back() for the first time, v accesses the allocator to get the requested
memory. Because the allocator boost::pool_allocator is used, a memory block with space for 32 int
values is allocated. v receives the pointer to the first segment in that memory block that has the
size of an int.
With every subsequent call to push_back(), another segment is used from the memory block until
the allocator detects that a bigger memory block is required.

Output :

 Program2 :  (boost::fast_pool_allocator)
       
#define BOOST_POOL_NO_MT
#include <boost/pool/pool_alloc.hpp>
#include <bits/stdc++.h>
using namespace std;
int main()
{
typedef boost::fast_pool_allocator<int,
boost::default_user_allocator_new_delete,
boost::details::pool::default_mutex,
64, 128> allocator;

std::list<int, allocator> l;
for (int i = 0; i < 100; ++i)
l.push_back(i);
for (auto it =l.begin(); it!=l.end(); ++it)
cout<<*it<<" ";
l.clear();
boost::singleton_pool<boost::fast_pool_allocator_tag, sizeof(int)>::
purge_memory();
}

Illustration :
The last two parameters passed to boost::fast_pool_allocator in Program2 set the size of the
first memory block and the maximum size of memory blocks to allocate.
boost::default_user_allocator_new_delete is a class that allocates memory blocks with new and
releases them with delete[]. You can also use boost::default_user_allocator_malloc_free, which calls
malloc() and free().
     boost::details::pool::default_mutex is a type that is set to boost::mutex or boost::details::pool::null_mutex. boost::mutex is the default type that supports multiple threads requesting memory from the allocator.
     The allocator in Program2 uses a null mutex.
      
Output :

// Thank You And Happy Coding.

Comments