void* InternalAllocator::Allocate(size_t size) { char* returnPtr = m_Tail->CurrentPtr; const auto roundedSize = size + (4 - size & 3); for(;;) { auto tail = m_Tail; returnPtr = tail->CurrentPtr; char* newPtr = returnPtr + roundedSize; // round to 4 bytes char* startPtr = tail->GetStartPtr(); if(size_t(newPtr - startPtr) <= tail->GetSize()) { if(tail->CurrentPtr.compare_exchange_weak(returnPtr, newPtr)) { break; } } else { // not enough memory - we need a new page { std::lock_guard<std::mutex> l(m_PagesMutex); if(tail == m_Tail) // check if tail hasn't changed in the mean time { Page* page = Page::Allocate(m_ExternalAllocator, std::max(PAGE_SIZE, roundedSize)); page->GetPrevious() = tail; m_Tail = page; } } } } return returnPtr; }
// This Proc assumes that all the records in pDB has a common Prefix PrefixSeq with lenght of PrefixLen. // It creates and returns projected db for that common prefix. struct PROJ_DB * MakeProjDB( const struct PROJ_DB* pDB, Prefix * aPrefix ) { int * PostStart = NULL; SeqWrap * aSeqWrap = NULL; //int * RecEnd; int * tmpPtr = NULL; int i = 0; int j = 0; int tmp = 0; int PatLen = 0; int Sup = 0; struct PROJ_DB * ProjDB = NULL; struct PROJ_SEQ * tmpSeq = NULL; // RTATest( pDB ); // if( (*pDB).m_nPatLen>2 && (*pDB).m_pnPat[0] == 33 && (*pDB).m_pnPat[2] == 56 ) // printf( " werwrwrwrewerwrwrwrwer\n" ); //if( (*pDB).m_nPatLen>4 && (*pDB).m_pnPat[0] == 867 && (*pDB).m_pnPat[2] == 92 && (*pDB).m_pnPat[4] == 631 ) // printf( " werwrwrwrewerwrwrwrwer\n" ); tmp = sizeof( struct PROJ_DB ); n_total_mem += tmp; ProjDB = (struct PROJ_DB*) memalloc(tmp); PatLen = (*aPrefix).Size() + (*pDB).m_nPatLen; (*ProjDB).m_nPatLen = PatLen; // Copy the prefix of pDB to ProjDB's prefix. tmp = PatLen * sizeof(int); n_total_mem += tmp; (*ProjDB).m_pnPat = (int*) memalloc(tmp); if( (*pDB).m_nPatLen == 1 ) (*ProjDB).m_pnPat[0] = (int) (*pDB).m_pnPat; else memcpy( (*ProjDB).m_pnPat, (*pDB).m_pnPat, (*pDB).m_nPatLen * sizeof(int) ); // Add aPrefix to ProjDB's prefix. tmpPtr = (*aPrefix).GetFirst(); for( i = (*pDB).m_nPatLen; i<PatLen; i++ ) { (*ProjDB).m_pnPat[i] = (int) *tmpPtr; tmpPtr = (*aPrefix).GetNext(); } (*ProjDB).m_nMaxSup = (*pDB).m_nSup; tmp = (*pDB).m_nSup * sizeof( struct PROJ_SEQ ); n_total_mem += tmp; (*ProjDB).m_pProjSeq = (struct PROJ_SEQ*) memalloc( tmp ); memset( (*ProjDB).m_pProjSeq, 0, tmp ); (*ProjDB).m_nVer = -1; (*ProjDB).m_nSup = 0; int JMax = 1; int PrjCnt; bool Inter; bool IncSup; if( *(*aPrefix).GetFirst() == -1 ) Inter = true; else Inter = false; #if defined( _USE_STRING_ELEMINATION ) bool CheckStrings = false; if( (*MainSeqTree).IsContained( new Sequence(pDB) ) ) { CheckStrings = true; } #endif // defined( _USE_STRING_ELEMINATION ) for( i=0; i<(*pDB).m_nSup; i++ ) // For every seq in DB { PrjCnt = 0; if( !Inter ) JMax = (*pDB).m_pProjSeq[i].m_nProjCount; IncSup = true; #if defined( _USE_STRING_ELEMINATION ) //if( (*MainSeqTree).IsContained( pDB, (*aSeqWrap).GetFirst() ) ) if( CheckStrings ) { if( (*MainSeqTree).IsContained( pDB, GetStartPtr( pDB, i) ) ) { //Sequence * aS = new Sequence( pDB, GetStartPtr( pDB, i) ); //(*aS).Print(); //printf( " Sequence Eleminated.\n" ); continue; } } #endif // defined( _USE_STRING_ELEMINATION ) for( j=0; j < JMax; j++ ) { aSeqWrap = new SeqWrap( GetStartPtr( pDB, i, j ) ); if( !(*aSeqWrap).IsEmpty() ) { if( IncSup ) { Sup++; IncSup = false; } PostStart = (*aSeqWrap).GetItemSet( (*aPrefix).NumOfItemSets ); /* Sequence * aSeq = NULL; aSeq = new Sequence( GetStartPtr( pDB, i ), false ); printf( "Rec:\n " ); (*aSeq).Print(); delete aSeq; aSeq = new Sequence( PostStart, false ); printf( "Post:\n " ); (*aSeq).Print(); delete aSeq; */ if( !SeqIsEmpty( PostStart ) ) { buf_idx[ PrjCnt++ ] = int( PostStart ); } } delete aSeqWrap; } if( PrjCnt > 0 ) { tmpSeq = (struct PROJ_SEQ *) (*ProjDB).m_pProjSeq + (*ProjDB).m_nSup; (*ProjDB).m_nSup++; (*tmpSeq).m_nProjCount = PrjCnt; if( PrjCnt == 1 ) { (*tmpSeq).m_ppSeq = (int**) buf_idx[0]; } else { tmp = PrjCnt * sizeof(int); n_total_mem += tmp; (*tmpSeq).m_ppSeq = (int**) memalloc( tmp ); memcpy( (*tmpSeq).m_ppSeq, buf_idx, tmp ); } } } #if defined( _USE_OUTPUT_BUFFER ) (*ProjDB).OutputBuf = OutputPattern( (*ProjDB).m_pnPat, PatLen, Sup ); (*(*ProjDB).OutputBuf).Parent = (*pDB).OutputBuf; (*(*pDB).OutputBuf).ReferenceCount++; #else OutputPattern( (*ProjDB).m_pnPat, PatLen, Sup ); #endif if( (*ProjDB).m_nSup < gSUP ) { //Added by Ramin #if defined( _USE_OUTPUT_BUFFER ) //fprintf( gpFreqFile, "+++===>>>>>> Right Pattern " ); //(*(*ProjDB).OutputBuf).Print( gpFreqFile ); EmptyBuffer( aSeqList, (*ProjDB).OutputBuf ); #else if( n_max_mem < n_total_mem ) n_max_mem = n_total_mem; #endif #if defined( _FIND_MAX_SEQS ) && !defined( _DO_NAIVE_APPROACH ) //Sequence * aSeq = new Sequence( (*ProjDB).m_pnPat, (*ProjDB).m_nPatLen, (*ProjDB).m_nSup ); Sequence * aSeq = new Sequence( ProjDB, Sup ); (*MainSeqTree).AddSeq( aSeq ); #endif tmp = (*ProjDB).m_nMaxSup * sizeof( struct PROJ_SEQ ); n_total_mem -= tmp; freemem( (void**) &(*ProjDB).m_pProjSeq ); tmp = PatLen * sizeof(int); n_total_mem -= tmp; freemem( (void**)&((*ProjDB).m_pnPat) ); n_total_mem -= sizeof( struct PROJ_DB ); freemem( (void**)&(ProjDB) ); return NULL; } else return ProjDB; }
Prefix::Prefix( const struct PROJ_DB * pDB ) : SeqWrap() { //#define GetDBPrefix_PRINT_DEBUG_INFO int i = 0; int j = 0; int JMax = 1; bool Inter; SeqWrap * aSeqWrap = NULL; bool PrefixIsEmpty = true; #ifdef GetDBPrefix_PRINT_DEBUG_INFO Sequence * aSeq = NULL; Sequence * tmpSeq = NULL; static int Cnt = 0; tmpSeq = new Sequence( pDB ); printf( " Original Prefix %dth Record ===> ", Cnt++ ); (*tmpSeq).Print(); delete tmpSeq; #endif End = NULL; NumOfItemSets = 0; //Sup = 1; Sup = 0; if( (*pDB).m_nSup > 0 ) { // Find first none empty sequence. for( i=0; i<(*pDB).m_nSup; i++ ) { for( j=0; j < (*pDB).m_pProjSeq[i].m_nProjCount; j++ ) { aSeqWrap = new SeqWrap( GetStartPtr( pDB, i, j ) ); if( !(*aSeqWrap).IsEmpty() ) { Start = (*aSeqWrap).GetFirst(); End = GetEndPtr( Start ) - 1; //TrimPrefix( Start ); //Trim it with itself. PrefixIsEmpty = false; #ifdef GetDBPrefix_PRINT_DEBUG_INFO aSeq = new Sequence( Start, End-Start, 0, true ); printf( " First Prefix %dth Record ===> ", i+1 ); if( aSeq ) (*aSeq).Print(); printf( " Real First Start=%d End=%d Size=%d ===> ", Start, End, End-Start ); Print(); #endif delete aSeqWrap; break; } delete aSeqWrap; } if( Start != End ) break; } } if( Start != End ) { JMax = 1; if( *(Start) == -1 ) Inter = true; else Inter = false; //for( i++; i<(*pDB).m_nSup; i++ ) // For every seq in DB for( i; i<(*pDB).m_nSup; i++ ) // For every seq in DB { if( !Inter ) JMax = (*pDB).m_pProjSeq[i].m_nProjCount; //JMax = 1; for( j=0; j < JMax; j++ ) { aSeqWrap = new SeqWrap( GetStartPtr( pDB, i, j ) ); #ifdef GetDBPrefix_PRINT_DEBUG_INFO tmpSeq = new Sequence( (*aSeqWrap).GetFirst(), true ); printf( " %dth Record ===> ", i+1 ); (*tmpSeq).Print(); delete tmpSeq; #endif if( !(*aSeqWrap).IsEmpty() ) { TrimPrefix( (*aSeqWrap).GetFirst() ); Sup++; #ifdef GetDBPrefix_PRINT_DEBUG_INFO if( Start != End ) { aSeq = new Sequence( Start, End-Start, 0, true ); printf( " ++++++++++++++++ Remaining prefix is " ); (*aSeq).Print(); printf( " +++++Real Start=%d End=%d Size=%d prefix is ", Start, End, End-Start ); Print(); } #endif } delete aSeqWrap; PrefixIsEmpty = IsEmpty(); if( PrefixIsEmpty ) break; } if( PrefixIsEmpty ) break; } } //if( Start == End ) if( PrefixIsEmpty ) { Start = NULL; End = NULL; NumOfItemSets = 0; Sup = 0; } else { CalcNumOfItemSets(); } }