35#include <unordered_map>
42typedef struct _memoryNode {
55 static std::shared_ptr<MemoryPool> mMemoryPoolInstance;
56 std::mutex mMemoryPoolMutex;
58 MemoryNode* mFreeListHead;
59 MemoryNode* mFreeListTail;
60 MemoryNode* mAllocatedListHead;
65 int32_t addNodesToFreeList(int32_t blockCount);
98 std::unordered_map<std::type_index, MemoryPool*> mMemoryPoolRefs;
99 std::mutex mPoolWrapperMutex;
101 MemoryPool* getMemoryPool(std::type_index typeIndex);
103 int32_t makeAllocation(int32_t blockCount, int32_t blockSize, std::type_index typeIndex);
104 void* getBlock(int32_t blockSize, std::type_index typeIndex);
105 void freeBlock(std::type_index typeIndex,
void* block);
116 template <
typename T>
117 int32_t makeAllocation(int32_t blockCount) {
118 return makeAllocation(blockCount,
sizeof(T), std::type_index(
typeid(T)));
127 template <
typename T>
129 return getBlock(
sizeof(T), std::type_index(
typeid(T)));
138 typename std::enable_if<std::is_class<T>::value,
void>::type
139 freeBlock(
void* block) {
140 reinterpret_cast<T*
>(block)->~T();
141 freeBlock(std::type_index(
typeid(T)), block);
149 typename std::enable_if<!std::is_class<T>::value,
void>::type
150 freeBlock(
void* block) {
151 freeBlock(std::type_index(
typeid(T)), block);
155std::shared_ptr<PoolWrapper> getPoolWrapper();
158inline void MakeAlloc(int32_t blockCount) {
159 getPoolWrapper()->makeAllocation<T>(blockCount);
163inline void* GetBlock() {
164 return getPoolWrapper()->getBlock<T>();
168inline void FreeBlock(
void* block) {
169 getPoolWrapper()->freeBlock<T>(block);
void * getBlock()
Get an allocated block for the already allocated type T.
void freeBlock(void *block)
Free an allocated block of the specified type T.
int32_t makeAllocation(int32_t blockCount)
Allocate memory for the specified type T.