src/GlobalRoots.cpp
author David Anderson <danderson@mozilla.com>
Wed Jul 25 00:48:08 2012 -0700 (2012-07-25)
changeset 91 5f4402180204
parent 86 32ad0523b309
child 92 e3974e7360e9
permissions -rw-r--r--
Added clang support. A lot of the motion here is introducing a new offsetof because clang's seems broken?
     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         ZONE()->reportOutOfMemory();
    57         return NULL;
    58     }
    59 
    60     root->obj = obj;
    61     root->refcount = 1;
    62 
    63     roots_.append(root);
    64     return root;
    65 }
    66 
    67 void
    68 GlobalRoots::incref(HeapRoot *root)
    69 {
    70     root->refcount++;
    71 }
    72 
    73 void
    74 GlobalRoots::decref(HeapRoot *root)
    75 {
    76     assert(root->refcount > 0);
    77     if (--root->refcount == 0) {
    78         roots_.remove(root);
    79         delete root;
    80     }
    81 }