/*The allocate works in this way:
At the beginning, no block is allocated, we need to find the free address,
we use GetAddrOfOffset().
As time goes by, more and more blocks are allocated using the above method,
until when some blocks are freed by user, then GetFreeBlock() can get a
second hand block, we just need to clear the memory to put it into use again.
 */
void* FixedMemPool::Alloc(bool zero) {
    AllocBlock* p = GetFreeBlock();
    if (p == NULL) {
        //can not find free block, we need to allocate from the pool
        if (m_header->used_size + m_header->block_size <= m_header->max_size) {
            p = (AllocBlock*)GetAddrOfOffset(m_header->used_size);
            //a new block is used
            m_header->used_size += m_header->block_size;
            m_header->used_node_num++;
        } else {
            snprintf(m_error_msg, sizeof(m_error_msg), "Alloc() error, No space left in the pool");
        }
    } else if (!IsValidBlock(p)){
        snprintf(m_error_msg, sizeof(m_error_msg), "Alloc() error, wrong free block format");
        p = NULL;
    }

    if (p == NULL) {
        return NULL;
    }

    //if use former freed block, reset the memory
    if (zero) {
        memset(p, 0, m_header->block_size);
    }

    //0 mean p is not in the free list
    p->next = 0;
    return p->node;
}
Пример #2
0
/**
Loops through until a valid chunk is identified.
@param aStartPos
@return ETrue if valid chunk is found.
*/
TBool CAviReader::ReadNextFrameStartL(TInt& aStartPos)
{
    TBool found = EFalse;
    TInt count = 0;

    CMMFDescriptorBuffer* buffer = CMMFDescriptorBuffer::NewL(KReadBufferSize);
    CleanupStack::PushL(buffer);
    iClip.ReadBufferL(buffer, aStartPos);

    TUint8* rawform = &(buffer->Data()[0]);

    while (count < KReadBufferSize)
    {
        if (IsValidBlock(Read32(rawform)))
        {
            found = ETrue;
            break;
        }
        aStartPos++;
        rawform++;
        count++;
    }
    CleanupStack::PopAndDestroy(buffer);
    return found;
}
Пример #3
0
/**
// DEF113319 - added to check last audio chunk
Loops through to check whether valid audio chunk is available.
@param aStartPos
@return ETrue if valid audio chunk is found.
*/
TBool CAviReader::IsAudioDataAvailableL(TInt aStartPos)
{
    TBool foundValidAudioChunk = EFalse;
    TUint32 dwChunkId = 0;
    TUint8* rawform = NULL;

    CMMFDescriptorBuffer* headerFrame = CMMFDescriptorBuffer::NewL(KChunkHeaderSize);
    CleanupStack::PushL(headerFrame);

    for(;;)
    {
        iClip.ReadBufferL(headerFrame, aStartPos);
        TInt len = headerFrame->Data().Length();
        if (len == 0)
        {
            // end of stream
            break;
        }
        rawform = &(headerFrame->Data()[0]);
        dwChunkId = Read32(rawform);

        if (!IsValidBlock(dwChunkId))
        {
            // No valid chunk id found ...
            //Sometimes we dont get next chunk immediately after the Previous chunk(Padding).
            //we need to search for next valid data chunk ourselves if the chunkId read is not valid
            if (!ReadNextFrameStartL(aStartPos))
            {
                // reached EOS
                break;
            }
            continue;//found valid chunk. so continue to read the chunk
        }
        if ((dwChunkId == KRiffChunkName00wb) || (dwChunkId == KRiffChunkName01wb)) // check if its valid audio chunk
        {
            foundValidAudioChunk = ETrue;
            break;
        }
        // non audio chunk found, so go and look for next frame header
        rawform += 4; //Skip ChunkId
        aStartPos += ((Read32(rawform)) + 4 + 4); //Skip: 1. dwChunkSz, 2. ChunkSize(4), 3.ChunkId(4)
    }
    CleanupStack::PopAndDestroy(headerFrame);
    return foundValidAudioChunk;
}
void* ResultsReaderThread::ReadResults(void* args) {
  // Get result reader arguments
  PicoDrv** pico_drivers = ((ResultsReaderThreadArgs*)args)->pico_drivers;
  int** streams = ((ResultsReaderThreadArgs*)args)->streams;
  ThreadQueue<HighScoreRegion>* hsr_queue = ((ResultsReaderThreadArgs*)args)->hsr_queue;
  QuerySeqManager* query_seq_manager = ((ResultsReaderThreadArgs*)args)->query_seq_manager;
  RefSeqManager* ref_seq_manager = ((ResultsReaderThreadArgs*)args)->ref_seq_manager;
  ThreadQueue<EngineJob>** engine_job_queues = ((ResultsReaderThreadArgs*)args)->engine_job_queues;

  // Set up memory buffers
  uint32_t*** read_mem_buf = new uint32_t**[NUM_FPGAS];
  for (int i = 0; i < NUM_FPGAS; i++) {
    read_mem_buf[i] = new uint32_t*[NUM_ENGINES_PER_FPGA];
    for (int j = 0; j < NUM_ENGINES_PER_FPGA; j++) {
      read_mem_buf[i][j] = new uint32_t[4096];
      for (int k = 0; k < 4096; k++) {
        read_mem_buf[i][j][k] = 0;
      }
    }
  }

  // Initialize FSM states and values
  HSRParserState* states[NUM_FPGAS];
  for (int i = 0; i < NUM_FPGAS; i++) {
    states[i] = new HSRParserState[NUM_ENGINES_PER_FPGA];
    for (int j = 0; j < NUM_ENGINES_PER_FPGA; j++) {
      states[i][j] = INIT;
    }
  }
  CoalescedHighScoreBlock* chsbs[NUM_FPGAS];
  EngineJob* jobs[NUM_FPGAS];
  for (int i = 0; i < NUM_FPGAS; i++) {
    chsbs[i] = new CoalescedHighScoreBlock[NUM_ENGINES_PER_FPGA];
    jobs[i] = new EngineJob[NUM_ENGINES_PER_FPGA];
  }

  int num_HSRs = 0;

  while(true) {
    for (int i = 0; i < NUM_FPGAS; i++) {
      for (int j = 0; j < NUM_ENGINES_PER_FPGA; j++) {
        int num_bytes_available = pico_drivers[i]->GetBytesAvailable(streams[i][j], true);
        //std::cout<<num_bytes_available<<" ";
        if (num_bytes_available >= 16) {
          
          // Read full 128-bit packets (check might not be necessary)
          int num_bytes_to_read = num_bytes_available > 4096 ? 4096 : (num_bytes_available/16)*16;
          pico_drivers[i]->ReadStream(streams[i][j], read_mem_buf[i][j], num_bytes_to_read);

          for (int k = 0; k < num_bytes_to_read / 16; k++) {
            //std::cout<<"Read stream:\t"<<i<<" "<<j<<"\tHSB:"<< read_mem_buf[i][j][k] << "\tQuery ID:" << read_mem_buf[i][j][k+1] << std::endl;
            uint32_t high_score_block = read_mem_buf[i][j][k*4];
            uint32_t query_id = read_mem_buf[i][j][k*4 + 1];
            
            // High scoring ref seq block parser FSM
            switch(states[i][j]) {
              case INIT:
                  jobs[i][j] = engine_job_queues[i][j].Pop();
                  
                  //std::cout<<"Engine Job:\tQuery ID:"<<jobs[i][j].query_id<<" Ref ID: "<<jobs[i][j].ref_id<<" Chr ID: " <<jobs[i][j].chr_id<<" Ref Offset: "<<jobs[i][j].ref_offset<<" Ref Len: "<<jobs[i][j].ref_len<<" Overlap Offset: "<<jobs[i][j].overlap_offset<<" Threshold: "<<jobs[i][j].threshold<<std::endl;
                  if (high_score_block == END_OF_ENGINE_ALIGNMENT) {
                    query_seq_manager->DecHighScoreRegionCount(jobs[i][j].query_id);
                    states[i][j] = INIT;
                  } else if (IsValidBlock(jobs[i][j], high_score_block)) {
                    chsbs[i][j] = StartCHSB(high_score_block);
                    states[i][j] = IN_HSR;
                  } else {
                    std::cerr << "Invalid high scoring block received! " << jobs[i][j].ref_offset <<" " <<jobs[i][j].ref_len<<" " <<high_score_block << std::endl;
                    assert(false);
                  }
                break;

              case IN_HSR:
                if (high_score_block == END_OF_ENGINE_ALIGNMENT) {
                  StoreHSR(chsbs[i][j], jobs[i][j], hsr_queue, query_seq_manager, ref_seq_manager);
                  num_HSRs++;
                  if (num_HSRs % 10000 == 0) {
                    std::cout << "HSRS: " << num_HSRs << std::endl;
                  }
                  query_seq_manager->DecHighScoreRegionCount(jobs[i][j].query_id);
                  //std::cout<<"Query " <<jobs[i][j].query_id<<" Decrement HSR count"<<std::endl;
                  states[i][j] = INIT;
                } else if (IsValidBlock(jobs[i][j], high_score_block) && IsAdjacentBlock(high_score_block, chsbs[i][j])) {
                  chsbs[i][j] = ExtendCHSB(chsbs[i][j]);
                  states[i][j] = IN_HSR;
                } else if (IsValidBlock(jobs[i][j], high_score_block)) {
                  StoreHSR(chsbs[i][j], jobs[i][j], hsr_queue, query_seq_manager, ref_seq_manager);
                  num_HSRs++;
                  if (num_HSRs % 10000 == 0) {
                    std::cout << "HSRS: " << num_HSRs << std::endl;
                  }
                  chsbs[i][j] = StartCHSB(high_score_block);
                  states[i][j] = IN_HSR;
                } else {
                  StoreHSR(chsbs[i][j], jobs[i][j], hsr_queue, query_seq_manager, ref_seq_manager);
                  num_HSRs++;
                  if (num_HSRs % 10000 == 0) {
                    std::cout << "HSRS: " << num_HSRs << std::endl;
                  }
                  states[i][j] = INIT;
                }
                break;
              
              case NOT_IN_HSR:
                if (high_score_block == END_OF_ENGINE_ALIGNMENT) {
                  query_seq_manager->DecHighScoreRegionCount(jobs[i][j].query_id);
                  states[i][j] = INIT;
                } else if (IsValidBlock(jobs[i][j], high_score_block)) {
                  chsbs[i][j] = StartCHSB(high_score_block);
                  states[i][j] = IN_HSR;
                }
                break;

              default: // Shouldn't get here
                break;
            }
          }
        }
      }
    }
  }
}
Пример #5
0
/**
Identifies if the chunk is valid by the media type and fills the data in buffer
@param aMediaType
@param aPosition
*/
void CAviReader::ReadNextFrameL(TUid aMediaType, TInt& aPosition)
{
    TUint32 dwChunkId = 0;
    TUint32 dwChunkSz = 0;
    TUint8* rawForm = NULL;

    //We read chunkId and chunkSize first into the headerframe below. If Audio and Video chunks are found, data is read into the respective buffers
    //we skip chunkSize bytes if we come across any other blocks.
    CMMFDescriptorBuffer* headerFrame = CMMFDescriptorBuffer::NewL(KChunkHeaderSize);
    CleanupStack::PushL(headerFrame);

    for(;;)
    {
        iClip.ReadBufferL(headerFrame, aPosition);
        TInt len = headerFrame->Data().Length();
        if (len == 0)
        {
            SetMediaEOS(aMediaType);
            break;
        }

        rawForm = &(headerFrame->Data()[0]);
        dwChunkId = Read32(rawForm);
        if (!IsValidBlock(dwChunkId))
        {
            //Sometimes we dont get next chunk immediately after the Previous chunk(Padding).
            //we need to search for next valid data chunk ourselves if the chunkId read is not valid
            if (!ReadNextFrameStartL(aPosition))
            {
                //no more data frames
                SetMediaEOS(aMediaType);
                break;
            }
            continue;//found valid chunk. so continue to read the chunk
        }

        rawForm += 4; //Skip ChunkId
        aPosition += 4; //Skip ChunkId
        dwChunkSz = Read32(rawForm);

        rawForm+=4; //Skip ChunkSize
        aPosition += 4; //Skip ChunkSIze

        if (dwChunkSz == 0)
        {
            //We may get chunks of length Zero. So loop again to read the next chunk.
            continue;
        }
        if (dwChunkId == KRiffChunkNameList)//list rec
        {
            if(iMainHeader.iFlags == KAVIF_ISINTERLEAVED)
            {
                //found REC list. Read the whole list into the SourceBuffer
                delete iSourceBuffer;
                iSourceBuffer = NULL;
                iSourceBuffer = CMMFDescriptorBuffer::NewL(dwChunkSz);
                iClip.ReadBufferL(iSourceBuffer,aPosition, this);
                aPosition += dwChunkSz;//skip the list size
                break;
            }
            else
            {
                continue;
            }
        }
        else if (dwChunkId == KRiffChunkNameJunk)//junk
        {
            //found Junk chunk. Skip it.
            aPosition += dwChunkSz;
        }
        else if ((dwChunkId == KRiffChunkName00db) || (dwChunkId == KRiffChunkName00dc)
                 || (dwChunkId == KRiffChunkName01db) || (dwChunkId == KRiffChunkName01dc)) //video
        {
            if (aMediaType == KUidMediaTypeVideo)
            {
                //found video when aMediaType is Video. So read the video chunk into the videobuffer.
                delete iVideoBuffer;
                iVideoBuffer = NULL;
                iVideoBuffer = CMMFDescriptorBuffer::NewL(dwChunkSz);
                iClip.ReadBufferL(iVideoBuffer,aPosition, this);
                aPosition += dwChunkSz;
                break;
            }
            else
            {
                //found video when aMediaType is not Video. So Skip it.
                aPosition += dwChunkSz;
            }
        }
        else if ((dwChunkId == KRiffChunkName00wb) || (dwChunkId == KRiffChunkName01wb)) //audio
        {
            if (aMediaType == KUidMediaTypeAudio)
            {
                //found audio when aMediaType is Audio. So read the audio chunk into the audiobuffer.
                delete iAudioBuffer;
                iAudioBuffer = NULL;
                iAudioBuffer = CMMFDescriptorBuffer::NewL(dwChunkSz);
                iClip.ReadBufferL(iAudioBuffer,aPosition, this);
                aPosition += dwChunkSz;
                break;
            }
            else
            {
                //found audio when aMediaType is not audio. So Skip it.
                aPosition += dwChunkSz;
            }
        }
        else if (dwChunkId == KRiffChunkNameIdx1)//index block
        {
            aPosition += dwChunkSz;
            //Reached index block. This is placed at the end of the data. No more data
            SetMediaEOS(aMediaType);
            break;
        }
    }
    CleanupStack::PopAndDestroy(headerFrame);

}