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