1.1 --- a/jitcraft/Zone.h Tue Jul 24 23:53:34 2012 -0700
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,189 +0,0 @@
1.4 -/* vim: set ts=4 sw=4 tw=99 et:
1.5 - *
1.6 - * Copyright (C) 2012 David Anderson
1.7 - *
1.8 - * This file is part of JITCraft.
1.9 - *
1.10 - * JITCraft is free software: you can redistribute it and/or modify it under
1.11 - * the terms of the GNU General Public License as published by the Free
1.12 - * Software Foundation, either version 3 of the License, or (at your option)
1.13 - * any later version.
1.14 - *
1.15 - * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
1.16 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.18 - *
1.19 - * You should have received a copy of the GNU General Public License along with
1.20 - * JITCraft. If not, see http://www.gnu.org/licenses/.
1.21 - */
1.22 -#ifndef _include_jitcraft_thread_h_
1.23 -#define _include_jitcraft_thread_h_
1.24 -
1.25 -#include <stdarg.h>
1.26 -#include "PoolAllocator.h"
1.27 -#include "Stack.h"
1.28 -#include "Heap.h"
1.29 -#include "Barriers.h"
1.30 -#include "Handles.h"
1.31 -#include "Messages.h"
1.32 -#include "RootScopes.h"
1.33 -#include "VirtualObjectVisitor.h"
1.34 -
1.35 -namespace ke {
1.36 -
1.37 -class TypeManager;
1.38 -class String;
1.39 -class StringPool;
1.40 -class GlobalRoots;
1.41 -class ArrayMap;
1.42 -class Exception;
1.43 -
1.44 -class Zone
1.45 -{
1.46 - PoolAllocator pool_;
1.47 - Stack stack_;
1.48 - Heap heap_;
1.49 -
1.50 - // Map cache.
1.51 - Unbarriered<Map> maps_[MapKinds_Total];
1.52 - Unbarriered<ArrayMap> genericArrayMap_;
1.53 - Unbarriered<ArrayMap> charArrayMap_;
1.54 -
1.55 - TypeManager *types_;
1.56 - StringPool *symbols_;
1.57 - GlobalRoots *globalRoots_;
1.58 -
1.59 - // Root list.
1.60 - Local<Object> *lastLocalRoot_;
1.61 - RootScopeManager rootScopes_;
1.62 -
1.63 - // Exception stuff.
1.64 - bool hasPendingException_;
1.65 - Ke::IMessageReporter *reporter_;
1.66 -
1.67 - private:
1.68 - Zone *thisFromCtor() {
1.69 - return this;
1.70 - }
1.71 -
1.72 - public:
1.73 - Zone();
1.74 - ~Zone();
1.75 - bool initialize();
1.76 -
1.77 - PoolAllocator &pool() {
1.78 - return pool_;
1.79 - }
1.80 - Stack &stack() {
1.81 - return stack_;
1.82 - }
1.83 - Heap &heap() {
1.84 - return heap_;
1.85 - }
1.86 - Object *allocate(MapKind kind, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
1.87 - assert(maps_[kind]);
1.88 - Local<Map> map(this, maps_[kind]);
1.89 - return allocate(map, bytes, tenure);
1.90 - }
1.91 - Object *allocate(Handle<Map> map, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
1.92 - return heap_.allocate(map, bytes, tenure);
1.93 - }
1.94 - Map *getMap(MapKind kind) {
1.95 - return maps_[kind];
1.96 - }
1.97 - Map *allocateMap(MapKind kind, size_t bytes, MapKind parent = MapKind_Meta);
1.98 -
1.99 - ArrayMap *genericArrayMap() {
1.100 - return genericArrayMap_;
1.101 - }
1.102 - ArrayMap *charArrayMap() {
1.103 - return charArrayMap_;
1.104 - }
1.105 -
1.106 - TypeManager *types() const {
1.107 - return types_;
1.108 - }
1.109 - Local<Object> **lastLocalRoot() {
1.110 - return &lastLocalRoot_;
1.111 - }
1.112 - Object **makeScopedRoot(Object *ptr) {
1.113 - return rootScopes_.make(ptr);
1.114 - }
1.115 - RootScopeManager &rootScopes() {
1.116 - return rootScopes_;
1.117 - }
1.118 - StringPool *symbols() {
1.119 - return symbols_;
1.120 - }
1.121 - GlobalRoots *globalRoots() {
1.122 - return globalRoots_;
1.123 - }
1.124 -
1.125 - String *makeSymbol(const char *string, size_t length);
1.126 - String *makeSymbol(const char *string);
1.127 -
1.128 - void reportOutOfMemory();
1.129 - void reportError(Message message, ...);
1.130 - void reportErrorVA(Message message, va_list ap);
1.131 - bool hasPendingException();
1.132 - void installErrorReporter(Ke::IMessageReporter *reporter);
1.133 - void beginCompilationErrors(const char *file);
1.134 - void reportCompilationError(Message message, unsigned line, unsigned col, va_list ap);
1.135 - void endCompilationErrors();
1.136 -
1.137 - void accept(VirtualObjectVisitor *visitor);
1.138 -};
1.139 -
1.140 -// In a multi-threaded system, this would be a TLS variable.
1.141 -// In the interest of simplicity, we are single-threaded for now.
1.142 -extern Zone *SingleThreadedZone;
1.143 -
1.144 -static inline Zone *ZONE()
1.145 -{
1.146 - return SingleThreadedZone;
1.147 -}
1.148 -
1.149 -template <typename T>
1.150 -Local<T>::Local(Zone *zone, T *thing)
1.151 - : zone_(zone),
1.152 - thing_(thing),
1.153 - prev_(*zone->lastLocalRoot())
1.154 -{
1.155 - *zone->lastLocalRoot() = toRaw();
1.156 -}
1.157 -
1.158 -template <typename T>
1.159 -Local<T>::~Local()
1.160 -{
1.161 - assert(*zone_->lastLocalRoot() == toRaw());
1.162 - *zone_->lastLocalRoot() = prev_;
1.163 -}
1.164 -
1.165 -template <typename T>
1.166 -ScopedRoot<T>::ScopedRoot()
1.167 - : thingp_(make(NULL))
1.168 -{
1.169 -}
1.170 -
1.171 -template <typename T>
1.172 -ScopedRoot<T>::ScopedRoot(T *thing)
1.173 - : thingp_(make(thing))
1.174 -{
1.175 - // Type check that T is an Object.
1.176 - (void)static_cast<Object *>(thing);
1.177 -}
1.178 -
1.179 -template <typename T> T **
1.180 -ScopedRoot<T>::make(T *other)
1.181 -{
1.182 - return reinterpret_cast<T **>(ZONE()->makeScopedRoot(other));
1.183 -}
1.184 -
1.185 -RootScope::RootScope(Zone *zone)
1.186 - : Ke::HandleScope(zone, zone->rootScopes().position(), *zone->rootScopes().scope())
1.187 -{
1.188 -}
1.189 -
1.190 -}
1.191 -
1.192 -#endif // _include_jitcraft_thread_h_