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