TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/host/memory/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         typedef MemoryPool This;
00038 
00039 public:
00040         MemoryPool( const std::size_t maxSize = 0 );
00041         ~MemoryPool();
00042 
00043         IPoolDataPtr allocate( const std::size_t size );
00044         std::size_t  updateMemoryAuthorizedWithRAM();
00045 
00046         void referenced( PoolData* );
00047         void released( PoolData* );
00048 
00049         std::size_t getUsedMemorySize() const;
00050         std::size_t getAllocatedAndUnusedMemorySize() const;
00051         std::size_t getAllocatedMemorySize() const;
00052         std::size_t getMaxMemorySize() const;
00053         std::size_t getAvailableMemorySize() const;
00054         std::size_t getWastedMemorySize() const;
00055 
00056         std::size_t getDataUsedSize() const;
00057         std::size_t getDataUnusedSize() const;
00058         
00059         PoolData* getOneAvailableData( const size_t size );
00060 
00061         void clear( std::size_t size );
00062         void clear();
00063         void clearOne();
00064 
00065         friend std::ostream& operator<<( std::ostream& os, const This& v );
00066 
00067 private:
00068         typedef boost::unordered_set<PoolData*> DataList;
00069         boost::ptr_list<PoolData> _allDatas; // the owner
00070         std::map<char*, PoolData*> _dataMap;
00071         DataList _dataUsed;
00072         DataList _dataUnused;
00073         std::size_t _memoryAuthorized;
00074         mutable boost::mutex _mutex;
00075 };
00076 
00077 #ifndef SWIG
00078 std::ostream& operator<<( std::ostream& os, const MemoryPool& memoryPool );
00079 #endif
00080 
00081 }
00082 }
00083 }
00084 
00085 #endif
00086