1 /* src/native/native.hpp - native library support
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.
7 This file is part of CACAO.
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.
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.
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
38 #include "native/jni.hpp"
40 #include "vm/class.hpp"
41 #include "vm/global.h"
42 #include "vm/loader.hpp"
43 #include "vm/method.hpp"
48 /* defines ********************************************************************/
50 #define NATIVE_METHODS_COUNT sizeof(methods) / sizeof(JNINativeMethod)
53 #define NATIVE_LIBRARY_PREFIX "lib"
55 #if defined(__DARWIN__)
56 # define NATIVE_LIBRARY_SUFFIX ".dylib"
58 # define NATIVE_LIBRARY_SUFFIX ".so"
64 #if defined(ENABLE_DL)
66 * Represents a native library.
70 utf* _filename; ///< Name of the native library.
71 classloader_t* _classloader; ///< Defining classloader.
72 void* _handle; ///< Filesystem handle.
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) {}
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; }
84 bool load(JNIEnv* env);
86 void* resolve_symbol(utf* symbolname) const;
91 * Table containing all loaded native libraries.
93 class NativeLibraries {
95 Mutex _mutex; ///< Mutex to make the container thread-safe.
96 typedef std::multimap<classloader_t*, NativeLibrary> MAP;
101 class comparator : public std::binary_function<std::pair<classloader_t*, NativeLibrary>, utf*, bool> {
103 bool operator() (std::pair<classloader_t*, NativeLibrary> args, const utf* filename) const
105 return (args.second.get_filename() == filename);
110 void add(NativeLibrary& library);
111 bool is_loaded(NativeLibrary& library);
112 void* resolve_symbol(utf* symbolname, classloader_t* classloader);
114 #endif /* defined(ENABLE_DL) */
118 * Represents a native method.
122 utf* _classname; ///< Class name.
123 utf* _name; ///< Method name.
124 utf* _descriptor; ///< Method signature.
125 void* _function; ///< Pointer to the native function.
127 friend bool operator< (const NativeMethod& first, const NativeMethod& second);
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) {}
133 inline void* get_function() const { return _function; }
138 * Table containing all native methods registered with the VM.
140 class NativeMethods {
143 std::set<NativeMethod> _methods;
147 class comparator : public std::binary_function<std::pair<classloader_t*, NativeLibrary>, utf*, bool> {
149 bool operator() (std::pair<classloader_t*, NativeLibrary> args, const utf* filename) const
151 return (args.second.get_filename() == filename);
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);
162 #if defined(ENABLE_JVMTI)
164 * Represents a registered native agent.
172 NativeAgent(char* library, char* options) : _library(library), _options(options) {}
174 char* get_library() const { return _library; }
175 char* get_options() const { return _options; }
180 * Table containing all native agent libraries.
184 std::vector<NativeAgent> _agents;
187 void register_agent_library(char* library, char* options);
188 void register_agent_path(char* path, char* options);
190 bool unload_agents();
192 #endif /* defined(ENABLE_JVMTI) */
196 /* function prototypes ********************************************************/
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);
209 #endif // _NATIVE_HPP
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 * ---------------------------------------------------------------------
219 * indent-tabs-mode: t
223 * vim:noexpandtab:sw=4:ts=4: