< prev index next >

src/hotspot/share/oops/symbolHandle.hpp

Print this page

 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_OOPS_SYMBOLHANDLE_HPP
 26 #define SHARE_OOPS_SYMBOLHANDLE_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "oops/symbol.hpp"
 30 










 31 // TempNewSymbol acts as a handle class in a handle/body idiom and is
 32 // responsible for proper resource management of the body (which is a Symbol*).
 33 // The body is resource managed by a reference counting scheme.
 34 // TempNewSymbol can therefore be used to properly hold a newly created or referenced
 35 // Symbol* temporarily in scope.
 36 //
 37 // Routines in SymbolTable will initialize the reference count of a Symbol* before
 38 // it becomes "managed" by TempNewSymbol instances. As a handle class, TempNewSymbol
 39 // needs to maintain proper reference counting in context of copy semantics.
 40 //
 41 // In SymbolTable, new_symbol() will create a Symbol* if not already in the
 42 // symbol table and add to the symbol's reference count.
 43 // probe() and lookup_only() will increment the refcount if symbol is found.
 44 template <bool TEMP>
 45 class SymbolHandleBase : public StackObj {
 46   Symbol* _temp;
 47 
 48 public:
 49   SymbolHandleBase() : _temp(nullptr) { }
 50 
 51   // Conversion from a Symbol* to a SymbolHandleBase.
 52   // Does not increment the current reference count if temporary.
 53   SymbolHandleBase(Symbol *s) : _temp(s) {
 54     if (!TEMP) {
 55       Symbol::maybe_increment_refcount(_temp);








 56     }
 57   }
 58 
 59   // Copy constructor increments reference count.
 60   SymbolHandleBase(const SymbolHandleBase& rhs) : _temp(rhs._temp) {
 61     Symbol::maybe_increment_refcount(_temp);
 62   }
 63 
 64   // Assignment operator uses a c++ trick called copy and swap idiom.
 65   // rhs is passed by value so within the scope of this method it is a copy.
 66   // At method exit it contains the former value of _temp, triggering the correct refcount
 67   // decrement upon destruction.
 68   void operator=(SymbolHandleBase rhs) {
 69     Symbol* tmp = rhs._temp;
 70     rhs._temp = _temp;
 71     _temp = tmp;
 72   }
 73 
 74   // Decrement reference counter so it can go away if it's unused
 75   ~SymbolHandleBase() {

 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_OOPS_SYMBOLHANDLE_HPP
 26 #define SHARE_OOPS_SYMBOLHANDLE_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "oops/symbol.hpp"
 30 
 31 class TempSymbolCleanupDelayer : AllStatic {
 32   static Symbol* volatile _queue[];
 33   static volatile uint _index;
 34 
 35 public:
 36   static const uint QueueSize = 128;
 37   static void delay_cleanup(Symbol* s);
 38   static void drain_queue();
 39 };
 40 
 41 // TempNewSymbol acts as a handle class in a handle/body idiom and is
 42 // responsible for proper resource management of the body (which is a Symbol*).
 43 // The body is resource managed by a reference counting scheme.
 44 // TempNewSymbol can therefore be used to properly hold a newly created or referenced
 45 // Symbol* temporarily in scope.
 46 //
 47 // Routines in SymbolTable will initialize the reference count of a Symbol* before
 48 // it becomes "managed" by TempNewSymbol instances. As a handle class, TempNewSymbol
 49 // needs to maintain proper reference counting in context of copy semantics.
 50 //
 51 // In SymbolTable, new_symbol() will create a Symbol* if not already in the
 52 // symbol table and add to the symbol's reference count.
 53 // probe() and lookup_only() will increment the refcount if symbol is found.
 54 template <bool TEMP>
 55 class SymbolHandleBase : public StackObj {
 56   Symbol* _temp;
 57 
 58 public:
 59   SymbolHandleBase() : _temp(nullptr) { }
 60 
 61   // Conversion from a Symbol* to a SymbolHandleBase.

 62   SymbolHandleBase(Symbol *s) : _temp(s) {
 63     if (!TEMP) {
 64       Symbol::maybe_increment_refcount(_temp);
 65       return;
 66     }
 67 
 68     // Delay cleanup for temp symbols. Refcount is incremented while in
 69     // queue. But don't requeue existing entries, or entries that are held
 70     // elsewhere - it's a waste of effort.
 71     if (s != nullptr && s->refcount() == 1) {
 72       TempSymbolCleanupDelayer::delay_cleanup(s);
 73     }
 74   }
 75 
 76   // Copy constructor increments reference count.
 77   SymbolHandleBase(const SymbolHandleBase& rhs) : _temp(rhs._temp) {
 78     Symbol::maybe_increment_refcount(_temp);
 79   }
 80 
 81   // Assignment operator uses a c++ trick called copy and swap idiom.
 82   // rhs is passed by value so within the scope of this method it is a copy.
 83   // At method exit it contains the former value of _temp, triggering the correct refcount
 84   // decrement upon destruction.
 85   void operator=(SymbolHandleBase rhs) {
 86     Symbol* tmp = rhs._temp;
 87     rhs._temp = _temp;
 88     _temp = tmp;
 89   }
 90 
 91   // Decrement reference counter so it can go away if it's unused
 92   ~SymbolHandleBase() {
< prev index next >