1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/jitcraft/RootScopes.h Tue Jul 03 00:21:37 2012 -0700
1.3 @@ -0,0 +1,107 @@
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_jc_root_scopes_h_
1.23 +#define _include_jc_root_scopes_h_
1.24 +
1.25 +#include "Objects.h"
1.26 +
1.27 +namespace jc {
1.28 +
1.29 +class RootScope;
1.30 +
1.31 +// A RootScopeManager is essentially a specialized PoolAllocator that can
1.32 +// only allocate Object ** entries. It is used to allow huge, temporary
1.33 +// heap data structures like ASTs to hold safe references to objects. When
1.34 +// the data structures is no longer needed, all of the roots are removed
1.35 +// at once.
1.36 +class RootScopeManager
1.37 +{
1.38 + static const size_t kObjectsPerBlock = 4096;
1.39 +
1.40 + struct Block {
1.41 + Object *objects[kObjectsPerBlock];
1.42 + Object **cur;
1.43 + Object **end;
1.44 + Block *prev;
1.45 +
1.46 + Block(Block *prev)
1.47 + : cur(objects),
1.48 + end(objects + kObjectsPerBlock),
1.49 + prev(prev)
1.50 + {
1.51 + }
1.52 + };
1.53 +
1.54 + private:
1.55 + bool grow();
1.56 + void unwindSlow(Object **position);
1.57 +
1.58 + public:
1.59 + RootScopeManager();
1.60 + ~RootScopeManager();
1.61 +
1.62 + Object **position() const {
1.63 + return blocks_->cur;
1.64 + }
1.65 +
1.66 + void unwind(Object **position) {
1.67 + if (position >= blocks_->cur && position <= blocks_->end) {
1.68 + blocks_->cur = position;
1.69 + return;
1.70 + }
1.71 + unwindSlow(position);
1.72 + }
1.73 +
1.74 + Object **make(Object *obj) {
1.75 + assert(scope_);
1.76 + if (blocks_->cur + 1 > blocks_->end) {
1.77 + if (!grow())
1.78 + return NULL;
1.79 + }
1.80 + Object **ptr = blocks_->cur;
1.81 + blocks_->cur++;
1.82 + *ptr = obj;
1.83 + return ptr;
1.84 + }
1.85 +
1.86 + RootScope **scope() {
1.87 + return &scope_;
1.88 + }
1.89 +
1.90 + private:
1.91 + Block *blocks_;
1.92 + Block first_;
1.93 + RootScope *scope_;
1.94 +};
1.95 +
1.96 +class RootScope
1.97 +{
1.98 + Zone *zone_;
1.99 + Object **position_;
1.100 + RootScope *prev_;
1.101 +
1.102 + public:
1.103 + // These are implemented in Zone.h.
1.104 + inline RootScope(Zone *zone);
1.105 + inline ~RootScope();
1.106 +};
1.107 +
1.108 +}
1.109 +
1.110 +#endif // _include_jc_root_scope_h_