Boost logo

Boost Users :

From: Seweryn Habdank-Wojewódzki (habdank_at_[hidden])
Date: 2007-04-11 08:26:00


Hi!

Can any one show me the proper example of usage of pool allocator.

I have two examples (code below) and usage of pool consumes a lot of memory,
and nothing is faster/better/etc.

So what should be a design goal for using pool allocator?

Kind regards.

The code without allocator:

#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>

namespace primes
{
    template < typename T >
    void PrimesLE( T const Limit, std::vector<T> & result ) {
        using namespace std;

        typedef vector<T> cont_t;
        result.clear();
        result.resize(static_cast<typename cont_t::size_type>(Limit/2+1));

        result[0] = static_cast<T>(2);
       
        typename cont_t::iterator pos = result.begin();

        T lim = 0;
        T sq = 4;
        T j = 1;
        for (T counter = 3; counter <= Limit; counter += 2 ) {
            if ( counter >= sq ) {
                ++lim;
                sq = result[lim] * result[lim];
            }

            T f = 1;
            for ( ; f < lim; ++f ) {
                if ( counter % result[f] == 0 )
                    break;
            }
           
            if ( f >= lim ) {
                result[j++] = counter;
            }
        }
        return;
    }
}

int main()
{
    using namespace std;
    typedef vector<int> Vi;
    Vi p;
    primes::PrimesLE(100000000,p);
   
    //Vi::const_iterator pos = p.begin();
    //Vi::const_iterator const end = p.end();
    //for ( ; pos != end && *pos > 0 ; ++pos ) {
    //    cout << *pos << endl;
    //}
    cout << "Done" << endl;
    cin.get();
}

The same code with allocator:

#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <boost/pool/pool_alloc.hpp>

namespace primes
{
    template < typename T >
    void PrimesLE( T const Limit,
std::vector<T,boost::fast_pool_allocator<T> > & result ) {
        using namespace std;

        typedef vector<T,boost::fast_pool_allocator<T> > cont_t;
        result.clear();
       
        result.push_back(static_cast<T>(2));
       
        T lim = 0;
        T sq = 4;
        for (T counter = 3; counter <= Limit; counter += 2 ) {
            if ( counter >= sq ) {
                ++lim;
                sq = result[lim] * result[lim];
            }

            T f = 1;
            for ( ; f < lim; ++f ) {
                if ( counter % result[f] == 0 )
                    break;
            }
           
            if ( f >= lim ) {
                result.push_back( counter );
            }
        }
        return;
    }
}

int main()
{
    using namespace std;
    vector<int,boost::fast_pool_allocator<int> > p;
    int const limit = 100000000;
    primes::PrimesLE(limit,p);
   
    //for ( size_t i = 0; i < limit/2+1 && p[i] > 0 ; ++i ) {
    //    cout << p[i] << endl;
    //}
    //boost::singleton_pool<boost::pool_allocator_tag,sizeof(int)>::release_memory();
    cout << "Done" << endl;
    cin.get();
}

-- 
|\/\/|   Seweryn Habdank-Wojewódzki
 \/\/

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net