src/share/vm/runtime/handles.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 6876
710a3c8b516e
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "memory/allocation.inline.hpp"
aoqi@0 27 #include "oops/constantPool.hpp"
aoqi@0 28 #include "oops/oop.inline.hpp"
aoqi@0 29 #include "runtime/handles.inline.hpp"
aoqi@0 30 #include "runtime/thread.inline.hpp"
aoqi@0 31 #ifdef TARGET_OS_FAMILY_linux
aoqi@0 32 # include "os_linux.inline.hpp"
aoqi@0 33 #endif
aoqi@0 34 #ifdef TARGET_OS_FAMILY_solaris
aoqi@0 35 # include "os_solaris.inline.hpp"
aoqi@0 36 #endif
aoqi@0 37 #ifdef TARGET_OS_FAMILY_windows
aoqi@0 38 # include "os_windows.inline.hpp"
aoqi@0 39 #endif
aoqi@0 40 #ifdef TARGET_OS_FAMILY_bsd
aoqi@0 41 # include "os_bsd.inline.hpp"
aoqi@0 42 #endif
aoqi@0 43
aoqi@0 44 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 45
aoqi@0 46 #ifdef ASSERT
aoqi@0 47 oop* HandleArea::allocate_handle(oop obj) {
aoqi@0 48 assert(_handle_mark_nesting > 1, "memory leak: allocating handle outside HandleMark");
aoqi@0 49 assert(_no_handle_mark_nesting == 0, "allocating handle inside NoHandleMark");
aoqi@0 50 assert(obj->is_oop(), err_msg("not an oop: " INTPTR_FORMAT, (intptr_t*) obj));
aoqi@0 51 return real_allocate_handle(obj);
aoqi@0 52 }
aoqi@0 53
aoqi@0 54 Handle::Handle(Thread* thread, oop obj) {
aoqi@0 55 assert(thread == Thread::current(), "sanity check");
aoqi@0 56 if (obj == NULL) {
aoqi@0 57 _handle = NULL;
aoqi@0 58 } else {
aoqi@0 59 _handle = thread->handle_area()->allocate_handle(obj);
aoqi@0 60 }
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 #endif
aoqi@0 64
aoqi@0 65 static uintx chunk_oops_do(OopClosure* f, Chunk* chunk, char* chunk_top) {
aoqi@0 66 oop* bottom = (oop*) chunk->bottom();
aoqi@0 67 oop* top = (oop*) chunk_top;
aoqi@0 68 uintx handles_visited = top - bottom;
aoqi@0 69 assert(top >= bottom && top <= (oop*) chunk->top(), "just checking");
aoqi@0 70 // during GC phase 3, a handle may be a forward pointer that
aoqi@0 71 // is not yet valid, so loosen the assertion
aoqi@0 72 while (bottom < top) {
aoqi@0 73 // This test can be moved up but for now check every oop.
aoqi@0 74
aoqi@0 75 assert((*bottom)->is_oop(), "handle should point to oop");
aoqi@0 76
aoqi@0 77 f->do_oop(bottom++);
aoqi@0 78 }
aoqi@0 79 return handles_visited;
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 // Used for debugging handle allocation.
aoqi@0 83 NOT_PRODUCT(jint _nof_handlemarks = 0;)
aoqi@0 84
aoqi@0 85 void HandleArea::oops_do(OopClosure* f) {
aoqi@0 86 uintx handles_visited = 0;
aoqi@0 87 // First handle the current chunk. It is filled to the high water mark.
aoqi@0 88 handles_visited += chunk_oops_do(f, _chunk, _hwm);
aoqi@0 89 // Then handle all previous chunks. They are completely filled.
aoqi@0 90 Chunk* k = _first;
aoqi@0 91 while(k != _chunk) {
aoqi@0 92 handles_visited += chunk_oops_do(f, k, k->top());
aoqi@0 93 k = k->next();
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 // The thread local handle areas should not get very large
aoqi@0 97 if (TraceHandleAllocation && handles_visited > TotalHandleAllocationLimit) {
aoqi@0 98 #ifdef ASSERT
aoqi@0 99 warning("%d: Visited in HandleMark : %d",
aoqi@0 100 _nof_handlemarks, handles_visited);
aoqi@0 101 #else
aoqi@0 102 warning("Visited in HandleMark : %d", handles_visited);
aoqi@0 103 #endif
aoqi@0 104 }
aoqi@0 105 if (_prev != NULL) _prev->oops_do(f);
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 void HandleMark::initialize(Thread* thread) {
aoqi@0 109 _thread = thread;
aoqi@0 110 // Save area
aoqi@0 111 _area = thread->handle_area();
aoqi@0 112 // Save current top
aoqi@0 113 _chunk = _area->_chunk;
aoqi@0 114 _hwm = _area->_hwm;
aoqi@0 115 _max = _area->_max;
aoqi@0 116 _size_in_bytes = _area->_size_in_bytes;
aoqi@0 117 debug_only(_area->_handle_mark_nesting++);
aoqi@0 118 assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks");
aoqi@0 119 debug_only(Atomic::inc(&_nof_handlemarks);)
aoqi@0 120
aoqi@0 121 // Link this in the thread
aoqi@0 122 set_previous_handle_mark(thread->last_handle_mark());
aoqi@0 123 thread->set_last_handle_mark(this);
aoqi@0 124 }
aoqi@0 125
aoqi@0 126
aoqi@0 127 HandleMark::~HandleMark() {
aoqi@0 128 HandleArea* area = _area; // help compilers with poor alias analysis
aoqi@0 129 assert(area == _thread->handle_area(), "sanity check");
aoqi@0 130 assert(area->_handle_mark_nesting > 0, "must stack allocate HandleMarks" );
aoqi@0 131 debug_only(area->_handle_mark_nesting--);
aoqi@0 132
aoqi@0 133 // Debug code to trace the number of handles allocated per mark/
aoqi@0 134 #ifdef ASSERT
aoqi@0 135 if (TraceHandleAllocation) {
aoqi@0 136 size_t handles = 0;
aoqi@0 137 Chunk *c = _chunk->next();
aoqi@0 138 if (c == NULL) {
aoqi@0 139 handles = area->_hwm - _hwm; // no new chunk allocated
aoqi@0 140 } else {
aoqi@0 141 handles = _max - _hwm; // add rest in first chunk
aoqi@0 142 while(c != NULL) {
aoqi@0 143 handles += c->length();
aoqi@0 144 c = c->next();
aoqi@0 145 }
aoqi@0 146 handles -= area->_max - area->_hwm; // adjust for last trunk not full
aoqi@0 147 }
aoqi@0 148 handles /= sizeof(void *); // Adjust for size of a handle
aoqi@0 149 if (handles > HandleAllocationLimit) {
aoqi@0 150 // Note: _nof_handlemarks is only set in debug mode
aoqi@0 151 warning("%d: Allocated in HandleMark : %d", _nof_handlemarks, handles);
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 tty->print_cr("Handles %d", handles);
aoqi@0 155 }
aoqi@0 156 #endif
aoqi@0 157
aoqi@0 158 // Delete later chunks
aoqi@0 159 if( _chunk->next() ) {
aoqi@0 160 // reset arena size before delete chunks. Otherwise, the total
aoqi@0 161 // arena size could exceed total chunk size
aoqi@0 162 assert(area->size_in_bytes() > size_in_bytes(), "Sanity check");
aoqi@0 163 area->set_size_in_bytes(size_in_bytes());
aoqi@0 164 _chunk->next_chop();
aoqi@0 165 } else {
aoqi@0 166 assert(area->size_in_bytes() == size_in_bytes(), "Sanity check");
aoqi@0 167 }
aoqi@0 168 // Roll back arena to saved top markers
aoqi@0 169 area->_chunk = _chunk;
aoqi@0 170 area->_hwm = _hwm;
aoqi@0 171 area->_max = _max;
aoqi@0 172 #ifdef ASSERT
aoqi@0 173 // clear out first chunk (to detect allocation bugs)
aoqi@0 174 if (ZapVMHandleArea) {
aoqi@0 175 memset(_hwm, badHandleValue, _max - _hwm);
aoqi@0 176 }
aoqi@0 177 Atomic::dec(&_nof_handlemarks);
aoqi@0 178 #endif
aoqi@0 179
aoqi@0 180 // Unlink this from the thread
aoqi@0 181 _thread->set_last_handle_mark(previous_handle_mark());
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 void* HandleMark::operator new(size_t size) throw() {
aoqi@0 185 return AllocateHeap(size, mtThread);
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 void* HandleMark::operator new [] (size_t size) throw() {
aoqi@0 189 return AllocateHeap(size, mtThread);
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 void HandleMark::operator delete(void* p) {
aoqi@0 193 FreeHeap(p, mtThread);
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 void HandleMark::operator delete[](void* p) {
aoqi@0 197 FreeHeap(p, mtThread);
aoqi@0 198 }
aoqi@0 199
aoqi@0 200 #ifdef ASSERT
aoqi@0 201
aoqi@0 202 NoHandleMark::NoHandleMark() {
aoqi@0 203 HandleArea* area = Thread::current()->handle_area();
aoqi@0 204 area->_no_handle_mark_nesting++;
aoqi@0 205 assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
aoqi@0 206 }
aoqi@0 207
aoqi@0 208
aoqi@0 209 NoHandleMark::~NoHandleMark() {
aoqi@0 210 HandleArea* area = Thread::current()->handle_area();
aoqi@0 211 assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
aoqi@0 212 area->_no_handle_mark_nesting--;
aoqi@0 213 }
aoqi@0 214
aoqi@0 215
aoqi@0 216 ResetNoHandleMark::ResetNoHandleMark() {
aoqi@0 217 HandleArea* area = Thread::current()->handle_area();
aoqi@0 218 _no_handle_mark_nesting = area->_no_handle_mark_nesting;
aoqi@0 219 area->_no_handle_mark_nesting = 0;
aoqi@0 220 }
aoqi@0 221
aoqi@0 222
aoqi@0 223 ResetNoHandleMark::~ResetNoHandleMark() {
aoqi@0 224 HandleArea* area = Thread::current()->handle_area();
aoqi@0 225 area->_no_handle_mark_nesting = _no_handle_mark_nesting;
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 #endif

mercurial