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