CVS kaffe (robilad): made boehm-gc gc-refs.c implementation closer to kaffe-gc one
Kaffe CVS <[email protected]> Fri, 18 Jan 2008 15:10:53 -0800
| Newsgroups | gmane.comp.java.vm.kaffe.general |
|---|---|
| Message-ID | <[email protected]> |
PatchSet 7690 Date: 2008/01/18 23:08:51 Author: robilad Branch: HEAD Tag: (none) Log: made boehm-gc gc-refs.c implementation closer to kaffe-gc one 2008-01-19 Dalibor Topic <[email protected]> * kaffe/kaffevm/boehm-gc/gc-refs.c: Added debugging code from kaffe-gc version. Use KaffeGC_malloc & KaffeGC_free instead of boehm-specific macros. Rearranged functions to match layout in kaffe-gc version in preparation for unifying them again. Members: ChangeLog:1.5192->1.5193 kaffe/kaffevm/boehm-gc/gc-refs.c:1.9->1.10 Index: kaffe/ChangeLog diff -u kaffe/ChangeLog:1.5192 kaffe/ChangeLog:1.5193 --- kaffe/ChangeLog:1.5192 Fri Jan 18 22:19:54 2008 +++ kaffe/ChangeLog Fri Jan 18 23:08:51 2008 @@ -1,3 +1,10 @@ +2008-01-19 Dalibor Topic <[email protected]> + + * kaffe/kaffevm/boehm-gc/gc-refs.c: Added debugging code from kaffe-gc + version. Use KaffeGC_malloc & KaffeGC_free instead of boehm-specific + macros. Rearranged functions to match layout in kaffe-gc version in + preparation for unifying them again. + 2008-01-18 Dalibor Topic <[email protected]> * kaffe/kaffevm/Makefile.am (DIST_SUBDIRS): Removed jni and verifier dirs. Index: kaffe/kaffe/kaffevm/boehm-gc/gc-refs.c diff -u kaffe/kaffe/kaffevm/boehm-gc/gc-refs.c:1.9 kaffe/kaffe/kaffevm/boehm-gc/gc-refs.c:1.10 --- kaffe/kaffe/kaffevm/boehm-gc/gc-refs.c:1.9 Tue Aug 7 01:44:49 2007 +++ kaffe/kaffe/kaffevm/boehm-gc/gc-refs.c Fri Jan 18 23:08:53 2008 @@ -27,8 +27,6 @@ #include "java_lang_Thread.h" #include "gc2.h" -#include <gc/gc.h> - #define REFOBJHASHSZ 128 typedef struct _strongRefObject { const void* mem; @@ -71,6 +69,10 @@ uint32 idx; strongRefObject* obj; + + DBG(REFERENCE, dprintf("Adding persistent reference for object %p\n", + mem); ); + idx = REFOBJHASH(mem); for (obj = strongRefObjects.hash[idx]; obj != 0; obj = obj->next) { /* Found it - just increase reference */ @@ -81,7 +83,7 @@ } /* Not found - create a new one */ - obj = (strongRefObject*)GC_MALLOC_UNCOLLECTABLE(sizeof(strongRefObject)); + obj = (strongRefObject*)KGC_malloc(collector, sizeof(strongRefObject), KGC_ALLOC_REF); if (!obj) return false; @@ -94,6 +96,44 @@ return true; } +/* + * Remove a persistent reference to an object. If the count becomes + * zero then the reference is removed. + */ +bool +KaffeGC_rmRef(Collector *collector UNUSED, void* mem) +{ + uint32 idx; + strongRefObject** objp; + strongRefObject* obj; + + + DBG(REFERENCE, dprintf("Removing persistent reference for object %p\n", + mem); ); + + idx = REFOBJHASH(mem); + mem = ALIGN_BACKWARD(mem); + + lockStaticMutex(&strongRefLock); + for (objp = &strongRefObjects.hash[idx]; *objp != 0; objp = &obj->next) { + obj = *objp; + /* Found it - just decrease reference */ + if (obj->mem == mem) { + obj->ref--; + if (obj->ref == 0) { + *objp = obj->next; + KGC_free(collector, obj); + } + unlockStaticMutex(&strongRefLock); + return true; + } + } + unlockStaticMutex(&strongRefLock); + + /* Not found!! */ + return false; +} + /** * Grow the weak reference list for a weakly referenced object. * Assert: weakRefLock is held by the calling thread. @@ -121,7 +161,7 @@ { previousSize = obj->allRefSize; unlockStaticMutex(&weakRefLock); - refs = GC_MALLOC_UNCOLLECTABLE(size * sizeof(void **)); + refs = KGC_malloc(collector, size * sizeof(void **), KGC_ALLOC_VMWEAKREF); lockStaticMutex(&weakRefLock); if (refs == NULL) { @@ -133,7 +173,7 @@ if (previousSize != obj->allRefSize) { unlockStaticMutex(&weakRefLock); - GC_FREE(refs); + KGC_free(collector, refs); lockStaticMutex(&weakRefLock); continue; } @@ -147,7 +187,7 @@ memcpy(refs, oldRefs, sizeof(void **) * obj->ref); unlockStaticMutex(&weakRefLock); - GC_FREE(oldRefs); + KGC_free(collector, oldRefs); lockStaticMutex(&weakRefLock); } @@ -157,82 +197,90 @@ while (1); } -/* - * Remove a persistent reference to an object. If the count becomes - * zero then the reference is removed. - */ -bool -KaffeGC_rmRef(Collector *collector UNUSED, void* mem) + +static weakRefObject * +findWeakRefObject(void *mem) { - uint32 idx; - strongRefObject** objp; - strongRefObject* obj; + int idx; + weakRefObject* obj; idx = REFOBJHASH(mem); - mem = ALIGN_BACKWARD(mem); - lockStaticMutex(&strongRefLock); - for (objp = &strongRefObjects.hash[idx]; *objp != 0; objp = &obj->next) { - obj = *objp; - /* Found it - just decrease reference */ - if (obj->mem == mem) { - obj->ref--; - if (obj->ref == 0) { - *objp = obj->next; - GC_FREE(obj); - } - unlockStaticMutex(&strongRefLock); - return true; - } + for (obj = weakRefObjects.hash[idx]; obj != 0; obj = obj->next) { + /* Found it - just register a new weak reference */ + if (obj->mem == mem) + return obj; } - unlockStaticMutex(&strongRefLock); - /* Not found!! */ - return false; + return NULL; +} + +static bool +insertInWeakRef(Collector *collector, weakRefObject *obj, void **refobj) +{ + obj->ref++; + + if (obj->ref >= obj->allRefSize) + if (!resizeWeakReferenceObject(collector, obj, obj->ref * 2 + 1)) + return false; + + obj->allRefs[obj->ref-1] = refobj; + + return true; } bool KaffeGC_addWeakRef(Collector *collector, void* mem, void** refobj) { + weakRefObject* obj, *obj2; int idx; - weakRefObject* obj; - idx = REFOBJHASH(mem); + DBG(REFERENCE, dprintf("Adding weak reference for object %p\n", + mem); ); lockStaticMutex(&weakRefLock); - for (obj = weakRefObjects.hash[idx]; obj != 0; obj = obj->next) { - /* Found it - just register a new weak reference */ - if (obj->mem == mem) { - obj->ref++; - - if (obj->ref >= obj->allRefSize) - if (!resizeWeakReferenceObject(collector, obj, obj->ref * 2 + 1)) - { - unlockStaticMutex(&weakRefLock); - return false; - } - - obj->allRefs[obj->ref-1] = refobj; - + obj = findWeakRefObject(mem); + if (obj != NULL) + { + bool ret = insertInWeakRef(collector, obj, refobj); + unlockStaticMutex(&weakRefLock); - return true; + return ret; } - } /* Not found - create a new one */ - obj = (weakRefObject*)GC_MALLOC_UNCOLLECTABLE(sizeof(weakRefObject)); + unlockStaticMutex(&weakRefLock); + obj = (weakRefObject*)KGC_malloc(collector, sizeof(weakRefObject), KGC_ALLOC_VMWEAKREF); if (obj == NULL) - { - unlockStaticMutex(&weakRefLock); - return false; - } + return false; obj->mem = mem; obj->ref = 1; - unlockStaticMutex(&weakRefLock); - obj->allRefs = (void ***)GC_MALLOC_UNCOLLECTABLE(sizeof(void ***)); + obj->allRefs = (void ***)KGC_malloc(collector, sizeof(void ***), KGC_ALLOC_VMWEAKREF); lockStaticMutex(&weakRefLock); obj->allRefs[0] = refobj; + + /* Now we check whether has inserted the reference + * in the meantime. + */ + obj2 = findWeakRefObject(mem); + if (obj2 != NULL) + { + bool ret; + + /* Calling free is safe as the GC thread is not + * called at that time. + */ + KGC_free(collector, obj->allRefs); + KGC_free(collector, obj); + + ret = insertInWeakRef(collector, obj2, refobj); + unlockStaticMutex(&weakRefLock); + + return ret; + } + + idx = REFOBJHASH(mem); obj->next = weakRefObjects.hash[idx]; weakRefObjects.hash[idx] = obj; unlockStaticMutex(&weakRefLock); @@ -248,6 +296,10 @@ weakRefObject* obj; unsigned int i; + + DBG(REFERENCE, dprintf("Removing weak reference for object %p \n", + mem); ); + idx = REFOBJHASH(mem); lockStaticMutex(&weakRefLock); @@ -279,8 +331,8 @@ unlockStaticMutex(&weakRefLock); if (obj->allRefs != NULL) - GC_FREE(obj->allRefs); - GC_FREE(obj); + KGC_free(collector, obj->allRefs); + KGC_free(collector, obj); lockStaticMutex(&weakRefLock); } unlockStaticMutex(&weakRefLock); @@ -309,6 +361,9 @@ weakRefObject* obj; unsigned int i; + DBG(REFERENCE, dprintf("Clearing all weak references for object %p\n", + mem); ); + idx = REFOBJHASH(mem); lockStaticMutex(&weakRefLock); @@ -324,7 +379,7 @@ if (obj->allRefs != NULL) { - GC_FREE(obj->allRefs); + KGC_free(collector, obj->allRefs); obj->allRefs = NULL; } @@ -335,7 +390,7 @@ obj->next = NULL; obj->destroyed = true; if (obj->keep_object == 0) - GC_FREE(obj); + KGC_free(collector, obj); unlockStaticMutex(&weakRefLock); return;