jitcraft/Zone.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jul 08 11:33:12 2012 -0700 (2012-07-08)
changeset 61 864e4b02a4c1
parent 60 581a9c793062
child 65 7c942b5adebc
permissions -rw-r--r--
Added global root API.
     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 "StringPool.h"
    21 #include "TypeManager.h"
    22 #include "GlobalRoots.h"
    23 #include "AllocatorPolicies.h"
    24 
    25 using namespace ke;
    26 
    27 Zone *ke::SingleThreadedZone;
    28 
    29 Zone::Zone()
    30   : heap_(this),
    31     types_(NULL),
    32     symbols_(NULL),
    33     globalRoots_(NULL),
    34     lastLocalRoot_(NULL)
    35 {
    36 }
    37 
    38 Zone::~Zone()
    39 {
    40     delete types_;
    41     delete symbols_;
    42     delete globalRoots_;
    43 }
    44 
    45 bool
    46 Zone::initialize()
    47 {
    48     if (!stack_.initialize())
    49         return false;
    50     if (!heap_.initialize())
    51         return false;
    52 
    53     symbols_ = new StringPool(this);
    54     if (!symbols_ || !symbols_->initialize())
    55         return false;
    56 
    57     globalRoots_ = new GlobalRoots(this);
    58     if (!globalRoots_ || !globalRoots_->initialize())
    59         return false;
    60 
    61     maps_[MapKind_Meta] = heap_.metaMap();
    62     if ((maps_[MapKind_Type] = allocateMap(MapKind_Type, sizeof(Map))) == NULL)
    63         return false;
    64     if ((maps_[MapKind_FixedArray] = allocateMap(MapKind_FixedArray, sizeof(Map))) == NULL)
    65         return false;
    66     if ((maps_[MapKind_ByteArray] = allocateMap(MapKind_ByteArray, sizeof(Map))) == NULL)
    67         return false;
    68     if ((maps_[MapKind_Code] = allocateMap(MapKind_Code, sizeof(Map))) == NULL)
    69         return false;
    70     if ((maps_[MapKind_Reference] = allocateMap(MapKind_Reference, sizeof(Map))) == NULL)
    71         return false;
    72     if ((maps_[MapKind_MetaArray] = allocateMap(MapKind_MetaArray, sizeof(Map))) == NULL)
    73         return false;
    74     if ((maps_[MapKind_Module] = allocateMap(MapKind_Module, sizeof(Map))) == NULL)
    75         return false;
    76     if ((maps_[MapKind_Function] = allocateMap(MapKind_Function, sizeof(Map))) == NULL)
    77         return false;
    78     if ((maps_[MapKind_String] = allocateMap(MapKind_String, sizeof(Map))) == NULL)
    79         return false;
    80     if ((maps_[MapKind_FreeRegion] = allocateMap(MapKind_FreeRegion, sizeof(Map))) == NULL)
    81         return false;
    82 
    83     types_ = new TypeManager(this);
    84     if (!types_ || !types_->initialize())
    85         return false;
    86 
    87     return true;
    88 }
    89 
    90 Map *
    91 Zone::allocateMap(MapKind kind, size_t bytes, MapKind parent)
    92 {
    93     assert(maps_[parent]);
    94     Map *map = heap_.allocateMap(kind, bytes);
    95     if (!map)
    96         return NULL;
    97     map->init(maps_[parent]);
    98     return map;
    99 }
   100 
   101 void
   102 Zone::accept(VirtualObjectVisitor *visitor)
   103 {
   104     for (unsigned i = 0; i < MapKinds_Total; i++)
   105         visitor->visit(maps_[i].address());
   106     types()->accept(visitor);
   107 
   108     Local<Object> *local = lastLocalRoot_;
   109     while (local) {
   110         visitor->visit(local->address());
   111         local = local->prev();
   112     }
   113 
   114     globalRoots_->accept(visitor);
   115     rootScopes_.accept(visitor);
   116 
   117     stack_.accept(visitor);
   118 }
   119 
   120 String *
   121 Zone::makeSymbol(const char *str, size_t length)
   122 {
   123     return symbols_->add(str, length);
   124 }
   125 
   126 String *
   127 Zone::makeSymbol(const char *str)
   128 {
   129     return makeSymbol(str, strlen(str));
   130 }
   131 
   132 void
   133 ZoneAllocatorPolicy::reportOutOfMemory()
   134 {
   135     // :TODO: report
   136     assert(false);
   137 }
   138 
   139 void
   140 ZoneAllocatorPolicy::reportAllocationOverflow()
   141 {
   142     // :TODO: report
   143     assert(false);
   144 }