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 | #ifndef ART_RUNTIME_OAT_FILE_MANAGER_H_ |
| 18 | #define ART_RUNTIME_OAT_FILE_MANAGER_H_ |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "base/macros.h" |
| 25 | #include "base/mutex.h" |
| 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | namespace gc { |
| 30 | namespace space { |
| 31 | class ImageSpace; |
| 32 | } // namespace space |
| 33 | } // namespace gc |
| 34 | |
| 35 | class DexFile; |
| 36 | class OatFile; |
| 37 | |
| 38 | // Class for dealing with oat file management. |
| 39 | // |
| 40 | // This class knows about all the loaded oat files and provides utility functions. The oat file |
| 41 | // pointers returned from functions are always valid. |
| 42 | class OatFileManager { |
| 43 | public: |
| 44 | OatFileManager() : have_non_pic_oat_file_(false) {} |
| 45 | ~OatFileManager(); |
| 46 | |
| 47 | // Add an oat file to the internal accounting, std::aborts if there already exists an oat file |
| 48 | // with the same base address. Returns the oat file pointer from oat_file. |
| 49 | const OatFile* RegisterOatFile(std::unique_ptr<const OatFile> oat_file) |
| 50 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 51 | |
| 52 | // Find the first opened oat file with the same location, returns null if there are none. |
| 53 | const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) const |
| 54 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 55 | |
| 56 | // Returns true if we have a non pic oat file. |
| 57 | bool HaveNonPicOatFile() const { |
| 58 | return have_non_pic_oat_file_; |
| 59 | } |
| 60 | |
| 61 | // Returns the boot image oat file. |
| 62 | const OatFile* GetBootOatFile() const; |
| 63 | |
| 64 | // Returns the first non-image oat file in the class path. |
| 65 | const OatFile* GetPrimaryOatFile() const REQUIRES(!Locks::oat_file_manager_lock_); |
| 66 | |
| 67 | // Return the oat file for an image, registers the oat file. Takes ownership of the imagespace's |
| 68 | // underlying oat file. |
| 69 | const OatFile* RegisterImageOatFile(gc::space::ImageSpace* space) |
| 70 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 71 | |
| 72 | // Finds or creates the oat file holding dex_location. Then loads and returns |
| 73 | // all corresponding dex files (there may be more than one dex file loaded |
| 74 | // in the case of multidex). |
| 75 | // This may return the original, unquickened dex files if the oat file could |
| 76 | // not be generated. |
| 77 | // |
| 78 | // Returns an empty vector if the dex files could not be loaded. In this |
| 79 | // case, there will be at least one error message returned describing why no |
| 80 | // dex files could not be loaded. The 'error_msgs' argument must not be |
| 81 | // null, regardless of whether there is an error or not. |
| 82 | // |
| 83 | // This method should not be called with the mutator_lock_ held, because it |
| 84 | // could end up starving GC if we need to generate or relocate any oat |
| 85 | // files. |
| 86 | std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat( |
| 87 | const char* dex_location, |
| 88 | const char* oat_location, |
| 89 | /*out*/std::vector<std::string>* error_msgs) |
| 90 | REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_); |
| 91 | |
| 92 | private: |
| 93 | // Check for duplicate class definitions of the given oat file against all open oat files. |
| 94 | // Return true if there are any class definition collisions in the oat_file. |
| 95 | bool HasCollisions(const OatFile* oat_file, /*out*/std::string* error_msg) const |
| 96 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 97 | |
| 98 | std::vector<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_); |
| 99 | bool have_non_pic_oat_file_; |
| 100 | DISALLOW_COPY_AND_ASSIGN(OatFileManager); |
| 101 | }; |
| 102 | |
| 103 | } // namespace art |
| 104 | |
| 105 | #endif // ART_RUNTIME_OAT_FILE_MANAGER_H_ |