1 /*
   2  * Copyright (c) 1997, 2010, 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/oop.inline.hpp"
  28 #include "runtime/handles.inline.hpp"
  29 #ifdef TARGET_OS_FAMILY_linux
  30 # include "os_linux.inline.hpp"
  31 # include "thread_linux.inline.hpp"
  32 #endif
  33 #ifdef TARGET_OS_FAMILY_solaris
  34 # include "os_solaris.inline.hpp"
  35 # include "thread_solaris.inline.hpp"
  36 #endif
  37 #ifdef TARGET_OS_FAMILY_windows
  38 # include "os_windows.inline.hpp"
  39 # include "thread_windows.inline.hpp"
  40 #endif
  41 
  42 #ifdef ASSERT
  43 oop* HandleArea::allocate_handle(oop obj) {
  44   assert(_handle_mark_nesting > 1, "memory leak: allocating handle outside HandleMark");
  45   assert(_no_handle_mark_nesting == 0, "allocating handle inside NoHandleMark");
  46   assert(SharedSkipVerify || obj->is_oop(), "sanity check");
  47   return real_allocate_handle(obj);
  48 }
  49 
  50 Handle::Handle(Thread* thread, oop obj) {
  51   assert(thread == Thread::current(), "sanity check");
  52   if (obj == NULL) {
  53     _handle = NULL;
  54   } else {
  55     _handle = thread->handle_area()->allocate_handle(obj);
  56   }
  57 }
  58 
  59 #endif
  60 
  61 static uintx chunk_oops_do(OopClosure* f, Chunk* chunk, char* chunk_top) {
  62   oop* bottom = (oop*) chunk->bottom();
  63   oop* top    = (oop*) chunk_top;
  64   uintx handles_visited = top - bottom;
  65   assert(top >= bottom && top <= (oop*) chunk->top(), "just checking");
  66   // during GC phase 3, a handle may be a forward pointer that
  67   // is not yet valid, so loosen the assertion
  68   while (bottom < top) {
  69 //    assert((*bottom)->is_oop(), "handle should point to oop");
  70       assert(Universe::heap()->is_in(*bottom), "handle should be valid heap address");
  71     f->do_oop(bottom++);
  72   }
  73   return handles_visited;
  74 }
  75 
  76 // Used for debugging handle allocation.
  77 NOT_PRODUCT(jint _nof_handlemarks  = 0;)
  78 
  79 void HandleArea::oops_do(OopClosure* f) {
  80   uintx handles_visited = 0;
  81   // First handle the current chunk. It is filled to the high water mark.
  82   handles_visited += chunk_oops_do(f, _chunk, _hwm);
  83   // Then handle all previous chunks. They are completely filled.
  84   Chunk* k = _first;
  85   while(k != _chunk) {
  86     handles_visited += chunk_oops_do(f, k, k->top());
  87     k = k->next();
  88   }
  89 
  90   // The thread local handle areas should not get very large
  91   if (TraceHandleAllocation && handles_visited > TotalHandleAllocationLimit) {
  92 #ifdef ASSERT
  93     warning("%d: Visited in HandleMark : %d",
  94       _nof_handlemarks, handles_visited);
  95 #else
  96     warning("Visited in HandleMark : %d", handles_visited);
  97 #endif
  98   }
  99   if (_prev != NULL) _prev->oops_do(f);
 100 }
 101 
 102 void HandleMark::initialize(Thread* thread) {
 103   _thread = thread;
 104   // Save area
 105   _area  = thread->handle_area();
 106   // Save current top
 107   _chunk = _area->_chunk;
 108   _hwm   = _area->_hwm;
 109   _max   = _area->_max;
 110   NOT_PRODUCT(_size_in_bytes = _area->_size_in_bytes;)
 111   debug_only(_area->_handle_mark_nesting++);
 112   assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks");
 113   debug_only(Atomic::inc(&_nof_handlemarks);)
 114 
 115   // Link this in the thread
 116   set_previous_handle_mark(thread->last_handle_mark());
 117   thread->set_last_handle_mark(this);
 118 }
 119 
 120 
 121 HandleMark::~HandleMark() {
 122   HandleArea* area = _area;   // help compilers with poor alias analysis
 123   assert(area == _thread->handle_area(), "sanity check");
 124   assert(area->_handle_mark_nesting > 0, "must stack allocate HandleMarks" );
 125   debug_only(area->_handle_mark_nesting--);
 126 
 127   // Debug code to trace the number of handles allocated per mark/
 128 #ifdef ASSERT
 129   if (TraceHandleAllocation) {
 130     size_t handles = 0;
 131     Chunk *c = _chunk->next();
 132     if (c == NULL) {
 133       handles = area->_hwm - _hwm; // no new chunk allocated
 134     } else {
 135       handles = _max - _hwm;      // add rest in first chunk
 136       while(c != NULL) {
 137         handles += c->length();
 138         c = c->next();
 139       }
 140       handles -= area->_max - area->_hwm; // adjust for last trunk not full
 141     }
 142     handles /= sizeof(void *); // Adjust for size of a handle
 143     if (handles > HandleAllocationLimit) {
 144       // Note: _nof_handlemarks is only set in debug mode
 145       warning("%d: Allocated in HandleMark : %d", _nof_handlemarks, handles);
 146     }
 147   }
 148 #endif
 149 
 150   // Delete later chunks
 151   if( _chunk->next() ) {
 152     _chunk->next_chop();
 153   }
 154   // Roll back arena to saved top markers
 155   area->_chunk = _chunk;
 156   area->_hwm = _hwm;
 157   area->_max = _max;
 158   NOT_PRODUCT(area->set_size_in_bytes(_size_in_bytes);)
 159 #ifdef ASSERT
 160   // clear out first chunk (to detect allocation bugs)
 161   if (ZapVMHandleArea) {
 162     memset(_hwm, badHandleValue, _max - _hwm);
 163   }
 164   Atomic::dec(&_nof_handlemarks);
 165 #endif
 166 
 167   // Unlink this from the thread
 168   _thread->set_last_handle_mark(previous_handle_mark());
 169 }
 170 
 171 #ifdef ASSERT
 172 
 173 NoHandleMark::NoHandleMark() {
 174   HandleArea* area = Thread::current()->handle_area();
 175   area->_no_handle_mark_nesting++;
 176   assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
 177 }
 178 
 179 
 180 NoHandleMark::~NoHandleMark() {
 181   HandleArea* area = Thread::current()->handle_area();
 182   assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
 183   area->_no_handle_mark_nesting--;
 184 }
 185 
 186 
 187 ResetNoHandleMark::ResetNoHandleMark() {
 188   HandleArea* area = Thread::current()->handle_area();
 189   _no_handle_mark_nesting = area->_no_handle_mark_nesting;
 190   area->_no_handle_mark_nesting = 0;
 191 }
 192 
 193 
 194 ResetNoHandleMark::~ResetNoHandleMark() {
 195   HandleArea* area = Thread::current()->handle_area();
 196   area->_no_handle_mark_nesting = _no_handle_mark_nesting;
 197 }
 198 
 199 #endif