1.1 --- a/src/RootScopes.h Mon Jun 10 00:46:29 2013 +0100
1.2 +++ b/src/RootScopes.h Mon Jun 10 01:04:43 2013 +0100
1.3 @@ -1,106 +1,106 @@
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 SourcePawn.
1.9 - *
1.10 - * SourcePawn 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 - * SourcePawn 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 - * SourcePawn. If not, see http://www.gnu.org/licenses/.
1.21 - */
1.22 -#ifndef _include_jc_root_scopes_h_
1.23 -#define _include_jc_root_scopes_h_
1.24 -
1.25 -#include "../include/keima.h"
1.26 -#include "Objects.h"
1.27 -#include "VirtualObjectVisitor.h"
1.28 -
1.29 -namespace ke {
1.30 -
1.31 -class RootScope;
1.32 -
1.33 -// A RootScopeManager is essentially a specialized PoolAllocator that can
1.34 -// only allocate Object ** entries. It is used to allow huge, temporary
1.35 -// heap data structures like ASTs to hold safe references to objects. When
1.36 -// the data structures is no longer needed, all of the roots are removed
1.37 -// at once.
1.38 -class RootScopeManager
1.39 -{
1.40 - static const size_t kObjectsPerBlock = 4096;
1.41 -
1.42 - struct Block {
1.43 - Object *objects[kObjectsPerBlock];
1.44 - Object **cur;
1.45 - Object **end;
1.46 - Block *prev;
1.47 -
1.48 - Block(Block *prev)
1.49 - : cur(objects),
1.50 - end(objects + kObjectsPerBlock),
1.51 - prev(prev)
1.52 - {
1.53 - }
1.54 - };
1.55 -
1.56 - private:
1.57 - bool grow();
1.58 - void unwindSlow(Object **position);
1.59 -
1.60 - public:
1.61 - RootScopeManager();
1.62 - ~RootScopeManager();
1.63 -
1.64 - Object **position() const {
1.65 - return blocks_->cur;
1.66 - }
1.67 -
1.68 - void unwind(Object **position) {
1.69 - if (position >= blocks_->cur && position <= blocks_->end) {
1.70 - blocks_->cur = position;
1.71 - return;
1.72 - }
1.73 - unwindSlow(position);
1.74 - }
1.75 -
1.76 - Object **make(Object *obj) {
1.77 - assert(scope_);
1.78 - if (blocks_->cur + 1 > blocks_->end) {
1.79 - if (!grow())
1.80 - return NULL;
1.81 - }
1.82 - Object **ptr = blocks_->cur;
1.83 - blocks_->cur++;
1.84 - *ptr = obj;
1.85 - return ptr;
1.86 - }
1.87 -
1.88 - Ke::HandleScope **scope() {
1.89 - return &scope_;
1.90 - }
1.91 -
1.92 - void accept(VirtualObjectVisitor *visitor);
1.93 -
1.94 - private:
1.95 - Block *blocks_;
1.96 - Block first_;
1.97 - Ke::HandleScope *scope_;
1.98 -};
1.99 -
1.100 -class RootScope : public Ke::HandleScope
1.101 -{
1.102 - public:
1.103 - // These are implemented in Zone.h.
1.104 - inline RootScope(Zone *zone);
1.105 -};
1.106 -
1.107 -}
1.108 -
1.109 -#endif // _include_jc_root_scope_h_
1.110 +/* vim: set ts=4 sw=4 tw=99 et:
1.111 + *
1.112 + * Copyright (C) 2012 David Anderson
1.113 + *
1.114 + * This file is part of SourcePawn.
1.115 + *
1.116 + * SourcePawn is free software: you can redistribute it and/or modify it under
1.117 + * the terms of the GNU General Public License as published by the Free
1.118 + * Software Foundation, either version 3 of the License, or (at your option)
1.119 + * any later version.
1.120 + *
1.121 + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
1.122 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.123 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.124 + *
1.125 + * You should have received a copy of the GNU General Public License along with
1.126 + * SourcePawn. If not, see http://www.gnu.org/licenses/.
1.127 + */
1.128 +#ifndef _include_jc_root_scopes_h_
1.129 +#define _include_jc_root_scopes_h_
1.130 +
1.131 +#include "../include/keima.h"
1.132 +#include "Objects.h"
1.133 +#include "VirtualObjectVisitor.h"
1.134 +
1.135 +namespace ke {
1.136 +
1.137 +class RootScope;
1.138 +
1.139 +// A RootScopeManager is essentially a specialized PoolAllocator that can
1.140 +// only allocate Object ** entries. It is used to allow huge, temporary
1.141 +// heap data structures like ASTs to hold safe references to objects. When
1.142 +// the data structures is no longer needed, all of the roots are removed
1.143 +// at once.
1.144 +class RootScopeManager
1.145 +{
1.146 + static const size_t kObjectsPerBlock = 4096;
1.147 +
1.148 + struct Block {
1.149 + Object *objects[kObjectsPerBlock];
1.150 + Object **cur;
1.151 + Object **end;
1.152 + Block *prev;
1.153 +
1.154 + Block(Block *prev)
1.155 + : cur(objects),
1.156 + end(objects + kObjectsPerBlock),
1.157 + prev(prev)
1.158 + {
1.159 + }
1.160 + };
1.161 +
1.162 + private:
1.163 + bool grow();
1.164 + void unwindSlow(Object **position);
1.165 +
1.166 + public:
1.167 + RootScopeManager();
1.168 + ~RootScopeManager();
1.169 +
1.170 + Object **position() const {
1.171 + return blocks_->cur;
1.172 + }
1.173 +
1.174 + void unwind(Object **position) {
1.175 + if (position >= blocks_->cur && position <= blocks_->end) {
1.176 + blocks_->cur = position;
1.177 + return;
1.178 + }
1.179 + unwindSlow(position);
1.180 + }
1.181 +
1.182 + Object **make(Object *obj) {
1.183 + assert(scope_);
1.184 + if (blocks_->cur + 1 > blocks_->end) {
1.185 + if (!grow())
1.186 + return NULL;
1.187 + }
1.188 + Object **ptr = blocks_->cur;
1.189 + blocks_->cur++;
1.190 + *ptr = obj;
1.191 + return ptr;
1.192 + }
1.193 +
1.194 + Ke::HandleScope **scope() {
1.195 + return &scope_;
1.196 + }
1.197 +
1.198 + void accept(VirtualObjectVisitor *visitor);
1.199 +
1.200 + private:
1.201 + Block *blocks_;
1.202 + Block first_;
1.203 + Ke::HandleScope *scope_;
1.204 +};
1.205 +
1.206 +class RootScope : public Ke::HandleScope
1.207 +{
1.208 + public:
1.209 + // These are implemented in Zone.h.
1.210 + inline RootScope(Zone *zone);
1.211 +};
1.212 +
1.213 +}
1.214 +
1.215 +#endif // _include_jc_root_scope_h_