1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ 2 3 #ifndef _MEM_POOL_H_ 4 #define _MEM_POOL_H_ 5 6 #include <new> 7 #include <cstring> // for size_t 8 #include <vector> 9 10 static const size_t MAX_MEM_SIZE = 200; 11 12 /** 13 * Speeds-up for memory-allocation of often allocated/deallocated structs 14 * or classes, or other memory blocks of equal size. 15 * You may think of this as something like a very primitive garbage collector. 16 * Instead of actually freeing memory, it is kept allocated, and is just 17 * reassigned next time an alloc of the same size is performed. 18 * The maximum memory held by this class is approximately MAX_MEM_SIZE^2 bytes. 19 */ 20 class CMemPool 21 { 22 public: 23 CMemPool(); 24 ~CMemPool(); 25 26 void* Alloc(size_t numBytes); 27 void Free(void* pnt, size_t numBytes); 28 29 private: UseExternalMemory(size_t numBytes)30 static bool UseExternalMemory(size_t numBytes) { 31 return (numBytes > MAX_MEM_SIZE) || (numBytes < 4); 32 } 33 34 void* nextFree[MAX_MEM_SIZE + 1]; 35 int poolSize[MAX_MEM_SIZE + 1]; 36 std::vector<void *> allocated; 37 }; 38 39 extern CMemPool mempool; 40 41 #endif // _MEM_POOL_H_ 42 43