SG_BEGIN_DECL()
int JAVA_HOST_LEVEL = -1;
-int JAVA_STORAGE_LEVEL = -1;
XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java);
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]));
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)
#ifndef MSG4JAVA_H
#define MSG4JAVA_H
-#include <simgrid/msg.h>
#include <jni.h>
+#include <simgrid/msg.h>
+#include <unordered_map>
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();
jstorage_name = env->NewStringUTF(storage_name);
env->SetObjectArrayElement(jtable, index, jstorage_name);
}
-
+ xbt_dynar_free_container(&dyn);
return jtable;
}
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) {
}
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);
/* 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) {
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);
}