memory_view: fix possible nullptr crash, add exists() getter

This commit is contained in:
hap 2024-08-14 13:43:58 +02:00
parent 76323a8fd1
commit 1949b85c81
2 changed files with 16 additions and 12 deletions

View File

@ -2645,13 +2645,13 @@ public:
void select(int entry); void select(int entry);
void disable(); void disable();
bool exists() const { return m_config != nullptr; }
std::optional<int> entry() const { return m_cur_id == -1 ? std::optional<int>() : m_cur_slot; } std::optional<int> entry() const { return m_cur_id == -1 ? std::optional<int>() : m_cur_slot; }
const std::string &name() const { return m_name; } const std::string &name() const { return m_name; }
private: private:
device_t & m_device; device_t & m_device;
std::string m_name; std::string m_name;
std::map<int, int> m_entry_mapping; std::map<int, int> m_entry_mapping;
@ -2671,6 +2671,7 @@ private:
void make_subdispatch(std::string context); void make_subdispatch(std::string context);
int id_to_slot(int id) const; int id_to_slot(int id) const;
void register_state(); void register_state();
void refresh_id();
}; };

View File

@ -505,18 +505,25 @@ void memory_view::register_state()
{ {
m_device.machine().save().save_item(&m_device, "view", m_device.subtag(m_name).c_str(), 0, NAME(m_cur_slot)); m_device.machine().save().save_item(&m_device, "view", m_device.subtag(m_name).c_str(), 0, NAME(m_cur_slot));
m_device.machine().save().save_item(&m_device, "view", m_device.subtag(m_name).c_str(), 0, NAME(m_cur_id)); m_device.machine().save().save_item(&m_device, "view", m_device.subtag(m_name).c_str(), 0, NAME(m_cur_id));
m_device.machine().save().register_postload(save_prepost_delegate(NAME([this]() { m_handler_read->select_a(m_cur_id); m_handler_write->select_a(m_cur_id); }))); m_device.machine().save().register_postload(save_prepost_delegate(FUNC(memory_view::refresh_id), this));
}
void memory_view::refresh_id()
{
if (m_handler_read) {
m_handler_read->select_a(m_cur_id);
m_handler_write->select_a(m_cur_id);
}
if (m_space)
m_space->invalidate_caches(read_or_write::READWRITE);
} }
void memory_view::disable() void memory_view::disable()
{ {
m_cur_slot = -1; m_cur_slot = -1;
m_cur_id = -1; m_cur_id = -1;
m_handler_read->select_a(-1); refresh_id();
m_handler_write->select_a(-1);
if(m_space)
m_space->invalidate_caches(read_or_write::READWRITE);
} }
void memory_view::select(int slot) void memory_view::select(int slot)
@ -527,11 +534,7 @@ void memory_view::select(int slot)
m_cur_slot = slot; m_cur_slot = slot;
m_cur_id = i->second; m_cur_id = i->second;
m_handler_read->select_a(m_cur_id); refresh_id();
m_handler_write->select_a(m_cur_id);
if(m_space)
m_space->invalidate_caches(read_or_write::READWRITE);
} }
int memory_view::id_to_slot(int id) const int memory_view::id_to_slot(int id) const