19 #include <SectionReader.hpp>
20 #include <TransporterDefinitions.hpp>
21 #include "LongSignal.hpp"
30 SectionReader::SectionReader
47 m_currentSegment = ptr.p;
51 SectionReader::SectionReader
58 m_len = firstSeg->m_sz;
59 m_headI= m_currI= firstSectionIVal;
60 m_head = m_currentSegment = firstSeg;
64 SectionReader::reset(){
67 m_currentSegment = m_head;
71 SectionReader::step(Uint32 len){
72 if(m_pos + len >= m_len) {
76 while(len > SectionSegment::DataLength){
77 m_currI= m_currentSegment->m_nextSegment;
78 m_currentSegment = m_pool.
getPtr(m_currI);
80 len -= SectionSegment::DataLength;
81 m_pos += SectionSegment::DataLength;
84 Uint32 ind = m_pos % SectionSegment::DataLength;
90 if(ind == SectionSegment::DataLength){
92 m_currI= m_currentSegment->m_nextSegment;
93 m_currentSegment = m_pool.
getPtr(m_currI);
100 SectionReader::getWord(Uint32 * dst){
109 SectionReader::peekWord(Uint32 * dst)
const {
111 Uint32 ind = m_pos % SectionSegment::DataLength;
112 * dst = m_currentSegment->theData[ind];
122 Uint32 ind = m_pos % SectionSegment::DataLength;
123 m_currentSegment->theData[ind] = value;
130 SectionReader::peekWords(Uint32 * dst, Uint32 len)
const {
131 if(m_pos + len > m_len)
134 Uint32 ind = (m_pos % SectionSegment::DataLength);
135 Uint32 left = SectionSegment::DataLength - ind;
139 memcpy(dst, &p->theData[ind], 4 * left);
143 left = SectionSegment::DataLength;
144 p = m_pool.
getPtr(p->m_nextSegment);
147 memcpy(dst, &p->theData[ind], 4 * len);
152 SectionReader::getWords(Uint32 * dst, Uint32 len){
153 if(m_pos + len > m_len)
160 const Uint32* readPtr;
163 if (!getWordsPtr(len,
168 memcpy(dst, readPtr, readLen << 2);
177 SectionReader::getWordsPtr(Uint32 maxLen,
178 const Uint32*& readPtr,
190 const Uint32 sectionRemain= m_len - m_pos;
191 const Uint32 startInd = (m_pos % SectionSegment::DataLength);
192 const Uint32 segmentSpace = SectionSegment::DataLength - startInd;
195 const Uint32 remain= MIN(sectionRemain, segmentSpace);
196 actualLen= MIN(remain, maxLen);
197 readPtr= &p->theData[startInd];
205 if (((startInd + actualLen) == SectionSegment::DataLength) &&
208 m_currI= p->m_nextSegment;
209 m_currentSegment= m_pool.
getPtr(m_currI);
216 SectionReader::getWordsPtr(
const Uint32*& readPtr,
222 return getWordsPtr(SectionSegment::DataLength,
229 SectionReader::getPos()
233 pi.currIVal= m_currI;
239 SectionReader::setPos(PosInfo posInfo)
241 if (posInfo.currPos > m_len)
244 if (posInfo.currIVal == RNIL)
246 if (posInfo.currPos > 0)
252 assert(segmentContainsPos(posInfo));
254 m_currentSegment= m_pool.
getPtr(posInfo.currIVal);
257 m_pos= posInfo.currPos;
258 m_currI= posInfo.currIVal;
264 SectionReader::segmentContainsPos(PosInfo posInfo)
271 Uint32 IVal= m_headI;
272 Uint32 pos= posInfo.currPos;
274 while (pos >= SectionSegment::DataLength)
279 IVal= seg->m_nextSegment;
280 pos-= SectionSegment::DataLength;
283 return (IVal == posInfo.currIVal);
289 #define VERIFY(x) if ((x) == 0) { printf("VERIFY failed at Line %u : %s\n",__LINE__, #x); return -1; }
292 void ErrorReporter::handleAssert(
const char*
message,
const char*
file,
int line,
int ec)
294 printf(
"Error :\"%s\" at file : %s line %u ec %u\n",
295 message, file, line, ec);
299 void* ndbd_malloc(
size_t size)
304 void ndbd_free(
void* p,
size_t size)
323 VERIFY(pool->
seize(first));
325 first.p->m_sz= length;
328 while (length > SectionSegment::DataLength)
330 for (Uint32
i=0;
i<SectionSegment::DataLength;
i++)
331 current.p->theData[
i]= pos+
i;
333 pos+= SectionSegment::DataLength;
334 length-= SectionSegment::DataLength;
337 VERIFY(pool->
seize(current));
338 prev->m_nextSegment= current.i;
343 for (Uint32
i=0;
i< length;
i++)
344 current.p->theData[
i]= pos+
i;
347 first.p->m_lastSegment= current.i;
360 const Uint32 segs= (p.p->m_sz + SectionSegment::DataLength -1) /
361 SectionSegment::DataLength;
363 pool->releaseList(segs, p.i, p.p->m_lastSegment);
368 int checkBuffer(
const Uint32* buffer,
372 for (Uint32
i=0;
i<len;
i++)
374 if (buffer[
i] != start +
i)
375 printf(
"i=%u buffer[i]=%u, start=%u\n",
376 i, buffer[
i], start);
377 VERIFY(buffer[
i] == start +
i);
392 VERIFY(srStepPeek.getSize() == len);
395 const Uint32 noResetPos= 9999999;
396 Uint32 resetAt= len> 10 ? myRandom48(len) : noResetPos;
399 for (Uint32
i=0;
i < len;
i++)
403 const Uint32* ptrWord;
409 VERIFY(srStepPeek.peekWord(&peekWord));
411 VERIFY(srStepPeek.step(1));
412 VERIFY(srGetWord.getWord(&getWord));
413 VERIFY(srPtrWord.getWordsPtr(1, ptrWord, ptrReadSize));
414 VERIFY(ptrReadSize == 1);
417 VERIFY(peekWord ==
i);
418 VERIFY(peekWord == getWord);
419 VERIFY(peekWord == *ptrWord);
427 Uint32 srcWord, destWord;
429 VERIFY(srPosSource.getWord(&srcWord));
430 VERIFY(srPosDest.getWord(&destWord));
432 VERIFY(srcWord == peekWord);
433 VERIFY(srcWord == destWord);
449 if ((myRandom48(400) == 1) &&
453 Uint32 stepSize= myRandom48((len -
i) -1 );
455 VERIFY(srStepPeek.step(stepSize));
456 VERIFY(srGetWord.step(stepSize));
457 VERIFY(srPosSource.step(stepSize));
458 VERIFY(srPosDest.step(stepSize));
459 VERIFY(srPtrWord.step(stepSize));
466 VERIFY(!srStepPeek.step(1));
467 VERIFY(!srGetWord.step(1));
468 VERIFY(!srPosSource.step(1));
469 VERIFY(!srPosDest.step(1));
470 VERIFY(!srPtrWord.step(1));
480 Uint32* buffer= (Uint32*) malloc(len * 4);
482 VERIFY(buffer != NULL);
486 const Uint32 remain= len-pos;
487 const Uint32 readSize= remain == 1 ? 1 : myRandom48(remain);
490 VERIFY(srStepPeek.peekWords(buffer, readSize));
491 if (len > pos + readSize)
493 VERIFY(srStepPeek.step(readSize));
496 VERIFY(srStepPeek.step((len - pos) - 1));
498 VERIFY(checkBuffer(buffer, pos, readSize) == 0);
501 VERIFY(srGetWord.getWords(buffer, readSize));
502 VERIFY(checkBuffer(buffer, pos, readSize) == 0);
505 VERIFY(srPosDest.setPos(srPosSource.getPos()));
506 VERIFY(srPosSource.getWords(buffer, readSize));
507 VERIFY(checkBuffer(buffer, pos, readSize) == 0);
509 VERIFY(srPosDest.getWords(buffer, readSize));
510 VERIFY(checkBuffer(buffer, pos, readSize) == 0);
513 Uint32 ptrWordsRead= 0;
516 while (ptrWordsRead < readSize)
518 const Uint32* ptr= NULL;
520 VERIFY(srPtrWord.getWordsPtr((readSize - ptrWordsRead),
523 VERIFY(readLen <= readSize);
526 VERIFY(checkBuffer(ptr, pos+ ptrWordsRead, readLen) == 0);
527 ptrWordsRead+= readLen;
534 VERIFY(!srStepPeek.step(1));
535 VERIFY(!srGetWord.step(1));
536 VERIFY(!srPosSource.step(1));
537 VERIFY(!srPosDest.step(1));
538 VERIFY(!srPtrWord.step(1));
545 while (readWords < len)
547 const Uint32* readPtr;
549 VERIFY(srPtrWord.getWordsPtr(20, readPtr, wordsRead));
550 readWords+= wordsRead;
551 VERIFY(readWords <= len);
559 int main(
int arg,
char** argv)
571 g_sectionSegmentPool.
setSize(1024);
573 printf(
"g_sectionSegmentPool size is %u\n",
574 g_sectionSegmentPool.getSize());
576 const Uint32 Iterations= 2000;
577 const Uint32 Sections= 5;
578 Uint32 sizes[ Sections ];
579 Uint32 iVals[ Sections ];
581 for (Uint32 t=0; t < Iterations; t++)
583 for (Uint32
i=0;
i<Sections;
i++)
585 Uint32 available= g_sectionSegmentPool.getNoOfFree();
586 sizes[
i] = available ?
587 myRandom48(SectionSegment::DataLength *
596 printf(
"\nIteration %u", t);
600 iVals[
i]= createSection(&g_sectionSegmentPool, sizes[
i]);
602 VERIFY(testSR(iVals[
i], &g_sectionSegmentPool, sizes[i]) == 0);
609 for (Uint32
i=0;
i < Sections;
i++)
612 freeSection(&g_sectionSegmentPool, iVals[
i]);