+ jvm_bind(env, jVm, vm);
+ jVm = env->NewGlobalRef(jVm);
+ // We use the extension level of the host, even if that's somehow disturbing
+ vm->extension_set(JAVA_HOST_LEVEL, (void*)jVm);
+}
+
+JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_VM_all(JNIEnv* env, jclass cls_arg)
+{
+ xbt_dynar_t hosts = MSG_hosts_as_dynar();
+ std::vector<jobject> vms;
+
+ unsigned int it;
+ msg_host_t h;
+ xbt_dynar_foreach (hosts, it, h) {
+ simgrid::s4u::VirtualMachine* vm = dynamic_cast<simgrid::s4u::VirtualMachine*>(h);
+ if (vm != nullptr && vm->getState() != SURF_VM_STATE_DESTROYED) {
+ jobject jvm = static_cast<jobject>(vm->extension(JAVA_HOST_LEVEL));
+ vms.push_back(jvm);
+ }
+ }
+ xbt_dynar_free(&hosts);
+
+ vms.shrink_to_fit();
+ int count = vms.size();
+
+ jclass cls = jxbt_get_class(env, "org/simgrid/msg/VM");
+ if (not cls)
+ return nullptr;
+
+ jobjectArray jtable = env->NewObjectArray((jsize)count, cls, nullptr);
+ if (not jtable) {
+ jxbt_throw_jni(env, "Hosts table allocation failed");
+ return nullptr;
+ }
+
+ for (int index = 0; index < count; index++) {
+ jobject jhost = vms.at(index);
+ env->SetObjectArrayElement(jtable, index, jhost);
+ }
+ return jtable;