src/Zone.h
author David Anderson <dvander@alliedmods.net>
Mon Dec 24 02:56:02 2012 -0800 (2012-12-24)
changeset 235 43287eef2fff
parent 201 54f3a4f41f61
child 240 e3c851a14f16
permissions -rw-r--r--
Move PrimitiveType_String to a dedicated string type.
     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     void beginCompilationErrors(const char *file);
   144     void reportCompilationError(Message message, unsigned line, unsigned col, va_list ap);
   145     void endCompilationErrors();
   146 
   147     void accept(VirtualObjectVisitor *visitor);
   148 
   149     void setPublicFunctionListener(Ke::IPublicFunctionListener *listener);
   150     void registerPublic(Handle<Function> fun);
   151     void unregisterPublic(Handle<Function> fun);
   152 };
   153 
   154 // In a multi-threaded system, this would be a TLS variable.
   155 // In the interest of simplicity, we are single-threaded for now.
   156 extern Zone *SingleThreadedZone;
   157 
   158 static inline Zone *ZONE()
   159 {
   160     return SingleThreadedZone;
   161 }
   162 
   163 template <typename T>
   164 Local<T>::Local(Zone *zone, T *thing)
   165   : zone_(zone),
   166     thing_(thing),
   167     prev_(*zone->lastLocalRoot())
   168 {
   169     *zone->lastLocalRoot() = toRaw();
   170 }
   171 
   172 template <typename T>
   173 
   174 Local<T>::~Local()
   175 {
   176     assert(*zone_->lastLocalRoot() == toRaw());
   177     *zone_->lastLocalRoot() = prev_;
   178 }
   179 
   180 template <typename T>
   181 ScopedRoot<T>::ScopedRoot()
   182   : thingp_(make(NULL))
   183 {
   184 }
   185 
   186 template <typename T>
   187 ScopedRoot<T>::ScopedRoot(T *thing)
   188   : thingp_(make(thing))
   189 {
   190     // Type check that T is an Object.
   191     (void)static_cast<Object *>(thing);
   192 }
   193 
   194 template <typename T> T **
   195 ScopedRoot<T>::make(T *other)
   196 {
   197     return reinterpret_cast<T **>(ZONE()->makeScopedRoot(other));
   198 }
   199 
   200 RootScope::RootScope(Zone *zone)
   201   : Ke::HandleScope(zone, zone->rootScopes().position(), *zone->rootScopes().scope())
   202 {
   203 }
   204 
   205 }
   206 
   207 #endif // _include_jitcraft_thread_h_