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