include/Allocator.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by www.databasecache.com                           *
00003  *   Contact: praba_tuty@databasecache.com                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015   ***************************************************************************/
00016 #ifndef ALLOCATOR_H
00017 #define ALLOCATOR_H
00018 #include<os.h>
00019 #include<Mutex.h>
00020 #include<ErrorType.h>
00021 
00022 typedef void Page;
00023 
00024 enum AllocType
00025 {
00026     FixedSizeAllocator = 0,
00027     VariableSizeAllocator = 1,
00028     UnknownAllocator
00029 };
00030 
00031 //Used to store the meta data information about the variable size data
00032 class VarSizeInfo
00033 {
00034     public:
00035     size_t size_;
00036     int isUsed_;
00037 };
00038 
00039 //Each Page has this info.
00040 //pages are of size PAGE_SIZE normally.
00041 //If data size is more than PAGE_SIZE then
00042 //contigous pages are merged and those pages wont
00043 //have this info in them.Only the start page where that
00044 //data is stored will have this info
00045 //This object is stored at the start of each page
00046 class PageInfo
00047 {
00048      public:
00049      int isUsed_;
00050      int hasFreeSpace_;
00051 
00052      Page *nextPageAfterMerge_; //used only in case of
00053      //where pages are merged to store data which are more than
00054      //PAGE_SIZE.
00055      //More detail about how it is used is found in Database::getFreePage
00056 
00057      Page *nextPage_; //next page in the same chunk
00058      void setPageAsUsed(size_t offset);
00059      void setFirstPageAsUsed();
00060 
00061 
00062 };
00063 class Chunk;
00064 
00065 
00066 //Iterator for the data
00067 //Data is stored in chunks and this class gives
00068 //iterator for it.
00069 class ChunkIterator
00070 {
00071     int chunkID_;
00072     size_t allocSize_; // used if it is a fixed size allocator
00073     AllocType allocType_;
00074 
00075     //current iterating page
00076     Page *iterPage_;
00077 
00078     //Each page is divided into nodes of size allocSize_
00079     //This gives the offset of the node in the page
00080     int nodeOffset_;
00081 
00082     //Total number of nodes in the page
00083     //It is a constant value for this chunk
00084     //and it is cached for performance
00085     int noOfNodes_;
00086 
00087     public:
00088     void* nextElement();
00089     friend class Chunk;
00090 };
00091 
00092 class Database;
00093 class DatabaseManagerImpl;
00094 
00095 class Chunk
00096 {
00097     int chunkID_;
00098 
00099     // used if it is a fixed size allocator
00100     size_t allocSize_;
00101     AllocType allocType_;
00102 
00103     //Current page where the last data allocation was made
00104     Page *curPage_;
00105 
00106     //Page where data allocation was made for the first time
00107     //This is the start of the data
00108     //Iterator should start from this page
00109     Page *firstPage_;
00110 
00111     Mutex chunkMutex_;
00112 
00113     public:
00114 
00115     //sets the size of the allocator
00116     //for fixed size allocator
00117     void setSize(size_t size);
00118 
00119     size_t getSize()                  { return allocSize_; }
00120     void setChunkID(unsigned int id)           { chunkID_ = id; }
00121     int getChunkID()                  { return chunkID_; }
00122     void setAllocType(AllocType type) { allocType_ = type; }
00123     AllocType getAllocType()          { return allocType_; }
00124 
00125 
00126     PageInfo* getPageInfo(Database *db, void *ptr);
00127     void* allocate(Database *db, DbRetVal *status = NULL);
00128 
00129     void* allocate(Database *db, size_t size, DbRetVal *status = NULL);
00130 
00131     void  free(Database *db, void* ptr);
00132     ChunkIterator getIterator();
00133     void print(){}
00134 
00135     long getTotalDataNodes();
00136     int totalPages();
00137     int compact();
00138 
00139     private:
00140 
00141     int initMutex();
00142     int getChunkMutex(int procSlot);
00143     int releaseChunkMutex(int procSlot);
00144     int destroyMutex();
00145     void createDataBucket(Page *page, size_t totalSize, size_t needSize);
00146     void splitDataBucket(VarSizeInfo *varInfo, size_t needSize);
00147     void* varSizeFirstFitAllocate(size_t size);
00148     void freeForLargeAllocator(void *ptr, int pslot);
00149     void freeForVarSizeAllocator(void *ptr, int pslot);
00150 
00151     void* allocateForLargeDataSize(Database *db);
00152     void* allocateFromFirstPage(Database *db, int noOfDataNodes);
00153     void* allocateFromNewPage(Database *db);
00154 
00155     void* allocateForLargeDataSize(Database *db, size_t size);
00156     void* allocFromNewPageForVarSize(Database *db, size_t size);
00157     void* allocateFromCurPageForVarSize(size_t size);
00158 
00159 
00160     friend class Database;
00161     friend class DatabaseManagerImpl;
00162 };
00163 #endif

Generated on Mon Jun 9 22:37:14 2008 for csql by  doxygen 1.4.7