freestoremanager.h

00001 #ifndef _FREE_STORE_MANAGER_
00002 #define _FREE_STORE_MANAGER_
00003 
00004 #include <iostream>
00005 
00006 using namespace std;
00007 
00008 class FreeStoreManager
00009 {
00010   public:
00011          
00012    FreeStoreManager(int blockSize)
00013    {
00014      storePtrList_ = NULL;
00015      maxCount_ = 0;
00016          allocatedCount_ = 0;
00017          occupiedCount_ = 0;
00018          this->blockSize_ = blockSize;
00019    }
00020 
00021    ~FreeStoreManager()
00022    {
00023          if(storePtrList_ == NULL)
00024            return;
00025          for(int i=0;i<allocatedCount_;i++)
00026            delete [] storePtrList_[i];
00027          delete [] storePtrList_;
00028    }
00029           
00030          /* get the block size */
00031    int getBlockSize(){ return blockSize_; }
00032 
00033          /* get next available store ptr */       
00034    int* getStorePtr() {
00035          //     cout<<"came to get a new block.."<<endl;
00036      int *storePtr;
00037          if(allocatedCount_ == maxCount_)
00038            increaseMaxCount();
00039          if(allocatedCount_ == occupiedCount_)
00040          {
00041            storePtr = new int[blockSize_];
00042            storePtrList_[allocatedCount_++] = storePtr;
00043          }
00044      return storePtrList_[occupiedCount_++];
00045    }
00046 
00047    /* release a store ptr */
00048    void releaseStorePtr()
00049    {
00050          if(occupiedCount_ == 0)
00051            cout<<"Problem in freeStorePtr!!"<<endl<<"Nothing to free!"<<endl;
00052          occupiedCount_--;
00053    }
00054 
00055    /* release a store ptr */
00056    void releaseAllStorePtr()
00057    {
00058          //     cout<<"releasing all the blocks.."<<endl;
00059          occupiedCount_=0;
00060    }
00061         
00062   private:
00063         
00064    /* increase the max size */
00065    void increaseMaxCount()
00066    {
00067          maxCount_ = maxCount_*2+1;
00068          int **tmpPtrList = new int*[maxCount_];
00069          if(storePtrList_ != NULL)
00070          {
00071            for(int i=0;i<allocatedCount_;i++)
00072          tmpPtrList[i] = storePtrList_[i];
00073            delete storePtrList_;
00074          }
00075          storePtrList_ = tmpPtrList;
00076    }
00077 
00078   private:
00079         
00080    int maxCount_;
00081    int allocatedCount_;
00082    int occupiedCount_;
00083    int **storePtrList_;
00084    int blockSize_;
00085 };
00086 
00087 
00088 #endif

Generated on Tue Jan 16 05:30:02 2007 for Alchemy by  doxygen 1.5.1