• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/collection/MemoryPool.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
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         // Copy constructor doesn't make sense for this class
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