00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00020 #ifndef _memorypool_h
00021 #define _memorypool_h
00022
00023 #include <spl/types.h>
00024 #include <spl/Debug.h>
00025 #include <spl/collection/Array.h>
00026 #include <spl/Memory.h>
00027
00039 typedef struct MemoryBlock
00040 {
00041 int32 m_blockId;
00042 byte* m_data;
00043 int m_size;
00044 int m_freeSize;
00045 int m_pos;
00046 MemoryBlock *m_next;
00047 int m_checkBits;
00048 } MemoryBlock;
00049
00050 static const int DEFAULT_MEMORY_POOL_BLOCK_SIZE = 1024 ;
00051
00058 class MemoryPool : public IMemoryValidate
00059 {
00060 private:
00061
00062 inline MemoryPool(const MemoryPool& mp) {}
00063 inline void operator =(const MemoryPool& mp) {}
00064
00065 MemoryBlock m_head;
00066
00067 int m_totalMemoryPoolSize ;
00068 int m_usedMemoryPoolSize ;
00069 int m_freeMemoryPoolSize ;
00070
00071 int m_memoryBlockSize ;
00072 int m_memoryBlockCount ;
00073
00074 bool m_setMemoryData ;
00075
00076 MemoryBlock *AllocateBlock(int size);
00077
00078 public:
00086 MemoryPool(const int blockSize = DEFAULT_MEMORY_POOL_BLOCK_SIZE, bool bSetMemoryData = false);
00087 virtual ~MemoryPool();
00088
00094 void *Malloc(int size) ;
00095
00101 void Free(void *ptr);
00102
00109 bool WriteMemoryDumpToFile(const char *fileName) const;
00110
00118 bool IsValidPointer(const void *ptr) const;
00119
00120 #ifdef DEBUG
00121 virtual void ValidateMem () const;
00122 virtual void CheckMem () const;
00123 #endif
00124 };
00125
00126 void *operator new (size_t size, MemoryPool& pool);
00127 void operator delete (void *ptr, MemoryPool& pool);
00128
00129 REGISTER_TYPEOF( 400, MemoryPool );
00130
00133 #endif