Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #ifndef ART_RUNTIME_DEX_CACHE_RESOLVED_CLASSES_H_ |
| 18 | #define ART_RUNTIME_DEX_CACHE_RESOLVED_CLASSES_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | #include <unordered_set> |
| 22 | #include <vector> |
| 23 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 24 | #include "dex_file_types.h" |
| 25 | |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 26 | namespace art { |
| 27 | |
| 28 | // Data structure for passing around which classes belonging to a dex cache / dex file are resolved. |
| 29 | class DexCacheResolvedClasses { |
| 30 | public: |
Mathieu Chartier | 9275af6 | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 31 | DexCacheResolvedClasses(const std::string& dex_location, |
| 32 | const std::string& base_location, |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 33 | uint32_t location_checksum, |
| 34 | uint32_t num_method_ids) |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 35 | : dex_location_(dex_location), |
Mathieu Chartier | 9275af6 | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 36 | base_location_(base_location), |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 37 | location_checksum_(location_checksum), |
| 38 | num_method_ids_(num_method_ids) {} |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 39 | |
| 40 | // Only compare the key elements, ignore the resolved classes. |
| 41 | int Compare(const DexCacheResolvedClasses& other) const { |
| 42 | if (location_checksum_ != other.location_checksum_) { |
| 43 | return static_cast<int>(location_checksum_ - other.location_checksum_); |
| 44 | } |
Mathieu Chartier | 9275af6 | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 45 | // Don't need to compare base_location_ since dex_location_ has more info. |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 46 | return dex_location_.compare(other.dex_location_); |
| 47 | } |
| 48 | |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 49 | bool AddClass(dex::TypeIndex index) const { |
| 50 | return classes_.insert(index).second; |
| 51 | } |
| 52 | |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 53 | template <class InputIt> |
| 54 | void AddClasses(InputIt begin, InputIt end) const { |
| 55 | classes_.insert(begin, end); |
| 56 | } |
| 57 | |
| 58 | const std::string& GetDexLocation() const { |
| 59 | return dex_location_; |
| 60 | } |
| 61 | |
Mathieu Chartier | 9275af6 | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 62 | const std::string& GetBaseLocation() const { |
| 63 | return base_location_; |
| 64 | } |
| 65 | |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 66 | uint32_t GetLocationChecksum() const { |
| 67 | return location_checksum_; |
| 68 | } |
| 69 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 70 | const std::unordered_set<dex::TypeIndex>& GetClasses() const { |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 71 | return classes_; |
| 72 | } |
| 73 | |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 74 | size_t NumMethodIds() const { |
| 75 | return num_method_ids_; |
| 76 | } |
| 77 | |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 78 | private: |
| 79 | const std::string dex_location_; |
Mathieu Chartier | 9275af6 | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 80 | const std::string base_location_; |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 81 | const uint32_t location_checksum_; |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 82 | const uint32_t num_method_ids_; |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 83 | // Array of resolved class def indexes. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 84 | mutable std::unordered_set<dex::TypeIndex> classes_; |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | inline bool operator<(const DexCacheResolvedClasses& a, const DexCacheResolvedClasses& b) { |
| 88 | return a.Compare(b) < 0; |
| 89 | } |
| 90 | |
| 91 | } // namespace art |
| 92 | |
| 93 | #endif // ART_RUNTIME_DEX_CACHE_RESOLVED_CLASSES_H_ |