blob: f077aaf3fb012a7a358e3de7893344cfc77e1de4 [file] [log] [blame]
Alex Light185d1342016-08-11 10:48:03 -07001/*
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
23namespace 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
29using PluginInitializationFunction = bool (*)();
30using 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
35class Plugin {
36 public:
Andreas Gampeca620d72016-11-08 08:09:33 -080037 static Plugin Create(const std::string& lib) {
Alex Light185d1342016-08-11 10:48:03 -070038 return Plugin(lib);
39 }
40
41 bool IsLoaded() const {
42 return dlopen_handle_ != nullptr;
43 }
44
45 const std::string& GetLibrary() const {
46 return library_;
47 }
48
49 bool Load(/*out*/std::string* error_msg);
50 bool Unload();
51
52
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
62 Plugin(Plugin&& other)
63 : library_(other.library_),
64 dlopen_handle_(other.dlopen_handle_) {
65 other.dlopen_handle_ = nullptr;
66 }
67
68 private:
Andreas Gampeca620d72016-11-08 08:09:33 -080069 explicit Plugin(const std::string& library) : library_(library), dlopen_handle_(nullptr) { }
Alex Light185d1342016-08-11 10:48:03 -070070
71 std::string library_;
72 void* dlopen_handle_;
73
74 friend std::ostream& operator<<(std::ostream &os, Plugin const& m);
75};
76
77std::ostream& operator<<(std::ostream &os, Plugin const& m);
78std::ostream& operator<<(std::ostream &os, const Plugin* m);
79
80} // namespace art
81
82#endif // ART_RUNTIME_PLUGIN_H_