TuttleOFX  1
MemoryPool.hpp
Go to the documentation of this file.
00001 #ifndef _TUTTLE_HOST_CORE_MEMORYPOOL_HPP_
00002 #define _TUTTLE_HOST_CORE_MEMORYPOOL_HPP_
00003 
00004 #include "IMemoryPool.hpp"
00005 
00006 #include <boost/ptr_container/ptr_list.hpp>
00007 #include <boost/unordered_set.hpp>
00008 #include <boost/thread.hpp>
00009 
00010 #include <map>
00011 #include <list>
00012 #include <sstream>
00013 #include <numeric>
00014 #include <functional>
00015 #include <climits>
00016 
00017 namespace tuttle {
00018 namespace host {
00019 namespace memory {
00020 
00021 class PoolData; ///< forward declaration
00022 class IPool
00023 {
00024 public:
00025         virtual ~IPool()                     = 0;
00026         virtual void referenced( PoolData* ) = 0;
00027         virtual void released( PoolData* )   = 0;
00028 };
00029 
00030 /**
00031  * @todo tuttle: virtual destructor or nothing in virtual
00032  */
00033 class MemoryPool : public IMemoryPool
00034         , public IPool
00035 {
00036 public:
00037         MemoryPool( const std::size_t maxSize = 0 );
00038         ~MemoryPool();
00039 
00040         IPoolDataPtr allocate( const std::size_t size );
00041         std::size_t  updateMemoryAuthorizedWithRAM();
00042 
00043         void referenced( PoolData* );
00044         void released( PoolData* );
00045 
00046         std::size_t getUsedMemorySize() const;
00047         std::size_t getAllocatedAndUnusedMemorySize() const;
00048         std::size_t getAllocatedMemorySize() const;
00049         std::size_t getMaxMemorySize() const;
00050         std::size_t getAvailableMemorySize() const;
00051         std::size_t getWastedMemorySize() const;
00052 
00053         std::size_t getDataUsedSize() const;
00054         std::size_t getDataUnusedSize() const;
00055         
00056         void clear( std::size_t size );
00057         void clear();
00058         void clearOne();
00059 
00060 private:
00061         typedef boost::unordered_set<PoolData*> DataList;
00062         boost::ptr_list<PoolData> _allDatas; // the owner
00063         std::map<char*, PoolData*> _dataMap;
00064         DataList _dataUsed;
00065         DataList _dataUnused;
00066         std::size_t _memoryAuthorized;
00067         mutable boost::mutex _mutex;
00068 };
00069 /*
00070 #ifndef SWIG
00071 std::ostream& operator<<( std::ostream& os, const MemoryPool& memoryPool );
00072 #endif
00073 */
00074 }
00075 }
00076 }
00077 
00078 #endif
00079