21 #define DBTUP_TAB_DES_MAN_CPP
23 #include <RefConvert.hpp>
24 #include <ndb_limits.h>
41 Dbtup::getTabDescrOffsets(Uint32 noOfAttrs,
49 ndbrequire((sizeOfPointer & 0x3) == 0);
50 sizeOfPointer = (sizeOfPointer >> 2);
54 offset[0] = allocSize += ZTD_SIZE;
55 offset[1] = allocSize += noOfAttrs * sizeOfReadFunction();
56 offset[2] = allocSize += noOfAttrs * sizeOfReadFunction();
57 offset[3] = allocSize += noOfCharsets * sizeOfPointer;
58 offset[4] = allocSize += noOfKeyAttr;
59 offset[5] = allocSize += (noOfAttrs + extraColumns) * ZAD_SIZE;
60 offset[6] = allocSize += (noOfAttrs+1) >> 1;
61 allocSize += ZTD_TRAILER_SIZE;
67 Dbtup::getDynTabDescrOffsets(Uint32 MaskSize, Uint32* offset)
71 offset[0]= allocSize += ZTD_SIZE;
72 offset[1]= allocSize += MaskSize;
73 offset[2]= allocSize += MaskSize;
74 allocSize+= ZTD_TRAILER_SIZE;
80 Dbtup::releaseTabDescr(Uint32 descriptor)
82 if (descriptor != RNIL)
84 Uint32 retNo= getTabDescrWord(descriptor + ZTD_DATASIZE);
85 ndbrequire(getTabDescrWord(descriptor + ZTD_HEADER) == ZTD_TYPE_NORMAL);
86 ndbrequire(retNo == getTabDescrWord((descriptor + retNo) - ZTD_TR_SIZE));
87 ndbrequire(ZTD_TYPE_NORMAL ==
88 getTabDescrWord((descriptor + retNo) - ZTD_TR_TYPE));
89 freeTabDescr(descriptor, retNo);
93 Uint32 Dbtup::allocTabDescr(Uint32 allocSize)
95 Uint32 reference = RNIL;
99 allocSize = (((allocSize - 1) >> 4) + 1) << 4;
100 Uint32 list = nextHigherTwoLog(allocSize - 1);
101 for (Uint32
i = list;
i < 16;
i++) {
103 if (cfreeTdList[
i] != RNIL) {
105 reference = cfreeTdList[
i];
106 removeTdArea(reference,
i);
107 Uint32 retNo = (1 <<
i) - allocSize;
108 if (retNo >= ZTD_FREE_SIZE) {
111 Uint32 retRef = reference + allocSize;
112 freeTabDescr(retRef, retNo,
false);
120 if (reference == RNIL) {
122 terrorCode = ZMEM_NOTABDESCR_ERROR;
126 setTabDescrWord((reference + allocSize) - ZTD_TR_TYPE, ZTD_TYPE_NORMAL);
127 setTabDescrWord(reference + ZTD_DATASIZE, allocSize);
132 setTabDescrWord(reference + ZTD_HEADER, ZTD_TYPE_NORMAL);
133 setTabDescrWord((reference + allocSize) - ZTD_TR_SIZE, allocSize);
138 void Dbtup::freeTabDescr(Uint32 retRef, Uint32 retNo,
bool normal)
140 itdaMergeTabDescr(retRef, retNo, normal);
141 while (retNo >= ZTD_FREE_SIZE) {
143 Uint32 list = nextHigherTwoLog(retNo);
145 Uint32 sizeOfChunk = 1 << list;
146 insertTdArea(retRef, list);
147 retRef += sizeOfChunk;
148 retNo -= sizeOfChunk;
150 ndbassert(retNo == 0);
154 Dbtup::getTabDescrWord(Uint32
index)
156 ndbrequire(index < cnoOfTabDescrRec);
157 return tableDescriptor[
index].tabDescr;
161 Dbtup::setTabDescrWord(Uint32
index, Uint32
word)
163 ndbrequire(index < cnoOfTabDescrRec);
167 void Dbtup::insertTdArea(Uint32 tabDesRef, Uint32 list)
169 ndbrequire(list < 16);
170 RSS_OP_FREE_X(cnoOfFreeTabDescrRec, 1 << list);
171 setTabDescrWord(tabDesRef + ZTD_FL_HEADER, ZTD_TYPE_FREE);
172 setTabDescrWord(tabDesRef + ZTD_FL_NEXT, cfreeTdList[list]);
173 if (cfreeTdList[list] != RNIL) {
175 setTabDescrWord(cfreeTdList[list] + ZTD_FL_PREV, tabDesRef);
177 cfreeTdList[list] = tabDesRef;
179 setTabDescrWord(tabDesRef + ZTD_FL_PREV, RNIL);
180 setTabDescrWord(tabDesRef + ZTD_FL_SIZE, 1 << list);
181 setTabDescrWord((tabDesRef + (1 << list)) - ZTD_TR_TYPE, ZTD_TYPE_FREE);
182 setTabDescrWord((tabDesRef + (1 << list)) - ZTD_TR_SIZE, 1 << list);
190 void Dbtup::itdaMergeTabDescr(Uint32& retRef, Uint32& retNo,
bool normal)
193 while ((retRef + retNo) < cnoOfTabDescrRec) {
195 Uint32 tabDesRef = retRef + retNo;
196 Uint32 headerWord = getTabDescrWord(tabDesRef + ZTD_FL_HEADER);
197 if (headerWord == ZTD_TYPE_FREE) {
199 Uint32 sizeOfMergedPart = getTabDescrWord(tabDesRef + ZTD_FL_SIZE);
201 retNo += sizeOfMergedPart;
202 Uint32 list = nextHigherTwoLog(sizeOfMergedPart - 1);
203 removeTdArea(tabDesRef, list);
210 const bool mergeLeft = normal;
211 while (mergeLeft && retRef > 0) {
213 Uint32 trailerWord = getTabDescrWord(retRef - ZTD_TR_TYPE);
214 if (trailerWord == ZTD_TYPE_FREE) {
216 Uint32 sizeOfMergedPart = getTabDescrWord(retRef - ZTD_TR_SIZE);
217 ndbrequire(retRef >= sizeOfMergedPart);
218 retRef -= sizeOfMergedPart;
219 retNo += sizeOfMergedPart;
220 Uint32 list = nextHigherTwoLog(sizeOfMergedPart - 1);
221 removeTdArea(retRef, list);
227 ndbrequire((retRef + retNo) <= cnoOfTabDescrRec);
241 void Dbtup::removeTdArea(Uint32 tabDesRef, Uint32 list)
243 ndbrequire(list < 16);
244 RSS_OP_ALLOC_X(cnoOfFreeTabDescrRec, 1 << list);
246 Uint32 tabDescrNextPtr = getTabDescrWord(tabDesRef + ZTD_FL_NEXT);
247 Uint32 tabDescrPrevPtr = getTabDescrWord(tabDesRef + ZTD_FL_PREV);
249 setTabDescrWord(tabDesRef + ZTD_HEADER, ZTD_TYPE_NORMAL);
250 setTabDescrWord((tabDesRef + (1 << list)) - ZTD_TR_TYPE, ZTD_TYPE_NORMAL);
252 if (tabDesRef == cfreeTdList[list]) {
254 cfreeTdList[list] = tabDescrNextPtr;
256 if (tabDescrNextPtr != RNIL) {
258 setTabDescrWord(tabDescrNextPtr + ZTD_FL_PREV, tabDescrPrevPtr);
260 if (tabDescrPrevPtr != RNIL) {
262 setTabDescrWord(tabDescrPrevPtr + ZTD_FL_NEXT, tabDescrNextPtr);
266 #if defined VM_TRACE || defined ERROR_INSERT
268 Dbtup::verifytabdes()
274 WordType() : fl(-1), ti(-1), td(-1) {}
276 WordType* wt =
new WordType [cnoOfTabDescrRec];
282 for (uint
i = 0;
i < 16;
i++) {
284 Uint32 desc = cfreeTdList[
i];
285 while (desc != RNIL) {
286 const Uint32
size = (1 <<
i);
287 ndbrequire(size >= ZTD_FREE_SIZE);
288 ndbrequire(desc + size <= cnoOfTabDescrRec);
289 { Uint32
index = desc + ZTD_FL_HEADER;
290 ndbrequire(tableDescriptor[index].tabDescr == ZTD_TYPE_FREE);
292 { Uint32
index = desc + ZTD_FL_SIZE;
293 ndbrequire(tableDescriptor[index].tabDescr == size);
295 { Uint32
index = desc + size - ZTD_TR_TYPE;
296 ndbrequire(tableDescriptor[index].tabDescr == ZTD_TYPE_FREE);
298 { Uint32
index = desc + size - ZTD_TR_SIZE;
299 ndbrequire(tableDescriptor[index].tabDescr == size);
301 { Uint32
index = desc + ZTD_FL_PREV;
302 ndbrequire(tableDescriptor[index].tabDescr == desc2);
304 for (uint j = 0; j <
size; j++) {
305 ndbrequire(wt[desc + j].fl == -1);
309 desc = tableDescriptor[desc + ZTD_FL_NEXT].tabDescr;
310 free_words += (1 <<
i);
317 for (uint
i = 0;
i < cnoOfTablerec;
i++) {
320 ptrAss(ptr, tablerec);
321 if (ptr.p->tableStatus != DEFINED)
325 const Uint32 alloc = getTabDescrOffsets(ptr.p->m_no_of_attributes,
328 ptr.p->m_no_of_extra_columns,
330 const Uint32 desc = ptr.p->readKeyArray - offset[3];
332 if (size % ZTD_FREE_SIZE != 0)
333 size += ZTD_FREE_SIZE - size % ZTD_FREE_SIZE;
334 ndbrequire(desc + size <= cnoOfTabDescrRec);
335 { Uint32
index = desc + ZTD_FL_HEADER;
336 ndbrequire(tableDescriptor[index].tabDescr == ZTD_TYPE_NORMAL);
338 { Uint32
index = desc + ZTD_FL_SIZE;
339 ndbrequire(tableDescriptor[index].tabDescr == size);
341 { Uint32
index = desc + size - ZTD_TR_TYPE;
342 ndbrequire(tableDescriptor[index].tabDescr == ZTD_TYPE_NORMAL);
344 { Uint32
index = desc + size - ZTD_TR_SIZE;
345 ndbrequire(tableDescriptor[index].tabDescr == size);
347 for (uint j = 0; j <
size; j++) {
348 ndbrequire(wt[desc + j].ti == -1);
354 for (uint k = 0; k < NO_DYNAMICS; k++)
357 Uint32 MaskSize = (ptr.p->m_dyn_null_bits[k] + 31) >> 5;
358 const Uint32 alloc = getDynTabDescrOffsets(MaskSize, offset);
359 const Uint32 desc = ptr.p->dynTabDescriptor[k];
361 if (size % ZTD_FREE_SIZE != 0)
362 size += ZTD_FREE_SIZE - size % ZTD_FREE_SIZE;
363 ndbrequire(desc + size <= cnoOfTabDescrRec);
364 { Uint32
index = desc + ZTD_FL_HEADER;
365 ndbrequire(tableDescriptor[index].tabDescr == ZTD_TYPE_NORMAL);
367 { Uint32
index = desc + ZTD_FL_SIZE;
368 ndbrequire(tableDescriptor[index].tabDescr == size);
370 { Uint32
index = desc + size - ZTD_TR_TYPE;
371 ndbrequire(tableDescriptor[index].tabDescr == ZTD_TYPE_NORMAL);
373 { Uint32
index = desc + size - ZTD_TR_SIZE;
374 ndbrequire(tableDescriptor[index].tabDescr == size);
376 for (uint j = 0; j <
size; j++) {
377 ndbrequire(wt[desc + j].ti == -1);
379 wt[desc + j].td = 1 + k;
387 for (uint
i = 0;
i < cnoOfTabDescrRec;
i++) {
388 bool is_fl = wt[
i].fl != -1;
389 bool is_ti = wt[
i].ti != -1;
390 ndbrequire(is_fl != is_ti);
394 ndbrequire(used_words + free_words == cnoOfTabDescrRec);
395 ndbout <<
"verifytabdes:"
396 <<
" total: " << cnoOfTabDescrRec
397 <<
" used: " << used_words
398 <<
" free: " << free_words
399 <<
" frags: " << free_frags