bool CFat32Device::ReadFromCache(unsigned long sector, unsigned char *buffer) { // round sector size down to page size unsigned long page = sector & ~(FAT_PAGE_SIZE / m_sectorsize - 1); FATCACHE::iterator it = m_cache.find(page); if (it != m_cache.end()) { cacheHit++; CSector *cacheSector = (*it).second; cacheSector->IncrementUsage(timeGetTime()); fast_memcpy(buffer, cacheSector->Get() + (sector - page)*m_sectorsize, m_sectorsize); return true; } return false; }
void CFat32Device::CachePage(unsigned long page, unsigned char *buffer) { DWORD time = timeGetTime(); if (m_cache.size() >= CACHE_SIZE) { // remove the last used cache FATCACHE::iterator lastUsed = m_cache.begin(); //CStdString usage; unsigned long smallest = 0; for (FATCACHE::iterator it = m_cache.begin(); it != m_cache.end(); ++it) { if ((*it).second->Usage(time) > (*lastUsed).second->Usage(time)) lastUsed = it; if ((*it).second->Usage(time) < smallest) smallest = (*it).second->Usage(time); /* CStdString log; if ((*it).second->Usage(time) > 0xFF) log = "FF "; else log.Format("%02x ", (*it).second->Usage()); usage += log;*/ } //CLog::Log(LOGDEBUG, "%s", usage.c_str()); // lastUsed is the one we remove unsigned long oldpage = (*lastUsed).first; CSector *sector = (*lastUsed).second; m_cache.erase(lastUsed); // CLog::Log(LOGDEBUG, "Swapping page %i for page %i, oldest usage: %d (%d)", oldpage, page, sector->Usage(time), smallest); #ifdef FAT32_ALLOW_WRITING // write it out to disk if dirty if (sector->IsDirty()) WritePage(oldpage, sector->Get()); #endif delete sector; } // cache our page m_cache.insert(pair<unsigned int, CSector *>(page, new CSector(buffer, time))); }