diff --git a/Storage/Disk/Disk.cpp b/Storage/Disk/Disk.cpp index f431bba4d..aee00e39d 100644 --- a/Storage/Disk/Disk.cpp +++ b/Storage/Disk/Disk.cpp @@ -7,3 +7,16 @@ // #include "Disk.hpp" + +using namespace Storage::Disk; + +std::shared_ptr Disk::get_track_at_position(unsigned int head, unsigned int position) +{ + int address = (int)(position * get_head_count() + head); + std::map>::iterator cached_track = cached_tracks_.find(address); + if(cached_track != cached_tracks_.end()) return cached_track->second; + + std::shared_ptr track = virtual_get_track_at_position(head, position); + cached_tracks_[address] = track; + return track; +} diff --git a/Storage/Disk/Disk.hpp b/Storage/Disk/Disk.hpp index 50466161e..2f974c2fb 100644 --- a/Storage/Disk/Disk.hpp +++ b/Storage/Disk/Disk.hpp @@ -10,6 +10,7 @@ #define Disk_hpp #include +#include #include "../Storage.hpp" namespace Storage { @@ -81,7 +82,13 @@ class Disk { /*! Returns the @c Track at @c position if there are any detectable events there; returns @c nullptr otherwise. */ - virtual std::shared_ptr get_track_at_position(unsigned int head, unsigned int position) = 0; + std::shared_ptr get_track_at_position(unsigned int head, unsigned int position); + + protected: + virtual std::shared_ptr virtual_get_track_at_position(unsigned int head, unsigned int position) = 0; + + private: + std::map> cached_tracks_; }; } diff --git a/Storage/Disk/Formats/AcornADF.cpp b/Storage/Disk/Formats/AcornADF.cpp index 98779de80..b0ca1144a 100644 --- a/Storage/Disk/Formats/AcornADF.cpp +++ b/Storage/Disk/Formats/AcornADF.cpp @@ -47,7 +47,7 @@ unsigned int AcornADF::get_head_count() return 1; } -std::shared_ptr AcornADF::get_track_at_position(unsigned int head, unsigned int position) +std::shared_ptr AcornADF::virtual_get_track_at_position(unsigned int head, unsigned int position) { std::shared_ptr track; diff --git a/Storage/Disk/Formats/AcornADF.hpp b/Storage/Disk/Formats/AcornADF.hpp index 17455e007..ccd2ea200 100644 --- a/Storage/Disk/Formats/AcornADF.hpp +++ b/Storage/Disk/Formats/AcornADF.hpp @@ -35,7 +35,8 @@ class AcornADF: public Disk, public Storage::FileHolder { // implemented to satisfy @c Disk unsigned int get_head_position_count(); unsigned int get_head_count(); - std::shared_ptr get_track_at_position(unsigned int head, unsigned int position); + private: + std::shared_ptr virtual_get_track_at_position(unsigned int head, unsigned int position); }; } diff --git a/Storage/Disk/Formats/D64.cpp b/Storage/Disk/Formats/D64.cpp index 50f54257b..75277d986 100644 --- a/Storage/Disk/Formats/D64.cpp +++ b/Storage/Disk/Formats/D64.cpp @@ -41,7 +41,7 @@ unsigned int D64::get_head_position_count() return number_of_tracks_*2; } -std::shared_ptr D64::get_track_at_position(unsigned int head, unsigned int position) +std::shared_ptr D64::virtual_get_track_at_position(unsigned int head, unsigned int position) { // every other track is missing, as is any head above 0 if(position&1 || head) diff --git a/Storage/Disk/Formats/D64.hpp b/Storage/Disk/Formats/D64.hpp index 83621faf1..afe0a16d8 100644 --- a/Storage/Disk/Formats/D64.hpp +++ b/Storage/Disk/Formats/D64.hpp @@ -34,9 +34,9 @@ class D64: public Disk, public Storage::FileHolder { // implemented to satisfy @c Disk unsigned int get_head_position_count(); - std::shared_ptr get_track_at_position(unsigned int head, unsigned int position); private: + std::shared_ptr virtual_get_track_at_position(unsigned int head, unsigned int position); unsigned int number_of_tracks_; uint16_t disk_id_; }; diff --git a/Storage/Disk/Formats/G64.cpp b/Storage/Disk/Formats/G64.cpp index f20598813..ddc3bf791 100644 --- a/Storage/Disk/Formats/G64.cpp +++ b/Storage/Disk/Formats/G64.cpp @@ -40,7 +40,7 @@ unsigned int G64::get_head_position_count() return number_of_tracks_ > 84 ? number_of_tracks_ : 84; } -std::shared_ptr G64::get_track_at_position(unsigned int head, unsigned int position) +std::shared_ptr G64::virtual_get_track_at_position(unsigned int head, unsigned int position) { std::shared_ptr resulting_track; diff --git a/Storage/Disk/Formats/G64.hpp b/Storage/Disk/Formats/G64.hpp index 31ca5ca60..34ec84c13 100644 --- a/Storage/Disk/Formats/G64.hpp +++ b/Storage/Disk/Formats/G64.hpp @@ -37,9 +37,9 @@ class G64: public Disk, public Storage::FileHolder { // implemented to satisfy @c Disk unsigned int get_head_position_count(); - std::shared_ptr get_track_at_position(unsigned int head, unsigned int position); private: + std::shared_ptr virtual_get_track_at_position(unsigned int head, unsigned int position); uint8_t number_of_tracks_; uint16_t maximum_track_size_; }; diff --git a/Storage/Disk/Formats/OricMFMDSK.cpp b/Storage/Disk/Formats/OricMFMDSK.cpp index 59b4b2b10..a581985da 100644 --- a/Storage/Disk/Formats/OricMFMDSK.cpp +++ b/Storage/Disk/Formats/OricMFMDSK.cpp @@ -36,7 +36,7 @@ unsigned int OricMFMDSK::get_head_count() return head_count_; } -std::shared_ptr OricMFMDSK::get_track_at_position(unsigned int head, unsigned int position) +std::shared_ptr OricMFMDSK::virtual_get_track_at_position(unsigned int head, unsigned int position) { long seek_offset = 0; switch(geometry_type_) diff --git a/Storage/Disk/Formats/OricMFMDSK.hpp b/Storage/Disk/Formats/OricMFMDSK.hpp index c87bd4d89..1f02ba36d 100644 --- a/Storage/Disk/Formats/OricMFMDSK.hpp +++ b/Storage/Disk/Formats/OricMFMDSK.hpp @@ -35,9 +35,9 @@ class OricMFMDSK: public Disk, public Storage::FileHolder { // implemented to satisfy @c Disk unsigned int get_head_position_count(); unsigned int get_head_count(); - std::shared_ptr get_track_at_position(unsigned int head, unsigned int position); private: + std::shared_ptr virtual_get_track_at_position(unsigned int head, unsigned int position); uint32_t head_count_; uint32_t track_count_; uint32_t geometry_type_; diff --git a/Storage/Disk/Formats/SSD.cpp b/Storage/Disk/Formats/SSD.cpp index aaa6e467a..07ddf242e 100644 --- a/Storage/Disk/Formats/SSD.cpp +++ b/Storage/Disk/Formats/SSD.cpp @@ -40,7 +40,7 @@ unsigned int SSD::get_head_count() return head_count_; } -std::shared_ptr SSD::get_track_at_position(unsigned int head, unsigned int position) +std::shared_ptr SSD::virtual_get_track_at_position(unsigned int head, unsigned int position) { std::shared_ptr track; diff --git a/Storage/Disk/Formats/SSD.hpp b/Storage/Disk/Formats/SSD.hpp index 074f57af0..7d8ccf0df 100644 --- a/Storage/Disk/Formats/SSD.hpp +++ b/Storage/Disk/Formats/SSD.hpp @@ -35,9 +35,9 @@ class SSD: public Disk, public Storage::FileHolder { // implemented to satisfy @c Disk unsigned int get_head_position_count(); unsigned int get_head_count(); - std::shared_ptr get_track_at_position(unsigned int head, unsigned int position); private: + std::shared_ptr virtual_get_track_at_position(unsigned int head, unsigned int position); unsigned int head_count_; unsigned int track_count_; };