* src/native/native.hpp (NativeAgent, NativeAgents): Added new classes for
[cacao.git] / src / native / native.hpp
1 /* src/native/native.hpp - native library support
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
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
29
30 #include <stdint.h>
31
32 #ifdef __cplusplus
33 #include <map>
34 #include <set>
35 #include <vector>
36 #endif
37
38 #include "native/jni.hpp"
39
40 #include "vm/class.hpp"
41 #include "vm/global.h"
42 #include "vm/loader.hpp"
43 #include "vm/method.hpp"
44 #include "vm/os.hpp"
45 #include "vm/utf8.h"
46
47
48 /* defines ********************************************************************/
49
50 #define NATIVE_METHODS_COUNT    sizeof(methods) / sizeof(JNINativeMethod)
51
52
53 #define NATIVE_LIBRARY_PREFIX     "lib"
54
55 #if defined(__DARWIN__)
56 # define NATIVE_LIBRARY_SUFFIX    ".dylib"
57 #else
58 # define NATIVE_LIBRARY_SUFFIX    ".so"
59 #endif
60
61
62 #ifdef __cplusplus
63
64 #if defined(ENABLE_DL)
65 /**
66  * Represents a native library.
67  */
68 class NativeLibrary {
69 private:
70         utf*           _filename;    ///< Name of the native library.
71         classloader_t* _classloader; ///< Defining classloader.
72         void*          _handle;      ///< Filesystem handle.
73
74 public:
75         NativeLibrary(utf* filename, classloader_t* classloader = 0, void* handle = 0) : _filename(filename), _classloader(classloader), _handle(handle) {}
76         NativeLibrary(void* handle) : _filename(0), _classloader(0), _handle(handle) {}
77
78         inline classloader_t* get_classloader() const { return _classloader; }
79         inline utf*           get_filename   () const { return _filename; }
80         inline void*          get_handle     () const { return _handle; }
81
82         void* open();
83         void  close();
84         bool  load(JNIEnv* env);
85         bool  is_loaded();
86         void* resolve_symbol(utf* symbolname) const;
87 };
88
89
90 /**
91  * Table containing all loaded native libraries.
92  */
93 class NativeLibraries {
94 private:
95         Mutex _mutex; ///< Mutex to make the container thread-safe.
96         typedef std::multimap<classloader_t*, NativeLibrary> MAP;
97         MAP _libraries;
98
99 private:
100         // Comparator class.
101         class comparator : public std::binary_function<std::pair<classloader_t*, NativeLibrary>, utf*, bool> {
102         public:
103                 bool operator() (std::pair<classloader_t*, NativeLibrary> args, const utf* filename) const
104                 {
105                         return (args.second.get_filename() == filename);
106                 }
107         };
108
109 public:
110         void  add(NativeLibrary& library);
111         bool  is_loaded(NativeLibrary& library);
112         void* resolve_symbol(utf* symbolname, classloader_t* classloader);
113 };
114 #endif /* defined(ENABLE_DL) */
115
116
117 /**
118  * Represents a native method.
119  */
120 class NativeMethod {
121 private:
122         utf*  _classname;  ///< Class name.
123         utf*  _name;       ///< Method name.
124         utf*  _descriptor; ///< Method signature.
125         void* _function;   ///< Pointer to the native function.
126
127         friend bool operator< (const NativeMethod& first, const NativeMethod& second);
128
129 public:
130         NativeMethod(utf* classname, utf* name, utf* signature, void* function) : _classname(classname), _name(name), _descriptor(signature), _function(function) {}
131         NativeMethod(methodinfo* m) : _classname(m->clazz->name), _name(m->name), _descriptor(m->descriptor), _function(0) {}
132
133         inline void* get_function() const { return _function; }
134 };
135
136
137 /**
138  * Table containing all native methods registered with the VM.
139  */
140 class NativeMethods {
141 private:
142         Mutex _mutex;
143         std::set<NativeMethod> _methods;
144
145 private:
146         // Comparator class.
147         class comparator : public std::binary_function<std::pair<classloader_t*, NativeLibrary>, utf*, bool> {
148         public:
149                 bool operator() (std::pair<classloader_t*, NativeLibrary> args, const utf* filename) const
150                 {
151                         return (args.second.get_filename() == filename);
152                 }
153         };
154         
155 public:
156         void  register_methods(utf* classname, const JNINativeMethod* methods, size_t count);
157         void* resolve_method(methodinfo* m);
158         void* find_registered_method(methodinfo* m);
159 };
160
161
162 #if defined(ENABLE_JVMTI)
163 /**
164  * Represents a registered native agent.
165  */
166 class NativeAgent {
167 private:
168         char* _library;
169         char* _options;
170
171 public:
172         NativeAgent(char* library, char* options) : _library(library), _options(options) {}
173
174         char* get_library() const { return _library; }
175         char* get_options() const { return _options; }
176 };
177
178
179 /**
180  * Table containing all native agent libraries.
181  */
182 class NativeAgents {
183 private:
184         std::vector<NativeAgent> _agents;
185
186 public:
187         void register_agent_library(char* library, char* options);
188         void register_agent_path(char* path, char* options);
189         bool load_agents();
190         bool unload_agents();
191 };
192 #endif /* defined(ENABLE_JVMTI) */
193
194 #endif
195
196 /* function prototypes ********************************************************/
197
198 #ifdef __cplusplus
199 extern "C" {
200 #endif
201
202 java_handle_t *native_new_and_init(classinfo *c);
203 java_handle_t *native_new_and_init_string(classinfo *c, java_handle_t *s);
204
205 #ifdef __cplusplus
206 } // extern "C"
207 #endif
208
209 #endif // _NATIVE_HPP
210
211
212 /*
213  * These are local overrides for various environment variables in Emacs.
214  * Please do not remove this and leave it at the end of the file, where
215  * Emacs will automagically detect them.
216  * ---------------------------------------------------------------------
217  * Local variables:
218  * mode: c++
219  * indent-tabs-mode: t
220  * c-basic-offset: 4
221  * tab-width: 4
222  * End:
223  * vim:noexpandtab:sw=4:ts=4:
224  */