| author | Asher Baker <asherkin@gmail.com> |
| Mon Jun 10 01:04:43 2013 +0100 (2013-06-10) | |
| changeset 263 | ba85a47ee414 |
| parent 261 | 219b49bf4a03 |
| permissions | -rw-r--r-- |
| asherkin@263 | 1 |
/* vim: set ts=4 sw=4 tw=99 et: |
| asherkin@263 | 2 |
* |
| asherkin@263 | 3 |
* Copyright (C) 2012 David Anderson |
| asherkin@263 | 4 |
* |
| asherkin@263 | 5 |
* This file is part of SourcePawn. |
| asherkin@263 | 6 |
* |
| asherkin@263 | 7 |
* SourcePawn is free software: you can redistribute it and/or modify it under |
| asherkin@263 | 8 |
* the terms of the GNU General Public License as published by the Free |
| asherkin@263 | 9 |
* Software Foundation, either version 3 of the License, or (at your option) |
| asherkin@263 | 10 |
* any later version. |
| asherkin@263 | 11 |
* |
| asherkin@263 | 12 |
* SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY |
| asherkin@263 | 13 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| asherkin@263 | 14 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| asherkin@263 | 15 |
* |
| asherkin@263 | 16 |
* You should have received a copy of the GNU General Public License along with |
| asherkin@263 | 17 |
* SourcePawn. If not, see http://www.gnu.org/licenses/. |
| asherkin@263 | 18 |
*/ |
| asherkin@263 | 19 |
#ifndef _include_jc_root_scopes_h_ |
| asherkin@263 | 20 |
#define _include_jc_root_scopes_h_ |
| asherkin@263 | 21 |
|
| asherkin@263 | 22 |
#include "../include/keima.h" |
| asherkin@263 | 23 |
#include "Objects.h" |
| asherkin@263 | 24 |
#include "VirtualObjectVisitor.h" |
| asherkin@263 | 25 |
|
| asherkin@263 | 26 |
namespace ke { |
| asherkin@263 | 27 |
|
| asherkin@263 | 28 |
class RootScope; |
| asherkin@263 | 29 |
|
| asherkin@263 | 30 |
// A RootScopeManager is essentially a specialized PoolAllocator that can |
| asherkin@263 | 31 |
// only allocate Object ** entries. It is used to allow huge, temporary |
| asherkin@263 | 32 |
// heap data structures like ASTs to hold safe references to objects. When |
| asherkin@263 | 33 |
// the data structures is no longer needed, all of the roots are removed |
| asherkin@263 | 34 |
// at once. |
| asherkin@263 | 35 |
class RootScopeManager |
| asherkin@263 | 36 |
{ |
| asherkin@263 | 37 |
static const size_t kObjectsPerBlock = 4096; |
| asherkin@263 | 38 |
|
| asherkin@263 | 39 |
struct Block { |
| asherkin@263 | 40 |
Object *objects[kObjectsPerBlock]; |
| asherkin@263 | 41 |
Object **cur; |
| asherkin@263 | 42 |
Object **end; |
| asherkin@263 | 43 |
Block *prev; |
| asherkin@263 | 44 |
|
| asherkin@263 | 45 |
Block(Block *prev) |
| asherkin@263 | 46 |
: cur(objects), |
| asherkin@263 | 47 |
end(objects + kObjectsPerBlock), |
| asherkin@263 | 48 |
prev(prev) |
| asherkin@263 | 49 |
{ |
| asherkin@263 | 50 |
} |
| asherkin@263 | 51 |
}; |
| asherkin@263 | 52 |
|
| asherkin@263 | 53 |
private: |
| asherkin@263 | 54 |
bool grow(); |
| asherkin@263 | 55 |
void unwindSlow(Object **position); |
| asherkin@263 | 56 |
|
| asherkin@263 | 57 |
public: |
| asherkin@263 | 58 |
RootScopeManager(); |
| asherkin@263 | 59 |
~RootScopeManager(); |
| asherkin@263 | 60 |
|
| asherkin@263 | 61 |
Object **position() const { |
| asherkin@263 | 62 |
return blocks_->cur; |
| asherkin@263 | 63 |
} |
| asherkin@263 | 64 |
|
| asherkin@263 | 65 |
void unwind(Object **position) { |
| asherkin@263 | 66 |
if (position >= blocks_->cur && position <= blocks_->end) { |
| asherkin@263 | 67 |
blocks_->cur = position; |
| asherkin@263 | 68 |
return; |
| asherkin@263 | 69 |
} |
| asherkin@263 | 70 |
unwindSlow(position); |
| asherkin@263 | 71 |
} |
| asherkin@263 | 72 |
|
| asherkin@263 | 73 |
Object **make(Object *obj) { |
| asherkin@263 | 74 |
assert(scope_); |
| asherkin@263 | 75 |
if (blocks_->cur + 1 > blocks_->end) { |
| asherkin@263 | 76 |
if (!grow()) |
| asherkin@263 | 77 |
return NULL; |
| asherkin@263 | 78 |
} |
| asherkin@263 | 79 |
Object **ptr = blocks_->cur; |
| asherkin@263 | 80 |
blocks_->cur++; |
| asherkin@263 | 81 |
*ptr = obj; |
| asherkin@263 | 82 |
return ptr; |
| asherkin@263 | 83 |
} |
| asherkin@263 | 84 |
|
| asherkin@263 | 85 |
Ke::HandleScope **scope() { |
| asherkin@263 | 86 |
return &scope_; |
| asherkin@263 | 87 |
} |
| asherkin@263 | 88 |
|
| asherkin@263 | 89 |
void accept(VirtualObjectVisitor *visitor); |
| asherkin@263 | 90 |
|
| asherkin@263 | 91 |
private: |
| asherkin@263 | 92 |
Block *blocks_; |
| asherkin@263 | 93 |
Block first_; |
| asherkin@263 | 94 |
Ke::HandleScope *scope_; |
| asherkin@263 | 95 |
}; |
| asherkin@263 | 96 |
|
| asherkin@263 | 97 |
class RootScope : public Ke::HandleScope |
| asherkin@263 | 98 |
{ |
| asherkin@263 | 99 |
public: |
| asherkin@263 | 100 |
// These are implemented in Zone.h. |
| asherkin@263 | 101 |
inline RootScope(Zone *zone); |
| asherkin@263 | 102 |
}; |
| asherkin@263 | 103 |
|
| asherkin@263 | 104 |
} |
| asherkin@263 | 105 |
|
| asherkin@263 | 106 |
#endif // _include_jc_root_scope_h_ |