Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Jan 13, 2025
1 parent 2254f7c commit 65db1f8
Showing 1 changed file with 18 additions and 32 deletions.
50 changes: 18 additions & 32 deletions cpp/devices/disk_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,54 +95,40 @@ shared_ptr<DiskTrack> DiskCache::Assign(int track)
}
}

// Next, check for empty
for (size_t i = 0; i < cache.size(); ++i) {
if (!cache[i].disktrk) {
// Try loading
if (Load(static_cast<int>(i), track, nullptr)) {
// Success loading
cache[i].serial = serial;
return cache[i].disktrk;
}

// Load failed
return nullptr;
// Check for an empty cache slot
for (CacheData &c : cache) {
if (!c.disktrk && Load(&c - &cache[0], track, nullptr)) {
c.serial = serial;
return c.disktrk;
}
}

// Finally, find the youngest serial number and delete it

// Set index 0 as candidate c
// Find the cache entry with the smallest serial number, i.e. the oldest entry
uint32_t s = cache[0].serial;
size_t c = 0;

// Compare candidate with serial and update to smaller one
for (size_t i = 0; i < cache.size(); ++i) {
for (size_t i = 1; i < cache.size(); ++i) {
assert(cache[i].disktrk);

// Compare and update the existing serial
if (cache[i].serial < s) {
s = cache[i].serial;
c = i;
}
}

// Save this track
if (!cache[c].disktrk->Save(sec_path, cache_miss_write_count)) {
return nullptr;
}

// Delete this track
shared_ptr<DiskTrack> disktrk = cache[c].disktrk;
cache[c].disktrk.reset();

if (Load(static_cast<int>(c), track, disktrk)) {
// Successful loading
cache[c].serial = serial;
return cache[c].disktrk;
if (cache[c].disktrk->Save(sec_path, cache_miss_write_count)) {
// Delete this track
shared_ptr<DiskTrack> disktrk = cache[c].disktrk;
cache[c].disktrk.reset();

if (Load(static_cast<int>(c), track, disktrk)) {
// Successful loading
cache[c].serial = serial;
return cache[c].disktrk;
}
}

// Load failed
// Save or load failed
return nullptr;
}

Expand Down

0 comments on commit 65db1f8

Please sign in to comment.