From 79b6186408f149d641b253ef0db88bd258c4eb3a Mon Sep 17 00:00:00 2001 From: Frederic Suter Date: Wed, 10 May 2017 11:03:08 +0200 Subject: [PATCH] no xbt_lib in java world + leak plug --- src/bindings/java/jmsg.cpp | 13 ++----------- src/bindings/java/jmsg.h | 5 +++-- src/bindings/java/jmsg_host.cpp | 2 +- src/bindings/java/jmsg_storage.cpp | 18 +++++++++--------- 4 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/bindings/java/jmsg.cpp b/src/bindings/java/jmsg.cpp index ec98fdec31..3a5392d70a 100644 --- a/src/bindings/java/jmsg.cpp +++ b/src/bindings/java/jmsg.cpp @@ -39,7 +39,6 @@ SG_BEGIN_DECL() int JAVA_HOST_LEVEL = -1; -int JAVA_STORAGE_LEVEL = -1; XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java); @@ -132,7 +131,6 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, j MSG_init(&argc, argv); JAVA_HOST_LEVEL = simgrid::s4u::Host::extension_create(__JAVA_host_priv_free); - JAVA_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, __JAVA_storage_priv_free); for (index = 0; index < argc - 1; index++) { env->SetObjectArrayElement(jargs, index, (jstring)env->NewStringUTF(argv[index + 1])); @@ -164,15 +162,8 @@ JNIEXPORT void JNICALL JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass xbt_dynar_free(&hosts); /* Cleanup java storages */ - xbt_dynar_t storages = MSG_storages_as_dynar(); - if(!xbt_dynar_is_empty(storages)){ - for (unsigned long index = 0; index < xbt_dynar_length(storages) - 1; index++) { - jobject jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL); - if (jstorage) - jstorage_unref(env, jstorage); - } - } - xbt_dynar_free(&storages); + for (auto elm : java_storage_map) + jstorage_unref(env, elm.second); } JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls, jstring jplatformFile) diff --git a/src/bindings/java/jmsg.h b/src/bindings/java/jmsg.h index de185402f8..34ba2a24a9 100644 --- a/src/bindings/java/jmsg.h +++ b/src/bindings/java/jmsg.h @@ -7,8 +7,9 @@ #ifndef MSG4JAVA_H #define MSG4JAVA_H -#include #include +#include +#include SG_BEGIN_DECL() @@ -22,7 +23,7 @@ SG_BEGIN_DECL() /* end of eclipse-mandated pimple */ extern int JAVA_HOST_LEVEL; -extern int JAVA_STORAGE_LEVEL; +static std::unordered_map java_storage_map; JavaVM *get_java_VM(); JNIEnv *get_current_thread_env(); diff --git a/src/bindings/java/jmsg_host.cpp b/src/bindings/java/jmsg_host.cpp index 7b45e78355..1169dafa1f 100644 --- a/src/bindings/java/jmsg_host.cpp +++ b/src/bindings/java/jmsg_host.cpp @@ -282,7 +282,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getAttachedStorage(JNIE jstorage_name = env->NewStringUTF(storage_name); env->SetObjectArrayElement(jtable, index, jstorage_name); } - + xbt_dynar_free_container(&dyn); return jtable; } diff --git a/src/bindings/java/jmsg_storage.cpp b/src/bindings/java/jmsg_storage.cpp index 48e73bc19b..15bd3fe4ed 100644 --- a/src/bindings/java/jmsg_storage.cpp +++ b/src/bindings/java/jmsg_storage.cpp @@ -53,7 +53,7 @@ void jstorage_unref(JNIEnv * env, jobject jstorage) { JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls, jstring jname) { msg_storage_t storage; - jobject jstorage; + jobject jstorage = nullptr; /* get the C string from the java string */ if (jname == nullptr) { @@ -70,8 +70,7 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, j } env->ReleaseStringUTFChars(jname, name); - if (!xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL)) { /* native host not associated yet with java host */ - + if (java_storage_map.find(storage->key) == java_storage_map.end()) { /* Instantiate a new java storage */ jstorage = jstorage_new_instance(env); @@ -95,11 +94,12 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, j /* the native storage data field is set with the global reference to the * java storage returned by this function */ - xbt_lib_set(storage_lib, storage->key, JAVA_STORAGE_LEVEL, (void *) jstorage); - } + java_storage_map.insert({storage->key, jstorage}); + } else + jstorage = java_storage_map.at(storage->key); /* return the global reference to the java storage instance */ - return (jobject) xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL); + return (jobject)jstorage; } JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getSize(JNIEnv * env,jobject jstorage) { @@ -216,9 +216,9 @@ JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Storage_all(JNIEnv * env, jc for (index = 0; index < count; index++) { storage = xbt_dynar_get_as(table,index,msg_storage_t); - jstorage = (jobject) (xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL)); - - if (!jstorage) { + if (java_storage_map.find(storage->key) != java_storage_map.end()) { + jstorage = java_storage_map.at(storage->key); + } else { jname = env->NewStringUTF(MSG_storage_get_name(storage)); jstorage = Java_org_simgrid_msg_Storage_getByName(env, cls_arg, jname); } -- 2.20.1