| 6 |
| 7 | #include <algorithm> |
| 8 | #include <iterator> |
| 9 | #include <utility> |
| 10 |
| 11 | namespace base { |
| 12 |
| 13 | namespace { |
| 14 |
| 15 | // Supports heterogeneous comparisons on modules and addresses, for use in |
| 16 | // binary searching modules sorted by range for a contained address. |
| 17 | struct ModuleAddressCompare { |
| 18 | bool operator()(const std::unique_ptr<const ModuleCache::Module>& module, |
| 19 | uintptr_t address) const { |
| 20 | return module->GetBaseAddress() + module->GetSize() <= address; |
| 21 | } |
| 22 |
| 23 | bool operator()( |
| 24 | uintptr_t address, |
| 25 | const std::unique_ptr<const ModuleCache::Module>& module) const { |
| 26 | return address < module->GetBaseAddress(); |
| 27 | } |
| 28 | }; |
| 29 |
| 30 | } // namespace |
| 31 |
| 32 | ModuleCache::ModuleCache() = default; |
| 33 | ModuleCache::~ModuleCache() = default; |
| 34 |
| 35 | const ModuleCache::Module* ModuleCache::GetModuleForAddress(uintptr_t address) { |
| 36 | const auto non_native_module_loc = non_native_modules_.find(address); |
| 37 | if (non_native_module_loc != non_native_modules_.end()) |
| 38 | return non_native_module_loc->get(); |
| 39 |
| 40 | const auto native_module_loc = native_modules_.find(address); |
| 41 | if (native_module_loc != native_modules_.end()) |
| 42 | return native_module_loc->get(); |
| 43 |
| 44 | std::unique_ptr<const Module> new_module = CreateModuleForAddress(address); |
| 45 | if (!new_module) |
| 46 | return nullptr; |
| 47 | const auto loc = native_modules_.insert(std::move(new_module)); |
| 48 | return loc.first->get(); |
| 49 | } |
| 50 |
| 51 | std::vector<const ModuleCache::Module*> ModuleCache::GetModules() const { |
| 52 | std::vector<const Module*> result; |
| 53 | result.reserve(native_modules_.size()); |
| 54 | for (const std::unique_ptr<const Module>& module : native_modules_) |
| 55 | result.push_back(module.get()); |
| 56 | for (const std::unique_ptr<const Module>& module : non_native_modules_) |
| 57 | result.push_back(module.get()); |
| 58 | return result; |
| 59 | } |
| 60 |
| 61 | void ModuleCache::UpdateNonNativeModules( |
| 62 | const std::vector<const Module*>& to_remove, |
| 63 | std::vector<std::unique_ptr<const Module>> to_add) { |
| 64 | // Insert the modules to remove into a set to support O(log(n)) lookup below. |
| 65 | flat_set<const Module*> to_remove_set(to_remove.begin(), to_remove.end()); |
| 66 |
| 67 | // Reorder the modules to be removed to the last slots in the set, then move |
| 68 | // them to the inactive modules, then erase the moved-from modules from the |
| 69 | // set. The flat_set docs endorse using base::EraseIf() which performs the |
| 70 | // same operations -- exclusive of the moves -- so this is OK even though it |
| 71 | // might seem like we're messing with the internal set representation. |
| 72 | // |
| 73 | // remove_if is O(m*log(r)) where m is the number of current modules and r is |
| 74 | // the number of modules to remove. insert and erase are both O(r). |
| 75 | auto first_module_to_remove = std::remove_if( |
| 76 | non_native_modules_.begin(), non_native_modules_.end(), |
| 77 | [&to_remove_set](const std::unique_ptr<const Module>& module) { |
| 78 | return to_remove_set.find(module.get()) != to_remove_set.end(); |
| 79 | }); |
| 80 | // All modules requested to be removed should have been found. |
| 81 | DCHECK_EQ(static_cast<ptrdiff_t>(to_remove.size()), |
| 82 | std::distance(first_module_to_remove, non_native_modules_.end())); |
| 83 | inactive_non_native_modules_.insert( |
| 84 | inactive_non_native_modules_.end(), |
| 85 | std::make_move_iterator(first_module_to_remove), |
| 86 | std::make_move_iterator(non_native_modules_.end())); |
| 87 | non_native_modules_.erase(first_module_to_remove, non_native_modules_.end()); |
| 88 |
| 89 | // Insert the modules to be added. This operation is O((m + a) + a*log(a)) |
| 90 | // where m is the number of current modules and a is the number of modules to |
| 91 | // be added. |
| 92 | non_native_modules_.insert(std::make_move_iterator(to_add.begin()), |
| 93 | std::make_move_iterator(to_add.end())); |
| 94 | } |
| 95 |
| 96 | void ModuleCache::AddCustomNativeModule(std::unique_ptr<const Module> module) { |
| 97 | native_modules_.insert(std::move(module)); |
| 98 | } |
| 99 |
| 100 | bool ModuleCache::ModuleAndAddressCompare::operator()( |
| 101 | const std::unique_ptr<const Module>& m1, |
| 102 | const std::unique_ptr<const Module>& m2) const { |
| 103 | return m1->GetBaseAddress() < m2->GetBaseAddress(); |
| 104 | } |
| 105 |
| 106 | bool ModuleCache::ModuleAndAddressCompare::operator()( |
| 107 | const std::unique_ptr<const Module>& m1, |
| 108 | uintptr_t address) const { |
| 109 | return m1->GetBaseAddress() + m1->GetSize() <= address; |
| 110 | } |
| 111 |
| 112 | bool ModuleCache::ModuleAndAddressCompare::operator()( |
| 113 | uintptr_t address, |
| 114 | const std::unique_ptr<const Module>& m2) const { |
| 115 | return address < m2->GetBaseAddress(); |
| 116 | } |
| 117 |
| 118 | } // namespace base |
| 119 |