jitcraft/GlobalRoots.cpp
author David Anderson <dvander@alliedmods.net>
Mon Jul 09 22:25:52 2012 -0700 (2012-07-09)
changeset 67 b765a6383849
parent 65 7c942b5adebc
child 69 dfce0539a77d
permissions -rw-r--r--
Implemented const for references and arrays.
     1 /*
     2  * Copyright (C) 2004-2012 David Anderson
     3  *
     4  * This file is part of SourcePawn.
     5  *
     6  * SourcePawn is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #include "GlobalRoots.h"
    19 #include "Zone.h"
    20 
    21 using namespace ke;
    22 
    23 GlobalRoots::GlobalRoots(Zone *zone)
    24   : zone_(zone)
    25 {
    26 }
    27 
    28 GlobalRoots::~GlobalRoots()
    29 {
    30     InlineList<HeapRoot>::iterator iter = roots_.begin();
    31     while (iter != roots_.end()) {
    32 		HeapRoot *root = *iter;
    33         iter = roots_.erase(iter);
    34 		delete root;
    35     }
    36 }
    37 
    38 bool
    39 GlobalRoots::initialize()
    40 {
    41     return true;
    42 }
    43 
    44 void
    45 GlobalRoots::accept(VirtualObjectVisitor *visitor)
    46 {
    47     for (InlineList<HeapRoot>::iterator iter = roots_.begin(); iter != roots_.end(); iter++)
    48         visitor->visit(&iter->obj);
    49 }
    50 
    51 HeapRoot *
    52 GlobalRoots::create(Handle<Object> obj)
    53 {
    54     HeapRoot *root = new HeapRoot();
    55     if (!root)
    56         return NULL;
    57 
    58     root->obj = obj;
    59     root->refcount = 1;
    60 
    61     roots_.append(root);
    62     return root;
    63 }
    64 
    65 void
    66 GlobalRoots::incref(HeapRoot *root)
    67 {
    68     root->refcount++;
    69 }
    70 
    71 void
    72 GlobalRoots::decref(HeapRoot *root)
    73 {
    74     assert(root->refcount > 0);
    75     if (--root->refcount == 0) {
    76         roots_.remove(root);
    77         delete root;
    78     }
    79 }