From e62e5789314c8f6fd5817a532ac87f3cefa5e1c4 Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Thu, 3 Apr 2025 13:10:34 -0700 Subject: [PATCH] Remove bus_t::find_device variant that lacks len argument --- riscv/devices.cc | 32 ++++++++++++++++---------------- riscv/devices.h | 2 -- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/riscv/devices.cc b/riscv/devices.cc index 99e19b0a..59c065ec 100644 --- a/riscv/devices.cc +++ b/riscv/devices.cc @@ -55,26 +55,26 @@ reg_t bus_t::size() return 0; } -std::pair bus_t::find_device(reg_t addr) +std::pair bus_t::find_device(reg_t addr, size_t len) { + if (!len || addr + len - 1 < addr) + return std::make_pair(0, nullptr); + // Obtain iterator to device immediately after the one that might match - auto it = devices.upper_bound(addr); - if (devices.empty() || it == devices.begin()) { - // No devices with base address <= addr - return std::make_pair((reg_t)0, (abstract_device_t*)NULL); + auto it_after = devices.upper_bound(addr); + reg_t base, size; + if (it_after != devices.begin()) { + // Obtain iterator to device that might match + auto it = std::prev(it_after); + base = it->first; + size = it->second->size(); + if (addr - base + len - 1 < size) { + // it fully contains [addr, addr + len) + return std::make_pair(it->first, it->second); + } } - // Rewind to device that will match if its size suffices - it--; - - return std::make_pair(it->first, it->second); -} - -std::pair bus_t::find_device(reg_t addr, size_t len) -{ - if (auto [base, dev] = find_device(addr); addr - base + len - 1 < dev->size()) - return std::make_pair(base, dev); - + // No matching device return std::make_pair(0, nullptr); } diff --git a/riscv/devices.h b/riscv/devices.h index 4e113013..479bd3e1 100644 --- a/riscv/devices.h +++ b/riscv/devices.h @@ -24,8 +24,6 @@ class bus_t : public abstract_device_t { std::pair find_device(reg_t addr, size_t len); private: - std::pair find_device(reg_t addr); - std::map devices; };