dynamic_pool.cc
Go to the documentation of this file.
00001 #include <utilmm/memory/dynamic_pool.hh>
00002 #include <utilmm/memory/sweep.hh>
00003 
00004 #include <boost/cstdint.hpp>
00005 
00006 using namespace utilmm::pools;
00007 
00008 dynamic_pool::dynamic_pool() : m_size(0) {}
00009 dynamic_pool::~dynamic_pool()
00010 { sweep_arrays(m_free.begin(), m_free.end()); }
00011 
00012 dynamic_pool::item_t* dynamic_pool::get_base(void* ptr)
00013 { 
00014     byte_t* base = reinterpret_cast<byte_t*>(ptr) - offsetof(item_t, payload);
00015     return reinterpret_cast<item_t*>(base); 
00016 }
00017 
00018 void* dynamic_pool::allocate(size_t size)
00019 {
00020     if (m_size < size)
00021     {
00022         sweep_arrays(m_free.begin(), m_free.end());
00023         m_free.clear();
00024         m_size = size;
00025     }
00026 
00027     item_t* item;
00028     if (m_free.empty())
00029     {
00030         byte_t* buffer = new byte_t[sizeof(item_t) + m_size];
00031         item = new(buffer) item_t;
00032         item->size = m_size;
00033     }
00034     else
00035     {
00036         item = m_free.back();
00037         m_free.pop_back();
00038     }
00039 
00040     return item->payload;
00041 }
00042 
00043 void dynamic_pool::deallocate(void* vbuffer)
00044 {
00045     if (! vbuffer) return;
00046         
00047     item_t* item = get_base(vbuffer);
00048 
00049     if (item->size != m_size)
00050         delete[] item;
00051     else
00052         m_free.push_back(item);
00053 }
00054 


utilmm
Author(s): Sylvain Joyeux/sylvain.joyeux@m4x.org
autogenerated on Mon Oct 6 2014 03:17:01