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