src/share/vm/runtime/handles.cpp

Wed, 09 Apr 2008 15:10:22 -0700

author
rasbold
date
Wed, 09 Apr 2008 15:10:22 -0700
changeset 544
9f4457a14b58
parent 435
a61af66fc99e
child 1907
c18cbe5936b8
permissions
-rw-r--r--

Merge

duke@435 1 /*
duke@435 2 * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_handles.cpp.incl"
duke@435 27
duke@435 28 #ifdef ASSERT
duke@435 29 oop* HandleArea::allocate_handle(oop obj) {
duke@435 30 assert(_handle_mark_nesting > 1, "memory leak: allocating handle outside HandleMark");
duke@435 31 assert(_no_handle_mark_nesting == 0, "allocating handle inside NoHandleMark");
duke@435 32 assert(SharedSkipVerify || obj->is_oop(), "sanity check");
duke@435 33 return real_allocate_handle(obj);
duke@435 34 }
duke@435 35
duke@435 36 Handle::Handle(Thread* thread, oop obj) {
duke@435 37 assert(thread == Thread::current(), "sanity check");
duke@435 38 if (obj == NULL) {
duke@435 39 _handle = NULL;
duke@435 40 } else {
duke@435 41 _handle = thread->handle_area()->allocate_handle(obj);
duke@435 42 }
duke@435 43 }
duke@435 44
duke@435 45 #endif
duke@435 46
duke@435 47 static uintx chunk_oops_do(OopClosure* f, Chunk* chunk, char* chunk_top) {
duke@435 48 oop* bottom = (oop*) chunk->bottom();
duke@435 49 oop* top = (oop*) chunk_top;
duke@435 50 uintx handles_visited = top - bottom;
duke@435 51 assert(top >= bottom && top <= (oop*) chunk->top(), "just checking");
duke@435 52 // during GC phase 3, a handle may be a forward pointer that
duke@435 53 // is not yet valid, so loosen the assertion
duke@435 54 while (bottom < top) {
duke@435 55 // assert((*bottom)->is_oop(), "handle should point to oop");
duke@435 56 assert(Universe::heap()->is_in(*bottom), "handle should be valid heap address");
duke@435 57 f->do_oop(bottom++);
duke@435 58 }
duke@435 59 return handles_visited;
duke@435 60 }
duke@435 61
duke@435 62 // Used for debugging handle allocation.
duke@435 63 NOT_PRODUCT(jint _nof_handlemarks = 0;)
duke@435 64
duke@435 65 void HandleArea::oops_do(OopClosure* f) {
duke@435 66 uintx handles_visited = 0;
duke@435 67 // First handle the current chunk. It is filled to the high water mark.
duke@435 68 handles_visited += chunk_oops_do(f, _chunk, _hwm);
duke@435 69 // Then handle all previous chunks. They are completely filled.
duke@435 70 Chunk* k = _first;
duke@435 71 while(k != _chunk) {
duke@435 72 handles_visited += chunk_oops_do(f, k, k->top());
duke@435 73 k = k->next();
duke@435 74 }
duke@435 75
duke@435 76 // The thread local handle areas should not get very large
duke@435 77 if (TraceHandleAllocation && handles_visited > TotalHandleAllocationLimit) {
duke@435 78 #ifdef ASSERT
duke@435 79 warning("%d: Visited in HandleMark : %d",
duke@435 80 _nof_handlemarks, handles_visited);
duke@435 81 #else
duke@435 82 warning("Visited in HandleMark : %d", handles_visited);
duke@435 83 #endif
duke@435 84 }
duke@435 85 if (_prev != NULL) _prev->oops_do(f);
duke@435 86 }
duke@435 87
duke@435 88 void HandleMark::initialize(Thread* thread) {
duke@435 89 _thread = thread;
duke@435 90 // Save area
duke@435 91 _area = thread->handle_area();
duke@435 92 // Save current top
duke@435 93 _chunk = _area->_chunk;
duke@435 94 _hwm = _area->_hwm;
duke@435 95 _max = _area->_max;
duke@435 96 NOT_PRODUCT(_size_in_bytes = _area->_size_in_bytes;)
duke@435 97 debug_only(_area->_handle_mark_nesting++);
duke@435 98 assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks");
duke@435 99 debug_only(Atomic::inc(&_nof_handlemarks);)
duke@435 100
duke@435 101 // Link this in the thread
duke@435 102 set_previous_handle_mark(thread->last_handle_mark());
duke@435 103 thread->set_last_handle_mark(this);
duke@435 104 }
duke@435 105
duke@435 106
duke@435 107 HandleMark::~HandleMark() {
duke@435 108 HandleArea* area = _area; // help compilers with poor alias analysis
duke@435 109 assert(area == _thread->handle_area(), "sanity check");
duke@435 110 assert(area->_handle_mark_nesting > 0, "must stack allocate HandleMarks" );
duke@435 111 debug_only(area->_handle_mark_nesting--);
duke@435 112
duke@435 113 // Debug code to trace the number of handles allocated per mark/
duke@435 114 #ifdef ASSERT
duke@435 115 if (TraceHandleAllocation) {
duke@435 116 size_t handles = 0;
duke@435 117 Chunk *c = _chunk->next();
duke@435 118 if (c == NULL) {
duke@435 119 handles = area->_hwm - _hwm; // no new chunk allocated
duke@435 120 } else {
duke@435 121 handles = _max - _hwm; // add rest in first chunk
duke@435 122 while(c != NULL) {
duke@435 123 handles += c->length();
duke@435 124 c = c->next();
duke@435 125 }
duke@435 126 handles -= area->_max - area->_hwm; // adjust for last trunk not full
duke@435 127 }
duke@435 128 handles /= sizeof(void *); // Adjust for size of a handle
duke@435 129 if (handles > HandleAllocationLimit) {
duke@435 130 // Note: _nof_handlemarks is only set in debug mode
duke@435 131 warning("%d: Allocated in HandleMark : %d", _nof_handlemarks, handles);
duke@435 132 }
duke@435 133 }
duke@435 134 #endif
duke@435 135
duke@435 136 // Delete later chunks
duke@435 137 if( _chunk->next() ) {
duke@435 138 _chunk->next_chop();
duke@435 139 }
duke@435 140 // Roll back arena to saved top markers
duke@435 141 area->_chunk = _chunk;
duke@435 142 area->_hwm = _hwm;
duke@435 143 area->_max = _max;
duke@435 144 NOT_PRODUCT(area->set_size_in_bytes(_size_in_bytes);)
duke@435 145 #ifdef ASSERT
duke@435 146 // clear out first chunk (to detect allocation bugs)
duke@435 147 if (ZapVMHandleArea) {
duke@435 148 memset(_hwm, badHandleValue, _max - _hwm);
duke@435 149 }
duke@435 150 Atomic::dec(&_nof_handlemarks);
duke@435 151 #endif
duke@435 152
duke@435 153 // Unlink this from the thread
duke@435 154 _thread->set_last_handle_mark(previous_handle_mark());
duke@435 155 }
duke@435 156
duke@435 157 #ifdef ASSERT
duke@435 158
duke@435 159 NoHandleMark::NoHandleMark() {
duke@435 160 HandleArea* area = Thread::current()->handle_area();
duke@435 161 area->_no_handle_mark_nesting++;
duke@435 162 assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
duke@435 163 }
duke@435 164
duke@435 165
duke@435 166 NoHandleMark::~NoHandleMark() {
duke@435 167 HandleArea* area = Thread::current()->handle_area();
duke@435 168 assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
duke@435 169 area->_no_handle_mark_nesting--;
duke@435 170 }
duke@435 171
duke@435 172
duke@435 173 ResetNoHandleMark::ResetNoHandleMark() {
duke@435 174 HandleArea* area = Thread::current()->handle_area();
duke@435 175 _no_handle_mark_nesting = area->_no_handle_mark_nesting;
duke@435 176 area->_no_handle_mark_nesting = 0;
duke@435 177 }
duke@435 178
duke@435 179
duke@435 180 ResetNoHandleMark::~ResetNoHandleMark() {
duke@435 181 HandleArea* area = Thread::current()->handle_area();
duke@435 182 area->_no_handle_mark_nesting = _no_handle_mark_nesting;
duke@435 183 }
duke@435 184
duke@435 185 #endif

mercurial