Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
no xbt_lib in java world + leak plug
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Wed, 10 May 2017 09:03:08 +0000 (11:03 +0200)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Wed, 10 May 2017 09:03:08 +0000 (11:03 +0200)
src/bindings/java/jmsg.cpp
src/bindings/java/jmsg.h
src/bindings/java/jmsg_host.cpp
src/bindings/java/jmsg_storage.cpp

index ec98fde..3a5392d 100644 (file)
@@ -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)
index de18540..34ba2a2 100644 (file)
@@ -7,8 +7,9 @@
 
 #ifndef MSG4JAVA_H
 #define MSG4JAVA_H
-#include <simgrid/msg.h>
 #include <jni.h>
+#include <simgrid/msg.h>
+#include <unordered_map>
 
 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<char*, jobject> java_storage_map;
 
 JavaVM *get_java_VM();
 JNIEnv *get_current_thread_env();
index 7b45e78..1169daf 100644 (file)
@@ -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;
 }
 
index 48e73bc..15bd3fe 100644 (file)
@@ -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);
     }