src/GlobalRoots.cpp
changeset 86 32ad0523b309
parent 69 dfce0539a77d
child 92 e3974e7360e9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/GlobalRoots.cpp	Tue Jul 24 23:53:34 2012 -0700
     1.3 @@ -0,0 +1,81 @@
     1.4 +/*
     1.5 + * Copyright (C) 2004-2012 David Anderson
     1.6 + *
     1.7 + * This file is part of SourcePawn.
     1.8 + *
     1.9 + * SourcePawn is free software: you can redistribute it and/or modify it under
    1.10 + * the terms of the GNU General Public License as published by the Free
    1.11 + * Software Foundation, either version 3 of the License, or (at your option)
    1.12 + * any later version.
    1.13 + * 
    1.14 + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    1.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License along with
    1.19 + * SourcePawn. If not, see http://www.gnu.org/licenses/.
    1.20 + */
    1.21 +#include "GlobalRoots.h"
    1.22 +#include "Zone.h"
    1.23 +
    1.24 +using namespace ke;
    1.25 +
    1.26 +GlobalRoots::GlobalRoots(Zone *zone)
    1.27 +  : zone_(zone)
    1.28 +{
    1.29 +}
    1.30 +
    1.31 +GlobalRoots::~GlobalRoots()
    1.32 +{
    1.33 +    InlineList<HeapRoot>::iterator iter = roots_.begin();
    1.34 +    while (iter != roots_.end()) {
    1.35 +		HeapRoot *root = *iter;
    1.36 +        iter = roots_.erase(iter);
    1.37 +		delete root;
    1.38 +    }
    1.39 +}
    1.40 +
    1.41 +bool
    1.42 +GlobalRoots::initialize()
    1.43 +{
    1.44 +    return true;
    1.45 +}
    1.46 +
    1.47 +void
    1.48 +GlobalRoots::accept(VirtualObjectVisitor *visitor)
    1.49 +{
    1.50 +    for (InlineList<HeapRoot>::iterator iter = roots_.begin(); iter != roots_.end(); iter++)
    1.51 +        visitor->visit(&iter->obj);
    1.52 +}
    1.53 +
    1.54 +HeapRoot *
    1.55 +GlobalRoots::create(Handle<Object> obj)
    1.56 +{
    1.57 +    HeapRoot *root = new HeapRoot();
    1.58 +    if (!root) {
    1.59 +        ZONE()->reportOutOfMemory();
    1.60 +        return NULL;
    1.61 +    }
    1.62 +
    1.63 +    root->obj = obj;
    1.64 +    root->refcount = 1;
    1.65 +
    1.66 +    roots_.append(root);
    1.67 +    return root;
    1.68 +}
    1.69 +
    1.70 +void
    1.71 +GlobalRoots::incref(HeapRoot *root)
    1.72 +{
    1.73 +    root->refcount++;
    1.74 +}
    1.75 +
    1.76 +void
    1.77 +GlobalRoots::decref(HeapRoot *root)
    1.78 +{
    1.79 +    assert(root->refcount > 0);
    1.80 +    if (--root->refcount == 0) {
    1.81 +        roots_.remove(root);
    1.82 +        delete root;
    1.83 +    }
    1.84 +}