Allocator.h
#include <deque>
#include <algorithm>
#include <boost/bind.hpp>
#include <boost/pool/pool_alloc.hpp>
#include <boost/pool/object_pool.hpp>
#include <boost/scoped_ptr.hpp>
template<class TAllocator>
class buffer_alloc
{
private:
boost::scoped_ptr<TAllocator> _impl;
public:
typedef typename TAllocator::buffer_type buffer_type;
buffer_alloc():_impl(new TAllocator)
{
}
~buffer_alloc()
{
}
buffer_type* alloc()
{
return _impl->alloc();
}
void free(buffer_type* buffer)
{
_impl->free(buffer);
}
};
template<class Tbuffer>
class alloc_impl_mypool
{
std::deque<Tbuffer*> _dequeue;
Tbuffer* _buffers;
public:
typedef Tbuffer buffer_type;
enum{ max_pool_size = 100000};
alloc_impl_mypool()
{
makeBuffer(max_pool_size);
}
~alloc_impl_mypool()
{
delete []_buffers;
}
Tbuffer* alloc()
{
if( _dequeue.empty() )
return NULL;
Tbuffer* buffer = _dequeue.back();
_dequeue.pop_back();
return buffer;
}
void free(Tbuffer* buffer)
{
insert(*buffer);
}
private:
void makeBuffer(int buffercount)
{
_buffers = new Tbuffer[buffercount];
std::for_each(_buffers, _buffers+buffercount,
boost::bind(&alloc_impl_mypool::insert, this, _1));
}
void print(Tbuffer& buffer)
{
std::cout << buffer.body_length() << std::endl;
}
void insert(Tbuffer& buffer)
{
_dequeue.push_front(&buffer);
}
};
template<class Tbuffer>
class alloc_impl
{
public:
typedef Tbuffer buffer_type;
Tbuffer* alloc()
{
return new Tbuffer();
}
void free(Tbuffer* buffer)
{
delete buffer;
}
private:
};
template<class Tbuffer>
class alloc_impl_boost_pool
{
boost::object_pool<Tbuffer> pool;
public:
typedef Tbuffer buffer_type;
Tbuffer* alloc()
{
return pool.construct();
}
void free(Tbuffer* buffer)
{
pool.destroy(buffer);
}
private:
};
template<class Tbuffer>
class alloc_impl_boost_allocate
{
boost::pool_allocator<Tbuffer> pool;
public:
typedef Tbuffer buffer_type;
Tbuffer* alloc()
{
Tbuffer* temp;
temp = pool.allocate(1,0);
new (temp) Tbuffer();
return temp;
}
void free(Tbuffer* buffer)
{
buffer->~Tbuffer();
boost::pool_allocator<Tbuffer>::deallocate(buffer, 1);
}
};
template<class Tbuffer>
class alloc_impl_boost_fast_allocate
{
boost::fast_pool_allocator<Tbuffer> pool;
public:
typedef Tbuffer buffer_type;
Tbuffer* alloc()
{
Tbuffer* temp;
temp = pool.allocate(1,0);
new (temp) Tbuffer();
return temp;
}
void free(Tbuffer* buffer)
{
buffer->~Tbuffer();
boost::fast_pool_allocator<Tbuffer>::deallocate(buffer, 1);
}
};
Main.cpp
#include "message_buffer.h"
#include "allocator.h"
#include <boost/timer.hpp>
#define MAX_NEW_CNT 50000
template<class TAllocator>
void test(std::string funcName)
{
std::vector<TAllocator::buffer_type*> vt;
vt.reserve(MAX_NEW_CNT);
buffer_alloc<TAllocator> pool;
boost::timer t;
for( int i = 0; i < MAX_NEW_CNT; i++)
{
vt.push_back(pool.alloc());
}
for( int i = 0; i < MAX_NEW_CNT; i++)
{
pool.free(vt[i]);
}
std::cout << funcName << " : " << t.elapsed() << std::endl;
vt.clear();
}
int _tmain(int argc, _TCHAR* argv[])
{
test<alloc_impl_mypool<message_buffer> >("미리잡아놓고dequeue를이용한풀[message_buffer]");
test<alloc_impl<message_buffer> >("기본new/delete[message_buffer]");
test<alloc_impl_boost_pool<message_buffer> >("boost::object_pool[message_buffer]");
test<alloc_impl_boost_allocate<message_buffer> >("boost::pool_allocator[message_buffer]");
test<alloc_impl_boost_fast_allocate<message_buffer> >("boost::fast_pool_allocator[message_buffer]");
test<alloc_impl_mypool<int> >("미리잡아놓고dequeue를이용한풀[int]");
test<alloc_impl<int> >("기본new/delete[int]");
test<alloc_impl_boost_pool<int> >("boost::object_pool[int]");
test<alloc_impl_boost_allocate<int> >("boost::pool_allocator[int]");
test<alloc_impl_boost_fast_allocate<int> >("boost::fast_pool_allocator[int]");
return 0;
}