Example #1
0
bool
TEightTrack::ReadNextSegment ()
{
	char		track;
	sector_t	sector;	
	size_t	x;


	if (!segmentsToRead)
	{
		// read all there is to read
		return FALSE;
	}

	// BUGBUG, this is an inefficient way to check befor reading track byte
	if (CacheIsFull())
	{
		return FALSE;
	}

	// read byte, gives track number
	x = fread (&track, 1, 1, fpPhysFile);
	ASSERT (x == 1);

	// if this fails, we are out of synch with file
	ASSERT (track < 8);

	// update count
	segmentsToRead--;

	if (aTracks[track].fIgnoreThisTrack)
	{
		// We are ignoring this track. This can happen if
		// there is no sound driver.
		fseek (fpPhysFile, SECTOR_SIZE, SEEK_CUR);
		return TRUE;
	}

	// alloc sector, read into it
	sector = AllocSector (track);

	// CacheIsFull() should have caught this, but just to be sure
	ASSERT (sector != -1);

	SetSegmentSector(track, sector);

	// read segment
	x = fread (GetSectorPtr(sector), SECTOR_SIZE, 1, fpPhysFile);
	ASSERT (x == 1);

	// return successful
	return TRUE;
}
/**
Implementation of pure virtual function.
@see    MWTCacheInterface::MakePageMRU()
*/
void CDynamicDirCache::MakePageMRU(TInt64 aPos)
    {
    __PRINT1(_L("MakePageMRU (%lx)"), aPos);
//  __PRINT4(_L("Current Cache State: iLockedQCount=%d, iUnlockedQCount=%d, iLookupTbl=%d, iMaxSizeInPages=%d"), iLockedQCount, iUnlockedQCount, iLookupTable.Count(), iMaxSizeInPages);
    // check the MRU page first, if it is already the MRU page, we can return immediately
    TInt64 pageStartMedPos = CalcPageStartPos(aPos);
    if (!iLockedQ.IsEmpty())
        {
        if (iLockedQ.First()->StartPos() == pageStartMedPos)
            {
            return;
            }
        }

    TDynamicDirCachePage* pPage = FindPageByPos(aPos);
    if (pPage)
        {
        ASSERT(pPage->IsValid());
        // lock page before make it MRU
        if (pPage->PageType() == TDynamicDirCachePage::EUnlocked)
            {
            ASSERT(!pPage->IsLocked());
            if (LockPage(pPage) == NULL)
                {
                DeQueue(pPage);
                LookupTblRemove(pPage->StartPos());
                DecommitPage(pPage);
                delete pPage;
                pPage = NULL;
                }
            }
        else
            {
            // error checking: page should either be locked or active
            ASSERT(LockPage(pPage) != NULL);
            }
        }

    // if page not found or page data not valid anymore, use active page to read data
    if (!pPage)
        {
        TRAPD(err, pPage = UpdateActivePageL(aPos));
        if (err != KErrNone)
            {
            // problem occurred reading active page, return immediately.
            return;
            }
        }

    // by now, the page is either locked or active page
    ASSERT(pPage && pPage->IsValid() && pPage->IsLocked());

    switch (pPage->PageType())
        {
        // if the page is the active page, we will need to find a new active page for replacement
        case TDynamicDirCachePage::EActivePage:
            {
            TDynamicDirCachePage* newAP = NULL;
            // if there is more cache room available, try to create a new page first
            if (!CacheIsFull())
                {
                // allocate and lock a new page
                TRAPD(err, newAP = AllocateAndLockNewPageL(0));
                // if any error ocurrs, return immediately
                if (err != KErrNone)
                    {
                    // unlock the page that was originally unlocked before leave
                    if (pPage->PageType() == TDynamicDirCachePage::EUnlocked)
                        {
                        UnlockPage(pPage);
                        }
                    return;
                    }

                if (newAP)
                    {
                    // replace the active page with the new page
                    newAP->SetPageType(TDynamicDirCachePage::EActivePage);
                    iActivePage = newAP;
                    }
                }

            // if cache has grown to its max size, or new page allocation failed
            if (!newAP)
                {
                // try to lock the LRU page on the unlocked page queque first
                if (!iUnlockedQ.IsEmpty())
                    {
                    newAP = iUnlockedQ.Last();
                    ASSERT(newAP->IsValid());
                    if (LockPage(newAP) != NULL)
                        {
                        // deque, reset pos, set new type
                        DeQueue(newAP);
                        LookupTblRemove(newAP->StartPos());
                        ResetPagePos(newAP);
                        newAP->SetPageType(TDynamicDirCachePage::EActivePage);
                        // replace active page
                        iActivePage = newAP;
                        }
                    // if falied locking the LRU page from unclocked queque,
                    // delete it
                    else
                        {
                        DeQueue(newAP);
                        LookupTblRemove(newAP->StartPos());
                        DecommitPage(newAP);
                        delete newAP;
                        newAP = NULL;
                        }
                    }
                }

            // if still have not found new active page
            // grab the LRU page from Locked Page Queue for active page
            if (!newAP)
                {
                ASSERT(!iLockedQ.IsEmpty());
                newAP = iLockedQ.Last();
                // deque, reset pos, set new type
                DeQueue(newAP);
                LookupTblRemove(newAP->StartPos());
                ResetPagePos(newAP);
                newAP->SetPageType(TDynamicDirCachePage::EActivePage);
                // replace active page
                iActivePage = newAP;
                }

            // we should always be able to find a locked page for active page
            ASSERT(newAP != NULL);

            // make original page (i.e. former active page) MRU
            // add onto locked queue
            AddFirstOntoQueue(pPage, TDynamicDirCachePage::ELocked);
            // add onto lookuptbl, as active page is not on lookup tbl originally
            LookupTblAdd(pPage);
            // check cache limit
            CheckThresholds();
            return;
            }
        case TDynamicDirCachePage::EUnlocked:
            {
            // if page was originally on Unlocked Page Queque, remove it from Unlocked Page Queue, add it
            // to the Locked Page Queue and make it MRU
            DeQueue(pPage);
            AddFirstOntoQueue(pPage, TDynamicDirCachePage::ELocked);
            // check cache limit
            CheckThresholds();
            return;
            }
        case TDynamicDirCachePage::ELocked:
            {
            // otherwise the page was on Locked Page Queue, make it MRU
            // no need to check cache limit
            if (pPage != iLockedQ.First())
                {
                DeQueue(pPage);
                AddFirstOntoQueue(pPage, TDynamicDirCachePage::ELocked);
                return;
                }
            break;
            }
        default:
            ASSERT(0);
        }
    }