// TempNewSymbol acts as a handle class in a handle/body idiom and is // responsible for proper resource management of the body (which is a Symbol*). // The body is resource managed by a reference counting scheme. // TempNewSymbol can therefore be used to properly hold a newly created or referenced // Symbol* temporarily in scope. // // Routines in SymbolTable will initialize the reference count of a Symbol* before // it becomes "managed" by TempNewSymbol instances. As a handle class, TempNewSymbol // needs to maintain proper reference counting in context of copy semantics. // // In SymbolTable, new_symbol() will create a Symbol* if not already in the // symbol table and add to the symbol's reference count. // probe() and lookup_only() will increment the refcount if symbol is found. template <bool TEMP> class SymbolHandleBase : public StackObj {
Symbol* _temp;
public:
SymbolHandleBase() : _temp(NULL) { }
// Conversion from a Symbol* to a SymbolHandleBase. // Does not increment the current reference count if temporary.
SymbolHandleBase(Symbol *s) : _temp(s) { if (!TEMP) {
assert(s != nullptr, "must not be null");
s->increment_refcount();
}
}
// Assignment operator uses a c++ trick called copy and swap idiom. // rhs is passed by value so within the scope of this method it is a copy. // At method exit it contains the former value of _temp, triggering the correct refcount // decrement upon destruction. voidoperator=(SymbolHandleBase rhs) {
Symbol* tmp = rhs._temp;
rhs._temp = _temp;
_temp = tmp;
}
// Decrement reference counter so it can go away if it's unused
~SymbolHandleBase() {
Symbol::maybe_decrement_refcount(_temp);
}
// TempNewSymbol is a temporary holder for a newly created symbol using TempNewSymbol = SymbolHandleBase<true>;
// SymbolHandle is a non-temp symbol used to hold a symbol in a semi permanent place, // like in a hashtable. The only difference is that the constructor increments the refcount. using SymbolHandle = SymbolHandleBase<false>;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.