Fixing Build failure.
[csql.git] / include / Allocator.h
blobc587f13e8825aeddc8afe62eb42344f976ff58ac
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>
20 #include<ErrorType.h>
22 typedef void Page;
24 enum AllocType
26 FixedSizeAllocator = 0,
27 VariableSizeAllocator = 1,
28 UnknownAllocator
31 //Used to store the meta data information about the variable size data
32 class VarSizeInfo
34 public:
35 size_t size_;
36 int isUsed_;
39 //Each Page has this info.
40 //pages are of size PAGE_SIZE normally.
41 //If data size is more than PAGE_SIZE then
42 //contigous pages are merged and those pages wont
43 //have this info in them.Only the start page where that
44 //data is stored will have this info
45 //This object is stored at the start of each page
46 class PageInfo
48 public:
49 int isUsed_;
50 int hasFreeSpace_;
52 Page *nextPageAfterMerge_; //used only in case of
53 //where pages are merged to store data which are more than
54 //PAGE_SIZE.
55 //More detail about how it is used is found in Database::getFreePage
57 Page *nextPage_; //next page in the same chunk
58 void setPageAsUsed(size_t offset);
59 void setFirstPageAsUsed();
63 class Chunk;
66 //Iterator for the data
67 //Data is stored in chunks and this class gives
68 //iterator for it.
69 class ChunkIterator
71 int chunkID_;
72 size_t allocSize_; // used if it is a fixed size allocator
73 AllocType allocType_;
75 //current iterating page
76 PageInfo *iterPage_;
78 //Each page is divided into nodes of size allocSize_
79 //This gives the offset of the node in the page
80 int nodeOffset_;
81 char *data;
82 char *iterPageEnd;
84 //Total number of nodes in the page
85 //It is a constant value for this chunk
86 //and it is cached for performance
87 int noOfNodes_;
89 public:
90 ChunkIterator() { allocSize_ =0; iterPage_ = NULL; nodeOffset_ =0;
91 chunkID_ = -1; noOfNodes_ =0; }
92 void* nextElement();
93 friend class Chunk;
96 class Database;
97 class DatabaseManagerImpl;
99 class Chunk
101 int chunkID_;
103 // used if it is a fixed size allocator
104 size_t allocSize_;
105 AllocType allocType_;
107 //Current page where the last data allocation was made
108 Page *curPage_;
110 //Page where data allocation was made for the first time
111 //This is the start of the data
112 //Iterator should start from this page
113 Page *firstPage_;
114 char chunkName[CHUNK_NAME_LEN];
115 Mutex chunkMutex_;
117 public:
119 //sets the size of the allocator
120 //for fixed size allocator
121 void setSize(size_t size);
123 void setChunkNameForSystemDB(int id);
124 void setChunkName(char const *name){strcpy(chunkName,name);}
125 char *getChunkName(){return chunkName;}
127 size_t getSize() { return allocSize_; }
128 void setChunkID(unsigned int id) { chunkID_ = id; }
129 int getChunkID() { return chunkID_; }
130 void setAllocType(AllocType type) { allocType_ = type; }
131 AllocType getAllocType() { return allocType_; }
132 Page* getFirstPage(){ return firstPage_; }
134 PageInfo* getPageInfo(Database *db, void *ptr);
135 void* allocate(Database *db, DbRetVal *status = NULL);
137 void* allocate(Database *db, size_t size, DbRetVal *status = NULL);
139 void free(Database *db, void* ptr);
140 ChunkIterator getIterator();
141 void print();
143 long getTotalDataNodes();
144 int totalPages();
145 int compact();
147 private:
149 int initMutex();
150 int getChunkMutex(int procSlot);
151 int releaseChunkMutex(int procSlot);
152 int destroyMutex();
153 void createDataBucket(Page *page, size_t totalSize, size_t needSize);
154 void splitDataBucket(VarSizeInfo *varInfo, size_t needSize);
155 void* varSizeFirstFitAllocate(size_t size);
156 void freeForLargeAllocator(void *ptr, int pslot);
157 void freeForVarSizeAllocator(void *ptr, int pslot);
159 void* allocateForLargeDataSize(Database *db);
160 void* allocateFromFirstPage(Database *db, int noOfDataNodes);
161 void* allocateFromNewPage(Database *db);
163 void* allocateForLargeDataSize(Database *db, size_t size);
164 void* allocFromNewPageForVarSize(Database *db, size_t size);
165 void* allocateFromCurPageForVarSize(size_t size);
168 friend class Database;
169 friend class DatabaseManagerImpl;
171 #endif