Line data Source code
1 : /* src/native/native.hpp - native library support
2 :
3 : Copyright (C) 1996-2013
4 : CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 : Copyright (C) 2008 Theobroma Systems Ltd.
6 :
7 : This file is part of CACAO.
8 :
9 : This program is free software; you can redistribute it and/or
10 : modify it under the terms of the GNU General Public License as
11 : published by the Free Software Foundation; either version 2, or (at
12 : your option) any later version.
13 :
14 : This program is distributed in the hope that it will be useful, but
15 : WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with this program; if not, write to the Free Software
21 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 : 02110-1301, USA.
23 :
24 : */
25 :
26 :
27 : #ifndef NATIVE_HPP_
28 : #define NATIVE_HPP_ 1
29 :
30 : #include <map>
31 : #include <set>
32 : #include <vector>
33 : #include "native/jni.hpp"
34 : #include "threads/mutex.hpp"
35 : #include "vm/class.hpp"
36 : #include "vm/global.hpp"
37 : #include "vm/loader.hpp"
38 : #include "vm/method.hpp"
39 : #include "vm/os.hpp"
40 : #include "vm/utf8.hpp"
41 :
42 :
43 : /* defines ********************************************************************/
44 :
45 : #define NATIVE_METHODS_COUNT sizeof(methods) / sizeof(JNINativeMethod)
46 :
47 :
48 : #define NATIVE_LIBRARY_PREFIX "lib"
49 :
50 : #if defined(__DARWIN__)
51 : # define NATIVE_LIBRARY_SUFFIX ".dylib"
52 : #else
53 : # define NATIVE_LIBRARY_SUFFIX ".so"
54 : #endif
55 :
56 : #if defined(ENABLE_DL)
57 : /**
58 : * Represents a native library.
59 : */
60 : class NativeLibrary {
61 : private:
62 : Utf8String _filename; ///< Name of the native library.
63 : classloader_t *_classloader; ///< Defining classloader.
64 : void *_handle; ///< Filesystem handle.
65 :
66 : public:
67 593 : NativeLibrary(Utf8String filename, classloader_t* classloader = 0, void* handle = 0) : _filename(filename), _classloader(classloader), _handle(handle) {}
68 : NativeLibrary(void* handle) : _filename(0), _classloader(0), _handle(handle) {}
69 :
70 1021 : inline classloader_t* get_classloader() const { return _classloader; }
71 1079 : inline Utf8String get_filename () const { return _filename; }
72 : inline void* get_handle () const { return _handle; }
73 :
74 : void* open();
75 : void close();
76 : bool load(JNIEnv* env);
77 : bool is_loaded();
78 : void* resolve_symbol(Utf8String symbolname) const;
79 : };
80 :
81 :
82 : /**
83 : * Table containing all loaded native libraries.
84 : */
85 165 : class NativeLibraries {
86 : private:
87 : Mutex _mutex; ///< Mutex to make the container thread-safe.
88 : typedef std::multimap<classloader_t*, NativeLibrary> MAP;
89 : MAP _libraries;
90 :
91 : private:
92 : // Comparator class.
93 : class comparator : public std::binary_function<std::pair<classloader_t*, NativeLibrary>, Utf8String, bool> {
94 : public:
95 633 : bool operator() (std::pair<classloader_t*, NativeLibrary> args, Utf8String filename) const
96 : {
97 633 : return (args.second.get_filename() == filename);
98 : }
99 : };
100 :
101 : public:
102 : void add(NativeLibrary& library);
103 : bool is_loaded(NativeLibrary& library);
104 : void* resolve_symbol(Utf8String symbolname, classloader_t* classloader);
105 : };
106 : #endif /* defined(ENABLE_DL) */
107 :
108 :
109 : /**
110 : * Represents a native method.
111 : */
112 0 : class NativeMethod {
113 : private:
114 : Utf8String _classname; ///< Class name.
115 : Utf8String _name; ///< Method name.
116 : Utf8String _descriptor; ///< Method signature.
117 : void* _function; ///< Pointer to the native function.
118 :
119 : friend bool operator< (const NativeMethod& first, const NativeMethod& second);
120 :
121 : public:
122 38794 : NativeMethod(Utf8String classname, Utf8String name, Utf8String signature, void* function) : _classname(classname), _name(name), _descriptor(signature), _function(function) {}
123 5832 : NativeMethod(methodinfo* m) : _classname(m->clazz->name), _name(m->name), _descriptor(m->descriptor), _function(0) {}
124 :
125 4073 : inline void* get_function() const { return _function; }
126 : };
127 :
128 :
129 : /**
130 : * Table containing all native methods registered with the VM.
131 : */
132 165 : class NativeMethods {
133 : private:
134 : Mutex _mutex;
135 : std::set<NativeMethod> _methods;
136 :
137 : private:
138 : // Comparator class.
139 : class comparator : public std::binary_function<std::pair<classloader_t*, NativeLibrary>, Utf8String, bool> {
140 : public:
141 : bool operator() (std::pair<classloader_t*, NativeLibrary> args, Utf8String filename) const
142 : {
143 : return (args.second.get_filename() == filename);
144 : }
145 : };
146 :
147 : public:
148 : void register_methods(Utf8String classname, const JNINativeMethod* methods, size_t count);
149 : void* resolve_method(methodinfo* m);
150 : void* find_registered_method(methodinfo* m);
151 : };
152 :
153 :
154 : #if defined(ENABLE_JVMTI)
155 : /**
156 : * Represents a registered native agent.
157 : */
158 : class NativeAgent {
159 : private:
160 : char* _library;
161 : char* _options;
162 :
163 : public:
164 : NativeAgent(char* library, char* options) : _library(library), _options(options) {}
165 :
166 : char* get_library() const { return _library; }
167 : char* get_options() const { return _options; }
168 : };
169 :
170 :
171 : /**
172 : * Table containing all native agent libraries.
173 : */
174 : class NativeAgents {
175 : private:
176 : std::vector<NativeAgent> _agents;
177 :
178 : public:
179 : void register_agent_library(char* library, char* options);
180 : void register_agent_path(char* path, char* options);
181 : bool load_agents();
182 : bool unload_agents();
183 : };
184 : #endif /* defined(ENABLE_JVMTI) */
185 :
186 : /* function prototypes ********************************************************/
187 :
188 : java_handle_t *native_new_and_init(classinfo *c);
189 : java_handle_t *native_new_and_init_string(classinfo *c, java_handle_t *s);
190 :
191 : #endif // NATIVE_HPP_
192 :
193 :
194 : /*
195 : * These are local overrides for various environment variables in Emacs.
196 : * Please do not remove this and leave it at the end of the file, where
197 : * Emacs will automagically detect them.
198 : * ---------------------------------------------------------------------
199 : * Local variables:
200 : * mode: c++
201 : * indent-tabs-mode: t
202 : * c-basic-offset: 4
203 : * tab-width: 4
204 : * End:
205 : * vim:noexpandtab:sw=4:ts=4:
206 : */
|