MemPool.h 761 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #ifndef __MAS_MEMPOOL_H
  2. #define __MAS_MEMPOOL_H
  3. #include "Types.h"
  4. #include "Locks.h"
  5. #include "MAS_Definition.h"
  6. NAMESPACE_MAS_BEGIN
  7. #define MEMPOOL MemPool::Instance()
  8. class MemPool {
  9. public:
  10. static MemPool * Instance();
  11. static void DestroyInstance();
  12. TZ_INT Initialize();
  13. TZ_INT Dispose();
  14. // return nullptr when failed
  15. void * AllocAvailMem(TZ_Uint32 expect);
  16. TZ_INT ReturnFreeMem(void * memBlock, TZ_Uint32 memSize);
  17. TZ_Uint32 GetAvailCount(TZ_Uint32 expect);
  18. private:
  19. MemPool();
  20. ~MemPool();
  21. private:
  22. tzc::Mutex m_lock;
  23. std::vector<std::list<void *>> m_memDepot;
  24. const TZ_Uint32 MIN_BLOCK_SIZE = 4096;
  25. const TZ_Uint32 BLOCK_LIST_COUNT = 16;
  26. static tzc::Mutex _mutex;
  27. static MemPool * _instance;
  28. };
  29. NAMESPACE_MAS_END
  30. #endif