[vmkit-commits] [vmkit] r197882 - defines a stack class and uses it to implement J3FixedPoint

Gael Thomas gael.thomas at lip6.fr
Sun Dec 22 09:07:23 PST 2013


Author: gthomas
Date: Sun Dec 22 11:07:23 2013
New Revision: 197882

URL: http://llvm.org/viewvc/llvm-project?rev=197882&view=rev
Log:
defines a stack class and uses it to implement J3FixedPoint

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

Modified: vmkit/branches/mcjit/include/j3/j3object.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/j3/j3object.h?rev=197882&r1=197881&r2=197882&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/j3/j3object.h (original)
+++ vmkit/branches/mcjit/include/j3/j3object.h Sun Dec 22 11:07:23 2013
@@ -4,6 +4,8 @@
 #include <pthread.h>
 #include <stdint.h>
 
+#include "vmkit/stack.h"
+
 #include "j3/j3typesdef.h"
 
 namespace vmkit {
@@ -150,35 +152,15 @@ namespace j3 {
 #undef defAccessor
 	};
 
-	class J3FixedPointNode {
-	public:
-		J3FixedPointNode* nextFree;
-		J3FixedPointNode* nextBusy;
-		J3ObjectHandle*   top;
-		J3ObjectHandle*   max;
-	};
-
-	class J3FixedPoint {
-		static const uint32_t defaultNodeCapacity = 256;
-
-		pthread_mutex_t       mutex;
-		vmkit::BumpAllocator* allocator;
-		J3FixedPointNode*     head;
-
-		void createNode(uint32_t capacity=defaultNodeCapacity);
+	class J3FixedPoint : public vmkit::Stack<J3ObjectHandle> {
 	public:
-		J3FixedPoint(vmkit::BumpAllocator* _allocator);
-
-		void            unsyncEnsureCapacity(uint32_t capacity);
+		J3FixedPoint(vmkit::BumpAllocator* _allocator) : vmkit::Stack<J3ObjectHandle>(_allocator) {}
 
 		J3ObjectHandle* syncPush(J3ObjectHandle* handle) { return syncPush(handle->obj()); }
-		J3ObjectHandle* syncPush(J3Object* obj);
 		J3ObjectHandle* unsyncPush(J3ObjectHandle* handle) { return unsyncPush(handle->obj()); }
-		J3ObjectHandle* unsyncPush(J3Object* obj);
-		void            unsyncPop();
 
-		J3ObjectHandle* unsyncTell() { return head->top; }
-		void            unsyncRestore(J3ObjectHandle* ptr);
+		J3ObjectHandle* syncPush(J3Object* obj);
+		J3ObjectHandle* unsyncPush(J3Object* obj);
 	};
 
 	class J3Value {

Added: vmkit/branches/mcjit/include/vmkit/stack.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/vmkit/stack.h?rev=197882&view=auto
==============================================================================
--- vmkit/branches/mcjit/include/vmkit/stack.h (added)
+++ vmkit/branches/mcjit/include/vmkit/stack.h Sun Dec 22 11:07:23 2013
@@ -0,0 +1,100 @@
+#ifndef _STACK_H_
+#define _STACK_H_
+
+#include "vmkit/allocator.h"
+
+namespace vmkit {
+	template <class T>
+	class StackNode {
+	public:
+		StackNode<T>* nextFree;
+		StackNode<T>* nextBusy;
+		T*            top;
+		T*            max;
+	};
+
+	template <class T>
+	class Stack {
+		static const uint32_t defaultNodeCapacity = 256;
+
+		pthread_mutex_t mutex;
+		BumpAllocator*  allocator;
+		StackNode<T>*   head;
+
+		void createNode(uint32_t capacity=defaultNodeCapacity) {
+			uint64_t size = capacity * sizeof(T) + sizeof(StackNode<T>);
+			StackNode<T>* nn = (StackNode<T>*)allocator->allocate(size);
+			nn->top = (T*)(nn + 1);
+			nn->max = (T*)((uintptr_t)nn + size);
+			nn->nextFree = 0;
+			nn->nextBusy = head;
+			if(head)
+				head->nextFree = nn;
+			head = nn;
+		}
+
+	public:
+		Stack(BumpAllocator* _allocator) {
+			pthread_mutex_init(&mutex, 0);
+			allocator = _allocator;
+			head = 0;
+			createNode();
+		}
+
+		void unsyncEnsureCapacity(uint32_t capacity) {
+			T* reserve = head->top + capacity;
+			if(reserve > head->max)
+				createNode(capacity);
+		}
+
+		T* syncPush() { 
+			StackNode<T>* cur = head;
+			T* res = (T*)__sync_fetch_and_add((uintptr_t*)&cur->top, (uintptr_t)sizeof(T));
+
+			if(res < cur->max)
+				return res;
+
+			pthread_mutex_lock(&mutex);
+			if(cur->nextFree)
+				head = cur->nextFree;
+			else
+				createNode();
+			pthread_mutex_unlock(&mutex);
+			return syncPush();
+		}
+
+		T* unsyncPush() {
+			T* res = head->top++;
+
+			if(res < head->max)
+				return res;
+
+			if(head->nextFree)
+				head = head->nextFree;
+			else
+				createNode();
+			return unsyncPush();
+		}
+
+		void unsyncPop() {
+			T* res = head->top - 1;
+			if(res < (T*)(head + 1)) {
+				head = head->nextBusy;
+				head->top = (T*)(head+1);		
+			} else
+				head->top = res;
+		}
+
+		T* unsyncTell() { 
+			return head->top; 
+		}
+
+		void unsyncRestore(T* ptr) {
+			while(ptr <= (T*)head || ptr > head->max)
+				head = head->nextBusy;
+			head->top = ptr;
+		}
+	};
+}
+
+#endif

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=197882&r1=197881&r2=197882&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3jni.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3jni.cc Sun Dec 22 11:07:23 2013
@@ -267,7 +267,6 @@ jmethodID JNICALL GetStaticMethodID(JNIE
 	enterJVM(); 
 	
 	J3ObjectType* cl = J3ObjectType::nativeClass(clazz);
-	fprintf(stderr, "cl: %p %p\n", clazz, cl);
 	cl->initialise();
 	vmkit::Names* n = cl->loader()->vm()->names();
 	res = cl->findStaticMethod(n->get(name), n->get(sig));

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=197882&r1=197881&r2=197882&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3object.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3object.cc Sun Dec 22 11:07:23 2013
@@ -396,79 +396,16 @@ void* J3ObjectHandle::trampoline(J3Objec
 /*
  *  J3FixedPoint
  */
-J3FixedPoint::J3FixedPoint(vmkit::BumpAllocator* _allocator) {
-	pthread_mutex_init(&mutex, 0);
-	allocator = _allocator;
-	head = 0;
-	createNode();
-}
-
-void J3FixedPoint::unsyncEnsureCapacity(uint32_t capacity) {
-	J3ObjectHandle* reserve = head->top + capacity;
-	if(reserve > head->max)
-		createNode(capacity);
-}
-
-void J3FixedPoint::createNode(uint32_t capacity) {
-	uint64_t size = capacity * sizeof(J3Object*) + sizeof(J3FixedPointNode);
-	J3FixedPointNode* nn = (J3FixedPointNode*)allocator->allocate(size);
-	nn->top = (J3ObjectHandle*)(nn + 1);
-	nn->max = (J3ObjectHandle*)((uintptr_t)nn + size);
-	nn->nextFree = 0;
-	nn->nextBusy = head;
-	if(head)
-		head->nextFree = nn;
-	head = nn;
-}
-
 J3ObjectHandle* J3FixedPoint::syncPush(J3Object* obj) {
-	J3FixedPointNode* cur = head;
-	J3ObjectHandle* res = (J3ObjectHandle*)__sync_fetch_and_add((uintptr_t*)&cur->top, (uintptr_t)sizeof(J3ObjectHandle));
-
-	if(res >= cur->max) {
-		pthread_mutex_lock(&mutex);
-		if(cur->nextFree)
-			head = cur->nextFree;
-		else
-			createNode();
-		pthread_mutex_unlock(&mutex);
-		return syncPush(obj);
-	} else {
-		res->_obj = obj;
-		return res;
-	}
+	J3ObjectHandle* res = Stack<J3ObjectHandle>::syncPush();
+	res->_obj = obj;
+	return res;
 }
 
 J3ObjectHandle* J3FixedPoint::unsyncPush(J3Object* obj) {
-	J3ObjectHandle* res = head->top++;
-
-	if(res >= head->max) {
-		if(head->nextFree)
-			head = head->nextFree;
-		else
-			createNode();
-		return unsyncPush(obj);
-	} else {
-		res->_obj = obj;
-		return res;
-	}
-}
-
-void J3FixedPoint::unsyncPop() {
-	J3ObjectHandle* res = head->top - 1;
-	if(res < (J3ObjectHandle*)(head + 1)) {
-		head = head->nextBusy;
-		head->top = (J3ObjectHandle*)(head+1);		
-	} else
-		head->top = res;
-}
-
-void J3FixedPoint::unsyncRestore(J3ObjectHandle* obj) {
-	while(obj <= (J3ObjectHandle*)head || obj > head->max) {
-		head = head->nextBusy;
-		head->top = (J3ObjectHandle*)(head+1);
-	}
-	head->top = obj;
+	J3ObjectHandle* res = Stack<J3ObjectHandle>::unsyncPush();
+	res->_obj = obj;
+	return res;
 }
 
 





More information about the vmkit-commits mailing list