/* vim: set ts=4 sw=4 tw=99 et:
 *
 * Copyright (C) 2004-2012 David Anderson
 *
 * This file is part of SourcePawn.
 *
 * SourcePawn is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 * 
 * SourcePawn 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
 */
#include "GlobalRoots.h"
#include "Zone.h"
#include "Spaces-inl.h"
#include "Heap-inl.h"

using namespace ke;

GlobalRoots::GlobalRoots(Zone *zone)
{
}

GlobalRoots::~GlobalRoots()
{
    InlineList<HeapRoot>::iterator iter = roots_.begin();
    while (iter != roots_.end()) {
        HeapRoot *root = *iter;
        iter = roots_.erase(iter);
        delete root;
    }
}

bool
GlobalRoots::initialize()
{
    return true;
}

void
GlobalRoots::accept(VirtualObjectVisitor *visitor)
{
    for (InlineList<HeapRoot>::iterator iter = roots_.begin(); iter != roots_.end(); iter++)
        visitor->visit(&iter->obj);
}

HeapRoot *
GlobalRoots::create()
{
    HeapRoot *root = new HeapRoot();
    if (!root) {
        ZONE()->reportOutOfMemory();
        return NULL;
    }

    root->obj = NULL;

    roots_.append(root);
    return root;
}

void
GlobalRoots::destroy(HeapRoot *root)
{
    roots_.remove(root);
    delete root;
}
