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 8c73616 commit 73debdb
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions cpp/devices/disk_track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,18 @@ bool DiskTrack::Load(const string &path, uint64_t &cache_miss_read_count)

++cache_miss_read_count;

// Calculate offset (previous tracks are considered to hold 256 sectors)
off_t offset = (off_t)track_number << 8;
offset <<= shift_count;

const int size = sector_count << shift_count;

if (!buffer && !posix_memalign((void**)&buffer, 512, ((size + 511) / 512) * 512)) {
buffer_size = size;
}

// Reallocate if the buffer length is different
if (buffer && buffer_size != static_cast<uint32_t>(size)) {
free(buffer); // NOSONAR free() must be used here because of allocation with posix_memalign
// Allocate or reallocate the buffer
if (!buffer || buffer_size != static_cast<uint32_t>(size)) {
free(buffer); // NOSONAR free() must be used here due to posix_memalign
buffer = nullptr;
if (!posix_memalign((void**)&buffer, 512, ((size + 511) / 512) * 512)) {
buffer_size = size;

if (posix_memalign((void**)&buffer, 512, (size + 511) & ~511)) {
return false;
}
}

if (!buffer) {
return false;
buffer_size = size;
}

modified_flags.resize(sector_count);
Expand All @@ -74,6 +65,10 @@ bool DiskTrack::Load(const string &path, uint64_t &cache_miss_read_count)
return false;
}

// Calculate offset (previous tracks are considered to hold 256 sectors)
off_t offset = static_cast<off_t>(track_number) << 8;
offset <<= shift_count;

in.seekg(offset);
in.read((char*)buffer, size);
return in.good();
Expand Down

0 comments on commit 73debdb

Please sign in to comment.