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
37 #include "native/jni.hpp"
39 #include "vm/class.hpp"
40 #include "vm/global.h"
41 #include "vm/loader.hpp"
42 #include "vm/method.h"
47 /* defines ********************************************************************/
49 #define NATIVE_METHODS_COUNT sizeof(methods) / sizeof(JNINativeMethod)
52 #define NATIVE_LIBRARY_PREFIX "lib"
54 #if defined(__DARWIN__)
55 # define NATIVE_LIBRARY_SUFFIX ".dylib"
57 # define NATIVE_LIBRARY_SUFFIX ".so"
63 #if defined(ENABLE_DL)
65 * Represents a native library.
69 utf* _filename; ///< Name of the native library.
70 classloader_t* _classloader; ///< Defining classloader.
71 void* _handle; ///< Filesystem handle.
74 NativeLibrary(utf* filename, classloader_t* classloader = 0, void* handle = 0) : _filename(filename), _classloader(classloader), _handle(handle) {}
75 NativeLibrary(void* handle) : _filename(0), _classloader(0), _handle(handle) {}
77 inline classloader_t* get_classloader() const { return _classloader; }
78 inline utf* get_filename () const { return _filename; }
79 inline void* get_handle () const { return _handle; }
83 bool load(JNIEnv* env);
85 void* resolve_symbol(utf* symbolname) const;
90 * Table containing all loaded native libraries.
92 class NativeLibraries {
94 Mutex _mutex; ///< Mutex to make the container thread-safe.
95 typedef std::multimap<classloader_t*, NativeLibrary> MAP;
100 class comparator : public std::binary_function<std::pair<classloader_t*, NativeLibrary>, utf*, bool> {
102 bool operator() (std::pair<classloader_t*, NativeLibrary> args, const utf* filename) const
104 return (args.second.get_filename() == filename);
109 void add(NativeLibrary& library);
110 bool is_loaded(NativeLibrary& library);
111 void* resolve_symbol(utf* symbolname, classloader_t* classloader);
117 * Represents a native method.
121 utf* _classname; ///< Class name.
122 utf* _name; ///< Method name.
123 utf* _descriptor; ///< Method signature.
124 void* _function; ///< Pointer to the native function.
126 friend bool operator< (const NativeMethod& first, const NativeMethod& second);
129 NativeMethod(utf* classname, utf* name, utf* signature, void* function) : _classname(classname), _name(name), _descriptor(signature), _function(function) {}
130 NativeMethod(methodinfo* m) : _classname(m->clazz->name), _name(m->name), _descriptor(m->descriptor), _function(0) {}
132 inline void* get_function() const { return _function; }
137 * Table containing all native methods registered with the VM.
139 class NativeMethods {
142 std::set<NativeMethod> _methods;
146 class comparator : public std::binary_function<std::pair<classloader_t*, NativeLibrary>, utf*, bool> {
148 bool operator() (std::pair<classloader_t*, NativeLibrary> args, const utf* filename) const
150 return (args.second.get_filename() == filename);
155 void register_methods(utf* classname, const JNINativeMethod* methods, size_t count);
156 void* resolve_method(methodinfo* m);
157 void* find_registered_method(methodinfo* m);
162 /* function prototypes ********************************************************/
168 java_handle_t *native_new_and_init(classinfo *c);
169 java_handle_t *native_new_and_init_string(classinfo *c, java_handle_t *s);
175 #endif // _NATIVE_HPP
179 * These are local overrides for various environment variables in Emacs.
180 * Please do not remove this and leave it at the end of the file, where
181 * Emacs will automagically detect them.
182 * ---------------------------------------------------------------------
185 * indent-tabs-mode: t
189 * vim:noexpandtab:sw=4:ts=4: