1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
26 FixedSizeAllocator
= 0,
27 VariableSizeAllocator
= 1,
31 //Used to store the meta data information about the variable size data
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
52 Page
*nextPageAfterMerge_
; //used only in case of
53 //where pages are merged to store data which are more than
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();
66 //Iterator for the data
67 //Data is stored in chunks and this class gives
72 size_t allocSize_
; // used if it is a fixed size allocator
75 //current iterating page
78 //Each page is divided into nodes of size allocSize_
79 //This gives the offset of the node in the page
82 //Total number of nodes in the page
83 //It is a constant value for this chunk
84 //and it is cached for performance
88 ChunkIterator() { allocSize_
=0; iterPage_
= NULL
; nodeOffset_
=0;
89 chunkID_
= -1; noOfNodes_
=0; }
95 class DatabaseManagerImpl
;
101 // used if it is a fixed size allocator
103 AllocType allocType_
;
105 //Current page where the last data allocation was made
108 //Page where data allocation was made for the first time
109 //This is the start of the data
110 //Iterator should start from this page
112 char chunkName
[CHUNK_NAME_LEN
];
117 //sets the size of the allocator
118 //for fixed size allocator
119 void setSize(size_t size
);
121 void setChunkNameForSystemDB(int id
);
122 void setChunkName(char const *name
){strcpy(chunkName
,name
);}
123 char *getChunkName(){return chunkName
;}
125 size_t getSize() { return allocSize_
; }
126 void setChunkID(unsigned int id
) { chunkID_
= id
; }
127 int getChunkID() { return chunkID_
; }
128 void setAllocType(AllocType type
) { allocType_
= type
; }
129 AllocType
getAllocType() { return allocType_
; }
130 Page
* getFirstPage(){ return firstPage_
; }
132 PageInfo
* getPageInfo(Database
*db
, void *ptr
);
133 void* allocate(Database
*db
, DbRetVal
*status
= NULL
);
135 void* allocate(Database
*db
, size_t size
, DbRetVal
*status
= NULL
);
137 void free(Database
*db
, void* ptr
);
138 ChunkIterator
getIterator();
141 long getTotalDataNodes();
148 int getChunkMutex(int procSlot
);
149 int releaseChunkMutex(int procSlot
);
151 void createDataBucket(Page
*page
, size_t totalSize
, size_t needSize
);
152 void splitDataBucket(VarSizeInfo
*varInfo
, size_t needSize
);
153 void* varSizeFirstFitAllocate(size_t size
);
154 void freeForLargeAllocator(void *ptr
, int pslot
);
155 void freeForVarSizeAllocator(void *ptr
, int pslot
);
157 void* allocateForLargeDataSize(Database
*db
);
158 void* allocateFromFirstPage(Database
*db
, int noOfDataNodes
);
159 void* allocateFromNewPage(Database
*db
);
161 void* allocateForLargeDataSize(Database
*db
, size_t size
);
162 void* allocFromNewPageForVarSize(Database
*db
, size_t size
);
163 void* allocateFromCurPageForVarSize(size_t size
);
166 friend class Database
;
167 friend class DatabaseManagerImpl
;