src/Zone.h
author David Anderson <dvander@alliedmods.net>
Thu Dec 27 20:44:50 2012 -0800 (2012-12-27)
changeset 245 9371f058625a
parent 240 e3c851a14f16
child 250 0998a43cc155
permissions -rw-r--r--
Support admin.inc again.
     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     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 static inline Zone *ZONE()
   160 {
   161     return SingleThreadedZone;
   162 }
   163 
   164 template <typename T>
   165 Local<T>::Local(Zone *zone, T *thing)
   166   : zone_(zone),
   167     thing_(thing),
   168     prev_(*zone->lastLocalRoot())
   169 {
   170     *zone->lastLocalRoot() = toRaw();
   171 }
   172 
   173 template <typename T>
   174 
   175 Local<T>::~Local()
   176 {
   177     assert(*zone_->lastLocalRoot() == toRaw());
   178     *zone_->lastLocalRoot() = prev_;
   179 }
   180 
   181 template <typename T>
   182 ScopedRoot<T>::ScopedRoot()
   183   : thingp_(make(NULL))
   184 {
   185 }
   186 
   187 template <typename T>
   188 ScopedRoot<T>::ScopedRoot(T *thing)
   189   : thingp_(make(thing))
   190 {
   191     // Type check that T is an Object.
   192     (void)static_cast<Object *>(thing);
   193 }
   194 
   195 template <typename T> T **
   196 ScopedRoot<T>::make(T *other)
   197 {
   198     return reinterpret_cast<T **>(ZONE()->makeScopedRoot(other));
   199 }
   200 
   201 RootScope::RootScope(Zone *zone)
   202   : Ke::HandleScope(zone, zone->rootScopes().position(), *zone->rootScopes().scope())
   203 {
   204 }
   205 
   206 }
   207 
   208 #endif // _include_jitcraft_thread_h_