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