fbxmemorypool.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /****************************************************************************************
  2. Copyright (C) 2015 Autodesk, Inc.
  3. All rights reserved.
  4. Use of this software is subject to the terms of the Autodesk license agreement
  5. provided at the time of installation or download, or which otherwise accompanies
  6. this software in either electronic or hard copy form.
  7. ****************************************************************************************/
  8. //! \file fbxmemorypool.h
  9. #ifndef _FBXSDK_CORE_BASE_MEMORY_H_
  10. #define _FBXSDK_CORE_BASE_MEMORY_H_
  11. #include <fbxsdk/fbxsdk_def.h>
  12. #include <fbxsdk/core/sync/fbxatomic.h>
  13. #include <fbxsdk/fbxsdk_nsbegin.h>
  14. /** \brief Class to create a simple fixed-size-blocks memory pool to allocate memory dynamically. */
  15. class FBXSDK_DLL FbxMemoryPool
  16. {
  17. public:
  18. /** Memory pool constructor.
  19. * \param pBlockSize The size of one memory block.
  20. * \param pBlockCount The count of block that should be pre-allocated.
  21. * \param pResizable Whether memory pool can grow if no block are availalbe upon calling Allocate.
  22. * \param pConcurrent Whether the pool supports concurrent allocation and release operations.
  23. * \remark All memory blocks must be released before the memory pool is destroyed, otherwise a memory leak will occur. */
  24. FbxMemoryPool(size_t pBlockSize, FbxInt64 pBlockCount=0, bool pResizable=true, bool pConcurrent=true);
  25. /** Memory pool destructor. Upon destruction, all memory blocks of the pool will be de-allocated. */
  26. ~FbxMemoryPool();
  27. /** Free memory of all memory blocks from this memory pool, also effectively resetting the block count to zero.
  28. * \remark The block size and alignment/resize/concurrent support will remain unchanged. */
  29. void Reset();
  30. /** Allocate or lock a memory block for usage.
  31. * \return An memory block pointer that can be NULL if the memory pool cannot grow in size and no blocks are available. */
  32. void* Allocate();
  33. /** Dispose or unlock a memory block.
  34. * \param pMemBlock A pointer to the memory block to release. This will not free the block's memory, instead simply putting it back in the available stack. */
  35. void Release(void* pMemBlock);
  36. /*****************************************************************************************************************************
  37. ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
  38. *****************************************************************************************************************************/
  39. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  40. private:
  41. void* Pop();
  42. FbxInt64 mMaxBlockCount;
  43. FbxAtomic mFreeBlockCount;
  44. void* mFreeBlocksStack;
  45. size_t mBlockSize;
  46. bool mResizable;
  47. bool mSupportConcurrentAccess;
  48. #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
  49. };
  50. #include <fbxsdk/fbxsdk_nsend.h>
  51. #endif /* _FBXSDK_CORE_BASE_MEMORY_H_ */