/* Tuplr: A programming language * Copyright (C) 2008-2009 David Robillard * * Tuplr is free software: you can redistribute it and/or modify it under * the terms of the GNU Affero General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * Tuplr is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General * Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with Tuplr. If not, see . */ #include #include #include "tuplr.hpp" using namespace std; void* GC::alloc(size_t size) { void* ret = malloc(size); _heap.push_back((Object*)ret); return ret; } inline void mark(CEnv& cenv, const Object* obj) { if (!obj || obj->used) return; obj->used = true; const ATuple* tup = dynamic_cast(obj); if (tup) { FOREACH(ATuple::const_iterator, i, *tup) { mark(cenv, *i); mark(cenv, cenv.type(*i)); } } } void GC::collect(CEnv& cenv, const Roots& roots) { for (Roots::const_iterator i = roots.begin(); i != roots.end(); ++i) mark(cenv, *i); for (Heap::iterator i = _heap.begin(); i != _heap.end();) { Heap::iterator next = i; ++next; if ((*i)->used) { (*i)->used = false; } else { AType* t = dynamic_cast(*i); // Don't delete types that are keys in the current type substitution if (!t || cenv.tsubst.find(t) == cenv.tsubst.end()) { (*i)->~Object(); free(*i); _heap.erase(i); } } i = next; } }