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