Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "oat_file_manager.h" |
| 18 | |
| 19 | #include <memory> |
| 20 | #include <queue> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "base/logging.h" |
| 24 | #include "base/stl_util.h" |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 25 | #include "dex_file-inl.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 26 | #include "gc/space/image_space.h" |
| 27 | #include "oat_file_assistant.h" |
| 28 | #include "thread-inl.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | // For b/21333911. |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 33 | // Only enabled for debug builds to prevent bit rot. There are too many performance regressions for |
| 34 | // normal builds. |
| 35 | static constexpr bool kDuplicateClassesCheck = kIsDebugBuild; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 36 | |
| 37 | const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 38 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 39 | DCHECK(oat_file != nullptr); |
| 40 | if (kIsDebugBuild) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 41 | CHECK(oat_files_.find(oat_file) == oat_files_.end()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 42 | for (const std::unique_ptr<const OatFile>& existing : oat_files_) { |
| 43 | CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation(); |
| 44 | // Check that we don't have an oat file with the same address. Copies of the same oat file |
| 45 | // should be loaded at different addresses. |
| 46 | CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location"; |
| 47 | } |
| 48 | } |
| 49 | have_non_pic_oat_file_ = have_non_pic_oat_file_ || !oat_file->IsPic(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 50 | const OatFile* ret = oat_file.get(); |
| 51 | oat_files_.insert(std::move(oat_file)); |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | void OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) { |
| 56 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 57 | DCHECK(oat_file != nullptr); |
| 58 | std::unique_ptr<const OatFile> compare(oat_file); |
| 59 | auto it = oat_files_.find(compare); |
| 60 | CHECK(it != oat_files_.end()); |
| 61 | oat_files_.erase(it); |
| 62 | compare.release(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location) |
| 66 | const { |
| 67 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 68 | return FindOpenedOatFileFromOatLocationLocked(oat_location); |
| 69 | } |
| 70 | |
| 71 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked( |
| 72 | const std::string& oat_location) const { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 73 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 74 | if (oat_file->GetLocation() == oat_location) { |
| 75 | return oat_file.get(); |
| 76 | } |
| 77 | } |
| 78 | return nullptr; |
| 79 | } |
| 80 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 81 | std::vector<const OatFile*> OatFileManager::GetBootOatFiles() const { |
| 82 | std::vector<const OatFile*> oat_files; |
| 83 | std::vector<gc::space::ImageSpace*> image_spaces = |
| 84 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 85 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 86 | oat_files.push_back(image_space->GetOatFile()); |
| 87 | } |
| 88 | return oat_files; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | const OatFile* OatFileManager::GetPrimaryOatFile() const { |
| 92 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 93 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 94 | if (!boot_oat_files.empty()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 95 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 96 | if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) == |
| 97 | boot_oat_files.end()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 98 | return oat_file.get(); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | return nullptr; |
| 103 | } |
| 104 | |
| 105 | OatFileManager::~OatFileManager() { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 106 | // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for |
| 107 | // UnRegisterOatFileLocation. |
| 108 | oat_files_.clear(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 111 | std::vector<const OatFile*> OatFileManager::RegisterImageOatFiles( |
| 112 | std::vector<gc::space::ImageSpace*> spaces) { |
| 113 | std::vector<const OatFile*> oat_files; |
| 114 | for (gc::space::ImageSpace* space : spaces) { |
| 115 | oat_files.push_back(RegisterOatFile(space->ReleaseOatFile())); |
| 116 | } |
| 117 | return oat_files; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | class DexFileAndClassPair : ValueObject { |
| 121 | public: |
| 122 | DexFileAndClassPair(const DexFile* dex_file, size_t current_class_index, bool from_loaded_oat) |
| 123 | : cached_descriptor_(GetClassDescriptor(dex_file, current_class_index)), |
| 124 | dex_file_(dex_file), |
| 125 | current_class_index_(current_class_index), |
| 126 | from_loaded_oat_(from_loaded_oat) {} |
| 127 | |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 128 | DexFileAndClassPair(const DexFileAndClassPair& rhs) = default; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 129 | |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 130 | DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 131 | |
| 132 | const char* GetCachedDescriptor() const { |
| 133 | return cached_descriptor_; |
| 134 | } |
| 135 | |
| 136 | bool operator<(const DexFileAndClassPair& rhs) const { |
| 137 | const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_); |
| 138 | if (cmp != 0) { |
| 139 | // Note that the order must be reversed. We want to iterate over the classes in dex files. |
| 140 | // They are sorted lexicographically. Thus, the priority-queue must be a min-queue. |
| 141 | return cmp > 0; |
| 142 | } |
| 143 | return dex_file_ < rhs.dex_file_; |
| 144 | } |
| 145 | |
| 146 | bool DexFileHasMoreClasses() const { |
| 147 | return current_class_index_ + 1 < dex_file_->NumClassDefs(); |
| 148 | } |
| 149 | |
| 150 | void Next() { |
| 151 | ++current_class_index_; |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 152 | cached_descriptor_ = GetClassDescriptor(dex_file_.get(), current_class_index_); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | size_t GetCurrentClassIndex() const { |
| 156 | return current_class_index_; |
| 157 | } |
| 158 | |
| 159 | bool FromLoadedOat() const { |
| 160 | return from_loaded_oat_; |
| 161 | } |
| 162 | |
| 163 | const DexFile* GetDexFile() const { |
| 164 | return dex_file_.get(); |
| 165 | } |
| 166 | |
| 167 | private: |
| 168 | static const char* GetClassDescriptor(const DexFile* dex_file, size_t index) { |
| 169 | DCHECK(IsUint<16>(index)); |
| 170 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(static_cast<uint16_t>(index)); |
| 171 | return dex_file->StringByTypeIdx(class_def.class_idx_); |
| 172 | } |
| 173 | |
| 174 | const char* cached_descriptor_; |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 175 | std::shared_ptr<const DexFile> dex_file_; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 176 | size_t current_class_index_; |
| 177 | bool from_loaded_oat_; // We only need to compare mismatches between what we load now |
| 178 | // and what was loaded before. Any old duplicates must have been |
| 179 | // OK, and any new "internal" duplicates are as well (they must |
| 180 | // be from multidex, which resolves correctly). |
| 181 | }; |
| 182 | |
| 183 | static void AddDexFilesFromOat(const OatFile* oat_file, |
| 184 | bool already_loaded, |
| 185 | /*out*/std::priority_queue<DexFileAndClassPair>* heap) { |
| 186 | for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { |
| 187 | std::string error; |
| 188 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error); |
| 189 | if (dex_file == nullptr) { |
| 190 | LOG(WARNING) << "Could not create dex file from oat file: " << error; |
| 191 | } else if (dex_file->NumClassDefs() > 0U) { |
| 192 | heap->emplace(dex_file.release(), /*current_class_index*/0U, already_loaded); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static void AddNext(/*inout*/DexFileAndClassPair* original, |
| 198 | /*inout*/std::priority_queue<DexFileAndClassPair>* heap) { |
| 199 | if (original->DexFileHasMoreClasses()) { |
| 200 | original->Next(); |
| 201 | heap->push(std::move(*original)); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Check for class-def collisions in dex files. |
| 206 | // |
| 207 | // This works by maintaining a heap with one class from each dex file, sorted by the class |
| 208 | // descriptor. Then a dex-file/class pair is continually removed from the heap and compared |
| 209 | // against the following top element. If the descriptor is the same, it is now checked whether |
| 210 | // the two elements agree on whether their dex file was from an already-loaded oat-file or the |
| 211 | // new oat file. Any disagreement indicates a collision. |
| 212 | bool OatFileManager::HasCollisions(const OatFile* oat_file, |
| 213 | std::string* error_msg /*out*/) const { |
| 214 | DCHECK(oat_file != nullptr); |
| 215 | DCHECK(error_msg != nullptr); |
| 216 | if (!kDuplicateClassesCheck) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | // Dex files are registered late - once a class is actually being loaded. We have to compare |
| 221 | // against the open oat files. Take the oat_file_manager_lock_ that protects oat_files_ accesses. |
| 222 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 223 | |
| 224 | std::priority_queue<DexFileAndClassPair> queue; |
| 225 | |
| 226 | // Add dex files from already loaded oat files, but skip boot. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 227 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 228 | // The same OatFile can be loaded multiple times at different addresses. In this case, we don't |
| 229 | // need to check both against each other since they would have resolved the same way at compile |
| 230 | // time. |
| 231 | std::unordered_set<std::string> unique_locations; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 232 | for (const std::unique_ptr<const OatFile>& loaded_oat_file : oat_files_) { |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 233 | DCHECK_NE(loaded_oat_file.get(), oat_file); |
| 234 | const std::string& location = loaded_oat_file->GetLocation(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 235 | if (std::find(boot_oat_files.begin(), boot_oat_files.end(), loaded_oat_file.get()) == |
| 236 | boot_oat_files.end() && location != oat_file->GetLocation() && |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 237 | unique_locations.find(location) == unique_locations.end()) { |
| 238 | unique_locations.insert(location); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 239 | AddDexFilesFromOat(loaded_oat_file.get(), /*already_loaded*/true, &queue); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (queue.empty()) { |
| 244 | // No other oat files, return early. |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | // Add dex files from the oat file to check. |
| 249 | AddDexFilesFromOat(oat_file, /*already_loaded*/false, &queue); |
| 250 | |
| 251 | // Now drain the queue. |
| 252 | while (!queue.empty()) { |
| 253 | // Modifying the top element is only safe if we pop right after. |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 254 | DexFileAndClassPair compare_pop(queue.top()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 255 | queue.pop(); |
| 256 | |
| 257 | // Compare against the following elements. |
| 258 | while (!queue.empty()) { |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 259 | DexFileAndClassPair top(queue.top()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 260 | |
| 261 | if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) { |
| 262 | // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files. |
| 263 | if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) { |
| 264 | *error_msg = |
| 265 | StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s", |
| 266 | compare_pop.GetCachedDescriptor(), |
| 267 | compare_pop.GetDexFile()->GetLocation().c_str(), |
| 268 | top.GetDexFile()->GetLocation().c_str()); |
| 269 | return true; |
| 270 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 271 | queue.pop(); |
| 272 | AddNext(&top, &queue); |
| 273 | } else { |
| 274 | // Something else. Done here. |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | AddNext(&compare_pop, &queue); |
| 279 | } |
| 280 | |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat( |
| 285 | const char* dex_location, |
| 286 | const char* oat_location, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 287 | const OatFile** out_oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 288 | std::vector<std::string>* error_msgs) { |
| 289 | CHECK(dex_location != nullptr); |
| 290 | CHECK(error_msgs != nullptr); |
| 291 | |
| 292 | // Verify we aren't holding the mutator lock, which could starve GC if we |
| 293 | // have to generate or relocate an oat file. |
| 294 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); |
| 295 | |
| 296 | OatFileAssistant oat_file_assistant(dex_location, |
| 297 | oat_location, |
| 298 | kRuntimeISA, |
| 299 | !Runtime::Current()->IsAotCompiler()); |
| 300 | |
| 301 | // Lock the target oat location to avoid races generating and loading the |
| 302 | // oat file. |
| 303 | std::string error_msg; |
| 304 | if (!oat_file_assistant.Lock(/*out*/&error_msg)) { |
| 305 | // Don't worry too much if this fails. If it does fail, it's unlikely we |
| 306 | // can generate an oat file anyway. |
| 307 | VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg; |
| 308 | } |
| 309 | |
| 310 | const OatFile* source_oat_file = nullptr; |
| 311 | |
Nicolas Geoffray | e722d29 | 2015-12-15 11:51:37 +0000 | [diff] [blame] | 312 | // Update the oat file on disk if we can. This may fail, but that's okay. |
| 313 | // Best effort is all that matters here. |
| 314 | if (!oat_file_assistant.MakeUpToDate(/*out*/&error_msg)) { |
| 315 | LOG(WARNING) << error_msg; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // Get the oat file on disk. |
| 319 | std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release()); |
| 320 | if (oat_file != nullptr) { |
| 321 | // Take the file only if it has no collisions, or we must take it because of preopting. |
| 322 | bool accept_oat_file = !HasCollisions(oat_file.get(), /*out*/ &error_msg); |
| 323 | if (!accept_oat_file) { |
| 324 | // Failed the collision check. Print warning. |
| 325 | if (Runtime::Current()->IsDexFileFallbackEnabled()) { |
| 326 | LOG(WARNING) << "Found duplicate classes, falling back to interpreter mode for " |
| 327 | << dex_location; |
| 328 | } else { |
| 329 | LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to " |
| 330 | " load classes for " << dex_location; |
| 331 | } |
| 332 | LOG(WARNING) << error_msg; |
| 333 | |
| 334 | // However, if the app was part of /system and preopted, there is no original dex file |
| 335 | // available. In that case grudgingly accept the oat file. |
| 336 | if (!DexFile::MaybeDex(dex_location)) { |
| 337 | accept_oat_file = true; |
| 338 | LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. " |
| 339 | << "Allow oat file use. This is potentially dangerous."; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | if (accept_oat_file) { |
| 344 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 345 | source_oat_file = RegisterOatFile(std::move(oat_file)); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 346 | *out_oat_file = source_oat_file; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 351 | |
| 352 | // Load the dex files from the oat file. |
| 353 | if (source_oat_file != nullptr) { |
| 354 | dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location); |
| 355 | if (dex_files.empty()) { |
| 356 | error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation()); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | // Fall back to running out of the original dex file if we couldn't load any |
| 361 | // dex_files from the oat file. |
| 362 | if (dex_files.empty()) { |
| 363 | if (oat_file_assistant.HasOriginalDexFiles()) { |
| 364 | if (Runtime::Current()->IsDexFileFallbackEnabled()) { |
| 365 | if (!DexFile::Open(dex_location, dex_location, /*out*/ &error_msg, &dex_files)) { |
| 366 | LOG(WARNING) << error_msg; |
| 367 | error_msgs->push_back("Failed to open dex files from " + std::string(dex_location)); |
| 368 | } |
| 369 | } else { |
| 370 | error_msgs->push_back("Fallback mode disabled, skipping dex files."); |
| 371 | } |
| 372 | } else { |
| 373 | error_msgs->push_back("No original dex files found for dex location " |
| 374 | + std::string(dex_location)); |
| 375 | } |
| 376 | } |
| 377 | return dex_files; |
| 378 | } |
| 379 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 380 | bool OatFileManager::RegisterOatFileLocation(const std::string& oat_location) { |
| 381 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_count_lock_); |
| 382 | auto it = oat_file_count_.find(oat_location); |
| 383 | if (it != oat_file_count_.end()) { |
| 384 | ++it->second; |
| 385 | return false; |
| 386 | } |
| 387 | oat_file_count_.insert(std::pair<std::string, size_t>(oat_location, 1u)); |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | void OatFileManager::UnRegisterOatFileLocation(const std::string& oat_location) { |
| 392 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_count_lock_); |
| 393 | auto it = oat_file_count_.find(oat_location); |
| 394 | if (it != oat_file_count_.end()) { |
| 395 | --it->second; |
| 396 | if (it->second == 0) { |
| 397 | oat_file_count_.erase(it); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 402 | } // namespace art |