1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft 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)
12 * Foobar 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.
16 * You should have received a copy of the GNU General Public License along with
17 * JITCraft. If not, see http://www.gnu.org/licenses/.
19 #ifndef _include_jitcraft_thread_h_
20 #define _include_jitcraft_thread_h_
22 #include "PoolAllocator.h"
27 #include "RootScopes.h"
28 #include "VirtualObjectVisitor.h"
43 Unbarriered<Map> maps_[MapKinds_Total];
49 Local<Object> *lastLocalRoot_;
50 RootScopeManager rootScopes_;
57 PoolAllocator &pool() {
66 Object *allocate(MapKind kind, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
68 Local<Map> map(this, maps_[kind]);
69 return allocate(map, bytes, tenure);
71 Object *allocate(Handle<Map> map, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
72 return heap_.allocate(map, bytes, tenure);
74 Map *getMap(MapKind kind) {
77 Map *allocateMap(MapKind kind, size_t bytes, MapKind parent = MapKind_Meta);
78 TypeManager *types() const {
82 Local<Object> **lastLocalRoot() {
83 return &lastLocalRoot_;
85 Object **makeScopedRoot(Object *ptr) {
86 return rootScopes_.make(ptr);
88 RootScopeManager &rootScopes() {
91 StringPool *symbols() {
95 String *makeSymbol(const char *string, size_t length);
96 String *makeSymbol(const char *string);
98 void accept(VirtualObjectVisitor *visitor);
101 // In a multi-threaded system, this would be a TLS variable.
102 // In the interest of simplicity, we are single-threaded for now.
103 extern Zone SingleThreadedZone;
105 static inline Zone *ZONE()
107 return &SingleThreadedZone;
118 prev_(ZONE()->stack().top())
120 ZONE()->stack().setTop(&mine_);
125 ZONE()->stack().setTop(prev_);
129 template <typename T>
130 Local<T>::Local(Zone *zone, T *thing)
133 prev_(*zone->lastLocalRoot())
135 *zone->lastLocalRoot() = toRaw();
138 template <typename T>
141 assert(*zone_->lastLocalRoot() == toRaw());
142 *zone_->lastLocalRoot() = prev_;
145 template <typename T>
146 ScopedRoot<T>::ScopedRoot()
147 : thingp_(make(NULL))
151 template <typename T>
152 ScopedRoot<T>::ScopedRoot(T *thing)
153 : thingp_(make(thing))
155 // Type check that T is an Object.
156 (void)static_cast<Object *>(thing);
159 template <typename T> T **
160 ScopedRoot<T>::make(T *other)
162 return reinterpret_cast<T **>(ZONE()->makeScopedRoot(other));
165 RootScope::RootScope(Zone *zone)
167 position_(zone->rootScopes().position()),
168 prev_(*zone->rootScopes().scope())
170 *zone->rootScopes().scope() = this;
173 RootScope::~RootScope()
175 zone_->rootScopes().unwind(position_);
176 *zone_->rootScopes().scope() = prev_;
181 #endif // _include_jitcraft_thread_h_