src/GlobalRoots.cpp
author David Anderson <dvander@alliedmods.net>
Sat Jul 28 13:47:46 2012 -0700 (2012-07-28)
changeset 104 ef7531414fc6
parent 92 e3974e7360e9
child 127 74ad30a6214a
permissions -rw-r--r--
Trigger build.
     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 #include "Spaces-inl.h"
    21 #include "Heap-inl.h"
    22 
    23 using namespace ke;
    24 
    25 GlobalRoots::GlobalRoots(Zone *zone)
    26   : zone_(zone)
    27 {
    28 }
    29 
    30 GlobalRoots::~GlobalRoots()
    31 {
    32     InlineList<HeapRoot>::iterator iter = roots_.begin();
    33     while (iter != roots_.end()) {
    34 		HeapRoot *root = *iter;
    35         iter = roots_.erase(iter);
    36 		delete root;
    37     }
    38 }
    39 
    40 bool
    41 GlobalRoots::initialize()
    42 {
    43     return true;
    44 }
    45 
    46 void
    47 GlobalRoots::accept(VirtualObjectVisitor *visitor)
    48 {
    49     for (InlineList<HeapRoot>::iterator iter = roots_.begin(); iter != roots_.end(); iter++)
    50         visitor->visit(&iter->obj);
    51 }
    52 
    53 HeapRoot *
    54 GlobalRoots::create(Handle<Object> obj)
    55 {
    56     HeapRoot *root = new HeapRoot();
    57     if (!root) {
    58         ZONE()->reportOutOfMemory();
    59         return NULL;
    60     }
    61 
    62     root->obj = obj;
    63     root->refcount = 1;
    64 
    65     roots_.append(root);
    66     return root;
    67 }
    68 
    69 void
    70 GlobalRoots::incref(HeapRoot *root)
    71 {
    72     root->refcount++;
    73 }
    74 
    75 void
    76 GlobalRoots::decref(HeapRoot *root)
    77 {
    78     assert(root->refcount > 0);
    79     if (--root->refcount == 0) {
    80         roots_.remove(root);
    81         delete root;
    82     }
    83 }