aboutsummaryrefslogtreecommitdiff
path: root/gc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'gc.cpp')
-rw-r--r--gc.cpp62
1 files changed, 34 insertions, 28 deletions
diff --git a/gc.cpp b/gc.cpp
index 14f7edd..4df2d46 100644
--- a/gc.cpp
+++ b/gc.cpp
@@ -12,7 +12,7 @@ static EvalObj *gcq[GC_QUEUE_SIZE];
static Container *cyc_list[GC_QUEUE_SIZE];
GarbageCollector::GarbageCollector() {
- mapping.clear();
+ joined.clear();
pending_list = NULL;
resolve_threshold = GC_CYC_THRESHOLD;
}
@@ -22,21 +22,18 @@ GarbageCollector::PendingEntry::PendingEntry(
void GarbageCollector::expose(EvalObj *ptr) {
- bool flag = mapping.count(ptr);
- if (flag)
- {
+ if (ptr == NULL) return;
#ifdef GC_DEBUG
fprintf(stderr, "GC: 0x%llx exposed. count = %lu \"%s\"\n",
- (ull)ptr, mapping[ptr] - 1, ptr->ext_repr().c_str());
+ (ull)ptr, ptr->gc_get_cnt() - 1, ptr->ext_repr().c_str());
#endif
- if (!--mapping[ptr])
+ if (ptr->gc_dec())
{
#ifdef GC_DEBUG
fprintf(stderr, "GC: 0x%llx pending. \n", (ull)ptr);
#endif
pending_list = new PendingEntry(ptr, pending_list);
}
- }
}
void GarbageCollector::force() {
@@ -44,8 +41,9 @@ void GarbageCollector::force() {
for (PendingEntry *p = pending_list, *np; p; p = np)
{
np = p->next;
- if (mapping.count(p->obj) && mapping[p->obj] == 0)
- *r++ = p->obj;
+ EvalObj *obj = p->obj;
+ if (joined.count(obj) && obj->gc_get_cnt() == 0)
+ *r++ = obj;
delete p;
} // fetch the pending pointers in the list
// clear the list
@@ -55,7 +53,7 @@ void GarbageCollector::force() {
if (it->second == 0) *r++ = it->first;*/
#ifdef GC_INFO
- fprintf(stderr, "%ld\n", mapping.size());
+ fprintf(stderr, "%ld\n", joined.size());
size_t cnt = 0;
#endif
#ifdef GC_DEBUG
@@ -72,7 +70,6 @@ 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)
@@ -88,40 +85,42 @@ void GarbageCollector::force() {
#ifdef GC_INFO
fprintf(stderr, "GC: Forced clear, %lu objects are freed, "
"%lu remains\n"
- "=============================\n", cnt, mapping.size());
+ "=============================\n", cnt, joined.size());
#endif
#ifdef GC_DEBUG
- for (EvalObj2Int::iterator it = mapping.begin();
- it != mapping.end(); it++)
- fprintf(stderr, "%llx => %s\n", (ull)it->first, it->first->ext_repr().c_str());
+ for (EvalObjSet::iterator it = joined.begin();
+ it != joined.end(); it++)
+ fprintf(stderr, "%llx => %s\n", (ull)*it, (*it)->ext_repr().c_str());
#endif
}
EvalObj *GarbageCollector::attach(EvalObj *ptr) {
if (!ptr) return NULL; // NULL pointer
- bool flag = mapping.count(ptr);
+/* bool flag = mapping.count(ptr);
if (flag) mapping[ptr]++;
else mapping[ptr] = 1;
+ */
+ ptr->gc_inc();
#ifdef GC_DEBUG
fprintf(stderr, "GC: 0x%llx attached. count = %lu \"%s\"\n",
- (ull)ptr, mapping[ptr], ptr->ext_repr().c_str());
+ (ull)ptr, ptr->gc_get_cnt(), ptr->ext_repr().c_str());
#endif
- if (mapping.size() > GC_QUEUE_SIZE >> 1)
- force();
+/* if (mapping.size() > GC_QUEUE_SIZE >> 1)
+ force();*/
return ptr; // passing through
}
void GarbageCollector::cycle_resolve() {
- if (mapping.size() < resolve_threshold) return;
+// if (joined.size() < resolve_threshold) return;
EvalObjSet visited;
Container **clptr = cyc_list;
- for (EvalObj2Int::iterator it = mapping.begin();
- it != mapping.end(); it++)
- if (it->first->is_container())
+ for (EvalObjSet::iterator it = joined.begin();
+ it != joined.end(); it++)
+ if ((*it)->is_container())
{
- Container *p = static_cast<Container*>(it->first);
- (*clptr++ = p)->gc_refs = it->second; // init the count
+ Container *p = static_cast<Container*>(*it);
+ (*clptr++ = p)->gc_refs = (*it)->gc_get_cnt(); // init the count
p->keep = false;
}
@@ -144,7 +143,6 @@ void GarbageCollector::cycle_resolve() {
if (!(*p)->keep)
{
delete *p;
- mapping.erase(*p);
}
#ifdef GC_INFO
fprintf(stderr, "GC: cycle resolved.\n");
@@ -153,15 +151,23 @@ void GarbageCollector::cycle_resolve() {
void GarbageCollector::collect() {
force();
- if (mapping.size() < resolve_threshold) return;
+ if (joined.size() < resolve_threshold) return;
cycle_resolve();
force();
}
size_t GarbageCollector::get_remaining() {
- return mapping.size();
+ return joined.size();
}
void GarbageCollector::set_resolve_threshold(size_t new_thres) {
resolve_threshold = new_thres;
}
+
+void GarbageCollector::join(EvalObj *ptr) {
+ joined.insert(ptr);
+}
+
+void GarbageCollector::quit(EvalObj *ptr) {
+ joined.erase(ptr);
+}