Signed-off-by: Tomek Grabiec <tgrab...@gmail.com> --- include/vm/jni.h | 1 + vm/jni-interface.c | 2 +- vm/jni.c | 72 ++++++++++++++++++++++++++++++++++----------------- 3 files changed, 50 insertions(+), 25 deletions(-)
diff --git a/include/vm/jni.h b/include/vm/jni.h index 866255d..4dd8b1c 100644 --- a/include/vm/jni.h +++ b/include/vm/jni.h @@ -71,6 +71,7 @@ struct java_vm { typedef struct java_vm JavaVM; void vm_jni_init(void); +void vm_jni_init_interface(void); struct vm_jni_env *vm_jni_get_jni_env(void); struct java_vm *vm_jni_get_current_java_vm(void); int vm_jni_load_object(const char *name); diff --git a/vm/jni-interface.c b/vm/jni-interface.c index d961c52..4575846 100644 --- a/vm/jni-interface.c +++ b/vm/jni-interface.c @@ -1350,7 +1350,7 @@ struct vm_jni_env *vm_jni_get_jni_env(void) static void *jni_not_implemented_trap; -void vm_jni_init(void) +void vm_jni_init_interface(void) { jni_not_implemented_trap = alloc_guard_page(true); if (!jni_not_implemented_trap) diff --git a/vm/jni.c b/vm/jni.c index 95f98ef..38e7e62 100644 --- a/vm/jni.c +++ b/vm/jni.c @@ -37,10 +37,27 @@ #include "vm/jni.h" #include "vm/stack-trace.h" +#include "lib/hash-map.h" #include "lib/string.h" -static int vm_jni_nr_loaded_objects; -static void **vm_jni_loaded_objects; +struct jni_object { + void *handle; /* returned by dlopen() */ +}; + +static unsigned long jni_object_hash(const void *key, unsigned long size) +{ + unsigned long a; + + a = (unsigned long) key; + return ((a >> 24) ^ (a >> 16) ^ (a >> 8) ^ a) % size; +} + +static int jni_object_compare(const void *key1, const void *key2) +{ + return (long) key1 - (long) key2; +} + +struct hash_map *jni_objects; static char *vm_jni_get_mangled_name(const char *name) { @@ -85,34 +102,36 @@ static char *vm_jni_get_mangled_name(const char *name) return result; } -static int vm_jni_lookup_object_handle(void *handle) -{ - for (int i = 0; i < vm_jni_nr_loaded_objects; i++) - if (vm_jni_loaded_objects[i] == handle) - return i; - - return -1; -} - -static int vm_jni_add_object_handle(void *handle) +static int vm_jni_add_object(void *handle) { - void **new_table; - int new_size; + struct jni_object *object; - if (vm_jni_lookup_object_handle(handle) >= 0) - return 0; /* handle already in table */ + if (hash_map_contains(jni_objects, handle)) + return 0; - new_size = sizeof(void *) * (vm_jni_nr_loaded_objects + 1); - new_table = realloc(vm_jni_loaded_objects, new_size); - if (!new_table) + object = malloc(sizeof(*object)); + if (!object) return -ENOMEM; - vm_jni_loaded_objects = new_table; - vm_jni_loaded_objects[vm_jni_nr_loaded_objects++] = handle; + object->handle = handle; + + if (hash_map_put(jni_objects, handle, object)) { + free(object); + return -1; + } return 0; } +void vm_jni_init(void) +{ + jni_objects = alloc_hash_map(100, jni_object_hash, jni_object_compare); + if (!jni_objects) + error("failed to create jni_objects hash map"); + + vm_jni_init_interface(); +} + typedef jint onload_fn(JavaVM *, void *); int vm_jni_load_object(const char *name) @@ -123,7 +142,7 @@ int vm_jni_load_object(const char *name) if (!handle) return -1; - if (vm_jni_add_object_handle(handle)) { + if (vm_jni_add_object(handle)) { dlclose(handle); return -ENOMEM; } @@ -144,10 +163,15 @@ int vm_jni_load_object(const char *name) static void *vm_jni_lookup_symbol(const char *symbol_name) { - for (int i = 0; i < vm_jni_nr_loaded_objects; i++) { + struct hash_map_entry *this; + + hash_map_for_each_entry(this, jni_objects) { + struct jni_object *object; void *addr; - addr = dlsym(vm_jni_loaded_objects[i], symbol_name); + object = this->value; + + addr = dlsym(object->handle, symbol_name); if (addr) return addr; } -- 1.6.0.6 ------------------------------------------------------------------------------ Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july _______________________________________________ Jatovm-devel mailing list Jatovm-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jatovm-devel