Code Reorganization
[csql.git] / include / Allocator.h
blob094ffd67515ff8e9c063e0107111e07d0a99a303
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #ifndef ALLOCATOR_H
17 #define ALLOCATOR_H
18 #include<os.h>
19 #include<Mutex.h>
21 typedef void Page;
23 enum AllocType
25 FixedSizeAllocator = 0,
26 VariableSizeAllocator = 1,
27 UnknownAllocator
30 //Used to store the meta data information about the variable size data
31 class VarSizeInfo
33 public:
34 size_t size_;
35 int isUsed_;
38 //Each Page has this info.
39 //pages are of size PAGE_SIZE normally.
40 //If data size is more than PAGE_SIZE then
41 //contigous pages are merged and those pages wont
42 //have this info in them.Only the start page where that
43 //data is stored will have this info
44 //This object is stored at the start of each page
45 class PageInfo
47 public:
48 int isUsed_;
49 int hasFreeSpace_;
51 Page *nextPageAfterMerge_; //used only in case of
52 //where pages are merged to store data which are more than
53 //PAGE_SIZE.
54 //More detail about how it is used is found in Database::getFreePage
56 Page *nextPage_; //next page in the same chunk
57 void setPageAsUsed(size_t offset);
58 void setFirstPageAsUsed();
62 class Chunk;
65 //Iterator for the data
66 //Data is stored in chunks and this class gives
67 //iterator for it.
68 class ChunkIterator
70 int chunkID_;
71 size_t allocSize_; // used if it is a fixed size allocator
72 AllocType allocType_;
74 //current iterating page
75 Page *iterPage_;
77 //Each page is divided into nodes of size allocSize_
78 //This gives the offset of the node in the page
79 int nodeOffset_;
81 //Total number of nodes in the page
82 //It is a constant value for this chunk
83 //and it is cached for performance
84 int noOfNodes_;
86 public:
87 void* nextElement();
88 friend class Chunk;
91 class Database;
92 class DatabaseManagerImpl;
94 class Chunk
96 int chunkID_;
98 // used if it is a fixed size allocator
99 size_t allocSize_;
100 AllocType allocType_;
102 //Current page where the last data allocation was made
103 Page *curPage_;
105 //Page where data allocation was made for the first time
106 //This is the start of the data
107 //Iterator should start from this page
108 Page *firstPage_;
110 Mutex chunkMutex_;
112 public:
114 //sets the size of the allocator
115 //for fixed size allocator
116 void setSize(size_t size);
118 size_t getSize() { return allocSize_; }
119 void setChunkID(unsigned int id) { chunkID_ = id; }
120 int getChunkID() { return chunkID_; }
121 void setAllocType(AllocType type) { allocType_ = type; }
122 AllocType getAllocType() { return allocType_; }
125 PageInfo* getPageInfo(Database *db, void *ptr);
126 void* allocate(Database *db);
128 void* allocate(Database *db, size_t size);
130 void free(Database *db, void* ptr);
131 ChunkIterator getIterator();
132 void print(){}
134 long getTotalDataNodes();
135 int totalPages();
137 private:
139 int initMutex();
140 int getChunkMutex();
141 int releaseChunkMutex();
142 int destroyMutex();
143 void createDataBucket(Page *page, size_t totalSize, size_t needSize);
144 void splitDataBucket(VarSizeInfo *varInfo, size_t needSize);
145 void* varSizeFirstFitAllocate(size_t size);
146 void freeForLargeAllocator(void *ptr);
147 void freeForVarSizeAllocator(void *ptr);
149 void* allocateForLargeDataSize(Database *db);
150 void* allocateFromFirstPage(Database *db, int noOfDataNodes);
151 void* allocateFromNewPage(Database *db);
153 void* allocateForLargeDataSize(Database *db, size_t size);
154 void* allocFromNewPageForVarSize(Database *db, size_t size);
155 void* allocateFromCurPageForVarSize(size_t size);
158 friend class Database;
159 friend class DatabaseManagerImpl;
161 #endif