src/Zone.h
author David Anderson <dvander@alliedmods.net>
Sat Dec 29 00:55:38 2012 -0800 (2012-12-29)
changeset 250 0998a43cc155
parent 240 e3c851a14f16
child 251 ca99a81745fe
permissions -rw-r--r--
Collect error messages per-translation unit.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of SourcePawn.
     6  *
     7  * SourcePawn 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  * SourcePawn 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  * SourcePawn. 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 class NativeTable;
    41 
    42 class Zone
    43 {
    44     PoolAllocator pool_;
    45     Stack stack_;
    46     Heap heap_;
    47 
    48     // Map cache.
    49     Unbarriered<Map> maps_[MapKinds_Total];
    50     Unbarriered<ArrayMap> externalGenericArrayMap_;
    51     Unbarriered<ArrayMap> externalCharArrayMap_;
    52 
    53     TypeManager *types_;
    54     StringPool *symbols_;
    55     GlobalRoots *globalRoots_;
    56     NativeTable *nativeTable_;
    57 
    58     // Root list.
    59     Local<Object> *lastLocalRoot_;
    60     RootScopeManager rootScopes_;
    61     AutoRooted *specialRoots_;
    62 
    63     // Exception stuff.
    64     bool hasPendingException_;
    65     Ke::IMessageReporter *reporter_;
    66 
    67     Ke::IPublicFunctionListener *publicFunctionListener_;
    68 
    69   private:
    70     Zone *thisFromCtor() {
    71         return this;
    72     }
    73 
    74   public:
    75     Zone();
    76     ~Zone();
    77     bool initialize();
    78 
    79     PoolAllocator &pool() {
    80         return pool_;
    81     }
    82     Stack &stack() {
    83         return stack_;
    84     }
    85     Heap &heap() {
    86         return heap_;
    87     }
    88     Object *allocate(MapKind kind, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
    89         assert(maps_[kind]);
    90         Local<Map> map(this, maps_[kind]);
    91         return allocate(map, bytes, tenure);
    92     }
    93     Object *allocate(Handle<Map> map, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
    94         return heap_.allocate(map, bytes, tenure);
    95     }
    96     Map *getMap(MapKind kind) {
    97         return maps_[kind];
    98     }
    99     Map *allocateMap(MapKind kind, size_t bytes, MapKind parent = MapKind_Meta);
   100 
   101     ArrayMap *externalGenericArrayMap() {
   102         return externalGenericArrayMap_;
   103     }
   104     ArrayMap *externalCharArrayMap() {
   105         return externalCharArrayMap_;
   106     }
   107 
   108     TypeManager *types() const {
   109         return types_;
   110     }
   111     AutoRooted **lastSpecialRoot() {
   112         return &specialRoots_;
   113     }
   114     Local<Object> **lastLocalRoot() {
   115         return &lastLocalRoot_;
   116     }
   117     Object **makeScopedRoot(Object *ptr) {
   118         return rootScopes_.make(ptr);
   119     }
   120     RootScopeManager &rootScopes() {
   121         return rootScopes_;
   122     }
   123     StringPool *symbols() {
   124         return symbols_;
   125     }
   126     GlobalRoots *globalRoots() {
   127         return globalRoots_;
   128     }
   129     NativeTable *nativeTable() {
   130         return nativeTable_;
   131     }
   132 
   133     String *makeSymbol(const char *string, size_t length);
   134     String *makeSymbol(const char *string);
   135 
   136     void reportOutOfMemory();
   137     void reportAllocationOverflow();
   138     void reportError(Message message, ...);
   139     void reportErrorVA(Message message, va_list ap);
   140     bool hasPendingException();
   141     void clearPendingException();
   142     void installErrorReporter(Ke::IMessageReporter *reporter);
   143     Ke::IMessageReporter *errorReporter() {
   144         return reporter_;
   145     }
   146 
   147     void accept(VirtualObjectVisitor *visitor);
   148     void sweepWeakRoots();
   149 
   150     void setPublicFunctionListener(Ke::IPublicFunctionListener *listener);
   151     void registerPublic(Handle<Function> fun);
   152     void unregisterPublic(Handle<Function> fun);
   153 };
   154 
   155 // In a multi-threaded system, this would be a TLS variable.
   156 // In the interest of simplicity, we are single-threaded for now.
   157 extern Zone *SingleThreadedZone;
   158 
   159 // Exposed for the compiler.
   160 char *BuildErrorMessage(const char *format, va_list ap);
   161 void FreeErrorMessage(const char *message);
   162 
   163 static inline Zone *ZONE()
   164 {
   165     return SingleThreadedZone;
   166 }
   167 
   168 template <typename T>
   169 Local<T>::Local(Zone *zone, T *thing)
   170   : zone_(zone),
   171     thing_(thing),
   172     prev_(*zone->lastLocalRoot())
   173 {
   174     *zone->lastLocalRoot() = toRaw();
   175 }
   176 
   177 template <typename T>
   178 
   179 Local<T>::~Local()
   180 {
   181     assert(*zone_->lastLocalRoot() == toRaw());
   182     *zone_->lastLocalRoot() = prev_;
   183 }
   184 
   185 template <typename T>
   186 ScopedRoot<T>::ScopedRoot()
   187   : thingp_(make(NULL))
   188 {
   189 }
   190 
   191 template <typename T>
   192 ScopedRoot<T>::ScopedRoot(T *thing)
   193   : thingp_(make(thing))
   194 {
   195     // Type check that T is an Object.
   196     (void)static_cast<Object *>(thing);
   197 }
   198 
   199 template <typename T> T **
   200 ScopedRoot<T>::make(T *other)
   201 {
   202     return reinterpret_cast<T **>(ZONE()->makeScopedRoot(other));
   203 }
   204 
   205 RootScope::RootScope(Zone *zone)
   206   : Ke::HandleScope(zone, zone->rootScopes().position(), *zone->rootScopes().scope())
   207 {
   208 }
   209 
   210 }
   211 
   212 #endif // _include_jitcraft_thread_h_