From 93748f2df94168aa60d3b04d5d1d1f8238b8c518 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Tue, 10 Jul 2007 19:38:06 -0600 Subject: [PATCH] heap o' bugfixes --- classpath/TestThreads.java | 16 +++++------ classpath/java/lang/StringBuilder.java | 2 +- makefile | 2 +- src/builtin.cpp | 40 +++++++++++++++++++------- src/machine.cpp | 9 +++--- src/machine.h | 4 +-- src/main.cpp | 7 +++++ src/run.cpp | 15 +++------- src/system.h | 2 ++ src/types.def | 2 +- 10 files changed, 60 insertions(+), 39 deletions(-) diff --git a/classpath/TestThreads.java b/classpath/TestThreads.java index 025f944790..8a7ac9a28d 100644 --- a/classpath/TestThreads.java +++ b/classpath/TestThreads.java @@ -20,15 +20,15 @@ public class TestThreads implements Runnable { synchronized (this) { int i = 0; try { - System.out.println("I'm running in a seperate thread!"); + System.out.println("I'm running in a separate thread!"); - final int arrayCount = 8; - final int arraySize = 4; - System.out.println("Allocating and discarding " + arrayCount + - " arrays of " + arraySize + "MB each"); - for (; i < arrayCount; ++i) { - byte[] array = new byte[arraySize * 1024 * 1024]; - } +// final int arrayCount = 8; +// final int arraySize = 4; +// System.out.println("Allocating and discarding " + arrayCount + +// " arrays of " + arraySize + "MB each"); +// for (; i < arrayCount; ++i) { +// byte[] array = new byte[arraySize * 1024 * 1024]; +// } long nap = 5; System.out.println("sleeping for " + nap + " seconds"); diff --git a/classpath/java/lang/StringBuilder.java b/classpath/java/lang/StringBuilder.java index f9b2260370..4e4524ea18 100644 --- a/classpath/java/lang/StringBuilder.java +++ b/classpath/java/lang/StringBuilder.java @@ -27,7 +27,7 @@ public class StringBuilder { index -= c.value.length(); c.value.getChars(0, c.value.length(), array, index); } - return new String(array, 0, array.length, false); + return new String(array, 0, length, false); } private static class Cell { diff --git a/makefile b/makefile index 9834f52854..2e528dbcfa 100644 --- a/makefile +++ b/makefile @@ -29,7 +29,7 @@ thread-cflags = -pthread thread-lflags = -lpthread cflags = $(warnings) -fPIC -fno-rtti -fno-exceptions -fvisibility=hidden \ - -I$(src) -I$(bld) $(thread-cflags) + -I$(src) -I$(bld) $(thread-cflags) -D__STDC_LIMIT_MACROS lflags = $(thread-lflags) -ldl test-cflags = -DDEBUG_MEMORY stress-cflags = -DDEBUG_MEMORY -DDEBUG_MEMORY_MAJOR diff --git a/src/builtin.cpp b/src/builtin.cpp index 901ab309f9..869e13f7d1 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -34,6 +34,16 @@ notifyAll(Thread* t, jobject this_) vm::notifyAll(t, *this_); } +void +sleep(Thread* t, jlong milliseconds) +{ + if (milliseconds == 0) milliseconds = INT64_MAX; + + ENTER(t, Thread::IdleState); + + t->vm->system->sleep(milliseconds); +} + void loadLibrary(Thread* t, jstring nameString) { @@ -68,7 +78,7 @@ arraycopy(Thread* t, jobject src, jint srcOffset, jobject dst, jint dstOffset, unsigned elementSize = classArrayElementSize(t, objectClass(t, s)); if (LIKELY(elementSize)) { - unsigned offset = 0; + unsigned offset = 1; if (objectClass(t, s) == arrayBody(t, t->vm->types, Machine::ObjectArrayType)) @@ -76,7 +86,7 @@ arraycopy(Thread* t, jobject src, jint srcOffset, jobject dst, jint dstOffset, if (LIKELY(objectArrayElementClass(t, s) == objectArrayElementClass(t, d))) { - offset = 1; + offset = 2; } else { t->exception = makeArrayStoreException(t); return; @@ -86,12 +96,12 @@ arraycopy(Thread* t, jobject src, jint srcOffset, jobject dst, jint dstOffset, intptr_t sl = cast(s, offset * BytesPerWord); intptr_t dl = cast(d, offset * BytesPerWord); if (LIKELY(srcOffset >= 0 and srcOffset + length <= sl and - dstOffset >= 0 and dstOffset + length < dl)) + dstOffset >= 0 and dstOffset + length <= dl)) { - uint8_t* sbody = &cast(s, (offset * BytesPerWord) + 4); - uint8_t* dbody = &cast(s, (offset * BytesPerWord) + 4); - memcpy(sbody + (srcOffset * elementSize), - dbody + (dstOffset * elementSize), + uint8_t* sbody = &cast(s, (offset + 1) * BytesPerWord); + uint8_t* dbody = &cast(d, (offset + 1) * BytesPerWord); + memcpy(dbody + (dstOffset * elementSize), + sbody + (srcOffset * elementSize), length * elementSize); return; } @@ -143,7 +153,7 @@ start(Thread* t, jobject this_) class Runnable: public System::Runnable { public: - Runnable(Thread* t): t(t) { } + Runnable(System* s, Thread* t): s(s), t(t) { } virtual void run(System::Thread* st) { t->systemThread = st; @@ -153,10 +163,16 @@ start(Thread* t, jobject this_) t->exit(); } - Thread* t; - } r(p); + virtual void dispose() { + s->free(this); + } - if (not t->vm->system->success(t->vm->system->start(&r))) { + System* s; + Thread* t; + }* r = new (t->vm->system->allocate(sizeof(Runnable))) + Runnable(t->vm->system, p); + + if (not t->vm->system->success(t->vm->system->start(r))) { p->exit(); object message = makeString(t, "unable to start native thread"); @@ -179,6 +195,8 @@ populate(Thread* t, object map) reinterpret_cast(notify) }, { "Java_java_lang_Object_notifyAll", reinterpret_cast(notifyAll) }, + { "Java_java_lang_Thread_sleep", + reinterpret_cast(sleep) }, { "Java_java_lang_System_loadLibrary", reinterpret_cast(loadLibrary) }, { "Java_java_lang_System_arraycopy", diff --git a/src/machine.cpp b/src/machine.cpp index 1ffca8b4ac..14174e97d2 100644 --- a/src/machine.cpp +++ b/src/machine.cpp @@ -431,8 +431,9 @@ collect(Thread* t, Heap::CollectionType type) void removeMonitor(Thread* t, object o) { - abort(t); - hashMapRemove(t, t->vm->monitorMap, o, objectHash, referenceEqual); + object p = hashMapRemove + (t, t->vm->monitorMap, o, objectHash, referenceEqual); + static_cast(pointerValue(t, p))->dispose(); } object @@ -770,7 +771,7 @@ makeString(Thread* t, const char* format, ...) object s = ::makeByteArray(t, format, a); va_end(a); - return makeString(t, s, 0, byteArrayLength(t, s), 0); + return makeString(t, s, 0, byteArrayLength(t, s) - 1, 0); } void @@ -882,7 +883,7 @@ hashMapRemove(Thread* t, object map, object key, object p = 0; while (n) { if (equal(t, key, tripleFirst(t, n))) { - o = tripleFirst(t, n); + o = tripleSecond(t, n); if (p) { set(t, tripleThird(t, p), tripleThird(t, n)); } else { diff --git a/src/machine.h b/src/machine.h index 46d6042578..41a618c455 100644 --- a/src/machine.h +++ b/src/machine.h @@ -1439,7 +1439,7 @@ pushLong(Thread* t, uint64_t v) } pushInt(t, v >> 32); - pushInt(t, v & 0xFF); + pushInt(t, v & 0xFFFFFFFF); } inline object @@ -1555,7 +1555,7 @@ pokeLong(Thread* t, unsigned index, uint64_t value) } pokeInt(t, index, value >> 32); - pokeInt(t, index + 1, value & 0xFF); + pokeInt(t, index + 1, value & 0xFFFFFFFF); } inline object* diff --git a/src/main.cpp b/src/main.cpp index 1f5e13a0d4..bccbc6a1f4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -139,6 +139,7 @@ class System: public vm::System { } virtual void dispose() { + r->dispose(); s->free(this); } @@ -357,6 +358,12 @@ class System: public vm::System { return 0; } + virtual void sleep(int64_t milliseconds) { + timespec ts = { milliseconds / 1000, (milliseconds % 1000) * 1000 * 1000 }; + + nanosleep(&ts, 0); + } + virtual uint64_t call(void* function, uintptr_t* arguments, uint8_t* types, unsigned count, unsigned size, unsigned returnType) { diff --git a/src/run.cpp b/src/run.cpp index 9623c9dc97..2f942407c3 100644 --- a/src/run.cpp +++ b/src/run.cpp @@ -87,14 +87,6 @@ make(Thread* t, object class_) memset(static_cast(instance) + 1, 0, sizeInBytes - sizeof(object)); - fprintf(stderr, "new instance: %p\n", instance); - - if (class_ == arrayBody(t, t->vm->types, Machine::ThreadType)) { - if (threadPeer(t, instance)) { - fprintf(stderr, "yo!\n"); - } - } - return instance; } @@ -390,7 +382,8 @@ parsePool(Thread* t, Stream& s) case CONSTANT_String: { object bytes = arrayBody(t, pool, intArrayBody(t, o, 1) - 1); - object value = makeString(t, bytes, 0, byteArrayLength(t, bytes), 0); + object value = makeString + (t, bytes, 0, byteArrayLength(t, bytes) - 1, 0); set(t, arrayBody(t, pool, i), value); } break; @@ -2377,7 +2370,7 @@ run(Thread* t) if (isSpecialMethod(t, method, class_)) { class_ = classSuper(t, class_); - if (UNLIKELY(classVirtualTable(t, class_) == 0)) { + if (classVirtualTable(t, class_) == 0) { PROTECT(t, class_); resolveClass(t, className(t, class_)); @@ -2435,7 +2428,7 @@ run(Thread* t) if (LIKELY(peekObject(t, sp - parameterFootprint))) { object class_ = objectClass(t, peekObject(t, sp - parameterFootprint)); - if (UNLIKELY(classVirtualTable(t, class_) == 0)) { + if (classVirtualTable(t, class_) == 0) { PROTECT(t, class_); resolveClass(t, className(t, class_)); diff --git a/src/system.h b/src/system.h index 3f875402bb..789f615528 100644 --- a/src/system.h +++ b/src/system.h @@ -29,6 +29,7 @@ class System: public Allocator { public: virtual ~Runnable() { } virtual void run(Thread*) = 0; + virtual void dispose() = 0; }; class Monitor { @@ -58,6 +59,7 @@ class System: public Allocator { virtual Status attach(Thread**) = 0; virtual Status start(Runnable*) = 0; virtual Status make(Monitor**) = 0; + virtual void sleep(int64_t milliseconds) = 0; virtual uint64_t call(void* function, uintptr_t* arguments, uint8_t* types, unsigned count, unsigned size, unsigned returnType) = 0; diff --git a/src/types.def b/src/types.def index 3e3a6b9724..f83fbfe873 100644 --- a/src/types.def +++ b/src/types.def @@ -145,7 +145,7 @@ java/lang/ArrayIndexOutOfBoundsException (extends runtimeException)) -(type arrayStoreException java/lang/arrayStoreException +(type arrayStoreException java/lang/ArrayStoreException (extends runtimeException)) (type negativeArraySizeException java/lang/NegativeArraySizeException