dynamic_pool.hh
Go to the documentation of this file.
00001 #ifndef __UTILS_SLABS_H__
00002 #define __UTILS_SLABS_H__
00003 
00004 #include <vector>
00005 #include <boost/cstdint.hpp>
00006 
00007 namespace utilmm
00008 {
00009     namespace pools
00010     {
00011         template<typename T>
00012         class dynamic_auto;
00013 
00023         class dynamic_pool
00024         {
00025             template<typename T>
00026             friend class dynamic_auto;
00027                 
00028         public:
00029             typedef boost::uint32_t size_t;
00030 
00031         private:
00032             typedef boost::uint8_t byte_t;
00033             
00034             struct item_t
00035             {
00036                 size_t  size;
00037                 byte_t payload[];
00038             };
00039 
00040             typedef std::vector<item_t*> Buffers;
00041 
00042             static item_t* get_base(void* ptr);
00043 
00044             size_t  m_size;
00045             Buffers m_listpool, m_free;
00046 
00047         public:
00048             dynamic_pool();
00049             ~dynamic_pool();
00050 
00051             void* allocate(size_t size);
00052             void deallocate(void* vbuffer);
00053         };
00054 
00055 
00056         template<typename T>
00057         class dynamic_auto
00058         {
00059             T* m_ptr;
00060 
00061             struct safe_bool_struct
00062             { void method(); };
00063             typedef void (safe_bool_struct::*safe_bool)();
00064 
00065         public:
00066             dynamic_auto(T* t) throw()
00067                 : m_ptr(t) {}
00068 
00069             ~dynamic_auto()
00070             { 
00071                 if (m_ptr)
00072                     delete[] dynamic_pool::get_base(m_ptr); 
00073             }
00074 
00075             dynamic_auto(dynamic_auto const& copy) throw()
00076             {
00077                 m_ptr = copy.m_ptr;
00078                 copy.m_ptr = 0;
00079             }
00080 
00081             void reset(T* t) throw()
00082             { m_ptr = t; }
00083 
00084             operator safe_bool() const throw()
00085             { return m_ptr ? &safe_bool_struct::method : 0; }
00086 
00087             T* get() throw() { return m_ptr; }
00088             T const* get() const throw() { return m_ptr; }
00089             T* release() throw()
00090             {
00091                 T* temp = m_ptr;
00092                 m_ptr = 0;
00093                 return temp;
00094             }
00095         };
00096 
00097     }
00098 }
00099 
00100 #endif
00101 
00102 


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