MCPcopy Create free account
hub / github.com/PiSCSI/piscsi / Assign

Method Assign

cpp/devices/disk_cache.cpp:76–139  ·  view source on GitHub ↗

--------------------------------------------------------------------------- Track Assignment ---------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

74//
75//---------------------------------------------------------------------------
76shared_ptr<DiskTrack> DiskCache::Assign(int64_t track)
77{
78 assert(sec_size != 0);
79 assert(track >= 0);
80
81 // First, check if it is already assigned
82 for (cache_t& c : cache) {
83 if (c.disktrk && c.disktrk->GetTrack() == track) {
84 // Track match
85 c.serial = serial;
86 return c.disktrk;
87 }
88 }
89
90 // Next, check for empty
91 for (size_t i = 0; i < cache.size(); i++) {
92 if (cache[i].disktrk == nullptr) {
93 // Try loading
94 if (Load(static_cast<int>(i), track, nullptr)) {
95 // Success loading
96 cache[i].serial = serial;
97 return cache[i].disktrk;
98 }
99
100 // Load failed
101 return nullptr;
102 }
103 }
104
105 // Finally, find the youngest serial number and delete it
106
107 // Set index 0 as candidate c
108 uint32_t s = cache[0].serial;
109 size_t c = 0;
110
111 // Compare candidate with serial and update to smaller one
112 for (size_t i = 0; i < cache.size(); i++) {
113 assert(cache[i].disktrk);
114
115 // Compare and update the existing serial
116 if (cache[i].serial < s) {
117 s = cache[i].serial;
118 c = i;
119 }
120 }
121
122 // Save this track
123 if (!cache[c].disktrk->Save(sec_path, cache_miss_write_count)) {
124 return nullptr;
125 }
126
127 // Delete this track
128 shared_ptr<DiskTrack> disktrk = cache[c].disktrk;
129 cache[c].disktrk.reset();
130
131 if (Load(static_cast<int>(c), track, disktrk)) {
132 // Successful loading
133 cache[c].serial = serial;

Callers

nothing calls this directly

Calls 2

GetTrackMethod · 0.45
SaveMethod · 0.45

Tested by

no test coverage detected