jitcraft/Zone.cpp
author David Anderson <dvander@alliedmods.net>
Fri Jul 06 19:24:22 2012 -0400 (2012-07-06)
changeset 51 fb324242a675
parent 45 25b995bc75a0
child 53 13bd3a8df334
permissions -rw-r--r--
Removed HeapChunk::cursor_, converted LifoSpace to HeapChunk.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft 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  * Foobar 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  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #include "Zone.h"
    20 #include "TypeManager.h"
    21 
    22 using namespace jc;
    23 
    24 Zone jc::SingleThreadedZone;
    25 
    26 Zone::Zone()
    27   : heap_(this),
    28     types_(NULL),
    29     lastLocalRoot_(NULL)
    30 {
    31 }
    32 
    33 bool
    34 Zone::initialize()
    35 {
    36     if (!stack_.initialize())
    37         return false;
    38     if (!heap_.initialize())
    39         return false;
    40 
    41     maps_[MapKind_Meta] = heap_.metaMap();
    42     if ((maps_[MapKind_Type] = allocateMap(MapKind_Type, sizeof(Map))) == NULL)
    43         return false;
    44     if ((maps_[MapKind_FixedArray] = allocateMap(MapKind_FixedArray, sizeof(Map))) == NULL)
    45         return false;
    46     if ((maps_[MapKind_ByteArray] = allocateMap(MapKind_ByteArray, sizeof(Map))) == NULL)
    47         return false;
    48     if ((maps_[MapKind_Code] = allocateMap(MapKind_Code, sizeof(Map))) == NULL)
    49         return false;
    50     if ((maps_[MapKind_Reference] = allocateMap(MapKind_Reference, sizeof(Map))) == NULL)
    51         return false;
    52     if ((maps_[MapKind_MetaArray] = allocateMap(MapKind_MetaArray, sizeof(Map))) == NULL)
    53         return false;
    54     if ((maps_[MapKind_Module] = allocateMap(MapKind_Module, sizeof(Map))) == NULL)
    55         return false;
    56     if ((maps_[MapKind_Function] = allocateMap(MapKind_Function, sizeof(Map))) == NULL)
    57         return false;
    58 
    59     types_ = new TypeManager(this);
    60     if (!types_ || !types_->initialize())
    61         return false;
    62 
    63     return true;
    64 }
    65 
    66 Map *
    67 Zone::allocateMap(MapKind kind, size_t bytes, MapKind parent)
    68 {
    69     assert(maps_[parent]);
    70     Map *map = heap_.allocateMap(kind, bytes);
    71     if (!map)
    72         return NULL;
    73     map->init(maps_[parent]);
    74     return map;
    75 }
    76 
    77 void
    78 Zone::accept(VirtualObjectVisitor *visitor)
    79 {
    80     for (unsigned i = 0; i < MapKinds_Total; i++)
    81         visitor->visit(maps_[i].address());
    82     types()->accept(visitor);
    83 
    84     Local<Object> *local = lastLocalRoot_;
    85     while (local) {
    86         visitor->visit(local->address());
    87         local = local->prev();
    88     }
    89 
    90     rootScopes_.accept(visitor);
    91 
    92     stack_.accept(visitor);
    93 }
    94