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