* src/native/native.hpp: Likewise.
* src/native/jni.cpp,
src/native/vm/cldc1.1/com_sun_cldc_io_ResourceInputStream.cpp,
src/native/vm/cldc1.1/com_sun_cldc_io_j2me_socket_Protocol.cpp,
src/native/vm/cldc1.1/com_sun_cldchi_io_ConsoleOutputStream.cpp,
src/native/vm/cldc1.1/com_sun_cldchi_jvm_JVM.cpp,
src/native/vm/cldc1.1/java_lang_Class.cpp,
src/native/vm/cldc1.1/java_lang_Double.cpp,
src/native/vm/cldc1.1/java_lang_Float.cpp,
src/native/vm/cldc1.1/java_lang_Math.cpp,
src/native/vm/cldc1.1/java_lang_Object.cpp,
src/native/vm/cldc1.1/java_lang_Runtime.cpp,
src/native/vm/cldc1.1/java_lang_String.cpp,
src/native/vm/cldc1.1/java_lang_System.cpp,
src/native/vm/cldc1.1/java_lang_Thread.cpp,
src/native/vm/cldc1.1/java_lang_Throwable.cpp,
src/native/vm/gnuclasspath/gnu_classpath_VMStackWalker.cpp,
src/native/vm/gnuclasspath/gnu_classpath_VMSystemProperties.cpp,
src/native/vm/gnuclasspath/gnu_java_lang_VMCPStringBuilder.cpp,
src/native/vm/gnuclasspath/gnu_java_lang_management_VMClassLoadingMXBeanImpl.cpp,
src/native/vm/gnuclasspath/gnu_java_lang_management_VMMemoryMXBeanImpl.cpp,
src/native/vm/gnuclasspath/gnu_java_lang_management_VMRuntimeMXBeanImpl.cpp,
src/native/vm/gnuclasspath/gnu_java_lang_management_VMThreadMXBeanImpl.cpp,
src/native/vm/gnuclasspath/java_lang_VMClass.cpp,
src/native/vm/gnuclasspath/java_lang_VMClassLoader.cpp,
src/native/vm/gnuclasspath/java_lang_VMObject.cpp,
src/native/vm/gnuclasspath/java_lang_VMRuntime.cpp,
src/native/vm/gnuclasspath/java_lang_VMString.cpp,
src/native/vm/gnuclasspath/java_lang_VMSystem.cpp,
src/native/vm/gnuclasspath/java_lang_VMThread.cpp,
src/native/vm/gnuclasspath/java_lang_VMThrowable.cpp,
src/native/vm/gnuclasspath/java_lang_management_VMManagementFactory.cpp,
src/native/vm/gnuclasspath/java_lang_reflect_VMConstructor.cpp,
src/native/vm/gnuclasspath/java_lang_reflect_VMField.cpp,
src/native/vm/gnuclasspath/java_lang_reflect_VMMethod.cpp,
src/native/vm/gnuclasspath/java_security_VMAccessController.cpp,
src/native/vm/gnuclasspath/java_util_concurrent_atomic_AtomicLong.cpp,
src/native/vm/gnuclasspath/sun_reflect_ConstantPool.cpp,
src/native/vm/nativevm.cpp,
src/native/vm/openjdk/hpi.cpp,
src/native/vm/openjdk/jvm.cpp,
src/native/vm/sun_misc_Unsafe.cpp,
src/vm/jit/jit.cpp,
src/vm/jit/patcher-common.cpp,
src/vm/vm.cpp,
src/vm/vm.hpp: Related changes.
*******************************************************************************/
-jint _Jv_JNI_RegisterNatives(JNIEnv *env, jclass clazz,
- const JNINativeMethod *methods, jint nMethods)
+jint jni_RegisterNatives(JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods)
{
- classinfo *c;
-
- STATISTICS(jniinvokation());
+ TRACEJNICALLS(("jni_RegisterNatives(env=%p, clazz=%p, methods=%p, nMethods=%d)", env, clazz, methods, nMethods));
- c = LLNI_classinfo_unwrap(clazz);
+ classinfo* c = LLNI_classinfo_unwrap(clazz);
/* XXX: if implemented this needs a call to jvmti_NativeMethodBind
if (jvmti) jvmti_NativeMethodBind(method, address, new_address_ptr);
*/
- native_method_register(c->name, methods, nMethods);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(c->name, methods, nMethods);
return 0;
}
_Jv_JNI_SetFloatArrayRegion,
_Jv_JNI_SetDoubleArrayRegion,
- _Jv_JNI_RegisterNatives,
+ jni_RegisterNatives,
_Jv_JNI_UnregisterNatives,
_Jv_JNI_MonitorEnter,
#include <stdint.h>
-#include "vm/types.h"
+#include <algorithm>
+#include <functional>
+#include <map>
#include "mm/memory.h"
#include "threads/mutex.hpp"
-#include "toolbox/avl.h"
-#include "toolbox/hashtable.h"
#include "toolbox/logging.h"
#include "vm/jit/builtin.hpp"
#endif
-/* global variables ***********************************************************/
-
-static avl_tree_t *tree_native_methods;
-
-#if defined(ENABLE_DL)
-static hashtable *hashtable_library;
-#endif
-
-
-/* prototypes *****************************************************************/
-
-static s4 native_tree_native_methods_comparator(const void *treenode, const void *node);
-
-
-/* native_init *****************************************************************
-
- Initializes the native subsystem.
-
-*******************************************************************************/
-
-bool native_init(void)
-{
- TRACESUBSYSTEMINITIALIZATION("native_init");
-
-#if defined(ENABLE_DL)
- /* initialize library hashtable, 10 entries should be enough */
-
- hashtable_library = NEW(hashtable);
-
- hashtable_create(hashtable_library, 10);
-#endif
-
- /* initialize the native methods table */
-
- tree_native_methods = avl_create(&native_tree_native_methods_comparator);
-
- /* everything's ok */
-
- return true;
-}
-
-
-/* native_tree_native_methods_comparator ***************************************
-
- Comparison function for AVL tree of native methods.
-
- IN:
- treenode....node in the tree
- node........node to compare with tree-node
-
- RETURN VALUE:
- -1, 0, +1
-
-*******************************************************************************/
-
-static s4 native_tree_native_methods_comparator(const void *treenode, const void *node)
-{
- const native_methods_node_t *treenmn;
- const native_methods_node_t *nmn;
-
- treenmn = (native_methods_node_t*) treenode;
- nmn = (native_methods_node_t*) node;
-
- /* these are for walking the tree */
-
- if (treenmn->classname < nmn->classname)
- return -1;
- else if (treenmn->classname > nmn->classname)
- return 1;
-
- if (treenmn->name < nmn->name)
- return -1;
- else if (treenmn->name > nmn->name)
- return 1;
-
- if (treenmn->descriptor < nmn->descriptor)
- return -1;
- else if (treenmn->descriptor > nmn->descriptor)
- return 1;
-
- /* all pointers are equal, we have found the entry */
-
- return 0;
-}
-
-
/* native_make_overloaded_function *********************************************
XXX
}
-/* native_method_register ******************************************************
-
- Register a native method in the native method table.
-
-*******************************************************************************/
-
-void native_method_register(utf *classname, const JNINativeMethod *methods,
- int32_t count)
+bool operator< (const NativeMethod& first, const NativeMethod& second)
{
- native_methods_node_t *nmn;
- utf *name;
- utf *descriptor;
- int32_t i;
+ if (first._classname < second._classname)
+ return true;
+ else if (first._classname > second._classname)
+ return false;
+
+ if (first._name < second._name)
+ return true;
+ else if (first._name > second._name)
+ return false;
+
+ if (first._descriptor < second._descriptor)
+ return true;
+ else if (first._descriptor > second._descriptor)
+ return false;
+
+ // All pointers are equal, we have found the entry.
+ return false;
+}
- /* insert all methods passed */
- for (i = 0; i < count; i++) {
+/**
+ * Register native methods with the VM. This is done by inserting
+ * them into the native method table.
+ *
+ * @param classname
+ * @param methods Native methods array.
+ * @param count Number of methods in the array.
+ */
+void NativeMethods::register_methods(utf* classname, const JNINativeMethod* methods, size_t count)
+{
+ // Insert all methods passed */
+ for (size_t i = 0; i < count; i++) {
if (opt_verbosejni) {
printf("[Registering JNI native method ");
utf_display_printable_ascii_classname(classname);
printf(".%s]\n", methods[i].name);
}
- /* generate the utf8 names */
-
- name = utf_new_char(methods[i].name);
- descriptor = utf_new_char(methods[i].signature);
-
- /* allocate a new tree node */
+ // Generate the UTF8 names.
+ utf* name = utf_new_char(methods[i].name);
+ utf* signature = utf_new_char(methods[i].signature);
- nmn = NEW(native_methods_node_t);
+ NativeMethod nm(classname, name, signature, methods[i].fnPtr);
- nmn->classname = classname;
- nmn->name = name;
- nmn->descriptor = descriptor;
- nmn->function = (functionptr) (ptrint) methods[i].fnPtr;
-
- /* insert the method into the tree */
-
- avl_insert(tree_native_methods, nmn);
+ // Insert the method into the table.
+ _methods.insert(nm);
}
}
-/* native_method_find **********************************************************
-
- Find a native method in the native method table.
-
-*******************************************************************************/
-
-static functionptr native_method_find(methodinfo *m)
-{
- native_methods_node_t tmpnmn;
- native_methods_node_t *nmn;
-
- /* fill the temporary structure used for searching the tree */
-
- tmpnmn.classname = m->clazz->name;
- tmpnmn.name = m->name;
- tmpnmn.descriptor = m->descriptor;
-
- /* find the entry in the AVL-tree */
-
- nmn = (native_methods_node_t*) avl_find(tree_native_methods, &tmpnmn);
-
- if (nmn == NULL)
- return NULL;
-
- return nmn->function;
-}
-
-
-/* native_method_resolve *******************************************************
-
- Resolves a native method, maybe from a dynamic library.
-
- IN:
- m ... methodinfo of the native Java method to resolve
-
- RESULT:
- pointer to the resolved method (symbol)
-
-*******************************************************************************/
-
-functionptr native_method_resolve(methodinfo *m)
+/**
+ * Resolves a native method, maybe from a dynamic library.
+ *
+ * @param m Method structure of the native Java method to resolve.
+ *
+ * @return Pointer to the resolved method (symbol).
+ */
+void* NativeMethods::resolve_method(methodinfo* m)
{
- utf *name;
- utf *newname;
- functionptr f;
-#if defined(ENABLE_DL)
- classloader_t *cl;
- hashtable_library_loader_entry *le;
- hashtable_library_name_entry *ne;
- u4 key; /* hashkey */
- u4 slot; /* slot in hashtable */
-#endif
-#if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
- methodinfo *method_findNative;
- java_handle_t *s;
-#endif
-
- /* verbose output */
-
+ // Verbose output.
if (opt_verbosejni) {
printf("[Dynamic-linking native method ");
utf_display_printable_ascii_classname(m->clazz->name);
/* generate method symbol string */
- name = native_method_symbol(m->clazz->name, m->name);
+ utf* name = native_method_symbol(m->clazz->name, m->name);
/* generate overloaded function (having the types in it's name) */
- newname = native_make_overloaded_function(name, m->descriptor);
+ utf* newname = native_make_overloaded_function(name, m->descriptor);
- /* check the library hash entries of the classloader of the
- methods's class */
-
- f = NULL;
+ // Try to find the symbol.
+ void* symbol = NULL;
#if defined(ENABLE_DL)
- /* Get the classloader. */
-
- cl = class_get_classloader(m->clazz);
-
- /* normally addresses are aligned to 4, 8 or 16 bytes */
-
- key = ((u4) (ptrint) cl) >> 4; /* align to 16-byte */
- slot = key & (hashtable_library->size - 1);
- le = (hashtable_library_loader_entry*) hashtable_library->ptr[slot];
-
- /* iterate through loaders in this hash slot */
+ // Get the classloader.
+ classloader_t* classloader = class_get_classloader(m->clazz);
- while ((le != NULL) && (f == NULL)) {
- /* iterate through names in this loader */
+ // Resolve the native method name from the native libraries.
+ NativeLibraries& libraries = VM::get_current()->get_nativelibraries();
+ symbol = libraries.resolve_symbol(name, classloader);
- ne = le->namelink;
-
- while ((ne != NULL) && (f == NULL)) {
- f = (functionptr) (ptrint) os::dlsym(ne->handle, name->text);
-
- if (f == NULL)
- f = (functionptr) (ptrint) os::dlsym(ne->handle, newname->text);
-
- ne = ne->hashlink;
- }
-
- le = le->hashlink;
- }
+ if (symbol == NULL)
+ symbol = libraries.resolve_symbol(newname, classloader);
# if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
- if (f == NULL) {
+ if (symbol == NULL) {
/* We can resolve the function directly from
java.lang.ClassLoader as it's a static function. */
/* XXX should be done in native_init */
- method_findNative =
+ methodinfo* method_findNative =
class_resolveclassmethod(class_java_lang_ClassLoader,
utf_findNative,
utf_java_lang_ClassLoader_java_lang_String__J,
if (method_findNative == NULL)
return NULL;
- /* try the normal name */
+ // Try the normal name.
+ java_handle_t* s = javastring_new(name);
+ symbol = (void*) vm_call_method_long(method_findNative, NULL, classloader, s);
- s = javastring_new(name);
-
- f = (functionptr) (intptr_t) vm_call_method_long(method_findNative,
- NULL, cl, s);
-
- /* if not found, try the mangled name */
-
- if (f == NULL) {
+ // If not found, try the mangled name.
+ if (symbol == NULL) {
s = javastring_new(newname);
-
- f = (functionptr) (intptr_t) vm_call_method_long(method_findNative,
- NULL, cl, s);
+ symbol = (void*) vm_call_method_long(method_findNative, NULL, classloader, s);
}
}
# endif
- if (f != NULL)
+ if (symbol != NULL)
if (opt_verbosejni)
printf("JNI ]\n");
#endif
- /* If not found, try to find the native function symbol in the
- main program. */
+ // If not found already, try to find the native method symbol in
+ // the native methods registered with the VM.
+ if (symbol == NULL) {
+ symbol = find_registered_method(m);
- if (f == NULL) {
- f = native_method_find(m);
-
- if (f != NULL)
+ if (symbol != NULL)
if (opt_verbosejni)
printf("internal ]\n");
}
if (jvmti) jvmti_NativeMethodBind(m, f, &f);
#endif
- /* no symbol found? throw exception */
-
- if (f == NULL) {
+ // Symbol not found? Throw an exception.
+ if (symbol == NULL) {
if (opt_verbosejni)
printf("failed ]\n");
exceptions_throw_unsatisfiedlinkerror(m->name);
}
- return f;
+ return symbol;
}
-/* native_library_open *********************************************************
-
- Open a native library with the given utf8 name.
+/**
+ * Try to find the given method in the native methods registered with
+ * the VM.
+ *
+ * @param m Method structure.
+ *
+ * @return Pointer to function if found, NULL otherwise.
+ */
+void* NativeMethods::find_registered_method(methodinfo* m)
+{
+ NativeMethod nm(m);
+ std::set<NativeMethod>::iterator it = _methods.find(nm);
- IN:
- filename ... filename of the library to open
+ if (it == _methods.end())
+ return NULL;
- RETURN:
- handle of the opened library
+ return (*it).get_function();
+}
-*******************************************************************************/
+/**
+ * Open this native library.
+ *
+ * @return File handle on success, NULL otherwise.
+ */
#if defined(ENABLE_DL)
-void* native_library_open(utf *filename)
+void* NativeLibrary::open()
{
- void* handle;
-
if (opt_verbosejni) {
printf("[Loading native library ");
- utf_display_printable_ascii(filename);
+ utf_display_printable_ascii(_filename);
printf(" ... ");
}
- /* try to open the library */
+ // Sanity check.
+ assert(_filename != NULL);
- handle = os::dlopen(filename->text, RTLD_LAZY);
+ // Try to open the library.
+ _handle = os::dlopen(_filename->text, RTLD_LAZY);
- if (handle == NULL) {
+ if (_handle == NULL) {
if (opt_verbosejni)
printf("failed ]\n");
if (opt_verbose) {
log_start();
- log_print("native_library_open: os::dlopen failed: ");
- log_print(dlerror());
+ log_print("NativeLibrary::open: os::dlopen failed: ");
+ log_print(os::dlerror());
log_finish();
}
if (opt_verbosejni)
printf("OK ]\n");
- return handle;
+ return _handle;
}
#endif
-/* native_library_close ********************************************************
-
- Close the native library of the given handle.
-
- IN:
- handle ... handle of the open library
-
-*******************************************************************************/
-
+/**
+ * Close this native library.
+ */
#if defined(ENABLE_DL)
-void native_library_close(void* handle)
+void NativeLibrary::close()
{
- int result;
-
if (opt_verbosejni) {
printf("[Unloading native library ");
/* utf_display_printable_ascii(filename); */
printf(" ... ");
}
- /* Close the library. */
+ // Sanity check.
+ assert(_handle != NULL);
- result = os::dlclose(handle);
+ // Close the library.
+ int result = os::dlclose(_handle);
if (result != 0) {
+ if (opt_verbosejni)
+ printf("failed ]\n");
+
if (opt_verbose) {
log_start();
- log_print("native_library_close: os::dlclose failed: ");
- log_print(dlerror());
+ log_print("NativeLibrary::close: os::dlclose failed: ");
+ log_print(os::dlerror());
log_finish();
}
}
+
+ if (opt_verbosejni)
+ printf("OK ]\n");
}
#endif
-/* native_library_add **********************************************************
-
- Adds an entry to the native library hashtable.
-
-*******************************************************************************/
-
-#if defined(ENABLE_DL)
-void native_library_add(utf *filename, classloader_t *loader, void* handle)
+/**
+ * Load this native library and initialize it, if possible.
+ *
+ * @param env JNI environment.
+ *
+ * @return true if library loaded successfully, false otherwise.
+ */
+bool NativeLibrary::load(JNIEnv* env)
{
- hashtable_library_loader_entry *le;
- hashtable_library_name_entry *ne; /* library name */
- u4 key; /* hashkey */
- u4 slot; /* slot in hashtable */
-
- hashtable_library->mutex->lock();
-
- /* normally addresses are aligned to 4, 8 or 16 bytes */
-
- key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
- slot = key & (hashtable_library->size - 1);
- le = (hashtable_library_loader_entry*) hashtable_library->ptr[slot];
-
- /* search external hash chain for the entry */
-
- while (le) {
- if (le->loader == loader)
- break;
-
- le = le->hashlink; /* next element in external chain */
+#if defined(ENABLE_DL)
+ if (_filename == NULL) {
+ exceptions_throw_nullpointerexception();
+ return false;
}
- /* no loader found? create a new entry */
-
- if (le == NULL) {
- le = NEW(hashtable_library_loader_entry);
-
- le->loader = loader;
- le->namelink = NULL;
+ // Is the library already loaded?
+ if (is_loaded())
+ return true;
- /* insert entry into hashtable */
+ // Open the library.
+ open();
- le->hashlink =
- (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
- hashtable_library->ptr[slot] = le;
+ if (_handle == NULL)
+ return false;
- /* update number of hashtable-entries */
+# if defined(ENABLE_JNI)
+ // Resolve JNI_OnLoad function.
+ void* onload = os::dlsym(_handle, "JNI_OnLoad");
- hashtable_library->entries++;
- }
+ if (onload != NULL) {
+ JNIEXPORT jint (JNICALL *JNI_OnLoad) (JavaVM*, void*);
+ JavaVM *vm;
+ JNI_OnLoad = (JNIEXPORT jint (JNICALL *)(JavaVM*, void*)) (uintptr_t) onload;
- /* search for library name */
+ env->GetJavaVM(&vm);
- ne = le->namelink;
+ jint version = JNI_OnLoad(vm, NULL);
- while (ne) {
- if (ne->name == filename) {
- hashtable_library->mutex->unlock();
- return;
+ // If the version is not 1.2 and not 1.4 the library cannot be
+ // loaded.
+ if ((version != JNI_VERSION_1_2) && (version != JNI_VERSION_1_4)) {
+ os::dlclose(_handle);
+ return false;
}
-
- ne = ne->hashlink; /* next element in external chain */
}
+# endif
- /* not found? add the library name to the classloader */
-
- ne = NEW(hashtable_library_name_entry);
+ // Insert the library name into the native library table.
+ NativeLibraries& nativelibraries = VM::get_current()->get_nativelibraries();
+ nativelibraries.add(*this);
- ne->name = filename;
- ne->handle = handle;
+ return true;
+#else
+ os::abort("NativeLibrary::load: Not available in this configuration.");
- /* insert entry into external chain */
+ // Keep the compiler happy.
+ return false;
+#endif
+}
- ne->hashlink = le->namelink;
- le->namelink = ne;
- hashtable_library->mutex->unlock();
+/**
+ * Checks if this native library is loaded.
+ *
+ * @return true if loaded, false otherwise.
+ */
+#if defined(ENABLE_DL)
+bool NativeLibrary::is_loaded()
+{
+ NativeLibraries& libraries = VM::get_current()->get_nativelibraries();
+ return libraries.is_loaded(*this);
}
#endif
-/* native_library_find *********************************************************
-
- Find an entry in the native library hashtable.
+/**
+ * Resolve the given symbol in this native library.
+ *
+ * @param symbolname Symbol name.
+ *
+ * @return Pointer to symbol if found, NULL otherwise.
+ */
+void* NativeLibrary::resolve_symbol(utf* symbolname) const
+{
+ return os::dlsym(_handle, symbolname->text);
+}
-*******************************************************************************/
+/**
+ * Add the given native library to the native libraries table.
+ *
+ * @param library Native library to insert.
+ */
#if defined(ENABLE_DL)
-hashtable_library_name_entry *native_library_find(utf *filename,
- classloader_t *loader)
+void NativeLibraries::add(NativeLibrary& library)
{
- hashtable_library_loader_entry *le;
- hashtable_library_name_entry *ne; /* library name */
- u4 key; /* hashkey */
- u4 slot; /* slot in hashtable */
+ // Make the container thread-safe.
+ _mutex.lock();
- /* normally addresses are aligned to 4, 8 or 16 bytes */
+ // XXX Check for double entries.
+ // Insert the native library.
+ _libraries.insert(std::make_pair(library.get_classloader(), library));
- key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
- slot = key & (hashtable_library->size - 1);
- le = (hashtable_library_loader_entry*) hashtable_library->ptr[slot];
-
- /* search external hash chain for the entry */
-
- while (le) {
- if (le->loader == loader)
- break;
-
- le = le->hashlink; /* next element in external chain */
- }
-
- /* no loader found? return NULL */
-
- if (le == NULL)
- return NULL;
-
- /* search for library name */
-
- ne = le->namelink;
-
- while (ne) {
- if (ne->name == filename)
- return ne;
-
- ne = ne->hashlink; /* next element in external chain */
- }
-
- /* return entry, if no entry was found, ne is NULL */
-
- return ne;
+ _mutex.unlock();
}
#endif
-/* native_library_load *********************************************************
-
- Load a native library and initialize it, if possible.
-
- IN:
- name ... name of the library
- cl ..... classloader which loads this library
-
- RETURN:
- 1 ... library loaded successfully
- 0 ... error
-
-*******************************************************************************/
-
-int native_library_load(JNIEnv *env, utf *name, classloader_t *cl)
+/**
+ * Checks if the given native library is loaded.
+ *
+ * @param library Native library.
+ *
+ * @return true if loaded, false otherwise.
+ */
+bool NativeLibraries::is_loaded(NativeLibrary& library)
{
-#if defined(ENABLE_DL)
- void* handle;
-# if defined(ENABLE_JNI)
- void* onload;
- int32_t version;
-# endif
-
- if (name == NULL) {
- exceptions_throw_nullpointerexception();
- return 0;
- }
-
- /* Is the library already loaded? */
-
- if (native_library_find(name, cl) != NULL)
- return 1;
-
- /* Open the library. */
-
- handle = native_library_open(name);
-
- if (handle == NULL)
- return 0;
+ std::pair<MAP::const_iterator, MAP::const_iterator> its = _libraries.equal_range(library.get_classloader());
-# if defined(ENABLE_JNI)
- /* Resolve JNI_OnLoad function. */
+ // No entry for the classloader was found (the range has length
+ // zero).
+ if (its.first == its.second)
+ return false;
+
+ MAP::const_iterator it = find_if(its.first, its.second, std::bind2nd(comparator(), library.get_filename()));
- onload = os::dlsym(handle, "JNI_OnLoad");
+ // No matching entry in the range found.
+ if (it == its.second)
+ return false;
- if (onload != NULL) {
- JNIEXPORT int32_t (JNICALL *JNI_OnLoad) (JavaVM *, void *);
- JavaVM *vm;
+ return true;
+}
- JNI_OnLoad = (JNIEXPORT int32_t (JNICALL *)(JavaVM *, void *)) (ptrint) onload;
- env->GetJavaVM(&vm);
+/**
+ * Try to find a symbol with the given name in all loaded native
+ * libraries defined by classloader.
+ *
+ * @param symbolname Name of the symbol to find.
+ * @param classloader Defining classloader.
+ *
+ * @return Pointer to symbol if found, NULL otherwise.
+ */
+void* NativeLibraries::resolve_symbol(utf* symbolname, classloader_t* classloader)
+{
+ std::pair<MAP::const_iterator, MAP::const_iterator> its = _libraries.equal_range(classloader);
- version = JNI_OnLoad(vm, NULL);
+ // No entry for the classloader was found (the range has length
+ // zero).
+ if (its.first == its.second)
+ return NULL;
- /* If the version is not 1.2 and not 1.4 the library cannot be
- loaded. */
+ for (MAP::const_iterator it = its.first; it != its.second; it++) {
+ const NativeLibrary& library = (*it).second;
+ void* symbol = library.resolve_symbol(symbolname);
- if ((version != JNI_VERSION_1_2) && (version != JNI_VERSION_1_4)) {
- os::dlclose(handle);
- return 0;
- }
+ if (symbol != NULL)
+ return symbol;
}
-# endif
- /* Insert the library name into the library hash. */
-
- native_library_add(name, cl, handle);
-
- return 1;
-#else
- vm_abort("native_library_load: not available");
-
- /* Keep compiler happy. */
-
- return 0;
-#endif
+ return NULL;
}
Copyright (C) 1996-2005, 2006, 2007, 2008
CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
+ Copyright (C) 2008 Theobroma Systems Ltd.
This file is part of CACAO.
#include <stdint.h>
+#ifdef __cplusplus
+#include <map>
+#include <set>
+#endif
+
#include "native/jni.hpp"
#include "vm/class.h"
#endif
-/* native_methods_node_t ******************************************************/
+#ifdef __cplusplus
+
+#if defined(ENABLE_DL)
+/**
+ * Represents a native library.
+ */
+class NativeLibrary {
+private:
+ utf* _filename; ///< Name of the native library.
+ classloader_t* _classloader; ///< Defining classloader.
+ void* _handle; ///< Filesystem handle.
+
+public:
+ NativeLibrary(utf* filename, classloader_t* classloader = 0, void* handle = 0) : _filename(filename), _classloader(classloader), _handle(handle) {}
+ NativeLibrary(void* handle) : _filename(0), _classloader(0), _handle(handle) {}
+
+ inline classloader_t* get_classloader() const { return _classloader; }
+ inline utf* get_filename () const { return _filename; }
+ inline void* get_handle () const { return _handle; }
+
+ void* open();
+ void close();
+ bool load(JNIEnv* env);
+ bool is_loaded();
+ void* resolve_symbol(utf* symbolname) const;
+};
-typedef struct native_methods_node_t native_methods_node_t;
-struct native_methods_node_t {
- utf *classname; /* class name */
- utf *name; /* method name */
- utf *descriptor; /* descriptor name */
- functionptr function; /* pointer to the implementation */
+/**
+ * Table containing all loaded native libraries.
+ */
+class NativeLibraries {
+private:
+ Mutex _mutex; ///< Mutex to make the container thread-safe.
+ typedef std::multimap<classloader_t*, NativeLibrary> MAP;
+ MAP _libraries;
+
+private:
+ // Comparator class.
+ class comparator : public std::binary_function<std::pair<classloader_t*, NativeLibrary>, utf*, bool> {
+ public:
+ bool operator() (std::pair<classloader_t*, NativeLibrary> args, const utf* filename) const
+ {
+ return (args.second.get_filename() == filename);
+ }
+ };
+
+public:
+ void add(NativeLibrary& library);
+ bool is_loaded(NativeLibrary& library);
+ void* resolve_symbol(utf* symbolname, classloader_t* classloader);
};
+#endif
-/* hashtable_library_loader_entry *********************************************/
+/**
+ * Represents a native method.
+ */
+class NativeMethod {
+private:
+ utf* _classname; ///< Class name.
+ utf* _name; ///< Method name.
+ utf* _descriptor; ///< Method signature.
+ void* _function; ///< Pointer to the native function.
-#if defined(ENABLE_DL)
-typedef struct hashtable_library_loader_entry hashtable_library_loader_entry;
-typedef struct hashtable_library_name_entry hashtable_library_name_entry;
+ friend bool operator< (const NativeMethod& first, const NativeMethod& second);
-struct hashtable_library_loader_entry {
- classloader_t *loader; /* class loader */
- hashtable_library_name_entry *namelink;/* libs loaded by this loader */
- hashtable_library_loader_entry *hashlink;/* link for external chaining */
-};
-#endif
+public:
+ NativeMethod(utf* classname, utf* name, utf* signature, void* function) : _classname(classname), _name(name), _descriptor(signature), _function(function) {}
+ NativeMethod(methodinfo* m) : _classname(m->clazz->name), _name(m->name), _descriptor(m->descriptor), _function(0) {}
+ inline void* get_function() const { return _function; }
+};
-/* hashtable_library_name_entry ***********************************************/
-#if defined(ENABLE_DL)
-struct hashtable_library_name_entry {
- utf *name; /* library name */
- void* handle; /* libtool library handle */
- hashtable_library_name_entry *hashlink; /* link for external chaining */
+/**
+ * Table containing all native methods registered with the VM.
+ */
+class NativeMethods {
+private:
+ Mutex _mutex;
+ std::set<NativeMethod> _methods;
+
+private:
+ // Comparator class.
+ class comparator : public std::binary_function<std::pair<classloader_t*, NativeLibrary>, utf*, bool> {
+ public:
+ bool operator() (std::pair<classloader_t*, NativeLibrary> args, const utf* filename) const
+ {
+ return (args.second.get_filename() == filename);
+ }
+ };
+
+public:
+ void register_methods(utf* classname, const JNINativeMethod* methods, size_t count);
+ void* resolve_method(methodinfo* m);
+ void* find_registered_method(methodinfo* m);
};
-#endif
+#endif
/* function prototypes ********************************************************/
extern "C" {
#endif
-bool native_init(void);
-
-void native_method_register(utf *classname, const JNINativeMethod *methods, int32_t count);
-functionptr native_method_resolve(methodinfo *m);
-
-#if defined(ENABLE_DL)
-void* native_library_open(utf *filename);
-void native_library_close(void* handle);
-void native_library_add(utf *filename, classloader_t *loader, void *handle);
-hashtable_library_name_entry *native_library_find(utf *filename, classloader_t *loader);
-int native_library_load(JNIEnv *env, utf *name, classloader_t *cl);
-#endif
-
java_handle_t *native_new_and_init(classinfo *c);
java_handle_t *native_new_and_init_string(classinfo *c, java_handle_t *s);
void _Jv_com_sun_cldc_io_ResourceInputStream_init(void)
{
- utf *u;
+ utf* u = utf_new_char("com/sun/cldc/io/ResourceInputStream");
- u = utf_new_char("com/sun/cldc/io/ResourceInputStream");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_com_sun_cldc_io_j2me_socket_Protocol_init(void)
{
- utf *u;
+ utf* u = utf_new_char("com/sun/cldc/io/j2me/socket/Protocol");
- u = utf_new_char("com/sun/cldc/io/j2me/socket/Protocol");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
# include "native/include/com_sun_cldchi_io_ConsoleOutputStream.h"
#endif
+#include "vm/vm.hpp"
+
// Native functions are exported as C functions.
extern "C" {
void _Jv_com_sun_cldchi_io_ConsoleOutputStream_init(void)
{
- utf *u;
-
- u = utf_new_char("com/sun/cldchi/io/ConsoleOutputStream");
+ utf* u = utf_new_char("com/sun/cldchi/io/ConsoleOutputStream");
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
#include "vm/exceptions.hpp"
#include "vm/string.hpp"
+#include "vm/vm.hpp"
// Native functions are exported as C functions.
return;
}
- /* REMOVEME When we use Java-strings internally. */
+ // REMOVEME When we use Java-strings internally.
+ utf* name = javastring_toutf((java_handle_t*) libName, false);
- utf* name = javastring_toutf((java_handle_t *) libName, false);
+ NativeLibrary nl(name);
+ bool result = nl.load(env);
- int result = native_library_load(env, name, NULL);
-
- /* Check for error and throw an exception in case. */
-
- if (result == 0) {
+ // Check for error and throw an exception in case.
+ if (result == false) {
exceptions_throw_unsatisfiedlinkerror(name);
}
}
void _Jv_com_sun_cldchi_jvm_JVM_init(void)
{
- utf *u;
-
- u = utf_new_char("com/sun/cldchi/jvm/JVM");
+ utf* u = utf_new_char("com/sun/cldchi/jvm/JVM");
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_Class_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/Class");
- u = utf_new_char("java/lang/Class");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
# include "native/include/java_lang_Double.h"
#endif
+#include "vm/vm.hpp"
+
#include "vm/jit/builtin.hpp"
void _Jv_java_lang_Double_init(void)
{
- utf *u;
-
- u = utf_new_char("java/lang/Double");
+ utf* u = utf_new_char("java/lang/Double");
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
# include "native/include/java_lang_Float.h"
#endif
+#include "vm/vm.hpp"
+
#include "vm/jit/builtin.hpp"
void _Jv_java_lang_Float_init(void)
{
- utf *u;
-
- u = utf_new_char("java/lang/Float");
+ utf* u = utf_new_char("java/lang/Float");
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
# include "native/include/java_lang_Math.h"
#endif
+#include "vm/vm.hpp"
+
// Native functions are exported as C functions.
extern "C" {
void _Jv_java_lang_Math_init(void)
{
- utf *u;
-
- u = utf_new_char("java/lang/Math");
+ utf* u = utf_new_char("java/lang/Math");
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_Object_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/Object");
- u = utf_new_char("java/lang/Object");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_Runtime_init(void)
{
- utf *u;
-
- u = utf_new_char("java/lang/Runtime");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ utf* u = utf_new_char("java/lang/Runtime");
+
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_String_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/String");
- u = utf_new_char("java/lang/String");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_System_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/System");
- u = utf_new_char("java/lang/System");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_Thread_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/Thread");
- u = utf_new_char("java/lang/Thread");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_Throwable_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/Throwable");
- u = utf_new_char("java/lang/Throwable");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_gnu_classpath_VMStackWalker_init(void)
{
- utf *u;
+ utf* u = utf_new_char("gnu/classpath/VMStackWalker");
- u = utf_new_char("gnu/classpath/VMStackWalker");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_gnu_classpath_VMSystemProperties_init(void)
{
- utf *u;
+ utf* u = utf_new_char("gnu/classpath/VMSystemProperties");
- u = utf_new_char("gnu/classpath/VMSystemProperties");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_gnu_java_lang_VMCPStringBuilder_init(void)
{
- utf *u;
+ utf* u = utf_new_char("gnu/java/lang/VMCPStringBuilder");
- u = utf_new_char("gnu/java/lang/VMCPStringBuilder");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_gnu_java_lang_management_VMClassLoadingMXBeanImpl_init(void)
{
- utf *u;
+ utf* u = utf_new_char("gnu/java/lang/management/VMClassLoadingMXBeanImpl");
- u = utf_new_char("gnu/java/lang/management/VMClassLoadingMXBeanImpl");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_gnu_java_lang_management_VMMemoryMXBeanImpl_init(void)
{
- utf *u;
+ utf* u = utf_new_char("gnu/java/lang/management/VMMemoryMXBeanImpl");
- u = utf_new_char("gnu/java/lang/management/VMMemoryMXBeanImpl");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_gnu_java_lang_management_VMRuntimeMXBeanImpl_init(void)
{
- utf *u;
+ utf* u = utf_new_char("gnu/java/lang/management/VMRuntimeMXBeanImpl");
- u = utf_new_char("gnu/java/lang/management/VMRuntimeMXBeanImpl");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_gnu_java_lang_management_VMThreadMXBeanImpl_init(void)
{
- utf *u;
+ utf* u = utf_new_char("gnu/java/lang/management/VMThreadMXBeanImpl");
- u = utf_new_char("gnu/java/lang/management/VMThreadMXBeanImpl");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_VMClass_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/VMClass");
- u = utf_new_char("java/lang/VMClass");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_VMClassLoader_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/VMClassLoader");
- u = utf_new_char("java/lang/VMClassLoader");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_VMObject_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/VMObject");
- u = utf_new_char("java/lang/VMObject");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
*/
JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, jstring libname, jobject loader)
{
- classloader_t *cl;
- utf *name;
-
- cl = loader_hashtable_classloader_add((java_handle_t *) loader);
+ classloader_t* cl = loader_hashtable_classloader_add((java_handle_t *) loader);
/* REMOVEME When we use Java-strings internally. */
return 0;
}
- name = javastring_toutf((java_handle_t *) libname, false);
+ utf* name = javastring_toutf((java_handle_t *) libname, false);
- return native_library_load(env, name, cl);
+ NativeLibrary library(name, cl);
+ return library.load(env);
}
void _Jv_java_lang_VMRuntime_init(void)
{
- utf *u;
-
- u = utf_new_char("java/lang/VMRuntime");
+ utf* u = utf_new_char("java/lang/VMRuntime");
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
#endif
#include "vm/string.hpp"
+#include "vm/vm.hpp"
// Native functions are exported as C functions.
void _Jv_java_lang_VMString_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/VMString");
- u = utf_new_char("java/lang/VMString");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_VMSystem_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/VMSystem");
- u = utf_new_char("java/lang/VMSystem");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_VMThread_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/VMThread");
- u = utf_new_char("java/lang/VMThread");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_VMThrowable_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/VMThrowable");
- u = utf_new_char("java/lang/VMThrowable");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
#include "toolbox/logging.h"
-#include "vm/jit/builtin.hpp"
#include "vm/globals.hpp"
+#include "vm/vm.hpp"
+
+#include "vm/jit/builtin.hpp"
// Native functions are exported as C functions.
void _Jv_java_lang_management_VMManagementFactory_init(void)
{
- utf *u;
-
- u = utf_new_char("java/lang/management/VMManagementFactory");
+ utf* u = utf_new_char("java/lang/management/VMManagementFactory");
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_reflect_VMConstructor_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/reflect/VMConstructor");
- u = utf_new_char("java/lang/reflect/VMConstructor");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_reflect_VMField_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/reflect/VMField");
- u = utf_new_char("java/lang/reflect/VMField");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_lang_reflect_VMMethod_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/lang/reflect/VMMethod");
- u = utf_new_char("java/lang/reflect/VMMethod");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_java_security_VMAccessController_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/security/VMAccessController");
- u = utf_new_char("java/security/VMAccessController");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
#endif
#include "vm/utf8.h"
+#include "vm/vm.hpp"
// Native functions are exported as C functions.
void _Jv_java_util_concurrent_atomic_AtomicLong_init(void)
{
- utf *u;
+ utf* u = utf_new_char("java/util/concurrent/atomic/AtomicLong");
- u = utf_new_char("java/util/concurrent/atomic/AtomicLong");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
void _Jv_sun_reflect_ConstantPool_init(void)
{
- native_method_register(utf_new_char("sun/reflect/ConstantPool"), methods, NATIVE_METHODS_COUNT);
+ utf* u = utf_new_char("sun/reflect/ConstantPool");
+
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
utf* u = utf_new_char(p);
- void* handle = native_library_open(u);
+ NativeLibrary nl(u);
+ void* handle = nl.open();
if (handle == NULL)
- vm_abort("nativevm_init: failed to open libjava.so at: %s", p);
+ os::abort("nativevm_init: failed to open libjava.so at: %s", p);
MFREE(p, char, len);
- native_library_add(u, NULL, handle);
+ NativeLibraries& nls = vm->get_nativelibraries();
+ nls.add(nl);
// Initialize the HPI.
HPI& hpi = vm->get_hpi();
TRACESUBSYSTEMINITIALIZATION("hpi_init");
// Load libhpi.so
- Properties& properties = VM::get_current()->get_properties();
+ VM* vm = VM::get_current();
+ Properties& properties = vm->get_properties();
const char* boot_library_path = properties.get("sun.boot.library.path");
size_t len =
MFREE(p, char, len);
- void* handle = native_library_open(u);
+ NativeLibrary nl(u);
+ void* handle = nl.open();
if (handle == NULL)
if (opt_TraceHPI)
vm_abort("hpi_init: HPI DLL_Initialize failed");
}
- native_library_add(u, NULL, handle);
+ NativeLibraries& nls = vm->get_nativelibraries();
+ nls.add(nl);
if (opt_TraceHPI)
log_println("HPI::initialize: HPI loaded successfully");
/* JVM_LoadLibrary */
-void *JVM_LoadLibrary(const char *name)
+void* JVM_LoadLibrary(const char* name)
{
- utf* u;
- void* handle;
-
TRACEJVMCALLSENTER(("JVM_LoadLibrary(name=%s)", name));
- u = utf_new_char(name);
-
- handle = native_library_open(u);
+ utf* u = utf_new_char(name);
+ NativeLibrary nl(u);
+ void* handle = nl.open();
+
TRACEJVMCALLSEXIT(("->%p", handle));
return handle;
{
TRACEJVMCALLS(("JVM_UnloadLibrary(handle=%p)", handle));
- native_library_close(handle);
+ NativeLibrary nl(handle);
+ nl.close();
}
void _Jv_sun_misc_Unsafe_init(void)
{
- utf *u;
+ utf* u = utf_new_char("sun/misc/Unsafe");
- u = utf_new_char("sun/misc/Unsafe");
-
- native_method_register(u, methods, NATIVE_METHODS_COUNT);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
}
show_filters_apply(jd->m);
#endif
- /* Handle native methods and create a native stub. */
-
+ // Handle native methods and create a native stub.
if (m->flags & ACC_NATIVE) {
- functionptr f;
-
- f = native_method_resolve(m);
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ void* f = nm.resolve_method(m);
if (f == NULL)
return NULL;
- code = NativeStub::generate(m, f);
+ code = NativeStub::generate(m, (functionptr) f);
/* Native methods are never recompiled. */
{
methodinfo *m;
uint8_t *datap;
- functionptr f;
/* get stuff from the patcher reference */
/* resolve native function */
- if (!(f = native_method_resolve(m)))
+ NativeMethods& nm = VM::get_current()->get_nativemethods();
+ void* f = nm.resolve_method(m);
+
+ if (f == NULL)
return false;
/* patch native function pointer */
- *((intptr_t *) datap) = (intptr_t) f;
+ *((intptr_t*) datap) = (intptr_t) f;
/* synchronize data cache */
if (!builtin_init())
os::abort("vm_create: builtin_init failed");
- /* Initialize the native subsystem. */
- /* BEFORE: threads_init */
-
- if (!native_init())
- os::abort("vm_create: native_init failed");
-
/* Register the native methods implemented in the VM. */
/* BEFORE: threads_init */
// We need the JNI types for the VM class.
#include "native/jni.hpp"
+#include "native/native.hpp"
#if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
# include "native/vm/openjdk/hpi.hpp"
int64_t _starttime;
// Subsystems.
- Properties _properties; ///< Commandline properties.
+ Properties _properties; ///< Commandline properties.
#if defined(ENABLE_THREADS)
- Recompiler _recompiler; ///< JIT recompilation framework.
+ Recompiler _recompiler; ///< JIT recompilation framework.
#endif
#if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
- HPI _hpi;
- Management _management;
+ HPI _hpi; ///< Host Porting Interface.
+ Management _management; ///< Java management interface.
#endif
+ NativeLibraries _nativelibraries; ///< Native library table.
+ NativeMethods _nativemethods; ///< Native methods table.
public:
// Constructor, Destructor.
bool is_exiting() { return _exiting; }
int64_t get_starttime() { return _starttime; }
- Properties& get_properties() { return _properties; }
- Recompiler& get_recompiler() { return _recompiler; } // REMOVEME
+ Properties& get_properties () { return _properties; }
+ Recompiler& get_recompiler () { return _recompiler; } // REMOVEME
#if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
- HPI& get_hpi () { return _hpi; }
- Management& get_management() { return _management; }
+ HPI& get_hpi () { return _hpi; }
+ Management& get_management () { return _management; }
#endif
+ NativeLibraries& get_nativelibraries() { return _nativelibraries; }
+ NativeMethods& get_nativemethods () { return _nativemethods; }
};
#else