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_PLUGIN_H_ 18 #define ART_RUNTIME_PLUGIN_H_ 19 20 #include <string> 21 #include "base/logging.h" 22 23 namespace art { 24 25 // This function is loaded from the plugin (if present) and called during runtime initialization. 26 // By the time this has been called the runtime has been fully initialized but not other native 27 // libraries have been loaded yet. Failure to initialize is considered a fatal error. 28 // TODO might want to give initialization function some arguments 29 using PluginInitializationFunction = bool (*)(); 30 using PluginDeinitializationFunction = bool (*)(); 31 32 // A class encapsulating a plugin. There is no stable plugin ABI or API and likely never will be. 33 // TODO Might want to put some locking in this but ATM we only load these at initialization in a 34 // single-threaded fashion so not much need 35 class Plugin { 36 public: Create(const std::string & lib)37 static Plugin Create(const std::string& lib) { 38 return Plugin(lib); 39 } 40 IsLoaded()41 bool IsLoaded() const { 42 return dlopen_handle_ != nullptr; 43 } 44 GetLibrary()45 const std::string& GetLibrary() const { 46 return library_; 47 } 48 49 bool Load(/*out*/std::string* error_msg); 50 bool Unload(); 51 52 ~Plugin()53 ~Plugin() { 54 if (IsLoaded() && !Unload()) { 55 LOG(ERROR) << "Error unloading " << this; 56 } 57 } 58 59 Plugin(const Plugin& other); 60 61 // Create move constructor for putting this in a list Plugin(Plugin && other)62 Plugin(Plugin&& other) 63 : library_(other.library_), 64 dlopen_handle_(other.dlopen_handle_) { 65 other.dlopen_handle_ = nullptr; 66 } 67 68 private: Plugin(const std::string & library)69 explicit Plugin(const std::string& library) : library_(library), dlopen_handle_(nullptr) { } 70 71 std::string library_; 72 void* dlopen_handle_; 73 74 friend std::ostream& operator<<(std::ostream &os, Plugin const& m); 75 }; 76 77 std::ostream& operator<<(std::ostream &os, Plugin const& m); 78 std::ostream& operator<<(std::ostream &os, const Plugin* m); 79 80 } // namespace art 81 82 #endif // ART_RUNTIME_PLUGIN_H_ 83