jitcraft/Zone.h
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 #ifndef _include_jitcraft_thread_h_
    20 #define _include_jitcraft_thread_h_
    21 
    22 #include "PoolAllocator.h"
    23 #include "Stack.h"
    24 #include "Heap.h"
    25 #include "Barriers.h"
    26 #include "Handles.h"
    27 #include "RootScopes.h"
    28 #include "VirtualObjectVisitor.h"
    29 
    30 namespace ke {
    31 
    32 class TypeManager;
    33 class String;
    34 class StringPool;
    35 
    36 class Zone
    37 {
    38     PoolAllocator pool_;
    39     Stack stack_;
    40     Heap heap_;
    41 
    42     // Map cache.
    43     Unbarriered<Map> maps_[MapKinds_Total];
    44 
    45     TypeManager *types_;
    46     StringPool *symbols_;
    47 
    48     // Root list.
    49     Local<Object> *lastLocalRoot_;
    50     RootScopeManager rootScopes_;
    51 
    52   public:
    53     Zone();
    54     ~Zone();
    55     bool initialize();
    56 
    57     PoolAllocator &pool() {
    58         return pool_;
    59     }
    60     Stack &stack() {
    61         return stack_;
    62     }
    63     Heap &heap() {
    64         return heap_;
    65     }
    66     Object *allocate(MapKind kind, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
    67         assert(maps_[kind]);
    68         Local<Map> map(this, maps_[kind]);
    69         return allocate(map, bytes, tenure);
    70     }
    71     Object *allocate(Handle<Map> map, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
    72         return heap_.allocate(map, bytes, tenure);
    73     }
    74     Map *getMap(MapKind kind) {
    75         return maps_[kind];
    76     }
    77     Map *allocateMap(MapKind kind, size_t bytes, MapKind parent = MapKind_Meta);
    78     TypeManager *types() const {
    79         return types_;
    80     }
    81 
    82     Local<Object> **lastLocalRoot() {
    83         return &lastLocalRoot_;
    84     }
    85     Object **makeScopedRoot(Object *ptr) {
    86         return rootScopes_.make(ptr);
    87     }
    88     RootScopeManager &rootScopes() {
    89         return rootScopes_;
    90     }
    91     StringPool *symbols() {
    92         return symbols_;
    93     }
    94 
    95     String *makeSymbol(const char *string, size_t length);
    96     String *makeSymbol(const char *string);
    97 
    98     void accept(VirtualObjectVisitor *visitor);
    99 };
   100 
   101 // In a multi-threaded system, this would be a TLS variable.
   102 // In the interest of simplicity, we are single-threaded for now.
   103 extern Zone SingleThreadedZone;
   104 
   105 static inline Zone *ZONE()
   106 {
   107     return &SingleThreadedZone;
   108 }
   109 
   110 class LocalTop
   111 {
   112     Top &mine_;
   113     Top *prev_;
   114 
   115   public:
   116     LocalTop(Top &mine)
   117       : mine_(mine),
   118         prev_(ZONE()->stack().top())
   119     {
   120         ZONE()->stack().setTop(&mine_);
   121     }
   122 
   123     ~LocalTop()
   124     {
   125         ZONE()->stack().setTop(prev_);
   126     }
   127 };
   128 
   129 template <typename T>
   130 Local<T>::Local(Zone *zone, T *thing)
   131   : zone_(zone),
   132     thing_(thing),
   133     prev_(*zone->lastLocalRoot())
   134 {
   135     *zone->lastLocalRoot() = toRaw();
   136 }
   137 
   138 template <typename T>
   139 Local<T>::~Local()
   140 {
   141     assert(*zone_->lastLocalRoot() == toRaw());
   142     *zone_->lastLocalRoot() = prev_;
   143 }
   144 
   145 template <typename T>
   146 ScopedRoot<T>::ScopedRoot()
   147   : thingp_(make(NULL))
   148 {
   149 }
   150 
   151 template <typename T>
   152 ScopedRoot<T>::ScopedRoot(T *thing)
   153   : thingp_(make(thing))
   154 {
   155     // Type check that T is an Object.
   156     (void)static_cast<Object *>(thing);
   157 }
   158 
   159 template <typename T> T **
   160 ScopedRoot<T>::make(T *other)
   161 {
   162     return reinterpret_cast<T **>(ZONE()->makeScopedRoot(other));
   163 }
   164 
   165 RootScope::RootScope(Zone *zone)
   166   : zone_(zone),
   167     position_(zone->rootScopes().position()),
   168     prev_(*zone->rootScopes().scope())
   169 {
   170     *zone->rootScopes().scope() = this;
   171 }
   172 
   173 RootScope::~RootScope()
   174 {
   175     zone_->rootScopes().unwind(position_);
   176     *zone_->rootScopes().scope() = prev_;
   177 }
   178 
   179 }
   180 
   181 #endif // _include_jitcraft_thread_h_