| 49 | } |
| 50 | |
| 51 | bool DiskTrack::Load(const string& path, uint64_t& cache_miss_read_count) |
| 52 | { |
| 53 | // Not needed if already loaded |
| 54 | if (dt.init) { |
| 55 | assert(dt.buffer); |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | ++cache_miss_read_count; |
| 60 | |
| 61 | // Calculate offset (previous tracks are considered to hold 256 sectors) |
| 62 | off_t offset = ((off_t)dt.track << 8); |
| 63 | if (dt.raw) { |
| 64 | assert(dt.size == 11); |
| 65 | offset *= 0x930; |
| 66 | offset += 0x10; |
| 67 | } else { |
| 68 | offset <<= dt.size; |
| 69 | } |
| 70 | |
| 71 | // Add offset to real image |
| 72 | offset += dt.imgoffset; |
| 73 | |
| 74 | // Calculate length (data size of this track) |
| 75 | const int64_t length = dt.sectors << dt.size; |
| 76 | |
| 77 | // Allocate buffer memory |
| 78 | assert((dt.sectors > 0) && (dt.sectors <= 0x100)); |
| 79 | |
| 80 | if (dt.buffer == nullptr) { |
| 81 | if (posix_memalign((void **)&dt.buffer, 512, ((length + 511) / 512) * 512)) { |
| 82 | spdlog::warn("posix_memalign failed"); |
| 83 | } |
| 84 | dt.length = length; |
| 85 | } |
| 86 | |
| 87 | if (dt.buffer == nullptr) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | // Reallocate if the buffer length is different |
| 92 | if (dt.length != static_cast<uint32_t>(length)) { |
| 93 | free(dt.buffer); |
| 94 | if (posix_memalign((void **)&dt.buffer, 512, ((length + 511) / 512) * 512)) { |
| 95 | spdlog::warn("posix_memalign failed"); |
| 96 | } |
| 97 | dt.length = length; |
| 98 | } |
| 99 | |
| 100 | // Resize and clear changemap |
| 101 | dt.changemap.resize(dt.sectors); |
| 102 | fill(dt.changemap.begin(), dt.changemap.end(), false); //NOSONAR ranges::fill() cannot be applied to vector<bool> |
| 103 | |
| 104 | ifstream in(path, ios::binary); |
| 105 | if (in.fail()) { |
| 106 | return false; |
| 107 | } |
| 108 | |