1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  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 #include "precompiled.hpp"
  26 #include "memory/allocation.inline.hpp"
  27 #include "oops/constantPool.hpp"
  28 #include "oops/oop.inline.hpp"
  29 #include "runtime/atomic.hpp"
  30 #include "runtime/handles.inline.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 
  33 bool Handle::operator == (oop o) const {
  34   return oopDesc::equals(obj(), o);
  35 }
  36 
  37 bool Handle::operator == (const Handle& h) const {
  38   return oopDesc::equals(obj(), h.obj());
  39 }
  40 
  41 #ifdef ASSERT
  42 oop* HandleArea::allocate_handle(oop obj) {
  43   assert(_handle_mark_nesting > 1, "memory leak: allocating handle outside HandleMark");
  44   assert(_no_handle_mark_nesting == 0, "allocating handle inside NoHandleMark");
  45   assert(oopDesc::is_oop(obj), "not an oop: " INTPTR_FORMAT, p2i(obj));
  46   return real_allocate_handle(obj);
  47 }
  48 #endif
  49 
  50 // Copy constructors and destructors for metadata handles
  51 // These do too much to inline.
  52 #define DEF_METADATA_HANDLE_FN_NOINLINE(name, type) \
  53 name##Handle::name##Handle(const name##Handle &h) {                    \
  54   _value = h._value;                                                   \
  55   if (_value != NULL) {                                                \
  56     assert(_value->is_valid(), "obj is valid");                        \
  57     if (h._thread != NULL) {                                           \
  58       assert(h._thread == Thread::current(), "thread must be current");\
  59       _thread = h._thread;                                             \
  60     } else {                                                           \
  61       _thread = Thread::current();                                     \
  62     }                                                                  \
  63     assert (_thread->is_in_stack((address)this), "not on stack?");     \
  64     _thread->metadata_handles()->push((Metadata*)_value);              \
  65   } else {                                                             \
  66     _thread = NULL;                                                    \
  67   }                                                                    \
  68 }                                                                      \
  69 name##Handle& name##Handle::operator=(const name##Handle &s) {         \
  70   remove();                                                            \
  71   _value = s._value;                                                   \
  72   if (_value != NULL) {                                                \
  73     assert(_value->is_valid(), "obj is valid");                        \
  74     if (s._thread != NULL) {                                           \
  75       assert(s._thread == Thread::current(), "thread must be current");\
  76       _thread = s._thread;                                             \
  77     } else {                                                           \
  78       _thread = Thread::current();                                     \
  79     }                                                                  \
  80     assert (_thread->is_in_stack((address)this), "not on stack?");     \
  81     _thread->metadata_handles()->push((Metadata*)_value);              \
  82   } else {                                                             \
  83     _thread = NULL;                                                    \
  84   }                                                                    \
  85   return *this;                                                        \
  86 }                                                                      \
  87 inline void name##Handle::remove() {                                   \
  88   if (_value != NULL) {                                                \
  89     int i = _thread->metadata_handles()->find_from_end((Metadata*)_value); \
  90     assert(i!=-1, "not in metadata_handles list");                     \
  91     _thread->metadata_handles()->remove_at(i);                         \
  92   }                                                                    \
  93 }                                                                      \
  94 name##Handle::~name##Handle () { remove(); }                           \
  95 
  96 DEF_METADATA_HANDLE_FN_NOINLINE(method, Method)
  97 DEF_METADATA_HANDLE_FN_NOINLINE(constantPool, ConstantPool)
  98 
  99 
 100 static uintx chunk_oops_do(OopClosure* f, Chunk* chunk, char* chunk_top) {
 101   oop* bottom = (oop*) chunk->bottom();
 102   oop* top    = (oop*) chunk_top;
 103   uintx handles_visited = top - bottom;
 104   assert(top >= bottom && top <= (oop*) chunk->top(), "just checking");
 105   // during GC phase 3, a handle may be a forward pointer that
 106   // is not yet valid, so loosen the assertion
 107   while (bottom < top) {
 108     f->do_oop(bottom++);
 109   }
 110   return handles_visited;
 111 }
 112 
 113 // Used for debugging handle allocation.
 114 NOT_PRODUCT(jint _nof_handlemarks  = 0;)
 115 
 116 void HandleArea::oops_do(OopClosure* f) {
 117   uintx handles_visited = 0;
 118   // First handle the current chunk. It is filled to the high water mark.
 119   handles_visited += chunk_oops_do(f, _chunk, _hwm);
 120   // Then handle all previous chunks. They are completely filled.
 121   Chunk* k = _first;
 122   while(k != _chunk) {
 123     handles_visited += chunk_oops_do(f, k, k->top());
 124     k = k->next();
 125   }
 126 
 127   if (_prev != NULL) _prev->oops_do(f);
 128 }
 129 
 130 void HandleMark::initialize(Thread* thread) {
 131   _thread = thread;
 132   // Save area
 133   _area  = thread->handle_area();
 134   // Save current top
 135   _chunk = _area->_chunk;
 136   _hwm   = _area->_hwm;
 137   _max   = _area->_max;
 138   _size_in_bytes = _area->_size_in_bytes;
 139   debug_only(_area->_handle_mark_nesting++);
 140   assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks");
 141   debug_only(Atomic::inc(&_nof_handlemarks);)
 142 
 143   // Link this in the thread
 144   set_previous_handle_mark(thread->last_handle_mark());
 145   thread->set_last_handle_mark(this);
 146 }
 147 
 148 
 149 HandleMark::~HandleMark() {
 150   HandleArea* area = _area;   // help compilers with poor alias analysis
 151   assert(area == _thread->handle_area(), "sanity check");
 152   assert(area->_handle_mark_nesting > 0, "must stack allocate HandleMarks" );
 153   debug_only(area->_handle_mark_nesting--);
 154 
 155   // Delete later chunks
 156   if( _chunk->next() ) {
 157     // reset arena size before delete chunks. Otherwise, the total
 158     // arena size could exceed total chunk size
 159     assert(area->size_in_bytes() > size_in_bytes(), "Sanity check");
 160     area->set_size_in_bytes(size_in_bytes());
 161     _chunk->next_chop();
 162   } else {
 163     assert(area->size_in_bytes() == size_in_bytes(), "Sanity check");
 164   }
 165   // Roll back arena to saved top markers
 166   area->_chunk = _chunk;
 167   area->_hwm = _hwm;
 168   area->_max = _max;
 169 #ifdef ASSERT
 170   // clear out first chunk (to detect allocation bugs)
 171   if (ZapVMHandleArea) {
 172     memset(_hwm, badHandleValue, _max - _hwm);
 173   }
 174   Atomic::dec(&_nof_handlemarks);
 175 #endif
 176 
 177   // Unlink this from the thread
 178   _thread->set_last_handle_mark(previous_handle_mark());
 179 }
 180 
 181 void* HandleMark::operator new(size_t size) throw() {
 182   return AllocateHeap(size, mtThread);
 183 }
 184 
 185 void* HandleMark::operator new [] (size_t size) throw() {
 186   return AllocateHeap(size, mtThread);
 187 }
 188 
 189 void HandleMark::operator delete(void* p) {
 190   FreeHeap(p);
 191 }
 192 
 193 void HandleMark::operator delete[](void* p) {
 194   FreeHeap(p);
 195 }
 196 
 197 #ifdef ASSERT
 198 
 199 NoHandleMark::NoHandleMark() {
 200   HandleArea* area = Thread::current()->handle_area();
 201   area->_no_handle_mark_nesting++;
 202   assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
 203 }
 204 
 205 
 206 NoHandleMark::~NoHandleMark() {
 207   HandleArea* area = Thread::current()->handle_area();
 208   assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
 209   area->_no_handle_mark_nesting--;
 210 }
 211 
 212 
 213 ResetNoHandleMark::ResetNoHandleMark() {
 214   HandleArea* area = Thread::current()->handle_area();
 215   _no_handle_mark_nesting = area->_no_handle_mark_nesting;
 216   area->_no_handle_mark_nesting = 0;
 217 }
 218 
 219 
 220 ResetNoHandleMark::~ResetNoHandleMark() {
 221   HandleArea* area = Thread::current()->handle_area();
 222   area->_no_handle_mark_nesting = _no_handle_mark_nesting;
 223 }
 224 
 225 #endif // ASSERT