src/Zone.h
author David Anderson <dvander@alliedmods.net>
Sat Aug 04 19:33:06 2012 -0700 (2012-08-04)
changeset 147 9c544823d090
parent 129 86b368a4d80d
child 188 bab29bcec7fe
permissions -rw-r--r--
Structure declaration support.
     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 <stdarg.h>
    23 #include "PoolAllocator.h"
    24 #include "Stack.h"
    25 #include "Heap.h"
    26 #include "Barriers.h"
    27 #include "Handles.h"
    28 #include "Messages.h"
    29 #include "RootScopes.h"
    30 #include "VirtualObjectVisitor.h"
    31 
    32 namespace ke {
    33 
    34 class TypeManager;
    35 class String;
    36 class StringPool;
    37 class GlobalRoots;
    38 class ArrayMap;
    39 class Exception;
    40 
    41 class Zone
    42 {
    43     PoolAllocator pool_;
    44     Stack stack_;
    45     Heap heap_;
    46 
    47     // Map cache.
    48     Unbarriered<Map> maps_[MapKinds_Total];
    49     Unbarriered<ArrayMap> genericArrayMap_;
    50     Unbarriered<ArrayMap> charArrayMap_;
    51 
    52     TypeManager *types_;
    53     StringPool *symbols_;
    54     GlobalRoots *globalRoots_;
    55 
    56     // Root list.
    57     Local<Object> *lastLocalRoot_;
    58     RootScopeManager rootScopes_;
    59 
    60     // Exception stuff.
    61     bool hasPendingException_;
    62     Ke::IMessageReporter *reporter_;
    63 
    64   private:
    65     Zone *thisFromCtor() {
    66         return this;
    67     }
    68 
    69   public:
    70     Zone();
    71     ~Zone();
    72     bool initialize();
    73 
    74     PoolAllocator &pool() {
    75         return pool_;
    76     }
    77     Stack &stack() {
    78         return stack_;
    79     }
    80     Heap &heap() {
    81         return heap_;
    82     }
    83     Object *allocate(MapKind kind, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
    84         assert(maps_[kind]);
    85         Local<Map> map(this, maps_[kind]);
    86         return allocate(map, bytes, tenure);
    87     }
    88     Object *allocate(Handle<Map> map, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
    89         return heap_.allocate(map, bytes, tenure);
    90     }
    91     Map *getMap(MapKind kind) {
    92         return maps_[kind];
    93     }
    94     Map *allocateMap(MapKind kind, size_t bytes, MapKind parent = MapKind_Meta);
    95 
    96     ArrayMap *genericArrayMap() {
    97         return genericArrayMap_;
    98     }
    99     ArrayMap *charArrayMap() {
   100         return charArrayMap_;
   101     }
   102 
   103     TypeManager *types() const {
   104         return types_;
   105     }
   106     Local<Object> **lastLocalRoot() {
   107         return &lastLocalRoot_;
   108     }
   109     Object **makeScopedRoot(Object *ptr) {
   110         return rootScopes_.make(ptr);
   111     }
   112     RootScopeManager &rootScopes() {
   113         return rootScopes_;
   114     }
   115     StringPool *symbols() {
   116         return symbols_;
   117     }
   118     GlobalRoots *globalRoots() {
   119         return globalRoots_;
   120     }
   121 
   122     String *makeSymbol(const char *string, size_t length);
   123     String *makeSymbol(const char *string);
   124 
   125     void reportOutOfMemory();
   126     void reportError(Message message, ...);
   127     void reportErrorVA(Message message, va_list ap);
   128     bool hasPendingException();
   129     void clearPendingException();
   130     void installErrorReporter(Ke::IMessageReporter *reporter);
   131     void beginCompilationErrors(const char *file);
   132     void reportCompilationError(Message message, unsigned line, unsigned col, va_list ap);
   133     void endCompilationErrors();
   134 
   135     void accept(VirtualObjectVisitor *visitor);
   136 };
   137 
   138 // In a multi-threaded system, this would be a TLS variable.
   139 // In the interest of simplicity, we are single-threaded for now.
   140 extern Zone *SingleThreadedZone;
   141 
   142 static inline Zone *ZONE()
   143 {
   144     return SingleThreadedZone;
   145 }
   146 
   147 template <typename T>
   148 Local<T>::Local(Zone *zone, T *thing)
   149   : zone_(zone),
   150     thing_(thing),
   151     prev_(*zone->lastLocalRoot())
   152 {
   153     *zone->lastLocalRoot() = toRaw();
   154 }
   155 
   156 template <typename T>
   157 
   158 Local<T>::~Local()
   159 {
   160     assert(*zone_->lastLocalRoot() == toRaw());
   161     *zone_->lastLocalRoot() = prev_;
   162 }
   163 
   164 template <typename T>
   165 ScopedRoot<T>::ScopedRoot()
   166   : thingp_(make(NULL))
   167 {
   168 }
   169 
   170 template <typename T>
   171 ScopedRoot<T>::ScopedRoot(T *thing)
   172   : thingp_(make(thing))
   173 {
   174     // Type check that T is an Object.
   175     (void)static_cast<Object *>(thing);
   176 }
   177 
   178 template <typename T> T **
   179 ScopedRoot<T>::make(T *other)
   180 {
   181     return reinterpret_cast<T **>(ZONE()->makeScopedRoot(other));
   182 }
   183 
   184 RootScope::RootScope(Zone *zone)
   185   : Ke::HandleScope(zone, zone->rootScopes().position(), *zone->rootScopes().scope())
   186 {
   187 }
   188 
   189 }
   190 
   191 #endif // _include_jitcraft_thread_h_