[vmkit-commits] [vmkit] r197884 - add global references in JNI

Gael Thomas gael.thomas at lip6.fr
Sun Dec 22 09:49:48 PST 2013


Author: gthomas
Date: Sun Dec 22 11:49:48 2013
New Revision: 197884

URL: http://llvm.org/viewvc/llvm-project?rev=197884&view=rev
Log:
add global references in JNI

Modified:
    vmkit/branches/mcjit/include/j3/j3classloader.h
    vmkit/branches/mcjit/include/j3/j3object.h
    vmkit/branches/mcjit/include/vmkit/stack.h
    vmkit/branches/mcjit/lib/j3/vm/j3.cc
    vmkit/branches/mcjit/lib/j3/vm/j3class.cc
    vmkit/branches/mcjit/lib/j3/vm/j3jni.cc
    vmkit/branches/mcjit/lib/j3/vm/j3object.cc

Modified: vmkit/branches/mcjit/include/j3/j3classloader.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/j3/j3classloader.h?rev=197884&r1=197883&r2=197884&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/j3/j3classloader.h (original)
+++ vmkit/branches/mcjit/include/j3/j3classloader.h Sun Dec 22 11:49:48 2013
@@ -38,7 +38,7 @@ namespace j3 {
 		static J3MethodLess  j3MethodLess;
 
 		J3ObjectHandle*                      _javaClassLoader;
-		J3LocalReferences                    _globalReferences;
+		J3GlobalReferences                   _globalReferences;
 		pthread_mutex_t                      _mutex;       /* a lock */
 		vmkit::NameMap<J3Class*>::map        classes;      /* classes managed by this class loader */
 		vmkit::NameMap<J3Type*>::map         types;        /* shortcut to find types */
@@ -57,8 +57,8 @@ namespace j3 {
 	public:
 		J3ClassLoader(J3* vm, J3ObjectHandle* javaClassLoader, vmkit::BumpAllocator* allocator);
 
-		J3LocalReferences*            globalReferences() { return &_globalReferences; }
-
+		J3GlobalReferences*           globalReferences() { return &_globalReferences; }
+		
 		J3ObjectHandle*               javaClassLoader() { return _javaClassLoader; }
 
 		void                          lock() { pthread_mutex_lock(&_mutex); }

Modified: vmkit/branches/mcjit/include/j3/j3object.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/j3/j3object.h?rev=197884&r1=197883&r2=197884&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/j3/j3object.h (original)
+++ vmkit/branches/mcjit/include/j3/j3object.h Sun Dec 22 11:49:48 2013
@@ -161,6 +161,17 @@ namespace j3 {
 		J3ObjectHandle* push(J3Object* obj);
 	};
 
+	class J3GlobalReferences {
+		pthread_mutex_t               mutex;
+		vmkit::Stack<J3ObjectHandle>  references;
+		vmkit::Stack<J3ObjectHandle*> emptySlots;
+	public:
+		J3GlobalReferences(vmkit::BumpAllocator* _allocator);
+		
+		J3ObjectHandle* add(J3ObjectHandle* handle);
+		void            del(J3ObjectHandle* handle);
+	};
+
 	class J3Value {
 	public:
 		union {

Modified: vmkit/branches/mcjit/include/vmkit/stack.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/vmkit/stack.h?rev=197884&r1=197883&r2=197884&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/vmkit/stack.h (original)
+++ vmkit/branches/mcjit/include/vmkit/stack.h Sun Dec 22 11:49:48 2013
@@ -45,6 +45,10 @@ namespace vmkit {
 				createNode(capacity);
 		}
 
+		bool isEmpty() {
+			return head->top == (T*)(head+1) && !head->nextBusy;
+		}
+
 		T* push() {
 			T* res = head->top++;
 
@@ -58,13 +62,14 @@ namespace vmkit {
 			return push();
 		}
 
-		void pop() {
+		T* pop() {
 			T* res = head->top - 1;
 			if(res < (T*)(head + 1)) {
 				head = head->nextBusy;
 				head->top = (T*)(head+1);		
 			} else
 				head->top = res;
+			return head->top;
 		}
 
 		T* tell() { 

Modified: vmkit/branches/mcjit/lib/j3/vm/j3.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/j3/vm/j3.cc?rev=197884&r1=197883&r2=197884&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3.cc Sun Dec 22 11:49:48 2013
@@ -120,7 +120,7 @@ J3ObjectHandle* J3::arrayToString(J3Obje
 	J3ObjectHandle* res = charArrayToStrings[array];
 	if(!res) {
 		J3ObjectHandle* prev = J3Thread::get()->tell();
-		res = initialClassLoader->globalReferences()->push(J3ObjectHandle::doNewObject(stringClass));
+		res = initialClassLoader->globalReferences()->add(J3ObjectHandle::doNewObject(stringClass));
 		J3Thread::get()->restore(prev);
 
 		stringInit->invokeSpecial(res, array, 0);
@@ -136,7 +136,7 @@ J3ObjectHandle* J3::nameToString(const v
 	J3ObjectHandle* res = nameToCharArrays[name];
 	if(!res) {
 		J3ObjectHandle* prev = J3Thread::get()->tell();
-		res = initialClassLoader->globalReferences()->push(J3ObjectHandle::doNewArray(charArrayClass, name->length()));
+		res = initialClassLoader->globalReferences()->add(J3ObjectHandle::doNewArray(charArrayClass, name->length()));
 		J3Thread::get()->restore(prev);
 
 		for(uint32_t i=0; i<name->length(); i++)

Modified: vmkit/branches/mcjit/lib/j3/vm/j3class.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/j3/vm/j3class.cc?rev=197884&r1=197883&r2=197884&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3class.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3class.cc Sun Dec 22 11:49:48 2013
@@ -146,7 +146,7 @@ J3ObjectHandle* J3ObjectType::javaClass(
 		if(!_javaClass) {
 			J3ObjectHandle* prev = J3Thread::get()->tell();
 			_javaClass = J3ObjectHandle::doNewObject(loader()->vm()->classClass);
-			_javaClass = loader()->globalReferences()->push(_javaClass);
+			_javaClass = loader()->globalReferences()->add(_javaClass);
 			J3Thread::get()->restore(prev);
 			_javaClass->setLong(loader()->vm()->classVMData, (int64_t)(uintptr_t)this);
 			loader()->vm()->classInit->invokeSpecial(_javaClass);
@@ -291,7 +291,7 @@ void J3Class::doInitialise() {
 		J3ObjectHandle* stacked = J3ObjectHandle::allocate(staticLayout.vt(),
 																											 loader()->vm()->dataLayout()->getTypeAllocSize(staticLayout.llvmType()
 																																																			->getContainedType(0)));
-		_staticInstance = loader()->globalReferences()->push(stacked);
+		_staticInstance = loader()->globalReferences()->add(stacked);
 		J3Thread::get()->restore(prev);
 
 		for(size_t i=0; i<staticLayout.nbFields; i++) {

Modified: vmkit/branches/mcjit/lib/j3/vm/j3jni.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/j3/vm/j3jni.cc?rev=197884&r1=197883&r2=197884&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3jni.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3jni.cc Sun Dec 22 11:49:48 2013
@@ -55,8 +55,24 @@ void JNICALL FatalError(JNIEnv* env, con
 jint JNICALL PushLocalFrame(JNIEnv* env, jint capacity) { enterJVM(); leaveJVM(); NYI(); }
 jobject JNICALL PopLocalFrame(JNIEnv* env, jobject result) { enterJVM(); leaveJVM(); NYI(); }
 
-jobject JNICALL NewGlobalRef(JNIEnv* env, jobject lobj) { enterJVM(); leaveJVM(); NYI(); }
-void JNICALL DeleteGlobalRef(JNIEnv* env, jobject gref) { enterJVM(); leaveJVM(); NYI(); }
+jobject JNICALL NewGlobalRef(JNIEnv* env, jobject lobj) { 
+	jobject res;
+	enterJVM(); 
+	J3Method* m = J3Thread::get()->getJavaCaller();
+	J3ClassLoader* loader = m ? m->cl()->loader() : J3Thread::get()->vm()->initialClassLoader;
+	res = loader->globalReferences()->add(lobj);
+	leaveJVM(); 
+	return res;
+}
+
+void JNICALL DeleteGlobalRef(JNIEnv* env, jobject gref) { 
+	enterJVM(); 
+	J3Method* m = J3Thread::get()->getJavaCaller();
+	J3ClassLoader* loader = m ? m->cl()->loader() : J3Thread::get()->vm()->initialClassLoader;
+	loader->globalReferences()->del(gref);
+	leaveJVM(); 
+}
+
 void JNICALL DeleteLocalRef(JNIEnv* env, jobject obj) { 
 	enterJVM(); 
 	if(obj) obj->harakiri();

Modified: vmkit/branches/mcjit/lib/j3/vm/j3object.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/j3/vm/j3object.cc?rev=197884&r1=197883&r2=197884&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3object.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3object.cc Sun Dec 22 11:49:48 2013
@@ -402,4 +402,27 @@ J3ObjectHandle* J3LocalReferences::push(
 	return res;
 }
 
+/*
+ *  J3GlobalReferences
+ */
+J3GlobalReferences::J3GlobalReferences(vmkit::BumpAllocator* _allocator) :
+	references(_allocator),
+	emptySlots(_allocator) {
+	pthread_mutex_init(&mutex, 0);
+}
+		
+J3ObjectHandle* J3GlobalReferences::add(J3ObjectHandle* handle) {
+	pthread_mutex_lock(&mutex);
+	J3ObjectHandle* res = emptySlots.isEmpty() ? references.push() : *emptySlots.pop();
+	res->_obj = handle->_obj;
+	pthread_mutex_unlock(&mutex);
+	return res;
+}
+
+void J3GlobalReferences::del(J3ObjectHandle* handle) {
+	handle->harakiri();
+	pthread_mutex_lock(&mutex);
+	*emptySlots.push() = handle;
+	pthread_mutex_unlock(&mutex);
+}
 





More information about the vmkit-commits mailing list