18 #ifndef INCLUDE_RCF_OBJECTPOOL_HPP 19 #define INCLUDE_RCF_OBJECTPOOL_HPP 26 #include <RCF/MemStream.hpp> 27 #include <RCF/Tools.hpp> 28 #include <RCF/ThreadLibrary.hpp> 32 static const std::size_t CbSize = 128;
36 class RCF_EXPORT CbAllocatorBase
40 CbAllocatorBase(ObjectPool & objectPool);
41 CbAllocatorBase(
const CbAllocatorBase & rhs);
44 void deallocate(
void * pcb) ;
47 ObjectPool & mObjectPool;
51 class CbAllocator :
public CbAllocatorBase
56 typedef value_type* pointer;
57 typedef std::size_t size_type;
58 typedef std::ptrdiff_t difference_type;
63 typedef CbAllocator<U> other;
66 CbAllocator(ObjectPool & objectPool) : CbAllocatorBase(objectPool)
71 CbAllocator(
const CbAllocator<U> & rhs) : CbAllocatorBase(rhs)
79 static_assert(
sizeof(T) <= CbSize,
"Invalid type T." );
81 RCF_UNUSED_VARIABLE(cnt);
82 return reinterpret_cast<pointer
>(CbAllocatorBase::allocate());
85 void deallocate(pointer p, size_type)
87 CbAllocatorBase::deallocate(p);
94 TypeInfo(
const std::type_info & ti) : mpTypeInfo(&ti)
99 bool operator<(
const TypeInfo & rhs)
const 101 return (*mpTypeInfo).before(*rhs.mpTypeInfo) ? true :
false;
105 const std::type_info * mpTypeInfo;
109 typedef std::shared_ptr<ReallocBuffer> ReallocBufferPtr;
123 void enableCaching(std::size_t maxCount, std::function<
void(T *)> clearFunc)
125 enableCaching( (T *) NULL, maxCount, clearFunc);
132 disableCaching( (T *) NULL);
136 void enableCaching(T *, std::size_t maxCount, std::function<
void(T *)> clearFunc)
138 RCF::WriteLock lock(mObjPoolMutex);
139 RCF::TypeInfo ti(
typeid(T) );
140 mObjPool[ti].reset(
new RCF::ObjectPool::ObjList() );
141 mObjPool[ti]->mMaxSize = maxCount;
142 mObjPool[ti]->mOps.reset(
new RCF::ObjectPool::Ops<T>(clearFunc) );
146 void disableCaching(T *)
148 RCF::WriteLock lock(mObjPoolMutex);
149 RCF::TypeInfo ti(
typeid(T) );
150 mObjPool[ti]->mMaxSize = 0;
151 mObjPool[ti]->clear();
156 bool isCachingEnabled(T *)
158 ReadLock lock(mObjPoolMutex);
159 if (!mObjPool.empty())
161 RCF::TypeInfo ti(
typeid(T) );
162 ObjPool::iterator iter = mObjPool.find(ti);
163 if (iter != mObjPool.end())
165 if (iter->second->mMaxSize > 0)
174 MemOstreamPtr getMemOstreamPtr();
175 ReallocBufferPtr getReallocBufferPtr();
177 void enumerateWriteBuffers(std::vector<std::size_t> & bufferSizes);
178 void enumerateReadBuffers(std::vector<std::size_t> & bufferSizes);
180 void setBufferCountLimit(std::size_t bufferCountLimit);
181 std::size_t getBufferCountLimit();
183 void setBufferSizeLimit(std::size_t bufferSizeLimit);
184 std::size_t getBufferSizeLimit();
191 void getObj(std::shared_ptr<T> & objPtr,
bool alwaysCreate =
true)
195 std::shared_ptr<void> spv;
196 bool pfnDeleter =
false;
199 ReadLock poolLock(mObjPoolMutex);
201 if (mObjPool.empty())
214 TypeInfo ti(
typeid(T) );
215 ObjPool::iterator iter = mObjPool.find(ti);
216 if (iter == mObjPool.end())
229 ObjList & objList = *(iter->second);
230 Lock listLock(objList.mMutex);
231 if (objList.mMaxSize == 0)
242 else if (objList.mVec.empty())
249 pv = objList.mVec.back();
250 pt =
static_cast<T *
>(pv);
251 objList.mVec.pop_back();
261 TypeInfo ti(
typeid(T) );
265 objPtr = std::shared_ptr<T>(
267 std::bind(&ObjectPool::putObj,
this, ti, std::placeholders::_1),
268 CbAllocator<void>(*this) );
272 objPtr = std::shared_ptr<T>(pt);
276 void putObj(
const TypeInfo & ti,
void * pv)
278 ReadLock readLock(mObjPoolMutex);
279 RCF_ASSERT(!mObjPool.empty());
280 ObjPool::iterator iter = mObjPool.find(ti);
281 RCF_ASSERT(iter != mObjPool.end());
282 ObjList & objList = *(iter->second);
283 Lock lock(objList.mMutex);
284 if (objList.mVec.size() >= objList.mMaxSize)
288 objList.mOps->kill(pv);
292 objList.mOps->clear(pv);
293 objList.mVec.push_back(pv);
301 virtual void kill(
void * pv) = 0;
302 virtual void clear(
void * pv) = 0;
306 class Ops :
public I_Ops
309 Ops(std::function<
void(T *)> clearFunc) :
310 mClearFunc(clearFunc)
316 T * pt =
static_cast<T *
>(pv);
320 void clear(
void * pv)
324 T * pt =
static_cast<T *
>(pv);
329 std::function<void(T *)> mClearFunc;
332 class ObjList : Noncopyable
335 ObjList() : mMaxSize(0)
339 std::size_t mMaxSize;
340 std::vector<void *> mVec;
341 std::unique_ptr<I_Ops> mOps;
345 for (std::size_t i=0; i<mVec.size(); ++i)
353 typedef std::shared_ptr<ObjList> ObjListPtr;
355 typedef std::map< TypeInfo, ObjListPtr > ObjPool;
356 ReadWriteMutex mObjPoolMutex;
361 friend class CbAllocatorBase;
364 void putPcb(
void * pcb);
366 void putMemOstream(MemOstream * pOs);
367 void putReallocBuffer(ReallocBuffer * pRb);
369 std::size_t mBufferCountLimit;
370 std::size_t mBufferSizeLimit;
373 std::vector< MemOstream * > mOsPool;
376 std::vector< ReallocBuffer * > mRbPool;
379 std::vector< void * > mCbPool;
381 template<
typename T,
typename Spt,
typename PtrList,
typename Pfn>
386 Mutex & ptrListMutex,
392 Lock lock(ptrListMutex);
407 spt = std::shared_ptr<T>(
409 std::bind(pfn,
this, std::placeholders::_1),
410 CbAllocator<void>(*this) );
420 #endif // ! INCLUDE_RCF_OBJECTPOOL_HPP void enableCaching(std::size_t maxCount, std::function< void(T *)> clearFunc)
Definition: ObjectPool.hpp:123
Manages a cache of objects of various types.
Definition: ObjectPool.hpp:112
Definition: AmiIoHandler.hpp:23
void disableCaching()
Disables caching of objects of type T.
Definition: ObjectPool.hpp:130
void getObj(std::shared_ptr< T > &objPtr, bool alwaysCreate=true)
Definition: ObjectPool.hpp:191