aboutsummaryrefslogtreecommitdiff
path: root/gc.cpp
diff options
context:
space:
mode:
authorTeddy <ted.sybil@gmail.com>2013-08-12 21:34:55 +0800
committerTeddy <ted.sybil@gmail.com>2013-08-12 21:34:55 +0800
commitd4d9eee6bef1bc9169e765c9bf3d2382a70198c2 (patch)
tree045c3eff6ab6a83554ee2cb46651a19b144ff868 /gc.cpp
parent55d1072441582936d119ed04fd8c532c2760b9d4 (diff)
...
Diffstat (limited to 'gc.cpp')
-rw-r--r--gc.cpp30
1 files changed, 22 insertions, 8 deletions
diff --git a/gc.cpp b/gc.cpp
index a5fdc48..5aed8b8 100644
--- a/gc.cpp
+++ b/gc.cpp
@@ -13,39 +13,46 @@ GarbageCollector::GarbageCollector() {
mapping.clear();
pend_cnt = 0;
pending_list = NULL;
+ collecting = false;
}
GarbageCollector::PendingEntry::PendingEntry(
EvalObj *_obj, PendingEntry *_next) : obj(_obj), next(_next) {}
-void GarbageCollector::expose(EvalObj *ptr, bool delay) {
+void GarbageCollector::expose(EvalObj *ptr) {
bool flag = mapping.count(ptr);
if (flag)
{
- if (!--mapping[ptr] && !delay)
+#ifdef GC_DEBUG
+ fprintf(stderr, "GC: 0x%llx exposed. count = %lu \"%s\"\n",
+ (ull)ptr, mapping[ptr], ptr->ext_repr().c_str());
+#endif
+ if (!--mapping[ptr] && collecting)
{
#ifdef GC_DEBUG
fprintf(stderr, "GC: 0x%llx pending. \n", (ull)ptr);
#endif
pending_list = new PendingEntry(ptr, pending_list);
- if (++pend_cnt == GC_QUEUE_SIZE >> 1)
- force(); // the gc queue may overflow
- }
+ }
}
}
void GarbageCollector::force() {
EvalObj **l = gcq, **r = l;
- for (PendingEntry *p = pending_list, *np; p; p = np)
+/* for (PendingEntry *p = pending_list, *np; p; p = np)
{
np = p->next;
*r++ = p->obj;
delete p;
} // fetch the pending pointers in the list
// clear the list
- pending_list = NULL;
+ pending_list = NULL; */
+ for (EvalObj2Int::iterator it = mapping.begin();
+ it != mapping.end(); it++)
+ if (it->second == 0) *r++ = it->first;
+ collecting = true;
#ifdef GC_DEBUG
size_t cnt = 0;
fprintf(stderr, "GC: Forcing the clear process...\n");
@@ -57,6 +64,7 @@ void GarbageCollector::force() {
cnt++;
#endif
delete *l;
+ mapping.erase(*l);
// maybe it's a complex structure,
// so that more pointers are reported
for (PendingEntry *p = pending_list, *np; p; p = np)
@@ -70,8 +78,14 @@ void GarbageCollector::force() {
pending_list = NULL;
}
#ifdef GC_DEBUG
- fprintf(stderr, "GC: Forced clear, %lu objects are freed\n", cnt);
+ fprintf(stderr, "GC: Forced clear, %lu objects are freed, "
+ "%lu remains\n", cnt, mapping.size());
+/* for (EvalObj2Int::iterator it = mapping.begin();
+ it != mapping.end(); it++)
+ fprintf(stderr, "%llx => %lu\n", (ull)it->first, it->second);
+ */
#endif
+ collecting = false;
}
EvalObj *GarbageCollector::attach(EvalObj *ptr) {